This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
codeeditor.cpp
103 lines (86 loc) · 2.75 KB
/
codeeditor.cpp
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
#include "stdafx.h"
#include "codeeditor.h"
CodeEditor::CodeEditor(QWidget *parent) : ScintillaEdit(parent), m_isContent(false) {}
void CodeEditor::initialize()
{
m_sc.initScintilla(this);
m_sc.initEditorStyle(this);
connect(this, &ScintillaEdit::linesAdded, this, &CodeEditor::linesAdded);
connect(this, &ScintillaEdit::marginClicked, this, &CodeEditor::marginClicked);
}
void CodeEditor::setContent(const QString &content)
{
m_isContent = true;
auto b = content.toUtf8();
setText(b.data());
emptyUndoBuffer();
m_sc.initEditorStyle(this);
colourise(0, -1);
}
void CodeEditor::gotoLine(const QString &fileName, int line)
{
m_fileName = fileName;
const QString &lang = m_sc.matchPatternLanguage(fileName);
QFile f(fileName);
if (f.open(QIODevice::ReadOnly))
{
clear();
while (!f.atEnd())
{
QByteArray c = f.readAll();
appendText(c.length(), c.data());
}
f.close();
emptyUndoBuffer();
m_sc.initEditorStyle(this, lang);
colourise(0, -1);
grabFocus();
gotoPos(positionFromLine(line - 1));
setCurrentPos(positionFromLine(line - 1));
}
}
void CodeEditor::gotoLine(int line)
{
grabFocus();
gotoPos(positionFromLine(line - 1));
setCurrentPos(positionFromLine(line - 1));
}
bool CodeEditor::matched(const QString &fileName)
{
return QFileInfo(fileName) == QFileInfo(m_fileName);
}
void CodeEditor::linesAdded(int /*linesAdded*/)
{
ScintillaEdit *sci = qobject_cast<ScintillaEdit *>(sender());
sptr_t line_count = sci->lineCount();
sptr_t left = sci->marginLeft() + 2;
sptr_t right = sci->marginRight() + 2;
sptr_t width = left + right + sci->textWidth(STYLE_LINENUMBER, QString("%1").arg(line_count).toStdString().c_str());
if (width > sci->marginWidthN(0))
sci->setMarginWidthN(0, width);
}
void CodeEditor::marginClicked(int position, int /*modifiers*/, int margin)
{
ScintillaEdit *sci = qobject_cast<ScintillaEdit *>(sender());
if (sci->marginTypeN(margin) == SC_MARGIN_SYMBOL)
{
sptr_t maskN = sci->marginMaskN(margin);
if ((maskN & 0xFFFFFFFF) == SC_MASK_FOLDERS)
{
sptr_t line = sci->lineFromPosition(position);
sptr_t foldLevel = sci->foldLevel(line);
if (foldLevel & SC_FOLDLEVELHEADERFLAG)
{
sci->toggleFold(line);
}
}
}
}
bool CodeEditor::isContent() const
{
return m_isContent;
}
void CodeEditor::setLanguage(const QString &lang)
{
m_sc.initEditorStyle(this, lang);
}