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