summaryrefslogtreecommitdiffstats
path: root/sigmodr/ValidationDialog.cpp
blob: 43290859faeb5fde36b1640c63f1099a5dff8f6c (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
/*
 * 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 "ValidationDialog.h"

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

// KDE includes
#include <KColorScheme>
#include <KMessageBox>
#include <KProgressDialog>

// Qt includes
#include <QtGui/QLabel>
#include <QtGui/QTreeWidget>
#include <QtGui/QVBoxLayout>

Sigmodr::ValidationDialog::ValidationDialog(Sigmod::Object* object, QWidget* parent) :
        QWidget(parent),
        m_processed(false),
        m_object(object),
        m_valTree(NULL)
{
    connect(m_object, SIGNAL(changed()), this, SLOT(objectChanged()));
}

int Sigmodr::ValidationDialog::errors() const
{
    return m_errors;
}

int Sigmodr::ValidationDialog::warnings() const
{
    return m_warnings;
}

void Sigmodr::ValidationDialog::process()
{
    if (!m_processed)
    {
        m_errors = 0;
        m_warnings = 0;
        m_valTree = new QTreeWidget;
        m_valTree->setHeaderHidden(true);
        m_parents.clear();
        m_parents.push(ObjectErrorCount(m_valTree->invisibleRootItem(), 0));
        KProgressDialog* progress = new KProgressDialog(this, "Validating", "Please wait");
        progress->progressBar()->setRange(0, 0);
        progress->setAllowCancel(false);
        progress->setModal(true);
        progress->show();
        connect(m_object, SIGNAL(valMessage(QString)), this, SLOT(addMessage(QString)));
        connect(m_object, SIGNAL(valWarning(QString)), this, SLOT(addWarning(QString)));
        connect(m_object, SIGNAL(valError(QString)), this, SLOT(addError(QString)));
        m_object->validate();
        disconnect(m_object, SIGNAL(valMessage(QString)), this, SLOT(addMessage(QString)));
        disconnect(m_object, SIGNAL(valWarning(QString)), this, SLOT(addWarning(QString)));
        disconnect(m_object, SIGNAL(valError(QString)), this, SLOT(addError(QString)));
        delete progress;
        m_valTree->addTopLevelItem(m_parents.top().first);
    }
    m_processed = true;
}

void Sigmodr::ValidationDialog::show()
{
    if (!m_processed)
        process();
    if (m_parents.top().second)
    {
        KDialog* dialog = new KDialog(this);
        dialog->setCaption("Validation Messages");
        dialog->setButtons(KDialog::Ok);
        QWidget* widget = new QWidget;
        QVBoxLayout* layout = new QVBoxLayout;
        layout->addWidget(new QLabel(QString("Warnings: %1\nErrors: %2").arg(m_warnings).arg(m_errors)));
        layout->addWidget(m_valTree);
        widget->setLayout(layout);
        dialog->setMainWidget(widget);
        dialog->exec();
        delete dialog;
        m_processed = false;
    }
    else
        KMessageBox::information(this, "No messages", "Validation");
}

void Sigmodr::ValidationDialog::insertMessage(const QString& msg, const QBrush& brush)
{
    ++m_parents.top().second;
    QTreeWidgetItem* item = new QTreeWidgetItem(m_parents.top().first, QStringList(msg));
    item->setBackground(0, brush);
}

void Sigmodr::ValidationDialog::addMessage(const QString& msg)
{
    if (msg.startsWith("++"))
        m_parents.push(ObjectErrorCount(new QTreeWidgetItem(QStringList(msg.mid(2))), 0));
    else if (msg.startsWith("--"))
    {
        ObjectErrorCount count = m_parents.pop();
        if (count.second)
        {
            m_parents.top().first->addChild(count.first);
            ++m_parents.top().second;
        }
    }
    else
        insertMessage(msg, KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground).brush(QPalette::Normal));
}

void Sigmodr::ValidationDialog::addError(const QString& msg)
{
    insertMessage(msg, KStatefulBrush(KColorScheme::View, KColorScheme::NegativeBackground).brush(QPalette::Normal));
    ++m_errors;
}

void Sigmodr::ValidationDialog::addWarning(const QString& msg)
{
    insertMessage(msg, KStatefulBrush(KColorScheme::View, KColorScheme::NeutralBackground).brush(QPalette::Normal));
    ++m_warnings;
}

void Sigmodr::ValidationDialog::objectChanged()
{
    m_processed = false;
}