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 157 // 158 // Slots 159 // 160 161 void OptionsDialog::updateFontSize(const QString& fontFamily) 162 { 163 QFontDatabase database; 164 QString oldFontSize = fontSizeBox->currentText(); 165 QList<int> sizes = database.pointSizes( fontFamily, "" ); 166 167 fontSizeBox->clear(); 168 foreach (int size, sizes) { 169 fontSizeBox->addItem( QString::number(size) ); 170 } 171 // Now restore the size we had before 172 int i = fontSizeBox->findText(oldFontSize); 173 if ( i != -1 ) 174 fontSizeBox->setCurrentIndex(i); 175 } 176 177 void OptionsDialog::updateConfigFromDialog() 178 { 179 std::shared_ptr<Configuration> config = 180 Persistent<Configuration>( "settings" ); 181 182 QFont font = QFont( 183 fontFamilyBox->currentText(), 184 (fontSizeBox->currentText()).toInt() ); 185 config->setMainFont(font); 186 187 config->setMainRegexpType( 188 getRegexpTypeFromIndex( mainSearchBox->currentIndex() ) ); 189 config->setQuickfindRegexpType( 190 getRegexpTypeFromIndex( quickFindSearchBox->currentIndex() ) ); 191 config->setQuickfindIncremental( incrementalCheckBox->isChecked() ); 192 193 config->setPollingEnabled( pollingCheckBox->isChecked() ); 194 uint32_t poll_interval = pollIntervalLineEdit->text().toUInt(); 195 if ( poll_interval < POLL_INTERVAL_MIN ) 196 poll_interval = POLL_INTERVAL_MIN; 197 else if (poll_interval > POLL_INTERVAL_MAX ) 198 poll_interval = POLL_INTERVAL_MAX; 199 200 config->setPollIntervalMs( poll_interval ); 201 202 emit optionsChanged(); 203 } 204 205 void OptionsDialog::onButtonBoxClicked( QAbstractButton* button ) 206 { 207 QDialogButtonBox::ButtonRole role = buttonBox->buttonRole( button ); 208 if ( ( role == QDialogButtonBox::AcceptRole ) 209 || ( role == QDialogButtonBox::ApplyRole ) ) { 210 updateConfigFromDialog(); 211 } 212 213 if ( role == QDialogButtonBox::AcceptRole ) 214 accept(); 215 else if ( role == QDialogButtonBox::RejectRole ) 216 reject(); 217 } 218 219 void OptionsDialog::onIncrementalChanged() 220 { 221 setupIncremental(); 222 } 223 224 void OptionsDialog::onPollingChanged() 225 { 226 setupPolling(); 227 } 228