1 #ifndef CSV_WORLD_ENUMDELEGATE_H
2 #define CSV_WORLD_ENUMDELEGATE_H
3 
4 #include <vector>
5 
6 #include <QString>
7 #include <QStyledItemDelegate>
8 
9 #include <components/esm/defs.hpp>
10 
11 #include "util.hpp"
12 
13 namespace CSVWorld
14 {
15     /// \brief Integer value that represents an enum and is interacted with via a combobox
16     class EnumDelegate : public CommandDelegate
17     {
18         protected:
19 
20             std::vector<std::pair<int, QString> > mValues;
21 
22             int getValueIndex(const QModelIndex &index, int role = Qt::DisplayRole) const;
23 
24         private:
25 
26             void setModelDataImp (QWidget *editor, QAbstractItemModel *model,
27                 const QModelIndex& index) const override;
28 
29             virtual void addCommands (QAbstractItemModel *model,
30                 const QModelIndex& index, int type) const;
31 
32         public:
33 
34             EnumDelegate (const std::vector<std::pair<int, QString> >& values,
35                 CSMWorld::CommandDispatcher *dispatcher, CSMDoc::Document& document, QObject *parent);
36 
37             QWidget *createEditor(QWidget *parent,
38                                           const QStyleOptionViewItem& option,
39                                           const QModelIndex& index) const override;
40 
41             QWidget *createEditor(QWidget *parent,
42                                           const QStyleOptionViewItem& option,
43                                           const QModelIndex& index,
44                                           CSMWorld::ColumnBase::Display display = CSMWorld::ColumnBase::Display_None) const override;
45 
46             void setEditorData (QWidget *editor, const QModelIndex& index, bool tryDisplay = false) const override;
47 
48             void paint (QPainter *painter, const QStyleOptionViewItem& option,
49                 const QModelIndex& index) const override;
50 
51             QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
52 
53     };
54 
55     class EnumDelegateFactory : public CommandDelegateFactory
56     {
57         protected:
58             std::vector<std::pair<int, QString> > mValues;
59 
60         public:
61 
62             EnumDelegateFactory();
63 
64             EnumDelegateFactory (const char **names, bool allowNone = false);
65             ///< \param names Array of char pointer with a 0-pointer as end mark
66             /// \param allowNone Use value of -1 for "none selected" (empty string)
67 
68             EnumDelegateFactory (const std::vector<std::pair<int,std::string>>& names, bool allowNone = false);
69             /// \param allowNone Use value of -1 for "none selected" (empty string)
70 
71             CommandDelegate *makeDelegate (CSMWorld::CommandDispatcher *dispatcher, CSMDoc::Document& document, QObject *parent) const override;
72             ///< The ownership of the returned CommandDelegate is transferred to the caller.
73 
74             void add (int value, const QString& name);
75     };
76 
77 
78 }
79 
80 #endif
81