/home/docs/checkouts/readthedocs.org/user_builds/mathvizanimator/checkouts/latest/libs/mva_workflow/include/workflow/itemhandler.h Source File

MathVizAnimator: /home/docs/checkouts/readthedocs.org/user_builds/mathvizanimator/checkouts/latest/libs/mva_workflow/include/workflow/itemhandler.h Source File
MathVizAnimator  0.0.1
itemhandler.h
1 /* mathvizanimator
2  * Copyright (C) 2023 codingwithmagga
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef LIBS_MVA_WORKFLOW_INCLUDE_WORKFLOW_ITEMHANDLER_H_
19 #define LIBS_MVA_WORKFLOW_INCLUDE_WORKFLOW_ITEMHANDLER_H_
20 
21 #include <QItemSelectionModel>
22 #include <QObject>
23 #include <QQuickItem>
24 #include <QStandardItemModel>
25 
26 #include "basic_item.h"
27 #include "item_observer.h"
28 
29 class ItemModelItem;
30 
31 struct ItemProperty {
32  QString name;
33  QVariant value;
34 };
35 
36 class PropertyModel : public QStandardItemModel {
37  public:
47  void appendProperty(const ItemProperty& property);
48 
58  void appendProperties(const PropertyMap& properties);
59 
60  Qt::ItemFlags flags(const QModelIndex& index) const override;
61 };
62 
63 class ItemHandler : public QObject {
64  Q_OBJECT
65 
66  public:
67  enum ItemRoles { ITEM_OBSERVER = Qt::UserRole + 1 };
68 
69  explicit ItemHandler(QObject* parent = nullptr);
70 
71  qsizetype numItems() const { return m_item_model.rowCount(); }
72  QStandardItemModel* model() { return &m_item_model; }
73  QItemSelectionModel* selectionModel() { return &m_item_selection_model; }
74  PropertyModel* propertyModel() { return &m_property_model; }
75  QStandardItemModel* animationModel() { return &m_animation_model; }
76  QList<BasicItem*> basicItems();
77  QList<QSharedPointer<ItemObserver>> items();
78 
79  void scaleItems(const qreal width_ratio, const qreal height_ratio);
80 
81  public slots:
82  void clear();
83 
84  void addItem(BasicItem* const basic_item);
85  void removeItem(BasicItem* const basic_item);
86  void setCurrentItem(const QString& itemName);
87  void removeCurrentItem();
88 
89  void addAnimation(
90  const QString& item_name, const QString& animation_type, const qreal start_time, const qreal duration);
91  void addAnimations(const QString& item_name, const QList<QSharedPointer<AbstractAnimation>> animations);
92  void removeAnimation(const qint32 animation_number);
93 
94  void setTime(const qreal time);
95  void changeProperty(const QString& item_name, const QByteArray& property, const QVariant& value);
96 
97  private slots:
98  void propertyDataChanged(
99  const QModelIndex& topLeft, const QModelIndex& bottomRight, const QList<qint32>& roles = QList<qint32>());
100  void currentItemChanged(const QModelIndex& current, const QModelIndex& previous);
101 
102  private:
103  struct ItemExtract {
104  AbstractItem* item;
105  bool error;
106  };
107 
108  bool itemAlreadyExists(BasicItem* const basic_item);
109  bool itemNameAlreadyExists(const QString& name);
110 
111  QString prepareNewItemName(const QString& old_item_name);
112 
113  void repopulatePropertyModel(const QModelIndex& currentIndex);
114  void repopulateAnimationModel(const ItemModelItem* const item);
115 
116  void setDeleteEachQuickItem(QModelIndex parent = QModelIndex());
117 
118  ItemModelItem* getItemModelItemByName(const QString& item_name);
119  QSharedPointer<ItemObserver> getItemObserverByName(const QString& item_name);
120 
121  void prepareModelHeader();
122  void updateItemModelName(const QVariant& name);
123 
124  QStandardItemModel m_item_model;
125  QStandardItemModel m_animation_model;
126  QItemSelectionModel m_item_selection_model;
127 
128  PropertyModel m_property_model;
129 };
130 
131 class ItemModelItem : public QStandardItem {
132  public:
133  explicit ItemModelItem(const QString& text);
134 
135  ~ItemModelItem();
136 
137  void setDeleteQuickitem(const bool delete_quick_item) { m_delete_quick_item = delete_quick_item; }
138 
139  QSharedPointer<ItemObserver> itemObserver() const;
140  void setItemObserver(const QSharedPointer<ItemObserver>& new_item_observer);
141 
142  private:
143  /*
144  * This variable is necessary to determine if the (possibly) containing
145  * QQuickItem should be deleted or not. If the user deletes the item it should
146  * be set to true, to remove the item from the GUI. If the app gets closed,
147  * everything gets automatically deleted from the GUI and this variable should
148  * be set (or keeped) false. This is because in some cases both the GUI and
149  * the deconstructor of this class seems to try to delete the item
150  * simultaneously. This leads to a segmentation fault.
151  *
152  * I'm not really happy with this solution, because current extensions could
153  * break this workflow. This should be done automatically.
154  *
155  * TODO(codingwithmagga): GUI deletes the QQuickItem. The QQuickItem or
156  * better the containing AbstractItem should emit a signal or sth. similar
157  * when they get destroyed. Then this ItemModelItem should removed. So it
158  * goes the other way around. Not sure if this solves the problem when
159  * shutting down the app.
160  */
161  bool m_delete_quick_item = false;
162 
163  QSharedPointer<ItemObserver> m_item_observer;
164 };
165 
166 #endif // LIBS_MVA_WORKFLOW_INCLUDE_WORKFLOW_ITEMHANDLER_H_
An abstract class representing a visible item like a circle or a rectangle.
Definition: abstractitem.h:56
Definition: basic_item.h:28
Definition: itemhandler.h:63
Definition: itemhandler.h:131
Map which stores key (QString) value (QVariant) pairs.
Definition: abstractitem.h:37
Definition: itemhandler.h:36
void appendProperties(const PropertyMap &properties)
Appends all properties from the given property map to the model.
Definition: itemhandler.cpp:431
void appendProperty(const ItemProperty &property)
Appends a property to the model.
Definition: itemhandler.cpp:423
Definition: itemhandler.h:31