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