Provide a contextmenu for the ignore list
[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 #include <QDebug>
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   ui.ignoreListView->selectionModel()->reset();
76   ui.editIgnoreRuleButton->setEnabled(false);
77 }
78
79 void IgnoreListSettingsPage::defaults() {
80   _ignoreListModel.loadDefaults();
81 }
82
83 void IgnoreListSettingsPage::save() {
84   if(_ignoreListModel.configChanged()) {
85     _ignoreListModel.commit();
86   }
87   ui.ignoreListView->selectionModel()->reset();
88   ui.editIgnoreRuleButton->setEnabled(false);
89 }
90
91 void IgnoreListSettingsPage::enableDialog(bool enabled) {
92   ui.newIgnoreRuleButton->setEnabled(enabled);
93   setEnabled(enabled);
94 }
95
96 void IgnoreListSettingsPage::selectionChanged(const QItemSelection &selection, const QItemSelection &) {
97   bool state = !selection.isEmpty();
98   ui.deleteIgnoreRuleButton->setEnabled(state);
99   ui.editIgnoreRuleButton->setEnabled(state);
100 }
101
102 void IgnoreListSettingsPage::deleteSelectedIgnoreRule() {
103   if(!ui.ignoreListView->selectionModel()->hasSelection())
104     return;
105
106   _ignoreListModel.removeIgnoreRule(ui.ignoreListView->selectionModel()->selectedIndexes()[0].row());
107 }
108
109 void IgnoreListSettingsPage::newIgnoreRule(QString rule) {
110   IgnoreListManager::IgnoreListItem newItem = IgnoreListManager::IgnoreListItem();
111   newItem.isActive = true;
112   bool enableOkButton = false;
113   if(!rule.isEmpty()) {
114     // we're called from contextmenu
115     newItem.ignoreRule = rule;
116     enableOkButton = true;
117   }
118
119   IgnoreListEditDlg *dlg = new IgnoreListEditDlg(newItem, this, enableOkButton);
120   dlg->enableOkButton(enableOkButton);
121
122   while(dlg->exec() == QDialog::Accepted) {
123     if(!_ignoreListModel.newIgnoreRule(dlg->ignoreListItem())) {
124       if(QMessageBox::warning(this,
125                          tr("Rule already exists"),
126                          tr("There is already a rule\n\"%1\"\nPlease choose another rule.")
127                          .arg(dlg->ignoreListItem().ignoreRule),
128                          QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok)
129           == QMessageBox::Cancel)
130         break;
131
132       IgnoreListManager::IgnoreListItem item = dlg->ignoreListItem();
133       delete dlg;
134       dlg = new IgnoreListEditDlg(item, this);
135     }
136     else {
137       break;
138     }
139   }
140   dlg->deleteLater();
141 }
142
143 void IgnoreListSettingsPage::editSelectedIgnoreRule() {
144   if(!ui.ignoreListView->selectionModel()->hasSelection())
145     return;
146   int row = ui.ignoreListView->selectionModel()->selectedIndexes()[0].row();
147   IgnoreListEditDlg dlg(_ignoreListModel.ignoreListItemAt(row), this);
148   dlg.setAttribute(Qt::WA_DeleteOnClose, false);
149   if(dlg.exec() == QDialog::Accepted) {
150     _ignoreListModel.setIgnoreListItemAt(row, dlg.ignoreListItem());
151   }
152 }
153
154 void IgnoreListSettingsPage::editIgnoreRule(const QString &ignoreRule) {
155   ui.ignoreListView->selectionModel()->select(_ignoreListModel.indexOf(ignoreRule), QItemSelectionModel::Select);
156   if(ui.ignoreListView->selectionModel()->hasSelection())// && ui.ignoreListView->selectionModel()->selectedIndexes()[0].row() != -1)
157     editSelectedIgnoreRule();
158   else
159     newIgnoreRule(ignoreRule);
160 }
161
162 /*
163   IgnoreListDelegate
164 */
165 void IgnoreListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
166   if(index.column() == 0) {
167     QStyle *style = QApplication::style();
168     if(option.state & QStyle::State_Selected)
169       painter->fillRect(option.rect, option.palette.highlight());
170
171     QStyleOptionButton opts;
172     opts.direction = option.direction;
173     opts.rect = option.rect;
174     opts.rect.moveLeft(option.rect.center().rx()-10);
175     opts.state = option.state;
176     opts.state |= index.data().toBool() ? QStyle::State_On : QStyle::State_Off;
177     style->drawControl(QStyle::CE_CheckBox, &opts, painter);
178   }
179   else
180     QStyledItemDelegate::paint(painter, option, index);
181 }
182
183 // provide interactivity for the checkboxes
184 bool IgnoreListDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
185                                      const QStyleOptionViewItem &option, const QModelIndex &index) {
186   Q_UNUSED(option)
187   switch(event->type()) {
188     case QEvent::MouseButtonRelease:
189       model->setData(index, !index.data().toBool());
190       return true;
191       // don't show the default editor for the column
192     case QEvent::MouseButtonDblClick:
193       return true;
194     default:
195       return false;
196   }
197 }
198
199 /*
200   IgnoreListEditDlg
201 */
202 IgnoreListEditDlg::IgnoreListEditDlg(const IgnoreListManager::IgnoreListItem &item, QWidget *parent, bool enabled)
203     : QDialog(parent), _ignoreListItem(item), _hasChanged(enabled) {
204   ui.setupUi(this);
205   setAttribute(Qt::WA_DeleteOnClose, false);
206   setModal(true);
207
208   // setup buttongroups
209   // this could be moved to .ui file with qt4.5
210   _typeButtonGroup.addButton(ui.senderTypeButton, 0);
211   _typeButtonGroup.addButton(ui.messageTypeButton, 1);
212   _strictnessButtonGroup.addButton(ui.dynamicStrictnessButton, 0);
213   _strictnessButtonGroup.addButton(ui.permanentStrictnessButton, 1);
214   _scopeButtonGroup.addButton(ui.globalScopeButton, 0);
215   _scopeButtonGroup.addButton(ui.networkScopeButton, 1);
216   _scopeButtonGroup.addButton(ui.channelScopeButton, 2);
217
218   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
219
220   ui.ignoreRuleLineEdit->setText(item.ignoreRule);
221
222   if(item.type == IgnoreListManager::MessageIgnore)
223     ui.messageTypeButton->setChecked(true);
224   else
225     ui.senderTypeButton->setChecked(true);
226
227   ui.isRegExCheckBox->setChecked(item.isRegEx);
228   ui.isActiveCheckBox->setChecked(item.isActive);
229
230   if(item.strictness == IgnoreListManager::HardStrictness)
231     ui.permanentStrictnessButton->setChecked(true);
232   else
233     ui.dynamicStrictnessButton->setChecked(true);
234
235   switch(item.scope) {
236     case IgnoreListManager::NetworkScope:
237       ui.networkScopeButton->setChecked(true);
238       ui.scopeRuleTextEdit->setEnabled(true);
239       break;
240     case IgnoreListManager::ChannelScope:
241       ui.channelScopeButton->setChecked(true);
242       ui.scopeRuleTextEdit->setEnabled(true);
243       break;
244     default:
245       ui.globalScopeButton->setChecked(true);
246       ui.scopeRuleTextEdit->setEnabled(false);
247   }
248
249   if(item.scope == IgnoreListManager::GlobalScope)
250     ui.scopeRuleTextEdit->clear();
251   else
252     ui.scopeRuleTextEdit->setPlainText(item.scopeRule);
253
254   connect(ui.ignoreRuleLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(widgetHasChanged()));
255   connect(ui.scopeRuleTextEdit, SIGNAL(textChanged()), this, SLOT(widgetHasChanged()));
256   connect(&_typeButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(widgetHasChanged()));
257   connect(&_strictnessButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(widgetHasChanged()));
258   connect(&_scopeButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(widgetHasChanged()));
259   connect(ui.isRegExCheckBox, SIGNAL(stateChanged(int)), this, SLOT(widgetHasChanged()));
260   connect(ui.isActiveCheckBox, SIGNAL(stateChanged(int)), this, SLOT(widgetHasChanged()));
261
262   connect(ui.buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(aboutToAccept()));
263 }
264
265 void IgnoreListEditDlg::widgetHasChanged() {
266   if(ui.messageTypeButton->isChecked())
267     _clonedIgnoreListItem.type = IgnoreListManager::MessageIgnore;
268   else
269     _clonedIgnoreListItem.type = IgnoreListManager::SenderIgnore;
270
271   if(ui.permanentStrictnessButton->isChecked())
272     _clonedIgnoreListItem.strictness = IgnoreListManager::HardStrictness;
273   else
274     _clonedIgnoreListItem.strictness = IgnoreListManager::SoftStrictness;
275
276   if(ui.networkScopeButton->isChecked()) {
277     _clonedIgnoreListItem.scope = IgnoreListManager::NetworkScope;
278     ui.scopeRuleTextEdit->setEnabled(true);
279   }
280   else if(ui.channelScopeButton->isChecked()) {
281     _clonedIgnoreListItem.scope = IgnoreListManager::ChannelScope;
282     ui.scopeRuleTextEdit->setEnabled(true);
283   }
284   else {
285     _clonedIgnoreListItem.scope = IgnoreListManager::GlobalScope;
286     ui.scopeRuleTextEdit->setEnabled(false);
287   }
288
289   if(_clonedIgnoreListItem.scope == IgnoreListManager::GlobalScope) {
290     _clonedIgnoreListItem.scopeRule = QString();
291   }
292   else {
293     QStringList text = ui.scopeRuleTextEdit->toPlainText().split(";", QString::SkipEmptyParts);
294     QStringList::iterator it = text.begin();
295     while(it != text.end())
296       (*it++).trimmed();
297
298     _clonedIgnoreListItem.scopeRule = text.join("; ");
299   }
300
301   _clonedIgnoreListItem.ignoreRule = ui.ignoreRuleLineEdit->text();
302   _clonedIgnoreListItem.isRegEx = ui.isRegExCheckBox->isChecked();
303   _clonedIgnoreListItem.isActive = ui.isActiveCheckBox->isChecked();
304
305   if(!_clonedIgnoreListItem.ignoreRule.isEmpty() && _clonedIgnoreListItem != _ignoreListItem)
306     _hasChanged = true;
307   else
308     _hasChanged = false;
309   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(_hasChanged);
310 }
311
312 void IgnoreListEditDlg::enableOkButton(bool state) {
313   ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(state);
314 }