Crossfire Server, Trunk
CREReportDialog.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 
15 #include "CREReportDialog.h"
16 #include "CRESettings.h"
17 #include "CREReportDefinition.h"
18 
20 {
22  settings.loadReports(myReports);
23 
24  setWindowTitle(tr("Report parameters"));
25 
26  QGridLayout* layout = new QGridLayout(this);
27 
28  myList = new QListWidget(this);
29  layout->addWidget(myList, 0, 0, 10, 2);
30 
31  QPushButton* add = new QPushButton(tr("Add"), this);
32  connect(add, SIGNAL(clicked()), this, SLOT(onAdd()));
33  layout->addWidget(add, 10, 0, 1, 1);
34 
35  QPushButton* del = new QPushButton(tr("Remove"), this);
36  connect(del, SIGNAL(clicked()), this, SLOT(onDelete()));
37  layout->addWidget(del, 10, 1, 1, 1);
38 
39  layout->addWidget(new QLabel(tr("Name:"), this), 0, 2, 1, 3);
40 
41  myName = new QLineEdit(this);
42  layout->addWidget(myName, 1, 2, 1, 3);
43 
44  layout->addWidget(new QLabel(tr("Header:"), this), 2, 2, 1, 3);
45 
46  myHeader = new QTextEdit(this);
47  layout->addWidget(myHeader, 3, 2, 1, 3);
48  myHeader->setWhatsThis(tr("Text to display at the top of the report."));
49 
50  layout->addWidget(new QLabel(tr("Footer:"), this), 4, 2, 1, 3);
51 
52  myFooter = new QTextEdit(this);
53  layout->addWidget(myFooter, 5, 2, 1, 3);
54  myFooter->setWhatsThis(tr("Text to display at the bottom of the report."));
55 
56  layout->addWidget(new QLabel(tr("Item sort:"), this), 6, 2, 1, 3);
57 
58  mySort = new QTextEdit(this);
59  layout->addWidget(mySort, 7, 2, 1, 3);
60  mySort->setWhatsThis(tr("Expression used to sort items. The items to be compared are 'left' and 'right', and the expression should be true if left < right, false else."));
61 
62  layout->addWidget(new QLabel(tr("Item display:"), this), 8, 2, 1, 3);
63 
64  myDisplay = new QTextEdit(this);
65  layout->addWidget(myDisplay, 9, 2, 1, 3);
66  myDisplay->setWhatsThis(tr("Expression used to display one item. The current item is 'item', and the expression should be a string value."));
67 
68  QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Close | QDialogButtonBox::Help, Qt::Horizontal, this);
69  connect(buttons, SIGNAL(accepted()), this, SLOT(accept()));
70  connect(buttons, SIGNAL(rejected()), this, SLOT(reject()));
71  connect(buttons, SIGNAL(helpRequested()), this, SLOT(onHelp()));
72 
73  layout->addWidget(buttons, 10, 2, 3, 1);
74 
75  setLayout(layout);
76  connect(myList, SIGNAL(currentRowChanged(int)), this, SLOT(currentRowChanged(int)));
77  refreshList();
78 }
79 
81 {
84  settings.saveReports(myReports);
86 }
87 
89 {
90  if (QMessageBox::question(this, tr("Discard changes?"), tr("You are about to discard all changes!\nAre you sure?"), QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
91  return;
92 
93  QDialog::reject();
94 }
95 
97 {
98  QMessageBox::information(this, tr("Report help"), tr(
99 R"(
100 This dialog allows to define a report.<br />
101 <br />
102 A report consists of an optional header, a line for each item of the view optionally sorted, an optional footer.
103 <br />
104 <br />
105 'Item sort' contains an optional script expression used to sort items. The items to be compared are 'left' and 'right', and the expression should be true if left &lt; right, false else.
106 <br />
107 <b>Example: </b><i>left.name.toLowerCase() &lt; right.name.toLowerCase()</i> will sort by ascending item's name (case unsensitive).
108 <br />
109 <br />
110 'Item display' is the expression used to display one item. The current item is 'item', and the expression should be a string value.
111 <br />
112 <b>Example: </b><i>item.name + " " + item.level</i> will display for each item its name and level.
113 )"));
114 }
115 
117 {
120  report->setName(tr("<new Report>"));
121  myReports.reports().append(report);
122  refreshList();
123  myList->setCurrentRow(myReports.reports().size() - 1);
124 }
125 
127 {
128  if (myReportIndex == -1)
129  return;
130 
132  if (QMessageBox::question(this, tr("Delete Report?"), tr("Really delete Report '%1'?").arg(report->name()), QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
133  return;
134 
135  delete report;
136  myReports.reports().removeAt(myReportIndex);
137  myReportIndex = -1;
139 }
140 
142 {
143  myList->clear();
144 
145  foreach(const CREReportDefinition* report, myReports.reports())
146  {
147  myList->addItem(report->name());
148  }
149  myReportIndex = -1;
150 }
151 
153 {
154  if (myReportIndex != -1)
155  {
157  report->setName(myName->text());
158  report->setHeader(myHeader->toPlainText());
159  report->setItemDisplay(myDisplay->toPlainText());
160  report->setItemSort(mySort->toPlainText());
161  report->setFooter(myFooter->toPlainText());
162  myList->item(myReportIndex)->setText(report->name());
163  }
164 }
165 
166 void CREReportDialog::currentRowChanged(int currentRow)
167 {
169 
170  myReportIndex = -1;
171  if (currentRow >= 0 && currentRow < myReports.reports().size())
172  {
173  const CREReportDefinition* report = myReports.reports()[currentRow];
174  myName->setText(report->name());
175  myHeader->setText(report->header());
176  myFooter->setText(report->footer());
177  mySort->setText(report->itemSort());
178  myDisplay->setText(report->itemDisplay());
179  myReportIndex = currentRow;
180  }
181 }
CREReportDialog::CREReportDialog
CREReportDialog()
Definition: CREReportDialog.cpp:19
settings
struct Settings settings
Definition: init.cpp:138
CREReportDialog::onHelp
void onHelp()
Definition: CREReportDialog.cpp:96
layout
Definition: main.cpp:84
CREReportDialog::saveCurrentReport
void saveCurrentReport()
Definition: CREReportDialog.cpp:138
report.report
def report(pl)
Definition: report.py:6
CREReportDefinition
Definition: CREReportDefinition.h:19
CREReportDialog::reject
virtual void reject()
Definition: CREReportDialog.cpp:88
CREReportDefinition.h
CRESettings.h
CREReportDialog.h
CREReportDialog::refreshList
void refreshList()
Definition: CREReportDialog.cpp:127
CREReportDialog::myHeader
QTextEdit * myHeader
Definition: CREReportDialog.h:34
CREReportDefinitionManager::reports
QList< CREReportDefinition * > & reports()
Definition: CREReportDefinitionManager.cpp:42
CREReportDialog::currentRowChanged
void currentRowChanged(int currentRow)
Definition: CREReportDialog.cpp:152
CREReportDialog::mySort
QTextEdit * mySort
Definition: CREReportDialog.h:35
CREReportDialog::myFooter
QTextEdit * myFooter
Definition: CREReportDialog.h:37
CREReportDialog::accept
virtual void accept()
Definition: CREReportDialog.cpp:80
report
Definition: report.py:1
CREReportDialog::onDelete
void onDelete()
Definition: CREReportDialog.cpp:112
CREReportDialog::myReports
CREReportDefinitionManager myReports
Definition: CREReportDialog.h:38
CRESettings
Definition: CRESettings.h:21
CREReportDialog::myReportIndex
int myReportIndex
Definition: CREReportDialog.h:39
CREReportDialog::myName
QLineEdit * myName
Definition: CREReportDialog.h:33
CREReportDialog::myList
QListWidget * myList
Definition: CREReportDialog.h:32
CREReportDialog::myDisplay
QTextEdit * myDisplay
Definition: CREReportDialog.h:36
connect
Definition: connect.py:1
CREReportDialog::onAdd
void onAdd()
Definition: CREReportDialog.cpp:102
altar_valkyrie.accept
def accept(description)
Definition: altar_valkyrie.py:22