ui: panning in code view
* Implemented horizontal panning for code snippets by dragging the mouse left and right.
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include <QPainter>
|
||||
#include <QPushButton>
|
||||
#include <QToolTip>
|
||||
#include <qscrollbar.h>
|
||||
|
||||
#include "utility/messaging/type/MessageActivateTokenLocations.h"
|
||||
#include "utility/messaging/type/MessageShowFile.h"
|
||||
@@ -55,6 +56,7 @@ QtCodeArea::QtCodeArea(
|
||||
, m_locationFile(locationFile)
|
||||
, m_hoveredAnnotation(nullptr)
|
||||
, m_digits(0)
|
||||
, m_panningValue(-1)
|
||||
{
|
||||
setObjectName("code_area");
|
||||
setReadOnly(true);
|
||||
@@ -229,10 +231,19 @@ void QtCodeArea::leaveEvent(QEvent* event)
|
||||
setHoveredAnnotation(nullptr);
|
||||
}
|
||||
|
||||
void QtCodeArea::mousePressEvent(QMouseEvent* event)
|
||||
{
|
||||
if (event->button() == Qt::LeftButton)
|
||||
{
|
||||
m_panningValue = event->pos().x();
|
||||
}
|
||||
}
|
||||
|
||||
void QtCodeArea::mouseReleaseEvent(QMouseEvent* event)
|
||||
{
|
||||
if (event->button() == Qt::LeftButton)
|
||||
{
|
||||
m_panningValue = -1;
|
||||
QTextCursor cursor = this->cursorForPosition(event->pos());
|
||||
std::vector<Id> locationIds = findLocationIdsForPosition(cursor.position());
|
||||
|
||||
@@ -253,6 +264,19 @@ void QtCodeArea::mouseDoubleClickEvent(QMouseEvent* event)
|
||||
|
||||
void QtCodeArea::mouseMoveEvent(QMouseEvent* event)
|
||||
{
|
||||
if (m_panningValue!= -1)
|
||||
{
|
||||
int panningCurrentPosition = event->pos().x();
|
||||
int deltaPos = panningCurrentPosition - m_panningValue;
|
||||
m_panningValue = panningCurrentPosition;
|
||||
|
||||
QScrollBar* scrollbar = horizontalScrollBar();
|
||||
int visibleContentWidth = width() - lineNumberAreaWidth();
|
||||
float deltaPosRatio = float(deltaPos) / (visibleContentWidth);
|
||||
scrollbar->setValue(scrollbar->value() - utility::roundToInt(deltaPosRatio * scrollbar->pageStep()));
|
||||
}
|
||||
|
||||
|
||||
QTextCursor cursor = this->cursorForPosition(event->pos());
|
||||
|
||||
m_hoveredLocationIds = findLocationIdsForPosition(cursor.position());
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#include "utility/types.h"
|
||||
|
||||
class QDragMoveEvent;
|
||||
class QPaintEvent;
|
||||
class QResizeEvent;
|
||||
class QSize;
|
||||
@@ -34,7 +35,7 @@ public:
|
||||
QSize sizeHint() const;
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent* event);
|
||||
virtual void paintEvent(QPaintEvent* event);
|
||||
|
||||
private:
|
||||
QtCodeArea *m_codeArea;
|
||||
@@ -73,6 +74,7 @@ protected:
|
||||
virtual void leaveEvent(QEvent* event);
|
||||
virtual void mouseReleaseEvent(QMouseEvent* event);
|
||||
virtual void mouseDoubleClickEvent(QMouseEvent* event);
|
||||
virtual void mousePressEvent(QMouseEvent* event);
|
||||
virtual void mouseMoveEvent(QMouseEvent* event);
|
||||
|
||||
private slots:
|
||||
@@ -131,6 +133,7 @@ private:
|
||||
std::vector<Id> m_hoveredLocationIds;
|
||||
|
||||
int m_digits;
|
||||
int m_panningValue; // just for horizontal panning
|
||||
};
|
||||
|
||||
#endif // QT_CODE_AREA_H
|
||||
|
||||
@@ -73,3 +73,8 @@ size_t utility::digits(size_t n)
|
||||
|
||||
return digits;
|
||||
}
|
||||
|
||||
int utility::roundToInt(float n)
|
||||
{
|
||||
return (n > 0.0f) ? (n + 0.5f) : (n - 0.5f);
|
||||
}
|
||||
|
||||
@@ -39,6 +39,8 @@ namespace utility
|
||||
bool intersectionPoint(Vec2f a1, Vec2f b1, Vec2f a2, Vec2f b2, Vec2f* i);
|
||||
|
||||
size_t digits(size_t n);
|
||||
|
||||
int roundToInt(float n);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
||||
Reference in New Issue
Block a user