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