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