Crossfire Server, Trunk  R20513
CREReplyPanel.cpp
Go to the documentation of this file.
1 #include "CREReplyPanel.h"
2 #include <QtGui>
3 
4 CREReplyPanel::CREReplyPanel(QWidget* parent) : QWidget(parent)
5 {
6  QGridLayout* layout = new QGridLayout(this);
7 
8  myReplies = new QTreeWidget(this);
9  QStringList headers;
10  headers << "Text" << "Message" << "Type";
11  myReplies->setHeaderLabels(headers);
12  connect(myReplies, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), this, SLOT(currentReplyChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
13  layout->addWidget(myReplies, 0, 0, 4, 4);
14 
15  QPushButton* add = new QPushButton(tr("add"), this);
16  connect(add, SIGNAL(clicked(bool)), this, SLOT(onAddItem(bool)));
17  layout->addWidget(add, 4, 0, 1, 2);
18 
19  QPushButton* remove = new QPushButton(tr("remove"), this);
20  connect(remove, SIGNAL(clicked(bool)), this, SLOT(onDeleteItem(bool)));
21  layout->addWidget(remove, 4, 2, 1, 2);
22 
23  myText = new QLineEdit(this);
24  connect(myText, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged(const QString&)));
25  layout->addWidget(myText, 5, 0);
26  myMessage = new QLineEdit(this);
27  connect(myMessage, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged(const QString&)));
28  layout->addWidget(myMessage, 5, 1, 1, 2);
29  myType = new QComboBox(this);
30  myType->addItem(tr("say"));
31  myType->addItem(tr("reply"));
32  myType->addItem(tr("question"));
33  connect(myType, SIGNAL(currentIndexChanged(int)), this, SLOT(onTypeChanged(int)));
34  layout->addWidget(myType, 5, 3);
35 }
36 
38 {
39 }
40 
41 void CREReplyPanel::setData(const QList<QStringList>& data)
42 {
43  myData = data;
44  myReplies->clear();
45  foreach(QStringList reply, data)
46  {
47  setText(new QTreeWidgetItem(myReplies), reply);
48  }
49 }
50 
51 QList<QStringList> CREReplyPanel::getData()
52 {
53  return myData;
54 }
55 
57 {
58  QStringList add;
59  add << "text" << "message" << "0";
60  myData.append(add);
61  setText(new QTreeWidgetItem(myReplies), add);
62  myReplies->setCurrentItem(myReplies->topLevelItem(myData.size() - 1));
63  emit dataModified();
64 }
65 
67 {
68  int idx = myReplies->indexOfTopLevelItem(myReplies->currentItem());
69  if (idx < 0 || idx >= myData.size())
70  return;
71 
72  myData.removeAt(idx);
73  delete myReplies->takeTopLevelItem(idx);
74  emit dataModified();
75 }
76 
77 void CREReplyPanel::setText(QTreeWidgetItem* item, QStringList data)
78 {
79  while (data.size() > 3)
80  data.removeLast();
81  while (data.size() < 3)
82  data.append("");
83  data[2] = myType->itemText(data[2].toInt());
84  item->setText(0, data[0]);
85  item->setText(1, data[1]);
86  item->setText(2, data[2]);
87 }
88 
89 void CREReplyPanel::onTextChanged(const QString &)
90 {
91  updateItem();
92 }
93 
95 {
96  updateItem();
97 }
98 
100 {
101  int idx = myReplies->indexOfTopLevelItem(myReplies->currentItem());
102  if (idx < 0 || idx >= myData.size())
103  return;
104 
105  QStringList data;
106  data.append(myText->text());
107  data.append(myMessage->text());
108  data.append(QString::number(myType->currentIndex()));
109  myData[idx] = data;
110  setText(myReplies->currentItem(), data);
111  emit dataModified();
112 }
113 
114 void CREReplyPanel::currentReplyChanged(QTreeWidgetItem*, QTreeWidgetItem*)
115 {
116  QTreeWidgetItem* item = myReplies->currentItem();
117  if (!item)
118  return;
119 
120  int idx = myReplies->indexOfTopLevelItem(item);
121  if (idx < 0 || idx >= myData.size())
122  return;
123 
124  QStringList data = myData[idx];
125  myText->setText(data[0]);
126  myMessage->setText(data[1]);
127  if (data.size() > 2)
128  myType->setCurrentIndex(data[2].toInt());
129  else
130  myType->setCurrentIndex(0);
131 }
void onAddItem(bool)
void dataModified()
Emitted when the data this panel manages changes .
QList< QStringList > myData
The data this panel is working on.
Definition: CREReplyPanel.h:31
void onTypeChanged(int)
void setData(const QList< QStringList > &data)
void onTextChanged(const QString &)
void onDeleteItem(bool)
virtual ~CREReplyPanel()
CREReplyPanel(QWidget *parent)
QLineEdit * myMessage
Zone to edit the message the NPC will say.
Definition: CREReplyPanel.h:37
void currentReplyChanged(QTreeWidgetItem *, QTreeWidgetItem *)
void updateItem()
Update the currently being edited reply with data in the various fields.
QComboBox * myType
Available reply type.
Definition: CREReplyPanel.h:39
QTreeWidget * myReplies
Display for the replies.
Definition: CREReplyPanel.h:33
void setText(QTreeWidgetItem *item, QStringList data)
Fill a line of myReplies with the data.
QList< QStringList > getData()
QLineEdit * myText
Zone to edit the reply keyword.
Definition: CREReplyPanel.h:35
Definition: main.c:88