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