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 28*80bca0a3SNicolas Bonnefon static const uint32_t POLL_INTERVAL_MIN = 10; 29*80bca0a3SNicolas Bonnefon static const uint32_t POLL_INTERVAL_MAX = 3600000; 30*80bca0a3SNicolas Bonnefon 31bb02e0acSNicolas Bonnefon // Constructor 32bb02e0acSNicolas Bonnefon OptionsDialog::OptionsDialog( QWidget* parent ) : QDialog(parent) 33bb02e0acSNicolas Bonnefon { 34bb02e0acSNicolas Bonnefon setupUi( this ); 35bb02e0acSNicolas Bonnefon 36*80bca0a3SNicolas Bonnefon setupTabs(); 37bb02e0acSNicolas Bonnefon setupFontList(); 38bb02e0acSNicolas Bonnefon setupRegexp(); 39bb02e0acSNicolas Bonnefon 40*80bca0a3SNicolas Bonnefon // Validators 41*80bca0a3SNicolas Bonnefon QValidator* polling_interval_validator_ = new QIntValidator( 42*80bca0a3SNicolas Bonnefon POLL_INTERVAL_MIN, POLL_INTERVAL_MAX, this ); 43*80bca0a3SNicolas Bonnefon pollIntervalLineEdit->setValidator( polling_interval_validator_ ); 44*80bca0a3SNicolas 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() ) ); 51*80bca0a3SNicolas Bonnefon connect(pollingCheckBox, SIGNAL( toggled( bool ) ), 52*80bca0a3SNicolas Bonnefon this, SLOT( onPollingChanged() ) ); 53bb02e0acSNicolas Bonnefon 54bb02e0acSNicolas Bonnefon updateDialogFromConfig(); 55bb02e0acSNicolas Bonnefon 56bb02e0acSNicolas Bonnefon setupIncremental(); 57*80bca0a3SNicolas Bonnefon setupPolling(); 58bb02e0acSNicolas Bonnefon } 59bb02e0acSNicolas Bonnefon 60bb02e0acSNicolas Bonnefon // 61bb02e0acSNicolas Bonnefon // Private functions 62bb02e0acSNicolas Bonnefon // 63bb02e0acSNicolas Bonnefon 64*80bca0a3SNicolas Bonnefon // Setups the tabs depending on the configuration 65*80bca0a3SNicolas Bonnefon void OptionsDialog::setupTabs() 66*80bca0a3SNicolas Bonnefon { 67*80bca0a3SNicolas Bonnefon #ifndef GLOGG_SUPPORTS_POLLING 68*80bca0a3SNicolas Bonnefon tabWidget->removeTab( 1 ); 69*80bca0a3SNicolas Bonnefon #endif 70*80bca0a3SNicolas Bonnefon } 71*80bca0a3SNicolas 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 110*80bca0a3SNicolas Bonnefon void OptionsDialog::setupPolling() 111*80bca0a3SNicolas Bonnefon { 112*80bca0a3SNicolas Bonnefon pollIntervalLineEdit->setEnabled( pollingCheckBox->isChecked() ); 113*80bca0a3SNicolas Bonnefon } 114*80bca0a3SNicolas 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() ); 151*80bca0a3SNicolas Bonnefon 152*80bca0a3SNicolas Bonnefon // Polling 153*80bca0a3SNicolas Bonnefon pollingCheckBox->setChecked( config->pollingEnabled() ); 154*80bca0a3SNicolas Bonnefon pollIntervalLineEdit->setText( QString::number( config->pollIntervalMs() ) ); 155bb02e0acSNicolas Bonnefon } 156bb02e0acSNicolas Bonnefon 157bb02e0acSNicolas Bonnefon // 158bb02e0acSNicolas Bonnefon // Slots 159bb02e0acSNicolas Bonnefon // 160bb02e0acSNicolas Bonnefon 161bb02e0acSNicolas Bonnefon void OptionsDialog::updateFontSize(const QString& fontFamily) 162bb02e0acSNicolas Bonnefon { 163bb02e0acSNicolas Bonnefon QFontDatabase database; 164bb02e0acSNicolas Bonnefon QString oldFontSize = fontSizeBox->currentText(); 165bb02e0acSNicolas Bonnefon QList<int> sizes = database.pointSizes( fontFamily, "" ); 166bb02e0acSNicolas Bonnefon 167bb02e0acSNicolas Bonnefon fontSizeBox->clear(); 168bb02e0acSNicolas Bonnefon foreach (int size, sizes) { 169bb02e0acSNicolas Bonnefon fontSizeBox->addItem( QString::number(size) ); 170bb02e0acSNicolas Bonnefon } 171bb02e0acSNicolas Bonnefon // Now restore the size we had before 172bb02e0acSNicolas Bonnefon int i = fontSizeBox->findText(oldFontSize); 173bb02e0acSNicolas Bonnefon if ( i != -1 ) 174bb02e0acSNicolas Bonnefon fontSizeBox->setCurrentIndex(i); 175bb02e0acSNicolas Bonnefon } 176bb02e0acSNicolas Bonnefon 177bb02e0acSNicolas Bonnefon void OptionsDialog::updateConfigFromDialog() 178bb02e0acSNicolas Bonnefon { 17911582726SNicolas Bonnefon std::shared_ptr<Configuration> config = 18011582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 181bb02e0acSNicolas Bonnefon 182bb02e0acSNicolas Bonnefon QFont font = QFont( 183bb02e0acSNicolas Bonnefon fontFamilyBox->currentText(), 184bb02e0acSNicolas Bonnefon (fontSizeBox->currentText()).toInt() ); 18511582726SNicolas Bonnefon config->setMainFont(font); 186bb02e0acSNicolas Bonnefon 18711582726SNicolas Bonnefon config->setMainRegexpType( 188bb02e0acSNicolas Bonnefon getRegexpTypeFromIndex( mainSearchBox->currentIndex() ) ); 18911582726SNicolas Bonnefon config->setQuickfindRegexpType( 190bb02e0acSNicolas Bonnefon getRegexpTypeFromIndex( quickFindSearchBox->currentIndex() ) ); 19111582726SNicolas Bonnefon config->setQuickfindIncremental( incrementalCheckBox->isChecked() ); 192bb02e0acSNicolas Bonnefon 193*80bca0a3SNicolas Bonnefon config->setPollingEnabled( pollingCheckBox->isChecked() ); 194*80bca0a3SNicolas Bonnefon uint32_t poll_interval = pollIntervalLineEdit->text().toUInt(); 195*80bca0a3SNicolas Bonnefon if ( poll_interval < POLL_INTERVAL_MIN ) 196*80bca0a3SNicolas Bonnefon poll_interval = POLL_INTERVAL_MIN; 197*80bca0a3SNicolas Bonnefon else if (poll_interval > POLL_INTERVAL_MAX ) 198*80bca0a3SNicolas Bonnefon poll_interval = POLL_INTERVAL_MAX; 199*80bca0a3SNicolas Bonnefon 200*80bca0a3SNicolas Bonnefon config->setPollIntervalMs( poll_interval ); 201*80bca0a3SNicolas Bonnefon 202bb02e0acSNicolas Bonnefon emit optionsChanged(); 203bb02e0acSNicolas Bonnefon } 204bb02e0acSNicolas Bonnefon 205bb02e0acSNicolas Bonnefon void OptionsDialog::onButtonBoxClicked( QAbstractButton* button ) 206bb02e0acSNicolas Bonnefon { 207bb02e0acSNicolas Bonnefon QDialogButtonBox::ButtonRole role = buttonBox->buttonRole( button ); 208bb02e0acSNicolas Bonnefon if ( ( role == QDialogButtonBox::AcceptRole ) 209bb02e0acSNicolas Bonnefon || ( role == QDialogButtonBox::ApplyRole ) ) { 210bb02e0acSNicolas Bonnefon updateConfigFromDialog(); 211bb02e0acSNicolas Bonnefon } 212bb02e0acSNicolas Bonnefon 213bb02e0acSNicolas Bonnefon if ( role == QDialogButtonBox::AcceptRole ) 214bb02e0acSNicolas Bonnefon accept(); 215bb02e0acSNicolas Bonnefon else if ( role == QDialogButtonBox::RejectRole ) 216bb02e0acSNicolas Bonnefon reject(); 217bb02e0acSNicolas Bonnefon } 218bb02e0acSNicolas Bonnefon 219bb02e0acSNicolas Bonnefon void OptionsDialog::onIncrementalChanged() 220bb02e0acSNicolas Bonnefon { 221bb02e0acSNicolas Bonnefon setupIncremental(); 222bb02e0acSNicolas Bonnefon } 223*80bca0a3SNicolas Bonnefon 224*80bca0a3SNicolas Bonnefon void OptionsDialog::onPollingChanged() 225*80bca0a3SNicolas Bonnefon { 226*80bca0a3SNicolas Bonnefon setupPolling(); 227*80bca0a3SNicolas Bonnefon } 228