xref: /glogg/src/optionsdialog.cpp (revision 8b941e123ddc1953f679547d44fb51d36b42e416)
1 /*
2  * Copyright (C) 2009, 2010, 2011, 2013 Nicolas Bonnefon and other contributors
3  *
4  * This file is part of glogg.
5  *
6  * glogg is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * glogg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <QtGui>
21 
22 #include "optionsdialog.h"
23 
24 #include "log.h"
25 #include "persistentinfo.h"
26 #include "configuration.h"
27 
28 static const uint32_t POLL_INTERVAL_MIN = 10;
29 static const uint32_t POLL_INTERVAL_MAX = 3600000;
30 
31 // Constructor
32 OptionsDialog::OptionsDialog( QWidget* parent ) : QDialog(parent)
33 {
34     setupUi( this );
35 
36     setupTabs();
37     setupFontList();
38     setupRegexp();
39 
40     // Validators
41     QValidator* polling_interval_validator_ = new QIntValidator(
42            POLL_INTERVAL_MIN, POLL_INTERVAL_MAX, this );
43     pollIntervalLineEdit->setValidator( polling_interval_validator_ );
44 
45     connect(buttonBox, SIGNAL( clicked( QAbstractButton* ) ),
46             this, SLOT( onButtonBoxClicked( QAbstractButton* ) ) );
47     connect(fontFamilyBox, SIGNAL( currentIndexChanged(const QString& ) ),
48             this, SLOT( updateFontSize( const QString& ) ));
49     connect(incrementalCheckBox, SIGNAL( toggled( bool ) ),
50             this, SLOT( onIncrementalChanged() ) );
51     connect(pollingCheckBox, SIGNAL( toggled( bool ) ),
52             this, SLOT( onPollingChanged() ) );
53 
54     updateDialogFromConfig();
55 
56     setupIncremental();
57     setupPolling();
58 }
59 
60 //
61 // Private functions
62 //
63 
64 // Setups the tabs depending on the configuration
65 void OptionsDialog::setupTabs()
66 {
67 #ifndef GLOGG_SUPPORTS_POLLING
68     tabWidget->removeTab( 1 );
69 #endif
70 }
71 
72 // Populates the 'family' ComboBox
73 void OptionsDialog::setupFontList()
74 {
75     QFontDatabase database;
76 
77     // We only show the fixed fonts
78     foreach ( const QString &str, database.families() ) {
79          if ( database.isFixedPitch( str ) )
80              fontFamilyBox->addItem( str );
81      }
82 }
83 
84 // Populate the regexp ComboBoxes
85 void OptionsDialog::setupRegexp()
86 {
87     QStringList regexpTypes;
88 
89     regexpTypes << tr("Extended Regexp") << tr("Fixed Strings");
90 
91     mainSearchBox->addItems( regexpTypes );
92     quickFindSearchBox->addItems( regexpTypes );
93 }
94 
95 // Enable/disable the QuickFind options depending on the state
96 // of the "incremental" checkbox.
97 void OptionsDialog::setupIncremental()
98 {
99     if ( incrementalCheckBox->isChecked() ) {
100         quickFindSearchBox->setCurrentIndex(
101                 getRegexpIndex( FixedString ) );
102         quickFindSearchBox->setEnabled( false );
103     }
104     else {
105         quickFindSearchBox->setEnabled( true );
106     }
107 }
108 
109 void OptionsDialog::setupPolling()
110 {
111     pollIntervalLineEdit->setEnabled( pollingCheckBox->isChecked() );
112 }
113 
114 // Convert a regexp type to its index in the list
115 int OptionsDialog::getRegexpIndex( SearchRegexpType syntax ) const
116 {
117     int index;
118 
119     switch ( syntax ) {
120         case FixedString:
121             index = 1;
122             break;
123         default:
124             index = 0;
125             break;
126     }
127 
128     return index;
129 }
130 
131 // Convert the index of a regexp type to its type
132 SearchRegexpType OptionsDialog::getRegexpTypeFromIndex( int index ) const
133 {
134     SearchRegexpType type;
135 
136     switch ( index ) {
137         case 1:
138             type = FixedString;
139             break;
140         default:
141             type = ExtendedRegexp;
142             break;
143     }
144 
145     return type;
146 }
147 
148 // Updates the dialog box using values in global Config()
149 void OptionsDialog::updateDialogFromConfig()
150 {
151     std::shared_ptr<Configuration> config =
152         Persistent<Configuration>( "settings" );
153 
154     // Main font
155     QFontInfo fontInfo = QFontInfo( config->mainFont() );
156 
157     int familyIndex = fontFamilyBox->findText( fontInfo.family() );
158     if ( familyIndex != -1 )
159         fontFamilyBox->setCurrentIndex( familyIndex );
160 
161     int sizeIndex = fontSizeBox->findText( QString::number(fontInfo.pointSize()) );
162     if ( sizeIndex != -1 )
163         fontSizeBox->setCurrentIndex( sizeIndex );
164 
165     // Regexp types
166     mainSearchBox->setCurrentIndex(
167             getRegexpIndex( config->mainRegexpType() ) );
168     quickFindSearchBox->setCurrentIndex(
169             getRegexpIndex( config->quickfindRegexpType() ) );
170 
171     incrementalCheckBox->setChecked( config->isQuickfindIncremental() );
172 
173     // Polling
174     pollingCheckBox->setChecked( config->pollingEnabled() );
175     pollIntervalLineEdit->setText( QString::number( config->pollIntervalMs() ) );
176 
177     // Last session
178     loadLastSessionCheckBox->setChecked( config->loadLastSession() );
179 }
180 
181 //
182 // Slots
183 //
184 
185 void OptionsDialog::updateFontSize(const QString& fontFamily)
186 {
187     QFontDatabase database;
188     QString oldFontSize = fontSizeBox->currentText();
189     QList<int> sizes = database.pointSizes( fontFamily, "" );
190 
191     fontSizeBox->clear();
192     foreach (int size, sizes) {
193         fontSizeBox->addItem( QString::number(size) );
194     }
195     // Now restore the size we had before
196     int i = fontSizeBox->findText(oldFontSize);
197     if ( i != -1 )
198         fontSizeBox->setCurrentIndex(i);
199 }
200 
201 void OptionsDialog::updateConfigFromDialog()
202 {
203     std::shared_ptr<Configuration> config =
204         Persistent<Configuration>( "settings" );
205 
206     QFont font = QFont(
207             fontFamilyBox->currentText(),
208             (fontSizeBox->currentText()).toInt() );
209     config->setMainFont(font);
210 
211     config->setMainRegexpType(
212             getRegexpTypeFromIndex( mainSearchBox->currentIndex() ) );
213     config->setQuickfindRegexpType(
214             getRegexpTypeFromIndex( quickFindSearchBox->currentIndex() ) );
215     config->setQuickfindIncremental( incrementalCheckBox->isChecked() );
216 
217     config->setPollingEnabled( pollingCheckBox->isChecked() );
218     uint32_t poll_interval = pollIntervalLineEdit->text().toUInt();
219     if ( poll_interval < POLL_INTERVAL_MIN )
220         poll_interval = POLL_INTERVAL_MIN;
221     else if (poll_interval > POLL_INTERVAL_MAX )
222         poll_interval = POLL_INTERVAL_MAX;
223 
224     config->setPollIntervalMs( poll_interval );
225 
226     config->setLoadLastSession( loadLastSessionCheckBox->isChecked() );
227     emit optionsChanged();
228 }
229 
230 void OptionsDialog::onButtonBoxClicked( QAbstractButton* button )
231 {
232     QDialogButtonBox::ButtonRole role = buttonBox->buttonRole( button );
233     if (   ( role == QDialogButtonBox::AcceptRole )
234         || ( role == QDialogButtonBox::ApplyRole ) ) {
235         updateConfigFromDialog();
236     }
237 
238     if ( role == QDialogButtonBox::AcceptRole )
239         accept();
240     else if ( role == QDialogButtonBox::RejectRole )
241         reject();
242 }
243 
244 void OptionsDialog::onIncrementalChanged()
245 {
246     setupIncremental();
247 }
248 
249 void OptionsDialog::onPollingChanged()
250 {
251     setupPolling();
252 }
253