~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/xmlpatterns-schema-mainwindow-cpp.html

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<head>
 
6
  <title>Qt 4.6: mainwindow.cpp Example File (xmlpatterns/schema/mainwindow.cpp)</title>
 
7
  <link href="classic.css" rel="stylesheet" type="text/css" />
 
8
</head>
 
9
<body>
 
10
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
11
<tr>
 
12
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
 
13
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td><td class="searchBar" align="right" valign="center"><form action="http://www.google.com/cse" id="cse-search-box"><div><input type="hidden" name="cx" value="000136343326384750312:dhbxnqlakyu" /><input type="hidden" name="ie" value="UTF-8" /><input type="text" name="q" size="31" /><input type="submit" name="sa" value="Search" /></div></form></td>
 
14
</tr></table><h1 class="title">mainwindow.cpp Example File<br /><span class="small-subtitle">xmlpatterns/schema/mainwindow.cpp</span>
 
15
</h1>
 
16
<pre><span class="comment"> /****************************************************************************
 
17
 **
 
18
 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 
19
 ** All rights reserved.
 
20
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 
21
 **
 
22
 ** This file is part of the examples of the Qt Toolkit.
 
23
 **
 
24
 ** $QT_BEGIN_LICENSE:LGPL$
 
25
 ** No Commercial Usage
 
26
 ** This file contains pre-release code and may not be distributed.
 
27
 ** You may use this file in accordance with the terms and conditions
 
28
 ** contained in the Technology Preview License Agreement accompanying
 
29
 ** this package.
 
30
 **
 
31
 ** GNU Lesser General Public License Usage
 
32
 ** Alternatively, this file may be used under the terms of the GNU Lesser
 
33
 ** General Public License version 2.1 as published by the Free Software
 
34
 ** Foundation and appearing in the file LICENSE.LGPL included in the
 
35
 ** packaging of this file.  Please review the following information to
 
36
 ** ensure the GNU Lesser General Public License version 2.1 requirements
 
37
 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
38
 **
 
39
 ** In addition, as a special exception, Nokia gives you certain additional
 
40
 ** rights.  These rights are described in the Nokia Qt LGPL Exception
 
41
 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
42
 **
 
43
 ** If you have questions regarding the use of this file, please contact
 
44
 ** Nokia at qt-info@nokia.com.
 
45
 **
 
46
 **
 
47
 **
 
48
 **
 
49
 **
 
50
 **
 
51
 **
 
52
 **
 
53
 ** $QT_END_LICENSE$
 
54
 **
 
55
 ****************************************************************************/</span>
 
56
 
 
57
 #include &lt;QtGui&gt;
 
58
 #include &lt;QtXmlPatterns&gt;
 
59
 
 
60
 #include &quot;mainwindow.h&quot;
 
61
 #include &quot;xmlsyntaxhighlighter.h&quot;
 
62
 
 
63
 class MessageHandler : public QAbstractMessageHandler
 
64
 {
 
65
     public:
 
66
         MessageHandler()
 
67
             : QAbstractMessageHandler(0)
 
68
         {
 
69
         }
 
70
 
 
71
         QString statusMessage() const
 
72
         {
 
73
             return m_description;
 
74
         }
 
75
 
 
76
         int line() const
 
77
         {
 
78
             return m_sourceLocation.line();
 
79
         }
 
80
 
 
81
         int column() const
 
82
         {
 
83
             return m_sourceLocation.column();
 
84
         }
 
85
 
 
86
     protected:
 
87
         virtual void handleMessage(QtMsgType type, const QString &amp;description,
 
88
                                    const QUrl &amp;identifier, const QSourceLocation &amp;sourceLocation)
 
89
         {
 
90
             Q_UNUSED(type);
 
91
             Q_UNUSED(identifier);
 
92
 
 
93
             m_messageType = type;
 
94
             m_description = description;
 
95
             m_sourceLocation = sourceLocation;
 
96
         }
 
97
 
 
98
     private:
 
99
         QtMsgType m_messageType;
 
100
         QString m_description;
 
101
         QSourceLocation m_sourceLocation;
 
102
 };
 
103
 
 
104
 MainWindow::MainWindow()
 
105
 {
 
106
     setupUi(this);
 
107
 
 
108
     new XmlSyntaxHighlighter(schemaView-&gt;document());
 
109
     new XmlSyntaxHighlighter(instanceEdit-&gt;document());
 
110
 
 
111
     schemaSelection-&gt;addItem(tr(&quot;Contact Schema&quot;));
 
112
     schemaSelection-&gt;addItem(tr(&quot;Recipe Schema&quot;));
 
113
     schemaSelection-&gt;addItem(tr(&quot;Order Schema&quot;));
 
114
 
 
115
     instanceSelection-&gt;addItem(tr(&quot;Valid Contact Instance&quot;));
 
116
     instanceSelection-&gt;addItem(tr(&quot;Invalid Contact Instance&quot;));
 
117
 
 
118
     connect(schemaSelection, SIGNAL(currentIndexChanged(int)), SLOT(schemaSelected(int)));
 
119
     connect(instanceSelection, SIGNAL(currentIndexChanged(int)), SLOT(instanceSelected(int)));
 
120
     connect(validateButton, SIGNAL(clicked()), SLOT(validate()));
 
121
     connect(instanceEdit, SIGNAL(textChanged()), SLOT(textChanged()));
 
122
 
 
123
     validationStatus-&gt;setAlignment(Qt::AlignCenter | Qt::AlignVCenter);
 
124
 
 
125
     schemaSelected(0);
 
126
     instanceSelected(0);
 
127
 }
 
128
 
 
129
 void MainWindow::schemaSelected(int index)
 
130
 {
 
131
     instanceSelection-&gt;clear();
 
132
     if (index == 0) {
 
133
         instanceSelection-&gt;addItem(tr(&quot;Valid Contact Instance&quot;));
 
134
         instanceSelection-&gt;addItem(tr(&quot;Invalid Contact Instance&quot;));
 
135
     } else if (index == 1) {
 
136
         instanceSelection-&gt;addItem(tr(&quot;Valid Recipe Instance&quot;));
 
137
         instanceSelection-&gt;addItem(tr(&quot;Invalid Recipe Instance&quot;));
 
138
     } else if (index == 2) {
 
139
         instanceSelection-&gt;addItem(tr(&quot;Valid Order Instance&quot;));
 
140
         instanceSelection-&gt;addItem(tr(&quot;Invalid Order Instance&quot;));
 
141
     }
 
142
     textChanged();
 
143
 
 
144
     QFile schemaFile(QString(&quot;:/schema_%1.xsd&quot;).arg(index));
 
145
     schemaFile.open(QIODevice::ReadOnly);
 
146
     const QString schemaText(QString::fromUtf8(schemaFile.readAll()));
 
147
     schemaView-&gt;setPlainText(schemaText);
 
148
 
 
149
     validate();
 
150
 }
 
151
 
 
152
 void MainWindow::instanceSelected(int index)
 
153
 {
 
154
     QFile instanceFile(QString(&quot;:/instance_%1.xml&quot;).arg((2*schemaSelection-&gt;currentIndex()) + index));
 
155
     instanceFile.open(QIODevice::ReadOnly);
 
156
     const QString instanceText(QString::fromUtf8(instanceFile.readAll()));
 
157
     instanceEdit-&gt;setPlainText(instanceText);
 
158
 
 
159
     validate();
 
160
 }
 
161
 
 
162
 void MainWindow::validate()
 
163
 {
 
164
     const QByteArray schemaData = schemaView-&gt;toPlainText().toUtf8();
 
165
     const QByteArray instanceData = instanceEdit-&gt;toPlainText().toUtf8();
 
166
 
 
167
     MessageHandler messageHandler;
 
168
 
 
169
     QXmlSchema schema;
 
170
     schema.setMessageHandler(&amp;messageHandler);
 
171
 
 
172
     schema.load(schemaData);
 
173
 
 
174
     bool errorOccurred = false;
 
175
     if (!schema.isValid()) {
 
176
         errorOccurred = true;
 
177
     } else {
 
178
         QXmlSchemaValidator validator(schema);
 
179
         if (!validator.validate(instanceData))
 
180
             errorOccurred = true;
 
181
     }
 
182
 
 
183
     if (errorOccurred) {
 
184
         validationStatus-&gt;setText(messageHandler.statusMessage());
 
185
         moveCursor(messageHandler.line(), messageHandler.column());
 
186
     } else {
 
187
         validationStatus-&gt;setText(tr(&quot;validation successful&quot;));
 
188
     }
 
189
 
 
190
     const QString styleSheet = QString(&quot;QLabel {background: %1; padding: 3px}&quot;)
 
191
                                       .arg(errorOccurred ? QColor(Qt::red).lighter(160).name() :
 
192
                                                            QColor(Qt::green).lighter(160).name());
 
193
     validationStatus-&gt;setStyleSheet(styleSheet);
 
194
 }
 
195
 
 
196
 void MainWindow::textChanged()
 
197
 {
 
198
     instanceEdit-&gt;setExtraSelections(QList&lt;QTextEdit::ExtraSelection&gt;());
 
199
 }
 
200
 
 
201
 void MainWindow::moveCursor(int line, int column)
 
202
 {
 
203
     instanceEdit-&gt;moveCursor(QTextCursor::Start);
 
204
     for (int i = 1; i &lt; line; ++i)
 
205
         instanceEdit-&gt;moveCursor(QTextCursor::Down);
 
206
 
 
207
     for (int i = 1; i &lt; column; ++i)
 
208
         instanceEdit-&gt;moveCursor(QTextCursor::Right);
 
209
 
 
210
     QList&lt;QTextEdit::ExtraSelection&gt; extraSelections;
 
211
     QTextEdit::ExtraSelection selection;
 
212
 
 
213
     const QColor lineColor = QColor(Qt::red).lighter(160);
 
214
     selection.format.setBackground(lineColor);
 
215
     selection.format.setProperty(QTextFormat::FullWidthSelection, true);
 
216
     selection.cursor = instanceEdit-&gt;textCursor();
 
217
     selection.cursor.clearSelection();
 
218
     extraSelections.append(selection);
 
219
 
 
220
     instanceEdit-&gt;setExtraSelections(extraSelections);
 
221
 
 
222
     instanceEdit-&gt;setFocus();
 
223
 }</pre>
 
224
<p /><address><hr /><div align="center">
 
225
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
226
<td width="40%" align="left">Copyright &copy; 2009 Nokia Corporation and/or its subsidiary(-ies)</td>
 
227
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
228
<td width="40%" align="right"><div align="right">Qt 4.6.0</div></td>
 
229
<script type="text/javascript" src="http://www.google.com/jsapi"></script><script type="text/javascript">google.load("elements", "1", {packages: "transliteration"});</script><script type="text/javascript" src="http://www.google.com/coop/cse/t13n?form=cse-search-box&t13n_langs=en"></script><script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en"></script></tr></table></div></address></body>
 
230
</html>