typos
[quassel.git] / src / qtui / settingspages / highlightsettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "highlightsettingspage.h"
22
23 #include "qtui.h"
24 #include "uisettings.h"
25
26 #include <QHeaderView>
27
28
29 HighlightSettingsPage::HighlightSettingsPage(QWidget *parent)
30   : SettingsPage(tr("Behaviour"), tr("Highlight"), parent) {
31   ui.setupUi(this);
32   ui.highlightTable->verticalHeader()->hide();
33   ui.highlightTable->setShowGrid(false);
34
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.");
37
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>.");
40
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);
45
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 *)));
50
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 *)));
58 }
59
60 bool HighlightSettingsPage::hasDefaults() const {
61   return true;
62 }
63
64 void HighlightSettingsPage::defaults() {
65   ui.highlightCurrentNick->setChecked(true);
66   ui.nicksCaseSensitive->setChecked(false);
67   emptyTable();
68
69   widgetHasChanged();
70 }
71
72 void HighlightSettingsPage::addNewRow(QString name, bool regex, bool cs, bool enable) {
73   ui.highlightTable->setRowCount(ui.highlightTable->rowCount()+1);
74
75   QTableWidgetItem *nameItem = new QTableWidgetItem(name);
76
77   QTableWidgetItem *regexItem = new QTableWidgetItem("");
78   if(regex)
79     regexItem->setCheckState(Qt::Checked);
80   else
81     regexItem->setCheckState(Qt::Unchecked);
82   regexItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
83
84   QTableWidgetItem *csItem = new QTableWidgetItem("");
85   if(cs)
86     csItem->setCheckState(Qt::Checked);
87   else
88     csItem->setCheckState(Qt::Unchecked);
89   csItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
90
91   QTableWidgetItem *enableItem = new QTableWidgetItem("");
92   if(enable)
93     enableItem->setCheckState(Qt::Checked);
94   else
95     enableItem->setCheckState(Qt::Unchecked);
96   enableItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
97
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);
103
104   QVariantMap highlightRule;
105   highlightRule["Name"] = name;
106   highlightRule["RegEx"] = regex;
107   highlightRule["CS"] = cs;
108   highlightRule["Enable"] = enable;
109
110   highlightList.append(highlightRule);
111 }
112
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());
118   }
119   qSort(selectedRows.begin(), selectedRows.end(), qGreater<int>());
120   int lastRow = -1;
121   foreach(int row, selectedRows) {
122     if(row != lastRow) {
123       ui.highlightTable->removeRow(row);
124       highlightList.removeAt(row);
125     }
126     lastRow = row;
127   }
128 }
129
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);
134 }
135
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!";
140   }
141   while(ui.highlightTable->rowCount()) {
142     ui.highlightTable->removeRow(0);
143   }
144   while(highlightList.size()) {
145         highlightList.removeLast();
146   }
147 }
148
149 void HighlightSettingsPage::tableChanged(QTableWidgetItem *item) {
150   if(item->row()+1 > highlightList.size())
151     return;
152
153   QVariantMap highlightRule = highlightList.value(item->row()).toMap();
154
155   switch(item->column())
156   {
157     case HighlightSettingsPage::NameColumn:
158       if(item->text() == "")
159         item->setText(tr("this shouldn't be empty"));
160       highlightRule["Name"] = item->text();
161       break;
162     case HighlightSettingsPage::RegExColumn:
163       highlightRule["RegEx"] = (item->checkState() == Qt::Checked);
164       break;
165     case HighlightSettingsPage::CsColumn:
166       highlightRule["CS"] = (item->checkState() == Qt::Checked);
167       break;
168     case HighlightSettingsPage::EnableColumn:
169       highlightRule["Enable"] = (item->checkState() == Qt::Checked);
170       break;
171   }
172   highlightList[item->row()] = highlightRule;
173   emit widgetHasChanged();
174 }
175
176 void HighlightSettingsPage::load() {
177   NotificationSettings notificationSettings;
178
179   emptyTable();
180
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();
187
188     addNewRow(name, regex, cs, enable);
189   }
190
191   switch(notificationSettings.highlightNick())
192   {
193     case NotificationSettings::NoNick:
194       ui.highlightNoNick->setChecked(true);
195       break;
196     case NotificationSettings::CurrentNick:
197       ui.highlightCurrentNick->setChecked(true);
198       break;
199     case NotificationSettings::AllNicks:
200       ui.highlightAllNicks->setChecked(true);
201       break;
202   }
203   ui.nicksCaseSensitive->setChecked(notificationSettings.nicksCaseSensitive());
204
205   setChangedState(false);
206 }
207
208 void HighlightSettingsPage::save() {
209   NotificationSettings notificationSettings;
210   notificationSettings.setHighlightList(highlightList);
211
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;
219
220   notificationSettings.setHighlightNick(highlightNickType);
221   notificationSettings.setNicksCaseSensitive(ui.nicksCaseSensitive->isChecked());
222
223   load();
224   setChangedState(false);
225 }
226
227 void HighlightSettingsPage::widgetHasChanged() {
228   bool changed = testHasChanged();
229   if(changed != hasChanged()) setChangedState(changed);
230 }
231
232 bool HighlightSettingsPage::testHasChanged() {
233   NotificationSettings notificationSettings;
234
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;
242
243   if(notificationSettings.highlightNick() != highlightNickType) return true;
244   if(notificationSettings.nicksCaseSensitive() != ui.nicksCaseSensitive->isChecked()) return true;
245
246   if(notificationSettings.highlightList() != highlightList) return true;
247
248   return false;
249 }