-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrollsynchronizer.cpp
More file actions
34 lines (30 loc) · 1.46 KB
/
scrollsynchronizer.cpp
File metadata and controls
34 lines (30 loc) · 1.46 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
/////////////////////////////////////////////////////////////////////////////
// Name: scrollsynchronizer.cpp
// Purpose: Synchronize scrolling of MD editor with MD viewer
// Author: Jan Buchholz
// Created: 2025-11-20
/////////////////////////////////////////////////////////////////////////////
#include "scrollsynchronizer.h"
#include <QScrollBar>
ScrollSynchronizer::ScrollSynchronizer(QTextEdit* editor, QTextBrowser* browser, QSplitter* parentSplitter)
: QObject(parentSplitter), m_editor(editor), m_viewer(browser), m_splitter(parentSplitter), m_syncing(false) {
connect(m_editor->verticalScrollBar(), &QScrollBar::valueChanged,
this, &ScrollSynchronizer::sync);
}
void ScrollSynchronizer::setEnabled(bool enabled) {
m_enabled = enabled;
}
void ScrollSynchronizer::sync() {
if (m_syncing || !m_enabled) return;
m_syncing = true;
QScrollBar* sourceBar = m_editor->verticalScrollBar();
QScrollBar* targetBar = m_viewer->verticalScrollBar();
int sourceRange = sourceBar->maximum() - sourceBar->minimum();
int targetRange = targetBar->maximum() - targetBar->minimum();
if (sourceRange > 0 && targetRange > 0) {
double ratio = double(sourceBar->value() - sourceBar->minimum()) / sourceRange;
int newValue = targetBar->minimum() + ratio * targetRange;
if (std::abs(targetBar->value() - newValue) > 1) targetBar->setValue(newValue);
}
m_syncing = false;
}