Some tweaking of NotificationsSettingsPage
[quassel.git] / src / qtui / settingspages / highlightsettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
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.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 *)));
57 }
58
59 bool HighlightSettingsPage::hasDefaults() const {
60   return true;
61 }
62
63 void HighlightSettingsPage::defaults() {
64   ui.highlightCurrentNick->setChecked(true);
65   emptyTable();
66
67   widgetHasChanged();
68 }
69
70 void HighlightSettingsPage::addNewRow(QString name, bool regex, bool cs, bool enable) {
71   ui.highlightTable->setRowCount(ui.highlightTable->rowCount()+1);
72
73   QTableWidgetItem *nameItem = new QTableWidgetItem(name);
74
75   QTableWidgetItem *regexItem = new QTableWidgetItem("");
76   if(regex)
77     regexItem->setCheckState(Qt::Checked);
78   else
79     regexItem->setCheckState(Qt::Unchecked);
80   regexItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
81
82   QTableWidgetItem *csItem = new QTableWidgetItem("");
83   if(cs)
84     csItem->setCheckState(Qt::Checked);
85   else
86     csItem->setCheckState(Qt::Unchecked);
87   csItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
88
89   QTableWidgetItem *enableItem = new QTableWidgetItem("");
90   if(enable)
91     enableItem->setCheckState(Qt::Checked);
92   else
93     enableItem->setCheckState(Qt::Unchecked);
94   enableItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
95
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);
101
102   QVariantMap highlightRule;
103   highlightRule["name"] = name;
104   highlightRule["regex"] = regex;
105   highlightRule["cs"] = cs;
106   highlightRule["enable"] = enable;
107
108   highlightList.append(highlightRule);
109 }
110
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());
116   }
117   qSort(selectedRows.begin(), selectedRows.end(), qGreater<int>());
118   int lastRow = -1;
119   foreach(int row, selectedRows) {
120     if(row != lastRow) {
121       ui.highlightTable->removeRow(row);
122       highlightList.removeAt(row);
123     }
124     lastRow = row;
125   }
126 }
127
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);
132 }
133
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!";
138   }
139   while(ui.highlightTable->rowCount()) {
140     ui.highlightTable->removeRow(0);
141   }
142   while(highlightList.size()) {
143         highlightList.removeLast();
144   }
145 }
146
147 void HighlightSettingsPage::tableChanged(QTableWidgetItem *item) {
148   if(item->row()+1 > highlightList.size()) 
149     return;
150
151   QVariantMap highlightRule = highlightList.value(item->row()).toMap();
152
153   switch(item->column())
154   {
155     case HighlightSettingsPage::NameColumn:
156       if(item->text() == "")
157         item->setText(tr("this shouldn't be empty"));
158       highlightRule["name"] = item->text();
159       break;
160     case HighlightSettingsPage::RegExColumn:
161       highlightRule["regex"] = (item->checkState() == Qt::Checked);
162       break;
163     case HighlightSettingsPage::CsColumn:
164       highlightRule["cs"] = (item->checkState() == Qt::Checked);
165       break;
166     case HighlightSettingsPage::EnableColumn:
167       highlightRule["enable"] = (item->checkState() == Qt::Checked);
168       break;
169   }
170   highlightList[item->row()] = highlightRule;
171   emit widgetHasChanged();
172 }
173
174 void HighlightSettingsPage::load() {
175   NotificationSettings notificationSettings;
176
177   emptyTable();
178
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();
185
186     addNewRow(name, regex, cs, enable);
187   }
188
189   switch(notificationSettings.highlightNick())
190   {
191     case NotificationSettings::NoNick:
192       ui.highlightNoNick->setChecked(true);
193       break;
194     case NotificationSettings::CurrentNick:
195       ui.highlightCurrentNick->setChecked(true);
196       break;
197     case NotificationSettings::AllNicks:
198       ui.highlightAllNicks->setChecked(true);
199       break;
200   }
201
202   setChangedState(false);
203 }
204
205 void HighlightSettingsPage::save() {
206   NotificationSettings notificationSettings;
207   notificationSettings.setHighlightList(highlightList);
208
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;
216
217   notificationSettings.setHighlightNick(highlightNickType);
218
219   load();
220   setChangedState(false);
221 }
222
223 void HighlightSettingsPage::widgetHasChanged() {
224   bool changed = testHasChanged();
225   if(changed != hasChanged()) setChangedState(changed);
226 }
227
228 bool HighlightSettingsPage::testHasChanged() {
229   NotificationSettings notificationSettings;
230
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;
238
239   if(notificationSettings.highlightNick() != highlightNickType) return true;
240
241   if(notificationSettings.highlightList() != highlightList) return true;
242
243   return true;
244 }
245
246
247
248