Salome HOME
804cbfe68bc3ffefc70c5160f2a389855805902c
[modules/gui.git] / src / LightApp / LightApp_PreferencesDlg.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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
23 // File:      LightApp_PreferencesDlg.cxx
24 // Author:    Sergey TELKOV
25 //
26 #include "LightApp_PreferencesDlg.h"
27 #include "LightApp_Preferences.h"
28
29 #include "QtxResourceMgr.h"
30
31 #include <SUIT_Session.h>
32 #include <SUIT_Application.h>
33 #include <SUIT_MessageBox.h>
34 #include <SUIT_FileDlg.h>
35
36 #include <QAbstractButton>
37 #include <QVBoxLayout>
38 #include <QFileDialog>
39
40 /*!
41   Constructor.
42 */
43 LightApp_PreferencesDlg::LightApp_PreferencesDlg( LightApp_Preferences* prefs, QWidget* parent )
44 : QtxDialog( parent, true, true, OK | Close | Apply | Help ),
45 myPrefs( prefs ), mySaved ( false )
46 {
47   setWindowTitle( tr( "CAPTION" ) );
48
49   QVBoxLayout* main = new QVBoxLayout( mainFrame() );
50   main->setMargin( 0 );
51   main->setSpacing( 5 );
52   main->addWidget( myPrefs );
53
54   setFocusProxy( myPrefs );
55   myPrefs->setFrameStyle( QFrame::Box | QFrame::Sunken );
56   myPrefs->show();
57
58   setButtonPosition( Right, Close );
59
60   setDialogFlags( AlignOnce );
61
62   connect( this, SIGNAL( dlgHelp() ),  this, SLOT( onHelp() ) );
63   connect( this, SIGNAL( dlgApply() ), this, SLOT( onApply() ) );
64
65   QAbstractButton* defBtn = userButton( insertButton( tr( "DEFAULT_BTN_TEXT" ) ) );
66   if ( defBtn )
67     connect( defBtn, SIGNAL( clicked() ), this, SLOT( onDefault() ) );
68   QAbstractButton* impBtn = userButton( insertButton( tr( "IMPORT_BTN_TEXT" ) ) );
69   if( impBtn )
70     connect( impBtn, SIGNAL( clicked() ), this, SLOT( onImportPref() ) );
71   connect( this, SIGNAL( defaultPressed() ), prefs, SIGNAL( resetToDefaults() ) );
72
73   connect( prefs, SIGNAL( restartRequired() ), this, SLOT( onRestartRequired() ) );
74
75   setMinimumSize( 800, 600 );
76 }
77
78 /*!
79   Destructor.
80 */
81 LightApp_PreferencesDlg::~LightApp_PreferencesDlg()
82 {
83   if ( !myPrefs )
84     return;
85
86   mainFrame()->layout()->removeWidget( myPrefs );
87   myPrefs->setParent( 0 );
88   myPrefs->hide();
89   myPrefs = 0;
90 }
91
92 /*!Show/hide dialog.*/
93 void LightApp_PreferencesDlg::setVisible(bool visible)
94 {
95   if ( visible ) {
96     myPrefs->retrieve();
97     myPrefs->toBackup();
98   }
99
100   QtxDialog::setVisible(visible);
101 }
102
103 /*!Store preferences on accept.*/
104 void LightApp_PreferencesDlg::accept()
105 {
106   QtxDialog::accept();
107
108   myPrefs->store();
109   mySaved = true;
110 }
111
112 /*!Reject. Restore preferences from backup.*/
113 void LightApp_PreferencesDlg::reject()
114 {
115   QtxDialog::reject();
116
117   myPrefs->fromBackup();
118 }
119
120 /*!Do nothing.*/
121 void LightApp_PreferencesDlg::onHelp()
122 {
123   SUIT_Application* app = SUIT_Session::session()->activeApplication();
124   if ( app )
125     app->onHelpContextModule( "GUI", "setting_preferences.html" );
126 }
127
128 /*!Store preferences on apply.*/
129 void LightApp_PreferencesDlg::onApply()
130 {
131   myPrefs->store();
132
133   // Fix for Bug PAL11197: Restoring the corrected values from resource manager.
134   // (Correcting in VisuGUI.cxx and SMESHGUI.cxx in methods
135   // ::preferencesChanged( const QString& sect, const QString& name ))
136   myPrefs->retrieve();
137   //
138
139   myPrefs->toBackup();
140   mySaved = true;
141 }
142
143 /*! Restore default preferences*/
144 void LightApp_PreferencesDlg::onDefault()
145 {
146   if ( SUIT_MessageBox::Ok == SUIT_MessageBox::question( this, tr( "WARNING" ), tr( "DEFAULT_QUESTION" ),
147                                                          SUIT_MessageBox::Ok | SUIT_MessageBox::Cancel,
148                                                          SUIT_MessageBox::Ok ) )
149   {
150     if ( myPrefs && myPrefs->resourceMgr() )
151     {
152       QtxResourceMgr::WorkingMode prev = myPrefs->resourceMgr()->setWorkingMode( QtxResourceMgr::IgnoreUserValues );
153       myPrefs->retrieve();
154       myPrefs->resourceMgr()->setWorkingMode( prev );
155     }
156     emit defaultPressed();
157   }
158 }
159
160 /*! Import preferences from some file */
161 void LightApp_PreferencesDlg::onImportPref()
162 {
163   QtxResourceMgr* mgr = myPrefs->resourceMgr();
164   QStringList filtersList;
165   filtersList.append(tr("XML_FILES_FILTER"));
166   QString anInitialPath = "";
167   if ( SUIT_FileDlg::getLastVisitedPath().isEmpty() )
168     anInitialPath = QDir::currentPath();
169
170   QString aName = SUIT_FileDlg::getFileName( this, anInitialPath, filtersList, tr("IMPORT_PREFERENCES"), true, true );
171
172   if( mgr->import( aName ) )
173   {
174     myPrefs->retrieve();
175     myPrefs->toBackup();
176   }
177 }
178
179 /*! Called if some preferences that will come in force only after application restart are changed */
180 void LightApp_PreferencesDlg::onRestartRequired()
181 {
182   SUIT_MessageBox::information( this, tr( "WRN_WARNING" ), tr( "PREF_NEED_RESTART" ) );
183 }