]> SALOME platform Git repositories - modules/gui.git/blob - src/SUIT/SUIT_ViewWindow.cxx
Salome HOME
cb7045eee7d7dca2be79e4dcf253ee278c103002
[modules/gui.git] / src / SUIT / SUIT_ViewWindow.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 // SUIT_ViewWindow.cxx: implementation of the SUIT_ViewWindow class.
23 //
24 #include "SUIT_ViewWindow.h"
25
26 #include "SUIT_Tools.h"
27 #include "SUIT_Study.h"
28 #include "SUIT_Desktop.h"
29 #include "SUIT_MessageBox.h"
30 #include "SUIT_Application.h"
31 #include "SUIT_ViewManager.h"
32 #include "QtxActionToolMgr.h"
33
34 #include <QEvent>
35 #include <QIcon>
36 #include <QApplication>
37 #include <QContextMenuEvent>
38
39 /*!\class SUIT_ViewWindow
40  * Class provide view window.
41  */
42
43 /*! Dump view custom event*/
44 const int DUMP_EVENT = QEvent::User + 123;
45
46 /*! Constructor.*/
47 SUIT_ViewWindow::SUIT_ViewWindow( SUIT_Desktop* theDesktop )
48 : QMainWindow( theDesktop )
49 {
50   myDesktop = theDesktop;
51
52   setWindowIcon( myDesktop->windowIcon() );
53
54   setAttribute( Qt::WA_DeleteOnClose );
55
56   myToolMgr = new QtxActionToolMgr( this );
57 }
58
59 /*! Destructor.*/
60 SUIT_ViewWindow::~SUIT_ViewWindow()
61 {
62 }
63
64 /*!
65   Sets new view manager for window
66   \param theManager - new view manager
67 */
68 void SUIT_ViewWindow::setViewManager( SUIT_ViewManager* theManager )
69 {
70   myManager = theManager;
71 }
72
73 /*!
74   \return view manager of window
75 */
76 SUIT_ViewManager* SUIT_ViewWindow::getViewManager() const
77 {
78   return myManager;
79 }
80
81 /*!
82   \return QImage, containing all scene rendering in window
83 */
84 QImage SUIT_ViewWindow::dumpView()
85 {
86   return QImage();
87 }
88
89 /*!
90   Saves image to file according to the format
91   \param image - image
92   \param fileName - name of file
93   \param format - string contains name of format (for example, "BMP"(default) or "JPEG", "JPG")
94 */
95 bool SUIT_ViewWindow::dumpViewToFormat( const QImage& img, const QString& fileName, const QString& format )
96 {
97   if( img.isNull() )
98     return false;
99
100   QString fmt = format;
101   if( fmt.isEmpty() )
102     fmt = QString( "BMP" ); // default format
103   else if( fmt == "JPG" )
104     fmt = "JPEG";
105
106   QApplication::setOverrideCursor( Qt::WaitCursor );
107   bool res = img.save( fileName, fmt.toLatin1() );
108   QApplication::restoreOverrideCursor();
109   return res;
110 }
111
112 /*!
113   Saves scene rendering in window to file
114   \param fileName - name of file
115   \param format - string contains name of format (for example, "BMP"(default) or "JPEG", "JPG")
116 */
117 bool SUIT_ViewWindow::dumpViewToFormat( const QString& fileName, const QString& format )
118 {
119   return dumpViewToFormat( dumpView(), fileName, format );
120 }
121
122 /*!
123   Set or clear flag Qt::WDestructiveClose
124 */
125 void SUIT_ViewWindow::setDestructiveClose( const bool on )
126 {
127   setAttribute( Qt::WA_DeleteOnClose, on );
128 }
129
130 /*! Close event \a theEvent.
131 */
132 void SUIT_ViewWindow::closeEvent( QCloseEvent* e )
133 {
134   e->ignore();
135   emit closing( this );
136 }
137
138 /*! Context menu requested for event \a e.
139 */
140 void SUIT_ViewWindow::contextMenuEvent( QContextMenuEvent* e )
141 {
142   e->ignore();
143
144   QMainWindow::contextMenuEvent( e );
145
146   if ( e->isAccepted() )
147     return;
148
149   if ( e->reason() != QContextMenuEvent::Mouse )
150     emit contextMenuRequested( e );
151 }
152
153 /*! Post events on dump view.
154 */
155 void SUIT_ViewWindow::onDumpView()
156 {
157   QApplication::postEvent( this, new QPaintEvent( QRect( 0, 0, width(), height() ) ) );
158   QApplication::postEvent( this, new QEvent( (QEvent::Type)DUMP_EVENT ) );
159 }
160
161 /*!
162   \return filters for image files
163 */
164 QString SUIT_ViewWindow::filter() const
165 {
166   return tr( "TLT_IMAGE_FILES" );
167 }
168
169 /*! Reaction view window on event \a e.
170 */
171 bool SUIT_ViewWindow::event( QEvent* e )
172 {
173   if ( e->type() == DUMP_EVENT )
174   {
175     bool bOk = false;
176     if ( myManager && myManager->study() && myManager->study()->application() )
177     {
178       QImage im = dumpView();
179
180       // get file name
181       SUIT_Application* app = myManager->study()->application();
182       QString fileName = app->getFileName( false, QString(), filter(), tr( "TLT_DUMP_VIEW" ), 0 );
183       if ( !fileName.isEmpty() )
184       {
185               QString fmt = SUIT_Tools::extension( fileName ).toUpper();
186               bOk = dumpViewToFormat( im, fileName, fmt );
187       }
188       else
189               bOk = true; // cancelled
190     }
191     if ( !bOk )
192       SUIT_MessageBox::critical( this, tr( "ERROR" ), tr( "ERR_CANT_DUMP_VIEW" ) );
193
194     return true;
195   }
196   return QMainWindow::event( e );
197 }
198
199 /*! Called by SUIT_Accel::onActivated() when a key accelerator was activated and this window was active
200 */
201 bool SUIT_ViewWindow::onAccelAction( int _action )
202 {
203   return action( _action );
204 }
205
206 /*! action  handle standard action (zoom, pan) or custom action.  to be redefined in successors.
207 */
208 bool SUIT_ViewWindow::action( const int  )
209 {
210   return true;
211 }
212
213 /*!
214   \return string containing visual parameters of window
215 */
216 QString   SUIT_ViewWindow::getVisualParameters()
217 {
218   return "empty";
219 }
220
221 /*!
222   Sets visual parameters of window by its string representation
223   \param parameters - string with visual parameters
224 */
225 void SUIT_ViewWindow::setVisualParameters( const QString& /*parameters*/ )
226 {
227 }
228
229 /*!
230   \return associated tool bar manager
231 */
232 QtxActionToolMgr* SUIT_ViewWindow::toolMgr() const
233 {
234   return myToolMgr;
235 }
236
237 /*!
238   \return window unique identifier  
239 */
240 int SUIT_ViewWindow::getId() const
241 {
242   return int(long(this));
243 }