Crossfire Server, Trunk
CREReplyPanel.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 2022 the Crossfire Development Team
5  *
6  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
7  * welcome to redistribute it under certain conditions. For details, please
8  * see COPYING and LICENSE.
9  *
10  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
11  */
12 
13 #include "CREReplyPanel.h"
14 #include <QtWidgets>
15 
16 CREReplyPanel::CREReplyPanel(QWidget* parent) : QWidget(parent)
17 {
18  QGridLayout* layout = new QGridLayout(this);
19 
20  myReplies = new QTreeWidget(this);
21  QStringList headers;
22  headers << "Text" << "Message" << "Type";
23  myReplies->setHeaderLabels(headers);
24  connect(myReplies, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), this, SLOT(currentReplyChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
25  layout->addWidget(myReplies, 0, 0, 4, 4);
26 
27  QPushButton* add = new QPushButton(tr("add"), this);
28  connect(add, SIGNAL(clicked(bool)), this, SLOT(onAddItem(bool)));
29  layout->addWidget(add, 4, 0, 1, 2);
30 
31  QPushButton* remove = new QPushButton(tr("remove"), this);
32  connect(remove, SIGNAL(clicked(bool)), this, SLOT(onDeleteItem(bool)));
33  layout->addWidget(remove, 4, 2, 1, 2);
34 
35  myText = new QLineEdit(this);
36  connect(myText, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged(const QString&)));
37  layout->addWidget(myText, 5, 0);
38  myMessage = new QLineEdit(this);
39  connect(myMessage, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged(const QString&)));
40  layout->addWidget(myMessage, 5, 1, 1, 2);
41  myType = new QComboBox(this);
42  myType->addItem(tr("say"));
43  myType->addItem(tr("reply"));
44  myType->addItem(tr("question"));
45  connect(myType, SIGNAL(currentIndexChanged(int)), this, SLOT(onTypeChanged(int)));
46  layout->addWidget(myType, 5, 3);
47 }
48 
50 {
51 }
52 
53 void CREReplyPanel::setData(const QList<QStringList>& data)
54 {
55  myData = data;
56  myReplies->clear();
57  foreach(QStringList reply, data)
58  {
59  setText(new QTreeWidgetItem(myReplies), reply);
60  }
61 }
62 
63 QList<QStringList> CREReplyPanel::getData()
64 {
65  return myData;
66 }
67 
69 {
70  QStringList add;
71  add << "text" << "message" << "0";
72  myData.append(add);
73  setText(new QTreeWidgetItem(myReplies), add);
74  myReplies->setCurrentItem(myReplies->topLevelItem(myData.size() - 1));
75  emit dataModified();
76 }
77 
79 {
80  int idx = myReplies->indexOfTopLevelItem(myReplies->currentItem());
81  if (idx < 0 || idx >= myData.size())
82  return;
83 
84  myData.removeAt(idx);
85  delete myReplies->takeTopLevelItem(idx);
86  emit dataModified();
87 }
88 
89 void CREReplyPanel::setText(QTreeWidgetItem* item, QStringList data)
90 {
91  while (data.size() > 3)
92  data.removeLast();
93  while (data.size() < 3)
94  data.append("");
95  data[2] = myType->itemText(data[2].toInt());
96  item->setText(0, data[0]);
97  item->setText(1, data[1]);
98  item->setText(2, data[2]);
99 }
100 
101 void CREReplyPanel::onTextChanged(const QString &)
102 {
103  updateItem();
104 }
105 
107 {
108  updateItem();
109 }
110 
112 {
113  int idx = myReplies->indexOfTopLevelItem(myReplies->currentItem());
114  if (idx < 0 || idx >= myData.size())
115  return;
116 
117  QStringList data;
118  data.append(myText->text());
119  data.append(myMessage->text());
120  data.append(QString::number(myType->currentIndex()));
121  myData[idx] = data;
122  setText(myReplies->currentItem(), data);
123  emit dataModified();
124 }
125 
126 void CREReplyPanel::currentReplyChanged(QTreeWidgetItem*, QTreeWidgetItem*)
127 {
128  QTreeWidgetItem* item = myReplies->currentItem();
129  if (!item)
130  return;
131 
132  int idx = myReplies->indexOfTopLevelItem(item);
133  if (idx < 0 || idx >= myData.size())
134  return;
135 
136  QStringList data = myData[idx];
137  myText->setText(data[0]);
138  myMessage->setText(data[1]);
139  if (data.size() > 2)
140  myType->setCurrentIndex(data[2].toInt());
141  else
142  myType->setCurrentIndex(0);
143 }
CREReplyPanel::myText
QLineEdit * myText
Definition: CREReplyPanel.h:47
layout
Definition: main.c:85
CREReplyPanel::~CREReplyPanel
virtual ~CREReplyPanel()
Definition: CREReplyPanel.cpp:49
say.reply
string reply
Definition: say.py:77
CREReplyPanel::onTextChanged
void onTextChanged(const QString &)
Definition: CREReplyPanel.cpp:101
CREReplyPanel.h
CREReplyPanel::dataModified
void dataModified()
CREReplyPanel::currentReplyChanged
void currentReplyChanged(QTreeWidgetItem *, QTreeWidgetItem *)
Definition: CREReplyPanel.cpp:126
CREReplyPanel::myMessage
QLineEdit * myMessage
Definition: CREReplyPanel.h:49
CREReplyPanel::myData
QList< QStringList > myData
Definition: CREReplyPanel.h:43
CREReplyPanel::onTypeChanged
void onTypeChanged(int)
Definition: CREReplyPanel.cpp:106
CREReplyPanel::setData
void setData(const QList< QStringList > &data)
Definition: CREReplyPanel.cpp:53
navar-midane_time.data
data
Definition: navar-midane_time.py:11
CREReplyPanel::getData
QList< QStringList > getData()
Definition: CREReplyPanel.cpp:63
CREReplyPanel::updateItem
void updateItem()
Definition: CREReplyPanel.cpp:111
item
Definition: item.py:1
CREReplyPanel::setText
void setText(QTreeWidgetItem *item, QStringList data)
Definition: CREReplyPanel.cpp:89
CREReplyPanel::onDeleteItem
void onDeleteItem(bool)
Definition: CREReplyPanel.cpp:78
CREReplyPanel::myType
QComboBox * myType
Definition: CREReplyPanel.h:51
connect
Definition: connect.py:1
CREReplyPanel::CREReplyPanel
CREReplyPanel(QWidget *parent)
Definition: CREReplyPanel.cpp:16
CREReplyPanel::onAddItem
void onAddItem(bool)
Definition: CREReplyPanel.cpp:68
CREReplyPanel::myReplies
QTreeWidget * myReplies
Definition: CREReplyPanel.h:45