mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
dragging mouse with Ctrl and left button pressed to scroll in read and edit mode
This commit is contained in:
parent
71ea514bfa
commit
88fa722868
@ -449,3 +449,62 @@ var handleToc = function(needToc) {
|
||||
}
|
||||
};
|
||||
|
||||
// Implement mouse drag with Ctrl and left button pressed to scroll.
|
||||
var vds_oriMouseClientX = 0;
|
||||
var vds_oriMouseClientY = 0;
|
||||
var vds_readyToScroll = false;
|
||||
var vds_scrolled = false;
|
||||
|
||||
window.onmousedown = function(e) {
|
||||
e = e || window.event;
|
||||
// Left button and Ctrl key.
|
||||
if (e.buttons == 1
|
||||
&& e.ctrlKey
|
||||
&& window.getSelection().rangeCount == 0) {
|
||||
vds_oriMouseClientX = e.clientX;
|
||||
vds_oriMouseClientY = e.clientY;
|
||||
vds_readyToScroll = true;
|
||||
vds_scrolled = false;
|
||||
e.preventDefault();
|
||||
} else {
|
||||
vds_readyToScroll = false;
|
||||
vds_scrolled = false;
|
||||
}
|
||||
};
|
||||
|
||||
window.onmouseup = function(e) {
|
||||
e = e || window.event;
|
||||
if (vds_scrolled || vds_readyToScroll) {
|
||||
// Have been scrolled, restore the cursor style.
|
||||
document.body.style.cursor = "auto";
|
||||
e.preventDefault();
|
||||
}
|
||||
|
||||
vds_readyToScroll = false;
|
||||
vds_scrolled = false;
|
||||
};
|
||||
|
||||
window.onmousemove = function(e) {
|
||||
e = e || window.event;
|
||||
if (vds_readyToScroll) {
|
||||
deltaX = e.clientX - vds_oriMouseClientX;
|
||||
deltaY = e.clientY - vds_oriMouseClientY;
|
||||
|
||||
var threshold = 5;
|
||||
if (Math.abs(deltaX) >= threshold || Math.abs(deltaY) >= threshold) {
|
||||
vds_oriMouseClientX = e.clientX;
|
||||
vds_oriMouseClientY = e.clientY;
|
||||
|
||||
if (!vds_scrolled) {
|
||||
vds_scrolled = true;
|
||||
document.body.style.cursor = "all-scroll";
|
||||
}
|
||||
|
||||
var scrollX = -deltaX;
|
||||
var scrollY = -deltaY;
|
||||
window.scrollBy(scrollX, scrollY);
|
||||
}
|
||||
|
||||
e.preventDefault();
|
||||
}
|
||||
};
|
||||
|
@ -711,3 +711,73 @@ VEditConfig &VEdit::getConfig()
|
||||
{
|
||||
return m_config;
|
||||
}
|
||||
|
||||
void VEdit::mousePressEvent(QMouseEvent *p_event)
|
||||
{
|
||||
if (p_event->button() == Qt::LeftButton
|
||||
&& p_event->modifiers() == Qt::ControlModifier
|
||||
&& !textCursor().hasSelection()) {
|
||||
m_oriMouseX = p_event->x();
|
||||
m_oriMouseY = p_event->y();
|
||||
m_readyToScroll = true;
|
||||
m_mouseMoveScrolled = false;
|
||||
p_event->accept();
|
||||
return;
|
||||
}
|
||||
|
||||
m_readyToScroll = false;
|
||||
m_mouseMoveScrolled = false;
|
||||
|
||||
QTextEdit::mousePressEvent(p_event);
|
||||
}
|
||||
|
||||
void VEdit::mouseReleaseEvent(QMouseEvent *p_event)
|
||||
{
|
||||
if (m_mouseMoveScrolled || m_readyToScroll) {
|
||||
viewport()->setCursor(Qt::IBeamCursor);
|
||||
m_readyToScroll = false;
|
||||
m_mouseMoveScrolled = false;
|
||||
p_event->accept();
|
||||
return;
|
||||
}
|
||||
|
||||
m_readyToScroll = false;
|
||||
m_mouseMoveScrolled = false;
|
||||
|
||||
QTextEdit::mouseReleaseEvent(p_event);
|
||||
}
|
||||
|
||||
void VEdit::mouseMoveEvent(QMouseEvent *p_event)
|
||||
{
|
||||
const int threshold = 5;
|
||||
|
||||
if (m_readyToScroll) {
|
||||
int deltaX = p_event->x() - m_oriMouseX;
|
||||
int deltaY = p_event->y() - m_oriMouseY;
|
||||
|
||||
if (qAbs(deltaX) >= threshold || qAbs(deltaY) >= threshold) {
|
||||
m_oriMouseX = p_event->x();
|
||||
m_oriMouseY = p_event->y();
|
||||
|
||||
if (!m_mouseMoveScrolled) {
|
||||
m_mouseMoveScrolled = true;
|
||||
viewport()->setCursor(Qt::SizeAllCursor);
|
||||
}
|
||||
|
||||
QScrollBar *verBar = verticalScrollBar();
|
||||
QScrollBar *horBar = horizontalScrollBar();
|
||||
if (verBar->isVisible()) {
|
||||
verBar->setValue(verBar->value() - deltaY);
|
||||
}
|
||||
|
||||
if (horBar->isVisible()) {
|
||||
horBar->setValue(horBar->value() - deltaX);
|
||||
}
|
||||
}
|
||||
|
||||
p_event->accept();
|
||||
return;
|
||||
}
|
||||
|
||||
QTextEdit::mouseMoveEvent(p_event);
|
||||
}
|
||||
|
10
src/vedit.h
10
src/vedit.h
@ -110,6 +110,11 @@ protected:
|
||||
virtual void updateFontAndPalette();
|
||||
virtual void contextMenuEvent(QContextMenuEvent *p_event) Q_DECL_OVERRIDE;
|
||||
|
||||
// Used to implement dragging mouse with Ctrl and left button pressed to scroll.
|
||||
virtual void mousePressEvent(QMouseEvent *p_event) Q_DECL_OVERRIDE;
|
||||
virtual void mouseReleaseEvent(QMouseEvent *p_event) Q_DECL_OVERRIDE;
|
||||
virtual void mouseMoveEvent(QMouseEvent *p_event) Q_DECL_OVERRIDE;
|
||||
|
||||
// Update m_config according to VConfigManager.
|
||||
void updateConfig();
|
||||
|
||||
@ -128,6 +133,11 @@ private:
|
||||
// Timer for extra selections highlight.
|
||||
QTimer *m_highlightTimer;
|
||||
|
||||
bool m_readyToScroll;
|
||||
bool m_mouseMoveScrolled;
|
||||
int m_oriMouseX;
|
||||
int m_oriMouseY;
|
||||
|
||||
void showWrapLabel();
|
||||
|
||||
// Trigger the timer to request highlight.
|
||||
|
Loading…
x
Reference in New Issue
Block a user