1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
/*
* Copyright 2008 Ben Boeckel <MathStuf@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Header include
#include "ObjectUI.h"
// Pokemod includes
#include "../pokemod/Object.h"
#include "../pokemod/Pokemod.h"
// Qt includes
#include <QtGui/QLabel>
#include <QtGui/QTreeWidget>
#include <QtGui/QVBoxLayout>
// KDE includes
#include <KAction>
#include <KColorScheme>
#include <KDialog>
#include <KMenu>
#include <KMessageBox>
#include <KProgressDialog>
// #include <KStatefulBrush>
Pokemodr::ObjectUI::ObjectUI(QWidget* parent) :
QWidget(parent),
m_changed(false),
m_object(NULL),
m_object_mod(NULL),
m_valTree(NULL)
{
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(changed(bool)), SLOT(setChanged(bool)));
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(contextMenu(const QPoint&)));
}
Pokemodr::ObjectUI::~ObjectUI()
{
delete m_object_mod;
}
void Pokemodr::ObjectUI::reload()
{
refreshGui();
setGui();
}
bool Pokemodr::ObjectUI::isChanged() const
{
return m_changed;
}
const Pokemod::Pokemod* Pokemodr::ObjectUI::pokemod() const
{
return qobject_cast<const Pokemod::Pokemod*>(m_object->pokemod());
}
const Pokemod::Pokemod::Object* Pokemodr::ObjectUI::original() const
{
return m_object;
}
Pokemod::Object* Pokemodr::ObjectUI::original()
{
return m_object;
}
const Pokemod::Object* Pokemodr::ObjectUI::modified() const
{
return m_object_mod;
}
Pokemod::Object* Pokemodr::ObjectUI::modified()
{
return m_object_mod;
}
void Pokemodr::ObjectUI::setChanged(const bool changed)
{
m_changed = changed;
}
void Pokemodr::ObjectUI::errorMessage(const QString& message)
{
KMessageBox::error(this, message, "Error");
}
void Pokemodr::ObjectUI::warningMessage(const QString& message)
{
KMessageBox::warningContinueCancel(this, message, "Warning");
}
void Pokemodr::ObjectUI::initGui()
{
}
void Pokemodr::ObjectUI::refreshGui()
{
}
void Pokemodr::ObjectUI::contextMenu(const QPoint& pos)
{
KMenu* menu = new KMenu;
menu->addAction("&Apply", this, SLOT(apply()));
menu->addAction("&Discard", this, SLOT(discard()));
menu->addSeparator();
KAction* validate = new KAction("&Validate", this);
connect(validate, SIGNAL(triggered()), this, SLOT(validate()));
validate->setEnabled(!m_changed);
menu->addAction(validate);
menu->popup(mapToGlobal(pos));
}
void Pokemodr::ObjectUI::validate()
{
KProgressDialog* progress = new KProgressDialog(this, "Validating", "Please wait");
progress->progressBar()->setRange(0, 0);
progress->setAllowCancel(false);
progress->setModal(true);
progress->show();
if (!m_valTree)
m_valTree = new QTreeWidget;
m_valTree->setHeaderHidden(true);
m_parents.clear();
m_warnings = 0;
m_errors = 0;
m_valTree->clear();
connect(m_object, SIGNAL(valMessage(const QString&)), this, SLOT(addMessage(const QString&)));
connect(m_object, SIGNAL(valWarning(const QString&)), this, SLOT(addWarning(const QString&)));
connect(m_object, SIGNAL(valError(const QString&)), this, SLOT(addError(const QString&)));
m_object->validate();
delete progress;
KDialog* dialog = new KDialog(this);
dialog->setCaption("Validation Messages");
dialog->setButtons(KDialog::Ok);
QWidget* widget = new QWidget(this);
QVBoxLayout* layout = new QVBoxLayout(widget);
layout->addWidget(new QLabel(QString("Warnings: %1\nErrors: %2").arg(m_warnings).arg(m_errors), widget));
layout->addWidget(m_valTree);
dialog->setMainWidget(widget);
dialog->exec();
delete dialog;
m_valTree = NULL;
disconnect(m_object, SIGNAL(valMessage(const QString&)), this, SLOT(addMessage(const QString&)));
disconnect(m_object, SIGNAL(valWarning(const QString&)), this, SLOT(addWarning(const QString&)));
disconnect(m_object, SIGNAL(valError(const QString&)), this, SLOT(addError(const QString&)));
}
void Pokemodr::ObjectUI::addMessage(const QString& msg)
{
QTreeWidgetItem* item;
if (msg.startsWith("++"))
{
if (m_parents.size())
item = new QTreeWidgetItem(m_parents.top(), QStringList(msg.mid(2)));
else
item = new QTreeWidgetItem(m_valTree, QStringList(msg.mid(2)));
m_parents.push(item);
}
else if (msg.startsWith("--"))
m_parents.pop();
else
{
item = new QTreeWidgetItem(m_parents.top(), QStringList(msg));
item->setBackground(0, KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground).brush(QPalette::Normal));
}
}
void Pokemodr::ObjectUI::addWarning(const QString& msg)
{
++m_warnings;
QTreeWidgetItem* item;
item = new QTreeWidgetItem(m_parents.top(), QStringList(msg));
item->setBackground(0, KStatefulBrush(KColorScheme::View, KColorScheme::NeutralBackground).brush(QPalette::Normal));
}
void Pokemodr::ObjectUI::addError(const QString& msg)
{
++m_errors;
QTreeWidgetItem* item;
item = new QTreeWidgetItem(m_parents.top(), QStringList(msg));
item->setBackground(0, KStatefulBrush(KColorScheme::View, KColorScheme::NegativeBackground).brush(QPalette::Normal));
}
void Pokemodr::ObjectUI::setObjects(Pokemod::Object* original, Pokemod::Object* modified)
{
m_object = original;
m_object_mod = modified;
connect(m_object_mod, SIGNAL(changed()), this, SIGNAL(changed()));
connect(m_object_mod, SIGNAL(changed()), this, SLOT(setGui()));
connect(m_object_mod, SIGNAL(error(const QString&)), this, SLOT(setGui()));
connect(m_object_mod, SIGNAL(error(const QString&)), this, SLOT(errorMessage(const QString&)));
connect(m_object_mod, SIGNAL(warning(const QString&)), this, SLOT(warningMessage(const QString&)));
init();
}
void Pokemodr::ObjectUI::init()
{
initGui();
reload();
emit(changed(false));
}
|