1 /***************************************************************************
2 * Copyright (C) 2005-09 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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("Misc"), tr("Ignorelist"), 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());
68 IgnoreListSettingsPage::~IgnoreListSettingsPage() {
72 void IgnoreListSettingsPage::load() {
73 if(_ignoreListModel.configChanged())
74 _ignoreListModel.revert();
77 void IgnoreListSettingsPage::defaults() {
78 _ignoreListModel.loadDefaults();
81 void IgnoreListSettingsPage::save() {
82 if(_ignoreListModel.configChanged()) {
83 _ignoreListModel.commit();
85 ui.ignoreListView->selectionModel()->reset();
88 void IgnoreListSettingsPage::enableDialog(bool enabled) {
89 ui.newIgnoreRuleButton->setEnabled(enabled);
93 void IgnoreListSettingsPage::selectionChanged(const QItemSelection &selection, const QItemSelection &) {
94 bool state = !selection.isEmpty();
95 ui.deleteIgnoreRuleButton->setEnabled(state);
96 ui.editIgnoreRuleButton->setEnabled(state);
99 void IgnoreListSettingsPage::deleteSelectedIgnoreRule() {
100 if(!ui.ignoreListView->selectionModel()->hasSelection())
103 _ignoreListModel.removeIgnoreRule(ui.ignoreListView->selectionModel()->selectedIndexes()[0].row());
106 void IgnoreListSettingsPage::newIgnoreRule() {
107 IgnoreListEditDlg *dlg = new IgnoreListEditDlg(-1, IgnoreListManager::IgnoreListItem(), this);
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)
119 IgnoreListManager::IgnoreListItem item = dlg->ignoreListItem();
121 dlg = new IgnoreListEditDlg(-1, item, this);
130 void IgnoreListSettingsPage::editSelectedIgnoreRule() {
131 if(!ui.ignoreListView->selectionModel()->hasSelection())
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());
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());
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);
156 QStyledItemDelegate::paint(painter, option, index);
159 IgnoreListEditDlg::IgnoreListEditDlg(int row, const IgnoreListManager::IgnoreListItem &item, QWidget *parent)
160 : QDialog(parent), _selectedRow(row), _ignoreListItem(item), _hasChanged(false) {
162 setAttribute(Qt::WA_DeleteOnClose, false);
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);
175 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
177 ui.ignoreRuleLineEdit->setText(item.ignoreRule);
179 if(item.type == IgnoreListManager::MessageIgnore)
180 ui.messageTypeButton->setChecked(true);
182 ui.senderTypeButton->setChecked(true);
184 ui.isRegExCheckBox->setChecked(item.isRegEx);
185 ui.isActiveCheckBox->setChecked(item.isActive);
187 if(item.strictness == IgnoreListManager::HardStrictness)
188 ui.permanentStrictnessButton->setChecked(true);
190 ui.dynamicStrictnessButton->setChecked(true);
193 case IgnoreListManager::NetworkScope:
194 ui.networkScopeButton->setChecked(true);
195 ui.scopeRuleTextEdit->setEnabled(true);
197 case IgnoreListManager::ChannelScope:
198 ui.channelScopeButton->setChecked(true);
199 ui.scopeRuleTextEdit->setEnabled(true);
202 ui.globalScopeButton->setChecked(true);
203 ui.scopeRuleTextEdit->setEnabled(false);
206 if(item.scope == IgnoreListManager::GlobalScope)
207 ui.scopeRuleTextEdit->clear();
209 ui.scopeRuleTextEdit->setPlainText(item.scopeRule);
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()));
219 connect(ui.buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(aboutToAccept()));
222 void IgnoreListEditDlg::widgetHasChanged() {
223 if(ui.messageTypeButton->isChecked())
224 _clonedIgnoreListItem.type = IgnoreListManager::MessageIgnore;
226 _clonedIgnoreListItem.type = IgnoreListManager::SenderIgnore;
228 if(ui.permanentStrictnessButton->isChecked())
229 _clonedIgnoreListItem.strictness = IgnoreListManager::HardStrictness;
231 _clonedIgnoreListItem.strictness = IgnoreListManager::SoftStrictness;
233 if(ui.networkScopeButton->isChecked()) {
234 _clonedIgnoreListItem.scope = IgnoreListManager::NetworkScope;
235 ui.scopeRuleTextEdit->setEnabled(true);
237 else if(ui.channelScopeButton->isChecked()) {
238 _clonedIgnoreListItem.scope = IgnoreListManager::ChannelScope;
239 ui.scopeRuleTextEdit->setEnabled(true);
242 _clonedIgnoreListItem.scope = IgnoreListManager::GlobalScope;
243 ui.scopeRuleTextEdit->setEnabled(false);
246 if(_clonedIgnoreListItem.scope == IgnoreListManager::GlobalScope) {
247 _clonedIgnoreListItem.scopeRule = QString();
250 QStringList text = ui.scopeRuleTextEdit->toPlainText().split(";", QString::SkipEmptyParts);
251 QStringList::iterator it = text.begin();
252 while(it != text.end())
255 _clonedIgnoreListItem.scopeRule = text.join("; ");
258 _clonedIgnoreListItem.ignoreRule = ui.ignoreRuleLineEdit->text();
259 _clonedIgnoreListItem.isRegEx = ui.isRegExCheckBox->isChecked();
260 _clonedIgnoreListItem.isActive = ui.isActiveCheckBox->isChecked();
262 if(!_clonedIgnoreListItem.ignoreRule.isEmpty() && _clonedIgnoreListItem != _ignoreListItem)
266 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(_hasChanged);
269 // provide interactivity for the checkboxes
270 bool IgnoreListDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
271 const QStyleOptionViewItem &option, const QModelIndex &index) {
273 switch(event->type()) {
274 case QEvent::MouseButtonRelease:
275 model->setData(index, !index.data().toBool());
277 // don't show the default editor for the column
278 case QEvent::MouseButtonDblClick: