1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SUIT_ViewWindow.cxx: implementation of the SUIT_ViewWindow class.
25 #include "SUIT_ViewWindow.h"
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"
38 #include <QApplication>
39 #include <QContextMenuEvent>
41 /*!\class SUIT_ViewWindow
42 * Class provide view window.
45 /*! Dump view custom event*/
46 const int DUMP_EVENT = QEvent::User + 123;
49 SUIT_ViewWindow::SUIT_ViewWindow( SUIT_Desktop* theDesktop )
50 : QMainWindow( theDesktop ), myManager( 0 ), myIsDropDown( true )
52 myDesktop = theDesktop;
54 setWindowIcon( myDesktop ? myDesktop->windowIcon() : QApplication::windowIcon() );
56 setAttribute( Qt::WA_DeleteOnClose );
58 myToolMgr = new QtxActionToolMgr( this );
60 setProperty( "VectorsMode", false );
64 SUIT_ViewWindow::~SUIT_ViewWindow()
69 Sets new view manager for window
70 \param theManager - new view manager
72 void SUIT_ViewWindow::setViewManager( SUIT_ViewManager* theManager )
74 myManager = theManager;
78 \return view manager of window
80 SUIT_ViewManager* SUIT_ViewWindow::getViewManager() const
86 \return QImage, containing all scene rendering in window
88 QImage SUIT_ViewWindow::dumpView()
94 Saves image to file according to the format
96 \param fileName - name of file
97 \param format - string contains name of format (for example, "BMP"(default) or "JPEG", "JPG")
99 bool SUIT_ViewWindow::dumpViewToFormat( const QImage& img, const QString& fileName, const QString& format )
104 QString fmt = format;
106 fmt = QString( "BMP" ); // default format
107 else if( fmt == "JPG" )
110 QApplication::setOverrideCursor( Qt::WaitCursor );
111 bool res = img.save( fileName, fmt.toLatin1() );
112 QApplication::restoreOverrideCursor();
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")
121 bool SUIT_ViewWindow::dumpViewToFormat( const QString& fileName, const QString& format )
124 return dumpViewToFormat( dumpView(), fileName, format );
128 Set or clear flag Qt::WDestructiveClose
130 void SUIT_ViewWindow::setDestructiveClose( const bool on )
132 setAttribute( Qt::WA_DeleteOnClose, on );
135 /*! Close event \a theEvent.
137 void SUIT_ViewWindow::closeEvent( QCloseEvent* e )
140 emit tryClosing( this );
141 if ( closable() ) emit closing( this );
144 /*! Context menu requested for event \a e.
146 void SUIT_ViewWindow::contextMenuEvent( QContextMenuEvent* e )
150 QMainWindow::contextMenuEvent( e );
152 if ( e->isAccepted() )
155 if ( e->reason() != QContextMenuEvent::Mouse )
156 emit contextMenuRequested( e );
159 /*! Post events on dump view.
161 void SUIT_ViewWindow::onDumpView()
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 ) );
169 \return filters for image files
171 QString SUIT_ViewWindow::filter() const
173 return tr( "TLT_IMAGE_FILES" );
176 /*! Reaction view window on event \a e.
178 bool SUIT_ViewWindow::event( QEvent* e )
180 if ( e->type() == DUMP_EVENT )
183 if ( myManager && myManager->study() && myManager->study()->application() )
186 SUIT_Application* app = myManager->study()->application();
187 QString fileName = app->getFileName( false, QString(), filter(), tr( "TLT_DUMP_VIEW" ), 0 );
188 if ( !fileName.isEmpty() )
190 QImage im = dumpView();
191 QString fmt = SUIT_Tools::extension( fileName ).toUpper();
193 bOk = dumpViewToFormat( im, fileName, fmt );
196 bOk = true; // cancelled
199 SUIT_MessageBox::critical( this, tr( "ERROR" ), tr( "ERR_CANT_DUMP_VIEW" ) );
203 return QMainWindow::event( e );
206 /*! Called by SUIT_Accel::onActivated() when a key accelerator was activated and this window was active
208 bool SUIT_ViewWindow::onAccelAction( int _action )
210 return action( _action );
213 /*! action handle standard action (zoom, pan) or custom action. to be redefined in successors.
215 bool SUIT_ViewWindow::action( const int )
220 /*! Returns \c true if view window can be closed by the user
222 bool SUIT_ViewWindow::closable() const
224 QVariant val = property( "closable" );
225 return !val.isValid() || val.toBool();
228 /*! Set / reset "closable" option of the view window
230 bool SUIT_ViewWindow::setClosable( const bool on )
232 bool prev = closable();
233 setProperty( "closable", on );
238 \return string containing visual parameters of window
240 QString SUIT_ViewWindow::getVisualParameters()
246 Sets visual parameters of window by its string representation
247 \param parameters - string with visual parameters
249 void SUIT_ViewWindow::setVisualParameters( const QString& /*parameters*/ )
254 \return associated tool bar manager
256 QtxActionToolMgr* SUIT_ViewWindow::toolMgr() const
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()
266 void SUIT_ViewWindow::setDropDownButtons( bool on )
268 if ( myIsDropDown != on ) {
270 if ( myIsDropDown ) {
271 ActionsMap::const_iterator it;
272 for( it = myMultiActions.constBegin(); it != myMultiActions.constEnd(); ++it )
275 const QList<QtxMultiAction*>& mlist = it.value();
276 QList<QtxMultiAction*>::const_iterator mit;
277 for ( mit = mlist.constBegin(); mit != mlist.constEnd(); ++mit )
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 );
288 myMultiActions.clear();
291 QIntList tblist = toolMgr()->toolBarsIds();
292 QIntList alist = toolMgr()->idList();
293 foreach( int aid, alist )
295 QtxMultiAction* ma = qobject_cast<QtxMultiAction*>( toolMgr()->action( aid ) );
297 foreach( int tid, tblist )
299 int idx = toolMgr()->index( aid, tid );
302 myMultiActions[ tid ].append( ma );
303 toolMgr()->remove( aid, tid );
304 foreach( QAction* a, ma->actions() ) toolMgr()->insert( a, tid, idx++ );
313 \brief Get current buttons mode
314 \return current buttons mode
315 \sa setDropDownButtons()
317 bool SUIT_ViewWindow::dropDownButtons() const
323 \return window unique identifier
325 int SUIT_ViewWindow::getId() const
327 return int(long(this));