X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FPyViewer%2FPyViewer_ViewWindow.cxx;h=9436968f447858c25ca5a1b650cd5894ea4bc507;hb=41aded95c12ed86b77866d9dfe5212fa66869ab2;hp=47a9a4582c4d26d3d74ba8bd33b60871fa9efdf6;hpb=8d6a98a5c2f66655d1f4738afc7fffd914e59ad4;p=modules%2Fgui.git diff --git a/src/PyViewer/PyViewer_ViewWindow.cxx b/src/PyViewer/PyViewer_ViewWindow.cxx index 47a9a4582..9436968f4 100644 --- a/src/PyViewer/PyViewer_ViewWindow.cxx +++ b/src/PyViewer/PyViewer_ViewWindow.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2015 OPEN CASCADE +// Copyright (C) 2015-2016 OPEN CASCADE // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -22,23 +22,21 @@ #include "PyViewer_ViewWindow.h" -#include "PyEditor_Editor.h" -#include "PyEditor_Settings.h" +#include "PyEditor_Widget.h" #include "PyEditor_SettingsDlg.h" -#include -#include +#include "SUIT_Session.h" +#include "SUIT_ResourceMgr.h" -#include -#include -#include +#include "QtxAction.h" +#include "QtxActionToolMgr.h" -#include -#include +#include +#include #include #include -#include -#include +#include +#include /*! \class PyViewer_ViewWindow @@ -47,291 +45,243 @@ /*! \brief Constructor. - \param theParent parent widget + \param desktop SALOME desktop window */ -PyViewer_ViewWindow::PyViewer_ViewWindow( SUIT_Desktop* theDesktop , PyViewer_Viewer* theModel ) : - SUIT_ViewWindow(theDesktop), - myModel(theModel) -{ - my_IsExternal = (theDesktop == NULL); - - if( isExternal() ) - initLayout(); -} - -void PyViewer_ViewWindow::initLayout() +PyViewer_ViewWindow::PyViewer_ViewWindow( SUIT_Desktop* desktop ) : + SUIT_ViewWindow( desktop ) { - my_TextEditor = new PyEditor_Editor( my_IsExternal ,SUIT_Session::session()->resourceMgr(), this ); - setCentralWidget( my_TextEditor ); - - createActions(); - createToolBar(); + // Create central widget. + myEditor = new PyEditor_Widget( this ); + setCentralWidget( myEditor ); + + // Create actions. + SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr(); + QtxAction* action; + + // . New + action = new QtxAction( tr( "TTP_NEW" ), + resMgr->loadPixmap( "PyViewer", tr( "ICON_NEW" ) ), + tr( "ACT_NEW" ), 0, this ); + action->setStatusTip( tr( "DSC_NEW" ) ); + action->setShortcut( QKeySequence::New ); + connect( action, SIGNAL( triggered( bool ) ), this, SLOT( onNew() ) ); + toolMgr()->registerAction( action, NewId ); + + // . Open + action = new QtxAction( tr( "TTP_OPEN" ), + resMgr->loadPixmap( "PyViewer", tr( "ICON_OPEN" ) ), + tr( "ACT_OPEN" ), 0, this ); + action->setStatusTip( tr( "DSC_OPEN" ) ); + action->setShortcut( QKeySequence::Open ); + connect( action, SIGNAL( triggered( bool ) ), this, SLOT( onOpen() ) ); + toolMgr()->registerAction( action, OpenId ); + + // . Save + action = new QtxAction( tr( "TTP_SAVE" ), + resMgr->loadPixmap( "PyViewer", tr( "ICON_SAVE" ) ), + tr( "ACT_SAVE" ), 0, this ); + action->setStatusTip( tr( "DSC_SAVE" ) ); + action->setShortcut( QKeySequence::Save ); + connect( action, SIGNAL( triggered( bool ) ), this, SLOT( onSave() ) ); + action->setEnabled( false ); + connect( myEditor, SIGNAL( modificationChanged( bool ) ), + action, SLOT( setEnabled( bool ) ) ); + toolMgr()->registerAction( action, SaveId ); + + // . SaveAs + action = new QtxAction( tr( "TTP_SAVEAS" ), + resMgr->loadPixmap( "PyViewer", tr( "ICON_SAVEAS" ) ), + tr( "ACT_SAVEAS" ), 0, this ); + action->setStatusTip( tr( "DSC_SAVEAS" ) ); + action->setShortcut( QKeySequence::SaveAs ); + connect( action, SIGNAL( triggered( bool ) ), this, SLOT( onSaveAs() ) ); + toolMgr()->registerAction( action, SaveAsId ); + + // . Undo + action = new QtxAction( tr( "TTP_UNDO" ), + resMgr->loadPixmap( "PyViewer", tr( "ICON_UNDO" ) ), + tr( "ACT_UNDO" ), 0, this ); + action->setStatusTip( tr( "DSC_UNDO" ) ); + action->setShortcut( QKeySequence::Undo ); + connect( action, SIGNAL( triggered( bool ) ), myEditor, SLOT( undo() ) ); + action->setEnabled( false ); + connect( myEditor, SIGNAL( undoAvailable( bool ) ), + action, SLOT( setEnabled( bool ) ) ); + toolMgr()->registerAction( action, UndoId ); + + // . Redo + action = new QtxAction( tr( "TTP_REDO" ), + resMgr->loadPixmap( "PyViewer", tr( "ICON_REDO" ) ), + tr( "ACT_REDO" ), 0, this ); + action->setStatusTip( tr( "DSC_REDO" ) ); + action->setShortcut( QKeySequence::Redo ); + connect( action, SIGNAL( triggered( bool ) ), myEditor, SLOT( redo() ) ); + action->setEnabled( false ); + connect( myEditor, SIGNAL( redoAvailable( bool ) ), + action, SLOT( setEnabled( bool ) ) ); + toolMgr()->registerAction( action, RedoId ); + + // . Cut + action = new QtxAction( tr( "TTP_CUT" ), + resMgr->loadPixmap( "PyViewer", tr( "ICON_CUT" ) ), + tr( "ACT_CUT" ), 0, this ); + action->setStatusTip( tr( "DSC_CUT" ) ); + action->setShortcut( QKeySequence::Cut ); + connect( action, SIGNAL( triggered( bool ) ), myEditor, SLOT( cut() ) ); + action->setEnabled( false ); + connect( myEditor, SIGNAL( copyAvailable( bool ) ), + action, SLOT( setEnabled( bool ) ) ); + toolMgr()->registerAction( action, CutId ); + + // . Copy + action = new QtxAction( tr( "TTP_COPY" ), + resMgr->loadPixmap( "PyViewer", tr( "ICON_COPY" ) ), + tr( "ACT_COPY" ), 0, this ); + action->setStatusTip( tr( "DSC_COPY" ) ); + action->setShortcut( QKeySequence::Copy ); + connect( action, SIGNAL( triggered( bool ) ), myEditor, SLOT( copy() ) ); + action->setEnabled( false ); + connect( myEditor, SIGNAL( copyAvailable( bool ) ), + action, SLOT( setEnabled( bool ) ) ); + toolMgr()->registerAction( action, CopyId ); + + // . Paste + action = new QtxAction( tr( "TTP_PASTE" ), + resMgr->loadPixmap( "PyViewer", tr( "ICON_PASTE" ) ), + tr( "ACT_PASTE" ), 0, this ); + action->setStatusTip( tr( "DSC_PASTE" ) ); + action->setShortcut( QKeySequence::Paste ); + connect( action, SIGNAL( triggered( bool ) ), myEditor, SLOT( paste() ) ); + toolMgr()->registerAction( action, PasteId ); + + // . Delete + action = new QtxAction( tr( "TTP_DELETE" ), + resMgr->loadPixmap( "PyViewer", tr( "ICON_DELETE" ) ), + tr( "ACT_DELETE" ), 0, this ); + action->setStatusTip( tr( "DSC_DELETE" ) ); + action->setShortcut( QKeySequence::Delete ); + connect( action, SIGNAL( triggered( bool ) ), myEditor, SLOT( deleteSelected() ) ); + action->setEnabled( false ); + connect( myEditor, SIGNAL( copyAvailable( bool ) ), + action, SLOT( setEnabled( bool ) ) ); + toolMgr()->registerAction( action, DeleteId ); + + // . SelectAll + action = new QtxAction( tr( "TTP_SELECT_ALL" ), + resMgr->loadPixmap( "PyViewer", tr( "ICON_SELECT_ALL" ) ), + tr( "ACT_SELECT_ALL" ), 0, this ); + action->setStatusTip( tr( "DSC_SELECT_ALL" ) ); + action->setShortcut( QKeySequence::SelectAll ); + connect( action, SIGNAL( triggered( bool ) ), myEditor, SLOT( selectAll() ) ); + toolMgr()->registerAction( action, SelectAllId ); + + // . Find + action = new QtxAction( tr( "TTP_FIND" ), + resMgr->loadPixmap( "PyViewer", tr( "ICON_FIND" ) ), + tr( "ACT_FIND" ), 0, this ); + action->setStatusTip( tr( "DSC_FIND" ) ); + action->setShortcut( QKeySequence::Find ); + action->setShortcutContext( Qt::WidgetShortcut ); + connect( action, SIGNAL( triggered( bool ) ), myEditor, SLOT( find() ) ); + toolMgr()->registerAction( action, FindId ); + + // . Replace + action = new QtxAction( tr( "TTP_REPLACE" ), + resMgr->loadPixmap( "PyViewer", tr( "ICON_REPLACE" ) ), + tr( "ACT_REPLACE" ), 0, this ); + action->setStatusTip( tr( "DSC_REPLACE" ) ); + action->setShortcuts( QList() << QKeySequence( "Ctrl+H" ) << QKeySequence( QKeySequence::Replace ) ); + action->setShortcutContext( Qt::WidgetShortcut ); + connect( action, SIGNAL( triggered( bool ) ), myEditor, SLOT( replace() ) ); + toolMgr()->registerAction( action, ReplaceId ); + + // . Preferences + action = new QtxAction( tr( "TTP_PREFERENCES" ), + resMgr->loadPixmap( "PyViewer", tr( "ICON_PREFERENCES" ) ), + tr( "ACT_PREFERENCES" ), 0, this ); + action->setStatusTip( tr( "DSC_PREFERENCES" ) ); + connect( action, SIGNAL( triggered( bool ) ), this, SLOT( onPreferences() ) ); + toolMgr()->registerAction( action, PreferencesId ); + + // . Help + action = new QtxAction( tr( "TTP_HELP" ), + resMgr->loadPixmap( "PyViewer", tr( "ICON_HELP" ) ), + tr( "ACT_HELP" ), 0, this ); + action->setStatusTip( tr( "DSC_HELP" ) ); + connect( action, SIGNAL( triggered() ), this, SLOT( onHelp() ) ); + toolMgr()->registerAction( action, HelpId ); + + // Create toolbar. + int idTB = toolMgr()->createToolBar( tr("TOOLBAR_LABEL"), QString( "PythonEditor" ), false ); + toolMgr()->append( NewId, idTB ); + toolMgr()->append( OpenId, idTB ); + toolMgr()->append( SaveId, idTB ); + toolMgr()->append( SaveAsId, idTB ); + toolMgr()->append( toolMgr()->separator(), idTB ); + toolMgr()->append( UndoId, idTB ); + toolMgr()->append( RedoId, idTB ); + toolMgr()->append( toolMgr()->separator(), idTB ); + toolMgr()->append( CutId, idTB ); + toolMgr()->append( CopyId, idTB ); + toolMgr()->append( PasteId, idTB ); + toolMgr()->append( DeleteId, idTB ); + toolMgr()->append( SelectAllId, idTB ); + toolMgr()->append( toolMgr()->separator(), idTB ); + toolMgr()->append( FindId, idTB ); + toolMgr()->append( ReplaceId, idTB ); + toolMgr()->append( toolMgr()->separator(), idTB ); + toolMgr()->append( PreferencesId, idTB ); + toolMgr()->append( toolMgr()->separator(), idTB ); + toolMgr()->append( HelpId, idTB ); + + // Set current file. setCurrentFile( QString() ); - - if ( isExternal() ) - { - connect( my_TextEditor->document(), SIGNAL( modificationChanged( bool ) ), - this, SLOT( setWindowModified( bool ) ) ); - - statusBar()->showMessage( tr("STS_READY") ); - } } /*! \brief Destructor. - */ -PyViewer_ViewWindow::~PyViewer_ViewWindow() -{ - my_CurrentFile.clear(); - delete my_TextEditor; -} - -/*! - \return \c true if the application is external - */ -bool PyViewer_ViewWindow::isExternal() -{ - return my_IsExternal; -} - -/*! - \brief Creates actions of Python view window. */ -void PyViewer_ViewWindow::createActions() -{ - QtxActionToolMgr* aMgr = toolMgr(); - QtxAction* anAction; - - // 1. File operations - // 1.1. Create New action - anAction = new QtxAction( tr( "MNU_PY_NEW" ), QIcon( ":/images/py_new.png" ), - tr( "MNU_PY_NEW" ), 0, this ); - anAction->setStatusTip( tr( "DSC_PY_NEW" ) ); - anAction->setShortcuts( QKeySequence::New ); - connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onNew() ) ); - aMgr->registerAction( anAction, NewId ); - - // 1.2 Create Open action - anAction = new QtxAction( tr( "MNU_PY_OPEN" ), QIcon( ":/images/py_open.png" ), - tr( "MNU_PY_OPEN" ), 0, this ); - anAction->setStatusTip( tr( "DSC_PY_OPEN" ) ); - anAction->setShortcuts( QKeySequence::Open ); - connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onOpen() ) ); - aMgr->registerAction( anAction, OpenId ); - - // 1.3. Create Save action - anAction = new QtxAction( tr( "MNU_PY_SAVE" ), QIcon( ":/images/py_save.png" ), - tr( "MNU_PY_SAVE" ), 0, this ); - anAction->setStatusTip( tr( "DSC_PY_SAVE" ) ); - anAction->setShortcuts( QKeySequence::Save ); - connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onSave() ) ); - aMgr->registerAction( anAction, SaveId ); - // Set default statement for Save action - anAction->setEnabled( my_TextEditor->document()->isModified() ); - connect( my_TextEditor->document(), SIGNAL( modificationChanged( bool ) ), - anAction, SLOT( setEnabled( bool ) ) ); - - // 1.4. Create SaveAs action - anAction = new QtxAction( tr( "MNU_PY_SAVEAS" ), QIcon( ":/images/py_save_as.png" ), - tr( "MNU_PY_SAVEAS" ), 0, this ); - anAction->setStatusTip( tr( "DSC_PY_SAVEAS" ) ); - anAction->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_S ); - connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onSaveAs() ) ); - aMgr->registerAction( anAction, SaveAsId ); - - // 1.5 Create multi-action for file operations - /*QtxMultiAction* aFileAction = new QtxMultiAction( this ); - aFileAction->insertAction( aMgr->action( NewId ) ); - aFileAction->insertAction( aMgr->action( OpenId ) ); - aFileAction->insertAction( aMgr->action( SaveId ) ); - aFileAction->insertAction( aMgr->action( SaveAsId ) ); - aMgr->registerAction( aFileAction, FileOpId );*/ - - // 1.6. Create Close action - if (isExternal()) - { - anAction = new QtxAction( tr( "MNU_PY_CLOSE" ), QIcon( ":/images/py_close.png" ), - tr( "MNU_PY_CLOSE" ), 0, this ); - anAction->setStatusTip( tr( "DSC_PY_CLOSE" ) ); - anAction->setShortcut( Qt::CTRL + Qt::Key_Q ); - connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( close() ) ); - aMgr->registerAction( anAction, CloseId ); - } - - // 2. Edit operations - // 2.1. Create Undo action - anAction = new QtxAction( tr( "MNU_PY_UNDO" ), QIcon( ":/images/py_undo.png" ), - tr( "MNU_PY_UNDO" ), 0, this ); - anAction->setStatusTip( tr( "DSC_PY_UNDO" ) ); - anAction->setShortcuts( QKeySequence::Undo ); - connect( anAction, SIGNAL( triggered( bool ) ), my_TextEditor, SLOT( undo() ) ); - aMgr->registerAction( anAction, UndoId ); - // Set default statement for Undo action - anAction->setEnabled( my_TextEditor->document()->isUndoAvailable() ); - connect( my_TextEditor->document(), SIGNAL( undoAvailable( bool ) ), - anAction, SLOT( setEnabled( bool ) ) ); - - // 2.2. Create Redo action - anAction = new QtxAction( tr( "MNU_PY_REDO" ), QIcon( ":/images/py_redo.png" ), - tr( "MNU_PY_REDO" ), 0, this ); - anAction->setStatusTip( tr( "DSC_PY_REDO" ) ); - anAction->setShortcuts( QKeySequence::Redo ); - connect( anAction, SIGNAL( triggered( bool ) ), my_TextEditor, SLOT( redo() ) ); - aMgr->registerAction( anAction, RedoId ); - // Set default statement for Redo action - anAction->setEnabled( my_TextEditor->document()->isRedoAvailable() ); - connect( my_TextEditor->document(), SIGNAL( redoAvailable( bool ) ), - anAction, SLOT( setEnabled( bool ) ) ); - - // 2.3. Create Cut action - anAction = new QtxAction( tr( "MNU_PY_CUT" ), QIcon( ":/images/py_cut.png" ), - tr( "MNU_PY_CUT" ), 0, this ); - anAction->setStatusTip( tr( "DSC_PY_CUT" ) ); - anAction->setShortcuts( QKeySequence::Cut ); - connect( anAction, SIGNAL( triggered( bool ) ), my_TextEditor, SLOT( cut() ) ); - aMgr->registerAction( anAction, CutId ); - // Set default statement for Cut action - anAction->setEnabled( false ); - connect( my_TextEditor, SIGNAL( copyAvailable( bool ) ), - anAction, SLOT( setEnabled( bool ) ) ); - - // 2.4. Create Copy action - anAction = new QtxAction( tr( "MNU_PY_COPY" ), QIcon( ":/images/py_copy.png" ), - tr( "MNU_PY_COPY" ), 0, this ); - anAction->setStatusTip( tr( "DSC_PY_COPY" ) ); - anAction->setShortcuts( QKeySequence::Copy ); - connect( anAction, SIGNAL( triggered( bool ) ), my_TextEditor, SLOT( copy() ) ); - aMgr->registerAction( anAction, CopyId ); - // Set default statement for Copy action - anAction->setEnabled( false ); - connect( my_TextEditor, SIGNAL( copyAvailable( bool ) ), - anAction, SLOT( setEnabled( bool ) ) ); - - // 2.5. Create Paste action - anAction = new QtxAction( tr( "MNU_PY_PASTE" ), QIcon( ":/images/py_paste.png" ), - tr( "MNU_PY_PASTE" ), 0, this ); - anAction->setStatusTip( tr( "DSC_PY_PASTE" ) ); - anAction->setShortcuts( QKeySequence::Paste ); - connect( anAction, SIGNAL( triggered( bool ) ), my_TextEditor, SLOT( paste() ) ); - aMgr->registerAction( anAction, PasteId ); - - // 2.6. Create Delete action - anAction = new QtxAction( tr( "MNU_PY_DELETE" ), QIcon( ":/images/py_delete.png" ), - tr( "MNU_PY_DELETE" ), 0, this ); - anAction->setStatusTip( tr( "DSC_PY_DELETE" ) ); - anAction->setShortcuts( QKeySequence::Delete ); - connect( anAction, SIGNAL( triggered( bool ) ), my_TextEditor, SLOT( deleteSelected() ) ); - aMgr->registerAction( anAction, DeleteId ); - // Set default statement for Delete action - anAction->setEnabled( false ); - connect( my_TextEditor, SIGNAL( copyAvailable( bool ) ), - anAction, SLOT( setEnabled( bool ) ) ); - - // 2.7. Create SelectAll action - anAction = new QtxAction( tr( "MNU_PY_SELECTALL" ), QIcon( ":/images/py_select_all.png" ), - tr( "MNU_PY_SELECTALL" ), 0, this ); - anAction->setStatusTip( tr( "DSC_PY_SELECT_ALL" ) ); - anAction->setShortcuts( QKeySequence::SelectAll ); - connect( anAction, SIGNAL( triggered( bool ) ), my_TextEditor, SLOT( selectAll() ) ); - aMgr->registerAction( anAction, SelectAllId ); - - // 2.8. Create multi-action for edit operations - /*QtxMultiAction* anEditAction = new QtxMultiAction( this ); - anEditAction->insertAction( aMgr->action( UndoId ) ); - anEditAction->insertAction( aMgr->action( RedoId ) ); - anEditAction->insertAction( aMgr->action( CutId ) ); - anEditAction->insertAction( aMgr->action( CopyId ) ); - anEditAction->insertAction( aMgr->action( PasteId ) ); - anEditAction->insertAction( aMgr->action( DeleteId ) ); - anEditAction->insertAction( aMgr->action( SelectAllId ) ); - aMgr->registerAction( anEditAction, EditOpId );*/ - - // 3. Create Preference action - anAction = new QtxAction( tr( "MNU_PY_PREFERENCES" ), QIcon( ":/images/py_preferences.png" ), - tr( "MNU_PY_PREFERENCES" ), 0, this ); - anAction->setStatusTip( tr( "DSC_PY_PREFERENCES" ) ); - connect( anAction, SIGNAL( triggered( bool ) ), this, SLOT( onPreferences() ) ); - aMgr->registerAction( anAction, PreferencesId ); - - // 4. Help operations - - // 4.1. Create Help action - anAction = new QtxAction( tr( "MNU_PY_BROWSER" ), QIcon( ":/images/py_browser.png" ), - tr( "MNU_PY_BROWSER" ), 0, this ); - anAction->setStatusTip( tr( "DSC_PY_BROWSER" ) ); - connect( anAction, SIGNAL( triggered() ), this, SLOT( onBrowser() ) ); - //aMgr->registerAction( anAction, BrowserId ); - - // 4.2. Create multi-action for help operations - /*QtxMultiAction* aHelpAction = new QtxMultiAction( this ); - aHelpAction->insertAction( aMgr->action( BrowserId ) ); - aMgr->registerAction( aHelpAction, HelpOpId );*/ -} - -/*! - \brief Create toolbar for the python view window. -*/ -void PyViewer_ViewWindow::createToolBar() +PyViewer_ViewWindow::~PyViewer_ViewWindow() { - QtxActionToolMgr* aMgr = toolMgr(); - int idTB = aMgr->createToolBar( tr("LBL_TOOLBAR_LABEL"), // title (language-dependent) - QString( "PyEditorOperations" ), // name (language-independent) - false ); // disable floatable toolbar - aMgr->append( NewId, idTB ); - aMgr->append( OpenId, idTB ); - aMgr->append( SaveId, idTB ); - aMgr->append( SaveAsId, idTB ); - if ( isExternal() ) - aMgr->append( CloseId, idTB ); - aMgr->append( aMgr->separator(), idTB ); - aMgr->append( UndoId, idTB ); - aMgr->append( RedoId, idTB ); - aMgr->append( aMgr->separator(), idTB ); - aMgr->append( CutId, idTB ); - aMgr->append( CopyId, idTB ); - aMgr->append( PasteId, idTB ); - aMgr->append( DeleteId, idTB ); - aMgr->append( SelectAllId, idTB ); - aMgr->append( aMgr->separator(), idTB ); - aMgr->append( PreferencesId, idTB ); - aMgr->append( aMgr->separator(), idTB ); - aMgr->append( BrowserId, idTB ); - } /*! - \brief Reimplemented class is to receive a window close request. - \param theEvent event + \brief Manage window close request. + \param event close event */ -void PyViewer_ViewWindow::closeEvent( QCloseEvent* theEvent ) +void PyViewer_ViewWindow::closeEvent( QCloseEvent* event ) { if ( whetherSave() ) - theEvent->accept(); + event->accept(); else - theEvent->ignore(); + event->ignore(); } /*! - SLOT: Creates a new document - */ + SLOT: Create new document +*/ void PyViewer_ViewWindow::onNew() { if ( whetherSave() ) { - my_TextEditor->clear(); + myEditor->clear(); setCurrentFile( QString() ); } } /*! - SLOT: Open an existing python file - */ + SLOT: Open existing Python file +*/ void PyViewer_ViewWindow::onOpen() { if ( whetherSave() ) { - QString aFilePath = QFileDialog::getOpenFileName( - this, tr( "TIT_DLG_OPEN" ), QDir::currentPath(), "Python Files (*.py)" ); + QString filter = tr( "TIT_PY_FILES" ); + filter += " (*.py)"; + QString aFilePath = QFileDialog::getOpenFileName( this, + tr( "TIT_DLG_OPEN" ), + QDir::currentPath(), + filter ); if ( !aFilePath.isEmpty() ) loadFile( aFilePath ); @@ -339,24 +289,29 @@ void PyViewer_ViewWindow::onOpen() } /*! - SLOT: Save the current file - */ + SLOT: Save current document +*/ bool PyViewer_ViewWindow::onSave() { - if ( my_CurrentFile.isEmpty() ) + if ( myURL.isEmpty() ) return onSaveAs(); else - return saveFile( my_CurrentFile ); + return saveFile( myURL ); } /*! - SLOT: Save the current file under a new name - */ + SLOT: Save current document under a new name +*/ bool PyViewer_ViewWindow::onSaveAs() { - QString aFilePath = QFileDialog::getSaveFileName( - this, tr( "TIT_DLG_SAVEAS" ), QDir::currentPath(), "Python Files (*.py)" ); + QString filter = tr( "TIT_PY_FILES" ); + filter += " (*.py)"; + QString url = myURL.isEmpty() ? defaultName() : myURL; + QString aFilePath = QFileDialog::getSaveFileName( this, + tr( "TIT_DLG_SAVE" ), + url, + filter ); if ( !aFilePath.isEmpty() ) return saveFile( aFilePath ); @@ -366,143 +321,120 @@ bool PyViewer_ViewWindow::onSaveAs() /*! SLOT: Open preferences dialog - */ +*/ void PyViewer_ViewWindow::onPreferences() { - PyEditor_SettingsDlg* aPage = new PyEditor_SettingsDlg( my_TextEditor, this ); - connect( aPage, SIGNAL( onHelpClicked() ), - this, SLOT( onHelp() ) ); - aPage->exec(); - delete aPage; + PyEditor_SettingsDlg dlg( myEditor->editor(), true, this ); + connect( &dlg, SIGNAL( help() ), this, SLOT( onHelp() ) ); + dlg.exec(); } /*! - \brief Set preferece values for view. - */ -void PyViewer_ViewWindow::setPreferences() + \brief Associate \a filePath with the current document + \param filePath document's file path +*/ +void PyViewer_ViewWindow::setCurrentFile( const QString& filePath ) { - my_TextEditor->settings()->readSettings(); - my_TextEditor->updateStatement(); + myURL = filePath; + myEditor->setModified( false ); } /*! - \brief Associates the theFilePath with the python view. - \param theFilePath file path - */ -void PyViewer_ViewWindow::setCurrentFile( const QString &theFilePath ) -{ - my_CurrentFile = theFilePath; - my_TextEditor->document()->setModified( false ); - - if ( isExternal() ) - { - setWindowModified( false ); - - QString aShownName = my_CurrentFile; - if ( my_CurrentFile.isEmpty() ) - aShownName = "untitled.py"; - setWindowFilePath( aShownName ); - - // Set window title with associated file path - QFileInfo anInfo( aShownName ); - setWindowTitle( "Python Viewer - " + anInfo.fileName() + "[*]" ); - } -} - -/*! - \brief Checks whether the file is modified. + \brief Check whether the file is modified. If it has the modifications then ask the user to save it. \return true if the document is saved. - */ +*/ bool PyViewer_ViewWindow::whetherSave() { - if ( my_TextEditor->document()->isModified() ) + if ( myEditor->isModified() ) { - QMessageBox::StandardButton aReturn; - aReturn = QMessageBox::warning( - this, tr( "TIT_DLG_SAVE" ),tr( "WRN_PY_SAVE_FILE" ), - QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel ); - - if ( aReturn == QMessageBox::Save ) + QMessageBox::StandardButton answer = QMessageBox::warning( this, + tr( "NAME_PYEDITOR" ), + tr( "WRN_SAVE_FILE" ), + QMessageBox::Save | + QMessageBox::Discard | + QMessageBox::Cancel ); + switch( answer ) + { + case QMessageBox::Save: return onSave(); - else if ( aReturn == QMessageBox::Cancel ) + case QMessageBox::Cancel: return false; + default: + break; + } } return true; } /*! - \brief Opens file. - \param theFilePath file path - */ -void PyViewer_ViewWindow::loadFile( const QString &theFilePath ) + \brief Open file. + \param filePath file path +*/ +void PyViewer_ViewWindow::loadFile( const QString& filePath ) { - QFile aFile( theFilePath ); + QFile aFile( filePath ); if ( !aFile.open(QFile::ReadOnly | QFile::Text) ) { QMessageBox::warning( this, tr( "NAME_PYEDITOR" ), - tr( "WRN_PY_READ_FILE" ).arg( theFilePath ).arg( aFile.errorString() ) ); + tr( "WRN_READ_FILE" ).arg( filePath ).arg( aFile.errorString() ) ); return; } QTextStream anInput( &aFile ); QApplication::setOverrideCursor( Qt::WaitCursor ); - my_TextEditor->setPlainText( anInput.readAll() ); + myEditor->setText( anInput.readAll() ); QApplication::restoreOverrideCursor(); - setCurrentFile( theFilePath ); + setCurrentFile( filePath ); aFile.close(); - if ( isExternal() ) - statusBar()->showMessage( tr( "STS_F_LOADED" ), 2000 ); + } /*! \brief Saves file. \param theFilePath file path - */ -bool PyViewer_ViewWindow::saveFile( const QString &theFilePath ) +*/ +bool PyViewer_ViewWindow::saveFile( const QString& filePath ) { - QFile aFile( theFilePath ); + QFile aFile( filePath ); if ( !aFile.open( QFile::WriteOnly | QFile::Text ) ) { QMessageBox::warning( this, tr( "NAME_PYEDITOR" ), - tr( "WRN_PY_WRITE_FILE" ).arg( theFilePath ).arg( aFile.errorString() ) ); + tr( "WRN_WRITE_FILE" ).arg( filePath ).arg( aFile.errorString() ) ); return false; } QTextStream anOutput( &aFile ); QApplication::setOverrideCursor( Qt::WaitCursor ); - anOutput << my_TextEditor->toPlainText(); + anOutput << myEditor->text(); QApplication::restoreOverrideCursor(); - setCurrentFile( theFilePath ); + setCurrentFile( filePath ); aFile.close(); - if ( isExternal() ) - statusBar()->showMessage( tr( "STS_F_SAVED" ), 2000 ); - return true; } /*! - \brief Opens help browser with python view help information. - */ -void PyViewer_ViewWindow::onBrowser() + Slot, called when user clicks "Help" button in "Preferences" dialog box. +*/ +void PyViewer_ViewWindow::onHelp() { - QDir appDir = QApplication::applicationDirPath(); - QStringList parameters; - parameters << QString( "--file=%1" ).arg( appDir.filePath( "pyeditor.html" ) ); - QProcess::startDetached( "HelpBrowser", parameters ); + SUIT_Application* app = SUIT_Session::session()->activeApplication(); + if ( app ) { + QString page = "python_viewer.html"; + if ( qobject_cast( sender() ) ) + page += "#custom-python-preferences"; + app->onHelpContextModule( "GUI", page ); + } } /*! - Slot, called when user clicks "Help" button in "Preferences" dialog box. + Get default name for Python file + \return default name */ -void PyViewer_ViewWindow::onHelp() +QString PyViewer_ViewWindow::defaultName() const { -#ifndef NO_SUIT - SUIT_Application* app = SUIT_Session::session()->activeApplication(); - if ( app ) - app->onHelpContextModule( "GUI", "python_viewer_page.html", "custom_python_preferences" ); -#endif + return tr( "NONAME" ); }