summaryrefslogtreecommitdiff
path: root/src/main-view/UndoCommands.h
blob: a4affec38ec6644fe979eff6f238c411e5da2abe (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef UNDOCOMMANDS_H
#define UNDOCOMMANDS_H

#include <QtCore>
#include <QtWidgets>

class DicRecord;
class MainWindow;
class DictTableView;
class DictTableModel;
class CardFilterModel;

class UndoRecordCmd: public QUndoCommand
{
public:
    UndoRecordCmd( const MainWindow* aMainWin );

protected:
    const DictTableView* getCurView();
    CardFilterModel* getProxyModel( const DictTableView* aCurDictView );

    void insertRows( QList<int> aRowNumbers );
    void setRecords( QMap<int, DicRecord*> aRecords );
    void removeRows( QList<int> aRowNumbers );
    void selectRows( QList<int> aRowNumbers );
    void selectRow( int aRow );

protected:
    const MainWindow* m_mainWindow;
    DictTableModel* m_dictModel;
};

class InsertRecordsCmd: public UndoRecordCmd
{
public:
    InsertRecordsCmd(const MainWindow* aMainWin);

    static const int Id = 1;

    void updateText();
    void redo();
    void undo();
    bool mergeWith( const QUndoCommand* command );
    int id() const { return Id; }

private:
    QList<int> m_rowNumbers;
};

class RemoveRecordsCmd: public UndoRecordCmd
{
public:
    RemoveRecordsCmd( const MainWindow* aMainWin );
    ~RemoveRecordsCmd();

    static const int Id = 2;

    void redo();
    void undo();
    bool mergeWith( const QUndoCommand* ) { return false; }
    int id() const { return Id; }

private:
    QMap<int, DicRecord*> m_records;    // row number -> pointer to dic record. Sorted by key: rows
        // The first element is the top row
};

/// Doesn't inherit UndoRecordCmd!
class EditRecordCmd: public QUndoCommand
{
public:
    EditRecordCmd( QAbstractItemModel* aTableModel, const QModelIndex& aIndex, const QVariant& aValue );

    static const int Id = 3;

    void redo();
    void undo();
    bool mergeWith( const QUndoCommand* ) { return false; }
    int id() const { return Id; }

private:
    static const int MaxMenuEditStrLen = 20;

    QAbstractItemModel* m_dictModel;
    int m_row;
    int m_col;
    QString m_oldStr;
    QString m_newStr;
    int m_insertedRow;
};

class PasteRecordsCmd: public UndoRecordCmd
{
public:
    PasteRecordsCmd( QList<DicRecord*> aRecords, const MainWindow* aMainWin );
    ~PasteRecordsCmd();

    static const int Id = 4;

    void redo();
    void undo();
    bool mergeWith( const QUndoCommand* ) { return false; }
    int id() const { return Id; }

private:
    QMap<int, DicRecord*> m_records;    // row number -> pointer to dic record. Sorted by key: rows
};

#endif // UNDOCOMMANDS_H