Salome HOME
refs #1458: disable chained panning on operations
[modules/gui.git] / tools / PyEditor / src / PyEditor_SettingsDlg.cxx
1 // Copyright (C) 2015-2016  OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // File   : PyEditor_SettingsDlg.cxx
20 // Author : Maxim GLIBIN, Open CASCADE S.A.S. (maxim.glibin@opencascade.com)
21 //
22
23 #include "PyEditor_SettingsDlg.h"
24
25 #include "PyEditor_Editor.h"
26 #include "PyEditor_Settings.h"
27
28 #include <QCheckBox>
29 #include <QComboBox>
30 #include <QFontComboBox>
31 #include <QGroupBox>
32 #include <QLabel>
33 #include <QPushButton>
34 #include <QSpinBox>
35 #include <QVBoxLayout>
36
37 /*!
38   \class PyEditor_SettingsDlg
39   \brief Dialog settings for python editor.
40 */
41
42 /*!
43   \brief Constructor.
44   \param theEditor widget that is used to edit and display text
45   \param theParent parent widget
46 */
47 PyEditor_SettingsDlg::PyEditor_SettingsDlg( PyEditor_Editor* theEditor,
48                                             bool showHelp, QWidget* theParent ) :
49   QDialog( theParent ),
50   myEditor( theEditor )
51 {
52   setWindowTitle( tr("TIT_PREFERENCES") );
53   QVBoxLayout* aMainLayout = new QVBoxLayout( this );
54   
55   // . Font settings <start>
56   QGroupBox* aFontSetBox = new QGroupBox( tr( "GR_FONT_SET" ), this );
57   QHBoxLayout* aFontSetLayout = new QHBoxLayout( aFontSetBox );
58   myFontFamily = new QFontComboBox( aFontSetBox );
59   myFontFamily->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
60   myFontSize = new QComboBox( aFontSetBox );
61   myFontSize->setInsertPolicy( QComboBox::NoInsert );
62   myFontSize->setEditable( true );
63   myFontSize->setValidator( new QIntValidator( 1, 250, myFontSize ) );
64   myFontSize->setSizeAdjustPolicy( QComboBox::AdjustToContents );
65   myFontSize->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed );
66   aFontSetLayout->addWidget( myFontFamily );
67   aFontSetLayout->addWidget( myFontSize );
68   connect( myFontFamily, SIGNAL( currentFontChanged( QFont ) ),
69            this, SLOT( onFontChanged() ) );
70   aMainLayout->addWidget( aFontSetBox );
71   // . Font settings <end>
72   
73   // . Display settings <start>
74   QGroupBox* aDisplaySetBox = new QGroupBox( tr( "GR_DISP_SET" ), this );
75   QVBoxLayout* aDisplaySetLayout = new QVBoxLayout( aDisplaySetBox );
76   myHighlightCurrentLine = new QCheckBox( tr( "LBL_CURRLINE_HIGHLIGHT" ), aDisplaySetBox );
77   myTextWrapping = new QCheckBox( tr( "LBL_TEXT_WRAP" ), aDisplaySetBox );
78   myCenterCursorOnScroll = new QCheckBox( tr( "LBL_CURSOR_SCROLL" ), aDisplaySetBox );
79   myLineNumberArea = new QCheckBox( tr( "LBL_LINE_NUMBS_AREA" ), aDisplaySetBox );
80   aDisplaySetLayout->addWidget( myHighlightCurrentLine );
81   aDisplaySetLayout->addWidget( myTextWrapping );
82   aDisplaySetLayout->addWidget( myCenterCursorOnScroll );
83   aDisplaySetLayout->addWidget( myLineNumberArea );
84   aDisplaySetLayout->addStretch( 1 );
85   aMainLayout->addWidget( aDisplaySetBox );
86   // . Display settings <end>
87
88   // . Tab settings <start>
89   QGroupBox* aTabSetBox = new QGroupBox( tr( "GR_TAB_SET" ), this );
90   QVBoxLayout* aTabSetLayout = new QVBoxLayout( aTabSetBox );
91   myTabSpaceVisible = new QCheckBox( tr( "LBL_TAB_SPACES" ), aTabSetBox );
92   QHBoxLayout* aTabSizeLayout = new QHBoxLayout;
93   QLabel* aTabSizeLabel = new QLabel( tr( "LBL_TAB_SIZE" ), aTabSetBox );
94   myTabSize = new QSpinBox( aTabSetBox );
95   myTabSize->setMinimum( 0 );
96   myTabSize->setSingleStep( 1 );
97   aTabSizeLayout->addWidget( aTabSizeLabel );
98   aTabSizeLayout->addWidget( myTabSize );
99   aTabSizeLayout->addStretch( 1 );
100   aTabSetLayout->addWidget( myTabSpaceVisible );
101   aTabSetLayout->addLayout( aTabSizeLayout );
102   // . Tab settings <end>
103
104   // . Vertical edge settings <start>
105   QGroupBox* aVertEdgeSetBox = new QGroupBox( tr( "GR_VERT_EDGE_SET" ), this );
106   QVBoxLayout* aVertEdgeLayout = new QVBoxLayout( aVertEdgeSetBox );
107   myVerticalEdge = new QCheckBox( tr( "LBL_VERT_EDGE" ), aVertEdgeSetBox );
108   QHBoxLayout* aNumberColLayout = new QHBoxLayout;
109   myNumberColumnsLbl = new QLabel( tr( "LBL_NUM_COLUMNS" ), aVertEdgeSetBox );
110   myNumberColumns = new QSpinBox( aVertEdgeSetBox );
111   myNumberColumns->setMinimum( 0 );
112   myNumberColumns->setSingleStep( 1 );
113   aNumberColLayout->addWidget( myNumberColumnsLbl );
114   aNumberColLayout->addWidget( myNumberColumns );
115   aNumberColLayout->addStretch( 1 );
116   aVertEdgeLayout->addWidget( myVerticalEdge );
117   aVertEdgeLayout->addLayout( aNumberColLayout );
118   connect( myVerticalEdge, SIGNAL( clicked( bool ) ), 
119            this, SLOT( onVerticalEdgeChecked() ) );
120   // . Vertical edge settings <end>
121
122   QHBoxLayout* aTabVertEdgeLayout = new QHBoxLayout;
123   aTabVertEdgeLayout->addWidget( aTabSetBox );
124   aTabVertEdgeLayout->addWidget( aVertEdgeSetBox );
125   aMainLayout->addLayout( aTabVertEdgeLayout );
126
127   // . "Set as default" check box <start>
128   if ( PyEditor_Settings::settings() )
129   {
130     myDefaultCheck = new QCheckBox( tr( "WDG_SET_AS_DEFAULT_CHECK" ), this );
131     aMainLayout->addWidget( myDefaultCheck );
132   }
133   else
134   {
135     myDefaultCheck = 0;
136   }
137   // . "Set as default" check box <end>
138
139   // . Control buttons <start>
140   QHBoxLayout* aButtonLayout = new QHBoxLayout;
141
142   QPushButton* okBtn = new QPushButton( tr( "BUT_OK" ), this );
143   okBtn->setAutoDefault( true );
144   okBtn->setDefault( true );
145   connect( okBtn, SIGNAL( clicked() ), this, SLOT( onOk() ) );
146   aButtonLayout->addWidget( okBtn );
147
148   if ( PyEditor_Settings::settings() )
149   {
150     QPushButton* defBtn = new QPushButton( tr( "BUT_DEFAULTS" ), this );
151     defBtn->setAutoDefault( true );
152     connect( defBtn, SIGNAL( clicked() ), this, SLOT( onDefault() ) );
153     aButtonLayout->addStretch();
154     aButtonLayout->addWidget( defBtn );
155   }
156
157   QPushButton* cancelBtn = new QPushButton( tr( "BUT_CANCEL" ), this );
158   cancelBtn->setAutoDefault( true );
159   connect( cancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
160   aButtonLayout->addStretch();
161   aButtonLayout->addWidget( cancelBtn );
162
163   if ( showHelp )
164   {
165     QPushButton* helpBtn = new QPushButton( tr( "BUT_HELP" ), this );
166     helpBtn->setAutoDefault( true );
167     connect( helpBtn, SIGNAL( clicked() ), this, SLOT( onHelp() ) );
168     aButtonLayout->addWidget( helpBtn );
169   }
170   aMainLayout->addStretch( 1 );
171   aMainLayout->addLayout( aButtonLayout );
172   // . Control buttons <end>
173   
174   settingsToGui();
175 }
176
177 /*!
178   Destructor.
179 */
180 PyEditor_SettingsDlg::~PyEditor_SettingsDlg()
181 {
182 }
183
184 /*!
185   SLOT: Changes the widget visibility depending on the set theState flag.
186   \param theState flag of visibility
187  */
188 void PyEditor_SettingsDlg::onVerticalEdgeChecked()
189 {
190   myNumberColumnsLbl->setEnabled( myVerticalEdge->isChecked() );
191   myNumberColumns->setEnabled( myVerticalEdge->isChecked() );
192 }
193
194 /*!
195   SLOT: Fills font sizesc combo-box with available values depending on the
196   chosen font family.
197  */
198 void PyEditor_SettingsDlg::onFontChanged()
199 {
200   bool blocked = myFontSize->blockSignals( true );
201
202   QString oldSize = myFontSize->currentText();
203
204   QList<int> szList = QFontDatabase().pointSizes( myFontFamily->currentFont().family() );
205   QStringList sizes;
206   foreach ( int size, szList )
207     sizes.append( QString::number( size ) );
208
209   myFontSize->clear();
210   myFontSize->addItems( sizes );
211   setFontSize( oldSize );
212
213   myFontSize->blockSignals( blocked );
214 }
215
216 /*!
217   \brief Sets settings from preferences dialog.
218  */
219 void PyEditor_SettingsDlg::settingsFromGui()
220 {
221   const PyEditor_Settings& oldSettings = myEditor->settings();
222   PyEditor_Settings settings;
223   
224   QFont font = oldSettings.font();
225   font.setFamily( myFontFamily->currentFont().family() );
226   bool ok;
227   int size = myFontSize->currentText().toInt( &ok );
228   if ( ok )
229     font.setPointSize( size );
230
231   settings.setHighlightCurrentLine( myHighlightCurrentLine->isChecked() );
232   settings.setTextWrapping( myTextWrapping->isChecked() );
233   settings.setCenterCursorOnScroll( myCenterCursorOnScroll->isChecked() );
234   settings.setLineNumberArea( myLineNumberArea->isChecked() );
235   settings.setTabSpaceVisible( myTabSpaceVisible->isChecked() );
236   settings.setTabSize( myTabSize->value() );
237   settings.setVerticalEdge( myVerticalEdge->isChecked() );
238   settings.setNumberColumns( myNumberColumns->value() );
239   settings.setFont( font );
240   myEditor->setSettings(settings); // updateContent()
241
242   PyEditor_Settings* globals = PyEditor_Settings::settings();
243   if ( globals && myDefaultCheck && myDefaultCheck->isChecked() )
244     globals->copyFrom( settings );
245 }
246
247 /*!
248   \brief Sets settings into preferences dialog.
249  */
250 void PyEditor_SettingsDlg::settingsToGui()
251 {
252   const PyEditor_Settings& settings = myEditor->settings();
253
254   myHighlightCurrentLine->setChecked( settings.highlightCurrentLine() );
255   myTextWrapping->setChecked( settings.textWrapping() );
256   myCenterCursorOnScroll->setChecked( settings.centerCursorOnScroll() );
257   myLineNumberArea->setChecked( settings.lineNumberArea() );
258   myTabSpaceVisible->setChecked( settings.tabSpaceVisible() );
259   myTabSize->setValue( settings.tabSize() );
260   myVerticalEdge->setChecked( settings.verticalEdge() );
261   myNumberColumns->setValue( settings.numberColumns() );
262   myFontFamily->setCurrentFont( settings.font() );
263   setFontSize( QString::number( settings.font().pointSize() ) );
264
265   onVerticalEdgeChecked();
266   onFontChanged();
267 }
268
269 /*!
270   \brief Set font size value to the combo box.
271   \param size new size value
272 */
273 void PyEditor_SettingsDlg::setFontSize( const QString& size )
274 {
275   int idx = myFontSize->findText( size );
276   if ( idx != -1 )
277     myFontSize->setCurrentIndex( idx );
278   else
279     myFontSize->setEditText( size );
280 }
281
282 /*!
283   Slot, called when user clicks "OK" button
284 */
285 void PyEditor_SettingsDlg::onOk()
286 {
287   settingsFromGui();
288   accept();
289 }
290
291 /*!
292   Slot, called when user clicks "Defaults" button
293 */
294 void PyEditor_SettingsDlg::onDefault()
295 {
296   PyEditor_Settings* settings = PyEditor_Settings::settings();
297   if ( settings )
298   {
299     settings->load(); // to reload from the resources
300     
301     QFont font = settings->font();
302
303     myHighlightCurrentLine->setChecked( settings->highlightCurrentLine() );
304     myTextWrapping->setChecked( settings->textWrapping() );
305     myCenterCursorOnScroll->setChecked( settings->centerCursorOnScroll() );
306     myLineNumberArea->setChecked( settings->lineNumberArea() );
307     myTabSpaceVisible->setChecked( settings->tabSpaceVisible() );
308     myTabSize->setValue( settings->tabSize() );
309     myVerticalEdge->setChecked( settings->verticalEdge() );
310     myNumberColumns->setValue( settings->numberColumns() );
311     myFontFamily->setCurrentFont( font );
312     setFontSize( QString::number( font.pointSize() ) );
313     
314     onVerticalEdgeChecked();
315     onFontChanged();
316   }
317 }
318
319 /*!
320   Slot, called when user clicks "Help" button.
321   Emits help() signal.
322 */
323 void PyEditor_SettingsDlg::onHelp()
324 {
325   emit help();
326 }