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