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