Crossfire Server, Trunk
CREFilterDialog.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 "CREFilterDialog.h"
16 #include "CRESettings.h"
17 #include "CREFilterDefinition.h"
18 
20 {
22  settings.loadFilters(myFilters);
23 
24  setWindowTitle(tr("Filter parameters"));
25 
26  QGridLayout* layout = new QGridLayout(this);
27 
28  myList = new QListWidget(this);
29  layout->addWidget(myList, 0, 0, 4, 2);
30 
31  QPushButton* add = new QPushButton(tr("Add"), this);
32  connect(add, SIGNAL(clicked()), this, SLOT(onAdd()));
33  layout->addWidget(add, 4, 0, 1, 1);
34 
35  QPushButton* del = new QPushButton(tr("Remove"), this);
36  connect(del, SIGNAL(clicked()), this, SLOT(onDelete()));
37  layout->addWidget(del, 4, 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("Filter:"), this), 2, 2, 1, 3);
45 
46  myScript = new QTextEdit(this);
47  layout->addWidget(myScript, 3, 2, 1, 3);
48 
49  QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Close | QDialogButtonBox::Help, Qt::Horizontal, this);
50  connect(buttons, SIGNAL(accepted()), this, SLOT(accept()));
51  connect(buttons, SIGNAL(rejected()), this, SLOT(reject()));
52  connect(buttons, SIGNAL(helpRequested()), this, SLOT(onHelp()));
53 
54  layout->addWidget(buttons, 4, 2, 3, 1);
55 
56  setLayout(layout);
57  connect(myList, SIGNAL(currentRowChanged(int)), this, SLOT(currentRowChanged(int)));
58  refreshList();
59 }
60 
62 {
65  settings.saveFilters(myFilters);
67 }
68 
70 {
71  if (QMessageBox::question(this, tr("Discard changes?"), tr("You are about to discard all changes!\nAre you sure?"), QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
72  return;
73 
74  QDialog::reject();
75 }
76 
78 {
79  QMessageBox::information(this, tr("Filter help"), tr("Enter the script expression with which to filter items in the view. Current item is <b>item</b>, and it has the following properties:<br /><ul><li>for an archetype: name, clone, head, more<ul><li>clone has the following properties: name, race, type, level, isMonster, isAlive, experience, attacktype, ac, wc, </li><li>head and more are archetypes if not null</li></ul></li><li>for a formulae: title, chance, difficulty, archs</li><li>for an artifact: item, chance, difficulty, allowed</li></ul><br />An item is shown if the expression evaluates to <i>true</i>. If a property is not defined for the current item, it will not be shown.<br /><br />Examples:<ul><li>archetypes of type 5: <i>item.clone.type == 5</i></li><li>artifact allowed for all items of the type: <i>item.allowed.length == 0</i></il></ul>"));
80 }
81 
83 {
86  filter->setName(tr("<new filter>"));
87  myFilters.filters().append(filter);
88  refreshList();
89  myList->setCurrentRow(myFilters.filters().size() - 1);
90 }
91 
93 {
94  if (myFilterIndex == -1)
95  return;
96 
98  if (QMessageBox::question(this, tr("Delete filter?"), tr("Really delete filter '%1'?").arg(filter->name()), QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
99  return;
100 
101  delete filter;
102  myFilters.filters().removeAt(myFilterIndex);
103  myFilterIndex = -1;
104  refreshList();
105 }
106 
108 {
109  myList->clear();
110 
111  foreach(const CREFilterDefinition* filter, myFilters.filters())
112  {
113  myList->addItem(filter->name());
114  }
115  myFilterIndex = -1;
116 }
117 
119 {
120  if (myFilterIndex != -1)
121  {
123  filter->setName(myName->text());
124  filter->setFilter(myScript->toPlainText());
125  myList->item(myFilterIndex)->setText(filter->name());
126  }
127 }
128 
130 {
132 
133  myFilterIndex = -1;
134  if (currentRow >= 0 && currentRow < myFilters.filters().size())
135  {
136  const CREFilterDefinition* filter = myFilters.filters()[currentRow];
137  myName->setText(filter->name());
138  myScript->setText(filter->filter());
139  myFilterIndex = currentRow;
140  }
141 }
settings
struct Settings settings
Definition: init.cpp:139
CREFilterDialog::onAdd
void onAdd()
Definition: CREFilterDialog.cpp:82
layout
Definition: main.cpp:84
CREFilterDialog::myFilters
CREFilterDefinitionManager myFilters
Definition: CREFilterDialog.h:35
filter
Definition: filter.py:1
CREFilterDialog::refreshList
void refreshList()
Definition: CREFilterDialog.cpp:107
CREFilterDialog.h
CREFilterDialog::onDelete
void onDelete()
Definition: CREFilterDialog.cpp:92
CREFilterDialog::onHelp
void onHelp()
Definition: CREFilterDialog.cpp:77
CREFilterDefinition.h
CRESettings.h
CREFilterDialog::CREFilterDialog
CREFilterDialog()
Definition: CREFilterDialog.cpp:19
CREFilterDialog::saveCurrentFilter
void saveCurrentFilter()
Definition: CREFilterDialog.cpp:118
CREFilterDialog::reject
virtual void reject()
Definition: CREFilterDialog.cpp:69
CRESettings
Definition: CRESettings.h:21
CREFilterDialog::accept
virtual void accept()
Definition: CREFilterDialog.cpp:61
CREFilterDialog::myFilterIndex
int myFilterIndex
Definition: CREFilterDialog.h:36
CREFilterDefinition
Definition: CREFilterDefinition.h:19
CREFilterDialog::myName
QLineEdit * myName
Definition: CREFilterDialog.h:33
CREFilterDialog::myList
QListWidget * myList
Definition: CREFilterDialog.h:32
connect
Definition: connect.py:1
altar_valkyrie.accept
def accept(description)
Definition: altar_valkyrie.py:22
CREFilterDialog::currentRowChanged
void currentRowChanged(int currentRow)
Definition: CREFilterDialog.cpp:129
CREFilterDialog::myScript
QTextEdit * myScript
Definition: CREFilterDialog.h:34
CREFilterDefinitionManager::filters
QList< CREFilterDefinition * > & filters()
Definition: CREFilterDefinitionManager.cpp:41