1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the examples of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 ** of its contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
42 #include <QtXmlPatterns>
44 #include "mainwindow.h"
45 #include "xmlsyntaxhighlighter.h"
48 class MessageHandler : public QAbstractMessageHandler
52 : QAbstractMessageHandler(0)
56 QString statusMessage() const
63 return m_sourceLocation.line();
68 return m_sourceLocation.column();
72 virtual void handleMessage(QtMsgType type, const QString &description,
73 const QUrl &identifier, const QSourceLocation &sourceLocation)
79 m_description = description;
80 m_sourceLocation = sourceLocation;
84 QtMsgType m_messageType;
85 QString m_description;
86 QSourceLocation m_sourceLocation;
91 MainWindow::MainWindow()
95 new XmlSyntaxHighlighter(schemaView->document());
96 new XmlSyntaxHighlighter(instanceEdit->document());
98 schemaSelection->addItem(tr("Contact Schema"));
99 schemaSelection->addItem(tr("Recipe Schema"));
100 schemaSelection->addItem(tr("Order Schema"));
102 instanceSelection->addItem(tr("Valid Contact Instance"));
103 instanceSelection->addItem(tr("Invalid Contact Instance"));
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()));
110 validationStatus->setAlignment(Qt::AlignCenter | Qt::AlignVCenter);
118 void MainWindow::schemaSelected(int index)
120 instanceSelection->clear();
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"));
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);
143 void MainWindow::instanceSelected(int index)
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);
155 void MainWindow::validate()
157 const QByteArray schemaData = schemaView->toPlainText().toUtf8();
158 const QByteArray instanceData = instanceEdit->toPlainText().toUtf8();
160 MessageHandler messageHandler;
163 schema.setMessageHandler(&messageHandler);
165 schema.load(schemaData);
167 bool errorOccurred = false;
168 if (!schema.isValid()) {
169 errorOccurred = true;
171 QXmlSchemaValidator validator(schema);
172 if (!validator.validate(instanceData))
173 errorOccurred = true;
177 validationStatus->setText(messageHandler.statusMessage());
178 moveCursor(messageHandler.line(), messageHandler.column());
180 validationStatus->setText(tr("validation successful"));
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);
190 void MainWindow::textChanged()
192 instanceEdit->setExtraSelections(QList<QTextEdit::ExtraSelection>());
195 void MainWindow::moveCursor(int line, int column)
197 instanceEdit->moveCursor(QTextCursor::Start);
198 for (int i = 1; i < line; ++i)
199 instanceEdit->moveCursor(QTextCursor::Down);
201 for (int i = 1; i < column; ++i)
202 instanceEdit->moveCursor(QTextCursor::Right);
204 QList<QTextEdit::ExtraSelection> extraSelections;
205 QTextEdit::ExtraSelection selection;
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);
214 instanceEdit->setExtraSelections(extraSelections);
216 instanceEdit->setFocus();