VR-Engage  2.2
Loading...
Searching...
No Matches
exampleQtQuick.h
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2025 MAK Technologies, Inc.
3 * All rights reserved.
4 ******************************************************************************/
5
6//! \file exampleQtQuick.h
7//! \brief Qt Quick chat window demonstrating Qt Model/View integration with VR-Engage
8
9#pragma once
10
11#include "export.h"
12#include "vrvUtil/signalslib.h"
13
14#include <QObject>
15#include <QAbstractListModel>
16
17class QQuickItem;
18
19namespace makVre
20{
21//! \brief Data container for a single chat message entry.
22//!
23//! This simple data class demonstrates the pattern for Qt-compatible data objects.
24//! All fields use Qt types (QString, double) for seamless integration with Qt models.
25class DtChatEntry
26{
27public:
28 //! \brief Default constructor initializing empty chat entry.
30
31 //! \brief Constructor with all chat entry fields.
32 //!
33 //! \param sender Entity name (marking text) who sent the message
34 //! \param receiver Entity name (marking text) who received the message
35 //! \param time Simulation time when message was sent
36 //! \param message The text content of the message
37 DtChatEntry(const QString& sender, const QString& receiver, const double time, const QString& message);
38
39 //! \brief Gets the sender's entity name.
40 QString getSender() const;
41
42 //! \brief Sets the sender's entity name.
43 void setSender(const QString& sender);
44
45 //! \brief Gets the receiver's entity name.
46 QString getReceiver() const;
47
48 //! \brief Sets the receiver's entity name.
49 void setReceiver(const QString& receiver);
50
51 //! \brief Gets the simulation time of the message.
52 double getTime() const;
53
54 //! \brief Sets the simulation time of the message.
55 void setTime(const double time);
56
57 //! \brief Gets the message text content.
58 QString getMessage() const;
59
60 //! \brief Sets the message text content.
61 void setMessage(const QString& message);
62
63protected:
64 QString mySender; //!< Entity name of message sender
65 QString myReceiver; //!< Entity name of message receiver
66 double myTime; //!< Simulation time of message
67 QString myMessage; //!< Text content of message
68};
69
70//! \brief Qt model for chat messages providing data to QML ListView.
71//!
72//! This class demonstrates the REUSABLE pattern for creating custom Qt models
73//! that expose C++ data to QML user interfaces. Key concepts:
74//! - Inheriting from QAbstractListModel for list-based data
75//! - Defining custom roles for QML property access
76//! - Implementing required virtual methods (rowCount, data, roleNames)
77//! - Proper model change notifications (beginInsertRows, endInsertRows, etc.)
78//!
79//! This pattern can be adapted for any list-based data that needs to be
80//! displayed in a QML ListView or Repeater.
81class DtChatQmlModel : public QAbstractListModel
82{
83 Q_OBJECT;
84
85public:
86 //! \brief Custom roles for accessing chat entry fields from QML.
87 //!
88 //! Qt roles allow QML to access C++ data using named properties.
89 //! Start at Qt::UserRole + 1 to avoid conflicts with standard roles.
91 {
92 SenderRole = Qt::UserRole + 1, //!< Access sender name via "sender" in QML
93 ReceiverRole, //!< Access receiver name via "receiver" in QML
94 TimeRole, //!< Access message time via "time" in QML
95 MessageRole //!< Access message text via "msg" in QML
96 };
97
98 //! \brief Constructor initializing the model.
99 //!
100 //! \param parent Optional parent QObject for Qt ownership hierarchy
101 DtChatQmlModel(QObject* parent = nullptr);
102
103 //! \brief Adds a new chat entry to the model.
104 //!
105 //! This demonstrates proper model update notifications:
106 //! - beginInsertRows notifies views that data is being added
107 //! - Actual data modification
108 //! - endInsertRows triggers view updates
109 //!
110 //! \param entry The chat entry to add to the model
111 virtual void addChatEntry(const DtChatEntry& entry);
112
113 //! \brief Clears all chat entries from the model.
114 //!
115 //! Uses beginResetModel/endResetModel for bulk changes.
116 virtual void clear();
117
118 //! \brief Finds the index of a message by its text content.
119 //!
120 //! \param messageName The message text to search for
121 //! \return Index of the message, or -1 if not found
122 virtual int getIndexOfName(const QString& messageName);
123
124 //! \brief Gets the message text at a specific index.
125 //!
126 //! \param idx The index of the message
127 //! \return The message text
128 virtual QString getMessage(const int idx);
129
130 // Required QAbstractListModel overrides for Qt Model/View framework
131
132 //! \brief Removes rows from the model.
133 //!
134 //! \param row The row index to remove
135 //! \param column Unused for list models (default 0)
136 //! \param parent Unused for list models
137 //! \return true if removal succeeded
138 virtual bool removeRows(int row, int column = 0, const QModelIndex& parent = QModelIndex()) override;
139
140 //! \brief Returns the number of entries in the model.
141 //!
142 //! \param parent Unused for list models
143 //! \return Number of chat entries
144 virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
145
146 //! \brief Returns data for a specific role at a given index.
147 //!
148 //! This is the core method QML uses to access data.
149 //!
150 //! \param index The model index of the item
151 //! \param role The data role being requested (SenderRole, MessageRole, etc.)
152 //! \return The requested data as a QVariant
153 virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
154
155 //! \brief Sets data for a specific role at a given index.
156 //!
157 //! \param index The model index of the item
158 //! \param value The new value to set
159 //! \param role The data role being modified
160 //! \return true if data was successfully set
161 virtual bool setData(const QModelIndex& index, const QVariant& value, int role) override;
162
163protected:
164 //! \brief Maps role enum values to QML property names.
165 //!
166 //! This method tells Qt how to expose roles as named properties in QML.
167 //! For example, SenderRole becomes accessible as "sender" in QML.
168 //!
169 //! \return Hash mapping role integers to QML property names
170 virtual QHash<int, QByteArray> roleNames() const override;
171
172 QList<DtChatEntry> myChatEntries; //!< Storage for all chat entries
173};
174
175//! \brief Qt Quick window managing the chat UI.
176//!
177//! This class demonstrates the REUSABLE pattern for integrating Qt Quick (QML)
178//! windows into VR-Engage. Key aspects:
179//! - Loading QML files using DtQtQuickRenderer
180//! - Exposing C++ objects to QML via context properties
181//! - Managing window visibility and lifecycle
182//! - Using Q_INVOKABLE for QML-callable methods
183//!
184//! This pattern applies to any custom Qt Quick UI that needs to be integrated
185//! with VR-Engage player stations.
186class DtExampleChatWindow : public QObject
187{
188 Q_OBJECT;
189
190public:
191 //! \brief Constructor that loads and initializes the QML chat window.
193
194 //! \brief Destructor for cleanup.
195 virtual ~DtExampleChatWindow() override;
196
197 //! \brief Sets the visibility of the chat window.
198 //!
199 //! \param visible true to show the window, false to hide it
200 virtual void setVisibility(bool visible);
201
202 //! \brief Checks if the chat window is currently visible.
203 //!
204 //! \return true if visible, false if hidden
205 virtual bool isVisible();
206
207 //! \brief Closes the chat window (hides it).
208 virtual void close();
209
210 //! \brief Opens the chat window (shows it).
211 virtual void open();
212
213 //! \brief Initializes the QML root item after loading.
214 //!
215 //! Called by the QML loader callback once the QML file is loaded.
216 virtual void initMain();
217
218 //! \brief Clears all messages from the chat window.
219 //!
220 //! Q_INVOKABLE makes this method callable from QML, allowing the UI
221 //! to trigger C++ functionality (e.g., a "Clear" button in QML).
222 Q_INVOKABLE virtual void clearChat();
223
224 //! \brief Adds a new message to the chat window.
225 //!
226 //! \param entry The chat entry to add
227 virtual void addNewMessage(const DtChatEntry& entry);
228
229 //! \brief Adds a new message to the chat window.
230 //!
231 //! Convenience overload that constructs a DtChatEntry from individual fields.
232 //!
233 //! \param sender Entity name of message sender
234 //! \param receiver Entity name of message receiver
235 //! \param time Simulation time of message
236 //! \param message Text content of message
237 virtual void addNewMessage(
238 const QString& sender, const QString& receiver, const double time, const QString& message);
239
240protected:
241 QQuickItem* myRoot; //!< Root QML item loaded from QML file
242 DtChatQmlModel myChatModel; //!< Qt model exposed to QML for data binding
243 std::string myTargetWindow; //!< Target window name (empty for standalone)
244 std::string myTargetFile; //!< QML file path relative to VR-Engage data directory
245};
246
247} // namespace makVre
Represents a single chat message entry.
Definition vreChatWindowModel.h:28
QString message() const
Gets the content of the message.
QString getReceiver() const
Gets the receiver's entity name.
void setSender(const QString &sender)
Sets the sender's entity name.
double getTime() const
Gets the simulation time of the message.
DtChatEntry()
Default constructor initializing empty chat entry.
QString mySender
Entity name of message sender.
Definition exampleQtQuick.h:64
void setTime(const double time)
Sets the simulation time of the message.
DtChatEntry(const QString &sender, const QString &receiver, const double time, const QString &message)
Constructor with all chat entry fields.
QString myMessage
Content of the chat message.
Definition vreChatWindowModel.h:109
QString getSender() const
Gets the sender's entity name.
QString getMessage() const
Gets the message text content.
void setMessage(const QString &message)
Sets the message text content.
double myTime
Simulation time of message.
Definition exampleQtQuick.h:66
void setReceiver(const QString &receiver)
Sets the receiver's entity name.
QString myReceiver
Entity name of message receiver.
Definition exampleQtQuick.h:65
Qt model for chat messages providing data to QML ListView.
Definition exampleQtQuick.h:82
DtChatQmlModel(QObject *parent=nullptr)
Constructor initializing the model.
virtual QHash< int, QByteArray > roleNames() const override
Maps role enum values to QML property names.
virtual QString getMessage(const int idx)
Gets the message text at a specific index.
virtual int getIndexOfName(const QString &messageName)
Finds the index of a message by its text content.
virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override
Sets data for a specific role at a given index.
virtual bool removeRows(int row, int column=0, const QModelIndex &parent=QModelIndex()) override
Removes rows from the model.
QList< DtChatEntry > myChatEntries
Storage for all chat entries.
Definition exampleQtQuick.h:172
ChatRoles
Custom roles for accessing chat entry fields from QML.
Definition exampleQtQuick.h:91
@ ReceiverRole
Access receiver name via "receiver" in QML.
Definition exampleQtQuick.h:93
@ MessageRole
Access message text via "msg" in QML.
Definition exampleQtQuick.h:95
@ SenderRole
Access sender name via "sender" in QML.
Definition exampleQtQuick.h:92
@ TimeRole
Access message time via "time" in QML.
Definition exampleQtQuick.h:94
virtual void clear()
Clears all chat entries from the model.
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of entries in the model.
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Returns data for a specific role at a given index.
virtual void addChatEntry(const DtChatEntry &entry)
Adds a new chat entry to the model.
virtual ~DtExampleChatWindow() override
Destructor for cleanup.
virtual void addNewMessage(const DtChatEntry &entry)
Adds a new message to the chat window.
DtExampleChatWindow()
Constructor that loads and initializes the QML chat window.
virtual Q_INVOKABLE void clearChat()
Clears all messages from the chat window.
std::string myTargetWindow
Target window name (empty for standalone)
Definition exampleQtQuick.h:243
virtual bool isVisible()
Checks if the chat window is currently visible.
virtual void addNewMessage(const QString &sender, const QString &receiver, const double time, const QString &message)
Adds a new message to the chat window.
virtual void open()
Opens the chat window (shows it).
QQuickItem * myRoot
Root QML item loaded from QML file.
Definition exampleQtQuick.h:241
virtual void close()
Closes the chat window (hides it).
std::string myTargetFile
QML file path relative to VR-Engage data directory.
Definition exampleQtQuick.h:244
virtual void initMain()
Initializes the QML root item after loading.
DtChatQmlModel myChatModel
Qt model exposed to QML for data binding.
Definition exampleQtQuick.h:242
virtual void setVisibility(bool visible)
Sets the visibility of the chat window.
Include export definitions for this library.
Definition glsVreMessageUtil.h:49