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