Salome HOME
4202a633211c8a5d0b7a74d3e51ce38389b88a3c
[modules/gui.git] / src / GraphicsView / GraphicsView_Viewer.cxx
1 // Copyright (C) 2013-2016  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::GlobalWheelScaling ) )
525     {
526       const double d = 1.05;
527       double q = pow( d, e->delta()/120 );
528       QGraphicsView::ViewportAnchor old_anchor = aViewPort->transformationAnchor();
529       aViewPort->setTransformationAnchor( QGraphicsView::AnchorUnderMouse );
530       aViewPort->scale( q, q );
531       aViewPort->setTransformationAnchor( old_anchor );
532     }
533
534     if( aViewPort->hasInteractionFlag( GraphicsView_ViewPort::WheelScaling ) )
535     {
536       bool anIsScaleUp = e->delta() > 0;
537       bool anIsCtrl = e->modifiers() & Qt::ControlModifier;
538
539       bool anIsScaleChanged = false;
540       for( aViewPort->initSelected(); aViewPort->moreSelected(); aViewPort->nextSelected() )
541         if( GraphicsView_Object* anObject = aViewPort->selectedObject() )
542           anIsScaleChanged = anObject->updateScale( anIsScaleUp, anIsCtrl ) || anIsScaleChanged;
543
544       if( anIsScaleChanged )
545       {
546         emit wheelScaleChanged();
547         aViewPort->onBoundingRectChanged();
548       }
549     }
550   }
551 }
552
553 //================================================================
554 // Function : onSketchingFinished
555 // Purpose  : 
556 //================================================================
557 void GraphicsView_Viewer::onSketchingFinished( QPainterPath thePath )
558 {
559   // testing ImageViewer
560   //onTestCropOperatorPerform( thePath );
561 }
562
563 //================================================================
564 // Function : onSelectionDone
565 // Purpose  : 
566 //================================================================
567 void GraphicsView_Viewer::onSelectionDone( GV_SelectionChangeStatus theStatus )
568 {
569   emit selectionChanged( theStatus );
570 }
571
572 //================================================================
573 // Function : onChangeBgColor
574 // Purpose  : 
575 //================================================================
576 void GraphicsView_Viewer::onChangeBgColor()
577 {
578   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
579   {
580     QColor aColor = aViewPort->isForegroundEnabled() ?
581       aViewPort->foregroundColor() : aViewPort->backgroundColor();
582     aColor = QColorDialog::getColor( aColor, aViewPort );       
583     if ( aColor.isValid() )
584     {
585       if( aViewPort->isForegroundEnabled() )
586       {
587         aViewPort->setForegroundColor( aColor );
588         aViewPort->updateForeground();
589       }
590       else
591         aViewPort->setBackgroundColor( aColor );
592     }
593   }
594 }
595
596 //================================================================
597 // Function : onSelectionCancel
598 // Purpose  : 
599 //================================================================
600 void GraphicsView_Viewer::onSelectionCancel()
601 {
602   emit selectionChanged( GVSCS_Invalid );
603 }
604
605 /*
606 //================================================================
607 // Function : onAddImage
608 // Purpose  : 
609 //================================================================
610 void GraphicsView_Viewer::onAddImage()
611 {
612   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
613   {
614     QString aFileName = QFileDialog::getOpenFileName();
615     if( aFileName.isEmpty() )
616       return;
617
618     GraphicsView_PrsImage* aPrs = new GraphicsView_PrsImage();
619
620     QImage anImage( aFileName );
621     aPrs->setImage( anImage );
622
623     aPrs->compute();
624
625     aViewPort->addItem( aPrs );
626     aViewPort->fitAll();
627   }
628 }
629
630 //================================================================
631 // Function : onRemoveImages
632 // Purpose  : 
633 //================================================================
634 void GraphicsView_Viewer::onRemoveImages()
635 {
636   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
637   {
638     GraphicsView_ObjectListIterator anIter( aViewPort->getSelectedObjects() );
639     while( anIter.hasNext() )
640     {
641       if( GraphicsView_PrsImage* aPrs = dynamic_cast<GraphicsView_PrsImage*>( anIter.next() ) )
642       {
643         aViewPort->removeItem( aPrs );
644         delete aPrs;
645       }
646     }
647   }
648 }
649
650 //================================================================
651 // Function : onBringToFront
652 // Purpose  : 
653 //================================================================
654 void GraphicsView_Viewer::onBringToFront()
655 {
656   processQueueOperation( BringToFront );
657 }
658
659 //================================================================
660 // Function : onSendToBack
661 // Purpose  : 
662 //================================================================
663 void GraphicsView_Viewer::onSendToBack()
664 {
665   processQueueOperation( SendToBack );
666 }
667
668 //================================================================
669 // Function : onBringForward
670 // Purpose  : 
671 //================================================================
672 void GraphicsView_Viewer::onBringForward()
673 {
674   processQueueOperation( BringForward );
675 }
676
677 //================================================================
678 // Function : onSendBackward
679 // Purpose  : 
680 //================================================================
681 void GraphicsView_Viewer::onSendBackward()
682 {
683   processQueueOperation( SendBackward );
684 }
685
686 //================================================================
687 // Function : processQueueOperation
688 // Purpose  : 
689 //================================================================
690 void GraphicsView_Viewer::processQueueOperation( const QueueOperation theOperation )
691 {
692   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
693   {
694     const GraphicsView_ObjectList& aSelectedList = aViewPort->getSelectedObjects();
695
696     GraphicsView_ObjectList aSortedList;
697
698     GraphicsView_ObjectList aList = aViewPort->getObjects();
699     GraphicsView_ObjectListIterator anIter( aList );
700     while( anIter.hasNext() )
701     {
702       if( GraphicsView_Object* anObject = anIter.next() )
703       {
704         if( !anObject->hasSpecificZValue() )
705         {
706           double aZValue = anObject->zValue();
707           GraphicsView_ObjectList::iterator anIter1, anIter1End = aSortedList.end();
708           for( anIter1 = aSortedList.begin(); anIter1 != anIter1End; anIter1++ )
709             if( GraphicsView_Object* anObjectRef = *anIter1 )
710               if( !anObjectRef->hasSpecificZValue() && anObjectRef->zValue() > aZValue )
711                 break;
712           aSortedList.insert( anIter1, anObject );
713         }
714       }
715     }
716
717     QList<int> anIndicesToMove;
718
719     int anIndex = 0;
720     anIter = aSortedList;
721     while( anIter.hasNext() )
722     {
723       if( GraphicsView_Object* anObject = anIter.next() )
724         if( aSelectedList.contains( anObject ) )
725           anIndicesToMove.append( anIndex );
726       anIndex++;
727     }
728
729     bool anIsReverse = theOperation == BringToFront || theOperation == BringForward;
730     QListIterator<int> anIndicesIter( anIndicesToMove );
731     if( anIsReverse )
732       anIndicesIter.toBack();
733
734     int aShiftForMultiple = 0;
735     int anObjectCount = aSortedList.count();
736     while( anIsReverse ? anIndicesIter.hasPrevious() : anIndicesIter.hasNext() )
737     {
738       int anIndex = anIsReverse ? anIndicesIter.previous() : anIndicesIter.next();
739       int aNewIndex = anIndex;
740       switch( theOperation )
741       {
742         case BringToFront: aNewIndex = anObjectCount - 1 - aShiftForMultiple; break;
743         case SendToBack:   aNewIndex = aShiftForMultiple; break;
744         case BringForward: aNewIndex = anIndex + 1; break;
745         case SendBackward: aNewIndex = anIndex - 1; break;
746       }
747       aShiftForMultiple++;
748
749       if( aNewIndex < 0 || aNewIndex > anObjectCount - 1 )
750         break;
751
752       aSortedList.move( anIndex, aNewIndex );
753     }
754
755     double aZValue = 1.0;
756     anIter = aSortedList;
757     while( anIter.hasNext() )
758     {
759       if( GraphicsView_Object* anObject = anIter.next() )
760       {
761         anObject->setZValue( aZValue );
762         aZValue += 1.0;
763       }
764     }
765   }
766 }
767
768 //================================================================
769 // Function : onPrsProperties
770 // Purpose  : 
771 //================================================================
772 void GraphicsView_Viewer::onPrsProperties()
773 {
774   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
775   {
776     aViewPort->initSelected();
777     if( GraphicsView_Object* anObject = aViewPort->selectedObject() )
778     {
779       if( GraphicsView_PrsImage* aPrs = dynamic_cast<GraphicsView_PrsImage*>( anObject ) )
780       {
781         double aPosX, aPosY, aScaleX, aScaleY, aRotationAngle;
782         aPrs->getPosition( aPosX, aPosY );
783         aPrs->getScaling( aScaleX, aScaleY );
784         aPrs->getRotationAngle( aRotationAngle );
785
786         double aZValue = aPrs->zValue();
787         double anOpacity = aPrs->opacity();
788
789         bool anIsLockAspectRatio = aPrs->getIsLockAspectRatio();
790         bool anIsSmoothTransformation = aPrs->getIsSmoothTransformation();
791
792         GraphicsView_PrsPropDlg aDlg( aViewPort );
793         aDlg.setData( aPosX, aPosY, aScaleX, aScaleY, aRotationAngle,
794                       aZValue, anOpacity, anIsLockAspectRatio,
795                       anIsSmoothTransformation );
796         if( aDlg.exec() )
797         {
798           aDlg.getData( aPosX, aPosY, aScaleX, aScaleY, aRotationAngle,
799                         aZValue, anOpacity, anIsLockAspectRatio,
800                         anIsSmoothTransformation );
801
802           aPrs->setPosition( aPosX, aPosY );
803           aPrs->setScaling( aScaleX, aScaleY );
804           aPrs->setRotationAngle( aRotationAngle );
805
806           aPrs->setZValue( aZValue );
807           aPrs->setOpacity( anOpacity );
808
809           aPrs->setIsLockAspectRatio( anIsLockAspectRatio );
810           aPrs->setIsSmoothTransformation( anIsSmoothTransformation );
811
812           aPrs->compute();
813         }
814       }
815     }
816   }
817 }
818
819 //================================================================
820 // Function : onTestFuseOperator
821 // Purpose  : 
822 //================================================================
823 void GraphicsView_Viewer::onTestFuseOperator()
824 {
825   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
826   {
827     GraphicsView_ObjectList aList = aViewPort->getObjects();
828     if( aList.count() < 3 )
829       return;
830
831     GraphicsView_PrsImage* anObj1 = dynamic_cast<GraphicsView_PrsImage*>( aList[0] );
832     GraphicsView_PrsImage* anObj2 = dynamic_cast<GraphicsView_PrsImage*>( aList[2] );
833
834     ImageComposer_Image anImage1;
835     anImage1 = anObj1->getImage();
836     anImage1.setTransform( anObj1->getTransform() );
837
838     ImageComposer_Image anImage2;
839     anImage2 = anObj2->getImage();
840     anImage2.setTransform( anObj2->getTransform() );
841
842     ImageComposer_Image aResult = anImage1 | anImage2;
843     GraphicsView_PrsImage* aResPrs = new GraphicsView_PrsImage();
844     aResPrs->setImage( aResult );
845
846     double aPosX, aPosY, aScaleX, aScaleY, aRotationAngle;
847     anObj1->getPosition( aPosX, aPosY );
848     anObj1->getScaling( aScaleX, aScaleY );
849     anObj1->getRotationAngle( aRotationAngle );
850
851     aResPrs->setPosition( aResult.transform().dx(), aResult.transform().dy() );
852     aResPrs->setScaling( aScaleX, aScaleY );
853     aResPrs->setRotationAngle( aRotationAngle );
854
855     aResPrs->compute();
856
857     aViewPort->addItem( aResPrs );
858     aViewPort->removeItem( anObj1 );
859     aViewPort->removeItem( anObj2 );
860   }
861 }
862
863 //================================================================
864 // Function : onTestCropOperatorPrepare
865 // Purpose  : 
866 //================================================================
867 void GraphicsView_Viewer::onTestCropOperatorPrepare()
868 {
869   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
870     aViewPort->prepareToSketch( true );
871 }
872
873 //================================================================
874 // Function : onTestCropOperatorPerform
875 // Purpose  : 
876 //================================================================
877 void GraphicsView_Viewer::onTestCropOperatorPerform( QPainterPath thePath )
878 {
879   if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() )
880   {
881     GraphicsView_ObjectList aList = aViewPort->getObjects();
882     if( aList.count() < 1 )
883       return;
884
885     GraphicsView_PrsImage* anObj = dynamic_cast<GraphicsView_PrsImage*>( aList[0] );
886
887     ImageComposer_Image anImage;
888     anImage = anObj->getImage();
889     anImage.setTransform( anObj->getTransform() );
890
891     ImageComposer_Image aResult = anImage & thePath;
892     GraphicsView_PrsImage* aResPrs = new GraphicsView_PrsImage();
893     aResPrs->setImage( aResult );
894
895     double aPosX, aPosY, aScaleX, aScaleY, aRotationAngle;
896     anObj->getPosition( aPosX, aPosY );
897     anObj->getScaling( aScaleX, aScaleY );
898     anObj->getRotationAngle( aRotationAngle );
899
900     aResPrs->setPosition( aResult.transform().dx(), aResult.transform().dy() );
901     aResPrs->setScaling( aScaleX, aScaleY );
902     aResPrs->setRotationAngle( aRotationAngle );
903
904     aResPrs->compute();
905
906     aViewPort->addItem( aResPrs );
907     aViewPort->removeItem( anObj );
908   }
909 }
910 */