xref: /glogg/src/optionsdialog.cpp (revision 821cac888d515a4e41b5d4ba4130c56db4463501)
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 // Constructor
29 OptionsDialog::OptionsDialog( QWidget* parent ) : QDialog(parent)
30 {
31     setupUi( this );
32 
33     setupFontList();
34     setupRegexp();
35 
36     connect(buttonBox, SIGNAL( clicked( QAbstractButton* ) ),
37             this, SLOT( onButtonBoxClicked( QAbstractButton* ) ) );
38     connect(fontFamilyBox, SIGNAL( currentIndexChanged(const QString& ) ),
39             this, SLOT( updateFontSize( const QString& ) ));
40     connect(incrementalCheckBox, SIGNAL( toggled( bool ) ),
41             this, SLOT( onIncrementalChanged() ) );
42 
43     updateDialogFromConfig();
44 
45     setupIncremental();
46 }
47 
48 //
49 // Private functions
50 //
51 
52 // Populates the 'family' ComboBox
53 void OptionsDialog::setupFontList()
54 {
55     QFontDatabase database;
56 
57     // We only show the fixed fonts
58     foreach ( const QString &str, database.families() ) {
59          if ( database.isFixedPitch( str ) )
60              fontFamilyBox->addItem( str );
61      }
62 }
63 
64 // Populate the regexp ComboBoxes
65 void OptionsDialog::setupRegexp()
66 {
67     QStringList regexpTypes;
68 
69     regexpTypes << tr("Extended Regexp")
70         << tr("Wildcards") << tr("Fixed Strings");
71 
72     mainSearchBox->addItems( regexpTypes );
73     quickFindSearchBox->addItems( regexpTypes );
74 }
75 
76 // Enable/disable the QuickFind options depending on the state
77 // of the "incremental" checkbox.
78 void OptionsDialog::setupIncremental()
79 {
80     if ( incrementalCheckBox->isChecked() ) {
81         quickFindSearchBox->setCurrentIndex(
82                 getRegexpIndex( FixedString ) );
83         quickFindSearchBox->setEnabled( false );
84     }
85     else {
86         quickFindSearchBox->setEnabled( true );
87     }
88 }
89 
90 // Convert a regexp type to its index in the list
91 int OptionsDialog::getRegexpIndex( SearchRegexpType syntax ) const
92 {
93     return static_cast<int>( syntax );
94 }
95 
96 // Convert the index of a regexp type to its type
97 SearchRegexpType OptionsDialog::getRegexpTypeFromIndex( int index ) const
98 {
99     return static_cast<SearchRegexpType>( index );;
100 }
101 
102 // Updates the dialog box using values in global Config()
103 void OptionsDialog::updateDialogFromConfig()
104 {
105     Configuration& config = Persistent<Configuration>( "settings" );
106 
107     // Main font
108     QFontInfo fontInfo = QFontInfo( config.mainFont() );
109 
110     int familyIndex = fontFamilyBox->findText( fontInfo.family() );
111     if ( familyIndex != -1 )
112         fontFamilyBox->setCurrentIndex( familyIndex );
113 
114     int sizeIndex = fontSizeBox->findText( QString::number(fontInfo.pointSize()) );
115     if ( sizeIndex != -1 )
116         fontSizeBox->setCurrentIndex( sizeIndex );
117 
118     // Regexp types
119     mainSearchBox->setCurrentIndex(
120             getRegexpIndex( config.mainRegexpType() ) );
121     quickFindSearchBox->setCurrentIndex(
122             getRegexpIndex( config.quickfindRegexpType() ) );
123 
124     incrementalCheckBox->setChecked( config.isQuickfindIncremental() );
125 }
126 
127 //
128 // Slots
129 //
130 
131 void OptionsDialog::updateFontSize(const QString& fontFamily)
132 {
133     QFontDatabase database;
134     QString oldFontSize = fontSizeBox->currentText();
135     QList<int> sizes = database.pointSizes( fontFamily, "" );
136 
137     fontSizeBox->clear();
138     foreach (int size, sizes) {
139         fontSizeBox->addItem( QString::number(size) );
140     }
141     // Now restore the size we had before
142     int i = fontSizeBox->findText(oldFontSize);
143     if ( i != -1 )
144         fontSizeBox->setCurrentIndex(i);
145 }
146 
147 void OptionsDialog::updateConfigFromDialog()
148 {
149     Configuration& config = Persistent<Configuration>( "settings" );
150 
151     QFont font = QFont(
152             fontFamilyBox->currentText(),
153             (fontSizeBox->currentText()).toInt() );
154     config.setMainFont(font);
155 
156     config.setMainRegexpType(
157             getRegexpTypeFromIndex( mainSearchBox->currentIndex() ) );
158     config.setQuickfindRegexpType(
159             getRegexpTypeFromIndex( quickFindSearchBox->currentIndex() ) );
160     config.setQuickfindIncremental( incrementalCheckBox->isChecked() );
161 
162     emit optionsChanged();
163 }
164 
165 void OptionsDialog::onButtonBoxClicked( QAbstractButton* button )
166 {
167     QDialogButtonBox::ButtonRole role = buttonBox->buttonRole( button );
168     if (   ( role == QDialogButtonBox::AcceptRole )
169         || ( role == QDialogButtonBox::ApplyRole ) ) {
170         updateConfigFromDialog();
171     }
172 
173     if ( role == QDialogButtonBox::AcceptRole )
174         accept();
175     else if ( role == QDialogButtonBox::RejectRole )
176         reject();
177 }
178 
179 void OptionsDialog::onIncrementalChanged()
180 {
181     setupIncremental();
182 }
183