Salome HOME
PAL10870 - to improve export of Plot2d to PostScript
[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/
18 //
19 // SUIT_ViewWindow.cxx: implementation of the SUIT_ViewWindow class.
20 //
21 //////////////////////////////////////////////////////////////////////
22
23 #include "SUIT_ViewWindow.h"
24 #include "SUIT_Desktop.h"
25 #include "SUIT_Application.h"
26 #include "SUIT_Study.h"
27 #include "SUIT_ViewManager.h"
28 #include "SUIT_Tools.h"
29 #include "SUIT_MessageBox.h"
30 #include <qhbox.h>
31 #include <qpopupmenu.h>
32 #include <qapplication.h>
33 #include <qimage.h>
34
35 /*!\class SUIT_ViewWindow
36  * Class provide view window.
37  */
38
39 /*! Dump view custom event*/
40 const int DUMP_EVENT = QEvent::User + 123;
41
42 /*! Constructor.*/
43 SUIT_ViewWindow::SUIT_ViewWindow(SUIT_Desktop* theDesktop)
44 : QMainWindow( theDesktop, "SUIT_ViewWindow", Qt::WDestructiveClose )
45 {
46   myDesktop = theDesktop;
47
48   if ( myDesktop->icon() )
49     setIcon( *myDesktop->icon() );
50 }
51
52 /*! Destructor.*/
53 SUIT_ViewWindow::~SUIT_ViewWindow()
54 {
55 }
56
57 void SUIT_ViewWindow::setViewManager( SUIT_ViewManager* theManager )
58 {
59   myManager = theManager;
60 }
61
62 SUIT_ViewManager* SUIT_ViewWindow::getViewManager() const
63 {
64   return myManager;
65 }
66
67 QImage SUIT_ViewWindow::dumpView()
68 {
69   return QImage();
70 }
71
72 bool SUIT_ViewWindow::dumpViewToFormat( const QString& fileName, const QString& format )
73 {
74   QImage img = dumpView();
75   if( img.isNull() )
76     return false; 
77
78   QString fmt = format;
79   if( fmt.isEmpty() )
80     fmt = QString( "BMP" ); // default format
81   else if( fmt == "JPG" )
82     fmt = "JPEG";
83
84   QApplication::setOverrideCursor( Qt::waitCursor );
85   bool res = img.save( fileName, fmt.latin1() );
86   QApplication::restoreOverrideCursor();
87   return res;
88 }
89
90 /*! Close event \a theEvent.
91 */
92 void SUIT_ViewWindow::closeEvent(QCloseEvent* theEvent)
93 {
94   QMainWindow::closeEvent( theEvent );
95   emit closing( this );
96 }
97
98 /*! Context menu requested for event \a e.
99 */
100 void SUIT_ViewWindow::contextMenuEvent ( QContextMenuEvent * e )
101 {
102   if ( e->reason() != QContextMenuEvent::Mouse )
103     emit contextMenuRequested( e );
104 }
105
106 /*! Post events on dump view.
107 */
108 void SUIT_ViewWindow::onDumpView()
109 {
110   qApp->postEvent( this, new QPaintEvent( QRect( 0, 0, width(), height() ), TRUE ) );
111   qApp->postEvent( this, new QCustomEvent( DUMP_EVENT ) );
112 }
113
114 QString SUIT_ViewWindow::filter() const
115 {
116   return tr( "TLT_IMAGE_FILES" );
117 }
118
119 /*! Reaction view window on event \a e.
120 */
121 bool SUIT_ViewWindow::event( QEvent* e )
122 {
123   if ( e->type() == DUMP_EVENT )
124   {
125     bool bOk = false;
126     if ( myManager && myManager->study() && myManager->study()->application() )
127     {
128       // get file name
129       SUIT_Application* app = myManager->study()->application();
130       QString fileName = app->getFileName( false, QString::null, filter(), tr( "TLT_DUMP_VIEW" ), 0 );
131       if( !fileName.isEmpty() )
132       {
133         QString fmt = SUIT_Tools::extension( fileName ).upper();
134         bOk = dumpViewToFormat( fileName, fmt );
135       }
136       else
137       {
138         bOk = true; // cancelled
139       }
140     }
141     if ( !bOk ) {
142       SUIT_MessageBox::error1( this, tr( "ERROR" ), tr( "ERR_CANT_DUMP_VIEW" ), tr( "BUT_OK" ) );
143     }
144     return TRUE;
145   }
146   return QMainWindow::event( e );
147 }
148
149 /*! Called by SUIT_Accel::onActivated() when a key accelerator was activated and this window was active
150 */
151 void SUIT_ViewWindow::onAccelAction( int _action )
152 {
153   action( _action );
154 }
155
156 /*! action  handle standard action (zoom, pan) or custom action.  to be redefined in successors.
157 */
158 void SUIT_ViewWindow::action( const int  )
159 {
160 }