Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/gui.git] / src / QxGraph / QxGraph_ViewModel.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "QxGraph_ViewModel.h"
24 #include "QxGraph_ViewWindow.h"
25 #include "QxGraph_Canvas.h"
26 #include "QxGraph_CanvasView.h"
27 #include "QxGraph_Def.h"
28 #include "QxGraph_Prs.h"
29
30 #include "SUIT_Desktop.h"
31 #include "SUIT_ViewWindow.h"
32 #include "SUIT_Session.h"
33
34 #include <qcolordialog.h>
35 #include <qpopupmenu.h>
36 #include <qcanvas.h> // for addRectangleItem(...) method (can be removed in the future)
37
38 /*!
39   Constructor
40 */
41 QxGraph_Viewer::QxGraph_Viewer()
42   :SUIT_ViewModel(),
43    myCanvas(0),
44    myCurrentView(0)
45 {
46   printf("Construct QxGraph_Viewer\n");
47   //myCanvases.setAutoDelete(true);
48   //myCanvasViews.setAutoDelete(true);
49 }
50
51 /*!
52   Destructor
53 */
54 QxGraph_Viewer::~QxGraph_Viewer() 
55 {
56   if ( myCanvas ) delete myCanvas;
57   //if ( !myCanvases.isEmpty() ) myCanvases.clear();
58   if ( !myCanvasViews.isEmpty() ) myCanvasViews.clear();
59   myCurrentView = 0;
60 }
61
62 /*!
63   Start initialization of view window
64   \param view - view window to be initialized
65 */
66 void QxGraph_Viewer::initView( QxGraph_ViewWindow* view )
67 {
68   if ( view )
69   {
70     view->initLayout();
71     
72     /*
73     // test add items into the current canvas view
74     QRect aRect(100,200,200,100);
75     QCanvasItem* aRectItem = aPrs->addRectangleItem(aRect);
76     //delete aRectItem;
77
78     QPointArray aPA(6);
79     aPA.putPoints(0, 6, 400,100, 500,70, 600,100, 600,200, 500,230, 400,200);
80     QCanvasItem* aPolyItem = aPrs->addPolygonItem(aPA);
81     //delete aPolyItem;
82
83     QPoint aStart(500,300), aEnd(700,250);
84     QCanvasItem* aLineItem = aPrs->addLineItem(aStart, aEnd);
85     //delete aLineItem;
86
87     QCanvasItem* aEllipseItem = aPrs->addEllipseItem(200, 100, 30*16, 120*16);
88     aEllipseItem->setX(400);
89     aEllipseItem->setY(400);
90     //delete aEllipseItem;
91
92     QCanvasItem* aTextItem = aPrs->addTextItem("This is a QCanvasText item");
93     aTextItem->setX(100);
94     aTextItem->setY(500);
95     //delete aTextItem;
96     */
97   }
98 }
99
100 /*!
101   Creates new view window
102   \param theDesktop - main window of application
103 */
104 SUIT_ViewWindow* QxGraph_Viewer::createView(SUIT_Desktop* theDesktop)
105 {
106   QxGraph_ViewWindow* aRes = new QxGraph_ViewWindow( theDesktop, this );
107   initView( aRes );
108   return aRes;
109 }
110
111 /*!
112   Set view with index theIndex from myCanvasViews as current view
113   \param theIndex - the index of the view in the list
114 */
115 void QxGraph_Viewer::setCurrentView(int theIndex)
116 {
117   if ( theIndex >= 0 && theIndex < myCanvasViews.count() )
118   {
119     myCurrentView = myCanvasViews.at(theIndex);
120     myViewManager->getActiveView()->setCentralWidget(myCurrentView);
121   }
122 }
123
124 /*!
125   Builds popup for QxGraph viewer
126 */
127 void QxGraph_Viewer::contextMenuPopup(QPopupMenu* thePopup)
128 {
129   printf("QxGraph_Viewer::contextMenuPopup\n");
130   thePopup->insertItem( tr( "MEN_CHANGE_BACKGROUND" ), this, SLOT( onChangeBgColor() ) );
131
132   thePopup->insertSeparator();
133
134   QxGraph_ViewWindow* aView = (QxGraph_ViewWindow*)(myViewManager->getActiveView());
135   if ( aView && !aView->getToolBar()->isVisible() )
136     thePopup->insertItem( tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
137 }
138
139 /*!
140   SLOT: called if background color is to be changed changed, passes new color to current canvas view
141 */
142 void QxGraph_Viewer::onChangeBgColor()
143 {
144   QxGraph_ViewWindow* aView = (QxGraph_ViewWindow*)(myViewManager->getActiveView());
145   if( !aView )
146     return;
147   QColor aColorActive = aView->backgroundColor();
148
149   QColor selColor = QColorDialog::getColor( aColorActive, aView);
150   if ( selColor.isValid() )
151     aView->setBackgroundColor(selColor);
152 }
153
154 /*!
155   SLOT: called when popup item "Show toolbar" is activated, shows toolbar of active view window
156 */
157 void QxGraph_Viewer::onShowToolbar() {
158   QxGraph_ViewWindow* aView = (QxGraph_ViewWindow*)(myViewManager->getActiveView());
159   if ( aView )
160     aView->getToolBar()->show();    
161 }