1/****************************************************************************
2**
3** Copyright (C) 2017 Ford Motor Company
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtRemoteObjects module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50#include <QTreeView>
51#include <QApplication>
52#include <QRemoteObjectNode>
53#include <QTimer>
54#include <QStandardItemModel>
55#include <QStandardItem>
56
57
58struct TimerHandler : public QObject
59{
60 Q_OBJECT
61public:
62 QStandardItemModel *model;
63public Q_SLOTS:
64 void changeData() {
65 Q_ASSERT(model);
66 Q_ASSERT(model->rowCount() > 50);
67 Q_ASSERT(model->columnCount() > 1);
68 for (int i = 10; i < 50; ++i)
69 model->setData(index: model->index(row: i, column: 1), value: QColor(Qt::blue), role: Qt::BackgroundRole);
70 }
71 void insertData() {
72 Q_ASSERT(model);
73 Q_ASSERT(model->rowCount() > 50);
74 Q_ASSERT(model->columnCount() > 1);
75 model->insertRows(row: 2, count: 9);
76 for (int i = 2; i < 11; ++i) {
77 model->setData(index: model->index(row: i, column: 1), value: QColor(Qt::green), role: Qt::BackgroundRole);
78 model->setData(index: model->index(row: i, column: 1), value: QLatin1String("InsertedRow"), role: Qt::DisplayRole);
79 }
80 }
81 void removeData() {
82 model->removeRows(row: 2, count: 4);
83 }
84
85 void changeFlags() {
86 QStandardItem *item = model->item(row: 0, column: 0);
87 item->setEnabled(false);
88 item = item->child(row: 0, column: 0);
89 item->setFlags(item->flags() & Qt::ItemIsSelectable);
90 }
91
92 void moveData() {
93 model->moveRows(sourceParent: QModelIndex(), sourceRow: 2, count: 4, destinationParent: QModelIndex(), destinationChild: 10);
94 }
95};
96
97QList<QStandardItem*> addChild(int numChildren, int nestingLevel)
98{
99 QList<QStandardItem*> result;
100 if (nestingLevel == 0)
101 return result;
102 for (int i = 0; i < numChildren; ++i) {
103 QStandardItem *child = new QStandardItem(QStringLiteral("Child num %1, nesting Level %2").arg(a: i+1).arg(a: nestingLevel));
104 if (i == 0)
105 child->appendRow(aitems: addChild(numChildren, nestingLevel: nestingLevel -1));
106 result.push_back(t: child);
107 }
108 return result;
109}
110
111int main(int argc, char *argv[])
112{
113 QLoggingCategory::setFilterRules("qt.remoteobjects.debug=false\n"
114 "qt.remoteobjects.warning=false");
115 QApplication app(argc, argv);
116
117 const int modelSize = 100000;
118 QStringList list;
119 QStandardItemModel sourceModel;
120 QStringList hHeaderList;
121 hHeaderList << QStringLiteral("First Column with spacing") << QStringLiteral("Second Column with spacing");
122 sourceModel.setHorizontalHeaderLabels(hHeaderList);
123 list.reserve(alloc: modelSize);
124 for (int i = 0; i < modelSize; ++i) {
125 QStandardItem *firstItem = new QStandardItem(QStringLiteral("FancyTextNumber %1").arg(a: i));
126 if (i == 0)
127 firstItem->appendRow(aitems: addChild(numChildren: 2, nestingLevel: 2));
128 QStandardItem *secondItem = new QStandardItem(QStringLiteral("FancyRow2TextNumber %1").arg(a: i));
129 if (i % 2 == 0)
130 firstItem->setBackground(Qt::red);
131 QList<QStandardItem*> row;
132 row << firstItem << secondItem;
133 sourceModel.invisibleRootItem()->appendRow(aitems: row);
134 //sourceModel.appendRow(row);
135 list << QStringLiteral("FancyTextNumber %1").arg(a: i);
136 }
137
138 // Needed by QMLModelViewClient
139 QHash<int,QByteArray> roleNames = {
140 {Qt::DisplayRole, "_text"},
141 {Qt::BackgroundRole, "_color"}
142 };
143 sourceModel.setItemRoleNames(roleNames);
144
145 QVector<int> roles;
146 roles << Qt::DisplayRole << Qt::BackgroundRole;
147
148 qDebug() << "Creating registry host";
149 QRemoteObjectRegistryHost node(QUrl(QStringLiteral("local:registry")));
150
151 QRemoteObjectHost node2(QUrl(QStringLiteral("local:replica")), QUrl(QStringLiteral("local:registry")));
152 node2.enableRemoting(model: &sourceModel, QStringLiteral("RemoteModel"), roles);
153
154 QTreeView view;
155 view.setWindowTitle(QStringLiteral("SourceView"));
156 view.setModel(&sourceModel);
157 view.show();
158 TimerHandler handler;
159 handler.model = &sourceModel;
160 QTimer::singleShot(interval: 5000, receiver: &handler, slot: &TimerHandler::changeData);
161 QTimer::singleShot(interval: 10000, receiver: &handler, slot: &TimerHandler::insertData);
162 QTimer::singleShot(interval: 11000, receiver: &handler, slot: &TimerHandler::changeFlags);
163 QTimer::singleShot(interval: 12000, receiver: &handler, slot: &TimerHandler::removeData);
164 QTimer::singleShot(interval: 13000, receiver: &handler, slot: &TimerHandler::moveData);
165
166 return app.exec();
167}
168
169#include "main.moc"
170

source code of qtremoteobjects/examples/remoteobjects/modelviewserver/main.cpp