summaryrefslogtreecommitdiffstats
path: root/pokemodr/PokemodrUI.cpp
blob: 31c3567b8090a93552c8ec8e9a9210d89eb59dbe (plain)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
 * 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 "PokemodrUI.h"

// Pokemodr includes
#include "ObjectUI.h"
#include "PokemodrPreferences.h"
#include "PokemodTreeModel.h"

// Pokemod includes
#include "../pokemod/Pokemod.h"
#include "../pokemod/Rules.h"

// Qt includes
#include <QtCore/QString>
#include <QtGui/QCloseEvent>

// KDE includes
#include <KAction>
#include <KActionCollection>
#include <KCmdLineArgs>
#include <KFileDialog>
#include <KLocalizedString>
#include <KMenu>
#include <KMenuBar>
#include <KMessageBox>
#include <KRecentFilesAction>
#include <KStandardAction>
#include <KStandardDirs>
#include <KTemporaryFile>
#include <KToolBar>
#include <KUrl>
#include <KIO/NetAccess>

Pokemodr::PokemodrUI::PokemodrUI(QWidget* parent) :
        KMainWindow(parent),
        m_config(KGlobal::config()->group("pokemodr"))
{
    setupUi(this);
    setupActions();
    splitter->setSizes(QList<int>() << m_config.readEntry("treeWidth", 100) << m_config.readEntry("panelWidth", 100));
    connect(buttonApply, SIGNAL(clicked()), this, SLOT(update()));
    KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
    if (args && args->count())
    {
        for(int i = 0; i < args->count(); ++i)
            openPokemod(args->url(i));
        args->clear();
    }
    else if (m_config.readEntry("reloadOnStart", false))
    {
        QStringList urls = m_config.readEntry("Opened Files", QStringList());
        foreach (QString url, urls)
            openPokemod(KUrl(url));
    }
    setAutoSaveSettings("MainWindow", true);
}

Pokemodr::PokemodrUI::~PokemodrUI()
{
    m_config.config()->sync();
    closeAllPokemods(true);
}

void Pokemodr::PokemodrUI::update()
{
    treePokemod->update(treePokemod->currentIndex());
}

void Pokemodr::PokemodrUI::closeEvent(QCloseEvent* event)
{
    m_recent->saveEntries(m_config.group("Recent Files"));
    if (closeAllPokemods())
        event->accept();
    else
        event->ignore();
}

void Pokemodr::PokemodrUI::setChangedTitle(const bool changed)
{
    setWindowTitle(QString::fromUtf8("%1%2 - Pokémodr").arg(treePokemod->description(treePokemod->currentIndex()), changed ? "*" : ""));
}

void Pokemodr::PokemodrUI::setDirty(const bool dirty)
{
    const Pokemod::Pokemod* pokemod = qobject_cast<const Pokemod::Pokemod*>(static_cast<Pokemod::Object*>(treePokemod->currentIndex().internalPointer())->pokemod());
    treePokemod->setDirty(pokemod, dirty);
}

void Pokemodr::PokemodrUI::newPokemod()
{
    treePokemod->addPokemod(new Pokemod::Pokemod);
}

void Pokemodr::PokemodrUI::openPokemod()
{
    KUrl::List urls = KFileDialog::getOpenUrls(KUrl("kfiledialog:///pokemod"), QString::fromUtf8("*.pmod|Pokémod files"), this);
    foreach (KUrl url, urls)
    {
        if (!url.isEmpty())
            openPokemod(url);
    }
}

bool Pokemodr::PokemodrUI::openPokemod(const KUrl& url)
{
    bool opened = false;
    if (treePokemod->isOpen(url))
    {
        KMessageBox::error(this, "File is already opened", QString::fromUtf8("Pokémod error"));
        return false;
    }
    if (url.isLocalFile())
        opened = openPokemod(url.path());
    else
    {
        QString temp;
        if (KIO::NetAccess::download(url, temp, this))
        {
            opened = openPokemod(temp, true);
            KIO::NetAccess::removeTempFile(temp);
        }
        else
            KMessageBox::error(this, KIO::NetAccess::lastErrorString(), "KIO Error");
    }
    if (opened)
        m_recent->addUrl(url);
    return opened;
}

bool Pokemodr::PokemodrUI::openPokemod(const QString& path, const bool isRemote)
{
    QFile file(path);
    if (file.open(QIODevice::ReadOnly))
    {
        QDomDocument xml;
        if (xml.setContent(&file))
        {
            if (xml.doctype().name() == "Pokemod")
            {
                treePokemod->addPokemod(new Pokemod::Pokemod(xml.documentElement()), isRemote ? KUrl() : KUrl(path));
                return true;
            }
            else
                KMessageBox::error(this, QString::fromUtf8("The file is not a Pokémod."), QString::fromUtf8("Invalid Pokémod"));
        }
        else
            KMessageBox::error(this, "The file is not a valid XML file.", QString::fromUtf8("Invalid Pokémod"));
        file.close();
    }
    else
        KMessageBox::error(this, QString("Cannot open file:\n%1").arg(path), "No such file");
    return false;
}

void Pokemodr::PokemodrUI::savePokemod()
{
    savePokemod(treePokemod->currentPokemod());
}

void Pokemodr::PokemodrUI::savePokemod(const Pokemod::Pokemod* pokemod)
{
    const KUrl url = treePokemod->url(pokemod);
    if (url.isEmpty())
        saveAsPokemod(pokemod);
    else
        savePokemod(pokemod, url);
}

bool Pokemodr::PokemodrUI::savePokemod(const Pokemod::Pokemod* pokemod, const KUrl& url)
{
    if (pokemod)
    {
        if (url.isLocalFile())
        {
            QFile file(url.path());
            if (file.open(QIODevice::WriteOnly))
            {
                file.write(Pokemod::Object::xml(pokemod).toByteArray());
                treePokemod->setDirty(pokemod, false);
                file.close();
                return true;
            }
            else
                KMessageBox::error(this, QString::fromUtf8("Error saving the Pokémod!"), "Save error");
        }
        else
        {
            KTemporaryFile temp;
            if (temp.open())
            {
                temp.write(Pokemod::Object::xml(pokemod).toByteArray());
                if (KIO::NetAccess::upload(temp.fileName(), url, this))
                {
                    treePokemod->setDirty(pokemod, false);
                    return true;
                }
                else
                    KMessageBox::error(this, KIO::NetAccess::lastErrorString(), "KIO Error");
            }
            else
                KMessageBox::error(this, QString::fromUtf8("Error saving the Pokémod!"), "Save error");
        }
    }
    return false;
}

void Pokemodr::PokemodrUI::saveAsPokemod()
{
    saveAsPokemod(treePokemod->currentPokemod());
}

void Pokemodr::PokemodrUI::saveAsPokemod(const Pokemod::Pokemod* pokemod)
{
    if (pokemod)
    {
        KUrl url = KFileDialog::getSaveUrl(KUrl("kfiledialog:///pokemod"), QString::fromUtf8("*.pmod|Pokémod files"), this, QString::fromUtf8("Open Pokémod"));
        if (!url.isEmpty())
        {
            if (savePokemod(pokemod, url))
                treePokemod->setUrl(pokemod, url);
        }
    }
}

void Pokemodr::PokemodrUI::closePokemod()
{
    closePokemod(treePokemod->currentPokemod());
}

bool Pokemodr::PokemodrUI::closePokemod(const Pokemod::Pokemod* pokemod, const bool force)
{
    if (pokemod)
    {
        if (treePokemod->dirty(pokemod))
        {
            int result;
            if (force)
                result = KMessageBox::questionYesNo(this, "You have unsaved changes, would you like to save them?", QString::fromUtf8("Unsaved Pokémod"));
            else
                result = KMessageBox::questionYesNoCancel(this, "You have unsaved changes, would you like to save them?", QString::fromUtf8("Unsaved Pokémod"));
            switch (result)
            {
                case KMessageBox::Yes:
                    savePokemod(pokemod);
                case KMessageBox::No:
                    break;
                case KMessageBox::Cancel:
                    return false;
            }
        }
        treePokemod->deletePokemod(pokemod);
        if (formPanel->widget())
            delete formPanel->takeWidget();
    }
    return true;
}

bool Pokemodr::PokemodrUI::closeAllPokemods(const bool force)
{
    QList<const Pokemod::Pokemod*> pokemods = treePokemod->openedPokemods();
    if (pokemods.size())
        m_config.writeEntry("Opened Files", treePokemod->urls());
    for (int i = 0; i < pokemods.size(); )
    {
        if (closePokemod(pokemods[i], force))
            pokemods.removeAt(i);
        else
            ++i;
    }
    if (!pokemods.size() || force)
        close();
    return !pokemods.size();
}

void Pokemodr::PokemodrUI::copyObject()
{
    m_clipboard = treePokemod->copy(treePokemod->currentIndex());
}

void Pokemodr::PokemodrUI::pasteObject()
{
    treePokemod->paste(treePokemod->currentIndex(), m_clipboard);
}

void Pokemodr::PokemodrUI::preferences()
{
    PokemodrPreferences* dialog = new PokemodrPreferences(this, m_config);
    connect(dialog, SIGNAL(okClicked()), dialog, SLOT(save()));
    dialog->exec();
}

void Pokemodr::PokemodrUI::on_splitter_splitterMoved()
{
    m_config.writeEntry("treeWidth", splitter->sizes()[0]);
    m_config.writeEntry("panelWidth", splitter->sizes()[1]);
}

void Pokemodr::PokemodrUI::on_treePokemod_clicked(const QModelIndex& index)
{
    if (!(treePokemod->model()->flags(index) & Qt::ItemIsSelectable))
        return;
    ObjectUI* widget = treePokemod->editorWidget(index);
    if (widget)
    {
        if (formPanel->widget() && boxButtons->isEnabled())
        {
            switch (KMessageBox::questionYesNoCancel(this, "You have unsaved changes, would you like to save them?", QString("Unsaved %1").arg(qobject_cast<ObjectUI*>(formPanel->widget())->original()->className())))
            {
                case KMessageBox::Yes:
                    qobject_cast<ObjectUI*>(formPanel->widget())->apply();
                    break;
                case KMessageBox::No:
                    qobject_cast<ObjectUI*>(formPanel->widget())->discard();
                    break;
                case KMessageBox::Cancel:
                    return;
            }
        }
        boxButtons->setEnabled(false);
        connect(widget, SIGNAL(changed(bool)), this, SLOT(setChangedTitle(bool)));
        connect(widget, SIGNAL(changed(bool)), boxButtons, SLOT(setEnabled(bool)));
        connect(buttonApply, SIGNAL(clicked()), widget, SLOT(apply()));
        connect(buttonApply, SIGNAL(clicked()), treePokemod, SLOT(setDirty()));
        connect(buttonDiscard, SIGNAL(clicked()), widget, SLOT(discard()));
        setChangedTitle(false);
        formPanel->setWidget(widget);
        formPanel->show();
    }
}

void Pokemodr::PokemodrUI::on_treePokemod_customContextMenuRequested(const QPoint& position)
{
    QModelIndex index = treePokemod->indexAt(position);
    KMenu* menu = treePokemod->contextMenu(index);
    if (menu)
    {
        QString type = treePokemod->model()->data(index, BaseModel::TypeRole).toString();
        KAction* paste = new KAction("&Paste", this);
        connect(paste, SIGNAL(triggered()), this, SLOT(pasteObject()));
        menu->addSeparator();
        if (!type.isEmpty())
        {
            menu->addAction("&Copy", this, SLOT(copyObject()));
            paste->setEnabled(m_clipboard.doctype().name() == type);
        }
        menu->addAction(paste);
        connect(menu, SIGNAL(triggered(QAction*)), treePokemod, SLOT(setDirty()));
        menu->popup(treePokemod->mapToGlobal(position));
    }
}

void Pokemodr::PokemodrUI::showAboutApplication()
{
    // TODO: Need to make an about...
}

void Pokemodr::PokemodrUI::setupActions()
{
    KActionCollection* collection = new KActionCollection(this);
    KAction* openNew = KStandardAction::openNew(this, SLOT(newPokemod()), collection);
    KAction* open = KStandardAction::open(this, SLOT(openPokemod()), collection);
    m_recent = KStandardAction::openRecent(this, SLOT(openPokemod(const KUrl&)), collection);
    m_recent->loadEntries(m_config.group("Recent Files"));
    KAction* close = KStandardAction::close(this, SLOT(closePokemod()), collection);
    KAction* save = KStandardAction::save(this, SLOT(savePokemod()), collection);
    KAction* saveAs = KStandardAction::saveAs(this, SLOT(saveAsPokemod()), collection);
    KAction* quit = KStandardAction::quit(this, SLOT(closeAllPokemods()), collection);
    KAction* cut = KStandardAction::cut(collection);
    KAction* copy = KStandardAction::copy(collection);
    KAction* paste = KStandardAction::paste(collection);
    KAction* preferences = KStandardAction::preferences(this, SLOT(preferences()), collection);
    KMenuBar* menubar = new KMenuBar(this);
    KMenu* menuFile = new KMenu("&File", menubar);
    menuFile->addAction(openNew);
    menuFile->addAction(open);
    menuFile->addAction(m_recent);
    menuFile->addAction(close);
    menuFile->addSeparator();
    menuFile->addAction(save);
    menuFile->addAction(saveAs);
    menuFile->addSeparator();
    menuFile->addAction(quit);
    menubar->addMenu(menuFile);
    KMenu* menuEdit = new KMenu("&Edit", menubar);
    menuEdit->addAction(cut);
    menuEdit->addAction(copy);
    menuEdit->addAction(paste);
    menuEdit->addSeparator();
    menuEdit->addAction(preferences);
    menubar->addMenu(menuEdit);
    menubar->addMenu(customHelpMenu());
    setMenuBar(menubar);
    KToolBar* toolbar = new KToolBar("toolbar", this, Qt::TopToolBarArea, false, true, true);
    toolbar->addAction(openNew);
    toolbar->addAction(open);
    toolbar->addAction(m_recent);
    toolbar->addAction(close);
    toolbar->addSeparator();
    toolbar->addAction(save);
    toolbar->addAction(saveAs);
    toolbar->addSeparator();
    toolbar->addAction(cut);
    toolbar->addAction(copy);
    toolbar->addAction(paste);
    addToolBar(toolbar);
}