summaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/guiSQLiteStudio/windows/codesnippeteditormodel.h
blob: ae5fa86add1d5980bc1cc81f28a30214ea2ba734 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef CODESNIPPETEDITORMODEL_H
#define CODESNIPPETEDITORMODEL_H

#include "guiSQLiteStudio_global.h"
#include "services/codesnippetmanager.h"
#include <QAbstractListModel>

class GUI_API_EXPORT CodeSnippetEditorModel : public QAbstractListModel
{
    Q_OBJECT

    public:
        using QAbstractItemModel::setData;

        enum Role
        {
            CODE = 1000,
            MODIFIED = 1001,
            VALID = 1002
        };

        explicit CodeSnippetEditorModel(QObject *parent = 0);

        void clearModified();
        bool isModified() const;
        bool isModified(int row) const;
        void setModified(int row, bool modified);
        bool isValid() const;
        bool isValid(int row) const;
        void setValid(int row, bool valid);
        void setCode(int row, const QString& code);
        QString getCode(int row) const;
        void setName(int row, const QString& newName);
        QString getName(int row) const;
        void setHotkey(int row, const QKeySequence& value);
        QKeySequence getHotkey(int row) const;
        void setData(const QList<CodeSnippetManager::CodeSnippet*>& snippets);
        void addSnippet(CodeSnippetManager::CodeSnippet* snippet);
        void deleteSnippet(int row);
        QList<CodeSnippetManager::CodeSnippet*> generateSnippets() const;
        QStringList getSnippetNames() const;
        void validateNames();
        bool isAllowedName(int rowToSkip, const QString& nameToValidate);
        bool isAllowedHotkey(int rowToSkip, const QKeySequence& hotkeyToValidate);
        bool isValidRowIndex(int row) const;

        int moveUp(int row);
        int moveDown(int row);
        int rowCount(const QModelIndex& parent = QModelIndex()) const;
        QVariant data(const QModelIndex& index, int role) const;

    private:
        struct Snippet
        {
            Snippet();
            Snippet(CodeSnippetManager::CodeSnippet* other);

            CodeSnippetManager::CodeSnippet data;
            bool modified = false;
            bool valid = true;
            QString originalName;
        };

        void emitDataChanged(int row);

        QList<Snippet*> snippetList;

        /**
         * @brief List of snippets pointers before modifications.
         *
         * This list is kept to check for modifications in the overall list of snippets.
         * Pointers on this list may be already deleted, so don't use them!
         * It's only used to compare list of pointers to snippetList, so it can tell you
         * if the list was modified in regards of adding or deleting functions.
         */
        QList<Snippet*> originalSnippetList;
        bool listModified = false;
};

#endif // CODESNIPPETEDITORMODEL_H