Crossfire Server, Trunk
CREMessagePanel.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 "CREMessagePanel.h"
15 #include "CREFilterDefinition.h"
16 #include "MessageFile.h"
17 #include "CREMapInformation.h"
18 #include "MessageManager.h"
19 #include "CREMessageItemModel.h"
23 #include "CREStringListDelegate.h"
24 
26 {
27  Q_ASSERT(manager != NULL);
29 
30  addTab(tr("Details"));
31 
32  myPath = addLineEdit(tr("Path:"), "path", false);
33  addLineEdit(tr("Location:"), "location", false);
34 
35  myModel = new CREMessageItemModel(this);
36  myRules = new QTableView();
37  myRules->setWordWrap(true);
38  myRules->setModel(myModel);
39  myRules->setSelectionMode(QAbstractItemView::SingleSelection);
40  myRules->setSelectionBehavior(QAbstractItemView::SelectRows);
41  myRules->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked);
42  myRules->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
43 
44  myRules->setItemDelegateForColumn(0, new CREMultilineItemDelegate(myRules, true, true));
46  myRules->setItemDelegateForColumn(2, new CREPlayerRepliesDelegate(myRules));
47  myRules->setItemDelegateForColumn(3, new CREStringListDelegate(myRules));
49  myRules->setItemDelegateForColumn(5, new CREMultilineItemDelegate(myRules, true, true));
50  myRules->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
51 
52  connect(myRules->selectionModel(), SIGNAL(currentRowChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(currentRowChanged(const QModelIndex&, const QModelIndex&)));
53  addWidget(tr("Message rules (blue: uses token set by current rule as pre-condition, red: rule that sets token for pre-condition of current rule)"), myRules, false, nullptr, nullptr);
54 
55  QHBoxLayout* buttons = new QHBoxLayout();
56 
57  QPushButton* add = new QPushButton(tr("insert rule"), this);
58  buttons->addWidget(add);
59  connect(add, SIGNAL(clicked(bool)), this, SLOT(onAddRule(bool)));
60  QPushButton* remove = new QPushButton(tr("remove rule"), this);
61  buttons->addWidget(remove);
62  connect(remove, SIGNAL(clicked(bool)), this, SLOT(onDeleteRule(bool)));
63 
64  QPushButton* up = new QPushButton(tr("move up"), this);
65  buttons->addWidget(up);
66  connect(up, SIGNAL(clicked(bool)), this, SLOT(onMoveUp(bool)));
67  QPushButton* down = new QPushButton(tr("move down"), this);
68  buttons->addWidget(down);
69  connect(down, SIGNAL(clicked(bool)), this, SLOT(onMoveDown(bool)));
70 
71  QPushButton* copy = new QPushButton(tr("copy"), this);
72  buttons->addWidget(copy);
73  connect(copy, SIGNAL(clicked(bool)), this, SLOT(onDuplicate(bool)));
74 
75  QPushButton* reset = new QPushButton(tr("reset all changes"), this);
76  buttons->addWidget(reset);
77  connect(reset, SIGNAL(clicked(bool)), this, SLOT(onReset(bool)));
78 
79  myLayout->addLayout(buttons, myLayout->rowCount(), 0, 1, 2);
80 
81  addTab(tr("Use"));
82  myUse = new QTreeWidget(this);
83  addWidget(tr("Use"), myUse, false, nullptr, nullptr);
84 
85  myOriginal = nullptr;
86 }
87 
89 {
90  delete myOriginal;
91 }
92 
94 {
95  if (!myItem) {
96  return;
97  }
98  /* can only change path when new file is created */
99  myPath->setReadOnly(myItem->path() != "<new file>");
100 
101  /* so the change handler won't do anything */
102  auto save = myItem;
103  myItem = NULL;
104  myModel->setMessage(save);
105  myItem = save;
106 
107  myUse->clear();
108 
109  QTreeWidgetItem* root = NULL;
110  if (myItem->maps().length() > 0)
111  {
112  root = new QTreeWidgetItem(myUse, QStringList(tr("Maps")));
113  root->setExpanded(true);
114  foreach(CREMapInformation* map, myItem->maps())
115  {
116  new QTreeWidgetItem(root, QStringList(map->path()));
117  }
118  root = NULL;
119  }
120 
122  {
123  bool got = false;
124  foreach(MessageRule* rule, file->rules())
125  {
126  QStringList includes = rule->include();
127  foreach(QString include, includes)
128  {
129  if (include.isEmpty())
130  continue;
131 
132  if (!include.startsWith('/'))
133  {
134  int last = file->path().lastIndexOf(QDir::separator());
135  if (last == -1)
136  continue;
137  include = file->path().left(last + 1) + include;
138  }
139 
140  if (include == myItem->path())
141  {
142  if (root == NULL)
143  {
144  root = new QTreeWidgetItem(myUse, QStringList(tr("Messages")));
145  root->setExpanded(true);
146  }
147 
148  new QTreeWidgetItem(root, QStringList(file->path()));
149 
150  got = true;
151  break;
152  }
153 
154  if (got)
155  break;
156  }
157  }
158 
159  if (got)
160  break;
161  }
162 
163  delete myOriginal;
165 }
166 
167 void CREMessagePanel::currentRowChanged(const QModelIndex& current, const QModelIndex& /*previous*/)
168 {
170 }
171 
173 {
174  int row = myRules->selectionModel()->currentIndex().row() + 1;
175  myModel->insertRows(row, 1);
176  myRules->selectionModel()->select(myModel->index(row, 0, QModelIndex()), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
177 }
178 
180 {
181  myModel->removeRows(myRules->selectionModel()->currentIndex().row(), 1);
182 }
183 
185 {
186  int index = myRules->currentIndex().row();
187  if (index <= 0 || index >= myItem->rules().size())
188  return;
189 
190  myModel->moveUpDown(index, true);
191  myRules->setCurrentIndex(myModel->index(index - 1, 0, QModelIndex()));
192 }
193 
195 {
196  int index = myRules->currentIndex().row();
197  if (index < 0 || index >= myItem->rules().size() - 1)
198  return;
199 
200  myModel->moveUpDown(index, false);
201  myRules->setCurrentIndex(myModel->index(index + 1, 0, QModelIndex()));
202 }
203 
205 {
206  int index = myRules->currentIndex().row();
207  if (index < 0 || index >= myItem->rules().size())
208  return;
209 
211  myRules->setCurrentIndex(myModel->index(index + 1, 0, QModelIndex()));
212 }
213 
215 {
216  if (!myItem)
217  return;
218  if (QMessageBox::question(this, "Confirm reset", "Reset message to its initial values?") != QMessageBox::StandardButton::Yes)
219  return;
220 
222  updateItem();
223 }
CREMessagePanel::myPath
QLineEdit * myPath
Definition: CREMessagePanel.h:44
MessageFile::path
QString path
Definition: MessageFile.h:71
CREMessagePanel.h
CREStringListDelegate
Definition: CREStringListDelegate.h:21
MessageFile::copy
void copy(const MessageFile *other)
Definition: MessageFile.cpp:147
CREMessageItemModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const override
Definition: CREMessageItemModel.cpp:143
sunnista.got
def got
Definition: sunnista.py:87
CREMessagePanel::currentRowChanged
void currentRowChanged(const QModelIndex &current, const QModelIndex &previous)
Definition: CREMessagePanel.cpp:167
CREMessageItemModel::insertRows
virtual bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
Definition: CREMessageItemModel.cpp:280
AssetWrapperPanel::addTab
void addTab(const QString &title)
Definition: AssetWrapperPanel.cpp:36
MessageManager.h
AssetWrapperPanel::addLineEdit
QLineEdit * addLineEdit(const QString &label, const char *property, bool readOnly=true)
Definition: AssetWrapperPanel.cpp:74
CREMapInformation.h
MessageFile::maps
QList< CREMapInformation * > & maps()
Definition: MessageFile.cpp:271
MessageFile::duplicate
MessageFile * duplicate() const
Definition: MessageFile.cpp:137
PrePostWidget::PostConditions
@ PostConditions
Definition: CREPrePostList.h:27
CREMessagePanel::updateItem
virtual void updateItem() override
Definition: CREMessagePanel.cpp:93
CREMessageItemModel::setMessage
void setMessage(MessageFile *message)
Definition: CREMessageItemModel.cpp:25
CREPrePostConditionDelegate
Definition: CREPrePostConditionDelegate.h:47
CREMessagePanel::myModel
CREMessageItemModel * myModel
Definition: CREMessagePanel.h:46
root
static char root[500]
Definition: mapper.cpp:304
mad_mage_user.file
file
Definition: mad_mage_user.py:15
AssetSWrapperPanel< MessageFile >::myItem
MessageFile * myItem
Definition: AssetWrapperPanel.h:126
MessageManager::messages
QList< MessageFile * > & messages()
Definition: MessageManager.cpp:51
CREMessageItemModel::moveUpDown
void moveUpDown(int row, bool up)
Definition: CREMessageItemModel.cpp:316
CREMessagePanel::onDuplicate
void onDuplicate(bool)
Definition: CREMessagePanel.cpp:204
CREMessagePanel::myUse
QTreeWidget * myUse
Definition: CREMessagePanel.h:48
MessageFile
Definition: MessageFile.h:68
PrePostWidget::PreConditions
@ PreConditions
Definition: CREPrePostList.h:27
CREMessagePanel::onDeleteRule
void onDeleteRule(bool)
Definition: CREMessagePanel.cpp:179
CREMapInformation
Definition: CREMapInformation.h:27
CREMessagePanel::myRules
QTableView * myRules
Definition: CREMessagePanel.h:45
disinfect.map
map
Definition: disinfect.py:4
CREFilterDefinition.h
AssetWrapperPanel::myLayout
QGridLayout * myLayout
Definition: AssetWrapperPanel.h:56
CREMessageItemModel::removeRows
virtual bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
Definition: CREMessageItemModel.cpp:297
CREMessagePanel::~CREMessagePanel
virtual ~CREMessagePanel()
Definition: CREMessagePanel.cpp:88
CREStringListDelegate.h
CREPlayerRepliesDelegate.h
CREMessagePanel::myOriginal
MessageFile * myOriginal
Definition: CREMessagePanel.h:43
MessageManager
Definition: MessageManager.h:25
CREMessageItemModel
Definition: CREMessageItemModel.h:26
CREMessagePanel::CREMessagePanel
CREMessagePanel(const MessageManager *manager, QWidget *parent)
Definition: CREMessagePanel.cpp:25
MessageFile::rules
QList< MessageRule * > & rules()
Definition: MessageFile.cpp:266
AssetSWrapperPanel
Definition: AssetWrapperPanel.h:113
CREPlayerRepliesDelegate
Definition: CREPlayerRepliesDelegate.h:21
MessageFile.h
CREMultilineItemDelegate.h
CREMessageItemModel::setSelectedRule
void setSelectedRule(const QModelIndex &index)
Definition: CREMessageItemModel.cpp:32
AssetWrapperPanel::addWidget
T * addWidget(const QString &label, T *widget, bool sideBySide, const char *property, const char *widgetProperty)
Definition: AssetWrapperPanel.h:64
npc_dialog.index
int index
Definition: npc_dialog.py:102
CREMessagePanel::onMoveUp
void onMoveUp(bool)
Definition: CREMessagePanel.cpp:184
CREMessagePanel::onReset
void onReset(bool)
Definition: CREMessagePanel.cpp:214
CREMessagePanel::onAddRule
void onAddRule(bool)
Definition: CREMessagePanel.cpp:172
CREMessagePanel::onMoveDown
void onMoveDown(bool)
Definition: CREMessagePanel.cpp:194
CREPrePostConditionDelegate.h
connect
Definition: connect.py:1
CREMultilineItemDelegate
Definition: CREMultilineItemDelegate.h:19
replace.current
current
Definition: replace.py:64
npc_dialog.rule
rule
Definition: npc_dialog.py:108
CREMessagePanel::myMessageManager
const MessageManager * myMessageManager
Definition: CREMessagePanel.h:42
CREMessageItemModel::duplicateRow
void duplicateRow(int row)
Definition: CREMessageItemModel.cpp:333
manager
static AssetsManager * manager
Definition: assets.cpp:60
CREMessageItemModel.h
MessageRule
Definition: MessageFile.h:25