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 std::shared_ptr<Configuration> config = 106 Persistent<Configuration>( "settings" ); 107 108 // Main font 109 QFontInfo fontInfo = QFontInfo( config->mainFont() ); 110 111 int familyIndex = fontFamilyBox->findText( fontInfo.family() ); 112 if ( familyIndex != -1 ) 113 fontFamilyBox->setCurrentIndex( familyIndex ); 114 115 int sizeIndex = fontSizeBox->findText( QString::number(fontInfo.pointSize()) ); 116 if ( sizeIndex != -1 ) 117 fontSizeBox->setCurrentIndex( sizeIndex ); 118 119 // Regexp types 120 mainSearchBox->setCurrentIndex( 121 getRegexpIndex( config->mainRegexpType() ) ); 122 quickFindSearchBox->setCurrentIndex( 123 getRegexpIndex( config->quickfindRegexpType() ) ); 124 125 incrementalCheckBox->setChecked( config->isQuickfindIncremental() ); 126 } 127 128 // 129 // Slots 130 // 131 132 void OptionsDialog::updateFontSize(const QString& fontFamily) 133 { 134 QFontDatabase database; 135 QString oldFontSize = fontSizeBox->currentText(); 136 QList<int> sizes = database.pointSizes( fontFamily, "" ); 137 138 fontSizeBox->clear(); 139 foreach (int size, sizes) { 140 fontSizeBox->addItem( QString::number(size) ); 141 } 142 // Now restore the size we had before 143 int i = fontSizeBox->findText(oldFontSize); 144 if ( i != -1 ) 145 fontSizeBox->setCurrentIndex(i); 146 } 147 148 void OptionsDialog::updateConfigFromDialog() 149 { 150 std::shared_ptr<Configuration> config = 151 Persistent<Configuration>( "settings" ); 152 153 QFont font = QFont( 154 fontFamilyBox->currentText(), 155 (fontSizeBox->currentText()).toInt() ); 156 config->setMainFont(font); 157 158 config->setMainRegexpType( 159 getRegexpTypeFromIndex( mainSearchBox->currentIndex() ) ); 160 config->setQuickfindRegexpType( 161 getRegexpTypeFromIndex( quickFindSearchBox->currentIndex() ) ); 162 config->setQuickfindIncremental( incrementalCheckBox->isChecked() ); 163 164 emit optionsChanged(); 165 } 166 167 void OptionsDialog::onButtonBoxClicked( QAbstractButton* button ) 168 { 169 QDialogButtonBox::ButtonRole role = buttonBox->buttonRole( button ); 170 if ( ( role == QDialogButtonBox::AcceptRole ) 171 || ( role == QDialogButtonBox::ApplyRole ) ) { 172 updateConfigFromDialog(); 173 } 174 175 if ( role == QDialogButtonBox::AcceptRole ) 176 accept(); 177 else if ( role == QDialogButtonBox::RejectRole ) 178 reject(); 179 } 180 181 void OptionsDialog::onIncrementalChanged() 182 { 183 setupIncremental(); 184 } 185