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 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("Appearance"), 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.nicksCaseSensitive, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
55 connect(ui.add, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
56 connect(ui.remove, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
57 connect(ui.highlightTable, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(tableChanged(QTableWidgetItem *)));
60 bool HighlightSettingsPage::hasDefaults() const {
64 void HighlightSettingsPage::defaults() {
65 ui.highlightCurrentNick->setChecked(true);
66 ui.nicksCaseSensitive->setChecked(false);
72 void HighlightSettingsPage::addNewRow(QString name, bool regex, bool cs, bool enable) {
73 ui.highlightTable->setRowCount(ui.highlightTable->rowCount()+1);
75 QTableWidgetItem *nameItem = new QTableWidgetItem(name);
77 QTableWidgetItem *regexItem = new QTableWidgetItem("");
79 regexItem->setCheckState(Qt::Checked);
81 regexItem->setCheckState(Qt::Unchecked);
82 regexItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
84 QTableWidgetItem *csItem = new QTableWidgetItem("");
86 csItem->setCheckState(Qt::Checked);
88 csItem->setCheckState(Qt::Unchecked);
89 csItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
91 QTableWidgetItem *enableItem = new QTableWidgetItem("");
93 enableItem->setCheckState(Qt::Checked);
95 enableItem->setCheckState(Qt::Unchecked);
96 enableItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
98 int lastRow = ui.highlightTable->rowCount()-1;
99 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::NameColumn, nameItem);
100 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::RegExColumn, regexItem);
101 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::CsColumn, csItem);
102 ui.highlightTable->setItem(lastRow, HighlightSettingsPage::EnableColumn, enableItem);
104 QVariantMap highlightRule;
105 highlightRule["Name"] = name;
106 highlightRule["RegEx"] = regex;
107 highlightRule["CS"] = cs;
108 highlightRule["Enable"] = enable;
110 highlightList.append(highlightRule);
113 void HighlightSettingsPage::removeSelectedRows() {
114 QList<int> selectedRows;
115 QList<QTableWidgetItem *> selectedItemList = ui.highlightTable->selectedItems();
116 foreach(QTableWidgetItem *selectedItem, selectedItemList) {
117 selectedRows.append(selectedItem->row());
119 qSort(selectedRows.begin(), selectedRows.end(), qGreater<int>());
121 foreach(int row, selectedRows) {
123 ui.highlightTable->removeRow(row);
124 highlightList.removeAt(row);
130 void HighlightSettingsPage::selectRow(QTableWidgetItem *item) {
131 int row = item->row();
132 bool selected = item->isSelected();
133 ui.highlightTable->setRangeSelected(QTableWidgetSelectionRange(row, 0, row, HighlightSettingsPage::ColumnCount-1), selected);
136 void HighlightSettingsPage::emptyTable() {
137 // ui.highlight and highlightList should have the same size, but just to make sure.
138 if(ui.highlightTable->rowCount() != highlightList.size()) {
139 qDebug() << "something is wrong: ui.highlight and highlightList don't have the same size!";
141 while(ui.highlightTable->rowCount()) {
142 ui.highlightTable->removeRow(0);
144 while(highlightList.size()) {
145 highlightList.removeLast();
149 void HighlightSettingsPage::tableChanged(QTableWidgetItem *item) {
150 if(item->row()+1 > highlightList.size())
153 QVariantMap highlightRule = highlightList.value(item->row()).toMap();
155 switch(item->column())
157 case HighlightSettingsPage::NameColumn:
158 if(item->text() == "")
159 item->setText(tr("this shouldn't be empty"));
160 highlightRule["Name"] = item->text();
162 case HighlightSettingsPage::RegExColumn:
163 highlightRule["RegEx"] = (item->checkState() == Qt::Checked);
165 case HighlightSettingsPage::CsColumn:
166 highlightRule["CS"] = (item->checkState() == Qt::Checked);
168 case HighlightSettingsPage::EnableColumn:
169 highlightRule["Enable"] = (item->checkState() == Qt::Checked);
172 highlightList[item->row()] = highlightRule;
173 emit widgetHasChanged();
176 void HighlightSettingsPage::load() {
177 NotificationSettings notificationSettings;
181 foreach(QVariant highlight, notificationSettings.highlightList()) {
182 QVariantMap highlightRule = highlight.toMap();
183 QString name = highlightRule["Name"].toString();
184 bool regex = highlightRule["RegEx"].toBool();
185 bool cs = highlightRule["CS"].toBool();
186 bool enable = highlightRule["Enable"].toBool();
188 addNewRow(name, regex, cs, enable);
191 switch(notificationSettings.highlightNick())
193 case NotificationSettings::NoNick:
194 ui.highlightNoNick->setChecked(true);
196 case NotificationSettings::CurrentNick:
197 ui.highlightCurrentNick->setChecked(true);
199 case NotificationSettings::AllNicks:
200 ui.highlightAllNicks->setChecked(true);
203 ui.nicksCaseSensitive->setChecked(notificationSettings.nicksCaseSensitive());
205 setChangedState(false);
208 void HighlightSettingsPage::save() {
209 NotificationSettings notificationSettings;
210 notificationSettings.setHighlightList(highlightList);
212 NotificationSettings::HighlightNickType highlightNickType;
213 if(ui.highlightNoNick->isChecked())
214 highlightNickType = NotificationSettings::NoNick;
215 if(ui.highlightCurrentNick->isChecked())
216 highlightNickType = NotificationSettings::CurrentNick;
217 if(ui.highlightAllNicks->isChecked())
218 highlightNickType = NotificationSettings::AllNicks;
220 notificationSettings.setHighlightNick(highlightNickType);
221 notificationSettings.setNicksCaseSensitive(ui.nicksCaseSensitive->isChecked());
224 setChangedState(false);
227 void HighlightSettingsPage::widgetHasChanged() {
228 bool changed = testHasChanged();
229 if(changed != hasChanged()) setChangedState(changed);
232 bool HighlightSettingsPage::testHasChanged() {
233 NotificationSettings notificationSettings;
235 NotificationSettings::HighlightNickType highlightNickType;
236 if(ui.highlightNoNick->isChecked())
237 highlightNickType = NotificationSettings::NoNick;
238 if(ui.highlightCurrentNick->isChecked())
239 highlightNickType = NotificationSettings::CurrentNick;
240 if(ui.highlightAllNicks->isChecked())
241 highlightNickType = NotificationSettings::AllNicks;
243 if(notificationSettings.highlightNick() != highlightNickType) return true;
244 if(notificationSettings.nicksCaseSensitive() != ui.nicksCaseSensitive->isChecked()) return true;
246 if(notificationSettings.highlightList() != highlightList) return true;