Crossfire Server, Trunk  R20513
CREPrePostPanel.cpp
Go to the documentation of this file.
1 #include "CREPrePostPanel.h"
2 #include "CRERulePanel.h"
3 #include <QtGui>
4 #include "QuestConditionScript.h"
5 #include "QuestManager.h"
6 #include "Quest.h"
7 
9 {
10  QGridLayout* layout = new QGridLayout(this);
11 
12  mySubItems = new QListWidget(this);
13  connect(mySubItems, SIGNAL(currentRowChanged(int)), this, SLOT(currentSubItemChanged(int)));
14  layout->addWidget(mySubItems, 0, 0, 1, 2);
15 
16  QPushButton* addSubItem = new QPushButton(tr("add"), this);
17  connect(addSubItem, SIGNAL(clicked(bool)), this, SLOT(onAddSubItem(bool)));
18  layout->addWidget(addSubItem, 1, 0);
19 
20  QPushButton* delSubItem = new QPushButton(tr("delete"), this);
21  connect(delSubItem, SIGNAL(clicked(bool)), this, SLOT(onDeleteSubItem(bool)));
22  layout->addWidget(delSubItem, 1, 1);
23 
24  myItemEdit = new QLineEdit(this);
25  connect(myItemEdit, SIGNAL(textChanged(const QString&)), this, SLOT(subItemChanged(const QString&)));
26  layout->addWidget(myItemEdit, 2, 0, 1, 2);
27 }
28 
29 void CRESubItemList::setData(const QStringList& data)
30 {
31  myData = data;
32  myData.takeFirst();
33  mySubItems->clear();
34  mySubItems->addItems(myData);
35  myItemEdit->clear();
36 }
37 
39 {
40  if (mySubItems->currentItem())
41  myItemEdit->setText(mySubItems->currentItem()->text());
42 }
43 
44 
46 {
47  myData.append("(item)");
48  mySubItems->addItem("(item)");
49  mySubItems->setCurrentRow(myData.size() - 1);
50 
51  emit dataModified(myData);
52 }
53 
55 {
56  if (mySubItems->currentRow() < 0)
57  return;
58 
59  myData.removeAt(mySubItems->currentRow());
60  delete mySubItems->takeItem(mySubItems->currentRow());
61  mySubItems->setCurrentRow(0);
62  emit dataModified(myData);
63 }
64 
65 void CRESubItemList::subItemChanged(const QString& text)
66 {
67  if (mySubItems->currentRow() < 0)
68  return;
69 
70  myData[mySubItems->currentRow()] = text;
71  mySubItems->currentItem()->setText(text);
72  emit dataModified(myData);
73 }
74 
76 {
77  QVBoxLayout* layout = new QVBoxLayout(this);
78 
79  layout->addWidget(new QLabel(tr("Connection number:"), this));
80  myEdit = new QLineEdit(this);
81  myEdit->setValidator(new QIntValidator(1, 65000, myEdit));
82  connect(myEdit, SIGNAL(textChanged(const QString&)), this, SLOT(editChanged(const QString&)));
83  layout->addWidget(myEdit);
84  myWarning = new QLabel(this);
85  myWarning->setVisible(false);
86  layout->addWidget(myWarning);
87  layout->addStretch();
88 }
89 
90 void CRESubItemConnection::setData(const QStringList& data)
91 {
92  if (data.size() < 2)
93  {
94  showWarning(tr("Not enough arguments"));
95  return;
96  }
97 
98  bool ok = false;
99  int value = data[1].toInt(&ok);
100  if (!ok || value <= 0 || value > 65000)
101  {
102  showWarning(tr("Invalid number %1, must be a number between 1 and 65000").arg(data[1]));
103  value = 1;
104  }
105 
106  myWarning->setVisible(false);
107  myEdit->setText(QString::number(value));
108 }
109 
110 void CRESubItemConnection::showWarning(const QString& warning)
111 {
112  myWarning->setText(warning);
113  myWarning->setVisible(true);
114 }
115 
116 void CRESubItemConnection::editChanged(const QString& text)
117 {
118  bool ok = false;
119  int value = text.toInt(&ok);
120  if (!ok || value <= 0 || value > 65000)
121  {
122  showWarning(tr("Invalid number %1, must be a number between 1 and 65000").arg(text));
123  return;
124  }
125 
126  myWarning->setVisible(false);
127  emit dataModified(QStringList(text));
128 }
129 
130 CRESubItemQuest::CRESubItemQuest(bool isPre, const QuestManager* quests, QWidget* parent) : CRESubItemWidget(parent)
131 {
132  myQuests = quests;
133  myIsPre = isPre;
134  myInit = true;
135 
136  QVBoxLayout* layout = new QVBoxLayout(this);
137 
138  layout->addWidget(new QLabel(tr("Quest:"), this));
139 
140  myQuestList = new QComboBox(this);
141  layout->addWidget(myQuestList);
142 
143  if (isPre)
144  {
145  myAtStep = new QRadioButton(tr("at step"), this);
146  layout->addWidget(myAtStep);
147  myFromStep = new QRadioButton(tr("from step"), this);
148  layout->addWidget(myFromStep);
149  myStepRange = new QRadioButton(tr("from step to step"), this);
150  layout->addWidget(myStepRange);
151 
152  connect(myAtStep, SIGNAL(toggled(bool)), this, SLOT(checkToggled(bool)));
153  connect(myFromStep, SIGNAL(toggled(bool)), this, SLOT(checkToggled(bool)));
154  connect(myStepRange, SIGNAL(toggled(bool)), this, SLOT(checkToggled(bool)));
155  }
156  else
157  {
158  layout->addWidget(new QLabel(tr("new step:"), this));
159  myAtStep = NULL;
160  myFromStep = NULL;
161  }
162 
163  myFirstStep = new QComboBox(this);
164  layout->addWidget(myFirstStep);
165 
166  if (isPre)
167  {
168  mySecondStep = new QComboBox(this);
169  layout->addWidget(mySecondStep);
170  }
171  else
172  mySecondStep = NULL;
173 
174  layout->addStretch();
175 
176  connect(myQuestList, SIGNAL(currentIndexChanged(int)), this, SLOT(selectedQuestChanged(int)));
177  foreach(const Quest* quest, quests->quests())
178  {
179  myQuestList->addItem(quest->title() + " [" + quest->code() + "]", quest->code());
180  }
181  connect(myFirstStep, SIGNAL(currentIndexChanged(int)), this, SLOT(selectedStepChanged(int)));
182  if (mySecondStep)
183  connect(mySecondStep, SIGNAL(currentIndexChanged(int)), this, SLOT(selectedStepChanged(int)));
184 
185  myInit = false;
186 }
187 
188 void CRESubItemQuest::setData(const QStringList& data)
189 {
190  if (data.size() < 3)
191  return;
192 
193  int index = myQuestList->findData(data[1], Qt::UserRole);
194  if (index == -1)
195  {
196  return;
197  }
198 
199  myInit = true;
200  myQuestList->setCurrentIndex(index);
201 
202  if (!myIsPre)
203  {
204  myFirstStep->setCurrentIndex(myFirstStep->findData(data[2], Qt::UserRole));
205  myInit = false;
206  return;
207  }
208 
209  QString steps = data[2];
210  int idx = steps.indexOf('-');
211 
212  if (idx == -1)
213  {
214  int start = 0;
215  if (steps.startsWith('='))
216  {
217  myAtStep->setChecked(true);
218  start = 1;
219  }
220  else
221  myFromStep->setChecked(true);
222 
223  myFirstStep->setCurrentIndex(myFirstStep->findData(steps.mid(start), Qt::UserRole));
224  }
225  else
226  {
227  myStepRange->setChecked(true);
228  myFirstStep->setCurrentIndex(myFirstStep->findData(steps.left(idx), Qt::UserRole));
229  mySecondStep->setCurrentIndex(mySecondStep->findData(steps.mid(idx + 1), Qt::UserRole));
230  }
231 
232  myInit = false;
233 }
234 
236 {
237  myFirstStep->clear();
238  if (myIsPre)
239  myFirstStep->addItem("(not started)", "0");
240 
241  if (mySecondStep)
242  mySecondStep->clear();
243 
244  if (index < 0 || index >= myQuests->quests().size())
245  return;
246 
247  const Quest* quest = myQuests->quests()[index];
248 
249  QString desc;
250  foreach (const QuestStep* step, quest->steps())
251  {
252  desc = tr("%1 (%2)").arg(QString::number(step->step()), step->description().left(30));
253  if (step->isCompletion())
254  desc += " (end)";
255  myFirstStep->addItem(desc, QString::number(step->step()));
256  if (mySecondStep)
257  mySecondStep->addItem(desc, QString::number(step->step()));
258  }
259 }
260 
262 {
263  if (myInit)
264  return;
265 
266  QStringList data;
267 
268  data << myQuestList->itemData(myQuestList->currentIndex(), Qt::UserRole).toString();
269 
270  if (myIsPre)
271  {
272  QString value;
273  if (myStepRange->isChecked())
274  {
275  value = myFirstStep->itemData(myFirstStep->currentIndex(), Qt::UserRole).toString();
276  value += "-";
277  value += mySecondStep->itemData(mySecondStep->currentIndex(), Qt::UserRole).toString();
278  }
279  else
280  {
281  if (myAtStep->isChecked())
282  value = "=";
283  value += myFirstStep->itemData(myFirstStep->currentIndex(), Qt::UserRole).toString();
284  }
285 
286  data << value;
287  }
288  else
289  {
290  data << myFirstStep->itemData(myFirstStep->currentIndex(), Qt::UserRole).toString();
291  }
292 
293  emit dataModified(data);
294 }
295 
297 {
298  if (checked == false)
299  return;
300 
301  mySecondStep->setEnabled(myStepRange->isChecked());
302  updateData();
303 }
304 
306 {
307  if (index == -1)
308  return;
309 
310  updateData();
311 }
312 
313 CRESubItemToken::CRESubItemToken(bool isPre, QWidget* parent) : CRESubItemWidget(parent)
314 {
315  QVBoxLayout* layout = new QVBoxLayout(this);
316 
317  layout->addWidget(new QLabel(tr("Token:"), this));
318  myToken = new QLineEdit(this);
319  layout->addWidget(myToken);
320  connect(myToken, SIGNAL(textChanged(const QString&)), this, SLOT(tokenChanged(const QString&)));
321 
322  if (isPre)
323  {
324  layout->addWidget(new QLabel(tr("Values the token can be (one per line):"), this));
325  myValues = new QTextEdit(this);
326  myValues->setAcceptRichText(false);
327  layout->addWidget(myValues);
328  connect(myValues, SIGNAL(textChanged()), this, SLOT(valuesChanged()));
329  myValue = NULL;
330  }
331  else
332  {
333  layout->addWidget(new QLabel(tr("Value to set for the token:"), this));
334  myValue = new QLineEdit(this);
335  layout->addWidget(myValue);
336  connect(myValue, SIGNAL(textChanged(const QString&)), this, SLOT(tokenChanged(const QString&)));
337  myValues = NULL;
338  }
339  layout->addStretch();
340 }
341 
342 void CRESubItemToken::setData(const QStringList& data)
343 {
344  QStringList copy(data);
345 
346  if (data.size() < 2)
347  {
348  myToken->clear();
349  if (myValues != NULL)
350  myValues->clear();
351  if (myValue != NULL)
352  myValue->clear();
353 
354  return;
355  }
356  copy.removeFirst();
357  myToken->setText(copy.takeFirst());
358  if (myValues != NULL)
359  myValues->setText(copy.join("\n"));
360  else if (copy.size() > 0)
361  myValue->setText(copy[0]);
362  else
363  myValue->clear();
364 }
365 
367 {
368  QStringList values;
369  values.append(myToken->text());
370  if (myValues != NULL)
371  values.append(myValues->toPlainText().split("\n"));
372  else
373  values.append(myValue->text());
374  emit dataModified(values);
375 }
376 
377 void CRESubItemToken::tokenChanged(const QString&)
378 {
379  updateData();
380 }
381 
383 {
384  updateData();
385 }
386 
387 
388 CREPrePostPanel::CREPrePostPanel(bool isPre, const QList<QuestConditionScript*> scripts, const QuestManager* quests, QWidget* parent) : QWidget(parent)
389 {
390  QGridLayout* layout = new QGridLayout(this);
391 
392  myItems = new QListWidget(this);
393  connect(myItems, SIGNAL(currentRowChanged(int)), this, SLOT(currentItemChanged(int)));
394  layout->addWidget(myItems, 0, 0, 3, 2);
395 
396  QPushButton* addItem = new QPushButton(tr("add"), this);
397  connect(addItem, SIGNAL(clicked(bool)), this, SLOT(onAddItem(bool)));
398  layout->addWidget(addItem, 3, 0);
399 
400  QPushButton* delItem = new QPushButton(tr("delete"), this);
401  connect(delItem, SIGNAL(clicked(bool)), this, SLOT(onDeleteItem(bool)));
402  layout->addWidget(delItem, 3, 1);
403 
404  layout->addWidget(new QLabel(tr("Script:"), this), 0, 2);
405  myChoices = new QComboBox(this);
406  connect(myChoices, SIGNAL(currentIndexChanged(int)), this, SLOT(currentChoiceChanged(int)));
407 
408  mySubItemsStack = new QStackedWidget(this);
409 
410  for(int script = 0; script < scripts.size(); script++)
411  {
412  myChoices->addItem(scripts[script]->name());
413  myChoices->setItemData(script, scripts[script]->comment(), Qt::ToolTipRole);
414  mySubWidgets.append(createSubItemWidget(isPre, scripts[script], quests));
415  mySubItemsStack->addWidget(mySubWidgets.last());
416  connect(mySubWidgets.last(), SIGNAL(dataModified(const QStringList&)), this, SLOT(subItemChanged(const QStringList&)));
417  }
418 
419  layout->addWidget(myChoices, 0, 3);
420 
421  layout->addWidget(mySubItemsStack, 1, 2, 3, 2);
422 }
423 
425 {
426 }
427 
428 QList<QStringList> CREPrePostPanel::getData()
429 {
430  return myData;
431 }
432 
433 void CREPrePostPanel::setData(const QList<QStringList> data)
434 {
435  myItems->clear();
436 
437  myData = data;
438 
439  foreach(QStringList list, data)
440  {
441  if (list.size() > 0)
442  myItems->addItem(list[0]);
443  else
444  myItems->addItem(tr("(empty)"));
445  }
446 }
447 
449 {
450  myData.append(QStringList("quest"));
451  myItems->addItem("quest");
452  myItems->setCurrentRow(myData.size() - 1);
453  emit dataModified();
454 }
455 
457 {
458  if (myItems->currentRow() < 0 || myItems->currentRow() >= myData.size())
459  return;
460 
461  myData.removeAt(myItems->currentRow());
462  delete myItems->takeItem(myItems->currentRow());
463  myItems->setCurrentRow(0);
464  emit dataModified();
465 }
466 
468 {
469  if (index < 0 || index >= myData.size())
470  return;
471 
472  QStringList data = myData[index];
473  if (data.size() == 0)
474  return;
475 
476  myChoices->setCurrentIndex(myChoices->findText(data[0]));
477 
478  mySubWidgets[myChoices->currentIndex()]->setData(data);
479 }
480 
482 {
483  if (myItems->currentRow() < 0 || myItems->currentRow() >= myData.size())
484  return;
485 
486  QStringList& data = myData[myItems->currentRow()];
487  if (data.size() == 0)
488  data.append(myChoices->currentText());
489  else
490  data[0] = myChoices->currentText();
491  myItems->currentItem()->setText(data[0]);
492 
493  mySubItemsStack->setCurrentIndex(myChoices->currentIndex());
494  mySubWidgets[myChoices->currentIndex()]->setData(data);
495 
496  emit dataModified();
497 }
498 
499 void CREPrePostPanel::subItemChanged(const QStringList& data)
500 {
501  if (myItems->currentRow() < 0 || myItems->currentRow() >= myData.size())
502  return;
503 
504  QStringList& item = myData[myItems->currentRow()];
505  while (item.size() > 1)
506  item.removeLast();
507  item.append(data);
508 
509  emit dataModified();
510 }
511 
513 {
514  if (!isPre && script->name() == "connection")
515  return new CRESubItemConnection(this);
516 
517  if (script->name() == "quest")
518  return new CRESubItemQuest(isPre, quests, this);
519 
520  if (script->name() == "token" || script->name() == "settoken" || script->name() == "npctoken" || script->name() == "setnpctoken")
521  return new CRESubItemToken(isPre, this);
522 
523  return new CRESubItemList(this);
524 }
Post-condition panel displaying a connection (number).
QList< QStringList > myData
Pre- or post- conditions we&#39;re working on.
Pre- or post- conditions panel displaying a token, either as read or write.
void showWarning(const QString &warning)
QRadioButton * myFromStep
QStringList myData
Current arguments.
void editChanged(const QString &text)
QLineEdit * myItemEdit
Argument edit zone.
QLineEdit * myToken
QComboBox * myQuestList
List of quests.
void dataModified(const QStringList &data)
virtual void setData(const QStringList &data)
Definition: Quest.h:32
CREPrePostPanel(bool isPre, const QList< QuestConditionScript * > scripts, const QuestManager *quests, QWidget *parent)
Standard constructor.
QList< const QuestStep * > steps() const
Definition: Quest.cpp:138
virtual void setData(const QStringList &data)
const QString & code() const
Definition: Quest.cpp:63
QComboBox * myChoices
Available conditions types.
Pre- or post- panel displaying script arguments as a string list.
const QString & name() const
CRESubItemConnection(QWidget *parent)
CRESubItemWidget * createSubItemWidget(bool isPre, const QuestConditionScript *script, const QuestManager *quests)
Creates a CRESubItemWidget for the specified script.
const QString & title() const
Definition: Quest.cpp:76
virtual ~CREPrePostPanel()
Base class for a pre- or post- panel displaying script arguments.
void selectedStepChanged(int index)
QList< CRESubItemWidget * > mySubWidgets
Matching between index of myChoices and the variable subpanels.
QTextEdit * myValues
QListWidget * mySubItems
For one condition, arguments to the script.
QListWidget * myItems
The first item of each condition.
void onAddSubItem(bool)
QComboBox * myFirstStep
Steps of the current quest for new step (post-) or at/frop step (pre-).
Definition: Quest.h:9
QStackedWidget * mySubItemsStack
Arguments panels, only one visible based on the choice.
QComboBox * mySecondStep
Steps of the current quest for up to step (pre-).
QRadioButton * myAtStep
void setData(const QList< QStringList > data)
QList< QStringList > getData()
void subItemChanged(const QString &text)
int step() const
Definition: Quest.cpp:9
void onDeleteItem(bool)
const QuestManager * myQuests
CRESubItemList(QWidget *parent)
const QString & description() const
Definition: Quest.cpp:19
QLineEdit * myValue
void onDeleteSubItem(bool)
void subItemChanged(const QStringList &data)
void dataModified()
Emitted when the data this panel manages was changed.
CRESubItemToken(bool isPre, QWidget *parent)
void currentItemChanged(int index)
QList< const Quest * > quests() const
void tokenChanged(const QString &)
bool isCompletion() const
Definition: Quest.cpp:29
void currentChoiceChanged(int index)
void selectedQuestChanged(int index)
static quest_definition * quests
All known quests.
Definition: quest.c:93
QRadioButton * myStepRange
Pre- or post- conditions panel displaying a quest step.
void setData(const QStringList &data)
void checkToggled(bool checked)
virtual void setData(const QStringList &data)
CRESubItemQuest(bool isPre, const QuestManager *quests, QWidget *parent)
void currentSubItemChanged(int)
Definition: main.c:88