|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtGui> |
|
43 #include <QtXmlPatterns> |
|
44 |
|
45 #include "mainwindow.h" |
|
46 #include "xmlsyntaxhighlighter.h" |
|
47 |
|
48 //! [4] |
|
49 class MessageHandler : public QAbstractMessageHandler |
|
50 { |
|
51 public: |
|
52 MessageHandler() |
|
53 : QAbstractMessageHandler(0) |
|
54 { |
|
55 } |
|
56 |
|
57 QString statusMessage() const |
|
58 { |
|
59 return m_description; |
|
60 } |
|
61 |
|
62 int line() const |
|
63 { |
|
64 return m_sourceLocation.line(); |
|
65 } |
|
66 |
|
67 int column() const |
|
68 { |
|
69 return m_sourceLocation.column(); |
|
70 } |
|
71 |
|
72 protected: |
|
73 virtual void handleMessage(QtMsgType type, const QString &description, |
|
74 const QUrl &identifier, const QSourceLocation &sourceLocation) |
|
75 { |
|
76 Q_UNUSED(type); |
|
77 Q_UNUSED(identifier); |
|
78 |
|
79 m_messageType = type; |
|
80 m_description = description; |
|
81 m_sourceLocation = sourceLocation; |
|
82 } |
|
83 |
|
84 private: |
|
85 QtMsgType m_messageType; |
|
86 QString m_description; |
|
87 QSourceLocation m_sourceLocation; |
|
88 }; |
|
89 //! [4] |
|
90 |
|
91 //! [0] |
|
92 MainWindow::MainWindow() |
|
93 { |
|
94 setupUi(this); |
|
95 |
|
96 new XmlSyntaxHighlighter(schemaView->document()); |
|
97 new XmlSyntaxHighlighter(instanceEdit->document()); |
|
98 |
|
99 schemaSelection->addItem(tr("Contact Schema")); |
|
100 schemaSelection->addItem(tr("Recipe Schema")); |
|
101 schemaSelection->addItem(tr("Order Schema")); |
|
102 |
|
103 instanceSelection->addItem(tr("Valid Contact Instance")); |
|
104 instanceSelection->addItem(tr("Invalid Contact Instance")); |
|
105 |
|
106 connect(schemaSelection, SIGNAL(currentIndexChanged(int)), SLOT(schemaSelected(int))); |
|
107 connect(instanceSelection, SIGNAL(currentIndexChanged(int)), SLOT(instanceSelected(int))); |
|
108 connect(validateButton, SIGNAL(clicked()), SLOT(validate())); |
|
109 connect(instanceEdit, SIGNAL(textChanged()), SLOT(textChanged())); |
|
110 |
|
111 validationStatus->setAlignment(Qt::AlignCenter | Qt::AlignVCenter); |
|
112 |
|
113 schemaSelected(0); |
|
114 instanceSelected(0); |
|
115 } |
|
116 //! [0] |
|
117 |
|
118 //! [1] |
|
119 void MainWindow::schemaSelected(int index) |
|
120 { |
|
121 instanceSelection->clear(); |
|
122 if (index == 0) { |
|
123 instanceSelection->addItem(tr("Valid Contact Instance")); |
|
124 instanceSelection->addItem(tr("Invalid Contact Instance")); |
|
125 } else if (index == 1) { |
|
126 instanceSelection->addItem(tr("Valid Recipe Instance")); |
|
127 instanceSelection->addItem(tr("Invalid Recipe Instance")); |
|
128 } else if (index == 2) { |
|
129 instanceSelection->addItem(tr("Valid Order Instance")); |
|
130 instanceSelection->addItem(tr("Invalid Order Instance")); |
|
131 } |
|
132 textChanged(); |
|
133 |
|
134 QFile schemaFile(QString(":/schema_%1.xsd").arg(index)); |
|
135 schemaFile.open(QIODevice::ReadOnly); |
|
136 const QString schemaText(QString::fromUtf8(schemaFile.readAll())); |
|
137 schemaView->setPlainText(schemaText); |
|
138 |
|
139 validate(); |
|
140 } |
|
141 //! [1] |
|
142 |
|
143 //! [2] |
|
144 void MainWindow::instanceSelected(int index) |
|
145 { |
|
146 QFile instanceFile(QString(":/instance_%1.xml").arg((2*schemaSelection->currentIndex()) + index)); |
|
147 instanceFile.open(QIODevice::ReadOnly); |
|
148 const QString instanceText(QString::fromUtf8(instanceFile.readAll())); |
|
149 instanceEdit->setPlainText(instanceText); |
|
150 |
|
151 validate(); |
|
152 } |
|
153 //! [2] |
|
154 |
|
155 //! [3] |
|
156 void MainWindow::validate() |
|
157 { |
|
158 const QByteArray schemaData = schemaView->toPlainText().toUtf8(); |
|
159 const QByteArray instanceData = instanceEdit->toPlainText().toUtf8(); |
|
160 |
|
161 MessageHandler messageHandler; |
|
162 |
|
163 QXmlSchema schema; |
|
164 schema.setMessageHandler(&messageHandler); |
|
165 |
|
166 schema.load(schemaData); |
|
167 |
|
168 bool errorOccurred = false; |
|
169 if (!schema.isValid()) { |
|
170 errorOccurred = true; |
|
171 } else { |
|
172 QXmlSchemaValidator validator(schema); |
|
173 if (!validator.validate(instanceData)) |
|
174 errorOccurred = true; |
|
175 } |
|
176 |
|
177 if (errorOccurred) { |
|
178 validationStatus->setText(messageHandler.statusMessage()); |
|
179 moveCursor(messageHandler.line(), messageHandler.column()); |
|
180 } else { |
|
181 validationStatus->setText(tr("validation successful")); |
|
182 } |
|
183 |
|
184 const QString styleSheet = QString("QLabel {background: %1; padding: 3px}") |
|
185 .arg(errorOccurred ? QColor(Qt::red).lighter(160).name() : |
|
186 QColor(Qt::green).lighter(160).name()); |
|
187 validationStatus->setStyleSheet(styleSheet); |
|
188 } |
|
189 //! [3] |
|
190 |
|
191 void MainWindow::textChanged() |
|
192 { |
|
193 instanceEdit->setExtraSelections(QList<QTextEdit::ExtraSelection>()); |
|
194 } |
|
195 |
|
196 void MainWindow::moveCursor(int line, int column) |
|
197 { |
|
198 instanceEdit->moveCursor(QTextCursor::Start); |
|
199 for (int i = 1; i < line; ++i) |
|
200 instanceEdit->moveCursor(QTextCursor::Down); |
|
201 |
|
202 for (int i = 1; i < column; ++i) |
|
203 instanceEdit->moveCursor(QTextCursor::Right); |
|
204 |
|
205 QList<QTextEdit::ExtraSelection> extraSelections; |
|
206 QTextEdit::ExtraSelection selection; |
|
207 |
|
208 const QColor lineColor = QColor(Qt::red).lighter(160); |
|
209 selection.format.setBackground(lineColor); |
|
210 selection.format.setProperty(QTextFormat::FullWidthSelection, true); |
|
211 selection.cursor = instanceEdit->textCursor(); |
|
212 selection.cursor.clearSelection(); |
|
213 extraSelections.append(selection); |
|
214 |
|
215 instanceEdit->setExtraSelections(extraSelections); |
|
216 |
|
217 instanceEdit->setFocus(); |
|
218 } |