summaryrefslogtreecommitdiffstats
path: root/sigmodr/ObjectUI.cpp
blob: a7743bb8098d5792bfc2855504fab69dc6da3fff (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
/*
 * 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"

// Sigmodr includes
#include "ValidationDialog.h"

// Sigmod includes
#include "../sigmod/Object.h"
#include "../sigmod/Sigmod.h"

// KDE includes
#include <KAction>
#include <KDialog>
#include <KMenu>
#include <KMessageBox>

Sigmodr::ObjectUI::ObjectUI(QWidget* parent) :
        QWidget(parent),
        m_changed(false),
        m_validator(NULL),
        m_object(NULL),
        m_object_mod(NULL)
{
    setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, SIGNAL(changed(bool)), SLOT(setChanged(bool)));
    connect(this, SIGNAL(customContextMenuRequested(QPoint)), SLOT(contextMenu(QPoint)));
}

Sigmodr::ObjectUI::~ObjectUI()
{
    delete m_object_mod;
}

void Sigmodr::ObjectUI::reload()
{
    refreshGui();
    setGui();
}

bool Sigmodr::ObjectUI::isChanged() const
{
    return m_changed;
}

const Sigmod::Sigmod* Sigmodr::ObjectUI::sigmod() const
{
    return m_object->sigmod();
}

const Sigmod::Sigmod::Object* Sigmodr::ObjectUI::original() const
{
    return m_object;
}

Sigmod::Object* Sigmodr::ObjectUI::original()
{
    return m_object;
}

const Sigmod::Object* Sigmodr::ObjectUI::modified() const
{
    return m_object_mod;
}

Sigmod::Object* Sigmodr::ObjectUI::modified()
{
    return m_object_mod;
}

void Sigmodr::ObjectUI::setObjects(Sigmod::Object* original, Sigmod::Object* modified)
{
    m_object = original;
    m_object_mod = modified;
    if (m_validator)
        delete m_validator;
    m_validator = new ValidationDialog(m_object, this);
    connect(m_object_mod, SIGNAL(changed()), this, SIGNAL(changed()));
    connect(m_object_mod, SIGNAL(changed()), this, SLOT(setGui()));
    connect(m_object_mod, SIGNAL(error(QString)), this, SLOT(setGui()));
    connect(m_object_mod, SIGNAL(error(QString)), this, SLOT(errorMessage(QString)));
    connect(m_object_mod, SIGNAL(warning(QString)), this, SLOT(warningMessage(QString)));
    init();
}

void Sigmodr::ObjectUI::init()
{
    initGui();
    reload();
    emit(changed(false));
}

void Sigmodr::ObjectUI::setChanged(const bool changed)
{
    m_changed = changed;
}

void Sigmodr::ObjectUI::errorMessage(const QString& message)
{
    KMessageBox::error(this, message, "Error");
}

void Sigmodr::ObjectUI::warningMessage(const QString& message)
{
    KMessageBox::warningContinueCancel(this, message, "Warning");
}

void Sigmodr::ObjectUI::initGui()
{
}

void Sigmodr::ObjectUI::refreshGui()
{
}

void Sigmodr::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 Sigmodr::ObjectUI::validate()
{
    if (m_changed)
        apply();
    m_validator->show();
}