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();
75 ui.ignoreListView->selectionModel()->reset();
76 ui.editIgnoreRuleButton->setEnabled(false);
79 void IgnoreListSettingsPage::defaults() {
80 _ignoreListModel.loadDefaults();
83 void IgnoreListSettingsPage::save() {
84 if(_ignoreListModel.configChanged()) {
85 _ignoreListModel.commit();
87 ui.ignoreListView->selectionModel()->reset();
88 ui.editIgnoreRuleButton->setEnabled(false);
91 void IgnoreListSettingsPage::enableDialog(bool enabled) {
92 ui.newIgnoreRuleButton->setEnabled(enabled);
96 void IgnoreListSettingsPage::selectionChanged(const QItemSelection &selection, const QItemSelection &) {
97 bool state = !selection.isEmpty();
98 ui.deleteIgnoreRuleButton->setEnabled(state);
99 ui.editIgnoreRuleButton->setEnabled(state);
102 void IgnoreListSettingsPage::deleteSelectedIgnoreRule() {
103 if(!ui.ignoreListView->selectionModel()->hasSelection())
106 _ignoreListModel.removeIgnoreRule(ui.ignoreListView->selectionModel()->selectedIndexes()[0].row());
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;
119 IgnoreListEditDlg *dlg = new IgnoreListEditDlg(newItem, this, enableOkButton);
120 dlg->enableOkButton(enableOkButton);
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)
132 IgnoreListManager::IgnoreListItem item = dlg->ignoreListItem();
134 dlg = new IgnoreListEditDlg(item, this);
143 void IgnoreListSettingsPage::editSelectedIgnoreRule() {
144 if(!ui.ignoreListView->selectionModel()->hasSelection())
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());
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();
159 newIgnoreRule(ignoreRule);
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());
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);
180 QStyledItemDelegate::paint(painter, option, index);
183 // provide interactivity for the checkboxes
184 bool IgnoreListDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
185 const QStyleOptionViewItem &option, const QModelIndex &index) {
187 switch(event->type()) {
188 case QEvent::MouseButtonRelease:
189 model->setData(index, !index.data().toBool());
191 // don't show the default editor for the column
192 case QEvent::MouseButtonDblClick:
202 IgnoreListEditDlg::IgnoreListEditDlg(const IgnoreListManager::IgnoreListItem &item, QWidget *parent, bool enabled)
203 : QDialog(parent), _ignoreListItem(item), _hasChanged(enabled) {
205 setAttribute(Qt::WA_DeleteOnClose, false);
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);
218 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
220 ui.ignoreRuleLineEdit->setText(item.ignoreRule);
222 if(item.type == IgnoreListManager::MessageIgnore)
223 ui.messageTypeButton->setChecked(true);
225 ui.senderTypeButton->setChecked(true);
227 ui.isRegExCheckBox->setChecked(item.isRegEx);
228 ui.isActiveCheckBox->setChecked(item.isActive);
230 if(item.strictness == IgnoreListManager::HardStrictness)
231 ui.permanentStrictnessButton->setChecked(true);
233 ui.dynamicStrictnessButton->setChecked(true);
236 case IgnoreListManager::NetworkScope:
237 ui.networkScopeButton->setChecked(true);
238 ui.scopeRuleTextEdit->setEnabled(true);
240 case IgnoreListManager::ChannelScope:
241 ui.channelScopeButton->setChecked(true);
242 ui.scopeRuleTextEdit->setEnabled(true);
245 ui.globalScopeButton->setChecked(true);
246 ui.scopeRuleTextEdit->setEnabled(false);
249 if(item.scope == IgnoreListManager::GlobalScope)
250 ui.scopeRuleTextEdit->clear();
252 ui.scopeRuleTextEdit->setPlainText(item.scopeRule);
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()));
262 connect(ui.buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(aboutToAccept()));
265 void IgnoreListEditDlg::widgetHasChanged() {
266 if(ui.messageTypeButton->isChecked())
267 _clonedIgnoreListItem.type = IgnoreListManager::MessageIgnore;
269 _clonedIgnoreListItem.type = IgnoreListManager::SenderIgnore;
271 if(ui.permanentStrictnessButton->isChecked())
272 _clonedIgnoreListItem.strictness = IgnoreListManager::HardStrictness;
274 _clonedIgnoreListItem.strictness = IgnoreListManager::SoftStrictness;
276 if(ui.networkScopeButton->isChecked()) {
277 _clonedIgnoreListItem.scope = IgnoreListManager::NetworkScope;
278 ui.scopeRuleTextEdit->setEnabled(true);
280 else if(ui.channelScopeButton->isChecked()) {
281 _clonedIgnoreListItem.scope = IgnoreListManager::ChannelScope;
282 ui.scopeRuleTextEdit->setEnabled(true);
285 _clonedIgnoreListItem.scope = IgnoreListManager::GlobalScope;
286 ui.scopeRuleTextEdit->setEnabled(false);
289 if(_clonedIgnoreListItem.scope == IgnoreListManager::GlobalScope) {
290 _clonedIgnoreListItem.scopeRule = QString();
293 QStringList text = ui.scopeRuleTextEdit->toPlainText().split(";", QString::SkipEmptyParts);
294 QStringList::iterator it = text.begin();
295 while(it != text.end())
298 _clonedIgnoreListItem.scopeRule = text.join("; ");
301 _clonedIgnoreListItem.ignoreRule = ui.ignoreRuleLineEdit->text();
302 _clonedIgnoreListItem.isRegEx = ui.isRegExCheckBox->isChecked();
303 _clonedIgnoreListItem.isActive = ui.isActiveCheckBox->isChecked();
305 if(!_clonedIgnoreListItem.ignoreRule.isEmpty() && _clonedIgnoreListItem != _ignoreListItem)
309 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(_hasChanged);
312 void IgnoreListEditDlg::enableOkButton(bool state) {
313 ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(state);