-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditor.cc
99 lines (88 loc) · 3.29 KB
/
Editor.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// MIT License
//
// Copyright (c) 2024 Piotr Pszczółkowski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// Created by Piotr Pszczółkowski on 15/03/2024.
/*------- include files:
-------------------------------------------------------------------*/
#include "Editor.h"
#include "Highlighter.h"
#include "EventController.h"
#include <fmt/core.h>
using namespace std;
Editor::Editor(Highlighting const highlighting, QWidget* const parent) :
QTextEdit(parent)
{
setAcceptRichText(true);
setLineWrapMode(NoWrap);
QPalette p = palette();
p.setColor(QPalette::Base, {0x18, 0x18, 0x18});
p.setColor(QPalette::Text, {0xe0, 0xe0, 0xe0});
setPalette(p);
auto block_fmt = textCursor().blockFormat();
block_fmt.setLineHeight(5, QTextBlockFormat::LineDistanceHeight);
textCursor().setBlockFormat(block_fmt);
QFont font;
font.setFamily("Menlo");
font.setKerning(true);
font.setPointSize(12);
setFont(font);
if (highlighting == Highlighting::Yes) {
highlighter_ = new Highlighter(document());
connect(this, &QTextEdit::textChanged, this, &Editor::text_changed);
EventController::instance().append(this, event::Match);
}
}
void Editor::text_changed() noexcept {
// Highlighting off
highlighter_->action(false);
}
void Editor::customEvent(QEvent *event) {
auto const e = dynamic_cast<Event*>(event);
switch (int(e->type())) {
case event::AppendLine:
if (isReadOnly()) {
if (e->data().size() == 1) {
auto text = e->data()[0].toString();
append(text);
}
}
break;
case event::Match: {
auto str = e->data()[0].toString().toStdString();
auto data = glz::read_json<vector<Match>>(str);
if (not data.has_value()) {
fmt::print(stderr, "ERROR: invalid JSON string\n");
return;
}
highlighter_->upate_for(std::move(data.value()));
}
default:
{}
}
}
void Editor::set(vector<string> const& data) noexcept {
clear();
QString plain_text{};
for (auto const& text : data )
plain_text = plain_text.append(qstr::fromStdString(text)).append('\n');
insertPlainText(plain_text);
}