ORE Studio 0.0.4
Loading...
Searching...
No Matches
ConnectionTreeModel.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 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_CONNECTION_TREE_MODEL_HPP
21#define ORES_QT_CONNECTION_TREE_MODEL_HPP
22
23#include <QAbstractItemModel>
24#include <QIcon>
25#include <memory>
26#include <vector>
27#include <optional>
28#include <unordered_set>
29#include <boost/uuid/uuid.hpp>
30#include <boost/uuid/uuid_hash.hpp>
31#include "ores.connections/domain/folder.hpp"
32#include "ores.connections/domain/environment.hpp"
33#include "ores.connections/domain/connection.hpp"
34#include "ores.logging/make_logger.hpp"
35
36namespace ores::connections::service {
37class connection_manager;
38}
39
40namespace ores::qt {
41
46 enum class Type { Root, Folder, Environment, Connection };
47
48 Type type{Type::Root};
49 boost::uuids::uuid id{};
50 QString name;
51 std::optional<boost::uuids::uuid> parent_id;
52
53 // Environment / Connection-specific data
54 QString host;
55 int port{0};
56 QString username;
57 QString description;
58 std::optional<boost::uuids::uuid> environment_id;
59
60 // Tree structure
61 ConnectionTreeNode* parent{nullptr};
62 std::vector<std::unique_ptr<ConnectionTreeNode>> children;
63
64 ConnectionTreeNode() = default;
65 ~ConnectionTreeNode() = default;
66
68 ConnectionTreeNode& operator=(const ConnectionTreeNode&) = delete;
70 ConnectionTreeNode& operator=(ConnectionTreeNode&&) = default;
71
72 int row() const;
73};
74
83class ConnectionTreeModel : public QAbstractItemModel {
84 Q_OBJECT
85
86private:
87 inline static std::string_view logger_name =
88 "ores.qt.connection_tree_model";
89
90 [[nodiscard]] static auto& lg() {
91 using namespace ores::logging;
92 static auto instance = make_logger(logger_name);
93 return instance;
94 }
95
96public:
97 enum Column {
98 Name = 0,
99 Host,
100 Port,
101 Username,
102 ColumnCount
103 };
104
105 enum Role {
106 NodeTypeRole = Qt::UserRole + 1,
107 UuidRole,
108 IsEnvironmentRole,
109 IsConnectionRole,
110 IsFolderRole,
111 TagsRole
112 };
113
114 explicit ConnectionTreeModel(
116 QObject* parent = nullptr);
117 ~ConnectionTreeModel() override;
118
119 // QAbstractItemModel interface
120 QModelIndex index(int row, int column,
121 const QModelIndex& parent = QModelIndex()) const override;
122 QModelIndex parent(const QModelIndex& index) const override;
123 int rowCount(const QModelIndex& parent = QModelIndex()) const override;
124 int columnCount(const QModelIndex& parent = QModelIndex()) const override;
125 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
126 QVariant headerData(int section, Qt::Orientation orientation,
127 int role = Qt::DisplayRole) const override;
128 Qt::ItemFlags flags(const QModelIndex& index) const override;
129 bool setData(const QModelIndex& index, const QVariant& value,
130 int role = Qt::EditRole) override;
131
132 // Drag and drop support
133 Qt::DropActions supportedDropActions() const override;
134 QStringList mimeTypes() const override;
135 QMimeData* mimeData(const QModelIndexList& indexes) const override;
136 bool canDropMimeData(const QMimeData* data, Qt::DropAction action,
137 int row, int column, const QModelIndex& parent) const override;
138 bool dropMimeData(const QMimeData* data, Qt::DropAction action,
139 int row, int column, const QModelIndex& parent) override;
140
141 // Data access
142 ConnectionTreeNode* nodeFromIndex(const QModelIndex& index) const;
143 QModelIndex indexFromUuid(const boost::uuids::uuid& id) const;
144
145 // Data operations
146 void refresh();
147
148 // Expansion state (for folder icons)
149 void setFolderExpanded(const QModelIndex& index, bool expanded);
150 const std::unordered_set<boost::uuids::uuid>& expandedFolders() const { return expandedFolders_; }
151
152 // Get domain objects from selection
153 std::optional<connections::domain::folder> getFolderFromIndex(
154 const QModelIndex& index) const;
155 std::optional<connections::domain::environment> getEnvironmentFromIndex(
156 const QModelIndex& index) const;
157 std::optional<connections::domain::connection> getConnectionFromIndex(
158 const QModelIndex& index) const;
159
160signals:
161 void dataRefreshed();
162 void errorOccurred(const QString& message);
163
164private:
165 void buildTree();
166 void buildFolderNodes(ConnectionTreeNode* parentNode,
167 const std::optional<boost::uuids::uuid>& parentId);
168 void buildEnvironmentNodes(ConnectionTreeNode* parentNode,
169 const std::optional<boost::uuids::uuid>& folderId);
170 void buildConnectionNodes(ConnectionTreeNode* parentNode,
171 const std::optional<boost::uuids::uuid>& folderId);
172 QModelIndex indexFromNode(ConnectionTreeNode* node, int column = 0) const;
173 QModelIndex findNodeIndex(ConnectionTreeNode* searchNode,
174 const boost::uuids::uuid& id) const;
175
177 std::unique_ptr<ConnectionTreeNode> rootNode_;
178 QIcon folderIcon_;
179 QIcon folderOpenIcon_;
180 QIcon serverIcon_;
181 QIcon plugIcon_;
182 std::unordered_set<boost::uuids::uuid> expandedFolders_;
183};
184
185}
186
187#endif
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Qt-based graphical user interface for ORE Studio.
Definition AboutDialog.hpp:29
High-level service for managing server connections and environments.
Definition connection_manager.hpp:53
Tree node representing a folder, pure environment, or connection.
Definition ConnectionTreeModel.hpp:45
QString username
Connection only.
Definition ConnectionTreeModel.hpp:56
std::optional< boost::uuids::uuid > environment_id
Connection only.
Definition ConnectionTreeModel.hpp:58
Tree model for displaying folders, environments, and connections.
Definition ConnectionTreeModel.hpp:83