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