Crossfire Server, Trunk  R20513
CREFilterDialog.cpp
Go to the documentation of this file.
1 #include <QtGui>
2 
3 #include "CREFilterDialog.h"
4 #include "CREFilter.h"
5 #include "CRESettings.h"
6 #include "CREFilterDefinition.h"
7 
9 {
11  settings.loadFilters(myFilters);
12 
13  setWindowTitle(tr("Filter parameters"));
14 
15  QGridLayout* layout = new QGridLayout(this);
16 
17  myList = new QListWidget(this);
18  layout->addWidget(myList, 0, 0, 4, 2);
19 
20  QPushButton* add = new QPushButton(tr("Add"), this);
21  connect(add, SIGNAL(clicked()), this, SLOT(onAdd()));
22  layout->addWidget(add, 4, 0, 1, 1);
23 
24  QPushButton* del = new QPushButton(tr("Remove"), this);
25  connect(del, SIGNAL(clicked()), this, SLOT(onDelete()));
26  layout->addWidget(del, 4, 1, 1, 1);
27 
28  layout->addWidget(new QLabel(tr("Name:"), this), 0, 2, 1, 3);
29 
30  myName = new QLineEdit(this);
31  layout->addWidget(myName, 1, 2, 1, 3);
32 
33  layout->addWidget(new QLabel(tr("Filter:"), this), 2, 2, 1, 3);
34 
35  myScript = new QTextEdit(this);
36  layout->addWidget(myScript, 3, 2, 1, 3);
37 
38  QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Close | QDialogButtonBox::Help, Qt::Horizontal, this);
39  connect(buttons, SIGNAL(accepted()), this, SLOT(accept()));
40  connect(buttons, SIGNAL(rejected()), this, SLOT(reject()));
41  connect(buttons, SIGNAL(helpRequested()), this, SLOT(onHelp()));
42 
43  layout->addWidget(buttons, 4, 2, 3, 1);
44 
45  setLayout(layout);
46  connect(myList, SIGNAL(currentRowChanged(int)), this, SLOT(currentRowChanged(int)));
47  refreshList();
48 }
49 
51 {
54  settings.saveFilters(myFilters);
55  QDialog::accept();
56 }
57 
59 {
60  if (QMessageBox::question(this, tr("Discard changes?"), tr("You are about to discard all changes!\nAre you sure?"), QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
61  return;
62 
63  QDialog::reject();
64 }
65 
67 {
68  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, race, type, level, isMonster, isAlive, experience, attacktype, ac, wc, arch (with a name property)</li><li>for a formulae: title, chance, difficulty, archs</li><li>for an artifact: item, chance, difficulty, allowed</li><li>for an object (for clone and item): type</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>items of type 5: <i>item.type == 5</i></li><li>artifact allowed for all items of the type: <i>item.allowed.length == 0</i></il></ul>"));
69 }
70 
72 {
75  filter->setName(tr("<new filter>"));
76  myFilters.filters().append(filter);
77  refreshList();
78  myList->setCurrentRow(myFilters.filters().size() - 1);
79 }
80 
82 {
83  if (myFilterIndex == -1)
84  return;
85 
87  if (QMessageBox::question(this, tr("Delete filter?"), tr("Really delete filter '%1'?").arg(filter->name()), QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
88  return;
89 
90  delete filter;
91  myFilters.filters().removeAt(myFilterIndex);
92  myFilterIndex = -1;
93  refreshList();
94 }
95 
97 {
98  myList->clear();
99 
100  foreach(const CREFilterDefinition* filter, myFilters.filters())
101  {
102  myList->addItem(filter->name());
103  }
104  myFilterIndex = -1;
105 }
106 
108 {
109  if (myFilterIndex != -1)
110  {
112  filter->setName(myName->text());
113  filter->setFilter(myScript->toPlainText());
114  myList->item(myFilterIndex)->setText(filter->name());
115  }
116 }
117 
119 {
121 
122  myFilterIndex = -1;
123  if (currentRow >= 0 && currentRow < myFilters.filters().size())
124  {
125  const CREFilterDefinition* filter = myFilters.filters()[currentRow];
126  myName->setText(filter->name());
127  myScript->setText(filter->filter());
128  myFilterIndex = currentRow;
129  }
130 }
const QString & name() const
void saveFilters(const CREFilterDefinitionManager &filters)
Definition: CRESettings.cpp:53
const QString & filter() const
void setName(const QString &name)
virtual void accept()
QLineEdit * myName
virtual void reject()
QList< CREFilterDefinition * > & filters()
QTextEdit * myScript
void setFilter(const QString &filter)
CREFilterDefinitionManager myFilters
void currentRowChanged(int currentRow)
struct Settings settings
Server settings.
Definition: init.c:40
QListWidget * myList
void loadFilters(CREFilterDefinitionManager &filters)
Definition: CRESettings.cpp:48
Definition: main.c:88