Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/gui.git] / src / SUIT / SUIT_ViewWindow.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 // SUIT_ViewWindow.cxx: implementation of the SUIT_ViewWindow class.
24 //
25 #include "SUIT_ViewWindow.h"
26
27 #include "SUIT_Tools.h"
28 #include "SUIT_Study.h"
29 #include "SUIT_Desktop.h"
30 #include "SUIT_MessageBox.h"
31 #include "SUIT_Application.h"
32 #include "SUIT_ViewManager.h"
33 #include "QtxActionToolMgr.h"
34 #include "QtxMultiAction.h"
35
36 #include <QEvent>
37 #include <QIcon>
38 #include <QApplication>
39 #include <QContextMenuEvent>
40
41 /*!\class SUIT_ViewWindow
42  * Class provide view window.
43  */
44
45 /*! Dump view custom event*/
46 const int DUMP_EVENT = QEvent::User + 123;
47
48 /*! Constructor.*/
49 SUIT_ViewWindow::SUIT_ViewWindow( SUIT_Desktop* theDesktop )
50   : QMainWindow( theDesktop ), myManager( 0 ), myIsDropDown( true )
51 {
52   myDesktop = theDesktop;
53
54   setWindowIcon( myDesktop ? myDesktop->windowIcon() : QApplication::windowIcon() );
55
56   setAttribute( Qt::WA_DeleteOnClose );
57
58   myToolMgr = new QtxActionToolMgr( this );
59
60   setProperty( "VectorsMode", false );
61 }
62
63 /*! Destructor.*/
64 SUIT_ViewWindow::~SUIT_ViewWindow()
65 {
66 }
67
68 /*!
69   Sets new view manager for window
70   \param theManager - new view manager
71 */
72 void SUIT_ViewWindow::setViewManager( SUIT_ViewManager* theManager )
73 {
74   myManager = theManager;
75 }
76
77 /*!
78   \return view manager of window
79 */
80 SUIT_ViewManager* SUIT_ViewWindow::getViewManager() const
81 {
82   return myManager;
83 }
84
85 /*!
86   \return QImage, containing all scene rendering in window
87 */
88 QImage SUIT_ViewWindow::dumpView()
89 {
90   return QImage();
91 }
92
93 /*!
94   Saves image to file according to the format
95   \param image - image
96   \param fileName - name of file
97   \param format - string contains name of format (for example, "BMP"(default) or "JPEG", "JPG")
98 */
99 bool SUIT_ViewWindow::dumpViewToFormat( const QImage& img, const QString& fileName, const QString& format )
100 {
101   if( img.isNull() )
102     return false;
103
104   QString fmt = format;
105   if( fmt.isEmpty() )
106     fmt = QString( "BMP" ); // default format
107   else if( fmt == "JPG" )
108     fmt = "JPEG";
109
110   QApplication::setOverrideCursor( Qt::WaitCursor );
111   bool res = img.save( fileName, fmt.toLatin1() );
112   QApplication::restoreOverrideCursor();
113   return res;
114 }
115
116 /*!
117   Saves scene rendering in window to file
118   \param fileName - name of file
119   \param format - string contains name of format (for example, "BMP"(default) or "JPEG", "JPG")
120 */
121 bool SUIT_ViewWindow::dumpViewToFormat( const QString& fileName, const QString& format )
122 {
123   Qtx::Localizer loc;
124   return dumpViewToFormat( dumpView(), fileName, format );
125 }
126
127 /*!
128   Set or clear flag Qt::WDestructiveClose
129 */
130 void SUIT_ViewWindow::setDestructiveClose( const bool on )
131 {
132   setAttribute( Qt::WA_DeleteOnClose, on );
133 }
134
135 /*! Close event \a theEvent.
136 */
137 void SUIT_ViewWindow::closeEvent( QCloseEvent* e )
138 {
139   e->ignore();
140   emit tryClosing( this );
141   if ( closable() ) emit closing( this );
142 }
143
144 /*! Context menu requested for event \a e.
145 */
146 void SUIT_ViewWindow::contextMenuEvent( QContextMenuEvent* e )
147 {
148   e->ignore();
149
150   QMainWindow::contextMenuEvent( e );
151
152   if ( e->isAccepted() )
153     return;
154
155   if ( e->reason() != QContextMenuEvent::Mouse )
156     emit contextMenuRequested( e );
157 }
158
159 /*! Post events on dump view.
160 */
161 void SUIT_ViewWindow::onDumpView()
162 {
163   // VSV (TRIPOLI dev): next line commented: causes error messages
164   //QApplication::postEvent( this, new QPaintEvent( QRect( 0, 0, width(), height() ) ) );
165   QApplication::postEvent( this, new QEvent( (QEvent::Type)DUMP_EVENT ) );
166 }
167
168 /*!
169   \return filters for image files
170 */
171 QString SUIT_ViewWindow::filter() const
172 {
173   return tr( "TLT_IMAGE_FILES" );
174 }
175
176 /*! Reaction view window on event \a e.
177 */
178 bool SUIT_ViewWindow::event( QEvent* e )
179 {
180   if ( e->type() == DUMP_EVENT )
181   {
182     bool bOk = false;
183     if ( myManager && myManager->study() && myManager->study()->application() )
184     {
185       // get file name
186       SUIT_Application* app = myManager->study()->application();
187       QString fileName = app->getFileName( false, QString(), filter(), tr( "TLT_DUMP_VIEW" ), 0 );
188       if ( !fileName.isEmpty() )
189       {
190         QImage im = dumpView();
191         QString fmt = SUIT_Tools::extension( fileName ).toUpper();
192         Qtx::Localizer loc;
193         bOk = dumpViewToFormat( im, fileName, fmt );
194       }
195       else
196         bOk = true; // cancelled
197     }
198     if ( !bOk )
199       SUIT_MessageBox::critical( this, tr( "ERROR" ), tr( "ERR_CANT_DUMP_VIEW" ) );
200
201     return true;
202   }
203   return QMainWindow::event( e );
204 }
205
206 /*! Called by SUIT_Accel::onActivated() when a key accelerator was activated and this window was active
207 */
208 bool SUIT_ViewWindow::onAccelAction( int _action )
209 {
210   return action( _action );
211 }
212
213 /*! action  handle standard action (zoom, pan) or custom action.  to be redefined in successors.
214 */
215 bool SUIT_ViewWindow::action( const int  )
216 {
217   return true;
218 }
219
220 /*! Returns \c true if view window can be closed by the user
221 */
222 bool SUIT_ViewWindow::closable() const
223 {
224   QVariant val = property( "closable" );
225   return !val.isValid() || val.toBool();
226 }
227
228 /*! Set / reset "closable" option of the view window
229 */
230 bool SUIT_ViewWindow::setClosable( const bool on )
231 {
232   bool prev = closable();
233   setProperty( "closable", on );
234   return prev;
235 }
236
237 /*!
238   \return string containing visual parameters of window
239 */
240 QString SUIT_ViewWindow::getVisualParameters()
241 {
242   return "empty";
243 }
244
245 /*!
246   Sets visual parameters of window by its string representation
247   \param parameters - string with visual parameters
248 */
249 void SUIT_ViewWindow::setVisualParameters( const QString& /*parameters*/ )
250 {
251 }
252
253 /*!
254   \return associated tool bar manager
255 */
256 QtxActionToolMgr* SUIT_ViewWindow::toolMgr() const
257 {
258   return myToolMgr;
259 }
260
261 /*!
262   \brief Set buttons mode to drop-down (\a on = \c true) or ligned (\a on = \c false) 
263   \param on new buttons mode
264   \sa dropDownButtons()
265 */
266 void SUIT_ViewWindow::setDropDownButtons( bool on )
267 {
268   if ( myIsDropDown != on ) {
269     myIsDropDown = on;
270     if ( myIsDropDown ) {
271       ActionsMap::const_iterator it;
272       for( it = myMultiActions.constBegin(); it != myMultiActions.constEnd(); ++it )
273       {
274         int tid = it.key();
275         const QList<QtxMultiAction*>& mlist = it.value();
276         QList<QtxMultiAction*>::const_iterator mit;
277         for ( mit = mlist.constBegin(); mit != mlist.constEnd(); ++mit )
278         {
279           QtxMultiAction* ma = *mit;
280           const QList<QAction*> alist = ma->actions();
281           if ( alist.isEmpty() ) continue;
282           int idx = toolMgr()->index( toolMgr()->actionId( alist[0] ), tid );
283           if ( idx == -1 ) continue;
284           foreach ( QAction* a, alist ) toolMgr()->remove( toolMgr()->actionId( a ), tid );
285           toolMgr()->insert( ma, tid, idx );
286         }
287       }
288       myMultiActions.clear();
289     }
290     else {
291       QIntList tblist = toolMgr()->toolBarsIds();
292       QIntList alist  = toolMgr()->idList();
293       foreach( int aid, alist )
294       {
295         QtxMultiAction* ma = qobject_cast<QtxMultiAction*>( toolMgr()->action( aid ) );
296         if ( !ma ) continue;
297         foreach( int tid, tblist )
298         {
299           int idx = toolMgr()->index( aid, tid );
300           if ( idx >= 0 )
301           {
302             myMultiActions[ tid ].append( ma );
303             toolMgr()->remove( aid, tid );
304             foreach( QAction* a, ma->actions() ) toolMgr()->insert( a, tid, idx++ );
305           }
306         }
307       }
308     }
309   }
310 }
311
312 /*!
313   \brief Get current buttons mode
314   \return current buttons mode
315   \sa setDropDownButtons()
316 */
317 bool SUIT_ViewWindow::dropDownButtons() const
318 {
319   return myIsDropDown;
320 }
321
322 /*!
323   \return window unique identifier  
324 */
325 int SUIT_ViewWindow::getId() const
326 {
327   return int(long(this));
328 }