Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / QxGraph / QxGraph_ViewModel.cxx
1 //  Copyright (C) 2007-2008  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 #include "QxGraph_ViewModel.h"
23 #include "QxGraph_ViewWindow.h"
24 #include "QxGraph_Canvas.h"
25 #include "QxGraph_CanvasView.h"
26 #include "QxGraph_Def.h"
27 #include "QxGraph_Prs.h"
28
29 #include "SUIT_Desktop.h"
30 #include "SUIT_ViewWindow.h"
31 #include "SUIT_Session.h"
32
33 #include <qcolordialog.h>
34 #include <qpopupmenu.h>
35 #include <qcanvas.h> // for addRectangleItem(...) method (can be removed in the future)
36
37 /*!
38   Constructor
39 */
40 QxGraph_Viewer::QxGraph_Viewer()
41   :SUIT_ViewModel(),
42    myCanvas(0),
43    myCurrentView(0)
44 {
45   printf("Construct QxGraph_Viewer\n");
46   //myCanvases.setAutoDelete(true);
47   //myCanvasViews.setAutoDelete(true);
48 }
49
50 /*!
51   Destructor
52 */
53 QxGraph_Viewer::~QxGraph_Viewer() 
54 {
55   if ( myCanvas ) delete myCanvas;
56   //if ( !myCanvases.isEmpty() ) myCanvases.clear();
57   if ( !myCanvasViews.isEmpty() ) myCanvasViews.clear();
58   myCurrentView = 0;
59 }
60
61 /*!
62   Start initialization of view window
63   \param view - view window to be initialized
64 */
65 void QxGraph_Viewer::initView( QxGraph_ViewWindow* view )
66 {
67   if ( view )
68   {
69     view->initLayout();
70     
71     /*
72     // test add items into the current canvas view
73     QRect aRect(100,200,200,100);
74     QCanvasItem* aRectItem = aPrs->addRectangleItem(aRect);
75     //delete aRectItem;
76
77     QPointArray aPA(6);
78     aPA.putPoints(0, 6, 400,100, 500,70, 600,100, 600,200, 500,230, 400,200);
79     QCanvasItem* aPolyItem = aPrs->addPolygonItem(aPA);
80     //delete aPolyItem;
81
82     QPoint aStart(500,300), aEnd(700,250);
83     QCanvasItem* aLineItem = aPrs->addLineItem(aStart, aEnd);
84     //delete aLineItem;
85
86     QCanvasItem* aEllipseItem = aPrs->addEllipseItem(200, 100, 30*16, 120*16);
87     aEllipseItem->setX(400);
88     aEllipseItem->setY(400);
89     //delete aEllipseItem;
90
91     QCanvasItem* aTextItem = aPrs->addTextItem("This is a QCanvasText item");
92     aTextItem->setX(100);
93     aTextItem->setY(500);
94     //delete aTextItem;
95     */
96   }
97 }
98
99 /*!
100   Creates new view window
101   \param theDesktop - main window of application
102 */
103 SUIT_ViewWindow* QxGraph_Viewer::createView(SUIT_Desktop* theDesktop)
104 {
105   QxGraph_ViewWindow* aRes = new QxGraph_ViewWindow( theDesktop, this );
106   initView( aRes );
107   return aRes;
108 }
109
110 /*!
111   Set view with index theIndex from myCanvasViews as current view
112   \param theIndex - the index of the view in the list
113 */
114 void QxGraph_Viewer::setCurrentView(int theIndex)
115 {
116   if ( theIndex >= 0 && theIndex < myCanvasViews.count() )
117   {
118     myCurrentView = myCanvasViews.at(theIndex);
119     myViewManager->getActiveView()->setCentralWidget(myCurrentView);
120   }
121 }
122
123 /*!
124   Builds popup for QxGraph viewer
125 */
126 void QxGraph_Viewer::contextMenuPopup(QPopupMenu* thePopup)
127 {
128   printf("QxGraph_Viewer::contextMenuPopup\n");
129   thePopup->insertItem( tr( "MEN_CHANGE_BACKGROUND" ), this, SLOT( onChangeBgColor() ) );
130
131   thePopup->insertSeparator();
132
133   QxGraph_ViewWindow* aView = (QxGraph_ViewWindow*)(myViewManager->getActiveView());
134   if ( aView && !aView->getToolBar()->isVisible() )
135     thePopup->insertItem( tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
136 }
137
138 /*!
139   SLOT: called if background color is to be changed changed, passes new color to current canvas view
140 */
141 void QxGraph_Viewer::onChangeBgColor()
142 {
143   QxGraph_ViewWindow* aView = (QxGraph_ViewWindow*)(myViewManager->getActiveView());
144   if( !aView )
145     return;
146   QColor aColorActive = aView->backgroundColor();
147
148   QColor selColor = QColorDialog::getColor( aColorActive, aView);
149   if ( selColor.isValid() )
150     aView->setBackgroundColor(selColor);
151 }
152
153 /*!
154   SLOT: called when popup item "Show toolbar" is activated, shows toolbar of active view window
155 */
156 void QxGraph_Viewer::onShowToolbar() {
157   QxGraph_ViewWindow* aView = (QxGraph_ViewWindow*)(myViewManager->getActiveView());
158   if ( aView )
159     aView->getToolBar()->show();    
160 }