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