Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/gui.git] / src / SUIT / SUIT_ViewWindow.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // SUIT_ViewWindow.cxx: implementation of the SUIT_ViewWindow class.
20 //
21
22 #include "SUIT_ViewWindow.h"
23 #include "SUIT_Desktop.h"
24 #include "SUIT_Application.h"
25 #include "SUIT_Study.h"
26 #include "SUIT_ViewManager.h"
27 #include "SUIT_Tools.h"
28 #include "SUIT_MessageBox.h"
29 #include <qhbox.h>
30 #include <qpopupmenu.h>
31 #include <qapplication.h>
32 #include <qimage.h>
33
34 /*!\class SUIT_ViewWindow
35  * Class provide view window.
36  */
37
38 /*! Dump view custom event*/
39 const int DUMP_EVENT = QEvent::User + 123;
40
41 /*! Constructor.*/
42 SUIT_ViewWindow::SUIT_ViewWindow(SUIT_Desktop* theDesktop)
43 : QMainWindow( theDesktop, "SUIT_ViewWindow", Qt::WDestructiveClose )
44 {
45   myDesktop = theDesktop;
46
47   if ( myDesktop->icon() )
48     setIcon( *myDesktop->icon() );
49 }
50
51 /*! Destructor.*/
52 SUIT_ViewWindow::~SUIT_ViewWindow()
53 {
54 }
55
56 /*!
57   Sets new view manager for window
58   \param theManager - new view manager
59 */
60 void SUIT_ViewWindow::setViewManager( SUIT_ViewManager* theManager )
61 {
62   myManager = theManager;
63 }
64
65 /*!
66   \return view manager of window
67 */
68 SUIT_ViewManager* SUIT_ViewWindow::getViewManager() const
69 {
70   return myManager;
71 }
72
73 /*!
74   \return QImage, containing all scene rendering in window
75 */
76 QImage SUIT_ViewWindow::dumpView()
77 {
78   return QImage();
79 }
80
81 /*!
82   Saves image to file according to the format
83   \param image - image
84   \param fileName - name of file
85   \param format - string contains name of format (for example, "BMP"(default) or "JPEG", "JPG")
86 */
87 bool SUIT_ViewWindow::dumpViewToFormat( const QImage& img, const QString& fileName, const QString& format )
88 {
89   if( img.isNull() )
90     return false; 
91
92   QString fmt = format;
93   if( fmt.isEmpty() )
94     fmt = QString( "BMP" ); // default format
95   else if( fmt == "JPG" )
96     fmt = "JPEG";
97
98   QApplication::setOverrideCursor( Qt::waitCursor );
99   bool res = img.save( fileName, fmt.latin1() );
100   QApplication::restoreOverrideCursor();
101   return res;
102 }
103
104 /*!
105   Saves scene rendering in window to file
106   \param fileName - name of file
107   \param format - string contains name of format (for example, "BMP"(default) or "JPEG", "JPG")
108 */
109 bool SUIT_ViewWindow::dumpViewToFormat( const QString& fileName, const QString& format )
110 {
111   return dumpViewToFormat( dumpView(), fileName, format );
112 }
113
114 /*!
115   Set or clear flag Qt::WDestructiveClose
116 */
117 void SUIT_ViewWindow::setDestructiveClose( const bool on )
118 {
119   if ( on )
120     setWFlags( WDestructiveClose );
121   else
122     clearWFlags( WDestructiveClose );
123 }
124
125 /*! Close event \a theEvent.
126 */
127 void SUIT_ViewWindow::closeEvent(QCloseEvent* theEvent)
128 {
129 //  QMainWindow::closeEvent( theEvent );
130   emit closing( this );
131 }
132
133 /*! Context menu requested for event \a e.
134 */
135 void SUIT_ViewWindow::contextMenuEvent ( QContextMenuEvent * e )
136 {
137   if ( e->reason() != QContextMenuEvent::Mouse )
138     emit contextMenuRequested( e );
139 }
140
141 /*! Post events on dump view.
142 */
143 void SUIT_ViewWindow::onDumpView()
144 {
145   qApp->postEvent( this, new QPaintEvent( QRect( 0, 0, width(), height() ), TRUE ) );
146   qApp->postEvent( this, new QCustomEvent( DUMP_EVENT ) );
147 }
148
149 /*!
150   \return filters for image files
151 */
152 QString SUIT_ViewWindow::filter() const
153 {
154   return tr( "TLT_IMAGE_FILES" );
155 }
156
157 /*! Reaction view window on event \a e.
158 */
159 bool SUIT_ViewWindow::event( QEvent* e )
160 {
161   if ( e->type() == DUMP_EVENT )
162   {
163     bool bOk = false;
164     if ( myManager && myManager->study() && myManager->study()->application() )
165     {
166       QImage im = dumpView();
167
168       // get file name
169       SUIT_Application* app = myManager->study()->application();
170       QString fileName = app->getFileName( false, QString::null, filter(), tr( "TLT_DUMP_VIEW" ), 0 );
171       if( !fileName.isEmpty() )
172       {
173         QString fmt = SUIT_Tools::extension( fileName ).upper();
174         bOk = dumpViewToFormat( im, fileName, fmt );
175       }
176       else
177       {
178         bOk = true; // cancelled
179       }
180     }
181     if ( !bOk ) {
182       SUIT_MessageBox::error1( this, tr( "ERROR" ), tr( "ERR_CANT_DUMP_VIEW" ), tr( "BUT_OK" ) );
183     }
184     return TRUE;
185   }
186   return QMainWindow::event( e );
187 }
188
189 /*! Called by SUIT_Accel::onActivated() when a key accelerator was activated and this window was active
190 */
191 bool SUIT_ViewWindow::onAccelAction( int _action )
192 {
193   return action( _action );
194 }
195
196 /*! action  handle standard action (zoom, pan) or custom action.  to be redefined in successors.
197 */
198 bool SUIT_ViewWindow::action( const int  )
199 {
200   return true;
201 }
202
203 /*!
204   \return string containing visual parameters of window
205 */
206 QString   SUIT_ViewWindow::getVisualParameters()
207 {
208   return "empty";
209 }
210
211 /*!
212   Sets visual parameters of window by its string representation
213   \param parameters - string with visual parameters
214 */ 
215 void SUIT_ViewWindow::setVisualParameters( const QString& parameters )
216 {
217 }