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