Back to index

salome-gui  6.5.0
SUIT_ViewWindow.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // SUIT_ViewWindow.cxx: implementation of the SUIT_ViewWindow class.
00024 //
00025 #include "SUIT_ViewWindow.h"
00026 
00027 #include "SUIT_Tools.h"
00028 #include "SUIT_Study.h"
00029 #include "SUIT_Desktop.h"
00030 #include "SUIT_MessageBox.h"
00031 #include "SUIT_Application.h"
00032 #include "SUIT_ViewManager.h"
00033 #include "QtxActionToolMgr.h"
00034 #include "QtxMultiAction.h"
00035 
00036 #include <QEvent>
00037 #include <QIcon>
00038 #include <QApplication>
00039 #include <QContextMenuEvent>
00040 
00046 const int DUMP_EVENT = QEvent::User + 123;
00047 
00049 SUIT_ViewWindow::SUIT_ViewWindow( SUIT_Desktop* theDesktop )
00050   : QMainWindow( theDesktop ), myManager( 0 ), myIsDropDown( true )
00051 {
00052   myDesktop = theDesktop;
00053 
00054   setWindowIcon( myDesktop ? myDesktop->windowIcon() : QApplication::windowIcon() );
00055 
00056   setAttribute( Qt::WA_DeleteOnClose );
00057 
00058   myToolMgr = new QtxActionToolMgr( this );
00059 
00060   setProperty( "VectorsMode", false );
00061 }
00062 
00064 SUIT_ViewWindow::~SUIT_ViewWindow()
00065 {
00066 }
00067 
00072 void SUIT_ViewWindow::setViewManager( SUIT_ViewManager* theManager )
00073 {
00074   myManager = theManager;
00075 }
00076 
00080 SUIT_ViewManager* SUIT_ViewWindow::getViewManager() const
00081 {
00082   return myManager;
00083 }
00084 
00088 QImage SUIT_ViewWindow::dumpView()
00089 {
00090   return QImage();
00091 }
00092 
00099 bool SUIT_ViewWindow::dumpViewToFormat( const QImage& img, const QString& fileName, const QString& format )
00100 {
00101   if( img.isNull() )
00102     return false;
00103 
00104   QString fmt = format;
00105   if( fmt.isEmpty() )
00106     fmt = QString( "BMP" ); // default format
00107   else if( fmt == "JPG" )
00108     fmt = "JPEG";
00109 
00110   QApplication::setOverrideCursor( Qt::WaitCursor );
00111   bool res = img.save( fileName, fmt.toLatin1() );
00112   QApplication::restoreOverrideCursor();
00113   return res;
00114 }
00115 
00121 bool SUIT_ViewWindow::dumpViewToFormat( const QString& fileName, const QString& format )
00122 {
00123   Qtx::Localizer loc;
00124   return dumpViewToFormat( dumpView(), fileName, format );
00125 }
00126 
00130 void SUIT_ViewWindow::setDestructiveClose( const bool on )
00131 {
00132   setAttribute( Qt::WA_DeleteOnClose, on );
00133 }
00134 
00137 void SUIT_ViewWindow::closeEvent( QCloseEvent* e )
00138 {
00139   e->ignore();
00140   emit tryClosing( this );
00141   if ( closable() ) emit closing( this );
00142 }
00143 
00146 void SUIT_ViewWindow::contextMenuEvent( QContextMenuEvent* e )
00147 {
00148   e->ignore();
00149 
00150   QMainWindow::contextMenuEvent( e );
00151 
00152   if ( e->isAccepted() )
00153     return;
00154 
00155   if ( e->reason() != QContextMenuEvent::Mouse )
00156     emit contextMenuRequested( e );
00157 }
00158 
00161 void SUIT_ViewWindow::onDumpView()
00162 {
00163   // VSV (TRIPOLI dev): next line commented: causes error messages
00164   //QApplication::postEvent( this, new QPaintEvent( QRect( 0, 0, width(), height() ) ) );
00165   QApplication::postEvent( this, new QEvent( (QEvent::Type)DUMP_EVENT ) );
00166 }
00167 
00171 QString SUIT_ViewWindow::filter() const
00172 {
00173   return tr( "TLT_IMAGE_FILES" );
00174 }
00175 
00178 bool SUIT_ViewWindow::event( QEvent* e )
00179 {
00180   if ( e->type() == DUMP_EVENT )
00181   {
00182     bool bOk = false;
00183     if ( myManager && myManager->study() && myManager->study()->application() )
00184     {
00185       // get file name
00186       SUIT_Application* app = myManager->study()->application();
00187       QString fileName = app->getFileName( false, QString(), filter(), tr( "TLT_DUMP_VIEW" ), 0 );
00188       if ( !fileName.isEmpty() )
00189       {
00190         QImage im = dumpView();
00191        QString fmt = SUIT_Tools::extension( fileName ).toUpper();
00192        Qtx::Localizer loc;
00193        bOk = dumpViewToFormat( im, fileName, fmt );
00194       }
00195       else
00196        bOk = true; // cancelled
00197     }
00198     if ( !bOk )
00199       SUIT_MessageBox::critical( this, tr( "ERROR" ), tr( "ERR_CANT_DUMP_VIEW" ) );
00200 
00201     return true;
00202   }
00203   return QMainWindow::event( e );
00204 }
00205 
00208 bool SUIT_ViewWindow::onAccelAction( int _action )
00209 {
00210   return action( _action );
00211 }
00212 
00215 bool SUIT_ViewWindow::action( const int  )
00216 {
00217   return true;
00218 }
00219 
00222 bool SUIT_ViewWindow::closable() const
00223 {
00224   QVariant val = property( "closable" );
00225   return !val.isValid() || val.toBool();
00226 }
00227 
00230 bool SUIT_ViewWindow::setClosable( const bool on )
00231 {
00232   bool prev = closable();
00233   setProperty( "closable", on );
00234   return prev;
00235 }
00236 
00240 QString SUIT_ViewWindow::getVisualParameters()
00241 {
00242   return "empty";
00243 }
00244 
00249 void SUIT_ViewWindow::setVisualParameters( const QString& /*parameters*/ )
00250 {
00251 }
00252 
00256 QtxActionToolMgr* SUIT_ViewWindow::toolMgr() const
00257 {
00258   return myToolMgr;
00259 }
00260 
00266 void SUIT_ViewWindow::setDropDownButtons( bool on )
00267 {
00268   if ( myIsDropDown != on ) {
00269     myIsDropDown = on;
00270     if ( myIsDropDown ) {
00271       ActionsMap::const_iterator it;
00272       for( it = myMultiActions.constBegin(); it != myMultiActions.constEnd(); ++it )
00273       {
00274        int tid = it.key();
00275        const QList<QtxMultiAction*>& mlist = it.value();
00276        QList<QtxMultiAction*>::const_iterator mit;
00277        for ( mit = mlist.constBegin(); mit != mlist.constEnd(); ++mit )
00278        {
00279          QtxMultiAction* ma = *mit;
00280          const QList<QAction*> alist = ma->actions();
00281          if ( alist.isEmpty() ) continue;
00282          int idx = toolMgr()->index( toolMgr()->actionId( alist[0] ), tid );
00283          if ( idx == -1 ) continue;
00284          foreach ( QAction* a, alist ) toolMgr()->remove( toolMgr()->actionId( a ), tid );
00285          toolMgr()->insert( ma, tid, idx );
00286        }
00287       }
00288       myMultiActions.clear();
00289     }
00290     else {
00291       QIntList tblist = toolMgr()->toolBarsIds();
00292       QIntList alist  = toolMgr()->idList();
00293       foreach( int aid, alist )
00294       {
00295        QtxMultiAction* ma = qobject_cast<QtxMultiAction*>( toolMgr()->action( aid ) );
00296        if ( !ma ) continue;
00297        foreach( int tid, tblist )
00298        {
00299          int idx = toolMgr()->index( aid, tid );
00300          if ( idx >= 0 )
00301          {
00302            myMultiActions[ tid ].append( ma );
00303            toolMgr()->remove( aid, tid );
00304            foreach( QAction* a, ma->actions() ) toolMgr()->insert( a, tid, idx++ );
00305          }
00306        }
00307       }
00308     }
00309   }
00310 }
00311 
00317 bool SUIT_ViewWindow::dropDownButtons() const
00318 {
00319   return myIsDropDown;
00320 }
00321 
00325 int SUIT_ViewWindow::getId() const
00326 {
00327   return int(long(this));
00328 }