1 // Copyright (C) 2015-2016 OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // File : PyEditor_SettingsDlg.cxx
20 // Author : Maxim GLIBIN, Open CASCADE S.A.S. (maxim.glibin@opencascade.com)
23 #include "PyEditor_SettingsDlg.h"
25 #include "PyEditor_Editor.h"
26 #include "PyEditor_Settings.h"
30 #include <QFontComboBox>
33 #include <QPushButton>
35 #include <QVBoxLayout>
38 \class PyEditor_SettingsDlg
39 \brief Dialog settings for python editor.
44 \param theEditor widget that is used to edit and display text
45 \param theParent parent widget
47 PyEditor_SettingsDlg::PyEditor_SettingsDlg( PyEditor_Editor* theEditor,
48 bool showHelp, QWidget* theParent ) :
52 setWindowTitle( tr("TIT_PREFERENCES") );
53 QVBoxLayout* aMainLayout = new QVBoxLayout( this );
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>
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>
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>
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>
122 QHBoxLayout* aTabVertEdgeLayout = new QHBoxLayout;
123 aTabVertEdgeLayout->addWidget( aTabSetBox );
124 aTabVertEdgeLayout->addWidget( aVertEdgeSetBox );
125 aMainLayout->addLayout( aTabVertEdgeLayout );
127 // . "Set as default" check box <start>
128 if ( PyEditor_Settings::settings() )
130 myDefaultCheck = new QCheckBox( tr( "WDG_SET_AS_DEFAULT_CHECK" ), this );
131 aMainLayout->addWidget( myDefaultCheck );
137 // . "Set as default" check box <end>
139 // . Control buttons <start>
140 QHBoxLayout* aButtonLayout = new QHBoxLayout;
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 );
148 if ( PyEditor_Settings::settings() )
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 );
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 );
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 );
170 aMainLayout->addStretch( 1 );
171 aMainLayout->addLayout( aButtonLayout );
172 // . Control buttons <end>
180 PyEditor_SettingsDlg::~PyEditor_SettingsDlg()
185 SLOT: Changes the widget visibility depending on the set theState flag.
186 \param theState flag of visibility
188 void PyEditor_SettingsDlg::onVerticalEdgeChecked()
190 myNumberColumnsLbl->setEnabled( myVerticalEdge->isChecked() );
191 myNumberColumns->setEnabled( myVerticalEdge->isChecked() );
195 SLOT: Fills font sizesc combo-box with available values depending on the
198 void PyEditor_SettingsDlg::onFontChanged()
200 bool blocked = myFontSize->blockSignals( true );
202 QString oldSize = myFontSize->currentText();
204 QList<int> szList = QFontDatabase().pointSizes( myFontFamily->currentFont().family() );
206 foreach ( int size, szList )
207 sizes.append( QString::number( size ) );
210 myFontSize->addItems( sizes );
211 setFontSize( oldSize );
213 myFontSize->blockSignals( blocked );
217 \brief Sets settings from preferences dialog.
219 void PyEditor_SettingsDlg::settingsFromGui()
221 const PyEditor_Settings& oldSettings = myEditor->settings();
222 PyEditor_Settings settings;
224 QFont font = oldSettings.font();
225 font.setFamily( myFontFamily->currentFont().family() );
227 int size = myFontSize->currentText().toInt( &ok );
229 font.setPointSize( size );
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()
242 PyEditor_Settings* globals = PyEditor_Settings::settings();
243 if ( globals && myDefaultCheck && myDefaultCheck->isChecked() )
244 globals->copyFrom( settings );
248 \brief Sets settings into preferences dialog.
250 void PyEditor_SettingsDlg::settingsToGui()
252 const PyEditor_Settings& settings = myEditor->settings();
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() ) );
265 onVerticalEdgeChecked();
270 \brief Set font size value to the combo box.
271 \param size new size value
273 void PyEditor_SettingsDlg::setFontSize( const QString& size )
275 int idx = myFontSize->findText( size );
277 myFontSize->setCurrentIndex( idx );
279 myFontSize->setEditText( size );
283 Slot, called when user clicks "OK" button
285 void PyEditor_SettingsDlg::onOk()
292 Slot, called when user clicks "Defaults" button
294 void PyEditor_SettingsDlg::onDefault()
296 PyEditor_Settings* settings = PyEditor_Settings::settings();
299 settings->load(); // to reload from the resources
301 QFont font = settings->font();
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() ) );
314 onVerticalEdgeChecked();
320 Slot, called when user clicks "Help" button.
323 void PyEditor_SettingsDlg::onHelp()