Salome HOME
updated copyright message
[modules/gui.git] / tools / PyEditor / src / PyEditor_SettingsDlg.cxx
1 // Copyright (C) 2015-2023  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   // . Editor settings <start>
89   QGroupBox* aEditorSetBox = new QGroupBox( tr( "GR_EDIT_SET" ), this );
90   QGridLayout* aEditorSetLayout = new QGridLayout( aEditorSetBox );
91   aEditorSetLayout->addWidget( new QLabel( tr( "LBL_COMPLETION_MODE" ), aEditorSetBox ), 0, 0 );
92   aEditorSetLayout->addWidget( myCompletionPolicy = new QComboBox( aEditorSetBox ), 0, 1 );
93   myCompletionPolicy->addItems( QStringList() << tr( "LBL_NONE" ) << tr( "LBL_AUTO" )
94                                 << tr( "LBL_MANUAL" ) << tr( "LBL_ALWAYS" ) );
95   aMainLayout->addWidget( aEditorSetBox );
96   // . Editor settings <end>
97
98   // . Tab settings <start>
99   QGroupBox* aTabSetBox = new QGroupBox( tr( "GR_TAB_SET" ), this );
100   QVBoxLayout* aTabSetLayout = new QVBoxLayout( aTabSetBox );
101   myTabSpaceVisible = new QCheckBox( tr( "LBL_TAB_SPACES" ), aTabSetBox );
102   QHBoxLayout* aTabSizeLayout = new QHBoxLayout;
103   QLabel* aTabSizeLabel = new QLabel( tr( "LBL_TAB_SIZE" ), aTabSetBox );
104   myTabSize = new QSpinBox( aTabSetBox );
105   myTabSize->setMinimum( 0 );
106   myTabSize->setSingleStep( 1 );
107   aTabSizeLayout->addWidget( aTabSizeLabel );
108   aTabSizeLayout->addWidget( myTabSize );
109   aTabSizeLayout->addStretch( 1 );
110   aTabSetLayout->addWidget( myTabSpaceVisible );
111   aTabSetLayout->addLayout( aTabSizeLayout );
112   // . Tab settings <end>
113
114   // . Vertical edge settings <start>
115   QGroupBox* aVertEdgeSetBox = new QGroupBox( tr( "GR_VERT_EDGE_SET" ), this );
116   QVBoxLayout* aVertEdgeLayout = new QVBoxLayout( aVertEdgeSetBox );
117   myVerticalEdge = new QCheckBox( tr( "LBL_VERT_EDGE" ), aVertEdgeSetBox );
118   QHBoxLayout* aNumberColLayout = new QHBoxLayout;
119   myNumberColumnsLbl = new QLabel( tr( "LBL_NUM_COLUMNS" ), aVertEdgeSetBox );
120   myNumberColumns = new QSpinBox( aVertEdgeSetBox );
121   myNumberColumns->setMinimum( 0 );
122   myNumberColumns->setSingleStep( 1 );
123   aNumberColLayout->addWidget( myNumberColumnsLbl );
124   aNumberColLayout->addWidget( myNumberColumns );
125   aNumberColLayout->addStretch( 1 );
126   aVertEdgeLayout->addWidget( myVerticalEdge );
127   aVertEdgeLayout->addLayout( aNumberColLayout );
128   connect( myVerticalEdge, SIGNAL( clicked( bool ) ), 
129            this, SLOT( onVerticalEdgeChecked() ) );
130   // . Vertical edge settings <end>
131
132   QHBoxLayout* aTabVertEdgeLayout = new QHBoxLayout;
133   aTabVertEdgeLayout->addWidget( aTabSetBox );
134   aTabVertEdgeLayout->addWidget( aVertEdgeSetBox );
135   aMainLayout->addLayout( aTabVertEdgeLayout );
136
137   // . "Set as default" check box <start>
138   if ( PyEditor_Settings::settings() )
139   {
140     myDefaultCheck = new QCheckBox( tr( "WDG_SET_AS_DEFAULT_CHECK" ), this );
141     aMainLayout->addWidget( myDefaultCheck );
142   }
143   else
144   {
145     myDefaultCheck = 0;
146   }
147   // . "Set as default" check box <end>
148
149   // . Control buttons <start>
150   QHBoxLayout* aButtonLayout = new QHBoxLayout;
151
152   QPushButton* okBtn = new QPushButton( tr( "BUT_OK" ), this );
153   okBtn->setAutoDefault( true );
154   okBtn->setDefault( true );
155   connect( okBtn, SIGNAL( clicked() ), this, SLOT( onOk() ) );
156   aButtonLayout->addWidget( okBtn );
157
158   if ( PyEditor_Settings::settings() )
159   {
160     QPushButton* defBtn = new QPushButton( tr( "BUT_DEFAULTS" ), this );
161     defBtn->setAutoDefault( true );
162     connect( defBtn, SIGNAL( clicked() ), this, SLOT( onDefault() ) );
163     aButtonLayout->addStretch();
164     aButtonLayout->addWidget( defBtn );
165   }
166
167   QPushButton* cancelBtn = new QPushButton( tr( "BUT_CANCEL" ), this );
168   cancelBtn->setAutoDefault( true );
169   connect( cancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
170   aButtonLayout->addStretch();
171   aButtonLayout->addWidget( cancelBtn );
172
173   if ( showHelp )
174   {
175     QPushButton* helpBtn = new QPushButton( tr( "BUT_HELP" ), this );
176     helpBtn->setAutoDefault( true );
177     connect( helpBtn, SIGNAL( clicked() ), this, SLOT( onHelp() ) );
178     aButtonLayout->addWidget( helpBtn );
179   }
180   aMainLayout->addStretch( 1 );
181   aMainLayout->addLayout( aButtonLayout );
182   // . Control buttons <end>
183   
184   settingsToGui();
185 }
186
187 /*!
188   Destructor.
189 */
190 PyEditor_SettingsDlg::~PyEditor_SettingsDlg()
191 {
192 }
193
194 /*!
195   SLOT: Changes the widget visibility depending on the set theState flag.
196   \param theState flag of visibility
197  */
198 void PyEditor_SettingsDlg::onVerticalEdgeChecked()
199 {
200   myNumberColumnsLbl->setEnabled( myVerticalEdge->isChecked() );
201   myNumberColumns->setEnabled( myVerticalEdge->isChecked() );
202 }
203
204 /*!
205   SLOT: Fills font sizesc combo-box with available values depending on the
206   chosen font family.
207  */
208 void PyEditor_SettingsDlg::onFontChanged()
209 {
210   bool blocked = myFontSize->blockSignals( true );
211
212   QString oldSize = myFontSize->currentText();
213
214   QList<int> szList = QFontDatabase().pointSizes( myFontFamily->currentFont().family() );
215   QStringList sizes;
216   foreach ( int size, szList )
217     sizes.append( QString::number( size ) );
218
219   myFontSize->clear();
220   myFontSize->addItems( sizes );
221   setFontSize( oldSize );
222
223   myFontSize->blockSignals( blocked );
224 }
225
226 /*!
227   \brief Sets settings from preferences dialog.
228  */
229 void PyEditor_SettingsDlg::settingsFromGui()
230 {
231   const PyEditor_Settings& oldSettings = myEditor->settings();
232   PyEditor_Settings settings;
233   
234   QFont font = oldSettings.font();
235   font.setFamily( myFontFamily->currentFont().family() );
236   bool ok;
237   int size = myFontSize->currentText().toInt( &ok );
238   if ( ok )
239     font.setPointSize( size );
240
241   settings.setHighlightCurrentLine( myHighlightCurrentLine->isChecked() );
242   settings.setTextWrapping( myTextWrapping->isChecked() );
243   settings.setCenterCursorOnScroll( myCenterCursorOnScroll->isChecked() );
244   settings.setLineNumberArea( myLineNumberArea->isChecked() );
245   settings.setTabSpaceVisible( myTabSpaceVisible->isChecked() );
246   settings.setTabSize( myTabSize->value() );
247   settings.setVerticalEdge( myVerticalEdge->isChecked() );
248   settings.setNumberColumns( myNumberColumns->value() );
249   settings.setFont( font );
250   settings.setCompletionPolicy( myCompletionPolicy->currentIndex() );
251   myEditor->setSettings(settings); // updateContent()
252
253   PyEditor_Settings* globals = PyEditor_Settings::settings();
254   if ( globals && myDefaultCheck && myDefaultCheck->isChecked() )
255     globals->copyFrom( settings );
256 }
257
258 /*!
259   \brief Sets settings into preferences dialog.
260  */
261 void PyEditor_SettingsDlg::settingsToGui()
262 {
263   const PyEditor_Settings& settings = myEditor->settings();
264
265   myHighlightCurrentLine->setChecked( settings.highlightCurrentLine() );
266   myTextWrapping->setChecked( settings.textWrapping() );
267   myCenterCursorOnScroll->setChecked( settings.centerCursorOnScroll() );
268   myLineNumberArea->setChecked( settings.lineNumberArea() );
269   myTabSpaceVisible->setChecked( settings.tabSpaceVisible() );
270   myTabSize->setValue( settings.tabSize() );
271   myVerticalEdge->setChecked( settings.verticalEdge() );
272   myNumberColumns->setValue( settings.numberColumns() );
273   myFontFamily->setCurrentFont( settings.font() );
274   setFontSize( QString::number( settings.font().pointSize() ) );
275   myCompletionPolicy->setCurrentIndex( settings.completionPolicy() );
276
277   onVerticalEdgeChecked();
278   onFontChanged();
279 }
280
281 /*!
282   \brief Set font size value to the combo box.
283   \param size new size value
284 */
285 void PyEditor_SettingsDlg::setFontSize( const QString& size )
286 {
287   int idx = myFontSize->findText( size );
288   if ( idx != -1 )
289     myFontSize->setCurrentIndex( idx );
290   else
291     myFontSize->setEditText( size );
292 }
293
294 /*!
295   Slot, called when user clicks "OK" button
296 */
297 void PyEditor_SettingsDlg::onOk()
298 {
299   settingsFromGui();
300   accept();
301 }
302
303 /*!
304   Slot, called when user clicks "Defaults" button
305 */
306 void PyEditor_SettingsDlg::onDefault()
307 {
308   PyEditor_Settings* settings = PyEditor_Settings::settings();
309   if ( settings )
310   {
311     settings->load(); // to reload from the resources
312     
313     QFont font = settings->font();
314
315     myHighlightCurrentLine->setChecked( settings->highlightCurrentLine() );
316     myTextWrapping->setChecked( settings->textWrapping() );
317     myCenterCursorOnScroll->setChecked( settings->centerCursorOnScroll() );
318     myLineNumberArea->setChecked( settings->lineNumberArea() );
319     myTabSpaceVisible->setChecked( settings->tabSpaceVisible() );
320     myTabSize->setValue( settings->tabSize() );
321     myVerticalEdge->setChecked( settings->verticalEdge() );
322     myNumberColumns->setValue( settings->numberColumns() );
323     myFontFamily->setCurrentFont( font );
324     setFontSize( QString::number( font.pointSize() ) );
325     
326     onVerticalEdgeChecked();
327     onFontChanged();
328   }
329 }
330
331 /*!
332   Slot, called when user clicks "Help" button.
333   Emits help() signal.
334 */
335 void PyEditor_SettingsDlg::onHelp()
336 {
337   emit help();
338 }