Crossfire Server, Trunk
AssetWrapper.h
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 2022 the Crossfire Development Team
5  *
6  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
7  * welcome to redistribute it under certain conditions. For details, please
8  * see COPYING and LICENSE.
9  *
10  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
11  */
12 
13 #ifndef ASSETWRAPPER_H
14 #define ASSETWRAPPER_H
15 
16 #include <QObject>
17 #include <QIcon>
18 
19 class QMimeData;
20 class QMenu;
21 
22 class AssetWrapper : public QObject {
23  Q_OBJECT
24 
25  Q_PROPERTY(AssetWrapper *self READ self)
26 
27 public:
30  static const char *tipProperty;
31 
32  AssetWrapper(AssetWrapper *parent, const QString &panelName = "empty")
33  : QObject(parent), myParent(parent), myPanelName(panelName) {
34  }
35  virtual ~AssetWrapper() override {};
36 
37  AssetWrapper *self() { return this; }
38 
39  virtual QString displayName() const = 0;
40  virtual QIcon displayIcon() const { return QIcon(); }
41  virtual QString displayPanelName() const { return myPanelName; }
42  AssetWrapper *displayParent() const { return myParent; }
43  void setDisplayParent(AssetWrapper *parent) { myParent = parent; }
44 
45  virtual int childrenCount() const { return 0; }
46  virtual AssetWrapper *child(int) { return nullptr; }
47  virtual int childIndex(AssetWrapper *) { return -1; }
48 
49  virtual PossibleUse uses(const AssetWrapper *, std::string &) const { return DoesntUse; }
50 
51  void markModified(ChangeType change, int extra = 0) {
52  emit modified();
53  wasModified(this, change, extra);
54  }
55 
56  virtual bool canDrag() const { return false; }
57  virtual void drag(QMimeData *) const { }
58  virtual bool canDrop(const QMimeData *, int) const { return false; }
59  virtual void drop(const QMimeData *, int) { }
60  virtual void fillMenu(QMenu *) { }
61 
62  static bool compareByDisplayName(const AssetWrapper *left, const AssetWrapper *right) {
63  return left->displayName().compare(right->displayName(), Qt::CaseInsensitive) < 0;
64  }
65 
66 public slots:
67  virtual void removeChild(AssetWrapper *) { }
68 
69 signals:
70  void dataModified(AssetWrapper *asset, AssetWrapper::ChangeType type, int extra);
71  void modified();
72 
73 protected:
74  virtual void wasModified(AssetWrapper *asset, ChangeType type, int extra) {
75  if (myParent) {
76  myParent->wasModified(asset, type, extra);
77  } else {
78  emit dataModified(asset, type, extra);
79  }
80  }
81 
83  QString myPanelName;
84 };
85 
86 template<typename T>
87 class AssetTWrapper : public AssetWrapper {
88 public:
89  AssetTWrapper(AssetWrapper *parent, const QString &panelName, T* wrappedItem)
90  : AssetWrapper(parent, panelName) {
92  };
93 
94  T *wrappedItem() const { return myWrappedItem; }
95 
96 protected:
98 };
99 
100 #endif /* ASSETWRAPPER_H */
AssetTWrapper::myWrappedItem
T * myWrappedItem
Definition: AssetWrapper.h:97
AssetWrapper::AfterLayoutChange
@ AfterLayoutChange
Definition: AssetWrapper.h:29
AssetWrapper::AssetUpdated
@ AssetUpdated
Definition: AssetWrapper.h:29
AssetWrapper::drag
virtual void drag(QMimeData *) const
Definition: AssetWrapper.h:57
AssetWrapper::AfterChildAdd
@ AfterChildAdd
Definition: AssetWrapper.h:29
AssetWrapper::AssetWrapper
AssetWrapper(AssetWrapper *parent, const QString &panelName="empty")
Definition: AssetWrapper.h:32
AssetWrapper::PossibleUse
PossibleUse
Definition: AssetWrapper.h:28
AssetWrapper::AfterChildRemove
@ AfterChildRemove
Definition: AssetWrapper.h:29
AssetWrapper::setDisplayParent
void setDisplayParent(AssetWrapper *parent)
Definition: AssetWrapper.h:43
AssetWrapper::removeChild
virtual void removeChild(AssetWrapper *)
Definition: AssetWrapper.h:67
AssetWrapper::child
virtual AssetWrapper * child(int)
Definition: AssetWrapper.h:46
AssetWrapper::tipProperty
static const char * tipProperty
Definition: AssetWrapper.h:30
AssetWrapper::ChildrenMayUse
@ ChildrenMayUse
Definition: AssetWrapper.h:28
AssetWrapper::myPanelName
QString myPanelName
Definition: AssetWrapper.h:83
AssetWrapper::canDrag
virtual bool canDrag() const
Definition: AssetWrapper.h:56
AssetWrapper::BeforeChildAdd
@ BeforeChildAdd
Definition: AssetWrapper.h:29
AssetWrapper::childrenCount
virtual int childrenCount() const
Definition: AssetWrapper.h:45
AssetWrapper
Definition: AssetWrapper.h:22
AssetWrapper::myParent
AssetWrapper * myParent
Definition: AssetWrapper.h:82
AssetWrapper::canDrop
virtual bool canDrop(const QMimeData *, int) const
Definition: AssetWrapper.h:58
AssetWrapper::~AssetWrapper
virtual ~AssetWrapper() override
Definition: AssetWrapper.h:35
AssetWrapper::ChangeType
ChangeType
Definition: AssetWrapper.h:29
AssetWrapper::fillMenu
virtual void fillMenu(QMenu *)
Definition: AssetWrapper.h:60
AssetWrapper::Uses
@ Uses
Definition: AssetWrapper.h:28
AssetWrapper::modified
void modified()
AssetTWrapper::wrappedItem
T * wrappedItem() const
Definition: AssetWrapper.h:94
AssetWrapper::childIndex
virtual int childIndex(AssetWrapper *)
Definition: AssetWrapper.h:47
AssetWrapper::drop
virtual void drop(const QMimeData *, int)
Definition: AssetWrapper.h:59
AssetWrapper::displayIcon
virtual QIcon displayIcon() const
Definition: AssetWrapper.h:40
AssetWrapper::uses
virtual PossibleUse uses(const AssetWrapper *, std::string &) const
Definition: AssetWrapper.h:49
AssetWrapper::wasModified
virtual void wasModified(AssetWrapper *asset, ChangeType type, int extra)
Definition: AssetWrapper.h:74
AssetWrapper::BeforeLayoutChange
@ BeforeLayoutChange
Definition: AssetWrapper.h:29
AssetWrapper::dataModified
void dataModified(AssetWrapper *asset, AssetWrapper::ChangeType type, int extra)
AssetWrapper::BeforeChildRemove
@ BeforeChildRemove
Definition: AssetWrapper.h:29
AssetTWrapper::AssetTWrapper
AssetTWrapper(AssetWrapper *parent, const QString &panelName, T *wrappedItem)
Definition: AssetWrapper.h:89
AssetWrapper::markModified
void markModified(ChangeType change, int extra=0)
Definition: AssetWrapper.h:51
AssetWrapper::DoesntUse
@ DoesntUse
Definition: AssetWrapper.h:28
AssetWrapper::displayPanelName
virtual QString displayPanelName() const
Definition: AssetWrapper.h:41
AssetWrapper::compareByDisplayName
static bool compareByDisplayName(const AssetWrapper *left, const AssetWrapper *right)
Definition: AssetWrapper.h:62
AssetWrapper::displayParent
AssetWrapper * displayParent() const
Definition: AssetWrapper.h:42
is_valid_types_gen.type
list type
Definition: is_valid_types_gen.py:25
AssetWrapper::displayName
virtual QString displayName() const =0
AssetTWrapper
Definition: AssetWrapper.h:87