Quassel IRC  Pre-Release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
highlightsettingspage.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2015 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 
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 
74 {
75  return true;
76 }
77 
78 
80 {
81  ui.highlightCurrentNick->setChecked(true);
82  ui.nicksCaseSensitive->setChecked(false);
83  emptyTable();
84 
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 
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 
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  {
191  if (item->text() == "")
192  item->setText(tr("this shouldn't be empty"));
193  highlightRule["Name"] = item->text();
194  break;
196  highlightRule["RegEx"] = (item->checkState() == Qt::Checked);
197  break;
199  highlightRule["CS"] = (item->checkState() == Qt::Checked);
200  break;
202  highlightRule["Enable"] = (item->checkState() == Qt::Checked);
203  break;
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 
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  {
235  ui.highlightNoNick->setChecked(true);
236  break;
238  ui.highlightCurrentNick->setChecked(true);
239  break;
241  ui.highlightAllNicks->setChecked(true);
242  break;
243  }
244  ui.nicksCaseSensitive->setChecked(notificationSettings.nicksCaseSensitive());
245 
246  setChangedState(false);
247 }
248 
249 
251 {
252  NotificationSettings notificationSettings;
253  notificationSettings.setHighlightList(highlightList);
254 
256  if (ui.highlightCurrentNick->isChecked())
257  highlightNickType = NotificationSettings::CurrentNick;
258  if (ui.highlightAllNicks->isChecked())
259  highlightNickType = NotificationSettings::AllNicks;
260 
261  notificationSettings.setHighlightNick(highlightNickType);
262  notificationSettings.setNicksCaseSensitive(ui.nicksCaseSensitive->isChecked());
263 
264  load();
265  setChangedState(false);
266 }
267 
268 
270 {
271  bool changed = testHasChanged();
272  if (changed != hasChanged()) setChangedState(changed);
273 }
274 
275 
277 {
278  NotificationSettings notificationSettings;
279 
281  if (ui.highlightCurrentNick->isChecked())
282  highlightNickType = NotificationSettings::CurrentNick;
283  if (ui.highlightAllNicks->isChecked())
284  highlightNickType = NotificationSettings::AllNicks;
285 
286  if (notificationSettings.highlightNick() != highlightNickType)
287  return true;
288  if (notificationSettings.nicksCaseSensitive() != ui.nicksCaseSensitive->isChecked())
289  return true;
290  if (notificationSettings.highlightList() != highlightList)
291  return true;
292 
293  return false;
294 }