/
editbox.h
156 lines (134 loc) · 3.58 KB
/
editbox.h
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// vim:ts=4:sw=4:
//
// Interactive BASIC Compiler Project
// File: editbox.h - edit box header file
// Copyright (C) 2012 Thunder422
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// For a copy of the GNU General Public License,
// see <http://www.gnu.org/licenses/>.
//
//
// Change History:
//
// 2012-12-29 initial version
#ifndef EDITBOX_H
#define EDITBOX_H
#include <QPlainTextEdit>
#include <QTextBlock>
class QEvent;
class Selection
{
int m_start; // start position of selection
int m_end; // end position of selection
int m_startLine; // start line of selection
int m_endLine; // end line of selection
public:
Selection(void) {}
void setFromCursor(const QTextCursor &cursor)
{
m_start = cursor.selectionStart();
m_end = cursor.selectionEnd();
QTextBlock block = cursor.document()->findBlock(m_start);
m_startLine = block.blockNumber();
block = cursor.document()->findBlock(m_end);
m_endLine = block.blockNumber();
}
bool isEmpty(void)
{
return m_start == m_end;
}
int lines(void)
{
return m_endLine - m_startLine + 1;
}
int startLine(void)
{
return m_startLine;
}
int endLine(void)
{
return m_endLine;
}
};
class EditBox : public QPlainTextEdit
{
Q_OBJECT
public:
explicit EditBox(QWidget *parent = 0);
void cut(void);
void remove(void);
void selectAll(void);
void resetModified(void);
int lineNumberWidgetWidth(void);
void paintLineNumberWidget(QPaintEvent *event);
protected:
void keyPressEvent(QKeyEvent *event);
void resizeEvent(QResizeEvent *event);
signals:
void lineChanged(int number, QString line);
void linesInserted(int number, QStringList lines);
void linesDeleted(int number, int count);
public slots:
void documentChanged(void);
void documentChanged(int position, int charsRemoved, int charsAdded);
void cursorMoved(void);
void undo(void);
void redo(void);
void undoAdded(void);
private slots:
void updateLineNumberWidgetWidth(void);
void updateLineNumberWidget(const QRect &rect, int dy);
private:
void backspace(QTextCursor &cursor);
void insertNewLine(void);
void captureModifiedLine(void);
void captureDeletedLines(void);
enum LineType {
LineChanged,
LineInserted,
LineDeleted,
sizeof_LineType
};
enum {
BaseLineNumber = 0 // number of first line
};
int m_lineModified; // current line that has been modified
int m_lineModCount; // number of modifications to line
LineType m_lineModType; // lone modification type
bool m_undoActive; // line modified due to undo
bool m_ignoreChange; // ignore next document change flag
QWidget *m_lineNumberWidget; // widget to display line numbers
Selection m_beforeSelection; // selection info before edit operation
int m_charsRemoved; // from most recent docuemnt change
int m_charsAdded; // from most recent docuemnt change
};
class LineNumberWidget : public QWidget
{
public:
LineNumberWidget(EditBox *editBox) : QWidget(editBox)
{
m_editBox = editBox;
}
QSize sizeHint() const
{
return QSize(m_editBox->lineNumberWidgetWidth(), 0);
}
protected:
void paintEvent(QPaintEvent *event)
{
m_editBox->paintLineNumberWidget(event);
}
private:
EditBox *m_editBox;
};
#endif // EDITBOX_H