Salome HOME
Synchronize adm files
[modules/gui.git] / src / GraphicsView / GraphicsView_Viewer.cxx
1 // Copyright (C) 2013-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GraphicsView_Viewer.h"
21
22 #include "GraphicsView_Object.h"
23 #include "GraphicsView_Selector.h"
24 #include "GraphicsView_Scene.h"
25 #include "GraphicsView_ViewFrame.h"
26 #include "GraphicsView_ViewPort.h"
27 #include "GraphicsView_ViewTransformer.h"
28
29 #include <SUIT_ViewManager.h>
30
31 #include <ImageComposer_Image.h>
32
33 #include <QApplication>
34 #include <QColorDialog>
35 #include <QGraphicsSceneMouseEvent>
36 #include <QGraphicsSceneWheelEvent>
37 #include <QKeyEvent>
38 #include <QMenu>
39
40 // testing ImageViewer
41 /*
42 #include "GraphicsView_PrsImage.h"
43 #include "GraphicsView_PrsPropDlg.h"
44 #include <QFileDialog>
45 */
46
47 //=======================================================================
48 // Name    : GraphicsView_Viewer
49 // Purpose : Constructor
50 //=======================================================================
51 GraphicsView_Viewer::GraphicsView_Viewer( const QString& title, QWidget* widget )
52 : SUIT_ViewModel(),
53   mySelector( 0 ),
54   myTransformer( 0 ),
55   myWidget( widget ),
56   myIsInitialized( false )
57 {
58 }
59
60 //=======================================================================
61 // Name    : GraphicsView_Viewer
62 // Purpose : Destructor
63 //=======================================================================
64 GraphicsView_Viewer::~GraphicsView_Viewer()
65 {
66   delete mySelector;
67 }
68
69 //================================================================
70 // Function : createView
71 // Purpose  : 
72 //================================================================
73 SUIT_ViewWindow* GraphicsView_Viewer::createView( SUIT_Desktop* theDesktop )
74 {
75   GraphicsView_ViewFrame* aViewFrame = new GraphicsView_ViewFrame( theDesktop, this, myWidget );
76
77   connect( aViewFrame, SIGNAL( keyPressed( QKeyEvent* ) ),
78            this, SLOT( onKeyEvent( QKeyEvent* ) ) );
79
80   connect( aViewFrame, SIGNAL( keyReleased( QKeyEvent* ) ),
81            this, SLOT( onKeyEvent( QKeyEvent* ) ) );
82
83   connect( aViewFrame, SIGNAL( mousePressed( QGraphicsSceneMouseEvent* ) ),
84            this, SLOT( onMouseEvent( QGraphicsSceneMouseEvent* ) ) );
85
86   connect( aViewFrame, SIGNAL( mouseMoving( QGraphicsSceneMouseEvent* ) ),
87            this, SLOT( onMouseEvent( QGraphicsSceneMouseEvent* ) ) );
88
89   connect( aViewFrame, SIGNAL( mouseReleased( QGraphicsSceneMouseEvent* ) ),
90            this, SLOT( onMouseEvent( QGraphicsSceneMouseEvent* ) ) );
91
92   connect( aViewFrame, SIGNAL( wheeling( QGraphicsSceneWheelEvent* ) ),
93            this, SLOT( onWheelEvent( QGraphicsSceneWheelEvent* ) ) );
94
95   connect( aViewFrame, SIGNAL( sketchingFinished( QPainterPath ) ),
96            this, SLOT( onSketchingFinished( QPainterPath ) ) );
97
98   return aViewFrame;
99 }
100
101 //================================================================
102 // Function : contextMenuPopup
103 // Purpose  : 
104 //================================================================
105 void GraphicsView_Viewer::contextMenuPopup( QMenu* thePopup )
106 {
107   if( thePopup->actions().count() > 0 )
108     thePopup->addSeparator();
109
110   // testing ImageViewer
111   /*
112   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
113   {
114     int aNbSelected = aViewPort->nbSelected();
115     if( aNbSelected == 0 )
116     {
117       thePopup->addAction( tr( "ADD_IMAGE" ), this, SLOT( onAddImage() ) );
118       thePopup->addSeparator();
119
120       thePopup->addAction( tr( "TEST_FUSE_OPERATOR" ), this, SLOT( onTestFuseOperator() ) );
121       thePopup->addAction( tr( "TEST_CROP_OPERATOR" ), this, SLOT( onTestCropOperatorPrepare() ) );
122     }
123     else
124     {
125       thePopup->addAction( tr( "BRING_TO_FRONT" ), this, SLOT( onBringToFront() ) );
126       thePopup->addAction( tr( "SEND_TO_BACK" ), this, SLOT( onSendToBack() ) );
127       thePopup->addAction( tr( "BRING_FORWARD" ), this, SLOT( onBringForward() ) );
128       thePopup->addAction( tr( "SEND_BACKWARD" ), this, SLOT( onSendBackward() ) );
129       thePopup->addSeparator();
130
131       if( aNbSelected == 1 )
132       {
133         thePopup->addAction( tr( "PROPERTIES" ), this, SLOT( onPrsProperties() ) );
134         thePopup->addSeparator();
135       }
136
137       thePopup->addAction( tr( "REMOVE_IMAGES" ), this, SLOT( onRemoveImages() ) );
138     }
139     thePopup->addSeparator();
140   }
141   */
142
143   thePopup->addAction( tr( "CHANGE_BGCOLOR" ), this, SLOT( onChangeBgColor() ) );
144 }
145
146 //================================================================
147 // Function : getSelector
148 // Purpose  : 
149 //================================================================
150 GraphicsView_Selector* GraphicsView_Viewer::getSelector()
151 {
152   if( !mySelector )
153   {
154     mySelector = new GraphicsView_Selector( this );
155     if( mySelector )
156     {
157       connect( mySelector, SIGNAL( selSelectionDone( GV_SelectionChangeStatus ) ),
158                this, SLOT( onSelectionDone( GV_SelectionChangeStatus ) ) );
159       connect( mySelector, SIGNAL( selSelectionCancel() ),
160                this, SLOT( onSelectionCancel() ) );
161     }
162   }
163   return mySelector;
164 }
165
166 //================================================================
167 // Function : getActiveView
168 // Purpose  : 
169 //================================================================
170 GraphicsView_ViewFrame* GraphicsView_Viewer::getActiveView() const
171 {
172   if( SUIT_ViewManager* aViewManager = getViewManager() )
173     return dynamic_cast<GraphicsView_ViewFrame*>( aViewManager->getActiveView() );
174   return NULL;
175 }
176
177 //================================================================
178 // Function : getActiveViewPort
179 // Purpose  : 
180 //================================================================
181 GraphicsView_ViewPort* GraphicsView_Viewer::getActiveViewPort() const
182 {
183   if( GraphicsView_ViewFrame* aViewFrame = getActiveView() )
184     return aViewFrame->getViewPort();
185   return NULL;
186 }
187
188 //================================================================
189 // Function : getActiveScene
190 // Purpose  : 
191 //================================================================
192 GraphicsView_Scene* GraphicsView_Viewer::getActiveScene() const
193 {
194   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
195     return dynamic_cast<GraphicsView_Scene*>( aViewPort->scene() );
196   return NULL;
197 }
198
199 //================================================================
200 // Function : activateTransform
201 // Purpose  : 
202 //================================================================
203 void GraphicsView_Viewer::activateTransform( int theType )
204 {
205   if( theType == NoTransform ) // finish current transform
206   {
207     if ( myTransformer )
208     {
209       onTransformationFinished();
210       delete myTransformer;
211       myTransformer = 0;
212     }
213   }
214   else // activate new transform
215   {
216     activateTransform( NoTransform );
217     myTransformer = createTransformer( theType );
218     onTransformationStarted();
219     myTransformer->exec();
220   }
221 }
222
223 //================================================================
224 // Function : setIsInitialized
225 // Purpose  : 
226 //================================================================
227 void GraphicsView_Viewer::setIsInitialized( bool theFlag )
228 {
229   myIsInitialized = theFlag;
230 }
231
232 //================================================================
233 // Function : createTransformer
234 // Purpose  : 
235 //================================================================
236 GraphicsView_ViewTransformer* GraphicsView_Viewer::createTransformer( int theType )
237 {
238   return new GraphicsView_ViewTransformer( this, theType );
239 }
240
241 //================================================================
242 // Function : onTransformationStarted
243 // Purpose  : 
244 //================================================================
245 void GraphicsView_Viewer::onTransformationStarted()
246 {
247   if( GraphicsView_Selector* aSelector = getSelector() )
248   {
249     aSelector->undetectAll();
250     aSelector->lock( true ); // disable selection
251   }
252
253   // watch events: any mouse/key event outside the
254   // viewport will be considered as the end of transform
255   if( myTransformer )
256     qApp->installEventFilter( this );
257 }
258
259 //================================================================
260 // Function : onTransformationFinished
261 // Purpose  : 
262 //================================================================
263 void GraphicsView_Viewer::onTransformationFinished()
264 {
265   if( GraphicsView_Selector* aSelector = getSelector() )
266     aSelector->lock( false ); // enable selection
267
268   // stop watching events
269   if( myTransformer )
270     qApp->removeEventFilter( this );
271 }
272
273 //================================================================
274 // Function : onKeyEvent
275 // Purpose  : 
276 //================================================================
277 void GraphicsView_Viewer::onKeyEvent( QKeyEvent* e )
278 {
279   switch( e->type() )
280   {
281     case QEvent::KeyPress:
282       handleKeyPress( e );
283       break;
284     case QEvent::KeyRelease:
285       handleKeyRelease( e );
286       break;
287     default: break;
288   }
289 }
290
291 //================================================================
292 // Function : onMouseEvent
293 // Purpose  : 
294 //================================================================
295 void GraphicsView_Viewer::onMouseEvent( QGraphicsSceneMouseEvent* e )
296 {
297   switch( e->type() )
298   {
299     case QEvent::GraphicsSceneMousePress:
300       handleMousePress( e );
301       break;
302     case QEvent::GraphicsSceneMouseMove:
303       handleMouseMove( e );
304       break;
305     case QEvent::GraphicsSceneMouseRelease:
306       handleMouseRelease( e );
307       break;
308     default: break;
309   }
310 }
311
312 //================================================================
313 // Function : onWheelEvent
314 // Purpose  : 
315 //================================================================
316 void GraphicsView_Viewer::onWheelEvent( QGraphicsSceneWheelEvent* e )
317 {
318   switch( e->type() )
319   {
320     case QEvent::GraphicsSceneWheel:
321       handleWheel( e );
322       break;
323     default: break;
324   }
325 }
326
327 //================================================================
328 // Function : handleKeyPress
329 // Purpose  : 
330 //================================================================
331 void GraphicsView_Viewer::handleKeyPress( QKeyEvent* e )
332 {
333   if( e->key() == Qt::Key_Escape )
334   {
335     // Cancel current operation
336     bool anIsCancelled = false;
337     if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
338     {
339       anIsCancelled = aViewPort->cancelCurrentOperation();
340
341       // Unselect all objects (if there is no operation to cancel)
342       if( !anIsCancelled )
343       {
344         aViewPort->finishSelectByRect();
345         aViewPort->clearSelected();
346       }
347     }
348
349     // Emit unselection signal
350     if( !anIsCancelled )
351       if( GraphicsView_Selector* aSelector = getSelector() )
352         aSelector->unselectAll();
353   }
354 }
355
356 //================================================================
357 // Function : handleKeyRelease
358 // Purpose  : 
359 //================================================================
360 void GraphicsView_Viewer::handleKeyRelease( QKeyEvent* e )
361 {
362 }
363
364 //================================================================
365 // Function : handleMousePress
366 // Purpose  : 
367 //================================================================
368 void GraphicsView_Viewer::handleMousePress( QGraphicsSceneMouseEvent* e )
369 {
370   // test accel for transforms
371   if ( e->modifiers() & GraphicsView_ViewTransformer::accelKey() )
372   {
373     Qt::MouseButton bs = e->button();
374     if ( bs == GraphicsView_ViewTransformer::zoomButton() )
375       activateTransform( Zoom );
376     else if ( bs == GraphicsView_ViewTransformer::panButton() )
377       activateTransform( Pan );
378   }
379   else // checking for other operations before selection in release event
380   {
381     if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
382     {
383       bool append = bool ( e->modifiers() & GraphicsView_Selector::getAppendKey() );
384       if( e->button() == Qt::LeftButton &&
385           aViewPort->hasInteractionFlag( GraphicsView_ViewPort::Sketching ) &&
386           aViewPort->isPrepareToSketch() )
387       {
388         // Use 'append' flag for sketching by arbitrary path
389         aViewPort->startSketching( e->scenePos(), append );
390       }
391       else if( e->button() == Qt::LeftButton &&
392                aViewPort->hasInteractionFlag( GraphicsView_ViewPort::Pulling ) &&
393                !aViewPort->isSelectByRect() && 
394                !aViewPort->isDragging() &&
395                aViewPort->startPulling( e->scenePos() ) )
396       {
397         // Try to start pulling if rectangular selection is performed
398         aViewPort->finishSelectByRect();
399       }
400       else if( e->button() == Qt::LeftButton &&
401                !( aViewPort->currentBlock() & GraphicsView_ViewPort::BS_Selection ) &&
402                !aViewPort->getHighlightedObject() )
403       {
404         // Start rectangular selection if pulling was not started
405         QPoint p = aViewPort->mapFromScene( e->scenePos() );
406         aViewPort->startSelectByRect( p.x(), p.y() );
407       }
408       else if( e->button() != Qt::MidButton && !append &&
409                aViewPort->hasInteractionFlag( GraphicsView_ViewPort::ImmediateSelection ) &&
410                aViewPort->nbSelected() < 2 )
411       {
412         // Do not perform 'immediate selection' if the multiple objects are already selected
413         getSelector()->select( QRectF(), append );
414       }
415       else if( e->button() == Qt::RightButton &&
416                aViewPort->hasInteractionFlag( GraphicsView_ViewPort::ImmediateContextMenu ) &&
417                aViewPort->nbSelected() < 1 )
418       {
419         // If the 'immediate context menu' mode is enabled,
420         // try to perform selection before invoking context menu
421         getSelector()->select( QRectF(), append );
422       }
423     }
424   }
425 }
426
427 //================================================================
428 // Function : handleMouseMove
429 // Purpose  : 
430 //================================================================
431 void GraphicsView_Viewer::handleMouseMove( QGraphicsSceneMouseEvent* e )
432 {
433   GraphicsView_ViewPort* aViewPort = getActiveViewPort();
434
435   // highlight for selection
436   bool anIsDragged = ( e->buttons() & ( Qt::LeftButton | Qt::MidButton | Qt::RightButton ) );
437   bool anIsPrepareToSketch = aViewPort && aViewPort->isPrepareToSketch();
438   if ( !anIsDragged && !anIsPrepareToSketch )
439   {
440     if ( getSelector() )
441       getSelector()->detect( e->scenePos().x(), e->scenePos().y() );
442   }
443
444   // try to activate other operations
445   if( aViewPort )
446   {
447     if( aViewPort->isPulling() )
448     {
449       aViewPort->drawPulling( e->scenePos() );
450     }
451     else if( aViewPort->isSketching() )
452     {
453       aViewPort->drawSketching( e->scenePos() );
454     }
455     else if( e->button() == Qt::LeftButton &&
456              aViewPort->hasInteractionFlag( GraphicsView_ViewPort::Pulling ) &&
457              !aViewPort->isSelectByRect() &&
458              !aViewPort->isDragging() &&
459              aViewPort->startPulling( e->scenePos() ) )
460     {
461       aViewPort->finishSelectByRect();
462     }
463     else if( !aViewPort->getHighlightedObject() )
464     {
465       QPoint p = aViewPort->mapFromScene( e->scenePos() );
466       aViewPort->drawSelectByRect( p.x(), p.y() );
467     }
468   }
469 }
470
471 //================================================================
472 // Function : handleMouseRelease
473 // Purpose  : 
474 //================================================================
475 void GraphicsView_Viewer::handleMouseRelease( QGraphicsSceneMouseEvent* e )
476 {
477   // selection
478   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
479   {
480     if( e->button() == Qt::LeftButton &&
481         !( aViewPort->currentBlock() & GraphicsView_ViewPort::BS_Selection ) )
482     {
483       if ( getSelector() )
484       {
485         bool append = bool ( e->modifiers() & GraphicsView_Selector::getAppendKey() );
486         getSelector()->select( QRectF(), append );
487       }
488     }
489   }
490
491   // try to finish active operations
492   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
493   {
494     if( aViewPort->isPulling() )
495     {
496       aViewPort->finishPulling( true );
497     }
498     else if( aViewPort->isSketching() )
499     {
500       aViewPort->finishSketching( true );
501     }
502     else if( !aViewPort->getHighlightedObject() )
503     {
504       QRect aSelRect = aViewPort->selectionRect();
505       aViewPort->finishSelectByRect();
506       if ( getSelector() && !aSelRect.isNull() )
507       {            
508         bool append = bool ( e->modifiers() & GraphicsView_Selector::getAppendKey() );
509         QRectF aRect = aViewPort->mapToScene( aSelRect ).boundingRect();
510         getSelector()->select( aRect, append );
511       }
512     }
513   }
514 }
515
516 //================================================================
517 // Function : handleWheel
518 // Purpose  : 
519 //================================================================
520 void GraphicsView_Viewer::handleWheel( QGraphicsSceneWheelEvent* e )
521 {
522   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
523   {
524     if( aViewPort->hasInteractionFlag( GraphicsView_ViewPort::WheelScaling ) )
525     {
526       bool anIsScaleUp = e->delta() > 0;
527       bool anIsCtrl = e->modifiers() & Qt::ControlModifier;
528
529       bool anIsScaleChanged = false;
530       for( aViewPort->initSelected(); aViewPort->moreSelected(); aViewPort->nextSelected() )
531         if( GraphicsView_Object* anObject = aViewPort->selectedObject() )
532           anIsScaleChanged = anObject->updateScale( anIsScaleUp, anIsCtrl ) || anIsScaleChanged;
533
534       if( anIsScaleChanged )
535       {
536         emit wheelScaleChanged();
537         aViewPort->onBoundingRectChanged();
538       }
539     }
540   }
541 }
542
543 //================================================================
544 // Function : onSketchingFinished
545 // Purpose  : 
546 //================================================================
547 void GraphicsView_Viewer::onSketchingFinished( QPainterPath thePath )
548 {
549   // testing ImageViewer
550   //onTestCropOperatorPerform( thePath );
551 }
552
553 //================================================================
554 // Function : onSelectionDone
555 // Purpose  : 
556 //================================================================
557 void GraphicsView_Viewer::onSelectionDone( GV_SelectionChangeStatus theStatus )
558 {
559   emit selectionChanged( theStatus );
560 }
561
562 //================================================================
563 // Function : onChangeBgColor
564 // Purpose  : 
565 //================================================================
566 void GraphicsView_Viewer::onChangeBgColor()
567 {
568   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
569   {
570     QColor aColor = aViewPort->isForegroundEnabled() ?
571       aViewPort->foregroundColor() : aViewPort->backgroundColor();
572     aColor = QColorDialog::getColor( aColor, aViewPort );       
573     if ( aColor.isValid() )
574     {
575       if( aViewPort->isForegroundEnabled() )
576       {
577         aViewPort->setForegroundColor( aColor );
578         aViewPort->updateForeground();
579       }
580       else
581         aViewPort->setBackgroundColor( aColor );
582     }
583   }
584 }
585
586 //================================================================
587 // Function : onSelectionCancel
588 // Purpose  : 
589 //================================================================
590 void GraphicsView_Viewer::onSelectionCancel()
591 {
592   emit selectionChanged( GVSCS_Invalid );
593 }
594
595 /*
596 //================================================================
597 // Function : onAddImage
598 // Purpose  : 
599 //================================================================
600 void GraphicsView_Viewer::onAddImage()
601 {
602   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
603   {
604     QString aFileName = QFileDialog::getOpenFileName();
605     if( aFileName.isEmpty() )
606       return;
607
608     GraphicsView_PrsImage* aPrs = new GraphicsView_PrsImage();
609
610     QImage anImage( aFileName );
611     aPrs->setImage( anImage );
612
613     aPrs->compute();
614
615     aViewPort->addItem( aPrs );
616     aViewPort->fitAll();
617   }
618 }
619
620 //================================================================
621 // Function : onRemoveImages
622 // Purpose  : 
623 //================================================================
624 void GraphicsView_Viewer::onRemoveImages()
625 {
626   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
627   {
628     GraphicsView_ObjectListIterator anIter( aViewPort->getSelectedObjects() );
629     while( anIter.hasNext() )
630     {
631       if( GraphicsView_PrsImage* aPrs = dynamic_cast<GraphicsView_PrsImage*>( anIter.next() ) )
632       {
633         aViewPort->removeItem( aPrs );
634         delete aPrs;
635       }
636     }
637   }
638 }
639
640 //================================================================
641 // Function : onBringToFront
642 // Purpose  : 
643 //================================================================
644 void GraphicsView_Viewer::onBringToFront()
645 {
646   processQueueOperation( BringToFront );
647 }
648
649 //================================================================
650 // Function : onSendToBack
651 // Purpose  : 
652 //================================================================
653 void GraphicsView_Viewer::onSendToBack()
654 {
655   processQueueOperation( SendToBack );
656 }
657
658 //================================================================
659 // Function : onBringForward
660 // Purpose  : 
661 //================================================================
662 void GraphicsView_Viewer::onBringForward()
663 {
664   processQueueOperation( BringForward );
665 }
666
667 //================================================================
668 // Function : onSendBackward
669 // Purpose  : 
670 //================================================================
671 void GraphicsView_Viewer::onSendBackward()
672 {
673   processQueueOperation( SendBackward );
674 }
675
676 //================================================================
677 // Function : processQueueOperation
678 // Purpose  : 
679 //================================================================
680 void GraphicsView_Viewer::processQueueOperation( const QueueOperation theOperation )
681 {
682   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
683   {
684     const GraphicsView_ObjectList& aSelectedList = aViewPort->getSelectedObjects();
685
686     GraphicsView_ObjectList aSortedList;
687
688     GraphicsView_ObjectList aList = aViewPort->getObjects();
689     GraphicsView_ObjectListIterator anIter( aList );
690     while( anIter.hasNext() )
691     {
692       if( GraphicsView_Object* anObject = anIter.next() )
693       {
694         if( !anObject->hasSpecificZValue() )
695         {
696           double aZValue = anObject->zValue();
697           GraphicsView_ObjectList::iterator anIter1, anIter1End = aSortedList.end();
698           for( anIter1 = aSortedList.begin(); anIter1 != anIter1End; anIter1++ )
699             if( GraphicsView_Object* anObjectRef = *anIter1 )
700               if( !anObjectRef->hasSpecificZValue() && anObjectRef->zValue() > aZValue )
701                 break;
702           aSortedList.insert( anIter1, anObject );
703         }
704       }
705     }
706
707     QList<int> anIndicesToMove;
708
709     int anIndex = 0;
710     anIter = aSortedList;
711     while( anIter.hasNext() )
712     {
713       if( GraphicsView_Object* anObject = anIter.next() )
714         if( aSelectedList.contains( anObject ) )
715           anIndicesToMove.append( anIndex );
716       anIndex++;
717     }
718
719     bool anIsReverse = theOperation == BringToFront || theOperation == BringForward;
720     QListIterator<int> anIndicesIter( anIndicesToMove );
721     if( anIsReverse )
722       anIndicesIter.toBack();
723
724     int aShiftForMultiple = 0;
725     int anObjectCount = aSortedList.count();
726     while( anIsReverse ? anIndicesIter.hasPrevious() : anIndicesIter.hasNext() )
727     {
728       int anIndex = anIsReverse ? anIndicesIter.previous() : anIndicesIter.next();
729       int aNewIndex = anIndex;
730       switch( theOperation )
731       {
732         case BringToFront: aNewIndex = anObjectCount - 1 - aShiftForMultiple; break;
733         case SendToBack:   aNewIndex = aShiftForMultiple; break;
734         case BringForward: aNewIndex = anIndex + 1; break;
735         case SendBackward: aNewIndex = anIndex - 1; break;
736       }
737       aShiftForMultiple++;
738
739       if( aNewIndex < 0 || aNewIndex > anObjectCount - 1 )
740         break;
741
742       aSortedList.move( anIndex, aNewIndex );
743     }
744
745     double aZValue = 1.0;
746     anIter = aSortedList;
747     while( anIter.hasNext() )
748     {
749       if( GraphicsView_Object* anObject = anIter.next() )
750       {
751         anObject->setZValue( aZValue );
752         aZValue += 1.0;
753       }
754     }
755   }
756 }
757
758 //================================================================
759 // Function : onPrsProperties
760 // Purpose  : 
761 //================================================================
762 void GraphicsView_Viewer::onPrsProperties()
763 {
764   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
765   {
766     aViewPort->initSelected();
767     if( GraphicsView_Object* anObject = aViewPort->selectedObject() )
768     {
769       if( GraphicsView_PrsImage* aPrs = dynamic_cast<GraphicsView_PrsImage*>( anObject ) )
770       {
771         double aPosX, aPosY, aScaleX, aScaleY, aRotationAngle;
772         aPrs->getPosition( aPosX, aPosY );
773         aPrs->getScaling( aScaleX, aScaleY );
774         aPrs->getRotationAngle( aRotationAngle );
775
776         double aZValue = aPrs->zValue();
777         double anOpacity = aPrs->opacity();
778
779         bool anIsLockAspectRatio = aPrs->getIsLockAspectRatio();
780         bool anIsSmoothTransformation = aPrs->getIsSmoothTransformation();
781
782         GraphicsView_PrsPropDlg aDlg( aViewPort );
783         aDlg.setData( aPosX, aPosY, aScaleX, aScaleY, aRotationAngle,
784                       aZValue, anOpacity, anIsLockAspectRatio,
785                       anIsSmoothTransformation );
786         if( aDlg.exec() )
787         {
788           aDlg.getData( aPosX, aPosY, aScaleX, aScaleY, aRotationAngle,
789                         aZValue, anOpacity, anIsLockAspectRatio,
790                         anIsSmoothTransformation );
791
792           aPrs->setPosition( aPosX, aPosY );
793           aPrs->setScaling( aScaleX, aScaleY );
794           aPrs->setRotationAngle( aRotationAngle );
795
796           aPrs->setZValue( aZValue );
797           aPrs->setOpacity( anOpacity );
798
799           aPrs->setIsLockAspectRatio( anIsLockAspectRatio );
800           aPrs->setIsSmoothTransformation( anIsSmoothTransformation );
801
802           aPrs->compute();
803         }
804       }
805     }
806   }
807 }
808
809 //================================================================
810 // Function : onTestFuseOperator
811 // Purpose  : 
812 //================================================================
813 void GraphicsView_Viewer::onTestFuseOperator()
814 {
815   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
816   {
817     GraphicsView_ObjectList aList = aViewPort->getObjects();
818     if( aList.count() < 3 )
819       return;
820
821     GraphicsView_PrsImage* anObj1 = dynamic_cast<GraphicsView_PrsImage*>( aList[0] );
822     GraphicsView_PrsImage* anObj2 = dynamic_cast<GraphicsView_PrsImage*>( aList[2] );
823
824     ImageComposer_Image anImage1;
825     anImage1 = anObj1->getImage();
826     anImage1.setTransform( anObj1->getTransform() );
827
828     ImageComposer_Image anImage2;
829     anImage2 = anObj2->getImage();
830     anImage2.setTransform( anObj2->getTransform() );
831
832     ImageComposer_Image aResult = anImage1 | anImage2;
833     GraphicsView_PrsImage* aResPrs = new GraphicsView_PrsImage();
834     aResPrs->setImage( aResult );
835
836     double aPosX, aPosY, aScaleX, aScaleY, aRotationAngle;
837     anObj1->getPosition( aPosX, aPosY );
838     anObj1->getScaling( aScaleX, aScaleY );
839     anObj1->getRotationAngle( aRotationAngle );
840
841     aResPrs->setPosition( aResult.transform().dx(), aResult.transform().dy() );
842     aResPrs->setScaling( aScaleX, aScaleY );
843     aResPrs->setRotationAngle( aRotationAngle );
844
845     aResPrs->compute();
846
847     aViewPort->addItem( aResPrs );
848     aViewPort->removeItem( anObj1 );
849     aViewPort->removeItem( anObj2 );
850   }
851 }
852
853 //================================================================
854 // Function : onTestCropOperatorPrepare
855 // Purpose  : 
856 //================================================================
857 void GraphicsView_Viewer::onTestCropOperatorPrepare()
858 {
859   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
860     aViewPort->prepareToSketch( true );
861 }
862
863 //================================================================
864 // Function : onTestCropOperatorPerform
865 // Purpose  : 
866 //================================================================
867 void GraphicsView_Viewer::onTestCropOperatorPerform( QPainterPath thePath )
868 {
869   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
870   {
871     GraphicsView_ObjectList aList = aViewPort->getObjects();
872     if( aList.count() < 1 )
873       return;
874
875     GraphicsView_PrsImage* anObj = dynamic_cast<GraphicsView_PrsImage*>( aList[0] );
876
877     ImageComposer_Image anImage;
878     anImage = anObj->getImage();
879     anImage.setTransform( anObj->getTransform() );
880
881     ImageComposer_Image aResult = anImage & thePath;
882     GraphicsView_PrsImage* aResPrs = new GraphicsView_PrsImage();
883     aResPrs->setImage( aResult );
884
885     double aPosX, aPosY, aScaleX, aScaleY, aRotationAngle;
886     anObj->getPosition( aPosX, aPosY );
887     anObj->getScaling( aScaleX, aScaleY );
888     anObj->getRotationAngle( aRotationAngle );
889
890     aResPrs->setPosition( aResult.transform().dx(), aResult.transform().dy() );
891     aResPrs->setScaling( aScaleX, aScaleY );
892     aResPrs->setRotationAngle( aRotationAngle );
893
894     aResPrs->compute();
895
896     aViewPort->addItem( aResPrs );
897     aViewPort->removeItem( anObj );
898   }
899 }
900 */