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 
25 class AssetWrapper : public QObject {
26  Q_OBJECT
27 
28  Q_PROPERTY(AssetWrapper *self READ self)
29  Q_PROPERTY(QString displayName READ displayName)
30 
31 public:
34  static const char *tipProperty;
35 
36  AssetWrapper(AssetWrapper *parent, const QString &panelName = "empty")
37  : QObject(parent), myParent(parent), myPanelName(panelName) {
38  }
39  virtual ~AssetWrapper() override {};
40 
41  AssetWrapper *self() { return this; }
42 
43  virtual QString displayName() const = 0;
44  virtual QIcon displayIcon() const { return QIcon(); }
45  virtual QString displayPanelName() const { return myPanelName; }
46  AssetWrapper *displayParent() const { return myParent; }
47  void setDisplayParent(AssetWrapper *parent) { myParent = parent; }
48 
49  virtual int childrenCount() const { return 0; }
50  virtual AssetWrapper *child(int) { return nullptr; }
51  virtual int childIndex(AssetWrapper *) { return -1; }
52 
53  virtual PossibleUse uses(const AssetWrapper *, std::string &) const { return DoesntUse; }
54 
55  void markModified(ChangeType change, int extra = 0) {
56  emit modified();
57  wasModified(this, change, extra);
58  }
59 
60  virtual bool canDrag() const { return false; }
61  virtual void drag(QMimeData *) const { }
62  virtual bool canDrop(const QMimeData *, int) const { return false; }
63  virtual void drop(const QMimeData *, int) { }
64  virtual void fillMenu(QMenu *) { }
65 
66  static bool compareByDisplayName(const AssetWrapper *left, const AssetWrapper *right) {
67  return left->displayName().compare(right->displayName(), Qt::CaseInsensitive) < 0;
68  }
69 
70 public slots:
71  virtual void removeChild(AssetWrapper *) { }
72 
73 signals:
74  void dataModified(AssetWrapper *asset, AssetWrapper::ChangeType type, int extra);
75  void modified();
76 
77 protected:
78  virtual void wasModified(AssetWrapper *asset, ChangeType type, int extra) {
79  if (myParent) {
80  myParent->wasModified(asset, type, extra);
81  } else {
82  emit dataModified(asset, type, extra);
83  }
84  }
85 
87  QString myPanelName;
88 };
89 
93 template<typename T>
94 class AssetTWrapper : public AssetWrapper {
95 public:
96  AssetTWrapper(AssetWrapper *parent, const QString &panelName, T* wrappedItem)
97  : AssetWrapper(parent, panelName) {
99  };
100 
101  T *wrappedItem() const { return myWrappedItem; }
102 
103 protected:
105 };
106 
114 #define ATW_RPROP(type_, name_, field_) \
115  public: \
116  type_ name_() const { return myWrappedItem->field_; } \
117  Q_PROPERTY(type_ name_ READ name_)
118 
126 #define ATW_PROP(type_, name_, field_) \
127  public: \
128  type_ name_() const { return myWrappedItem->field_; } \
129  void name_(const type_ &val) { myWrappedItem->field_ = val; } \
130  Q_PROPERTY(type_ name_ READ name_ WRITE name_)
131 
132 #endif /* ASSETWRAPPER_H */
AssetTWrapper::myWrappedItem
T * myWrappedItem
Definition: AssetWrapper.h:104
AssetWrapper::AfterLayoutChange
@ AfterLayoutChange
Definition: AssetWrapper.h:33
AssetWrapper::AssetUpdated
@ AssetUpdated
Definition: AssetWrapper.h:33
AssetWrapper::drag
virtual void drag(QMimeData *) const
Definition: AssetWrapper.h:61
AssetWrapper::AfterChildAdd
@ AfterChildAdd
Definition: AssetWrapper.h:33
AssetWrapper::AssetWrapper
AssetWrapper(AssetWrapper *parent, const QString &panelName="empty")
Definition: AssetWrapper.h:36
AssetWrapper::PossibleUse
PossibleUse
Definition: AssetWrapper.h:32
AssetWrapper::AfterChildRemove
@ AfterChildRemove
Definition: AssetWrapper.h:33
AssetWrapper::setDisplayParent
void setDisplayParent(AssetWrapper *parent)
Definition: AssetWrapper.h:47
AssetWrapper::removeChild
virtual void removeChild(AssetWrapper *)
Definition: AssetWrapper.h:71
AssetWrapper::displayName
QString displayName
Definition: AssetWrapper.h:29
AssetWrapper::child
virtual AssetWrapper * child(int)
Definition: AssetWrapper.h:50
AssetWrapper::tipProperty
static const char * tipProperty
Definition: AssetWrapper.h:34
AssetWrapper::ChildrenMayUse
@ ChildrenMayUse
Definition: AssetWrapper.h:32
AssetWrapper::myPanelName
QString myPanelName
Definition: AssetWrapper.h:87
AssetWrapper::canDrag
virtual bool canDrag() const
Definition: AssetWrapper.h:60
AssetWrapper::BeforeChildAdd
@ BeforeChildAdd
Definition: AssetWrapper.h:33
AssetWrapper::childrenCount
virtual int childrenCount() const
Definition: AssetWrapper.h:49
AssetWrapper
Definition: AssetWrapper.h:25
AssetWrapper::myParent
AssetWrapper * myParent
Definition: AssetWrapper.h:86
AssetWrapper::canDrop
virtual bool canDrop(const QMimeData *, int) const
Definition: AssetWrapper.h:62
AssetWrapper::~AssetWrapper
virtual ~AssetWrapper() override
Definition: AssetWrapper.h:39
AssetWrapper::ChangeType
ChangeType
Definition: AssetWrapper.h:33
AssetWrapper::fillMenu
virtual void fillMenu(QMenu *)
Definition: AssetWrapper.h:64
AssetWrapper::Uses
@ Uses
Definition: AssetWrapper.h:32
AssetWrapper::modified
void modified()
AssetTWrapper::wrappedItem
T * wrappedItem() const
Definition: AssetWrapper.h:101
AssetWrapper::childIndex
virtual int childIndex(AssetWrapper *)
Definition: AssetWrapper.h:51
AssetWrapper::drop
virtual void drop(const QMimeData *, int)
Definition: AssetWrapper.h:63
AssetWrapper::displayIcon
virtual QIcon displayIcon() const
Definition: AssetWrapper.h:44
AssetWrapper::uses
virtual PossibleUse uses(const AssetWrapper *, std::string &) const
Definition: AssetWrapper.h:53
AssetWrapper::wasModified
virtual void wasModified(AssetWrapper *asset, ChangeType type, int extra)
Definition: AssetWrapper.h:78
AssetWrapper::BeforeLayoutChange
@ BeforeLayoutChange
Definition: AssetWrapper.h:33
AssetWrapper::dataModified
void dataModified(AssetWrapper *asset, AssetWrapper::ChangeType type, int extra)
AssetWrapper::BeforeChildRemove
@ BeforeChildRemove
Definition: AssetWrapper.h:33
AssetTWrapper::AssetTWrapper
AssetTWrapper(AssetWrapper *parent, const QString &panelName, T *wrappedItem)
Definition: AssetWrapper.h:96
AssetWrapper::markModified
void markModified(ChangeType change, int extra=0)
Definition: AssetWrapper.h:55
AssetWrapper::DoesntUse
@ DoesntUse
Definition: AssetWrapper.h:32
AssetWrapper::displayPanelName
virtual QString displayPanelName() const
Definition: AssetWrapper.h:45
AssetWrapper::compareByDisplayName
static bool compareByDisplayName(const AssetWrapper *left, const AssetWrapper *right)
Definition: AssetWrapper.h:66
AssetWrapper::displayParent
AssetWrapper * displayParent() const
Definition: AssetWrapper.h:46
is_valid_types_gen.type
list type
Definition: is_valid_types_gen.py:25
AssetTWrapper
Definition: AssetWrapper.h:94