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