1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/*
* Copyright (c) 2012 Maciej Niedzielski
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include <boost/algorithm/string.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <QStringList>
#include <QColor>
#include <Swift/Controllers/HighlightManager.h>
#include <Swift/QtUI/QtHighlightRulesItemModel.h>
#include <Swift/QtUI/QtSwiftUtil.h>
namespace Swift {
QtHighlightRulesItemModel::QtHighlightRulesItemModel(QObject* parent) : QAbstractItemModel(parent), highlightManager_(NULL)
{
}
void QtHighlightRulesItemModel::setHighlightManager(HighlightManager* highlightManager)
{
emit layoutAboutToBeChanged();
highlightManager_ = highlightManager;
emit layoutChanged();
}
QVariant QtHighlightRulesItemModel::headerData(int section, Qt::Orientation /* orientation */, int role) const
{
if (role == Qt::DisplayRole) {
switch (section) {
case ApplyTo: return QVariant(tr("Apply to"));
case Sender: return QVariant(tr("Sender"));
case Keyword: return QVariant(tr("Keyword"));
case Action: return QVariant(tr("Action"));
case NickIsKeyword: return QVariant(tr("Nick Is Keyword"));
case MatchCase: return QVariant(tr("Match Case"));
case MatchWholeWords: return QVariant(tr("Match Whole Words"));
case HighlightText: return QVariant(tr("Highlight Text"));
case TextColor: return QVariant(tr("Text Color"));
case TextBackground: return QVariant(tr("Text Background"));
case PlaySound: return QVariant(tr("Play Sounds"));
case SoundFile: return QVariant(tr("Sound File"));
}
}
return QVariant();
}
int QtHighlightRulesItemModel::columnCount(const QModelIndex& /* parent */) const
{
return NumberOfColumns;
}
QVariant QtHighlightRulesItemModel::data(const QModelIndex &index, int role) const
{
if (index.isValid() && highlightManager_ && (role == Qt::DisplayRole || role == Qt::EditRole)) {
const char* separator = (role == Qt::DisplayRole) ? " ; " : "\n";
if (boost::numeric_cast<std::vector<std::string>::size_type>(index.row()) < highlightManager_->getRules().size()) {
const HighlightRule& r = highlightManager_->getRules()[index.row()];
switch (index.column()) {
case ApplyTo: {
int applyTo = 0;
if (r.getMatchChat() && r.getMatchMUC()) {
applyTo = 1;
} else if (r.getMatchChat()) {
applyTo = 2;
} else if (r.getMatchMUC()) {
applyTo = 3;
}
if (role == Qt::DisplayRole) {
return QVariant(getApplyToString(applyTo));
} else {
return QVariant(applyTo);
}
}
case Sender: {
std::string s = boost::join(r.getSenders(), separator);
return QVariant(P2QSTRING(s));
}
case Keyword: {
std::string s = boost::join(r.getKeywords(), separator);
QString qs(P2QSTRING(s));
if (role == Qt::DisplayRole && r.getNickIsKeyword()) {
qs = tr("<nick>") + (qs.isEmpty() ? "" : separator + qs);
}
return QVariant(qs);
}
case Action: {
std::vector<std::string> v;
const HighlightAction & action = r.getAction();
if (action.highlightText()) {
v.push_back(Q2PSTRING(tr("Highlight text")));
}
if (action.playSound()) {
v.push_back(Q2PSTRING(tr("Play sound")));
}
std::string s = boost::join(v, separator);
return QVariant(P2QSTRING(s));
}
case NickIsKeyword: {
return QVariant(r.getNickIsKeyword());
}
case MatchCase: {
return QVariant(r.getMatchCase());
}
case MatchWholeWords: {
return QVariant(r.getMatchWholeWords());
}
case HighlightText: {
return QVariant(r.getAction().highlightText());
}
case TextColor: {
return QVariant(QColor(P2QSTRING(r.getAction().getTextColor())));
}
case TextBackground: {
return QVariant(QColor(P2QSTRING(r.getAction().getTextBackground())));
}
case PlaySound: {
return QVariant(r.getAction().playSound());
}
case SoundFile: {
return QVariant(P2QSTRING(r.getAction().getSoundFile()));
}
}
}
}
return QVariant();
}
bool QtHighlightRulesItemModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && highlightManager_ && role == Qt::EditRole) {
if (boost::numeric_cast<std::vector<std::string>::size_type>(index.row()) < highlightManager_->getRules().size()) {
HighlightRule r = highlightManager_->getRule(index.row());
std::vector<int> changedColumns;
switch (index.column()) {
case ApplyTo: {
bool ok = false;
int applyTo = value.toInt(&ok);
if (!ok) {
return false;
}
r.setMatchChat(applyTo == ApplyToAll || applyTo == ApplyToChat);
r.setMatchMUC(applyTo == ApplyToAll || applyTo == ApplyToMUC);
break;
}
case Sender:
case Keyword: {
std::string s = Q2PSTRING(value.toString());
std::vector<std::string> v;
boost::split(v, s, boost::is_any_of("\n"));
v.erase(std::remove_if(v.begin(), v.end(), boost::lambda::_1 == ""), v.end());
if (index.column() == Sender) {
r.setSenders(v);
} else {
r.setKeywords(v);
}
break;
}
case NickIsKeyword: {
r.setNickIsKeyword(value.toBool());
changedColumns.push_back(Keyword); // "<nick>"
break;
}
case MatchCase: {
r.setMatchCase(value.toBool());
break;
}
case MatchWholeWords: {
r.setMatchWholeWords(value.toBool());
break;
}
case HighlightText: {
r.getAction().setHighlightText(value.toBool());
changedColumns.push_back(Action);
break;
}
case TextColor: {
QColor c = value.value<QColor>();
r.getAction().setTextColor(c.isValid() ? Q2PSTRING(c.name()) : "");
break;
}
case TextBackground: {
QColor c = value.value<QColor>();
r.getAction().setTextBackground(c.isValid() ? Q2PSTRING(c.name()) : "");
break;
}
case PlaySound: {
r.getAction().setPlaySound(value.toBool());
changedColumns.push_back(Action);
break;
}
case SoundFile: {
r.getAction().setSoundFile(Q2PSTRING(value.toString()));
break;
}
}
highlightManager_->setRule(index.row(), r);
emit dataChanged(index, index);
foreach (int column, changedColumns) {
QModelIndex i = createIndex(index.row(), column, 0);
emit dataChanged(i, i);
}
}
}
return false;
}
QModelIndex QtHighlightRulesItemModel::parent(const QModelIndex& /* child */) const
{
return QModelIndex();
}
int QtHighlightRulesItemModel::rowCount(const QModelIndex& /* parent */) const
{
return highlightManager_ ? highlightManager_->getRules().size() : 0;
}
QModelIndex QtHighlightRulesItemModel::index(int row, int column, const QModelIndex& /* parent */) const
{
return createIndex(row, column, 0);
}
bool QtHighlightRulesItemModel::insertRows(int row, int count, const QModelIndex& /* parent */)
{
if (highlightManager_) {
beginInsertRows(QModelIndex(), row, row + count);
while (count--) {
highlightManager_->insertRule(row, HighlightRule());
}
endInsertRows();
return true;
}
return false;
}
bool QtHighlightRulesItemModel::removeRows(int row, int count, const QModelIndex& /* parent */)
{
if (highlightManager_) {
beginRemoveRows(QModelIndex(), row, row + count);
while (count--) {
highlightManager_->removeRule(row);
}
endRemoveRows();
return true;
}
return false;
}
bool QtHighlightRulesItemModel::swapRows(int row1, int row2)
{
if (highlightManager_) {
assert(row1 >= 0 && row2 >= 0 && boost::numeric_cast<std::vector<std::string>::size_type>(row1) < highlightManager_->getRules().size() && boost::numeric_cast<std::vector<std::string>::size_type>(row2) < highlightManager_->getRules().size());
HighlightRule r = highlightManager_->getRule(row1);
highlightManager_->setRule(row1, highlightManager_->getRule(row2));
highlightManager_->setRule(row2, r);
emit dataChanged(index(row1, 0, QModelIndex()), index(row1, 0, QModelIndex()));
emit dataChanged(index(row2, 0, QModelIndex()), index(row2, 0, QModelIndex()));
return true;
}
return false;
}
QString QtHighlightRulesItemModel::getApplyToString(int applyTo)
{
switch (applyTo) {
case ApplyToNone: return tr("None");
case ApplyToAll: return tr("Chat or MUC");
case ApplyToChat: return tr("Chat");
case ApplyToMUC: return tr("MUC");
default: return "";
}
}
}
|