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