ORE Studio 0.0.4
Loading...
Searching...
No Matches
EntityController.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2024 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_QT_ENTITY_CONTROLLER_HPP
21#define ORES_QT_ENTITY_CONTROLLER_HPP
22
23#include <QObject>
24#include <QMainWindow>
25#include <QMdiArea>
26#include <QString>
27#include <QStringList>
28#include <QDateTime>
29#include <QMap>
30#include <memory>
31#include <string>
32#include <string_view>
33#include "ores.qt/ClientManager.hpp"
34#include "ores.qt/DetailDialogBase.hpp"
35#include "ores.logging/make_logger.hpp"
36
37namespace ores::qt {
38
39class DetachableMdiSubWindow;
40class EntityListMdiWindow;
41
50class EntityController : public QObject {
51 Q_OBJECT
52
53private:
54 inline static std::string_view logger_name = "ores.qt.entity_controller";
55
56 [[nodiscard]] static auto& lg() {
57 using namespace ores::logging;
58 static auto instance = make_logger(logger_name);
59 return instance;
60 }
61
62public:
74 QMainWindow* mainWindow,
75 QMdiArea* mdiArea,
76 ClientManager* clientManager,
77 const QString& username,
78 std::string_view eventName = {},
79 QObject* parent = nullptr);
80
81 ~EntityController() override;
82
86 void setClientManager(ClientManager* clientManager, const QString& username);
87
91 void setUsername(const QString& username) { username_ = username; }
92
97 virtual void showListWindow() = 0;
98
103 virtual void closeAllWindows() = 0;
104
111 void setAutoReloadOnSave(bool enabled) { autoReloadOnSave_ = enabled; }
112
113signals:
115 void statusMessage(const QString& message);
116
118 void errorMessage(const QString& message);
119
127
132
133protected:
140 QString build_window_key(const QString& windowType, const QString& identifier) const;
141
147 bool try_reuse_window(const QString& key);
148
153
157 void track_window(const QString& key, DetachableMdiSubWindow* window);
158
162 void untrack_window(const QString& key);
163
177 DetachableMdiSubWindow* referenceWindow = nullptr,
178 QPoint offset = QPoint(30, 30));
179
190
200
209 virtual EntityListMdiWindow* listWindow() const { return nullptr; }
210
215 virtual void reloadListWindow() = 0;
216
223 void handleEntitySaved();
224
231 void handleEntityDeleted();
232
233private slots:
239 void onNotificationReceived(const QString& eventType,
240 const QDateTime& timestamp,
241 const QStringList& entityIds);
242
243private:
244 void setupEventSubscription();
245 void teardownEventSubscription();
246
247protected:
248 QMainWindow* mainWindow_;
249 QMdiArea* mdiArea_;
250 ClientManager* clientManager_;
251 QString username_;
252
254 QMap<QString, DetachableMdiSubWindow*> managed_windows_;
255
257 bool autoReloadOnSave_ = false;
258
259private:
261 std::string eventName_;
262};
263
264}
265
266#endif
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Qt-based graphical user interface for ORE Studio.
Definition AboutDialog.hpp:35
Manages the lifecycle of the network client and IO context.
Definition ClientManager.hpp:90
QMdiSubWindow that can be detached to become a floating window.
Definition DetachableMdiSubWindow.hpp:39
Base class for all detail dialogs.
Definition DetailDialogBase.hpp:54
Abstract base class for entity controllers.
Definition EntityController.hpp:50
void register_detachable_window(DetachableMdiSubWindow *window)
Registers a detachable window and emits detachableWindowCreated signal.
Definition EntityController.cpp:180
virtual EntityListMdiWindow * listWindow() const
Returns the list window for marking as stale on notifications.
Definition EntityController.hpp:209
QMap< QString, DetachableMdiSubWindow * > managed_windows_
Map of active windows indexed by unique key.
Definition EntityController.hpp:254
void setAutoReloadOnSave(bool enabled)
Sets whether the list window should auto-reload after save/delete.
Definition EntityController.hpp:111
void bring_window_to_front(DetachableMdiSubWindow *window)
Activates the specified window, handling detached state.
Definition EntityController.cpp:136
bool autoReloadOnSave_
Whether to auto-reload the list window after save/delete. Default: false.
Definition EntityController.hpp:257
void detachableWindowDestroyed(DetachableMdiSubWindow *window)
Emitted when a detachable window managed by this controller is destroyed.
void untrack_window(const QString &key)
Unregisters a window from tracking.
Definition EntityController.cpp:155
void show_managed_window(DetachableMdiSubWindow *window, DetachableMdiSubWindow *referenceWindow=nullptr, QPoint offset=QPoint(30, 30))
Shows a window in the MDI area, handling detach state.
Definition EntityController.cpp:159
QString build_window_key(const QString &windowType, const QString &identifier) const
Generates a unique key for tracking windows.
Definition EntityController.cpp:120
void statusMessage(const QString &message)
Emitted when a status message should be shown to the user.
void connect_dialog_close(DetailDialogBase *dialog, DetachableMdiSubWindow *window)
Connects a dialog's closeRequested signal to the window's close slot.
Definition EntityController.cpp:175
bool try_reuse_window(const QString &key)
Tries to reuse an existing window if one exists for the key.
Definition EntityController.cpp:125
void handleEntitySaved()
Called when an entity is saved.
Definition EntityController.cpp:192
virtual void closeAllWindows()=0
Closes all windows managed by this controller. Must be implemented by derived classes.
void track_window(const QString &key, DetachableMdiSubWindow *window)
Registers a window for tracking.
Definition EntityController.cpp:150
void detachableWindowCreated(DetachableMdiSubWindow *window)
Emitted when a detachable window is created by this controller.
void setClientManager(ClientManager *clientManager, const QString &username)
Updates the client manager and username (e.g. after re-login).
Definition EntityController.cpp:114
virtual void showListWindow()=0
Shows the main list window for this entity. Must be implemented by derived classes.
virtual void reloadListWindow()=0
Reloads the list window. Must be implemented by derived classes.
void setUsername(const QString &username)
Updates just the username.
Definition EntityController.hpp:91
void errorMessage(const QString &message)
Emitted when an error message should be shown to the user.
void handleEntityDeleted()
Called when an entity is deleted.
Definition EntityController.cpp:198
Base class for entity list MDI windows providing stale indicator support.
Definition EntityListMdiWindow.hpp:56