forked from cloose/CuteMarkEd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslidelinemapping.cpp
More file actions
101 lines (87 loc) · 3.2 KB
/
slidelinemapping.cpp
File metadata and controls
101 lines (87 loc) · 3.2 KB
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
/*
* Copyright 2014 Andreas Reischuck <https://github.com/arBmind>
* Copyright 2014 Christian Loose <christian.loose@hamburg.de>
*
* 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 2 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "slidelinemapping.h"
#include <QRegularExpression>
void SlideLineMapping::build(const QString &code)
{
static const QRegularExpression re("\n|\r\n|\r");
m_lineToSlide.clear();
m_slideToLine.clear();
int horizontal = 0;
int vertical = 0;
int lineNumber = 0;
QStringList lines = code.split(re);
m_slideToLine.insert(qMakePair(horizontal, vertical), lineNumber+1);
for (int i = 0; i < lines.count(); ++i) {
++lineNumber;
if (isHorizontalSlideSeparator(lines, i)) {
m_lineToSlide.insert(lineNumber, qMakePair(horizontal, vertical));
horizontal++;
vertical = 0;
m_slideToLine.insert(qMakePair(horizontal, vertical), lineNumber+1);
}
if (isVerticalSlideSeparator(lines, i)) {
m_lineToSlide.insert(lineNumber, qMakePair(horizontal, vertical));
vertical++;
m_slideToLine.insert(qMakePair(horizontal, vertical), lineNumber+1);
}
}
m_lineToSlide.insert(lineNumber, qMakePair(horizontal, vertical));
}
int SlideLineMapping::lineForSlide(const QPair<int, int>& slide) const
{
QMap<QPair<int, int>, int>::const_iterator it = m_slideToLine.find(slide);
if (it != m_slideToLine.end()) {
return it.value();
}
return -1;
}
QPair<int, int> SlideLineMapping::slideForLine(int lineNumber) const
{
QMap<int, QPair<int, int> >::const_iterator it = m_lineToSlide.lowerBound(lineNumber);
if (it != m_lineToSlide.end()) {
return it.value();
}
return qMakePair(-1, -1);
}
QMap<int, QPair<int, int> > SlideLineMapping::lineToSlide() const
{
return m_lineToSlide;
}
QMap<QPair<int, int>, int> SlideLineMapping::slideToLine() const
{
return m_slideToLine;
}
bool SlideLineMapping::isHorizontalSlideSeparator(const QStringList &lines, int lineNumber) const
{
static const QString horizontalMarker("---");
return lineNumber > 1 &&
lineNumber < lines.count()-1 &&
lines[lineNumber-1].isEmpty() &&
lines[lineNumber] == horizontalMarker &&
lines[lineNumber+1].isEmpty();
}
bool SlideLineMapping::isVerticalSlideSeparator(const QStringList &lines, int lineNumber) const
{
static const QString verticalMarker("--");
return lineNumber > 1 &&
lineNumber < lines.count()-1 &&
lines[lineNumber-1].isEmpty() &&
lines[lineNumber] == verticalMarker &&
lines[lineNumber+1].isEmpty();
}