1 /***************************************************************************
2 * Copyright (C) 2005-2013 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "ignorelistsettingspage.h"
23 #include <QHeaderView>
24 #include <QItemSelectionModel>
25 #include <QModelIndex>
27 #include <QMessageBox>
31 #include "iconloader.h"
33 IgnoreListSettingsPage::IgnoreListSettingsPage(QWidget *parent)
34 : SettingsPage(tr("IRC"), tr("Ignore List"), parent)
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"));
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);
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);
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)));
65 enableDialog(_ignoreListModel.isReady());
69 IgnoreListSettingsPage::~IgnoreListSettingsPage()
75 void IgnoreListSettingsPage::load()
77 if (_ignoreListModel.configChanged())
78 _ignoreListModel.revert();
79 ui.ignoreListView->selectionModel()->reset();
80 ui.editIgnoreRuleButton->setEnabled(false);
84 void IgnoreListSettingsPage::defaults()
86 _ignoreListModel.loadDefaults();
90 void IgnoreListSettingsPage::save()
92 if (_ignoreListModel.configChanged()) {
93 _ignoreListModel.commit();
95 ui.ignoreListView->selectionModel()->reset();
96 ui.editIgnoreRuleButton->setEnabled(false);
100 void IgnoreListSettingsPage::enableDialog(bool enabled)
102 ui.newIgnoreRuleButton->setEnabled(enabled);
107 void IgnoreListSettingsPage::selectionChanged(const QItemSelection &selection, const QItemSelection &)
109 bool state = !selection.isEmpty();
110 ui.deleteIgnoreRuleButton->setEnabled(state);
111 ui.editIgnoreRuleButton->setEnabled(state);
115 void IgnoreListSettingsPage::deleteSelectedIgnoreRule()
117 if (!ui.ignoreListView->selectionModel()->hasSelection())
120 _ignoreListModel.removeIgnoreRule(ui.ignoreListView->selectionModel()->selectedIndexes()[0].row());
124 void IgnoreListSettingsPage::newIgnoreRule(QString rule)
126 IgnoreListManager::IgnoreListItem newItem = IgnoreListManager::IgnoreListItem();
127 newItem.strictness = IgnoreListManager::SoftStrictness;
128 newItem.scope = IgnoreListManager::GlobalScope;
129 newItem.isRegEx = false;
130 newItem.isActive = true;
132 bool enableOkButton = false;
133 if (!rule.isEmpty()) {
134 // we're called from contextmenu
135 newItem.ignoreRule = rule;
136 enableOkButton = true;
139 IgnoreListEditDlg *dlg = new IgnoreListEditDlg(newItem, this, enableOkButton);
140 dlg->enableOkButton(enableOkButton);
141 while (dlg->exec() == QDialog::Accepted) {
142 if (!_ignoreListModel.newIgnoreRule(dlg->ignoreListItem())) {
143 if (QMessageBox::warning(this,
144 tr("Rule already exists"),
145 tr("There is already a rule\n\"%1\"\nPlease choose another rule.")
146 .arg(dlg->ignoreListItem().ignoreRule),
147 QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok)
148 == QMessageBox::Cancel)
151 IgnoreListManager::IgnoreListItem item = dlg->ignoreListItem();
153 dlg = new IgnoreListEditDlg(item, this);
163 void IgnoreListSettingsPage::editSelectedIgnoreRule()
165 if (!ui.ignoreListView->selectionModel()->hasSelection())
167 int row = ui.ignoreListView->selectionModel()->selectedIndexes()[0].row();
168 IgnoreListEditDlg dlg(_ignoreListModel.ignoreListItemAt(row), this);
169 dlg.setAttribute(Qt::WA_DeleteOnClose, false);
170 if (dlg.exec() == QDialog::Accepted) {
171 _ignoreListModel.setIgnoreListItemAt(row, dlg.ignoreListItem());
176 void IgnoreListSettingsPage::editIgnoreRule(const QString &ignoreRule)
178 ui.ignoreListView->selectionModel()->select(_ignoreListModel.indexOf(ignoreRule), QItemSelectionModel::Select);
179 if (ui.ignoreListView->selectionModel()->hasSelection()) // && ui.ignoreListView->selectionModel()->selectedIndexes()[0].row() != -1)
180 editSelectedIgnoreRule();
182 newIgnoreRule(ignoreRule);
189 void IgnoreListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
191 if (index.column() == 0) {
192 QStyle *style = QApplication::style();
193 if (option.state & QStyle::State_Selected)
194 painter->fillRect(option.rect, option.palette.highlight());
196 QStyleOptionButton opts;
197 opts.direction = option.direction;
198 opts.rect = option.rect;
199 opts.rect.moveLeft(option.rect.center().rx()-10);
200 opts.state = option.state;
201 opts.state |= index.data().toBool() ? QStyle::State_On : QStyle::State_Off;
202 style->drawControl(QStyle::CE_CheckBox, &opts, painter);
205 QStyledItemDelegate::paint(painter, option, index);
209 // provide interactivity for the checkboxes
210 bool IgnoreListDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
211 const QStyleOptionViewItem &option, const QModelIndex &index)
214 switch (event->type()) {
215 case QEvent::MouseButtonRelease:
216 model->setData(index, !index.data().toBool());
218 // don't show the default editor for the column
219 case QEvent::MouseButtonDblClick:
230 IgnoreListEditDlg::IgnoreListEditDlg(const IgnoreListManager::IgnoreListItem &item, QWidget *parent, bool enabled)
231 : QDialog(parent), _ignoreListItem(item), _hasChanged(enabled)
234 setAttribute(Qt::WA_DeleteOnClose, false);
236 // FIXME patch out the bugger completely if it's good without it
237 ui.isActiveCheckBox->hide();
239 // setup buttongroups
240 // this could be moved to .ui file with qt4.5
241 _typeButtonGroup.addButton(ui.senderTypeButton, 0);
242 _typeButtonGroup.addButton(ui.messageTypeButton, 1);
243 _typeButtonGroup.addButton(ui.ctcpTypeButton, 2);
244 _strictnessButtonGroup.addButton(ui.dynamicStrictnessButton, 0);
245 _strictnessButtonGroup.addButton(ui.permanentStrictnessButton, 1);
246 _scopeButtonGroup.addButton(ui.globalScopeButton, 0);
247 _scopeButtonGroup.addButton(ui.networkScopeButton, 1);
248 _scopeButtonGroup.addButton(ui.channelScopeButton, 2);
250 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
252 ui.ignoreRuleLineEdit->setText(item.ignoreRule);
254 if (item.type == IgnoreListManager::MessageIgnore)
255 ui.messageTypeButton->setChecked(true);
256 else if (item.type == IgnoreListManager::CtcpIgnore)
257 ui.ctcpTypeButton->setChecked(true);
259 ui.senderTypeButton->setChecked(true);
261 ui.isRegExCheckBox->setChecked(item.isRegEx);
262 ui.isActiveCheckBox->setChecked(item.isActive);
264 if (item.strictness == IgnoreListManager::HardStrictness)
265 ui.permanentStrictnessButton->setChecked(true);
267 ui.dynamicStrictnessButton->setChecked(true);
269 switch (item.scope) {
270 case IgnoreListManager::NetworkScope:
271 ui.networkScopeButton->setChecked(true);
272 ui.scopeRuleTextEdit->setEnabled(true);
274 case IgnoreListManager::ChannelScope:
275 ui.channelScopeButton->setChecked(true);
276 ui.scopeRuleTextEdit->setEnabled(true);
279 ui.globalScopeButton->setChecked(true);
280 ui.scopeRuleTextEdit->setEnabled(false);
283 if (item.scope == IgnoreListManager::GlobalScope)
284 ui.scopeRuleTextEdit->clear();
286 ui.scopeRuleTextEdit->setPlainText(item.scopeRule);
288 connect(ui.ignoreRuleLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(widgetHasChanged()));
289 connect(ui.scopeRuleTextEdit, SIGNAL(textChanged()), this, SLOT(widgetHasChanged()));
290 connect(&_typeButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(widgetHasChanged()));
291 connect(&_strictnessButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(widgetHasChanged()));
292 connect(&_scopeButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(widgetHasChanged()));
293 connect(ui.isRegExCheckBox, SIGNAL(stateChanged(int)), this, SLOT(widgetHasChanged()));
294 connect(ui.isActiveCheckBox, SIGNAL(stateChanged(int)), this, SLOT(widgetHasChanged()));
296 connect(ui.buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(aboutToAccept()));
301 void IgnoreListEditDlg::widgetHasChanged()
303 if (ui.messageTypeButton->isChecked())
304 _clonedIgnoreListItem.type = IgnoreListManager::MessageIgnore;
305 else if (ui.ctcpTypeButton->isChecked())
306 _clonedIgnoreListItem.type = IgnoreListManager::CtcpIgnore;
308 _clonedIgnoreListItem.type = IgnoreListManager::SenderIgnore;
310 if (ui.permanentStrictnessButton->isChecked())
311 _clonedIgnoreListItem.strictness = IgnoreListManager::HardStrictness;
313 _clonedIgnoreListItem.strictness = IgnoreListManager::SoftStrictness;
315 if (ui.networkScopeButton->isChecked()) {
316 _clonedIgnoreListItem.scope = IgnoreListManager::NetworkScope;
317 ui.scopeRuleTextEdit->setEnabled(true);
319 else if (ui.channelScopeButton->isChecked()) {
320 _clonedIgnoreListItem.scope = IgnoreListManager::ChannelScope;
321 ui.scopeRuleTextEdit->setEnabled(true);
324 _clonedIgnoreListItem.scope = IgnoreListManager::GlobalScope;
325 ui.scopeRuleTextEdit->setEnabled(false);
328 if (_clonedIgnoreListItem.scope == IgnoreListManager::GlobalScope) {
329 _clonedIgnoreListItem.scopeRule = QString();
332 QStringList text = ui.scopeRuleTextEdit->toPlainText().split(";", QString::SkipEmptyParts);
333 QStringList::iterator it = text.begin();
334 while (it != text.end()) {
339 _clonedIgnoreListItem.scopeRule = text.join("; ");
342 _clonedIgnoreListItem.ignoreRule = ui.ignoreRuleLineEdit->text();
343 _clonedIgnoreListItem.isRegEx = ui.isRegExCheckBox->isChecked();
344 _clonedIgnoreListItem.isActive = ui.isActiveCheckBox->isChecked();
346 if (!_clonedIgnoreListItem.ignoreRule.isEmpty() && _clonedIgnoreListItem != _ignoreListItem)
350 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(_hasChanged);
354 void IgnoreListEditDlg::enableOkButton(bool state)
356 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(state);