Crossfire Server, Trunk
CREPlayerRepliesPanel.cpp
Go to the documentation of this file.
1 #include <QtWidgets>
3 extern "C"
4 {
5 #include "global.h"
6 #include "dialog.h"
7 #include "sproto.h"
8 }
9 
13 class CREMessageTypeDelegate : public QStyledItemDelegate
14 {
15 public:
16  CREMessageTypeDelegate(QObject* parent) : QStyledItemDelegate(parent)
17  {
18  }
19 
20  virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex&) const override
21  {
22  QComboBox* box = new QComboBox(parent);
23  box->addItem(get_reply_text_own(rt_say), QString::number(static_cast<int>(rt_say)));
24  box->addItem(get_reply_text_own(rt_reply), QString::number(static_cast<int>(rt_reply)));
25  box->addItem(get_reply_text_own(rt_question), QString::number(static_cast<int>(rt_question)));
26  return box;
27  }
28 
29  virtual void setEditorData(QWidget* editor, const QModelIndex& index) const override
30  {
31  QComboBox* box = qobject_cast<QComboBox*>(editor);
32  if (!box)
33  return;
34 
35  box->setCurrentIndex(box->findData(index.data(Qt::UserRole).toString()));
36  }
37 
38  virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override
39  {
40  QComboBox* box = qobject_cast<QComboBox*>(editor);
41  if (!box)
42  return;
43  model->setData(index, box->currentData(Qt::UserRole).toString(), Qt::UserRole);
44  }
45 
46  virtual void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex&) const override
47  {
48  editor->setGeometry(option.rect);
49  QComboBox* box = qobject_cast<QComboBox*>(editor);
50  if (!box)
51  return;
52  box->showPopup();
53  }
54 };
55 
56 CREPlayerRepliesPanel::CREPlayerRepliesPanel(QWidget* parent) : QDialog(parent)
57 {
58  setWindowTitle(tr("Player suggested replies"));
59 
60  myReplies = new QTableWidget(this);
61  myReplies->setColumnCount(3);
62  myReplies->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
63  myReplies->setItemDelegateForColumn(2, new CREMessageTypeDelegate(this));
64 
65  QPushButton* addReply = new QPushButton(tr("add reply"), this);
66  connect(addReply, SIGNAL(clicked(bool)), this, SLOT(onAddCondition(bool)));
67 
68  QPushButton* deleteReply = new QPushButton(tr("delete reply"), this);
69  connect(deleteReply, SIGNAL(clicked(bool)), this, SLOT(onDeleteCondition(bool)));
70 
71  QPushButton* reset = new QPushButton(tr("reset changes"), this);
72  connect(reset, SIGNAL(clicked(bool)), this, SLOT(onReset(bool)));
73 
74  QHBoxLayout* buttons = new QHBoxLayout();
75  buttons->addWidget(addReply);
76  buttons->addWidget(deleteReply);
77  buttons->addWidget(reset);
78 
79  QVBoxLayout* l = new QVBoxLayout(this);
80  l->addWidget(myReplies);
81  l->addLayout(buttons);
82  setLayout(l);
83 }
84 
86 {
87 }
88 
89 const char* convertType(const QString& type)
90 {
91  int i = type.toInt();
92  if (i < 0 || i > rt_question)
93  i = 0;
94  return get_reply_text_own(static_cast<reply_type>(i));
95 }
96 
97 QTableWidgetItem* createItem(const QString& text, const QString& edit)
98 {
99  QTableWidgetItem* wi = new QTableWidgetItem(text);
100  wi->setData(Qt::UserRole, edit);
101  wi->setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
102  return wi;
103 }
104 
105 void CREPlayerRepliesPanel::addItem(const QStringList& item)
106 {
107  int row = myReplies->rowCount();
108  myReplies->setRowCount(row + 1);
109  myReplies->setItem(row, 0, createItem(item[0], item[0]));
110  myReplies->setItem(row, 1, createItem(item[1], item[1]));
111  myReplies->setItem(row, 2, createItem(convertType(item.size() > 2 ? item[2] : "0"), item.size() > 2 ? item[2] : "0"));
112 }
113 
114 void CREPlayerRepliesPanel::setData(const QList<QStringList>& data)
115 {
116  myReplies->clearContents();
117  myReplies->setRowCount(0);
118  myReplies->setHorizontalHeaderLabels(QStringList() << "What the player should say" << "What the player will be displayed as saying" << "Message type");
119  for (QStringList item : data)
120  {
121  addItem(item);
122  }
123  myOriginal = data;
124 }
125 
126 QList<QStringList> CREPlayerRepliesPanel::data() const
127 {
128  QList<QStringList> value;
129  for (int r = 0; r < myReplies->rowCount(); r++)
130  {
131  QStringList i;
132  i << myReplies->item(r, 0)->data(Qt::DisplayRole).toString();
133  i << myReplies->item(r, 1)->data(Qt::DisplayRole).toString();
134  i << myReplies->item(r, 2)->data(Qt::UserRole).toString();
135  value.append(i);
136  }
137  return value;
138 }
139 
141 {
142  QStringList item;
143  item << "*" << "?" << "0";
144  addItem(item);
145  myReplies->setCurrentItem(myReplies->item(myReplies->rowCount() - 1, 0));
146 }
147 
149 {
150  if (myReplies->currentRow() == -1)
151  return;
152 
153  myReplies->removeRow(myReplies->currentRow());
154 }
155 
157 {
158  if (QMessageBox::question(this, "Confirm reset", "Reset the replies to the initial values of the message?") != QMessageBox::StandardButton::Yes)
159  return;
161 }
global.h
banquet.l
l
Definition: banquet.py:164
CREPlayerRepliesPanel::data
QList< QStringList > data() const
Definition: CREPlayerRepliesPanel.cpp:126
CREPlayerRepliesPanel.h
CREPlayerRepliesPanel::setData
void setData(const QList< QStringList > &data)
Definition: CREPlayerRepliesPanel.cpp:114
CREPlayerRepliesPanel::myOriginal
QList< QStringList > myOriginal
Definition: CREPlayerRepliesPanel.h:31
addReply
static PyObject * addReply(PyObject *self, PyObject *args)
Definition: cfpython.c:668
CREMessageTypeDelegate::createEditor
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const override
Definition: CREPlayerRepliesPanel.cpp:20
CREPlayerRepliesPanel::onDeleteCondition
void onDeleteCondition(bool)
Definition: CREPlayerRepliesPanel.cpp:148
CREMessageTypeDelegate::updateEditorGeometry
virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const override
Definition: CREPlayerRepliesPanel.cpp:46
CREMessageTypeDelegate
Definition: CREPlayerRepliesPanel.cpp:13
convertType
const char * convertType(const QString &type)
Definition: CREPlayerRepliesPanel.cpp:89
CREMessageTypeDelegate::setModelData
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
Definition: CREPlayerRepliesPanel.cpp:38
navar-midane_time.data
data
Definition: navar-midane_time.py:11
rt_say
@ rt_say
Definition: dialog.h:10
say.box
box
Definition: say.py:152
sproto.h
rt_reply
@ rt_reply
Definition: dialog.h:11
CREMessageTypeDelegate::CREMessageTypeDelegate
CREMessageTypeDelegate(QObject *parent)
Definition: CREPlayerRepliesPanel.cpp:16
CREPlayerRepliesPanel::~CREPlayerRepliesPanel
virtual ~CREPlayerRepliesPanel()
Definition: CREPlayerRepliesPanel.cpp:85
CREPlayerRepliesPanel::onReset
void onReset(bool)
Definition: CREPlayerRepliesPanel.cpp:156
guild_entry.text
text
Definition: guild_entry.py:44
createItem
QTableWidgetItem * createItem(const QString &text, const QString &edit)
Definition: CREPlayerRepliesPanel.cpp:97
reply_type
reply_type
Definition: dialog.h:9
dialog.h
get_reply_text_own
const char * get_reply_text_own(reply_type rt)
Definition: monster.c:2278
CREPlayerRepliesPanel::CREPlayerRepliesPanel
CREPlayerRepliesPanel(QWidget *parent)
Definition: CREPlayerRepliesPanel.cpp:56
item
Definition: item.py:1
rt_question
@ rt_question
Definition: dialog.h:12
autojail.value
value
Definition: autojail.py:6
CFweardisguise.option
option
Definition: CFweardisguise.py:16
npc_dialog.index
int index
Definition: npc_dialog.py:102
CREMessageTypeDelegate::setEditorData
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override
Definition: CREPlayerRepliesPanel.cpp:29
CREPlayerRepliesPanel::onAddCondition
void onAddCondition(bool)
Definition: CREPlayerRepliesPanel.cpp:140
say.item
dictionary item
Definition: say.py:149
CREPlayerRepliesPanel::myReplies
QTableWidget * myReplies
Definition: CREPlayerRepliesPanel.h:30
connect
Definition: connect.py:1
ring_occidental_mages.r
r
Definition: ring_occidental_mages.py:6
CREPlayerRepliesPanel::addItem
void addItem(const QStringList &item)
Definition: CREPlayerRepliesPanel.cpp:105
is_valid_types_gen.type
list type
Definition: is_valid_types_gen.py:25