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 #include "AssetTextEdit.h"
19 
21 {
23  settings.loadFilters(myFilters);
24 
25  setWindowTitle(tr("Filter parameters"));
26 
27  QGridLayout* layout = new QGridLayout(this);
28 
29  myList = new QListWidget(this);
30  layout->addWidget(myList, 0, 0, 4, 2);
31 
32  QPushButton* add = new QPushButton(tr("Add"), this);
33  connect(add, SIGNAL(clicked()), this, SLOT(onAdd()));
34  layout->addWidget(add, 4, 0, 1, 1);
35 
36  QPushButton* del = new QPushButton(tr("Remove"), this);
37  connect(del, SIGNAL(clicked()), this, SLOT(onDelete()));
38  layout->addWidget(del, 4, 1, 1, 1);
39 
40  layout->addWidget(new QLabel(tr("Name:"), this), 0, 2, 1, 3);
41 
42  myName = new QLineEdit(this);
43  layout->addWidget(myName, 1, 2, 1, 3);
44 
45  layout->addWidget(new QLabel(tr("Filter:"), this), 2, 2, 1, 3);
46 
47  myScript = new AssetTextEdit(this);
48  layout->addWidget(myScript, 3, 2, 1, 3);
49 
50  QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Close | QDialogButtonBox::Help, Qt::Horizontal, this);
51  connect(buttons, SIGNAL(accepted()), this, SLOT(accept()));
52  connect(buttons, SIGNAL(rejected()), this, SLOT(reject()));
53  connect(buttons, SIGNAL(helpRequested()), this, SLOT(onHelp()));
54 
55  layout->addWidget(buttons, 4, 2, 3, 1);
56 
57  setLayout(layout);
58  connect(myList, SIGNAL(currentRowChanged(int)), this, SLOT(currentRowChanged(int)));
59  refreshList();
60 }
61 
63 {
66  settings.saveFilters(myFilters);
68 }
69 
71 {
72  if (QMessageBox::question(this, tr("Discard changes?"), tr("You are about to discard all changes!\nAre you sure?"), QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
73  return;
74 
75  QDialog::reject();
76 }
77 
79 {
80  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>"));
81 }
82 
84 {
87  filter->setName(tr("<new filter>"));
88  myFilters.filters().append(filter);
89  refreshList();
90  myList->setCurrentRow(myFilters.filters().size() - 1);
91 }
92 
94 {
95  if (myFilterIndex == -1)
96  return;
97 
99  if (QMessageBox::question(this, tr("Delete filter?"), tr("Really delete filter '%1'?").arg(filter->name()), QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
100  return;
101 
102  delete filter;
103  myFilters.filters().removeAt(myFilterIndex);
104  myFilterIndex = -1;
105  refreshList();
106 }
107 
109 {
110  myList->clear();
111 
112  foreach(const CREFilterDefinition* filter, myFilters.filters())
113  {
114  myList->addItem(filter->name());
115  }
116  myFilterIndex = -1;
117 }
118 
120 {
121  if (myFilterIndex != -1)
122  {
124  filter->setName(myName->text());
125  filter->setFilter(myScript->toPlainText());
126  myList->item(myFilterIndex)->setText(filter->name());
127  }
128 }
129 
131 {
133 
134  myFilterIndex = -1;
135  if (currentRow >= 0 && currentRow < myFilters.filters().size())
136  {
137  const CREFilterDefinition* filter = myFilters.filters()[currentRow];
138  myName->setText(filter->name());
139  myScript->setText(filter->filter());
140  myFilterIndex = currentRow;
141  }
142 }
settings
struct Settings settings
Definition: init.cpp:139
CREFilterDialog::onAdd
void onAdd()
Definition: CREFilterDialog.cpp:83
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:108
CREFilterDialog.h
AssetTextEdit
Definition: AssetTextEdit.h:25
CREFilterDialog::onDelete
void onDelete()
Definition: CREFilterDialog.cpp:93
CREFilterDialog::onHelp
void onHelp()
Definition: CREFilterDialog.cpp:78
CREFilterDefinition.h
CRESettings.h
CREFilterDialog::CREFilterDialog
CREFilterDialog()
Definition: CREFilterDialog.cpp:20
CREFilterDialog::saveCurrentFilter
void saveCurrentFilter()
Definition: CREFilterDialog.cpp:119
AssetTextEdit.h
CREFilterDialog::reject
virtual void reject()
Definition: CREFilterDialog.cpp:70
CRESettings
Definition: CRESettings.h:21
CREFilterDialog::accept
virtual void accept()
Definition: CREFilterDialog.cpp:62
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
CREFilterDialog::myScript
AssetTextEdit * myScript
Definition: CREFilterDialog.h:34
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:130
CREFilterDefinitionManager::filters
QList< CREFilterDefinition * > & filters()
Definition: CREFilterDefinitionManager.cpp:41