09887ba7a3de53297891340b06340c0458d1b73b
[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   ui.highlightTable->setColumnWidth( 0, 50 );
35   ui.highlightTable->setColumnWidth( 2, 50 );
36   ui.highlightTable->horizontalHeader()->setResizeMode(0, QHeaderView::Fixed); 
37   ui.highlightTable->horizontalHeader()->setResizeMode(1, QHeaderView::Stretch);
38   ui.highlightTable->horizontalHeader()->setResizeMode(2, QHeaderView::Fixed); 
39
40   connect(ui.add, SIGNAL(clicked(bool)), this, SLOT(addNewRow()));
41   connect(ui.remove, SIGNAL(clicked(bool)), this, SLOT(removeSelectedRows()));
42   //TODO: search for a better signal (one that emits everytime a selection has been changed for one item)
43   connect(ui.highlightTable, SIGNAL(itemClicked(QTableWidgetItem *)), this, SLOT(selectRow(QTableWidgetItem *)));
44
45   connect(ui.highlightCurrentNick, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
46   connect(ui.add, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
47   connect(ui.remove, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
48   connect(ui.highlightTable, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(tableChanged(QTableWidgetItem *)));
49 }
50
51 bool HighlightSettingsPage::hasDefaults() const {
52   return true;
53 }
54
55 void HighlightSettingsPage::defaults() {
56   ui.highlightCurrentNick->setChecked(true);
57   emptyTable();
58
59   widgetHasChanged();
60 }
61
62 void HighlightSettingsPage::addNewRow(bool regex, QString name, bool enable) {
63   ui.highlightTable->setRowCount(ui.highlightTable->rowCount()+1);
64   QTableWidgetItem *regexItem = new QTableWidgetItem("");
65   if(regex)
66     regexItem->setCheckState(Qt::Checked);
67   else
68     regexItem->setCheckState(Qt::Unchecked);
69   regexItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
70   QTableWidgetItem *nameItem = new QTableWidgetItem(name);
71   QTableWidgetItem *enableItem = new QTableWidgetItem("");
72   if(enable)
73     enableItem->setCheckState(Qt::Checked);
74   else
75     enableItem->setCheckState(Qt::Unchecked);
76   enableItem->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
77
78   int lastRow = ui.highlightTable->rowCount()-1;
79   ui.highlightTable->setItem(lastRow, 0, regexItem);
80   ui.highlightTable->setItem(lastRow, 1, nameItem);
81   ui.highlightTable->setItem(lastRow, 2, enableItem);
82
83   QVariantMap highlightRule;
84   highlightRule["regex"] = regex;
85   highlightRule["name"] = name;
86   highlightRule["enable"] = enable;
87
88   highlightList.append(highlightRule);
89 }
90
91 void HighlightSettingsPage::removeSelectedRows() {
92   QList<int> selectedRows;
93   QList<QTableWidgetItem *> selectedItemList = ui.highlightTable->selectedItems();
94   foreach(QTableWidgetItem *selectedItem, selectedItemList) {
95     selectedRows.append(selectedItem->row());
96   }
97   qSort(selectedRows.begin(), selectedRows.end(), qGreater<int>());
98   int lastRow = -1;
99   foreach(int row, selectedRows) {
100     if(row != lastRow) {
101       ui.highlightTable->removeRow(row);
102       highlightList.removeAt(row);
103     }
104     lastRow = row;
105   }
106 }
107
108 void HighlightSettingsPage::selectRow(QTableWidgetItem *item) {
109   int row = item->row();
110   bool selected = item->isSelected();
111   ui.highlightTable->setRangeSelected(QTableWidgetSelectionRange(row, 0, row, 2), selected);
112 }
113
114 void HighlightSettingsPage::emptyTable() {
115   // ui.highlight and highlightList should have the same size, but just to make sure.
116   if(ui.highlightTable->rowCount() != highlightList.size()) {
117     qDebug() << "something is wrong: ui.highlight and highlightList don't have the same size!";
118   }
119   while(ui.highlightTable->rowCount()) {
120     ui.highlightTable->removeRow(0);
121   }
122   while(highlightList.size()) {
123         highlightList.removeLast();
124   }
125 }
126
127 void HighlightSettingsPage::tableChanged(QTableWidgetItem *item) {
128   if(item->row()+1 > highlightList.size()) 
129     return;
130
131   QVariantMap highlightRule = highlightList.value(item->row()).toMap();
132
133   switch(item->column())
134   {
135     case 0:
136       highlightRule["regex"] = (item->checkState() == Qt::Checked);
137       break;
138     case 1:
139       if(item->text() == "") 
140         item->setText(tr("this shouldn't be empty"));
141       highlightRule["name"] = item->text();
142       break;
143     case 2:
144       highlightRule["enable"] = (item->checkState() == Qt::Checked);
145       break;
146   }
147   highlightList[item->row()] = highlightRule;
148   emit widgetHasChanged();
149 }
150
151 void HighlightSettingsPage::load() {
152   NotificationSettings notificationSettings;
153
154   emptyTable();
155
156   foreach(QVariant highlight, notificationSettings.highlightList()) {
157     QVariantMap highlightRule = highlight.toMap();
158     bool regex = highlightRule["regex"].toBool();
159     QString name = highlightRule["name"].toString();
160     bool enable = highlightRule["enable"].toBool();
161
162     addNewRow(regex, name, enable);
163   }
164
165   ui.highlightCurrentNick->setChecked(notificationSettings.highlightCurrentNick());
166
167   setChangedState(false);
168 }
169
170 void HighlightSettingsPage::save() {
171   NotificationSettings notificationSettings;
172   notificationSettings.setHighlightList(highlightList);
173   notificationSettings.setHighlightCurrentNick(ui.highlightCurrentNick->isChecked());
174
175   load();
176   setChangedState(false);
177 }
178
179 void HighlightSettingsPage::widgetHasChanged() {
180   bool changed = testHasChanged();
181   if(changed != hasChanged()) setChangedState(changed);
182 }
183
184 bool HighlightSettingsPage::testHasChanged() {
185   NotificationSettings notificationSettings;
186
187   if(notificationSettings.highlightCurrentNick() != ui.highlightCurrentNick->isChecked()) return true;
188   if(notificationSettings.highlightList() != highlightList) return true;
189
190   return true;
191 }
192
193
194
195