1 #ifndef FUNCTIONSEDITORMODEL_H
2 #define FUNCTIONSEDITORMODEL_H
3 
4 #include "services/config.h"
5 #include "services/functionmanager.h"
6 #include "guiSQLiteStudio_global.h"
7 #include <QIcon>
8 #include <QAbstractListModel>
9 
10 class GUI_API_EXPORT FunctionsEditorModel : public QAbstractListModel
11 {
12         Q_OBJECT
13 
14     public:
15         using QAbstractItemModel::setData;
16 
17         enum Role
18         {
19             CODE = 1000,
20             MODIFIED = 1001,
21             VALID = 1002,
22             TYPE = 1003
23         };
24 
25         explicit FunctionsEditorModel(QObject *parent = 0);
26 
27         void clearModified();
28         bool isModified() const;
29         bool isModified(int row) const;
30         void setModified(int row, bool modified);
31         bool isValid() const;
32         bool isValid(int row) const;
33         void setValid(int row, bool valid);
34         void setCode(int row, const QString& code);
35         QString getCode(int row) const;
36         void setFinalCode(int row, const QString& code);
37         QString getFinalCode(int row) const;
38         void setInitCode(int row, const QString& code);
39         QString getInitCode(int row) const;
40         void setName(int row, const QString& newName);
41         QString getName(int row) const;
42         void setLang(int row, const QString& lang);
43         QString getLang(int row) const;
44         QStringList getDatabases(int row) const;
45         void setDatabases(int row, const QStringList& value);
46         QStringList getArguments(int row) const;
47         void setArguments(int row, const QStringList& value);
48         FunctionManager::ScriptFunction::Type getType(int row) const;
49         void setType(int row, FunctionManager::ScriptFunction::Type type);
50         bool isAggregate(int row) const;
51         bool isScalar(int row) const;
52         bool getUndefinedArgs(int row) const;
53         void setUndefinedArgs(int row, bool value);
54         bool getAllDatabases(int row) const;
55         void setAllDatabases(int row, bool value);
56         void setData(const QList<FunctionManager::ScriptFunction*>& functions);
57         void addFunction(FunctionManager::ScriptFunction* function);
58         void deleteFunction(int row);
59         QList<FunctionManager::ScriptFunction*> generateFunctions() const;
60         QStringList getFunctionNames() const;
61         void validateNames();
62         bool isAllowedName(int rowToSkip, const QString& nameToValidate);
63         bool isValidRowIndex(int row) const;
64 
65         int rowCount(const QModelIndex& parent = QModelIndex()) const;
66         QVariant data(const QModelIndex& index, int role) const;
67 
68     private:
69         struct Function
70         {
71             Function();
72             Function(FunctionManager::ScriptFunction* other);
73 
74             FunctionManager::ScriptFunction data;
75             bool modified = false;
76             bool valid = true;
77             QString originalName;
78         };
79 
80         void init();
81         void emitDataChanged(int row);
82 
83         QList<Function*> functionList;
84 
85         /**
86          * @brief List of function pointers before modifications.
87          *
88          * This list is kept to check for modifications in the overall list of functions.
89          * Pointers on this list may be already deleted, so don't use them!
90          * It's only used to compare list of pointers to functionList, so it can tell you
91          * if the list was modified in regards of adding or deleting functions.
92          */
93         QList<Function*> originalFunctionList;
94         QHash<QString,QIcon> langToIcon;
95         bool listModified = false;
96 };
97 
98 #endif // FUNCTIONSEDITORMODEL_H
99