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 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 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 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 85bb02e0acSNicolas Bonnefon void OptionsDialog::setupRegexp() 86bb02e0acSNicolas Bonnefon { 87bb02e0acSNicolas Bonnefon QStringList regexpTypes; 88bb02e0acSNicolas Bonnefon 89bb02e0acSNicolas Bonnefon regexpTypes << tr("Extended Regexp") 90bb02e0acSNicolas Bonnefon << tr("Wildcards") << tr("Fixed Strings"); 91bb02e0acSNicolas Bonnefon 92bb02e0acSNicolas Bonnefon mainSearchBox->addItems( regexpTypes ); 93bb02e0acSNicolas Bonnefon quickFindSearchBox->addItems( regexpTypes ); 94bb02e0acSNicolas Bonnefon } 95bb02e0acSNicolas Bonnefon 96bb02e0acSNicolas Bonnefon // Enable/disable the QuickFind options depending on the state 97bb02e0acSNicolas Bonnefon // of the "incremental" checkbox. 98bb02e0acSNicolas Bonnefon void OptionsDialog::setupIncremental() 99bb02e0acSNicolas Bonnefon { 100bb02e0acSNicolas Bonnefon if ( incrementalCheckBox->isChecked() ) { 101bb02e0acSNicolas Bonnefon quickFindSearchBox->setCurrentIndex( 102bb02e0acSNicolas Bonnefon getRegexpIndex( FixedString ) ); 103bb02e0acSNicolas Bonnefon quickFindSearchBox->setEnabled( false ); 104bb02e0acSNicolas Bonnefon } 105bb02e0acSNicolas Bonnefon else { 106bb02e0acSNicolas Bonnefon quickFindSearchBox->setEnabled( true ); 107bb02e0acSNicolas Bonnefon } 108bb02e0acSNicolas Bonnefon } 109bb02e0acSNicolas Bonnefon 11080bca0a3SNicolas Bonnefon void OptionsDialog::setupPolling() 11180bca0a3SNicolas Bonnefon { 11280bca0a3SNicolas Bonnefon pollIntervalLineEdit->setEnabled( pollingCheckBox->isChecked() ); 11380bca0a3SNicolas Bonnefon } 11480bca0a3SNicolas Bonnefon 115bb02e0acSNicolas Bonnefon // Convert a regexp type to its index in the list 116bb02e0acSNicolas Bonnefon int OptionsDialog::getRegexpIndex( SearchRegexpType syntax ) const 117bb02e0acSNicolas Bonnefon { 118bb02e0acSNicolas Bonnefon return static_cast<int>( syntax ); 119bb02e0acSNicolas Bonnefon } 120bb02e0acSNicolas Bonnefon 121bb02e0acSNicolas Bonnefon // Convert the index of a regexp type to its type 122bb02e0acSNicolas Bonnefon SearchRegexpType OptionsDialog::getRegexpTypeFromIndex( int index ) const 123bb02e0acSNicolas Bonnefon { 124bb02e0acSNicolas Bonnefon return static_cast<SearchRegexpType>( index );; 125bb02e0acSNicolas Bonnefon } 126bb02e0acSNicolas Bonnefon 127bb02e0acSNicolas Bonnefon // Updates the dialog box using values in global Config() 128bb02e0acSNicolas Bonnefon void OptionsDialog::updateDialogFromConfig() 129bb02e0acSNicolas Bonnefon { 13011582726SNicolas Bonnefon std::shared_ptr<Configuration> config = 13111582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 132bb02e0acSNicolas Bonnefon 133bb02e0acSNicolas Bonnefon // Main font 13411582726SNicolas Bonnefon QFontInfo fontInfo = QFontInfo( config->mainFont() ); 135bb02e0acSNicolas Bonnefon 136bb02e0acSNicolas Bonnefon int familyIndex = fontFamilyBox->findText( fontInfo.family() ); 137bb02e0acSNicolas Bonnefon if ( familyIndex != -1 ) 138bb02e0acSNicolas Bonnefon fontFamilyBox->setCurrentIndex( familyIndex ); 139bb02e0acSNicolas Bonnefon 140bb02e0acSNicolas Bonnefon int sizeIndex = fontSizeBox->findText( QString::number(fontInfo.pointSize()) ); 141bb02e0acSNicolas Bonnefon if ( sizeIndex != -1 ) 142bb02e0acSNicolas Bonnefon fontSizeBox->setCurrentIndex( sizeIndex ); 143bb02e0acSNicolas Bonnefon 144bb02e0acSNicolas Bonnefon // Regexp types 145bb02e0acSNicolas Bonnefon mainSearchBox->setCurrentIndex( 14611582726SNicolas Bonnefon getRegexpIndex( config->mainRegexpType() ) ); 147bb02e0acSNicolas Bonnefon quickFindSearchBox->setCurrentIndex( 14811582726SNicolas Bonnefon getRegexpIndex( config->quickfindRegexpType() ) ); 149bb02e0acSNicolas Bonnefon 15011582726SNicolas Bonnefon incrementalCheckBox->setChecked( config->isQuickfindIncremental() ); 15180bca0a3SNicolas Bonnefon 15280bca0a3SNicolas Bonnefon // Polling 15380bca0a3SNicolas Bonnefon pollingCheckBox->setChecked( config->pollingEnabled() ); 15480bca0a3SNicolas Bonnefon pollIntervalLineEdit->setText( QString::number( config->pollIntervalMs() ) ); 155*3b104697SAnton Filimonov 156*3b104697SAnton Filimonov // Last session 157*3b104697SAnton Filimonov loadLastSessionCheckBox->setChecked( config->loadLastSession() ); 158bb02e0acSNicolas Bonnefon } 159bb02e0acSNicolas Bonnefon 160bb02e0acSNicolas Bonnefon // 161bb02e0acSNicolas Bonnefon // Slots 162bb02e0acSNicolas Bonnefon // 163bb02e0acSNicolas Bonnefon 164bb02e0acSNicolas Bonnefon void OptionsDialog::updateFontSize(const QString& fontFamily) 165bb02e0acSNicolas Bonnefon { 166bb02e0acSNicolas Bonnefon QFontDatabase database; 167bb02e0acSNicolas Bonnefon QString oldFontSize = fontSizeBox->currentText(); 168bb02e0acSNicolas Bonnefon QList<int> sizes = database.pointSizes( fontFamily, "" ); 169bb02e0acSNicolas Bonnefon 170bb02e0acSNicolas Bonnefon fontSizeBox->clear(); 171bb02e0acSNicolas Bonnefon foreach (int size, sizes) { 172bb02e0acSNicolas Bonnefon fontSizeBox->addItem( QString::number(size) ); 173bb02e0acSNicolas Bonnefon } 174bb02e0acSNicolas Bonnefon // Now restore the size we had before 175bb02e0acSNicolas Bonnefon int i = fontSizeBox->findText(oldFontSize); 176bb02e0acSNicolas Bonnefon if ( i != -1 ) 177bb02e0acSNicolas Bonnefon fontSizeBox->setCurrentIndex(i); 178bb02e0acSNicolas Bonnefon } 179bb02e0acSNicolas Bonnefon 180bb02e0acSNicolas Bonnefon void OptionsDialog::updateConfigFromDialog() 181bb02e0acSNicolas Bonnefon { 18211582726SNicolas Bonnefon std::shared_ptr<Configuration> config = 18311582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 184bb02e0acSNicolas Bonnefon 185bb02e0acSNicolas Bonnefon QFont font = QFont( 186bb02e0acSNicolas Bonnefon fontFamilyBox->currentText(), 187bb02e0acSNicolas Bonnefon (fontSizeBox->currentText()).toInt() ); 18811582726SNicolas Bonnefon config->setMainFont(font); 189bb02e0acSNicolas Bonnefon 19011582726SNicolas Bonnefon config->setMainRegexpType( 191bb02e0acSNicolas Bonnefon getRegexpTypeFromIndex( mainSearchBox->currentIndex() ) ); 19211582726SNicolas Bonnefon config->setQuickfindRegexpType( 193bb02e0acSNicolas Bonnefon getRegexpTypeFromIndex( quickFindSearchBox->currentIndex() ) ); 19411582726SNicolas Bonnefon config->setQuickfindIncremental( incrementalCheckBox->isChecked() ); 195bb02e0acSNicolas Bonnefon 19680bca0a3SNicolas Bonnefon config->setPollingEnabled( pollingCheckBox->isChecked() ); 19780bca0a3SNicolas Bonnefon uint32_t poll_interval = pollIntervalLineEdit->text().toUInt(); 19880bca0a3SNicolas Bonnefon if ( poll_interval < POLL_INTERVAL_MIN ) 19980bca0a3SNicolas Bonnefon poll_interval = POLL_INTERVAL_MIN; 20080bca0a3SNicolas Bonnefon else if (poll_interval > POLL_INTERVAL_MAX ) 20180bca0a3SNicolas Bonnefon poll_interval = POLL_INTERVAL_MAX; 20280bca0a3SNicolas Bonnefon 20380bca0a3SNicolas Bonnefon config->setPollIntervalMs( poll_interval ); 20480bca0a3SNicolas Bonnefon 205*3b104697SAnton Filimonov config->setLoadLastSession( loadLastSessionCheckBox->isChecked() ); 206bb02e0acSNicolas Bonnefon emit optionsChanged(); 207bb02e0acSNicolas Bonnefon } 208bb02e0acSNicolas Bonnefon 209bb02e0acSNicolas Bonnefon void OptionsDialog::onButtonBoxClicked( QAbstractButton* button ) 210bb02e0acSNicolas Bonnefon { 211bb02e0acSNicolas Bonnefon QDialogButtonBox::ButtonRole role = buttonBox->buttonRole( button ); 212bb02e0acSNicolas Bonnefon if ( ( role == QDialogButtonBox::AcceptRole ) 213bb02e0acSNicolas Bonnefon || ( role == QDialogButtonBox::ApplyRole ) ) { 214bb02e0acSNicolas Bonnefon updateConfigFromDialog(); 215bb02e0acSNicolas Bonnefon } 216bb02e0acSNicolas Bonnefon 217bb02e0acSNicolas Bonnefon if ( role == QDialogButtonBox::AcceptRole ) 218bb02e0acSNicolas Bonnefon accept(); 219bb02e0acSNicolas Bonnefon else if ( role == QDialogButtonBox::RejectRole ) 220bb02e0acSNicolas Bonnefon reject(); 221bb02e0acSNicolas Bonnefon } 222bb02e0acSNicolas Bonnefon 223bb02e0acSNicolas Bonnefon void OptionsDialog::onIncrementalChanged() 224bb02e0acSNicolas Bonnefon { 225bb02e0acSNicolas Bonnefon setupIncremental(); 226bb02e0acSNicolas Bonnefon } 22780bca0a3SNicolas Bonnefon 22880bca0a3SNicolas Bonnefon void OptionsDialog::onPollingChanged() 22980bca0a3SNicolas Bonnefon { 23080bca0a3SNicolas Bonnefon setupPolling(); 23180bca0a3SNicolas Bonnefon } 232