Crossfire Server, Trunk  R20513
CREMessagePanel.cpp
Go to the documentation of this file.
1 #include <QtGui>
2 #include <qdir.h>
3 #include "CREMessagePanel.h"
4 #include "CREFilterDefinition.h"
5 #include "MessageFile.h"
6 #include "CRERulePanel.h"
7 #include "CREMapInformation.h"
8 #include "MessageManager.h"
9 
11 {
12  Q_ASSERT(manager != NULL);
13  myMessageManager = manager;
14 
15  QVBoxLayout* main = new QVBoxLayout(this);
16  QTabWidget* tab = new QTabWidget(this);
17  main->addWidget(tab);
18 
19  QWidget* details = new QWidget(this);
20  tab->addTab(details, tr("Details"));
21 
22 
23  QGridLayout* layout = new QGridLayout(details);
24 
25  int line = 0;
26 
27  layout->addWidget(new QLabel(tr("Path:"), this), line, 0);
28  myPath = new QLineEdit(this);
29  layout->addWidget(myPath, line++, 1);
30 
31  layout->addWidget(new QLabel(tr("Location:"), this), line, 0, 1, 2);
32  myLocation = new QLineEdit(this);
33  layout->addWidget(myLocation, line++, 1, 1, 2);
34 
35  QGroupBox* box = new QGroupBox(tr("Rules"));
36  layout->addWidget(box, line++, 0, 1, 4);
37 
38  QGridLayout* rules = new QGridLayout();
39  box->setLayout(rules);
40 
41  myRules = new QTreeWidget();
42  rules->addWidget(myRules, 0, 0, 4, 4);
43  QStringList labels;
44  labels << tr("match") << tr("pre") << tr("message") << tr("post") << tr("replies") << tr("include");
45  myRules->setHeaderLabels(labels);
46  myRules->setWordWrap(true);
47  connect(myRules, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), this, SLOT(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
48 
49  QHBoxLayout* buttons = new QHBoxLayout();
50 
51  QPushButton* add = new QPushButton(tr("add rule"), this);
52  buttons->addWidget(add);
53  connect(add, SIGNAL(clicked(bool)), this, SLOT(onAddRule(bool)));
54  QPushButton* remove = new QPushButton(tr("remove rule"), this);
55  buttons->addWidget(remove);
56  connect(remove, SIGNAL(clicked(bool)), this, SLOT(onDeleteRule(bool)));
57 
58  QPushButton* up = new QPushButton(tr("move up"), this);
59  buttons->addWidget(up);
60  connect(up, SIGNAL(clicked(bool)), this, SLOT(onMoveUp(bool)));
61  QPushButton* down = new QPushButton(tr("move down"), this);
62  buttons->addWidget(down);
63  connect(down, SIGNAL(clicked(bool)), this, SLOT(onMoveDown(bool)));
64 
65  QPushButton* copy = new QPushButton(tr("copy"), this);
66  buttons->addWidget(copy);
67  connect(copy, SIGNAL(clicked(bool)), this, SLOT(onDuplicate(bool)));
68 
69  rules->addLayout(buttons, 4, 0, 1, 4);
70 
71  myRulePanel = new CRERulePanel(manager, quests, this);
72  connect(myRulePanel, SIGNAL(currentRuleModified()), this, SLOT(currentRuleModified()));
73  rules->addWidget(myRulePanel, 5, 0, 4, 4);
74 
75  myUse = new QTreeWidget(this);
76  tab->addTab(myUse, tr("Use"));
77  myUse->setHeaderLabel(tr("Referenced by..."));
78 
79  myMessage = NULL;
80 }
81 
83 {
84 }
85 
86 QString toDisplay(const QList<QStringList>& list)
87 {
88  QStringList data;
89  foreach(QStringList item, list)
90  data.append(item.join(" "));
91  return data.join("\n");
92 }
93 
95 {
96  myPath->setText(message->path());
97  /* can only change path when new file is created */
98  myPath->setEnabled(message->path() == "<new file>");
99  myLocation->setText(message->location());
100 
101  /* so the change handler won't do anything */
102  myMessage = NULL;
103  myRules->clear();
104  myMessage = message;
105 
106  foreach(MessageRule* rule, myMessage->rules())
107  {
108  QStringList data;
109  data << rule->match().join("\n");
110  data << toDisplay(rule->preconditions());
111  data << rule->messages().join("\n");
112  data << toDisplay(rule->postconditions());
113  data << toDisplay(rule->replies());
114  data << rule->include();
115  new QTreeWidgetItem(myRules, data);
116  }
117 
118  if (myRules->topLevelItemCount() != 0)
119  myDefaultBackground = myRules->topLevelItem(0)->background(0);
120 
122 
123  myUse->clear();
124 
125  QTreeWidgetItem* root = NULL;
126  if (myMessage->maps().length() > 0)
127  {
128  root = new QTreeWidgetItem(myUse, QStringList(tr("Maps")));
129  root->setExpanded(true);
130  foreach(CREMapInformation* map, myMessage->maps())
131  {
132  new QTreeWidgetItem(root, QStringList(map->path()));
133  }
134  root = NULL;
135  }
136 
137  foreach(MessageFile* file, myMessageManager->messages())
138  {
139  bool got = false;
140  foreach(MessageRule* rule, file->rules())
141  {
142  QStringList includes = rule->include();
143  foreach(QString include, includes)
144  {
145  if (include.isEmpty())
146  continue;
147 
148  if (!include.startsWith('/'))
149  {
150  int last = file->path().lastIndexOf(QDir::separator());
151  if (last == -1)
152  continue;
153  include = file->path().left(last + 1) + include;
154  }
155 
156  if (include == message->path())
157  {
158  if (root == NULL)
159  {
160  root = new QTreeWidgetItem(myUse, QStringList(tr("Messages")));
161  root->setExpanded(true);
162  }
163 
164  new QTreeWidgetItem(root, QStringList(file->path()));
165 
166  got = true;
167  break;
168  }
169 
170  if (got)
171  break;
172  }
173  }
174 
175  if (got)
176  break;
177  }
178 }
179 
180 void setBackgroundColor(QTreeWidgetItem* item, QBrush color)
181 {
182  for (int c = 0; c < item->columnCount(); c++)
183  item->setBackground(c, color);
184 }
185 
186 void CREMessagePanel::currentItemChanged(QTreeWidgetItem* current, QTreeWidgetItem* /*previous*/)
187 {
188  if (!myMessage || myRules->topLevelItemCount() == 0)
189  return;
190 
191  for (int r = 0; r < myMessage->rules().size(); r++)
192  {
194  }
195 
196  int index = myRules->indexOfTopLevelItem(current);
197  if (index < 0 || index >= myMessage->rules().size())
198  return;
199 
200  MessageRule* rule = myMessage->rules()[index];
201 
203 
204  foreach(QStringList pre, rule->preconditions())
205  {
206  if (pre.size() < 3)
207  continue;
208 
209  if (pre[0] != "token" && pre[0] != "npctoken")
210  continue;
211 
212  QStringList acceptable = pre;
213  acceptable.removeFirst();
214  acceptable.removeFirst();
215 
216  for (int c = 0; c < myMessage->rules().size(); c++)
217  {
218  MessageRule* check = myMessage->rules()[c];
219 
220  if (check == rule)
221  continue;
222 
223  bool match = false;
224  foreach(QStringList post, check->postconditions())
225  {
226  if (post.size() < 3)
227  continue;
228  if ((post[0] != "settoken" && post[0] != "setnpctoken") || post[1] != pre[1] || !acceptable.contains(post[2]))
229  continue;
230  match = true;
231  break;
232  }
233 
234  if (match)
235  setBackgroundColor(myRules->topLevelItem(c), Qt::red);
236  }
237  }
238 
239  foreach(QStringList post, rule->postconditions())
240  {
241  if (post.size() < 3)
242  continue;
243 
244  if (post[0] != "settoken" && post[0] != "setnpctoken")
245  continue;
246 
247  for (int c = 0; c < myMessage->rules().size(); c++)
248  {
249  MessageRule* check = myMessage->rules()[c];
250 
251  if (check == rule)
252  continue;
253 
254  bool match = false;
255  foreach(QStringList pre, check->preconditions())
256  {
257  if (pre.size() < 3)
258  continue;
259  if ((pre[0] != "token" && pre[0] != "npctoken") || pre[1] != post[1])
260  continue;
261 
262  QStringList acceptable = pre;
263  acceptable.removeFirst();
264  acceptable.removeFirst();
265  if (!acceptable.contains(post[2]))
266  continue;
267 
268  match = true;
269  break;
270  }
271 
272  if (match)
273  setBackgroundColor(myRules->topLevelItem(c), Qt::blue);
274  }
275  }
276 }
277 
279 {
280  int index = myRules->currentIndex().row();
281  fillRuleItem(myRules->currentItem(), myMessage->rules()[index]);
282  myMessage->rules()[index]->setModified();
283 }
284 
285 void CREMessagePanel::fillRuleItem(QTreeWidgetItem* item, MessageRule* rule)
286 {
287  item->setText(0, rule->match().join("\n"));
288  item->setText(1, toDisplay(rule->preconditions()));
289  item->setText(2, rule->messages().join("\n"));
290  item->setText(3, toDisplay(rule->postconditions()));
291  item->setText(4, toDisplay(rule->replies()));
292  item->setText(5, rule->include().join("\n"));
293 }
294 
296 {
297  MessageRule* rule = new MessageRule();
298  rule->match().append("*");
299  rule->setModified(true);
300  myMessage->rules().append(rule);
301  new QTreeWidgetItem(myRules, QStringList("*"));
302 }
303 
305 {
306  int index = myRules->currentIndex().row();
307  if (index < 0 || index > myMessage->rules().size())
308  return;
309 
310  myMessage->rules().removeAt(index);
311  delete myRules->takeTopLevelItem(index);
312 }
313 
315 {
316  myMessage->setPath(myPath->text());
317  myMessage->setLocation(myLocation->text());
318 }
319 
321 {
322  int index = myRules->currentIndex().row();
323  if (index <= 0 || index >= myMessage->rules().size())
324  return;
325 
326  MessageRule* swap = myMessage->rules()[index - 1];
327  myMessage->rules()[index - 1] = myMessage->rules()[index];
328  myMessage->rules()[index] = swap;
329 
330  QTreeWidgetItem* item = myRules->takeTopLevelItem(index - 1);
331  myRules->insertTopLevelItem(index, item);
332 
334 }
335 
337 {
338  int index = myRules->currentIndex().row();
339  if (index < 0 || index >= myMessage->rules().size() - 1)
340  return;
341 
342  MessageRule* swap = myMessage->rules()[index + 1];
343  myMessage->rules()[index + 1] = myMessage->rules()[index];
344  myMessage->rules()[index] = swap;
345 
346  QTreeWidgetItem* item = myRules->takeTopLevelItem(index + 1);
347  myRules->insertTopLevelItem(index, item);
348 
350 }
351 
353 {
354  int index = myRules->currentIndex().row();
355  if (index < 0 || index > myMessage->rules().size())
356  return;
357 
358  const MessageRule* original = myMessage->rules()[index];
359 
360  MessageRule* rule = new MessageRule(*original);
361  myMessage->rules().append(rule);
362  fillRuleItem(new QTreeWidgetItem(myRules, rule->match()), rule);
363 }
QList< MessageFile * > & messages()
void onDeleteRule(bool)
const QList< QStringList > & postconditions() const
Definition: MessageFile.cpp:66
const QString & location() const
void setBackgroundColor(QTreeWidgetItem *item, QBrush color)
const QList< QStringList > & preconditions() const
Definition: MessageFile.cpp:56
CRERulePanel * myRulePanel
QTreeWidget * myUse
int main(int argc, char *argv[])
Definition: main.c:159
CREMessagePanel(const MessageManager *manager, const QuestManager *quests)
void setModified(bool modified=true)
void setPath(const QString &path)
QList< MessageRule * > & rules()
void onDuplicate(bool)
void setModified(bool modified=true)
void currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
void setMessageRule(MessageRule *rule)
static char root[500]
Path to store generated files.
Definition: mapper.c:365
void fillRuleItem(QTreeWidgetItem *item, MessageRule *rule)
virtual void setItem(MessageFile *message)
QBrush myDefaultBackground
void setLocation(const QString &location)
const QStringList & match() const
Definition: MessageFile.cpp:41
MessageFile * myMessage
QTreeWidget * myRules
const MessageManager * myMessageManager
virtual ~CREMessagePanel()
const QStringList & include() const
Definition: MessageFile.cpp:86
virtual void commitData()
static quest_definition * quests
All known quests.
Definition: quest.c:93
QList< CREMapInformation * > & maps()
const QString & path() const
const QStringList & messages() const
Definition: MessageFile.cpp:76
QString toDisplay(const QList< QStringList > &list)
QLineEdit * myLocation
const QList< QStringList > & replies() const
Definition: MessageFile.cpp:96
QLineEdit * myPath
Definition: main.c:88