xref: /glogg/src/optionsdialog.cpp (revision 3b104697d849ffe3af885a1684937240f00fa9ad)
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")
90         << tr("Wildcards") << tr("Fixed Strings");
91 
92     mainSearchBox->addItems( regexpTypes );
93     quickFindSearchBox->addItems( regexpTypes );
94 }
95 
96 // Enable/disable the QuickFind options depending on the state
97 // of the "incremental" checkbox.
98 void OptionsDialog::setupIncremental()
99 {
100     if ( incrementalCheckBox->isChecked() ) {
101         quickFindSearchBox->setCurrentIndex(
102                 getRegexpIndex( FixedString ) );
103         quickFindSearchBox->setEnabled( false );
104     }
105     else {
106         quickFindSearchBox->setEnabled( true );
107     }
108 }
109 
110 void OptionsDialog::setupPolling()
111 {
112     pollIntervalLineEdit->setEnabled( pollingCheckBox->isChecked() );
113 }
114 
115 // Convert a regexp type to its index in the list
116 int OptionsDialog::getRegexpIndex( SearchRegexpType syntax ) const
117 {
118     return static_cast<int>( syntax );
119 }
120 
121 // Convert the index of a regexp type to its type
122 SearchRegexpType OptionsDialog::getRegexpTypeFromIndex( int index ) const
123 {
124     return static_cast<SearchRegexpType>( index );;
125 }
126 
127 // Updates the dialog box using values in global Config()
128 void OptionsDialog::updateDialogFromConfig()
129 {
130     std::shared_ptr<Configuration> config =
131         Persistent<Configuration>( "settings" );
132 
133     // Main font
134     QFontInfo fontInfo = QFontInfo( config->mainFont() );
135 
136     int familyIndex = fontFamilyBox->findText( fontInfo.family() );
137     if ( familyIndex != -1 )
138         fontFamilyBox->setCurrentIndex( familyIndex );
139 
140     int sizeIndex = fontSizeBox->findText( QString::number(fontInfo.pointSize()) );
141     if ( sizeIndex != -1 )
142         fontSizeBox->setCurrentIndex( sizeIndex );
143 
144     // Regexp types
145     mainSearchBox->setCurrentIndex(
146             getRegexpIndex( config->mainRegexpType() ) );
147     quickFindSearchBox->setCurrentIndex(
148             getRegexpIndex( config->quickfindRegexpType() ) );
149 
150     incrementalCheckBox->setChecked( config->isQuickfindIncremental() );
151 
152     // Polling
153     pollingCheckBox->setChecked( config->pollingEnabled() );
154     pollIntervalLineEdit->setText( QString::number( config->pollIntervalMs() ) );
155 
156     // Last session
157     loadLastSessionCheckBox->setChecked( config->loadLastSession() );
158 }
159 
160 //
161 // Slots
162 //
163 
164 void OptionsDialog::updateFontSize(const QString& fontFamily)
165 {
166     QFontDatabase database;
167     QString oldFontSize = fontSizeBox->currentText();
168     QList<int> sizes = database.pointSizes( fontFamily, "" );
169 
170     fontSizeBox->clear();
171     foreach (int size, sizes) {
172         fontSizeBox->addItem( QString::number(size) );
173     }
174     // Now restore the size we had before
175     int i = fontSizeBox->findText(oldFontSize);
176     if ( i != -1 )
177         fontSizeBox->setCurrentIndex(i);
178 }
179 
180 void OptionsDialog::updateConfigFromDialog()
181 {
182     std::shared_ptr<Configuration> config =
183         Persistent<Configuration>( "settings" );
184 
185     QFont font = QFont(
186             fontFamilyBox->currentText(),
187             (fontSizeBox->currentText()).toInt() );
188     config->setMainFont(font);
189 
190     config->setMainRegexpType(
191             getRegexpTypeFromIndex( mainSearchBox->currentIndex() ) );
192     config->setQuickfindRegexpType(
193             getRegexpTypeFromIndex( quickFindSearchBox->currentIndex() ) );
194     config->setQuickfindIncremental( incrementalCheckBox->isChecked() );
195 
196     config->setPollingEnabled( pollingCheckBox->isChecked() );
197     uint32_t poll_interval = pollIntervalLineEdit->text().toUInt();
198     if ( poll_interval < POLL_INTERVAL_MIN )
199         poll_interval = POLL_INTERVAL_MIN;
200     else if (poll_interval > POLL_INTERVAL_MAX )
201         poll_interval = POLL_INTERVAL_MAX;
202 
203     config->setPollIntervalMs( poll_interval );
204 
205     config->setLoadLastSession( loadLastSessionCheckBox->isChecked() );
206     emit optionsChanged();
207 }
208 
209 void OptionsDialog::onButtonBoxClicked( QAbstractButton* button )
210 {
211     QDialogButtonBox::ButtonRole role = buttonBox->buttonRole( button );
212     if (   ( role == QDialogButtonBox::AcceptRole )
213         || ( role == QDialogButtonBox::ApplyRole ) ) {
214         updateConfigFromDialog();
215     }
216 
217     if ( role == QDialogButtonBox::AcceptRole )
218         accept();
219     else if ( role == QDialogButtonBox::RejectRole )
220         reject();
221 }
222 
223 void OptionsDialog::onIncrementalChanged()
224 {
225     setupIncremental();
226 }
227 
228 void OptionsDialog::onPollingChanged()
229 {
230     setupPolling();
231 }
232