1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 | #include <QtXmlPatterns>
|
---|
43 |
|
---|
44 | #include "mainwindow.h"
|
---|
45 | #include "xmlsyntaxhighlighter.h"
|
---|
46 |
|
---|
47 | //! [4]
|
---|
48 | class MessageHandler : public QAbstractMessageHandler
|
---|
49 | {
|
---|
50 | public:
|
---|
51 | MessageHandler()
|
---|
52 | : QAbstractMessageHandler(0)
|
---|
53 | {
|
---|
54 | }
|
---|
55 |
|
---|
56 | QString statusMessage() const
|
---|
57 | {
|
---|
58 | return m_description;
|
---|
59 | }
|
---|
60 |
|
---|
61 | int line() const
|
---|
62 | {
|
---|
63 | return m_sourceLocation.line();
|
---|
64 | }
|
---|
65 |
|
---|
66 | int column() const
|
---|
67 | {
|
---|
68 | return m_sourceLocation.column();
|
---|
69 | }
|
---|
70 |
|
---|
71 | protected:
|
---|
72 | virtual void handleMessage(QtMsgType type, const QString &description,
|
---|
73 | const QUrl &identifier, const QSourceLocation &sourceLocation)
|
---|
74 | {
|
---|
75 | Q_UNUSED(type);
|
---|
76 | Q_UNUSED(identifier);
|
---|
77 |
|
---|
78 | m_messageType = type;
|
---|
79 | m_description = description;
|
---|
80 | m_sourceLocation = sourceLocation;
|
---|
81 | }
|
---|
82 |
|
---|
83 | private:
|
---|
84 | QtMsgType m_messageType;
|
---|
85 | QString m_description;
|
---|
86 | QSourceLocation m_sourceLocation;
|
---|
87 | };
|
---|
88 | //! [4]
|
---|
89 |
|
---|
90 | //! [0]
|
---|
91 | MainWindow::MainWindow()
|
---|
92 | {
|
---|
93 | setupUi(this);
|
---|
94 |
|
---|
95 | new XmlSyntaxHighlighter(schemaView->document());
|
---|
96 | new XmlSyntaxHighlighter(instanceEdit->document());
|
---|
97 |
|
---|
98 | schemaSelection->addItem(tr("Contact Schema"));
|
---|
99 | schemaSelection->addItem(tr("Recipe Schema"));
|
---|
100 | schemaSelection->addItem(tr("Order Schema"));
|
---|
101 |
|
---|
102 | instanceSelection->addItem(tr("Valid Contact Instance"));
|
---|
103 | instanceSelection->addItem(tr("Invalid Contact Instance"));
|
---|
104 |
|
---|
105 | connect(schemaSelection, SIGNAL(currentIndexChanged(int)), SLOT(schemaSelected(int)));
|
---|
106 | connect(instanceSelection, SIGNAL(currentIndexChanged(int)), SLOT(instanceSelected(int)));
|
---|
107 | connect(validateButton, SIGNAL(clicked()), SLOT(validate()));
|
---|
108 | connect(instanceEdit, SIGNAL(textChanged()), SLOT(textChanged()));
|
---|
109 |
|
---|
110 | validationStatus->setAlignment(Qt::AlignCenter | Qt::AlignVCenter);
|
---|
111 |
|
---|
112 | schemaSelected(0);
|
---|
113 | instanceSelected(0);
|
---|
114 | }
|
---|
115 | //! [0]
|
---|
116 |
|
---|
117 | //! [1]
|
---|
118 | void MainWindow::schemaSelected(int index)
|
---|
119 | {
|
---|
120 | instanceSelection->clear();
|
---|
121 | if (index == 0) {
|
---|
122 | instanceSelection->addItem(tr("Valid Contact Instance"));
|
---|
123 | instanceSelection->addItem(tr("Invalid Contact Instance"));
|
---|
124 | } else if (index == 1) {
|
---|
125 | instanceSelection->addItem(tr("Valid Recipe Instance"));
|
---|
126 | instanceSelection->addItem(tr("Invalid Recipe Instance"));
|
---|
127 | } else if (index == 2) {
|
---|
128 | instanceSelection->addItem(tr("Valid Order Instance"));
|
---|
129 | instanceSelection->addItem(tr("Invalid Order Instance"));
|
---|
130 | }
|
---|
131 | textChanged();
|
---|
132 |
|
---|
133 | QFile schemaFile(QString(":/schema_%1.xsd").arg(index));
|
---|
134 | schemaFile.open(QIODevice::ReadOnly);
|
---|
135 | const QString schemaText(QString::fromUtf8(schemaFile.readAll()));
|
---|
136 | schemaView->setPlainText(schemaText);
|
---|
137 |
|
---|
138 | validate();
|
---|
139 | }
|
---|
140 | //! [1]
|
---|
141 |
|
---|
142 | //! [2]
|
---|
143 | void MainWindow::instanceSelected(int index)
|
---|
144 | {
|
---|
145 | QFile instanceFile(QString(":/instance_%1.xml").arg((2*schemaSelection->currentIndex()) + index));
|
---|
146 | instanceFile.open(QIODevice::ReadOnly);
|
---|
147 | const QString instanceText(QString::fromUtf8(instanceFile.readAll()));
|
---|
148 | instanceEdit->setPlainText(instanceText);
|
---|
149 |
|
---|
150 | validate();
|
---|
151 | }
|
---|
152 | //! [2]
|
---|
153 |
|
---|
154 | //! [3]
|
---|
155 | void MainWindow::validate()
|
---|
156 | {
|
---|
157 | const QByteArray schemaData = schemaView->toPlainText().toUtf8();
|
---|
158 | const QByteArray instanceData = instanceEdit->toPlainText().toUtf8();
|
---|
159 |
|
---|
160 | MessageHandler messageHandler;
|
---|
161 |
|
---|
162 | QXmlSchema schema;
|
---|
163 | schema.setMessageHandler(&messageHandler);
|
---|
164 |
|
---|
165 | schema.load(schemaData);
|
---|
166 |
|
---|
167 | bool errorOccurred = false;
|
---|
168 | if (!schema.isValid()) {
|
---|
169 | errorOccurred = true;
|
---|
170 | } else {
|
---|
171 | QXmlSchemaValidator validator(schema);
|
---|
172 | if (!validator.validate(instanceData))
|
---|
173 | errorOccurred = true;
|
---|
174 | }
|
---|
175 |
|
---|
176 | if (errorOccurred) {
|
---|
177 | validationStatus->setText(messageHandler.statusMessage());
|
---|
178 | moveCursor(messageHandler.line(), messageHandler.column());
|
---|
179 | } else {
|
---|
180 | validationStatus->setText(tr("validation successful"));
|
---|
181 | }
|
---|
182 |
|
---|
183 | const QString styleSheet = QString("QLabel {background: %1; padding: 3px}")
|
---|
184 | .arg(errorOccurred ? QColor(Qt::red).lighter(160).name() :
|
---|
185 | QColor(Qt::green).lighter(160).name());
|
---|
186 | validationStatus->setStyleSheet(styleSheet);
|
---|
187 | }
|
---|
188 | //! [3]
|
---|
189 |
|
---|
190 | void MainWindow::textChanged()
|
---|
191 | {
|
---|
192 | instanceEdit->setExtraSelections(QList<QTextEdit::ExtraSelection>());
|
---|
193 | }
|
---|
194 |
|
---|
195 | void MainWindow::moveCursor(int line, int column)
|
---|
196 | {
|
---|
197 | instanceEdit->moveCursor(QTextCursor::Start);
|
---|
198 | for (int i = 1; i < line; ++i)
|
---|
199 | instanceEdit->moveCursor(QTextCursor::Down);
|
---|
200 |
|
---|
201 | for (int i = 1; i < column; ++i)
|
---|
202 | instanceEdit->moveCursor(QTextCursor::Right);
|
---|
203 |
|
---|
204 | QList<QTextEdit::ExtraSelection> extraSelections;
|
---|
205 | QTextEdit::ExtraSelection selection;
|
---|
206 |
|
---|
207 | const QColor lineColor = QColor(Qt::red).lighter(160);
|
---|
208 | selection.format.setBackground(lineColor);
|
---|
209 | selection.format.setProperty(QTextFormat::FullWidthSelection, true);
|
---|
210 | selection.cursor = instanceEdit->textCursor();
|
---|
211 | selection.cursor.clearSelection();
|
---|
212 | extraSelections.append(selection);
|
---|
213 |
|
---|
214 | instanceEdit->setExtraSelections(extraSelections);
|
---|
215 |
|
---|
216 | instanceEdit->setFocus();
|
---|
217 | }
|
---|