forked from cloose/CuteMarkEd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrevealviewsynchronizer.cpp
More file actions
131 lines (111 loc) · 4.15 KB
/
revealviewsynchronizer.cpp
File metadata and controls
131 lines (111 loc) · 4.15 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
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
/*
* 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 "revealviewsynchronizer.h"
#include <QPlainTextEdit>
#include <QTextBlock>
#include <QWebFrame>
#include <QWebView>
#include "slidelinemapping.h"
RevealViewSynchronizer::RevealViewSynchronizer(QWebView *webView, QPlainTextEdit *editor) :
ViewSynchronizer(webView, editor),
currentSlide(qMakePair(0, 0)),
slideLineMapping(new SlideLineMapping())
{
connect(webView, SIGNAL(loadFinished(bool)),
this, SLOT(registerEvents()));
connect(webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
this, SLOT(restoreSlidePosition()));
connect(editor, SIGNAL(cursorPositionChanged()),
this, SLOT(cursorPositionChanged()));
connect(editor, SIGNAL(textChanged()),
this, SLOT(textChanged()));
textChanged();
}
RevealViewSynchronizer::~RevealViewSynchronizer()
{
delete slideLineMapping;
}
int RevealViewSynchronizer::horizontalSlide() const
{
return currentSlide.first;
}
int RevealViewSynchronizer::verticalSlide() const
{
return currentSlide.second;
}
void RevealViewSynchronizer::slideChanged(int horizontal, int vertical)
{
if (currentSlide.first == horizontal && currentSlide.second == vertical)
return;
currentSlide = qMakePair(horizontal, vertical);
int lineNumber = slideLineMapping->lineForSlide(currentSlide);
if (lineNumber > 0) {
gotoLine(lineNumber);
}
}
void RevealViewSynchronizer::registerEvents()
{
m_webView->page()->mainFrame()->evaluateJavaScript(
"(function(){"
" var mainWinUpdate = false;"
" function feedbackPosition(event) {"
" if (mainWinUpdate) return;"
" synchronizer.slideChanged(event.indexh, event.indexv);"
" }"
" Reveal.addEventListener('ready', function() {"
" Reveal.addEventListener('slidechanged', feedbackPosition);"
" });"
" function gotoSlide(horizontal, vertical) {"
" mainWinUpdate = true;"
" Reveal.slide(horizontal, vertical);"
" mainWinUpdate = false;"
" }"
" synchronizer.gotoSlideRequested.connect(gotoSlide);"
"})();");
}
void RevealViewSynchronizer::restoreSlidePosition()
{
static QString restorePosition =
"window.location.hash = '/'+synchronizer.horizontalSlide+'/'+synchronizer.verticalSlide;";
m_webView->page()->mainFrame()->evaluateJavaScript(restorePosition);
}
void RevealViewSynchronizer::cursorPositionChanged()
{
int lineNumber = m_editor->textCursor().blockNumber() + 1;
QPair<int, int> slide = slideLineMapping->slideForLine(lineNumber);
if (slide.first >= 0 && slide.second >= 0) {
gotoSlide(slide);
}
}
void RevealViewSynchronizer::textChanged()
{
QString code = m_editor->toPlainText();
slideLineMapping->build(code);
}
void RevealViewSynchronizer::gotoLine(int lineNumber)
{
QTextCursor cursor(m_editor->document()->findBlockByNumber(lineNumber-1));
m_editor->setTextCursor(cursor);
}
void RevealViewSynchronizer::gotoSlide(QPair<int, int> slide)
{
if (currentSlide.first == slide.first && currentSlide.second == slide.second)
return;
currentSlide = slide;
emit gotoSlideRequested(slide.first, slide.second);
}