[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[2] | 11 | **
|
---|
[846] | 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.
|
---|
[2] | 25 | **
|
---|
[846] | 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."
|
---|
[2] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include <QApplication>
|
---|
| 42 | #include <QAxFactory>
|
---|
| 43 | #include <QTabWidget>
|
---|
| 44 | #include <QTimer>
|
---|
| 45 |
|
---|
| 46 | class Application;
|
---|
| 47 | class DocumentList;
|
---|
| 48 |
|
---|
| 49 | //! [0]
|
---|
| 50 | class Document : public QObject
|
---|
| 51 | {
|
---|
| 52 | Q_OBJECT
|
---|
| 53 |
|
---|
| 54 | Q_CLASSINFO("ClassID", "{2b5775cd-72c2-43da-bc3b-b0e8d1e1c4f7}")
|
---|
| 55 | Q_CLASSINFO("InterfaceID", "{2ce1761e-07a3-415c-bd11-0eab2c7283de}")
|
---|
| 56 |
|
---|
| 57 | Q_PROPERTY(Application *application READ application)
|
---|
| 58 | Q_PROPERTY(QString title READ title WRITE setTitle)
|
---|
| 59 |
|
---|
| 60 | public:
|
---|
| 61 | Document(DocumentList *list);
|
---|
| 62 | ~Document();
|
---|
| 63 |
|
---|
| 64 | Application *application() const;
|
---|
| 65 |
|
---|
| 66 | QString title() const;
|
---|
| 67 | void setTitle(const QString &title);
|
---|
| 68 |
|
---|
| 69 | private:
|
---|
| 70 | QWidget *page;
|
---|
| 71 | };
|
---|
| 72 | //! [0]
|
---|
| 73 |
|
---|
| 74 | //! [1]
|
---|
| 75 | class DocumentList : public QObject
|
---|
| 76 | {
|
---|
| 77 | Q_OBJECT
|
---|
| 78 |
|
---|
| 79 | Q_CLASSINFO("ClassID", "{496b761d-924b-4554-a18a-8f3704d2a9a6}")
|
---|
| 80 | Q_CLASSINFO("InterfaceID", "{6c9e30e8-3ff6-4e6a-9edc-d219d074a148}")
|
---|
| 81 |
|
---|
| 82 | Q_PROPERTY(Application* application READ application)
|
---|
| 83 | Q_PROPERTY(int count READ count)
|
---|
| 84 |
|
---|
| 85 | public:
|
---|
| 86 | DocumentList(Application *application);
|
---|
| 87 |
|
---|
| 88 | int count() const;
|
---|
| 89 | Application *application() const;
|
---|
| 90 |
|
---|
| 91 | public slots:
|
---|
| 92 | Document *addDocument();
|
---|
| 93 | Document *item(int index) const;
|
---|
| 94 |
|
---|
| 95 | private:
|
---|
| 96 | QList<Document*> list;
|
---|
| 97 | };
|
---|
| 98 | //! [1]
|
---|
| 99 |
|
---|
| 100 | //! [2]
|
---|
| 101 | class Application : public QObject
|
---|
| 102 | {
|
---|
| 103 | Q_OBJECT
|
---|
| 104 |
|
---|
| 105 | Q_CLASSINFO("ClassID", "{b50a71db-c4a7-4551-8d14-49983566afee}")
|
---|
| 106 | Q_CLASSINFO("InterfaceID", "{4a427759-16ef-4ed8-be79-59ffe5789042}")
|
---|
| 107 | Q_CLASSINFO("RegisterObject", "yes")
|
---|
| 108 |
|
---|
| 109 | Q_PROPERTY(DocumentList* documents READ documents)
|
---|
| 110 | Q_PROPERTY(QString id READ id)
|
---|
| 111 | Q_PROPERTY(bool visible READ isVisible WRITE setVisible)
|
---|
| 112 |
|
---|
| 113 | public:
|
---|
| 114 | Application(QObject *parent = 0);
|
---|
| 115 | DocumentList *documents() const;
|
---|
| 116 |
|
---|
| 117 | QString id() const { return objectName(); }
|
---|
| 118 |
|
---|
| 119 | void setVisible(bool on);
|
---|
| 120 | bool isVisible() const;
|
---|
| 121 |
|
---|
| 122 | QTabWidget *window() const { return ui; }
|
---|
| 123 |
|
---|
| 124 | public slots:
|
---|
| 125 | void quit();
|
---|
| 126 |
|
---|
| 127 | private:
|
---|
| 128 | DocumentList *docs;
|
---|
| 129 |
|
---|
| 130 | QTabWidget *ui;
|
---|
| 131 | };
|
---|
| 132 | //! [2]
|
---|
| 133 |
|
---|
| 134 | //! [3]
|
---|
| 135 | Document::Document(DocumentList *list)
|
---|
| 136 | : QObject(list)
|
---|
| 137 | {
|
---|
| 138 | QTabWidget *tabs = list->application()->window();
|
---|
| 139 | page = new QWidget(tabs);
|
---|
| 140 | page->setWindowTitle("Unnamed");
|
---|
| 141 | tabs->addTab(page, page->windowTitle());
|
---|
| 142 |
|
---|
| 143 | page->show();
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | Document::~Document()
|
---|
| 147 | {
|
---|
| 148 | delete page;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | Application *Document::application() const
|
---|
| 152 | {
|
---|
| 153 | return qobject_cast<DocumentList*>(parent())->application();
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | QString Document::title() const
|
---|
| 157 | {
|
---|
| 158 | return page->windowTitle();
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | void Document::setTitle(const QString &t)
|
---|
| 162 | {
|
---|
| 163 | page->setWindowTitle(t);
|
---|
| 164 |
|
---|
| 165 | QTabWidget *tabs = application()->window();
|
---|
| 166 | int index = tabs->indexOf(page);
|
---|
| 167 | tabs->setTabText(index, page->windowTitle());
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | //! [3] //! [4]
|
---|
| 171 | DocumentList::DocumentList(Application *application)
|
---|
| 172 | : QObject(application)
|
---|
| 173 | {
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | Application *DocumentList::application() const
|
---|
| 177 | {
|
---|
| 178 | return qobject_cast<Application*>(parent());
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | int DocumentList::count() const
|
---|
| 182 | {
|
---|
| 183 | return list.count();
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | Document *DocumentList::item(int index) const
|
---|
| 187 | {
|
---|
| 188 | if (index >= list.count())
|
---|
| 189 | return 0;
|
---|
| 190 |
|
---|
| 191 | return list.at(index);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | Document *DocumentList::addDocument()
|
---|
| 195 | {
|
---|
| 196 | Document *document = new Document(this);
|
---|
| 197 | list.append(document);
|
---|
| 198 |
|
---|
| 199 | return document;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 |
|
---|
| 203 | //! [4] //! [5]
|
---|
| 204 | Application::Application(QObject *parent)
|
---|
| 205 | : QObject(parent), ui(0)
|
---|
| 206 | {
|
---|
| 207 | ui = new QTabWidget;
|
---|
| 208 |
|
---|
| 209 | setObjectName("From QAxFactory");
|
---|
| 210 | docs = new DocumentList(this);
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | DocumentList *Application::documents() const
|
---|
| 214 | {
|
---|
| 215 | return docs;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | void Application::setVisible(bool on)
|
---|
| 219 | {
|
---|
| 220 | ui->setShown(on);
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | bool Application::isVisible() const
|
---|
| 224 | {
|
---|
| 225 | return ui->isVisible();
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | void Application::quit()
|
---|
| 229 | {
|
---|
| 230 | delete docs;
|
---|
| 231 | docs = 0;
|
---|
| 232 |
|
---|
| 233 | delete ui;
|
---|
| 234 | ui = 0;
|
---|
| 235 | QTimer::singleShot(0, qApp, SLOT(quit()));
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | #include "main.moc"
|
---|
| 239 | //! [5] //! [6]
|
---|
| 240 |
|
---|
| 241 |
|
---|
| 242 | QAXFACTORY_BEGIN("{edd3e836-f537-4c6f-be7d-6014c155cc7a}", "{b7da3de8-83bb-4bbe-9ab7-99a05819e201}")
|
---|
| 243 | QAXCLASS(Application)
|
---|
| 244 | QAXTYPE(Document)
|
---|
| 245 | QAXTYPE(DocumentList)
|
---|
| 246 | QAXFACTORY_END()
|
---|
| 247 |
|
---|
| 248 | //! [6] //! [7]
|
---|
| 249 | int main(int argc, char **argv)
|
---|
| 250 | {
|
---|
| 251 | QApplication app(argc, argv);
|
---|
| 252 | app.setQuitOnLastWindowClosed(false);
|
---|
| 253 |
|
---|
| 254 | // started by COM - don't do anything
|
---|
| 255 | if (QAxFactory::isServer())
|
---|
| 256 | return app.exec();
|
---|
| 257 |
|
---|
| 258 | // started by user
|
---|
| 259 | Application appobject(0);
|
---|
| 260 | appobject.setObjectName("From Application");
|
---|
| 261 |
|
---|
| 262 | QAxFactory::startServer();
|
---|
| 263 | QAxFactory::registerActiveObject(&appobject);
|
---|
| 264 |
|
---|
| 265 | appobject.setVisible(true);
|
---|
| 266 |
|
---|
| 267 | QObject::connect(qApp, SIGNAL(lastWindowClosed()), &appobject, SLOT(quit()));
|
---|
| 268 |
|
---|
| 269 | return app.exec();
|
---|
| 270 | }
|
---|
| 271 | //! [7]
|
---|