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