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 // . 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>
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>
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>
132 QHBoxLayout* aTabVertEdgeLayout = new QHBoxLayout;
133 aTabVertEdgeLayout->addWidget( aTabSetBox );
134 aTabVertEdgeLayout->addWidget( aVertEdgeSetBox );
135 aMainLayout->addLayout( aTabVertEdgeLayout );
137 // . "Set as default" check box <start>
138 if ( PyEditor_Settings::settings() )
140 myDefaultCheck = new QCheckBox( tr( "WDG_SET_AS_DEFAULT_CHECK" ), this );
141 aMainLayout->addWidget( myDefaultCheck );
147 // . "Set as default" check box <end>
149 // . Control buttons <start>
150 QHBoxLayout* aButtonLayout = new QHBoxLayout;
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 );
158 if ( PyEditor_Settings::settings() )
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 );
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 );
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 );
180 aMainLayout->addStretch( 1 );
181 aMainLayout->addLayout( aButtonLayout );
182 // . Control buttons <end>
190 PyEditor_SettingsDlg::~PyEditor_SettingsDlg()
195 SLOT: Changes the widget visibility depending on the set theState flag.
196 \param theState flag of visibility
198 void PyEditor_SettingsDlg::onVerticalEdgeChecked()
200 myNumberColumnsLbl->setEnabled( myVerticalEdge->isChecked() );
201 myNumberColumns->setEnabled( myVerticalEdge->isChecked() );
205 SLOT: Fills font sizesc combo-box with available values depending on the
208 void PyEditor_SettingsDlg::onFontChanged()
210 bool blocked = myFontSize->blockSignals( true );
212 QString oldSize = myFontSize->currentText();
214 QList<int> szList = QFontDatabase().pointSizes( myFontFamily->currentFont().family() );
216 foreach ( int size, szList )
217 sizes.append( QString::number( size ) );
220 myFontSize->addItems( sizes );
221 setFontSize( oldSize );
223 myFontSize->blockSignals( blocked );
227 \brief Sets settings from preferences dialog.
229 void PyEditor_SettingsDlg::settingsFromGui()
231 const PyEditor_Settings& oldSettings = myEditor->settings();
232 PyEditor_Settings settings;
234 QFont font = oldSettings.font();
235 font.setFamily( myFontFamily->currentFont().family() );
237 int size = myFontSize->currentText().toInt( &ok );
239 font.setPointSize( size );
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()
253 PyEditor_Settings* globals = PyEditor_Settings::settings();
254 if ( globals && myDefaultCheck && myDefaultCheck->isChecked() )
255 globals->copyFrom( settings );
259 \brief Sets settings into preferences dialog.
261 void PyEditor_SettingsDlg::settingsToGui()
263 const PyEditor_Settings& settings = myEditor->settings();
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() );
277 onVerticalEdgeChecked();
282 \brief Set font size value to the combo box.
283 \param size new size value
285 void PyEditor_SettingsDlg::setFontSize( const QString& size )
287 int idx = myFontSize->findText( size );
289 myFontSize->setCurrentIndex( idx );
291 myFontSize->setEditText( size );
295 Slot, called when user clicks "OK" button
297 void PyEditor_SettingsDlg::onOk()
304 Slot, called when user clicks "Defaults" button
306 void PyEditor_SettingsDlg::onDefault()
308 PyEditor_Settings* settings = PyEditor_Settings::settings();
311 settings->load(); // to reload from the resources
313 QFont font = settings->font();
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() ) );
326 onVerticalEdgeChecked();
332 Slot, called when user clicks "Help" button.
335 void PyEditor_SettingsDlg::onHelp()