Rename settingspages.inc to settingspages.cmake
[quassel.git] / src / qtui / settingspages / highlightsettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2013 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 General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
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 General Public License for more details.                          *
14  *                                                                         *
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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "highlightsettingspage.h"
22
23 #include "qtui.h"
24 #include "uisettings.h"
25
26 #include <QHeaderView>
27
28 HighlightSettingsPage::HighlightSettingsPage(QWidget *parent)
29     : SettingsPage(tr("Interface"), tr("Highlight"), parent)
30 {
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->horizontalHeaderItem(HighlightSettingsPage::ChanColumn)->setToolTip("<b>Channel</b>: This regular expression determines for which <b>channels</b> the highlight rule works. Leave blank to match any channel. Put <b>!</b> in the beginning to negate. Case insensitive.");
42     ui.highlightTable->horizontalHeaderItem(HighlightSettingsPage::ChanColumn)->setWhatsThis("<b>Channel</b>: This regular expression determines for which <b>channels</b> the highlight rule works. Leave blank to match any channel. Put <b>!</b> in the beginning to negate. Case insensitive.");
43
44     ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::NameColumn, QHeaderView::Stretch);
45     ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::RegExColumn, QHeaderView::ResizeToContents);
46     ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::CsColumn, QHeaderView::ResizeToContents);
47     ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::EnableColumn, QHeaderView::ResizeToContents);
48     ui.highlightTable->horizontalHeader()->setResizeMode(HighlightSettingsPage::ChanColumn, QHeaderView::ResizeToContents);
49
50     connect(ui.add, SIGNAL(clicked(bool)), this, SLOT(addNewRow()));
51     connect(ui.remove, SIGNAL(clicked(bool)), this, SLOT(removeSelectedRows()));
52     //TODO: search for a better signal (one that emits everytime a selection has been changed for one item)
53     connect(ui.highlightTable, SIGNAL(itemClicked(QTableWidgetItem *)), this, SLOT(selectRow(QTableWidgetItem *)));
54
55     connect(ui.highlightAllNicks, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
56     connect(ui.highlightCurrentNick, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
57     connect(ui.highlightNoNick, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
58     connect(ui.nicksCaseSensitive, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
59     connect(ui.add, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
60     connect(ui.remove, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
61     connect(ui.highlightTable, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(tableChanged(QTableWidgetItem *)));
62 }
63
64
65 bool HighlightSettingsPage::hasDefaults() const
66 {
67     return true;
68 }
69
70
71 void HighlightSettingsPage::defaults()
72 {
73     ui.highlightCurrentNick->setChecked(true);
74     ui.nicksCaseSensitive->setChecked(false);
75     emptyTable();
76
77     widgetHasChanged();
78 }
79
80
81 void HighlightSettingsPage::addNewRow(QString name, bool regex, bool cs, bool enable, QString chanName, bool self)
82 {
83     ui.highlightTable->setRowCount(ui.highlightTable->rowCount()+1);
84
85     QTableWidgetItem *nameItem = new QTableWidgetItem(name);
86
87     QTableWidgetItem *regexItem = new QTableWidgetItem("");
88     if (regex)
89         regexItem->setCheckState(Qt::Checked);
90     else
91         regexItem->setCheckState(Qt::Unchecked);
92     regexItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
93
94     QTableWidgetItem *csItem = new QTableWidgetItem("");
95     if (cs)
96         csItem->setCheckState(Qt::Checked);
97     else
98         csItem->setCheckState(Qt::Unchecked);
99     csItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
100
101     QTableWidgetItem *enableItem = new QTableWidgetItem("");
102     if (enable)
103         enableItem->setCheckState(Qt::Checked);
104     else
105         enableItem->setCheckState(Qt::Unchecked);
106     enableItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
107
108     QTableWidgetItem *chanNameItem = new QTableWidgetItem(chanName);
109
110     int lastRow = ui.highlightTable->rowCount()-1;
111     ui.highlightTable->setItem(lastRow, HighlightSettingsPage::NameColumn, nameItem);
112     ui.highlightTable->setItem(lastRow, HighlightSettingsPage::RegExColumn, regexItem);
113     ui.highlightTable->setItem(lastRow, HighlightSettingsPage::CsColumn, csItem);
114     ui.highlightTable->setItem(lastRow, HighlightSettingsPage::EnableColumn, enableItem);
115     ui.highlightTable->setItem(lastRow, HighlightSettingsPage::ChanColumn, chanNameItem);
116
117     if (!self)
118         ui.highlightTable->setCurrentItem(nameItem);
119
120     QVariantMap highlightRule;
121     highlightRule["Name"] = name;
122     highlightRule["RegEx"] = regex;
123     highlightRule["CS"] = cs;
124     highlightRule["Enable"] = enable;
125     highlightRule["Channel"] = chanName;
126
127     highlightList.append(highlightRule);
128 }
129
130
131 void HighlightSettingsPage::removeSelectedRows()
132 {
133     QList<int> selectedRows;
134     QList<QTableWidgetItem *> selectedItemList = ui.highlightTable->selectedItems();
135     foreach(QTableWidgetItem *selectedItem, selectedItemList) {
136         selectedRows.append(selectedItem->row());
137     }
138     qSort(selectedRows.begin(), selectedRows.end(), qGreater<int>());
139     int lastRow = -1;
140     foreach(int row, selectedRows) {
141         if (row != lastRow) {
142             ui.highlightTable->removeRow(row);
143             highlightList.removeAt(row);
144         }
145         lastRow = row;
146     }
147 }
148
149
150 void HighlightSettingsPage::selectRow(QTableWidgetItem *item)
151 {
152     int row = item->row();
153     bool selected = item->isSelected();
154     ui.highlightTable->setRangeSelected(QTableWidgetSelectionRange(row, 0, row, HighlightSettingsPage::ColumnCount-1), selected);
155 }
156
157
158 void HighlightSettingsPage::emptyTable()
159 {
160     // ui.highlight and highlightList should have the same size, but just to make sure.
161     if (ui.highlightTable->rowCount() != highlightList.size()) {
162         qDebug() << "something is wrong: ui.highlight and highlightList don't have the same size!";
163     }
164     while (ui.highlightTable->rowCount()) {
165         ui.highlightTable->removeRow(0);
166     }
167     while (highlightList.size()) {
168         highlightList.removeLast();
169     }
170 }
171
172
173 void HighlightSettingsPage::tableChanged(QTableWidgetItem *item)
174 {
175     if (item->row()+1 > highlightList.size())
176         return;
177
178     QVariantMap highlightRule = highlightList.value(item->row()).toMap();
179
180     switch (item->column())
181     {
182     case HighlightSettingsPage::NameColumn:
183         if (item->text() == "")
184             item->setText(tr("this shouldn't be empty"));
185         highlightRule["Name"] = item->text();
186         break;
187     case HighlightSettingsPage::RegExColumn:
188         highlightRule["RegEx"] = (item->checkState() == Qt::Checked);
189         break;
190     case HighlightSettingsPage::CsColumn:
191         highlightRule["CS"] = (item->checkState() == Qt::Checked);
192         break;
193     case HighlightSettingsPage::EnableColumn:
194         highlightRule["Enable"] = (item->checkState() == Qt::Checked);
195         break;
196     case HighlightSettingsPage::ChanColumn:
197         if (!item->text().isEmpty() && item->text().trimmed().isEmpty())
198             item->setText("");
199         highlightRule["Channel"] = item->text();
200         break;
201     }
202     highlightList[item->row()] = highlightRule;
203     emit widgetHasChanged();
204 }
205
206
207 void HighlightSettingsPage::load()
208 {
209     NotificationSettings notificationSettings;
210
211     emptyTable();
212
213     foreach(QVariant highlight, notificationSettings.highlightList()) {
214         QVariantMap highlightRule = highlight.toMap();
215         QString name = highlightRule["Name"].toString();
216         bool regex = highlightRule["RegEx"].toBool();
217         bool cs = highlightRule["CS"].toBool();
218         bool enable = highlightRule["Enable"].toBool();
219         QString chanName = highlightRule["Channel"].toString();
220
221         addNewRow(name, regex, cs, enable, chanName, true);
222     }
223
224     switch (notificationSettings.highlightNick())
225     {
226     case NotificationSettings::NoNick:
227         ui.highlightNoNick->setChecked(true);
228         break;
229     case NotificationSettings::CurrentNick:
230         ui.highlightCurrentNick->setChecked(true);
231         break;
232     case NotificationSettings::AllNicks:
233         ui.highlightAllNicks->setChecked(true);
234         break;
235     }
236     ui.nicksCaseSensitive->setChecked(notificationSettings.nicksCaseSensitive());
237
238     setChangedState(false);
239 }
240
241
242 void HighlightSettingsPage::save()
243 {
244     NotificationSettings notificationSettings;
245     notificationSettings.setHighlightList(highlightList);
246
247     NotificationSettings::HighlightNickType highlightNickType;
248     if (ui.highlightNoNick->isChecked())
249         highlightNickType = NotificationSettings::NoNick;
250     if (ui.highlightCurrentNick->isChecked())
251         highlightNickType = NotificationSettings::CurrentNick;
252     if (ui.highlightAllNicks->isChecked())
253         highlightNickType = NotificationSettings::AllNicks;
254
255     notificationSettings.setHighlightNick(highlightNickType);
256     notificationSettings.setNicksCaseSensitive(ui.nicksCaseSensitive->isChecked());
257
258     load();
259     setChangedState(false);
260 }
261
262
263 void HighlightSettingsPage::widgetHasChanged()
264 {
265     bool changed = testHasChanged();
266     if (changed != hasChanged()) setChangedState(changed);
267 }
268
269
270 bool HighlightSettingsPage::testHasChanged()
271 {
272     NotificationSettings notificationSettings;
273
274     NotificationSettings::HighlightNickType highlightNickType;
275     if (ui.highlightNoNick->isChecked())
276         highlightNickType = NotificationSettings::NoNick;
277     if (ui.highlightCurrentNick->isChecked())
278         highlightNickType = NotificationSettings::CurrentNick;
279     if (ui.highlightAllNicks->isChecked())
280         highlightNickType = NotificationSettings::AllNicks;
281
282     if (notificationSettings.highlightNick() != highlightNickType) return true;
283     if (notificationSettings.nicksCaseSensitive() != ui.nicksCaseSensitive->isChecked()) return true;
284
285     if (notificationSettings.highlightList() != highlightList) return true;
286
287     return false;
288 }