-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.cc
79 lines (62 loc) · 1.64 KB
/
buffer.cc
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
#include <QFile>
#include <QPlainTextDocumentLayout>
#include "buffer.hh"
Buffer::Buffer(QString const& name, QWidget* parent)
: QTextDocument(parent)
{
setObjectName(name);
setDocumentLayout(new QPlainTextDocumentLayout(this));
}
QString Buffer::read()
{
if (m_file_path.isEmpty())
return "Cannot load untracked file";
QFile file(m_file_path);
if (!file.open(QFile::ReadOnly | QFile::Text))
return file.errorString();
QTextStream file_stream(&file);
auto file_contents = file_stream.readAll();
file.close();
setPlainText(file_contents);
setModified(false);
return "";
}
QString Buffer::write()
{
if (!isModified())
return "";
if (m_file_path.isEmpty())
return "Cannot save untracked file";
QFile file(m_file_path);
if (!file.open(QFile::WriteOnly | QFile::Text))
return file.errorString();
QTextStream file_stream(&file);
file_stream << toPlainText();
file.close();
setModified(false);
return "";
}
void Buffer::set_file_path(QString file_path)
{
if (file_path != m_file_path) {
m_file_path = file_path;
auto file_name = QUrl(file_path).fileName();
setObjectName(file_name);
emit file_path_changed(file_path);
}
}
void Buffer::set_edit_mode(QString edit_mode)
{
if (edit_mode != m_edit_mode) {
m_edit_mode = edit_mode;
emit edit_mode_changed(edit_mode);
}
}
void Buffer::set_font_point_size(int font_point_size)
{
QFont font("");
font.setStyleHint(QFont::Monospace);
font.setFixedPitch(true);
font.setPointSize(font_point_size);
setDefaultFont(font);
}