1bb02e0acSNicolas Bonnefon /* 2bb02e0acSNicolas Bonnefon * Copyright (C) 2009, 2010 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 "log.h" 21bb02e0acSNicolas Bonnefon 22bb02e0acSNicolas Bonnefon #include "configuration.h" 23bb02e0acSNicolas Bonnefon #include "persistentinfo.h" 24bb02e0acSNicolas Bonnefon #include "filterset.h" 25bb02e0acSNicolas Bonnefon 26bb02e0acSNicolas Bonnefon #include "filtersdialog.h" 27bb02e0acSNicolas Bonnefon 28bb02e0acSNicolas Bonnefon static const QString DEFAULT_PATTERN = "New Filter"; 29b9c9a815SAnton Filimonov static const bool DEFAULT_IGNORE_CASE = false; 30bb02e0acSNicolas Bonnefon static const QString DEFAULT_FORE_COLOUR = "black"; 31bb02e0acSNicolas Bonnefon static const QString DEFAULT_BACK_COLOUR = "white"; 32bb02e0acSNicolas Bonnefon 33bb02e0acSNicolas Bonnefon // Construct the box, including a copy of the global FilterSet 34bb02e0acSNicolas Bonnefon // to handle ok/cancel/apply 35bb02e0acSNicolas Bonnefon FiltersDialog::FiltersDialog( QWidget* parent ) : QDialog( parent ) 36bb02e0acSNicolas Bonnefon { 37bb02e0acSNicolas Bonnefon setupUi( this ); 38bb02e0acSNicolas Bonnefon 39bb02e0acSNicolas Bonnefon // Reload the filter list from disk (in case it has been changed 40bb02e0acSNicolas Bonnefon // by another glogg instance) and copy it to here. 41bb02e0acSNicolas Bonnefon GetPersistentInfo().retrieve( "filterSet" ); 4211582726SNicolas Bonnefon filterSet = PersistentCopy<FilterSet>( "filterSet" ); 43bb02e0acSNicolas Bonnefon 44bb02e0acSNicolas Bonnefon populateColors(); 45bb02e0acSNicolas Bonnefon populateFilterList(); 46bb02e0acSNicolas Bonnefon 47bb02e0acSNicolas Bonnefon // Start with all buttons disabled except 'add' 48bb02e0acSNicolas Bonnefon removeFilterButton->setEnabled(false); 49bb02e0acSNicolas Bonnefon upFilterButton->setEnabled(false); 50bb02e0acSNicolas Bonnefon downFilterButton->setEnabled(false); 51bb02e0acSNicolas Bonnefon 52bb02e0acSNicolas Bonnefon // Default to black on white 53bb02e0acSNicolas Bonnefon int index = foreColorBox->findText( DEFAULT_FORE_COLOUR ); 54bb02e0acSNicolas Bonnefon foreColorBox->setCurrentIndex( index ); 55bb02e0acSNicolas Bonnefon index = backColorBox->findText( DEFAULT_BACK_COLOUR ); 56bb02e0acSNicolas Bonnefon backColorBox->setCurrentIndex( index ); 57bb02e0acSNicolas Bonnefon 58bb02e0acSNicolas Bonnefon // No filter selected by default 59bb02e0acSNicolas Bonnefon selectedRow_ = -1; 60bb02e0acSNicolas Bonnefon 61bb02e0acSNicolas Bonnefon connect( filterListWidget, SIGNAL( itemSelectionChanged() ), 62bb02e0acSNicolas Bonnefon this, SLOT( updatePropertyFields() ) ); 63bb02e0acSNicolas Bonnefon connect( patternEdit, SIGNAL( textEdited( const QString& ) ), 64bb02e0acSNicolas Bonnefon this, SLOT( updateFilterProperties() ) ); 65*c6557e6eSAnton Filimonov connect( ignoreCaseCheckBox, SIGNAL( clicked( bool ) ), 66b9c9a815SAnton Filimonov this, SLOT( updateFilterProperties() ) ); 67bb02e0acSNicolas Bonnefon connect( foreColorBox, SIGNAL( activated( int ) ), 68bb02e0acSNicolas Bonnefon this, SLOT( updateFilterProperties() ) ); 69bb02e0acSNicolas Bonnefon connect( backColorBox, SIGNAL( activated( int ) ), 70bb02e0acSNicolas Bonnefon this, SLOT( updateFilterProperties() ) ); 71b9c9a815SAnton Filimonov 72b9c9a815SAnton Filimonov if ( !filterSet->filterList.empty() ) { 73b9c9a815SAnton Filimonov filterListWidget->setCurrentItem( filterListWidget->item( 0 ) ); 74b9c9a815SAnton Filimonov } 75bb02e0acSNicolas Bonnefon } 76bb02e0acSNicolas Bonnefon 77bb02e0acSNicolas Bonnefon // 78bb02e0acSNicolas Bonnefon // Slots 79bb02e0acSNicolas Bonnefon // 80bb02e0acSNicolas Bonnefon 81bb02e0acSNicolas Bonnefon void FiltersDialog::on_addFilterButton_clicked() 82bb02e0acSNicolas Bonnefon { 83bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "on_addFilterButton_clicked()"; 84bb02e0acSNicolas Bonnefon 85b9c9a815SAnton Filimonov Filter newFilter = Filter( DEFAULT_PATTERN, DEFAULT_IGNORE_CASE, 86bb02e0acSNicolas Bonnefon DEFAULT_FORE_COLOUR, DEFAULT_BACK_COLOUR ); 8711582726SNicolas Bonnefon filterSet->filterList << newFilter; 88bb02e0acSNicolas Bonnefon 89bb02e0acSNicolas Bonnefon // Add and select the newly created filter 90bb02e0acSNicolas Bonnefon filterListWidget->addItem( DEFAULT_PATTERN ); 91bb02e0acSNicolas Bonnefon filterListWidget->setCurrentRow( filterListWidget->count() - 1 ); 92bb02e0acSNicolas Bonnefon } 93bb02e0acSNicolas Bonnefon 94bb02e0acSNicolas Bonnefon void FiltersDialog::on_removeFilterButton_clicked() 95bb02e0acSNicolas Bonnefon { 96bb02e0acSNicolas Bonnefon int index = filterListWidget->currentRow(); 97bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "on_removeFilterButton_clicked() index " << index; 98bb02e0acSNicolas Bonnefon 99bb02e0acSNicolas Bonnefon if ( index >= 0 ) { 10011582726SNicolas Bonnefon filterSet->filterList.removeAt( index ); 101bb02e0acSNicolas Bonnefon filterListWidget->setCurrentRow( -1 ); 102bb02e0acSNicolas Bonnefon delete filterListWidget->takeItem( index ); 103bb02e0acSNicolas Bonnefon 104bb02e0acSNicolas Bonnefon // Select the new item at the same index 105bb02e0acSNicolas Bonnefon filterListWidget->setCurrentRow( index ); 106bb02e0acSNicolas Bonnefon } 107bb02e0acSNicolas Bonnefon } 108bb02e0acSNicolas Bonnefon 109bb02e0acSNicolas Bonnefon void FiltersDialog::on_upFilterButton_clicked() 110bb02e0acSNicolas Bonnefon { 111bb02e0acSNicolas Bonnefon int index = filterListWidget->currentRow(); 112bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "on_upFilterButton_clicked() index " << index; 113bb02e0acSNicolas Bonnefon 114bb02e0acSNicolas Bonnefon if ( index > 0 ) { 11511582726SNicolas Bonnefon filterSet->filterList.move( index, index - 1 ); 116bb02e0acSNicolas Bonnefon 117bb02e0acSNicolas Bonnefon QListWidgetItem* item = filterListWidget->takeItem( index ); 118bb02e0acSNicolas Bonnefon filterListWidget->insertItem( index - 1, item ); 119bb02e0acSNicolas Bonnefon filterListWidget->setCurrentRow( index - 1 ); 120bb02e0acSNicolas Bonnefon } 121bb02e0acSNicolas Bonnefon } 122bb02e0acSNicolas Bonnefon 123bb02e0acSNicolas Bonnefon void FiltersDialog::on_downFilterButton_clicked() 124bb02e0acSNicolas Bonnefon { 125bb02e0acSNicolas Bonnefon int index = filterListWidget->currentRow(); 126bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "on_downFilterButton_clicked() index " << index; 127bb02e0acSNicolas Bonnefon 128bb02e0acSNicolas Bonnefon if ( ( index >= 0 ) && ( index < ( filterListWidget->count() - 1 ) ) ) { 12911582726SNicolas Bonnefon filterSet->filterList.move( index, index + 1 ); 130bb02e0acSNicolas Bonnefon 131bb02e0acSNicolas Bonnefon QListWidgetItem* item = filterListWidget->takeItem( index ); 132bb02e0acSNicolas Bonnefon filterListWidget->insertItem( index + 1, item ); 133bb02e0acSNicolas Bonnefon filterListWidget->setCurrentRow( index + 1 ); 134bb02e0acSNicolas Bonnefon } 135bb02e0acSNicolas Bonnefon } 136bb02e0acSNicolas Bonnefon 137bb02e0acSNicolas Bonnefon void FiltersDialog::on_buttonBox_clicked( QAbstractButton* button ) 138bb02e0acSNicolas Bonnefon { 139bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "on_buttonBox_clicked()"; 140bb02e0acSNicolas Bonnefon 141bb02e0acSNicolas Bonnefon QDialogButtonBox::ButtonRole role = buttonBox->buttonRole( button ); 142bb02e0acSNicolas Bonnefon if ( ( role == QDialogButtonBox::AcceptRole ) 143bb02e0acSNicolas Bonnefon || ( role == QDialogButtonBox::ApplyRole ) ) { 144bb02e0acSNicolas Bonnefon // Copy the filter set and persist it to disk 14511582726SNicolas Bonnefon *( Persistent<FilterSet>( "filterSet" ) ) = *filterSet; 146bb02e0acSNicolas Bonnefon GetPersistentInfo().save( "filterSet" ); 147bb02e0acSNicolas Bonnefon emit optionsChanged(); 148bb02e0acSNicolas Bonnefon } 149bb02e0acSNicolas Bonnefon 150bb02e0acSNicolas Bonnefon if ( role == QDialogButtonBox::AcceptRole ) 151bb02e0acSNicolas Bonnefon accept(); 152bb02e0acSNicolas Bonnefon else if ( role == QDialogButtonBox::RejectRole ) 153bb02e0acSNicolas Bonnefon reject(); 154bb02e0acSNicolas Bonnefon } 155bb02e0acSNicolas Bonnefon 156bb02e0acSNicolas Bonnefon void FiltersDialog::updatePropertyFields() 157bb02e0acSNicolas Bonnefon { 158bb02e0acSNicolas Bonnefon if ( filterListWidget->selectedItems().count() >= 1 ) 159bb02e0acSNicolas Bonnefon selectedRow_ = filterListWidget->row( 160bb02e0acSNicolas Bonnefon filterListWidget->selectedItems().at(0) ); 161bb02e0acSNicolas Bonnefon else 162bb02e0acSNicolas Bonnefon selectedRow_ = -1; 163bb02e0acSNicolas Bonnefon 164bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "updatePropertyFields(), row = " << selectedRow_; 165bb02e0acSNicolas Bonnefon 166bb02e0acSNicolas Bonnefon if ( selectedRow_ >= 0 ) { 16711582726SNicolas Bonnefon const Filter& currentFilter = filterSet->filterList.at( selectedRow_ ); 168bb02e0acSNicolas Bonnefon 169bb02e0acSNicolas Bonnefon patternEdit->setText( currentFilter.pattern() ); 170bb02e0acSNicolas Bonnefon patternEdit->setEnabled( true ); 171bb02e0acSNicolas Bonnefon 172b9c9a815SAnton Filimonov ignoreCaseCheckBox->setChecked( currentFilter.ignoreCase() ); 173b9c9a815SAnton Filimonov ignoreCaseCheckBox->setEnabled( true ); 174b9c9a815SAnton Filimonov 175bb02e0acSNicolas Bonnefon int index = foreColorBox->findText( currentFilter.foreColorName() ); 176bb02e0acSNicolas Bonnefon if ( index != -1 ) { 177bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "fore index = " << index; 178bb02e0acSNicolas Bonnefon foreColorBox->setCurrentIndex( index ); 179bb02e0acSNicolas Bonnefon foreColorBox->setEnabled( true ); 180bb02e0acSNicolas Bonnefon } 181bb02e0acSNicolas Bonnefon index = backColorBox->findText( currentFilter.backColorName() ); 182bb02e0acSNicolas Bonnefon if ( index != -1 ) { 183bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "back index = " << index; 184bb02e0acSNicolas Bonnefon backColorBox->setCurrentIndex( index ); 185bb02e0acSNicolas Bonnefon backColorBox->setEnabled( true ); 186bb02e0acSNicolas Bonnefon } 187bb02e0acSNicolas Bonnefon 188bb02e0acSNicolas Bonnefon // Enable the buttons if needed 189bb02e0acSNicolas Bonnefon removeFilterButton->setEnabled( true ); 190bb02e0acSNicolas Bonnefon upFilterButton->setEnabled( ( selectedRow_ > 0 ) ? true : false ); 191bb02e0acSNicolas Bonnefon downFilterButton->setEnabled( 192bb02e0acSNicolas Bonnefon ( selectedRow_ < ( filterListWidget->count() - 1 ) ) ? true : false ); 193bb02e0acSNicolas Bonnefon } 194bb02e0acSNicolas Bonnefon else { 195bb02e0acSNicolas Bonnefon // Nothing is selected, greys the buttons 196bb02e0acSNicolas Bonnefon patternEdit->setEnabled( false ); 197bb02e0acSNicolas Bonnefon foreColorBox->setEnabled( false ); 198bb02e0acSNicolas Bonnefon backColorBox->setEnabled( false ); 199b9c9a815SAnton Filimonov ignoreCaseCheckBox->setEnabled( false ); 200bb02e0acSNicolas Bonnefon } 201bb02e0acSNicolas Bonnefon } 202bb02e0acSNicolas Bonnefon 203bb02e0acSNicolas Bonnefon void FiltersDialog::updateFilterProperties() 204bb02e0acSNicolas Bonnefon { 205bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "updateFilterProperties()"; 206bb02e0acSNicolas Bonnefon 207bb02e0acSNicolas Bonnefon // If a row is selected 208bb02e0acSNicolas Bonnefon if ( selectedRow_ >= 0 ) { 20911582726SNicolas Bonnefon Filter& currentFilter = filterSet->filterList[selectedRow_]; 210bb02e0acSNicolas Bonnefon 211bb02e0acSNicolas Bonnefon // Update the internal data 212bb02e0acSNicolas Bonnefon currentFilter.setPattern( patternEdit->text() ); 213b9c9a815SAnton Filimonov currentFilter.setIgnoreCase( ignoreCaseCheckBox->isChecked() ); 214bb02e0acSNicolas Bonnefon currentFilter.setForeColor( foreColorBox->currentText() ); 215bb02e0acSNicolas Bonnefon currentFilter.setBackColor( backColorBox->currentText() ); 216bb02e0acSNicolas Bonnefon 217bb02e0acSNicolas Bonnefon // Update the entry in the filterList widget 218bb02e0acSNicolas Bonnefon filterListWidget->currentItem()->setText( patternEdit->text() ); 219bb02e0acSNicolas Bonnefon filterListWidget->currentItem()->setForeground( 220bb02e0acSNicolas Bonnefon QBrush( QColor( currentFilter.foreColorName() ) ) ); 221bb02e0acSNicolas Bonnefon filterListWidget->currentItem()->setBackground( 222bb02e0acSNicolas Bonnefon QBrush( QColor( currentFilter.backColorName() ) ) ); 223bb02e0acSNicolas Bonnefon } 224bb02e0acSNicolas Bonnefon } 225bb02e0acSNicolas Bonnefon 226bb02e0acSNicolas Bonnefon // 227bb02e0acSNicolas Bonnefon // Private functions 228bb02e0acSNicolas Bonnefon // 229bb02e0acSNicolas Bonnefon 230bb02e0acSNicolas Bonnefon // Fills the color selection combo boxes 231bb02e0acSNicolas Bonnefon void FiltersDialog::populateColors() 232bb02e0acSNicolas Bonnefon { 233bb02e0acSNicolas Bonnefon const QStringList colorNames = QStringList() 234bb02e0acSNicolas Bonnefon // Basic 16 HTML colors (minus greys): 235bb02e0acSNicolas Bonnefon << "black" 236bb02e0acSNicolas Bonnefon << "white" 237bb02e0acSNicolas Bonnefon << "maroon" 238bb02e0acSNicolas Bonnefon << "red" 239bb02e0acSNicolas Bonnefon << "purple" 240bb02e0acSNicolas Bonnefon << "fuchsia" 241bb02e0acSNicolas Bonnefon << "green" 242bb02e0acSNicolas Bonnefon << "lime" 243bb02e0acSNicolas Bonnefon << "olive" 244bb02e0acSNicolas Bonnefon << "yellow" 245bb02e0acSNicolas Bonnefon << "navy" 246bb02e0acSNicolas Bonnefon << "blue" 247bb02e0acSNicolas Bonnefon << "teal" 248bb02e0acSNicolas Bonnefon << "aqua" 249bb02e0acSNicolas Bonnefon // Greys 250bb02e0acSNicolas Bonnefon << "gainsboro" 251bb02e0acSNicolas Bonnefon << "lightgrey" 252bb02e0acSNicolas Bonnefon << "silver" 253bb02e0acSNicolas Bonnefon << "darkgrey" 254bb02e0acSNicolas Bonnefon << "grey" 255bb02e0acSNicolas Bonnefon << "dimgrey" 256bb02e0acSNicolas Bonnefon // Reds 257bb02e0acSNicolas Bonnefon << "tomato" 258bb02e0acSNicolas Bonnefon << "orangered" 259bb02e0acSNicolas Bonnefon << "orange" 260bb02e0acSNicolas Bonnefon << "crimson" 261bb02e0acSNicolas Bonnefon << "darkred" 262bb02e0acSNicolas Bonnefon // Greens 263bb02e0acSNicolas Bonnefon << "greenyellow" 264bb02e0acSNicolas Bonnefon << "lightgreen" 265bb02e0acSNicolas Bonnefon << "darkgreen" 266bb02e0acSNicolas Bonnefon << "lightseagreen" 267bb02e0acSNicolas Bonnefon // Blues 268bb02e0acSNicolas Bonnefon << "lightcyan" 269bb02e0acSNicolas Bonnefon << "darkturquoise" 270bb02e0acSNicolas Bonnefon << "steelblue" 271bb02e0acSNicolas Bonnefon << "lightblue" 272bb02e0acSNicolas Bonnefon << "royalblue" 273bb02e0acSNicolas Bonnefon << "darkblue" 274bb02e0acSNicolas Bonnefon << "midnightblue" 275bb02e0acSNicolas Bonnefon // Browns 276bb02e0acSNicolas Bonnefon << "bisque" 277bb02e0acSNicolas Bonnefon << "tan" 278bb02e0acSNicolas Bonnefon << "sandybrown" 279bb02e0acSNicolas Bonnefon << "chocolate"; 280bb02e0acSNicolas Bonnefon 281bb02e0acSNicolas Bonnefon for ( QStringList::const_iterator i = colorNames.constBegin(); 282bb02e0acSNicolas Bonnefon i != colorNames.constEnd(); ++i ) { 283bb02e0acSNicolas Bonnefon QPixmap solidPixmap( 20, 10 ); 284bb02e0acSNicolas Bonnefon solidPixmap.fill( QColor( *i ) ); 285bb02e0acSNicolas Bonnefon QIcon* solidIcon = new QIcon( solidPixmap ); 286bb02e0acSNicolas Bonnefon 287bb02e0acSNicolas Bonnefon foreColorBox->addItem( *solidIcon, *i ); 288bb02e0acSNicolas Bonnefon backColorBox->addItem( *solidIcon, *i ); 289bb02e0acSNicolas Bonnefon } 290bb02e0acSNicolas Bonnefon } 291bb02e0acSNicolas Bonnefon 292bb02e0acSNicolas Bonnefon void FiltersDialog::populateFilterList() 293bb02e0acSNicolas Bonnefon { 294bb02e0acSNicolas Bonnefon filterListWidget->clear(); 29511582726SNicolas Bonnefon foreach ( Filter filter, filterSet->filterList ) { 296bb02e0acSNicolas Bonnefon QListWidgetItem* new_item = new QListWidgetItem( filter.pattern() ); 297bb02e0acSNicolas Bonnefon // new_item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled ); 298bb02e0acSNicolas Bonnefon new_item->setForeground( QBrush( QColor( filter.foreColorName() ) ) ); 299bb02e0acSNicolas Bonnefon new_item->setBackground( QBrush( QColor( filter.backColorName() ) ) ); 300bb02e0acSNicolas Bonnefon filterListWidget->addItem( new_item ); 301bb02e0acSNicolas Bonnefon } 302bb02e0acSNicolas Bonnefon } 303