1 #ifndef CSM_WOLRD_COLUMNBASE_H
2 #define CSM_WOLRD_COLUMNBASE_H
3 
4 #include <string>
5 #include <vector>
6 #include <stdexcept>
7 
8 #include <Qt>
9 #include <QVariant>
10 
11 #include "record.hpp"
12 
13 namespace CSMWorld
14 {
15     struct ColumnBase
16     {
17         enum TableEditModes
18         {
19             TableEdit_None,      // no editing
20             TableEdit_Full,      // edit cells and add/remove rows
21             TableEdit_FixedRows  // edit cells only
22         };
23 
24         enum Roles
25         {
26             Role_Flags = Qt::UserRole,
27             Role_Display = Qt::UserRole+1,
28             Role_ColumnId = Qt::UserRole+2
29         };
30 
31         enum Flags
32         {
33             Flag_Table = 1, // column should be displayed in table view
34             Flag_Dialogue = 2, // column should be displayed in dialogue view
35             Flag_Dialogue_List = 4, // column should be diaplyed in dialogue view
36             Flag_Dialogue_Refresh = 8 // refresh dialogue view if this column is modified
37         };
38 
39         enum Display
40         {
41             Display_None, //Do not use
42             Display_String,
43             Display_LongString,
44 
45             //CONCRETE TYPES STARTS HERE (for drag and drop)
46             Display_Skill,
47             Display_Class,
48             Display_Faction,
49             Display_Rank,
50             Display_Race,
51             Display_Sound,
52             Display_Region,
53             Display_Birthsign,
54             Display_Spell,
55             Display_Cell,
56             Display_Referenceable,
57             Display_Activator,
58             Display_Potion,
59             Display_Apparatus,
60             Display_Armor,
61             Display_Book,
62             Display_Clothing,
63             Display_Container,
64             Display_Creature,
65             Display_Door,
66             Display_Ingredient,
67             Display_CreatureLevelledList,
68             Display_ItemLevelledList,
69             Display_Light,
70             Display_Lockpick,
71             Display_Miscellaneous,
72             Display_Npc,
73             Display_Probe,
74             Display_Repair,
75             Display_Static,
76             Display_Weapon,
77             Display_Reference,
78             Display_Filter,
79             Display_Topic,
80             Display_Journal,
81             Display_TopicInfo,
82             Display_JournalInfo,
83             Display_Scene,
84             Display_GlobalVariable,
85             Display_BodyPart,
86             Display_Enchantment,
87             //CONCRETE TYPES ENDS HERE
88 
89             Display_SignedInteger8,
90             Display_SignedInteger16,
91             Display_UnsignedInteger8,
92             Display_UnsignedInteger16,
93             Display_Integer,
94             Display_Float,
95             Display_Double,
96             Display_Var,
97             Display_GmstVarType,
98             Display_GlobalVarType,
99             Display_Specialisation,
100             Display_Attribute,
101             Display_Boolean,
102             Display_SpellType,
103             Display_Script,
104             Display_ApparatusType,
105             Display_ArmorType,
106             Display_ClothingType,
107             Display_CreatureType,
108             Display_WeaponType,
109             Display_RecordState,
110             Display_RefRecordType,
111             Display_DialogueType,
112             Display_QuestStatusType,
113             Display_EnchantmentType,
114             Display_BodyPartType,
115             Display_MeshType,
116             Display_Gender,
117             Display_Mesh,
118             Display_Icon,
119             Display_Music,
120             Display_SoundRes,
121             Display_Texture,
122             Display_Video,
123             Display_Colour,
124             Display_ScriptFile,
125             Display_ScriptLines, // console context
126             Display_SoundGeneratorType,
127             Display_School,
128             Display_Id,
129             Display_SkillId,
130             Display_EffectRange,
131             Display_EffectId,
132             Display_PartRefType,
133             Display_AiPackageType,
134             Display_InfoCondFunc,
135             Display_InfoCondVar,
136             Display_InfoCondComp,
137             Display_String32,
138             Display_LongString256,
139             Display_BookType,
140             Display_BloodType,
141             Display_EmitterType,
142 
143             Display_EffectSkill,     // must display at least one, unlike Display_Skill
144             Display_EffectAttribute, // must display at least one, unlike Display_Attribute
145             Display_IngredEffectId,  // display none allowed, unlike Display_EffectId
146             Display_GenderNpc,       // must display at least one, unlike Display_Gender
147 
148             //top level columns that nest other columns
149             Display_NestedHeader
150         };
151 
152         int mColumnId;
153         int mFlags;
154         Display mDisplayType;
155 
156         ColumnBase (int columnId, Display displayType, int flag);
157 
158         virtual ~ColumnBase();
159 
160         virtual bool isEditable() const = 0;
161 
162         virtual bool isUserEditable() const;
163         ///< Can this column be edited directly by the user?
164 
165         virtual std::string getTitle() const;
166 
167         virtual int getId() const;
168 
169         static bool isId (Display display);
170 
171         static bool isText (Display display);
172 
173         static bool isScript (Display display);
174     };
175 
176     class NestableColumn : public ColumnBase
177     {
178         std::vector<NestableColumn *> mNestedColumns;
179 
180     public:
181 
182         NestableColumn(int columnId, Display displayType, int flag);
183 
184         ~NestableColumn();
185 
186         void addColumn(CSMWorld::NestableColumn *column);
187 
188         const ColumnBase& nestedColumn(int subColumn) const;
189 
190         bool hasChildren() const;
191     };
192 
193     template<typename ESXRecordT>
194     struct Column : public NestableColumn
195     {
ColumnCSMWorld::Column196         Column (int columnId, Display displayType, int flags = Flag_Table | Flag_Dialogue)
197         : NestableColumn (columnId, displayType, flags) {}
198 
199         virtual QVariant get (const Record<ESXRecordT>& record) const = 0;
200 
setCSMWorld::Column201         virtual void set (Record<ESXRecordT>& record, const QVariant& data)
202         {
203             throw std::logic_error ("Column " + getTitle() + " is not editable");
204         }
205     };
206 
207     template<typename ESXRecordT>
208     struct NestedParentColumn : public Column<ESXRecordT>
209     {
NestedParentColumnCSMWorld::NestedParentColumn210         NestedParentColumn (int id, int flags = ColumnBase::Flag_Dialogue, bool fixedRows = false)
211             : Column<ESXRecordT> (id, ColumnBase::Display_NestedHeader, flags), mFixedRows(fixedRows)
212         {}
213 
setCSMWorld::NestedParentColumn214         void set (Record<ESXRecordT>& record, const QVariant& data) override
215         {
216             // There is nothing to do here.
217             // This prevents exceptions from parent's implementation
218         }
219 
getCSMWorld::NestedParentColumn220         QVariant get (const Record<ESXRecordT>& record) const override
221         {
222             // by default editable; also see IdTree::hasChildren()
223             if (mFixedRows)
224                 return QVariant::fromValue(ColumnBase::TableEdit_FixedRows);
225             else
226                 return QVariant::fromValue(ColumnBase::TableEdit_Full);
227         }
228 
isEditableCSMWorld::NestedParentColumn229         bool isEditable() const override
230         {
231             return true;
232         }
233 
234     private:
235         bool mFixedRows;
236     };
237 
238     struct NestedChildColumn : public NestableColumn
239     {
240         NestedChildColumn (int id,
241                 Display display, int flags = ColumnBase::Flag_Dialogue, bool isEditable = true);
242 
243         bool isEditable() const override;
244 
245     private:
246         bool mIsEditable;
247     };
248 }
249 
250 Q_DECLARE_METATYPE(CSMWorld::ColumnBase::TableEditModes)
251 
252 #endif
253