Imported Upstream version 0.9.0
[deb_shairplay.git] / AirTV-Qt / qtsingleapplication / doc / html / qtsingleapplication-example-loader.html
CommitLineData
15c988f7
JB
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<!-- loader.qdoc -->
6<head>
7 <title>Loading Documents</title>
8 <link href="classic.css" rel="stylesheet" type="text/css" />
9</head>
10<body>
11<table border="0" cellpadding="0" cellspacing="0" width="100%">
12<tr>
13<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="57" height="67" border="0" /></td>
14<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a></td>
15</tr></table><h1 class="title">Loading Documents<br /><span class="subtitle"></span>
16</h1>
17<p>The application in this example loads or prints the documents passed as commandline parameters to further instances of this application.</p>
18<pre><span class="comment"> /****************************************************************************
19 **
20 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
21 ** All rights reserved.
22 **
23 ** Contact: Nokia Corporation (qt-info@nokia.com)
24 **
25 ** This file is part of a Qt Solutions component.
26 **
27 ** You may use this file under the terms of the BSD license as follows:
28 **
29 ** &quot;Redistribution and use in source and binary forms, with or without
30 ** modification, are permitted provided that the following conditions are
31 ** met:
32 ** * Redistributions of source code must retain the above copyright
33 ** notice, this list of conditions and the following disclaimer.
34 ** * Redistributions in binary form must reproduce the above copyright
35 ** notice, this list of conditions and the following disclaimer in
36 ** the documentation and/or other materials provided with the
37 ** distribution.
38 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
39 ** the names of its contributors may be used to endorse or promote
40 ** products derived from this software without specific prior written
41 ** permission.
42 **
43 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
54 **
55 ****************************************************************************/</span>
56
57 #include &lt;qtsingleapplication.h&gt;
58 #include &lt;QtCore/QFile&gt;
59 #include &lt;QtGui/QMainWindow&gt;
60 #include &lt;QtGui/QPrinter&gt;
61 #include &lt;QtGui/QPainter&gt;
62 #include &lt;QtGui/QTextEdit&gt;
63 #include &lt;QtGui/QMdiArea&gt;
64 #include &lt;QtCore/QTextStream&gt;
65
66 class MainWindow : public QMainWindow
67 {
68 Q_OBJECT
69 public:
70 MainWindow();
71
72 public slots:
73 void handleMessage(const QString&amp; message);
74
75 signals:
76 void needToShow();
77
78 private:
79 QMdiArea *workspace;
80 };</pre>
81<p>The user interface in this application is a <a href="http://qt.nokia.com/doc/4.6/qmainwindow.html">QMainWindow</a> subclass with a <a href="http://qt.nokia.com/doc/4.6/qmdiarea.html">QMdiArea</a> as the central widget. It implements a slot <tt>handleMessage()</tt> that will be connected to the messageReceived() signal of the <a href="qtsingleapplication.html">QtSingleApplication</a> class.</p>
82<pre> MainWindow::MainWindow()
83 {
84 workspace = new QMdiArea(this);
85
86 setCentralWidget(workspace);
87 }</pre>
88<p>The <a href="http://qt.nokia.com/doc/4.6/designer-to-know.html">MainWindow</a> constructor creates a minimal user interface.</p>
89<pre> void MainWindow::handleMessage(const QString&amp; message)
90 {
91 enum Action {
92 Nothing,
93 Open,
94 Print
95 } action;
96
97 action = Nothing;
98 QString filename = message;
99 if (message.toLower().startsWith(&quot;/print &quot;)) {
100 filename = filename.mid(7);
101 action = Print;
102 } else if (!message.isEmpty()) {
103 action = Open;
104 }
105 if (action == Nothing) {
106 emit needToShow();
107 return;
108 }
109
110 QFile file(filename);
111 QString contents;
112 if (file.open(QIODevice::ReadOnly))
113 contents = file.readAll();
114 else
115 contents = &quot;[[Error: Could not load file &quot; + filename + &quot;]]&quot;;
116
117 QTextEdit *view = new QTextEdit;
118 view-&gt;setPlainText(contents);
119
120 switch(action) {</pre>
121<p>The handleMessage() slot interprets the message passed in as a filename that can be prepended with <i>/print</i> to indicate that the file should just be printed rather than loaded.</p>
122<pre> case Print:
123 {
124 QPrinter printer;
125 view-&gt;print(&amp;printer);
126 delete view;
127 }
128 break;
129
130 case Open:
131 {
132 workspace-&gt;addSubWindow(view);
133 view-&gt;setWindowTitle(message);
134 view-&gt;show();
135 emit needToShow();
136 }
137 break;
138 default:
139 break;
140 };
141 }</pre>
142<p>Loading the file will also activate the window.</p>
143<pre> #include &quot;main.moc&quot;
144
145 int main(int argc, char **argv)
146 {
147 QtSingleApplication instance(&quot;File loader QtSingleApplication example&quot;, argc, argv);
148 QString message;
149 for (int a = 1; a &lt; argc; ++a) {
150 message += argv[a];
151 if (a &lt; argc-1)
152 message += &quot; &quot;;
153 }
154
155 if (instance.sendMessage(message))
156 return 0;</pre>
157<p>The <tt>main</tt> entry point function creates a <a href="qtsingleapplication.html">QtSingleApplication</a> object, and creates a message to send to a running instance of the application. If the message was sent successfully the process exits immediately.</p>
158<pre> MainWindow mw;
159 mw.handleMessage(message);
160 mw.show();
161
162 QObject::connect(&amp;instance, SIGNAL(messageReceived(const QString&amp;)),
163 &amp;mw, SLOT(handleMessage(const QString&amp;)));
164
165 instance.setActivationWindow(&amp;mw, false);
166 QObject::connect(&amp;mw, SIGNAL(needToShow()), &amp;instance, SLOT(activateWindow()));
167
168 return instance.exec();
169 }</pre>
170<p>If the message could not be sent the application starts up. Note that <tt>false</tt> is passed to the call to setActivationWindow() to prevent automatic activation for every message received, e.g&#x2e; when the application should just print a file. Instead, the message handling function determines whether activation is requested, and signals that by emitting the needToShow() signal. This is then simply connected directly to <a href="qtsingleapplication.html">QtSingleApplication</a>'s activateWindow() slot.</p>
171<p /><address><hr /><div align="center">
172<table width="100%" cellspacing="0" border="0"><tr class="address">
173<td width="30%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
174<td width="40%" align="center"><a href="http://qt.nokia.com/doc/trademarks.html">Trademarks</a></td>
175<td width="30%" align="right"><div align="right">Qt Solutions</div></td>
176</tr></table></div></address></body>
177</html>