1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel IRC Team *
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 Highlight Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
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 Highlight Public License for more details. *
15 * You should have received a copy of the GNU Highlight 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 "highlightsettingspage.h"
24 #include "uisettings.h"
26 #include <QHeaderView>
29 HighlightSettingsPage::HighlightSettingsPage(QWidget *parent)
30 : SettingsPage(tr("Behaviour"), tr("Highlight"), parent) {
32 ui.highlightTable->verticalHeader()->hide();
33 ui.highlightTable->setShowGrid(false);
35 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::RegExColumn)->setToolTip("<b>RegEx</b>: This option determines if the highlight rule should be interpreted as a <b>regular expression</b> or just as a keyword.");
36 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::RegExColumn)->setWhatsThis("<b>RegEx</b>: This option determines if the highlight rule should be interpreted as a <b>regular expression</b> or just as a keyword.");
38 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::CsColumn)->setToolTip("<b>CS</b>: This option determines if the highlight rule should be interpreted <b>case sensitive</b>.");
39 ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::CsColumn)->setWhatsThis("<b>CS</b>: This option determines if the highlight rule should be interpreted <b>case sensitive</b>.");
41 ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::NameColumn, QHeaderView::Stretch);
42 ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::RegExColumn, QHeaderView::ResizeToContents);
43 ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::CsColumn, QHeaderView::ResizeToContents);
44 ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::EnableColumn, QHeaderView::ResizeToContents);
46 connect(ui.add, SIGNAL(clicked(bool)), this, SLOT(addNewRow()));
47 connect(ui.remove, SIGNAL(clicked(bool)), this, SLOT(removeSelectedRows()));
48 //TODO: search for a better signal (one that emits everytime a selection has been changed for one item)
49 connect(ui.highlightTable, SIGNAL(itemClicked(QTableWidgetItem *)), this, SLOT(selectRow(QTableWidgetItem *)));
51 connect(ui.highlightAllNicks, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
52 connect(ui.highlightCurrentNick, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
53 connect(ui.highlightNoNick, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
54 connect(ui.add, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
55 connect(ui.remove, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
56 connect(ui.highlightTable, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(tableChanged(QTableWidgetItem *)));
59 bool HighlightSettingsPage::hasDefaults() const {
63 void HighlightSettingsPage::defaults() {
64 ui.highlightCurrentNick->setChecked(true);
70 void HighlightSettingsPage::addNewRow(QString name, bool regex, bool cs, bool enable) {
71 ui.highlightTable->setRowCount(ui.highlightTable->rowCount()+1);
73 QTableWidgetItem *nameItem = new QTableWidgetItem(name);
75 QTableWidgetItem *regexItem = new QTableWidgetItem("");
77 regexItem->setCheckState(Qt::Checked);
79 regexItem->setCheckState(Qt::Unchecked);
80 regexItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
82 QTableWidgetItem *csItem = new QTableWidgetItem("");
84 csItem->setCheckState(Qt::Checked);
86 csItem->setCheckState(Qt::Unchecked);
87 csItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
89 QTableWidgetItem *enableItem = new QTableWidgetItem("");
91 enableItem->setCheckState(Qt::Checked);
93 enableItem->setCheckState(Qt::Unchecked);
94 enableItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
96 int lastRow = ui.highlightTable->rowCount()-1;
97 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::NameColumn, nameItem);
98 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::RegExColumn, regexItem);
99 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::CsColumn, csItem);
100 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::EnableColumn, enableItem);
102 QVariantMap highlightRule;
103 highlightRule["name"] = name;
104 highlightRule["regex"] = regex;
105 highlightRule["cs"] = cs;
106 highlightRule["enable"] = enable;
108 highlightList.append(highlightRule);
111 void HighlightSettingsPage::removeSelectedRows() {
112 QList<int> selectedRows;
113 QList<QTableWidgetItem *> selectedItemList = ui.highlightTable->selectedItems();
114 foreach(QTableWidgetItem *selectedItem, selectedItemList) {
115 selectedRows.append(selectedItem->row());
117 qSort(selectedRows.begin(), selectedRows.end(), qGreater<int>());
119 foreach(int row, selectedRows) {
121 ui.highlightTable->removeRow(row);
122 highlightList.removeAt(row);
128 void HighlightSettingsPage::selectRow(QTableWidgetItem *item) {
129 int row = item->row();
130 bool selected = item->isSelected();
131 ui.highlightTable->setRangeSelected(QTableWidgetSelectionRange(row, 0, row, HighlightSettingsPage::ColumnCount-1), selected);
134 void HighlightSettingsPage::emptyTable() {
135 // ui.highlight and highlightList should have the same size, but just to make sure.
136 if(ui.highlightTable->rowCount() != highlightList.size()) {
137 qDebug() << "something is wrong: ui.highlight and highlightList don't have the same size!";
139 while(ui.highlightTable->rowCount()) {
140 ui.highlightTable->removeRow(0);
142 while(highlightList.size()) {
143 highlightList.removeLast();
147 void HighlightSettingsPage::tableChanged(QTableWidgetItem *item) {
148 if(item->row()+1 > highlightList.size())
151 QVariantMap highlightRule = highlightList.value(item->row()).toMap();
153 switch(item->column())
155 case HighlightSettingsPage::NameColumn:
156 if(item->text() == "")
157 item->setText(tr("this shouldn't be empty"));
158 highlightRule["name"] = item->text();
160 case HighlightSettingsPage::RegExColumn:
161 highlightRule["regex"] = (item->checkState() == Qt::Checked);
163 case HighlightSettingsPage::CsColumn:
164 highlightRule["cs"] = (item->checkState() == Qt::Checked);
166 case HighlightSettingsPage::EnableColumn:
167 highlightRule["enable"] = (item->checkState() == Qt::Checked);
170 highlightList[item->row()] = highlightRule;
171 emit widgetHasChanged();
174 void HighlightSettingsPage::load() {
175 NotificationSettings notificationSettings;
179 foreach(QVariant highlight, notificationSettings.highlightList()) {
180 QVariantMap highlightRule = highlight.toMap();
181 QString name = highlightRule["name"].toString();
182 bool regex = highlightRule["regex"].toBool();
183 bool cs = highlightRule["cs"].toBool();
184 bool enable = highlightRule["enable"].toBool();
186 addNewRow(name, regex, cs, enable);
189 switch(notificationSettings.highlightNick())
191 case NotificationSettings::NoNick:
192 ui.highlightNoNick->setChecked(true);
194 case NotificationSettings::CurrentNick:
195 ui.highlightCurrentNick->setChecked(true);
197 case NotificationSettings::AllNicks:
198 ui.highlightAllNicks->setChecked(true);
202 setChangedState(false);
205 void HighlightSettingsPage::save() {
206 NotificationSettings notificationSettings;
207 notificationSettings.setHighlightList(highlightList);
209 NotificationSettings::HighlightNickType highlightNickType;
210 if(ui.highlightNoNick->isChecked())
211 highlightNickType = NotificationSettings::NoNick;
212 if(ui.highlightCurrentNick->isChecked())
213 highlightNickType = NotificationSettings::CurrentNick;
214 if(ui.highlightAllNicks->isChecked())
215 highlightNickType = NotificationSettings::AllNicks;
217 notificationSettings.setHighlightNick(highlightNickType);
220 setChangedState(false);
223 void HighlightSettingsPage::widgetHasChanged() {
224 bool changed = testHasChanged();
225 if(changed != hasChanged()) setChangedState(changed);
228 bool HighlightSettingsPage::testHasChanged() {
229 NotificationSettings notificationSettings;
231 NotificationSettings::HighlightNickType highlightNickType;
232 if(ui.highlightNoNick->isChecked())
233 highlightNickType = NotificationSettings::NoNick;
234 if(ui.highlightCurrentNick->isChecked())
235 highlightNickType = NotificationSettings::CurrentNick;
236 if(ui.highlightAllNicks->isChecked())
237 highlightNickType = NotificationSettings::AllNicks;
239 if(notificationSettings.highlightNick() != highlightNickType) return true;
241 if(notificationSettings.highlightList() != highlightList) return true;