Build with Qt 4.4 again
[quassel.git] / src / qtui / settingspages / ignorelistsettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "ignorelistsettingspage.h"
22
23 #include <QHeaderView>
24 #include <QItemSelectionModel>
25 #include <QModelIndex>
26 #include <QPainter>
27 #include <QMessageBox>
28 #include <QString>
29 #include <QEvent>
30
31 #include "iconloader.h"
32
33 IgnoreListSettingsPage::IgnoreListSettingsPage(QWidget *parent)
34   : SettingsPage(tr("Misc"), tr("Ignorelist"), parent)
35 {
36   ui.setupUi(this);
37   _delegate = new IgnoreListDelegate(ui.ignoreListView);
38   ui.newIgnoreRuleButton->setIcon(SmallIcon("list-add"));
39   ui.deleteIgnoreRuleButton->setIcon(SmallIcon("edit-delete"));
40   ui.editIgnoreRuleButton->setIcon(SmallIcon("configure"));
41
42   ui.ignoreListView->setSelectionBehavior(QAbstractItemView::SelectRows);
43   ui.ignoreListView->setSelectionMode(QAbstractItemView::SingleSelection);
44   ui.ignoreListView->setAlternatingRowColors(true);
45   ui.ignoreListView->setTabKeyNavigation(false);
46   ui.ignoreListView->setModel(&_ignoreListModel);
47  // ui.ignoreListView->setEditTriggers(QAbstractItemView::NoEditTriggers);
48
49   // ui.ignoreListView->setSortingEnabled(true);
50   ui.ignoreListView->verticalHeader()->hide();
51   ui.ignoreListView->hideColumn(1);
52   ui.ignoreListView->resizeColumnToContents(0);
53   ui.ignoreListView->horizontalHeader()->setStretchLastSection(true);
54   ui.ignoreListView->setItemDelegateForColumn(0, _delegate);
55   ui.ignoreListView->viewport()->setAttribute(Qt::WA_Hover);
56   ui.ignoreListView->viewport()->setMouseTracking(true);
57
58   connect(ui.ignoreListView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), this, SLOT(selectionChanged(const QItemSelection &, const QItemSelection &)));
59   connect(ui.newIgnoreRuleButton, SIGNAL(clicked()), this, SLOT(newIgnoreRule()));
60   connect(ui.deleteIgnoreRuleButton, SIGNAL(clicked()), this, SLOT(deleteSelectedIgnoreRule()));
61   connect(ui.editIgnoreRuleButton, SIGNAL(clicked()), this, SLOT(editSelectedIgnoreRule()));
62   connect(&_ignoreListModel, SIGNAL(configChanged(bool)), this, SLOT(setChangedState(bool)));
63   connect(&_ignoreListModel, SIGNAL(modelReady(bool)), this, SLOT(enableDialog(bool)));
64
65   enableDialog(_ignoreListModel.isReady());
66 }
67
68 IgnoreListSettingsPage::~IgnoreListSettingsPage() {
69   delete _delegate;
70 }
71
72 void IgnoreListSettingsPage::load() {
73   if(_ignoreListModel.configChanged())
74     _ignoreListModel.revert();
75 }
76
77 void IgnoreListSettingsPage::defaults() {
78   _ignoreListModel.loadDefaults();
79 }
80
81 void IgnoreListSettingsPage::save() {
82   if(_ignoreListModel.configChanged()) {
83     _ignoreListModel.commit();
84   }
85   ui.ignoreListView->selectionModel()->reset();
86 }
87
88 void IgnoreListSettingsPage::enableDialog(bool enabled) {
89   ui.newIgnoreRuleButton->setEnabled(enabled);
90   setEnabled(enabled);
91 }
92
93 void IgnoreListSettingsPage::selectionChanged(const QItemSelection &selection, const QItemSelection &) {
94   bool state = !selection.isEmpty();
95   ui.deleteIgnoreRuleButton->setEnabled(state);
96   ui.editIgnoreRuleButton->setEnabled(state);
97 }
98
99 void IgnoreListSettingsPage::deleteSelectedIgnoreRule() {
100   if(!ui.ignoreListView->selectionModel()->hasSelection())
101     return;
102
103   _ignoreListModel.removeIgnoreRule(ui.ignoreListView->selectionModel()->selectedIndexes()[0].row());
104 }
105
106 void IgnoreListSettingsPage::newIgnoreRule() {
107   IgnoreListEditDlg *dlg = new IgnoreListEditDlg(-1, IgnoreListManager::IgnoreListItem(), this);
108
109   while(dlg->exec() == QDialog::Accepted) {
110     if(!_ignoreListModel.newIgnoreRule(dlg->ignoreListItem())) {
111       if(QMessageBox::warning(this,
112                          tr("Rule already exists"),
113                          tr("There is already a rule\n\"%1\"\nPlease choose another rule.")
114                          .arg(dlg->ignoreListItem().ignoreRule),
115                          QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok)
116           == QMessageBox::Cancel)
117         break;
118
119       IgnoreListManager::IgnoreListItem item = dlg->ignoreListItem();
120       delete dlg;
121       dlg = new IgnoreListEditDlg(-1, item, this);
122     }
123     else {
124       break;
125     }
126   }
127   dlg->deleteLater();
128 }
129
130 void IgnoreListSettingsPage::editSelectedIgnoreRule() {
131   if(!ui.ignoreListView->selectionModel()->hasSelection())
132     return;
133   int row = ui.ignoreListView->selectionModel()->selectedIndexes()[0].row();
134   IgnoreListEditDlg dlg(row, _ignoreListModel.ignoreListItemAt(row), this);
135   dlg.setAttribute(Qt::WA_DeleteOnClose, false);
136   if(dlg.exec() == QDialog::Accepted) {
137     _ignoreListModel.setIgnoreListItemAt(row, dlg.ignoreListItem());
138   }
139 }
140
141 void IgnoreListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
142   if(index.column() == 0) {
143     QStyle *style = QApplication::style();
144     if(option.state & QStyle::State_Selected)
145       painter->fillRect(option.rect, option.palette.highlight());
146
147     QStyleOptionButton opts;
148     opts.direction = option.direction;
149     opts.rect = option.rect;
150     opts.rect.moveLeft(option.rect.center().rx()-10);
151     opts.state = option.state;
152     opts.state |= index.data().toBool() ? QStyle::State_On : QStyle::State_Off;
153     style->drawControl(QStyle::CE_CheckBox, &opts, painter);
154   }
155   else
156     QStyledItemDelegate::paint(painter, option, index);
157 }
158
159 IgnoreListEditDlg::IgnoreListEditDlg(int row, const IgnoreListManager::IgnoreListItem &item, QWidget *parent)
160     : QDialog(parent), _selectedRow(row), _ignoreListItem(item), _hasChanged(false) {
161   ui.setupUi(this);
162   setAttribute(Qt::WA_DeleteOnClose, false);
163   setModal(true);
164
165   // setup buttongroups
166   // this could be moved to .ui file with qt4.5
167   _typeButtonGroup.addButton(ui.senderTypeButton, 0);
168   _typeButtonGroup.addButton(ui.messageTypeButton, 1);
169   _strictnessButtonGroup.addButton(ui.dynamicStrictnessButton, 0);
170   _strictnessButtonGroup.addButton(ui.permanentStrictnessButton, 1);
171   _scopeButtonGroup.addButton(ui.globalScopeButton, 0);
172   _scopeButtonGroup.addButton(ui.networkScopeButton, 1);
173   _scopeButtonGroup.addButton(ui.channelScopeButton, 2);
174
175   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
176
177   ui.ignoreRuleLineEdit->setText(item.ignoreRule);
178
179   if(item.type == IgnoreListManager::MessageIgnore)
180     ui.messageTypeButton->setChecked(true);
181   else
182     ui.senderTypeButton->setChecked(true);
183
184   ui.isRegExCheckBox->setChecked(item.isRegEx);
185   ui.isActiveCheckBox->setChecked(item.isActive);
186
187   if(item.strictness == IgnoreListManager::HardStrictness)
188     ui.permanentStrictnessButton->setChecked(true);
189   else
190     ui.dynamicStrictnessButton->setChecked(true);
191
192   switch(item.scope) {
193     case IgnoreListManager::NetworkScope:
194       ui.networkScopeButton->setChecked(true);
195       ui.scopeRuleTextEdit->setEnabled(true);
196       break;
197     case IgnoreListManager::ChannelScope:
198       ui.channelScopeButton->setChecked(true);
199       ui.scopeRuleTextEdit->setEnabled(true);
200       break;
201     default:
202       ui.globalScopeButton->setChecked(true);
203       ui.scopeRuleTextEdit->setEnabled(false);
204   }
205
206   if(item.scope == IgnoreListManager::GlobalScope)
207     ui.scopeRuleTextEdit->clear();
208   else
209     ui.scopeRuleTextEdit->setPlainText(item.scopeRule);
210
211   connect(ui.ignoreRuleLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(widgetHasChanged()));
212   connect(ui.scopeRuleTextEdit, SIGNAL(textChanged()), this, SLOT(widgetHasChanged()));
213   connect(&_typeButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(widgetHasChanged()));
214   connect(&_strictnessButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(widgetHasChanged()));
215   connect(&_scopeButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(widgetHasChanged()));
216   connect(ui.isRegExCheckBox, SIGNAL(stateChanged(int)), this, SLOT(widgetHasChanged()));
217   connect(ui.isActiveCheckBox, SIGNAL(stateChanged(int)), this, SLOT(widgetHasChanged()));
218
219   connect(ui.buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(aboutToAccept()));
220 }
221
222 void IgnoreListEditDlg::widgetHasChanged() {
223   if(ui.messageTypeButton->isChecked())
224     _clonedIgnoreListItem.type = IgnoreListManager::MessageIgnore;
225   else
226     _clonedIgnoreListItem.type = IgnoreListManager::SenderIgnore;
227
228   if(ui.permanentStrictnessButton->isChecked())
229     _clonedIgnoreListItem.strictness = IgnoreListManager::HardStrictness;
230   else
231     _clonedIgnoreListItem.strictness = IgnoreListManager::SoftStrictness;
232
233   if(ui.networkScopeButton->isChecked()) {
234     _clonedIgnoreListItem.scope = IgnoreListManager::NetworkScope;
235     ui.scopeRuleTextEdit->setEnabled(true);
236   }
237   else if(ui.channelScopeButton->isChecked()) {
238     _clonedIgnoreListItem.scope = IgnoreListManager::ChannelScope;
239     ui.scopeRuleTextEdit->setEnabled(true);
240   }
241   else {
242     _clonedIgnoreListItem.scope = IgnoreListManager::GlobalScope;
243     ui.scopeRuleTextEdit->setEnabled(false);
244   }
245
246   if(_clonedIgnoreListItem.scope == IgnoreListManager::GlobalScope) {
247     _clonedIgnoreListItem.scopeRule = QString();
248   }
249   else {
250     QStringList text = ui.scopeRuleTextEdit->toPlainText().split(";", QString::SkipEmptyParts);
251     QStringList::iterator it = text.begin();
252     while(it != text.end())
253       (*it++).trimmed();
254
255     _clonedIgnoreListItem.scopeRule = text.join("; ");
256   }
257
258   _clonedIgnoreListItem.ignoreRule = ui.ignoreRuleLineEdit->text();
259   _clonedIgnoreListItem.isRegEx = ui.isRegExCheckBox->isChecked();
260   _clonedIgnoreListItem.isActive = ui.isActiveCheckBox->isChecked();
261
262   if(!_clonedIgnoreListItem.ignoreRule.isEmpty() && _clonedIgnoreListItem != _ignoreListItem)
263     _hasChanged = true;
264   else
265     _hasChanged = false;
266   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(_hasChanged);
267 }
268
269 // provide interactivity for the checkboxes
270 bool IgnoreListDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
271                                      const QStyleOptionViewItem &option, const QModelIndex &index) {
272   Q_UNUSED(option)
273   switch(event->type()) {
274     case QEvent::MouseButtonRelease:
275       model->setData(index, !index.data().toBool());
276       return true;
277       // don't show the default editor for the column
278     case QEvent::MouseButtonDblClick:
279       return true;
280     default:
281       return false;
282   }
283 }