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
FiltersDialog(QWidget * parent)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() ) );
65c6557e6eSAnton 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
on_addFilterButton_clicked()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
on_removeFilterButton_clicked()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
104*afd246c8SAlexander Hirsch int count = filterListWidget->count();
105*afd246c8SAlexander Hirsch if ( index < count ) {
106bb02e0acSNicolas Bonnefon // Select the new item at the same index
107bb02e0acSNicolas Bonnefon filterListWidget->setCurrentRow( index );
108bb02e0acSNicolas Bonnefon }
109*afd246c8SAlexander Hirsch else {
110*afd246c8SAlexander Hirsch // or the previous index if it is at the end
111*afd246c8SAlexander Hirsch filterListWidget->setCurrentRow( count - 1 );
112*afd246c8SAlexander Hirsch }
113*afd246c8SAlexander Hirsch }
114bb02e0acSNicolas Bonnefon }
115bb02e0acSNicolas Bonnefon
on_upFilterButton_clicked()116bb02e0acSNicolas Bonnefon void FiltersDialog::on_upFilterButton_clicked()
117bb02e0acSNicolas Bonnefon {
118bb02e0acSNicolas Bonnefon int index = filterListWidget->currentRow();
119bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "on_upFilterButton_clicked() index " << index;
120bb02e0acSNicolas Bonnefon
121bb02e0acSNicolas Bonnefon if ( index > 0 ) {
12211582726SNicolas 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
on_downFilterButton_clicked()130bb02e0acSNicolas Bonnefon void FiltersDialog::on_downFilterButton_clicked()
131bb02e0acSNicolas Bonnefon {
132bb02e0acSNicolas Bonnefon int index = filterListWidget->currentRow();
133bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "on_downFilterButton_clicked() index " << index;
134bb02e0acSNicolas Bonnefon
135bb02e0acSNicolas Bonnefon if ( ( index >= 0 ) && ( index < ( filterListWidget->count() - 1 ) ) ) {
13611582726SNicolas Bonnefon filterSet->filterList.move( index, index + 1 );
137bb02e0acSNicolas Bonnefon
138bb02e0acSNicolas Bonnefon QListWidgetItem* item = filterListWidget->takeItem( index );
139bb02e0acSNicolas Bonnefon filterListWidget->insertItem( index + 1, item );
140bb02e0acSNicolas Bonnefon filterListWidget->setCurrentRow( index + 1 );
141bb02e0acSNicolas Bonnefon }
142bb02e0acSNicolas Bonnefon }
143bb02e0acSNicolas Bonnefon
on_buttonBox_clicked(QAbstractButton * button)144bb02e0acSNicolas Bonnefon void FiltersDialog::on_buttonBox_clicked( QAbstractButton* button )
145bb02e0acSNicolas Bonnefon {
146bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "on_buttonBox_clicked()";
147bb02e0acSNicolas Bonnefon
148bb02e0acSNicolas Bonnefon QDialogButtonBox::ButtonRole role = buttonBox->buttonRole( button );
149bb02e0acSNicolas Bonnefon if ( ( role == QDialogButtonBox::AcceptRole )
150bb02e0acSNicolas Bonnefon || ( role == QDialogButtonBox::ApplyRole ) ) {
151bb02e0acSNicolas Bonnefon // Copy the filter set and persist it to disk
15211582726SNicolas Bonnefon *( Persistent<FilterSet>( "filterSet" ) ) = *filterSet;
153bb02e0acSNicolas Bonnefon GetPersistentInfo().save( "filterSet" );
154bb02e0acSNicolas Bonnefon emit optionsChanged();
155bb02e0acSNicolas Bonnefon }
156bb02e0acSNicolas Bonnefon
157bb02e0acSNicolas Bonnefon if ( role == QDialogButtonBox::AcceptRole )
158bb02e0acSNicolas Bonnefon accept();
159bb02e0acSNicolas Bonnefon else if ( role == QDialogButtonBox::RejectRole )
160bb02e0acSNicolas Bonnefon reject();
161bb02e0acSNicolas Bonnefon }
162bb02e0acSNicolas Bonnefon
updatePropertyFields()163bb02e0acSNicolas Bonnefon void FiltersDialog::updatePropertyFields()
164bb02e0acSNicolas Bonnefon {
165bb02e0acSNicolas Bonnefon if ( filterListWidget->selectedItems().count() >= 1 )
166bb02e0acSNicolas Bonnefon selectedRow_ = filterListWidget->row(
167bb02e0acSNicolas Bonnefon filterListWidget->selectedItems().at(0) );
168bb02e0acSNicolas Bonnefon else
169bb02e0acSNicolas Bonnefon selectedRow_ = -1;
170bb02e0acSNicolas Bonnefon
171bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "updatePropertyFields(), row = " << selectedRow_;
172bb02e0acSNicolas Bonnefon
173bb02e0acSNicolas Bonnefon if ( selectedRow_ >= 0 ) {
17411582726SNicolas Bonnefon const Filter& currentFilter = filterSet->filterList.at( selectedRow_ );
175bb02e0acSNicolas Bonnefon
176bb02e0acSNicolas Bonnefon patternEdit->setText( currentFilter.pattern() );
177bb02e0acSNicolas Bonnefon patternEdit->setEnabled( true );
178bb02e0acSNicolas Bonnefon
179b9c9a815SAnton Filimonov ignoreCaseCheckBox->setChecked( currentFilter.ignoreCase() );
180b9c9a815SAnton Filimonov ignoreCaseCheckBox->setEnabled( true );
181b9c9a815SAnton Filimonov
182bb02e0acSNicolas Bonnefon int index = foreColorBox->findText( currentFilter.foreColorName() );
183bb02e0acSNicolas Bonnefon if ( index != -1 ) {
184bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "fore index = " << index;
185bb02e0acSNicolas Bonnefon foreColorBox->setCurrentIndex( index );
186bb02e0acSNicolas Bonnefon foreColorBox->setEnabled( true );
187bb02e0acSNicolas Bonnefon }
188bb02e0acSNicolas Bonnefon index = backColorBox->findText( currentFilter.backColorName() );
189bb02e0acSNicolas Bonnefon if ( index != -1 ) {
190bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "back index = " << index;
191bb02e0acSNicolas Bonnefon backColorBox->setCurrentIndex( index );
192bb02e0acSNicolas Bonnefon backColorBox->setEnabled( true );
193bb02e0acSNicolas Bonnefon }
194bb02e0acSNicolas Bonnefon
195bb02e0acSNicolas Bonnefon // Enable the buttons if needed
196bb02e0acSNicolas Bonnefon removeFilterButton->setEnabled( true );
197e15651d2SAlexander Hirsch upFilterButton->setEnabled( selectedRow_ > 0 );
198bb02e0acSNicolas Bonnefon downFilterButton->setEnabled(
199e15651d2SAlexander Hirsch selectedRow_ < ( filterListWidget->count() - 1 ) );
200bb02e0acSNicolas Bonnefon }
201bb02e0acSNicolas Bonnefon else {
2023d913e04SAlexander Hirsch // Nothing is selected, reset and disable the controls
2033d913e04SAlexander Hirsch patternEdit->clear();
204bb02e0acSNicolas Bonnefon patternEdit->setEnabled( false );
2053d913e04SAlexander Hirsch
2063d913e04SAlexander Hirsch int index = foreColorBox->findText( DEFAULT_FORE_COLOUR );
2073d913e04SAlexander Hirsch foreColorBox->setCurrentIndex( index );
208bb02e0acSNicolas Bonnefon foreColorBox->setEnabled( false );
2093d913e04SAlexander Hirsch index = backColorBox->findText( DEFAULT_BACK_COLOUR );
2103d913e04SAlexander Hirsch
2113d913e04SAlexander Hirsch backColorBox->setCurrentIndex( index );
212bb02e0acSNicolas Bonnefon backColorBox->setEnabled( false );
2133d913e04SAlexander Hirsch
2143d913e04SAlexander Hirsch ignoreCaseCheckBox->setChecked( DEFAULT_IGNORE_CASE );
215b9c9a815SAnton Filimonov ignoreCaseCheckBox->setEnabled( false );
216e15651d2SAlexander Hirsch removeFilterButton->setEnabled( false );
217e15651d2SAlexander Hirsch upFilterButton->setEnabled( false );
218e15651d2SAlexander Hirsch downFilterButton->setEnabled( false );
219bb02e0acSNicolas Bonnefon }
220bb02e0acSNicolas Bonnefon }
221bb02e0acSNicolas Bonnefon
updateFilterProperties()222bb02e0acSNicolas Bonnefon void FiltersDialog::updateFilterProperties()
223bb02e0acSNicolas Bonnefon {
224bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "updateFilterProperties()";
225bb02e0acSNicolas Bonnefon
226bb02e0acSNicolas Bonnefon // If a row is selected
227bb02e0acSNicolas Bonnefon if ( selectedRow_ >= 0 ) {
22811582726SNicolas Bonnefon Filter& currentFilter = filterSet->filterList[selectedRow_];
229bb02e0acSNicolas Bonnefon
230bb02e0acSNicolas Bonnefon // Update the internal data
231bb02e0acSNicolas Bonnefon currentFilter.setPattern( patternEdit->text() );
232b9c9a815SAnton Filimonov currentFilter.setIgnoreCase( ignoreCaseCheckBox->isChecked() );
233bb02e0acSNicolas Bonnefon currentFilter.setForeColor( foreColorBox->currentText() );
234bb02e0acSNicolas Bonnefon currentFilter.setBackColor( backColorBox->currentText() );
235bb02e0acSNicolas Bonnefon
236bb02e0acSNicolas Bonnefon // Update the entry in the filterList widget
237bb02e0acSNicolas Bonnefon filterListWidget->currentItem()->setText( patternEdit->text() );
238bb02e0acSNicolas Bonnefon filterListWidget->currentItem()->setForeground(
239bb02e0acSNicolas Bonnefon QBrush( QColor( currentFilter.foreColorName() ) ) );
240bb02e0acSNicolas Bonnefon filterListWidget->currentItem()->setBackground(
241bb02e0acSNicolas Bonnefon QBrush( QColor( currentFilter.backColorName() ) ) );
242bb02e0acSNicolas Bonnefon }
243bb02e0acSNicolas Bonnefon }
244bb02e0acSNicolas Bonnefon
245bb02e0acSNicolas Bonnefon //
246bb02e0acSNicolas Bonnefon // Private functions
247bb02e0acSNicolas Bonnefon //
248bb02e0acSNicolas Bonnefon
249bb02e0acSNicolas Bonnefon // Fills the color selection combo boxes
populateColors()250bb02e0acSNicolas Bonnefon void FiltersDialog::populateColors()
251bb02e0acSNicolas Bonnefon {
252bb02e0acSNicolas Bonnefon const QStringList colorNames = QStringList()
253bb02e0acSNicolas Bonnefon // Basic 16 HTML colors (minus greys):
254bb02e0acSNicolas Bonnefon << "black"
255bb02e0acSNicolas Bonnefon << "white"
256bb02e0acSNicolas Bonnefon << "maroon"
257bb02e0acSNicolas Bonnefon << "red"
258bb02e0acSNicolas Bonnefon << "purple"
259bb02e0acSNicolas Bonnefon << "fuchsia"
260bb02e0acSNicolas Bonnefon << "green"
261bb02e0acSNicolas Bonnefon << "lime"
262bb02e0acSNicolas Bonnefon << "olive"
263bb02e0acSNicolas Bonnefon << "yellow"
264bb02e0acSNicolas Bonnefon << "navy"
265bb02e0acSNicolas Bonnefon << "blue"
266bb02e0acSNicolas Bonnefon << "teal"
267bb02e0acSNicolas Bonnefon << "aqua"
268bb02e0acSNicolas Bonnefon // Greys
269bb02e0acSNicolas Bonnefon << "gainsboro"
270bb02e0acSNicolas Bonnefon << "lightgrey"
271bb02e0acSNicolas Bonnefon << "silver"
272bb02e0acSNicolas Bonnefon << "darkgrey"
273bb02e0acSNicolas Bonnefon << "grey"
274bb02e0acSNicolas Bonnefon << "dimgrey"
275bb02e0acSNicolas Bonnefon // Reds
276bb02e0acSNicolas Bonnefon << "tomato"
277bb02e0acSNicolas Bonnefon << "orangered"
278bb02e0acSNicolas Bonnefon << "orange"
279bb02e0acSNicolas Bonnefon << "crimson"
280bb02e0acSNicolas Bonnefon << "darkred"
281bb02e0acSNicolas Bonnefon // Greens
282bb02e0acSNicolas Bonnefon << "greenyellow"
283bb02e0acSNicolas Bonnefon << "lightgreen"
284bb02e0acSNicolas Bonnefon << "darkgreen"
285bb02e0acSNicolas Bonnefon << "lightseagreen"
286bb02e0acSNicolas Bonnefon // Blues
287bb02e0acSNicolas Bonnefon << "lightcyan"
288bb02e0acSNicolas Bonnefon << "darkturquoise"
289bb02e0acSNicolas Bonnefon << "steelblue"
290bb02e0acSNicolas Bonnefon << "lightblue"
291bb02e0acSNicolas Bonnefon << "royalblue"
292bb02e0acSNicolas Bonnefon << "darkblue"
293bb02e0acSNicolas Bonnefon << "midnightblue"
294bb02e0acSNicolas Bonnefon // Browns
295bb02e0acSNicolas Bonnefon << "bisque"
296bb02e0acSNicolas Bonnefon << "tan"
297bb02e0acSNicolas Bonnefon << "sandybrown"
298bb02e0acSNicolas Bonnefon << "chocolate";
299bb02e0acSNicolas Bonnefon
300bb02e0acSNicolas Bonnefon for ( QStringList::const_iterator i = colorNames.constBegin();
301bb02e0acSNicolas Bonnefon i != colorNames.constEnd(); ++i ) {
302bb02e0acSNicolas Bonnefon QPixmap solidPixmap( 20, 10 );
303bb02e0acSNicolas Bonnefon solidPixmap.fill( QColor( *i ) );
304bb5e255bSAnton Filimonov QIcon solidIcon { solidPixmap };
305bb02e0acSNicolas Bonnefon
306bb5e255bSAnton Filimonov foreColorBox->addItem( solidIcon, *i );
307bb5e255bSAnton Filimonov backColorBox->addItem( solidIcon, *i );
308bb02e0acSNicolas Bonnefon }
309bb02e0acSNicolas Bonnefon }
310bb02e0acSNicolas Bonnefon
populateFilterList()311bb02e0acSNicolas Bonnefon void FiltersDialog::populateFilterList()
312bb02e0acSNicolas Bonnefon {
313bb02e0acSNicolas Bonnefon filterListWidget->clear();
31411582726SNicolas Bonnefon foreach ( Filter filter, filterSet->filterList ) {
315bb02e0acSNicolas Bonnefon QListWidgetItem* new_item = new QListWidgetItem( filter.pattern() );
316bb02e0acSNicolas Bonnefon // new_item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
317bb02e0acSNicolas Bonnefon new_item->setForeground( QBrush( QColor( filter.foreColorName() ) ) );
318bb02e0acSNicolas Bonnefon new_item->setBackground( QBrush( QColor( filter.backColorName() ) ) );
319bb02e0acSNicolas Bonnefon filterListWidget->addItem( new_item );
320bb02e0acSNicolas Bonnefon }
321bb02e0acSNicolas Bonnefon }
322