Crossfire Server, Trunk
CREMessageItemModel.cpp
Go to the documentation of this file.
1 #include <QtWidgets>
2 
3 #include "CREMessageItemModel.h"
4 
5 CREMessageItemModel::CREMessageItemModel(QObject* parent) : QAbstractItemModel(parent), myMessage(nullptr)
6 {
7 }
8 
10 {
11 }
12 
14 {
15  beginResetModel();
17  endResetModel();
18 }
19 
21 {
22  auto blue(myBlue), red(myRed);
23  myBlue.clear();
24  myRed.clear();
25 
26  int row = index.row();
27  if (row < 0 || row >= myMessage->rules().size())
28  return;
29 
30  MessageRule* rule = myMessage->rules()[row];
31 
32  foreach(QStringList pre, rule->preconditions())
33  {
34  if (pre.size() < 3)
35  continue;
36 
37  if (pre[0] != "token" && pre[0] != "npctoken")
38  continue;
39 
40  QStringList acceptable = pre;
41  acceptable.removeFirst();
42  acceptable.removeFirst();
43 
44  for (int c = 0; c < myMessage->rules().size(); c++)
45  {
47 
48  if (check == rule)
49  continue;
50 
51  bool match = false;
52  foreach(QStringList post, check->postconditions())
53  {
54  if (post.size() < 3)
55  continue;
56  if ((post[0] != "settoken" && post[0] != "setnpctoken") || post[1] != pre[1] || !acceptable.contains(post[2]))
57  continue;
58  match = true;
59  break;
60  }
61 
62  if (match)
63  myRed.insert(c);
64  }
65  }
66 
67  foreach(QStringList post, rule->postconditions())
68  {
69  if (post.size() < 3)
70  continue;
71 
72  if (post[0] != "settoken" && post[0] != "setnpctoken")
73  continue;
74 
75  for (int c = 0; c < myMessage->rules().size(); c++)
76  {
78 
79  if (check == rule)
80  continue;
81 
82  bool match = false;
83  foreach(QStringList pre, check->preconditions())
84  {
85  if (pre.size() < 3)
86  continue;
87  if ((pre[0] != "token" && pre[0] != "npctoken") || pre[1] != post[1])
88  continue;
89 
90  QStringList acceptable = pre;
91  acceptable.removeFirst();
92  acceptable.removeFirst();
93  if (!acceptable.contains(post[2]))
94  continue;
95 
96  match = true;
97  break;
98  }
99 
100  if (match)
101  myBlue.insert(c);
102  }
103  }
104 
105  for (int b : myBlue)
106  {
107  emit dataChanged(createIndex(b, 0), createIndex(b, 6));
108  blue.remove(b);
109  red.remove(b);
110  }
111  for (int r : myRed)
112  {
113  emit dataChanged(createIndex(r, 0), createIndex(r, 6));
114  blue.remove(r);
115  red.remove(r);
116  }
117 
118  for (int old : red + blue)
119  {
120  emit dataChanged(createIndex(old, 0), createIndex(old, 6));
121  }
122 }
123 
124 int CREMessageItemModel::columnCount(const QModelIndex& parent) const
125 {
126  if (parent.isValid())
127  return 0;
128  return 6;
129 }
130 
131 QModelIndex CREMessageItemModel::index(int row, int column, const QModelIndex& parent) const
132 {
133  if (parent.isValid())
134  return QModelIndex();
135  return createIndex(row, column);
136 }
137 
138 QModelIndex CREMessageItemModel::parent(const QModelIndex&) const
139 {
140  return QModelIndex();
141 }
142 
143 int CREMessageItemModel::rowCount(const QModelIndex& index) const
144 {
145  if (!myMessage || index.isValid())
146  return 0;
147 
148  return myMessage->rules().size();
149 }
150 
151 static QString toDisplay(const QList<QStringList>& list)
152 {
153  QStringList data;
154  foreach(QStringList item, list)
155  data.append(item.join(" "));
156  return data.join("\n");
157 }
158 
159 QVariant CREMessageItemModel::data(const QModelIndex& index, int role) const
160 {
161  if (myMessage == NULL || !index.isValid())
162  return QVariant();
163 
164  if (role == Qt::BackgroundColorRole)
165  {
166  if (myBlue.contains(index.row()))
167  return QVariant::fromValue(QBrush(Qt::blue));
168  if (myRed.contains(index.row()))
169  return QVariant::fromValue(QBrush(Qt::red));
170  return QVariant();
171  }
172 
173  if (role != Qt::DisplayRole && role != Qt::EditRole)
174  return QVariant();
175 
176  Q_ASSERT(index.row() < myMessage->rules().size());
177  const MessageRule* rule = myMessage->rules()[index.row()];
178 
179  switch(index.column())
180  {
181  case 0:
182  return role == Qt::DisplayRole ? QVariant::fromValue(rule->match().join("\n")) : QVariant::fromValue(rule->match());
183  case 1:
184  return role == Qt::DisplayRole ? QVariant::fromValue(toDisplay(rule->preconditions())) : QVariant::fromValue(rule->preconditions());
185  case 2:
186  return role == Qt::DisplayRole ? QVariant::fromValue(toDisplay(rule->replies())) : QVariant::fromValue(rule->replies());
187  case 3:
188  return role == Qt::DisplayRole ? QVariant::fromValue(rule->messages().join("\n")) : QVariant::fromValue(rule->messages());
189  case 4:
190  return role == Qt::DisplayRole ? QVariant::fromValue(toDisplay(rule->postconditions())) : QVariant::fromValue(rule->postconditions());
191  case 5:
192  return role == Qt::DisplayRole ? QVariant::fromValue(rule->include().join("\n")) : QVariant::fromValue(rule->include());
193  }
194 
195  return QVariant();;
196 }
197 
198 QVariant CREMessageItemModel::headerData(int section, Qt::Orientation orientation, int role) const
199 {
200  if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
201  return QVariant();
202 
203  switch(section)
204  {
205  case 0:
206  return tr("match");
207  case 1:
208  return tr("pre-conditions");
209  case 2:
210  return tr("player suggested replies");
211  case 3:
212  return tr("NPC messages");
213  case 4:
214  return tr("post-conditions");
215  case 5:
216  return tr("includes");
217  }
218 
219  return QVariant();
220 }
221 
222 Qt::ItemFlags CREMessageItemModel::flags(const QModelIndex& index) const
223 {
224  if (!index.isValid())
225  return 0;
226  return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
227 }
228 
229 bool CREMessageItemModel::setData(const QModelIndex& index, const QVariant& value, int role)
230 {
231  if (!index.isValid() || myMessage == nullptr || myMessage->rules().size() <= index.row())
232  return false;
233  if (role != Qt::EditRole)
234  return false;
235 
236  MessageRule* rule = myMessage->rules()[index.row()];
237 
238  switch (index.column())
239  {
240  case 0:
241  rule->setMatch(value.value<QStringList>());
242  break;
243  case 1:
244  rule->setPreconditions(value.value<QList<QStringList>>());
245  break;
246  case 2:
247  rule->setReplies(value.value<QList<QStringList>>());
248  break;
249  case 3:
250  rule->setMessages(value.value<QStringList>());
251  break;
252  case 4:
253  rule->setPostconditions(value.value<QList<QStringList>>());
254  break;
255  case 5:
256  rule->setInclude(value.value<QStringList>());
257  break;
258  default:
259  return false;
260  }
261 
262  myMessage->setModified(true);
263  emit dataChanged(index, index);
264 
265  return true;
266 }
267 
268 bool CREMessageItemModel::insertRows(int row, int count, const QModelIndex& parent)
269 {
270  if (parent.isValid())
271  return false;
272 
273  beginInsertRows(parent, row, row + count - 1);
274  while (count > 0)
275  {
276  myMessage->rules().insert(row, new MessageRule());
277  count--;
278  }
279  endInsertRows();
280  myMessage->setModified(true);
281 
282  return true;
283 }
284 
285 bool CREMessageItemModel::removeRows(int row, int count, const QModelIndex& parent)
286 {
287  if (parent.isValid())
288  return false;
289  if (row < 0 || row >= myMessage->rules().size())
290  return false;
291 
292  beginRemoveRows(parent, row, row + count);
293  while (count > 0 && row < myMessage->rules().size())
294  {
295  myMessage->rules().removeAt(row);
296  count--;
297  }
298  endRemoveRows();
299  myMessage->setModified(true);
300 
301  return true;
302 }
303 
304 void CREMessageItemModel::moveUpDown(int row, bool up)
305 {
306  if (!myMessage)
307  return;
308 
309  const int shift = up ? -1 : 1;
310  if (row < 0 || row >= myMessage->rules().size() || (row + shift) < 0 || (row + shift) >= myMessage->rules().size())
311  return;
312 
313  MessageRule* moved = myMessage->rules()[row];
314  myMessage->rules()[row] = myMessage->rules()[row + shift];
315  myMessage->rules()[row + shift] = moved;
316 
317  emit dataChanged(createIndex(up ? row - 1 : row, 0), createIndex(up ? row : row + 1, 6));
318  myMessage->setModified(true);
319 }
320 
322 {
323  if (!myMessage || row < 0 || row >= myMessage->rules().count())
324  return;
325 
326  beginInsertRows(QModelIndex(), row + 1, row + 1);
327  myMessage->rules().insert(row + 1, new MessageRule(*myMessage->rules()[row]));
328  endInsertRows();
329  myMessage->setModified(true);
330 }
CREMessageItemModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const override
Definition: CREMessageItemModel.cpp:131
CREMessageItemModel::insertRows
virtual bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
Definition: CREMessageItemModel.cpp:268
CREMessageItemModel::myRed
QSet< int > myRed
Definition: CREMessageItemModel.h:38
c
static event_registration c
Definition: citylife.cpp:425
CREMessageItemModel::data
virtual QVariant data(const QModelIndex &index, int role) const override
Definition: CREMessageItemModel.cpp:159
CREMessageItemModel::setMessage
void setMessage(MessageFile *message)
Definition: CREMessageItemModel.cpp:13
guildoracle.list
list
Definition: guildoracle.py:87
CREMessageItemModel::myMessage
MessageFile * myMessage
Definition: CREMessageItemModel.h:37
CREMessageItemModel::moveUpDown
void moveUpDown(int row, bool up)
Definition: CREMessageItemModel.cpp:304
CREMessageItemModel::~CREMessageItemModel
virtual ~CREMessageItemModel()
Definition: CREMessageItemModel.cpp:9
MessageFile
Definition: MessageFile.h:56
reputation_trigger_connect.check
def check()
Definition: reputation_trigger_connect.py:18
CREMessageItemModel::CREMessageItemModel
CREMessageItemModel(QObject *parent)
Definition: CREMessageItemModel.cpp:5
CREMessageItemModel::removeRows
virtual bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
Definition: CREMessageItemModel.cpp:285
CREMessageItemModel::columnCount
virtual int columnCount(const QModelIndex &parent) const override
Definition: CREMessageItemModel.cpp:124
Ice.b
b
Definition: Ice.py:48
navar-midane_time.data
data
Definition: navar-midane_time.py:11
CREMessageItemModel::rowCount
virtual int rowCount(const QModelIndex &parent) const override
Definition: CREMessageItemModel.cpp:143
disinfect.count
int count
Definition: disinfect.py:7
push.match
bool match
Definition: push.py:61
MessageFile::setModified
void setModified(bool modified=true)
Definition: MessageFile.cpp:384
toDisplay
static QString toDisplay(const QList< QStringList > &list)
Definition: CREMessageItemModel.cpp:151
CREMessageItemModel::flags
virtual Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: CREMessageItemModel.cpp:222
MessageFile::rules
QList< MessageRule * > & rules()
Definition: MessageFile.cpp:256
diamondslots.message
string message
Definition: diamondslots.py:57
item
Definition: item.py:1
autojail.value
value
Definition: autojail.py:6
CREMessageItemModel::setSelectedRule
void setSelectedRule(const QModelIndex &index)
Definition: CREMessageItemModel.cpp:20
npc_dialog.index
int index
Definition: npc_dialog.py:102
CREMessageItemModel::headerData
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override
Definition: CREMessageItemModel.cpp:198
CREMessageItemModel::myBlue
QSet< int > myBlue
Definition: CREMessageItemModel.h:38
CREMessageItemModel::parent
virtual QModelIndex parent(const QModelIndex &index) const override
Definition: CREMessageItemModel.cpp:138
npc_dialog.rule
rule
Definition: npc_dialog.py:108
CREMessageItemModel::setData
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Definition: CREMessageItemModel.cpp:229
CREMessageItemModel::duplicateRow
void duplicateRow(int row)
Definition: CREMessageItemModel.cpp:321
CREMessageItemModel.h
ring_occidental_mages.r
r
Definition: ring_occidental_mages.py:6
MessageRule
Definition: MessageFile.h:13