1bb02e0acSNicolas Bonnefon /*
2bb02e0acSNicolas Bonnefon * Copyright (C) 2009, 2010, 2011, 2013 Nicolas Bonnefon and other contributors
3bb02e0acSNicolas Bonnefon *
4bb02e0acSNicolas Bonnefon * This file is part of glogg.
5bb02e0acSNicolas Bonnefon *
6bb02e0acSNicolas Bonnefon * glogg is free software: you can redistribute it and/or modify
7bb02e0acSNicolas Bonnefon * it under the terms of the GNU General Public License as published by
8bb02e0acSNicolas Bonnefon * the Free Software Foundation, either version 3 of the License, or
9bb02e0acSNicolas Bonnefon * (at your option) any later version.
10bb02e0acSNicolas Bonnefon *
11bb02e0acSNicolas Bonnefon * glogg is distributed in the hope that it will be useful,
12bb02e0acSNicolas Bonnefon * but WITHOUT ANY WARRANTY; without even the implied warranty of
13bb02e0acSNicolas Bonnefon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14bb02e0acSNicolas Bonnefon * GNU General Public License for more details.
15bb02e0acSNicolas Bonnefon *
16bb02e0acSNicolas Bonnefon * You should have received a copy of the GNU General Public License
17bb02e0acSNicolas Bonnefon * along with glogg. If not, see <http://www.gnu.org/licenses/>.
18bb02e0acSNicolas Bonnefon */
19bb02e0acSNicolas Bonnefon
20bb02e0acSNicolas Bonnefon #include <QtGui>
21bb02e0acSNicolas Bonnefon
22bb02e0acSNicolas Bonnefon #include "optionsdialog.h"
23bb02e0acSNicolas Bonnefon
24bb02e0acSNicolas Bonnefon #include "log.h"
25bb02e0acSNicolas Bonnefon #include "persistentinfo.h"
26bb02e0acSNicolas Bonnefon #include "configuration.h"
27bb02e0acSNicolas Bonnefon
2880bca0a3SNicolas Bonnefon static const uint32_t POLL_INTERVAL_MIN = 10;
2980bca0a3SNicolas Bonnefon static const uint32_t POLL_INTERVAL_MAX = 3600000;
3080bca0a3SNicolas Bonnefon
31bb02e0acSNicolas Bonnefon // Constructor
OptionsDialog(QWidget * parent)32bb02e0acSNicolas Bonnefon OptionsDialog::OptionsDialog( QWidget* parent ) : QDialog(parent)
33bb02e0acSNicolas Bonnefon {
34bb02e0acSNicolas Bonnefon setupUi( this );
35bb02e0acSNicolas Bonnefon
3680bca0a3SNicolas Bonnefon setupTabs();
37bb02e0acSNicolas Bonnefon setupFontList();
38bb02e0acSNicolas Bonnefon setupRegexp();
39bb02e0acSNicolas Bonnefon
4080bca0a3SNicolas Bonnefon // Validators
4180bca0a3SNicolas Bonnefon QValidator* polling_interval_validator_ = new QIntValidator(
4280bca0a3SNicolas Bonnefon POLL_INTERVAL_MIN, POLL_INTERVAL_MAX, this );
4380bca0a3SNicolas Bonnefon pollIntervalLineEdit->setValidator( polling_interval_validator_ );
4480bca0a3SNicolas Bonnefon
45bb02e0acSNicolas Bonnefon connect(buttonBox, SIGNAL( clicked( QAbstractButton* ) ),
46bb02e0acSNicolas Bonnefon this, SLOT( onButtonBoxClicked( QAbstractButton* ) ) );
47bb02e0acSNicolas Bonnefon connect(fontFamilyBox, SIGNAL( currentIndexChanged(const QString& ) ),
48bb02e0acSNicolas Bonnefon this, SLOT( updateFontSize( const QString& ) ));
49bb02e0acSNicolas Bonnefon connect(incrementalCheckBox, SIGNAL( toggled( bool ) ),
50bb02e0acSNicolas Bonnefon this, SLOT( onIncrementalChanged() ) );
5180bca0a3SNicolas Bonnefon connect(pollingCheckBox, SIGNAL( toggled( bool ) ),
5280bca0a3SNicolas Bonnefon this, SLOT( onPollingChanged() ) );
53bb02e0acSNicolas Bonnefon
54bb02e0acSNicolas Bonnefon updateDialogFromConfig();
55bb02e0acSNicolas Bonnefon
56bb02e0acSNicolas Bonnefon setupIncremental();
5780bca0a3SNicolas Bonnefon setupPolling();
58bb02e0acSNicolas Bonnefon }
59bb02e0acSNicolas Bonnefon
60bb02e0acSNicolas Bonnefon //
61bb02e0acSNicolas Bonnefon // Private functions
62bb02e0acSNicolas Bonnefon //
63bb02e0acSNicolas Bonnefon
6480bca0a3SNicolas Bonnefon // Setups the tabs depending on the configuration
setupTabs()6580bca0a3SNicolas Bonnefon void OptionsDialog::setupTabs()
6680bca0a3SNicolas Bonnefon {
6780bca0a3SNicolas Bonnefon #ifndef GLOGG_SUPPORTS_POLLING
6880bca0a3SNicolas Bonnefon tabWidget->removeTab( 1 );
6980bca0a3SNicolas Bonnefon #endif
7080bca0a3SNicolas Bonnefon }
7180bca0a3SNicolas Bonnefon
72bb02e0acSNicolas Bonnefon // Populates the 'family' ComboBox
setupFontList()73bb02e0acSNicolas Bonnefon void OptionsDialog::setupFontList()
74bb02e0acSNicolas Bonnefon {
75bb02e0acSNicolas Bonnefon QFontDatabase database;
76bb02e0acSNicolas Bonnefon
77bb02e0acSNicolas Bonnefon // We only show the fixed fonts
78bb02e0acSNicolas Bonnefon foreach ( const QString &str, database.families() ) {
79bb02e0acSNicolas Bonnefon if ( database.isFixedPitch( str ) )
80bb02e0acSNicolas Bonnefon fontFamilyBox->addItem( str );
81bb02e0acSNicolas Bonnefon }
82bb02e0acSNicolas Bonnefon }
83bb02e0acSNicolas Bonnefon
84bb02e0acSNicolas Bonnefon // Populate the regexp ComboBoxes
setupRegexp()85bb02e0acSNicolas Bonnefon void OptionsDialog::setupRegexp()
86bb02e0acSNicolas Bonnefon {
87bb02e0acSNicolas Bonnefon QStringList regexpTypes;
88bb02e0acSNicolas Bonnefon
89*7cbb2ca4SNicolas Bonnefon regexpTypes << tr("Extended Regexp") << tr("Fixed Strings");
90bb02e0acSNicolas Bonnefon
91bb02e0acSNicolas Bonnefon mainSearchBox->addItems( regexpTypes );
92bb02e0acSNicolas Bonnefon quickFindSearchBox->addItems( regexpTypes );
93bb02e0acSNicolas Bonnefon }
94bb02e0acSNicolas Bonnefon
95bb02e0acSNicolas Bonnefon // Enable/disable the QuickFind options depending on the state
96bb02e0acSNicolas Bonnefon // of the "incremental" checkbox.
setupIncremental()97bb02e0acSNicolas Bonnefon void OptionsDialog::setupIncremental()
98bb02e0acSNicolas Bonnefon {
99bb02e0acSNicolas Bonnefon if ( incrementalCheckBox->isChecked() ) {
100bb02e0acSNicolas Bonnefon quickFindSearchBox->setCurrentIndex(
101bb02e0acSNicolas Bonnefon getRegexpIndex( FixedString ) );
102bb02e0acSNicolas Bonnefon quickFindSearchBox->setEnabled( false );
103bb02e0acSNicolas Bonnefon }
104bb02e0acSNicolas Bonnefon else {
105bb02e0acSNicolas Bonnefon quickFindSearchBox->setEnabled( true );
106bb02e0acSNicolas Bonnefon }
107bb02e0acSNicolas Bonnefon }
108bb02e0acSNicolas Bonnefon
setupPolling()10980bca0a3SNicolas Bonnefon void OptionsDialog::setupPolling()
11080bca0a3SNicolas Bonnefon {
11180bca0a3SNicolas Bonnefon pollIntervalLineEdit->setEnabled( pollingCheckBox->isChecked() );
11280bca0a3SNicolas Bonnefon }
11380bca0a3SNicolas Bonnefon
114bb02e0acSNicolas Bonnefon // Convert a regexp type to its index in the list
getRegexpIndex(SearchRegexpType syntax) const115bb02e0acSNicolas Bonnefon int OptionsDialog::getRegexpIndex( SearchRegexpType syntax ) const
116bb02e0acSNicolas Bonnefon {
117*7cbb2ca4SNicolas Bonnefon int index;
118*7cbb2ca4SNicolas Bonnefon
119*7cbb2ca4SNicolas Bonnefon switch ( syntax ) {
120*7cbb2ca4SNicolas Bonnefon case FixedString:
121*7cbb2ca4SNicolas Bonnefon index = 1;
122*7cbb2ca4SNicolas Bonnefon break;
123*7cbb2ca4SNicolas Bonnefon default:
124*7cbb2ca4SNicolas Bonnefon index = 0;
125*7cbb2ca4SNicolas Bonnefon break;
126*7cbb2ca4SNicolas Bonnefon }
127*7cbb2ca4SNicolas Bonnefon
128*7cbb2ca4SNicolas Bonnefon return index;
129bb02e0acSNicolas Bonnefon }
130bb02e0acSNicolas Bonnefon
131bb02e0acSNicolas Bonnefon // Convert the index of a regexp type to its type
getRegexpTypeFromIndex(int index) const132bb02e0acSNicolas Bonnefon SearchRegexpType OptionsDialog::getRegexpTypeFromIndex( int index ) const
133bb02e0acSNicolas Bonnefon {
134*7cbb2ca4SNicolas Bonnefon SearchRegexpType type;
135*7cbb2ca4SNicolas Bonnefon
136*7cbb2ca4SNicolas Bonnefon switch ( index ) {
137*7cbb2ca4SNicolas Bonnefon case 1:
138*7cbb2ca4SNicolas Bonnefon type = FixedString;
139*7cbb2ca4SNicolas Bonnefon break;
140*7cbb2ca4SNicolas Bonnefon default:
141*7cbb2ca4SNicolas Bonnefon type = ExtendedRegexp;
142*7cbb2ca4SNicolas Bonnefon break;
143*7cbb2ca4SNicolas Bonnefon }
144*7cbb2ca4SNicolas Bonnefon
145*7cbb2ca4SNicolas Bonnefon return type;
146bb02e0acSNicolas Bonnefon }
147bb02e0acSNicolas Bonnefon
148bb02e0acSNicolas Bonnefon // Updates the dialog box using values in global Config()
updateDialogFromConfig()149bb02e0acSNicolas Bonnefon void OptionsDialog::updateDialogFromConfig()
150bb02e0acSNicolas Bonnefon {
15111582726SNicolas Bonnefon std::shared_ptr<Configuration> config =
15211582726SNicolas Bonnefon Persistent<Configuration>( "settings" );
153bb02e0acSNicolas Bonnefon
154bb02e0acSNicolas Bonnefon // Main font
15511582726SNicolas Bonnefon QFontInfo fontInfo = QFontInfo( config->mainFont() );
156bb02e0acSNicolas Bonnefon
157bb02e0acSNicolas Bonnefon int familyIndex = fontFamilyBox->findText( fontInfo.family() );
158bb02e0acSNicolas Bonnefon if ( familyIndex != -1 )
159bb02e0acSNicolas Bonnefon fontFamilyBox->setCurrentIndex( familyIndex );
160bb02e0acSNicolas Bonnefon
161bb02e0acSNicolas Bonnefon int sizeIndex = fontSizeBox->findText( QString::number(fontInfo.pointSize()) );
162bb02e0acSNicolas Bonnefon if ( sizeIndex != -1 )
163bb02e0acSNicolas Bonnefon fontSizeBox->setCurrentIndex( sizeIndex );
164bb02e0acSNicolas Bonnefon
165bb02e0acSNicolas Bonnefon // Regexp types
166bb02e0acSNicolas Bonnefon mainSearchBox->setCurrentIndex(
16711582726SNicolas Bonnefon getRegexpIndex( config->mainRegexpType() ) );
168bb02e0acSNicolas Bonnefon quickFindSearchBox->setCurrentIndex(
16911582726SNicolas Bonnefon getRegexpIndex( config->quickfindRegexpType() ) );
170bb02e0acSNicolas Bonnefon
17111582726SNicolas Bonnefon incrementalCheckBox->setChecked( config->isQuickfindIncremental() );
17280bca0a3SNicolas Bonnefon
17380bca0a3SNicolas Bonnefon // Polling
17480bca0a3SNicolas Bonnefon pollingCheckBox->setChecked( config->pollingEnabled() );
17580bca0a3SNicolas Bonnefon pollIntervalLineEdit->setText( QString::number( config->pollIntervalMs() ) );
1763b104697SAnton Filimonov
1773b104697SAnton Filimonov // Last session
1783b104697SAnton Filimonov loadLastSessionCheckBox->setChecked( config->loadLastSession() );
179bb02e0acSNicolas Bonnefon }
180bb02e0acSNicolas Bonnefon
181bb02e0acSNicolas Bonnefon //
182bb02e0acSNicolas Bonnefon // Slots
183bb02e0acSNicolas Bonnefon //
184bb02e0acSNicolas Bonnefon
updateFontSize(const QString & fontFamily)185bb02e0acSNicolas Bonnefon void OptionsDialog::updateFontSize(const QString& fontFamily)
186bb02e0acSNicolas Bonnefon {
187bb02e0acSNicolas Bonnefon QFontDatabase database;
188bb02e0acSNicolas Bonnefon QString oldFontSize = fontSizeBox->currentText();
189bb02e0acSNicolas Bonnefon QList<int> sizes = database.pointSizes( fontFamily, "" );
190bb02e0acSNicolas Bonnefon
191bb02e0acSNicolas Bonnefon fontSizeBox->clear();
192bb02e0acSNicolas Bonnefon foreach (int size, sizes) {
193bb02e0acSNicolas Bonnefon fontSizeBox->addItem( QString::number(size) );
194bb02e0acSNicolas Bonnefon }
195bb02e0acSNicolas Bonnefon // Now restore the size we had before
196bb02e0acSNicolas Bonnefon int i = fontSizeBox->findText(oldFontSize);
197bb02e0acSNicolas Bonnefon if ( i != -1 )
198bb02e0acSNicolas Bonnefon fontSizeBox->setCurrentIndex(i);
199bb02e0acSNicolas Bonnefon }
200bb02e0acSNicolas Bonnefon
updateConfigFromDialog()201bb02e0acSNicolas Bonnefon void OptionsDialog::updateConfigFromDialog()
202bb02e0acSNicolas Bonnefon {
20311582726SNicolas Bonnefon std::shared_ptr<Configuration> config =
20411582726SNicolas Bonnefon Persistent<Configuration>( "settings" );
205bb02e0acSNicolas Bonnefon
206bb02e0acSNicolas Bonnefon QFont font = QFont(
207bb02e0acSNicolas Bonnefon fontFamilyBox->currentText(),
208bb02e0acSNicolas Bonnefon (fontSizeBox->currentText()).toInt() );
20911582726SNicolas Bonnefon config->setMainFont(font);
210bb02e0acSNicolas Bonnefon
21111582726SNicolas Bonnefon config->setMainRegexpType(
212bb02e0acSNicolas Bonnefon getRegexpTypeFromIndex( mainSearchBox->currentIndex() ) );
21311582726SNicolas Bonnefon config->setQuickfindRegexpType(
214bb02e0acSNicolas Bonnefon getRegexpTypeFromIndex( quickFindSearchBox->currentIndex() ) );
21511582726SNicolas Bonnefon config->setQuickfindIncremental( incrementalCheckBox->isChecked() );
216bb02e0acSNicolas Bonnefon
21780bca0a3SNicolas Bonnefon config->setPollingEnabled( pollingCheckBox->isChecked() );
21880bca0a3SNicolas Bonnefon uint32_t poll_interval = pollIntervalLineEdit->text().toUInt();
21980bca0a3SNicolas Bonnefon if ( poll_interval < POLL_INTERVAL_MIN )
22080bca0a3SNicolas Bonnefon poll_interval = POLL_INTERVAL_MIN;
22180bca0a3SNicolas Bonnefon else if (poll_interval > POLL_INTERVAL_MAX )
22280bca0a3SNicolas Bonnefon poll_interval = POLL_INTERVAL_MAX;
22380bca0a3SNicolas Bonnefon
22480bca0a3SNicolas Bonnefon config->setPollIntervalMs( poll_interval );
22580bca0a3SNicolas Bonnefon
2263b104697SAnton Filimonov config->setLoadLastSession( loadLastSessionCheckBox->isChecked() );
227bb02e0acSNicolas Bonnefon emit optionsChanged();
228bb02e0acSNicolas Bonnefon }
229bb02e0acSNicolas Bonnefon
onButtonBoxClicked(QAbstractButton * button)230bb02e0acSNicolas Bonnefon void OptionsDialog::onButtonBoxClicked( QAbstractButton* button )
231bb02e0acSNicolas Bonnefon {
232bb02e0acSNicolas Bonnefon QDialogButtonBox::ButtonRole role = buttonBox->buttonRole( button );
233bb02e0acSNicolas Bonnefon if ( ( role == QDialogButtonBox::AcceptRole )
234bb02e0acSNicolas Bonnefon || ( role == QDialogButtonBox::ApplyRole ) ) {
235bb02e0acSNicolas Bonnefon updateConfigFromDialog();
236bb02e0acSNicolas Bonnefon }
237bb02e0acSNicolas Bonnefon
238bb02e0acSNicolas Bonnefon if ( role == QDialogButtonBox::AcceptRole )
239bb02e0acSNicolas Bonnefon accept();
240bb02e0acSNicolas Bonnefon else if ( role == QDialogButtonBox::RejectRole )
241bb02e0acSNicolas Bonnefon reject();
242bb02e0acSNicolas Bonnefon }
243bb02e0acSNicolas Bonnefon
onIncrementalChanged()244bb02e0acSNicolas Bonnefon void OptionsDialog::onIncrementalChanged()
245bb02e0acSNicolas Bonnefon {
246bb02e0acSNicolas Bonnefon setupIncremental();
247bb02e0acSNicolas Bonnefon }
24880bca0a3SNicolas Bonnefon
onPollingChanged()24980bca0a3SNicolas Bonnefon void OptionsDialog::onPollingChanged()
25080bca0a3SNicolas Bonnefon {
25180bca0a3SNicolas Bonnefon setupPolling();
25280bca0a3SNicolas Bonnefon }
253