LibrePCB Developers Documentation
categorytreemodel.h
Go to the documentation of this file.
1/*
2 * LibrePCB - Professional EDA for everyone!
3 * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4 * https://librepcb.org/
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef LIBREPCB_EDITOR_CATEGORYTREEMODEL_H
21#define LIBREPCB_EDITOR_CATEGORYTREEMODEL_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
27
28#include <QtCore>
29
30#include <memory>
31
32/*******************************************************************************
33 * Namespace / Forward Declarations
34 ******************************************************************************/
35namespace librepcb {
36
37class WorkspaceLibraryDb;
38
39namespace editor {
40
41/*******************************************************************************
42 * Class CategoryTreeModel
43 ******************************************************************************/
44
48class CategoryTreeModel final : public QAbstractItemModel {
49 struct Item {
50 std::weak_ptr<Item> parent;
51 tl::optional<Uuid> uuid;
52 QString text;
53 QString tooltip;
54 QVector<std::shared_ptr<Item>> childs;
55 };
56
57public:
58 // Types
59 enum class Filter {
61 CmpCat = 1 << 0,
63 CmpCatWithSymbols = 1 << 1,
65 CmpCatWithComponents = 1 << 2,
67 CmpCatWithDevices = 1 << 3,
69 PkgCat = 1 << 4,
71 PkgCatWithPackages = 1 << 5,
72 };
73 Q_DECLARE_FLAGS(Filters, Filter)
74
75 // Constructors / Destructor
77 CategoryTreeModel(const CategoryTreeModel& other) = delete;
78 explicit CategoryTreeModel(const WorkspaceLibraryDb& library,
79 const QStringList& localeOrder,
80 Filters filters) noexcept;
81 ~CategoryTreeModel() noexcept;
82
83 // Setters
84 void setLocaleOrder(const QStringList& order) noexcept;
85
86 // Inherited Methods
87 int columnCount(
88 const QModelIndex& parent = QModelIndex()) const noexcept override;
89 int rowCount(
90 const QModelIndex& parent = QModelIndex()) const noexcept override;
91 QModelIndex index(
92 int row, int column,
93 const QModelIndex& parent = QModelIndex()) const noexcept override;
94 QModelIndex parent(const QModelIndex& index) const noexcept override;
95 QVariant headerData(int section, Qt::Orientation orientation,
96 int role = Qt::DisplayRole) const noexcept override;
97 QVariant data(const QModelIndex& index,
98 int role = Qt::DisplayRole) const noexcept override;
99
100 // Operator Overloadings
101 CategoryTreeModel& operator=(const CategoryTreeModel& rhs) = delete;
102
103private: // Methods
104 void update() noexcept;
105 QVector<std::shared_ptr<Item>> getChilds(
106 std::shared_ptr<Item> parent) const noexcept;
107 bool containsItems(const tl::optional<Uuid>& uuid) const;
108 bool listAll() const noexcept;
109 bool listPackageCategories() const noexcept;
110 void updateModelItem(
111 std::shared_ptr<Item> parentItem,
112 const QVector<std::shared_ptr<Item>>& newChilds) noexcept;
113 Item* itemFromIndex(const QModelIndex& index) const noexcept;
114 QModelIndex indexFromItem(const Item* item) const noexcept;
115
116private: // Data
118 QStringList mLocaleOrder;
119 const Filters mFilters;
120 std::shared_ptr<Item> mRootItem;
121};
122
123} // namespace editor
124} // namespace librepcb
125
126Q_DECLARE_OPERATORS_FOR_FLAGS(librepcb::editor::CategoryTreeModel::Filters)
127
128/*******************************************************************************
129 * End of File
130 ******************************************************************************/
131
132#endif
The Uuid class is a replacement for QUuid to get UUID strings without {} braces.
Definition: uuid.h:58
The WorkspaceLibraryDb class.
Definition: workspacelibrarydb.h:58
The CategoryTreeModel class.
Definition: categorytreemodel.h:48
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const noexcept override
Definition: categorytreemodel.cpp:101
Item * itemFromIndex(const QModelIndex &index) const noexcept
Definition: categorytreemodel.cpp:283
void updateModelItem(std::shared_ptr< Item > parentItem, const QVector< std::shared_ptr< Item > > &newChilds) noexcept
Definition: categorytreemodel.cpp:239
bool listPackageCategories() const noexcept
Definition: categorytreemodel.cpp:234
bool containsItems(const tl::optional< Uuid > &uuid) const
Definition: categorytreemodel.cpp:207
std::shared_ptr< Item > mRootItem
Definition: categorytreemodel.h:120
QModelIndex parent(const QModelIndex &index) const noexcept override
Definition: categorytreemodel.cpp:93
QModelIndex indexFromItem(const Item *item) const noexcept
Definition: categorytreemodel.cpp:298
bool listAll() const noexcept
Definition: categorytreemodel.cpp:230
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const noexcept override
Definition: categorytreemodel.cpp:83
int columnCount(const QModelIndex &parent=QModelIndex()) const noexcept override
Definition: categorytreemodel.cpp:73
int rowCount(const QModelIndex &parent=QModelIndex()) const noexcept override
Definition: categorytreemodel.cpp:78
const Filters mFilters
Definition: categorytreemodel.h:119
void update() noexcept
Definition: categorytreemodel.cpp:135
Filter
Definition: categorytreemodel.h:59
QStringList mLocaleOrder
Definition: categorytreemodel.h:118
void setLocaleOrder(const QStringList &order) noexcept
Definition: categorytreemodel.cpp:62
QVector< std::shared_ptr< Item > > getChilds(std::shared_ptr< Item > parent) const noexcept
Definition: categorytreemodel.cpp:163
const WorkspaceLibraryDb & mLibrary
Definition: categorytreemodel.h:117
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const noexcept override
Definition: categorytreemodel.cpp:114
Definition: occmodel.cpp:77
Definition: uuid.h:183
Definition: categorytreemodel.h:49
QString text
Definition: categorytreemodel.h:52
QString tooltip
Definition: categorytreemodel.h:53
QVector< std::shared_ptr< Item > > childs
Definition: categorytreemodel.h:54
tl::optional< Uuid > uuid
tl::nullopt for items without category
Definition: categorytreemodel.h:51
std::weak_ptr< Item > parent
nullptr for root categories
Definition: categorytreemodel.h:50