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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
#include "graphview.h"
#include <QMouseEvent>
#include <QApplication>
GraphView::GraphView(QWidget* parent) :
QWidget(parent),
m_viewLeft(0),
m_viewRight(0),
m_viewBottom(0),
m_viewTop(0),
m_graphLeft(0),
m_graphRight(0),
m_graphBottom(0),
m_graphTop(0),
m_viewWidth(0),
m_viewWidthMin(0),
m_viewWidthMax(0),
m_viewHeight(0),
m_viewHeightMin(0),
m_viewHeightMax(0),
m_selectionState(NULL)
{
memset(&m_previous, -1, sizeof(m_previous));
}
void GraphView::update()
{
if (m_graphLeft != m_previous.m_graphLeft || m_graphRight != m_previous.m_graphRight) {
m_previous.m_graphLeft = m_graphLeft;
m_previous.m_graphRight = m_graphRight;
emit horizontalRangeChanged(m_graphLeft, m_graphRight);
}
if (m_viewLeft != m_previous.m_viewLeft || m_viewRight != m_previous.m_viewRight) {
m_previous.m_viewLeft = m_viewLeft;
m_previous.m_viewRight = m_viewRight;
emit horizontalViewChanged(m_viewLeft, m_viewRight);
}
if (m_graphBottom != m_previous.m_graphBottom || m_graphTop != m_previous.m_graphTop) {
m_previous.m_graphBottom = m_graphBottom;
m_previous.m_graphTop = m_graphTop;
emit verticalRangeChanged(m_graphBottom, m_graphTop);
}
if (m_viewBottom != m_previous.m_viewBottom || m_viewTop != m_previous.m_viewTop) {
m_previous.m_viewBottom = m_viewBottom;
m_previous.m_viewTop = m_viewTop;
emit verticalViewChanged(m_viewBottom, m_viewTop);
}
QWidget::update();
}
void GraphView::resizeEvent(QResizeEvent *)
{
m_viewHeight = height();
m_viewHeightMin = m_viewHeight;
m_viewHeightMax = m_viewHeight;
m_viewTop = m_viewBottom + m_viewHeight;
update();
}
void GraphView::wheelEvent(QWheelEvent *e)
{
int zoomPercent = 10;
/* If holding Ctrl key then zoom 2x faster */
if (QApplication::keyboardModifiers() & Qt::ControlModifier) {
zoomPercent = 20;
}
/* Zoom view by adjusting width */
double dt = m_viewWidth;
double size = dt;
size *= -e->delta();
/* Zoom deltas normally come in increments of 120 */
size /= 120 * (100 / zoomPercent);
m_viewWidth += size;
m_viewWidth = qBound(m_viewWidthMin, m_viewWidth, m_viewWidthMax);
/* Scroll view to zoom around mouse */
dt -= m_viewWidth;
dt *= e->x();
dt /= width();
m_viewLeft = dt + m_viewLeft;
m_viewLeft = qBound(m_graphLeft, m_viewLeft, m_graphRight - m_viewWidth);
m_viewRight = m_viewLeft + m_viewWidth;
update();
}
void GraphView::mouseMoveEvent(QMouseEvent *e)
{
if (e->buttons().testFlag(Qt::LeftButton)) {
/* Horizontal scroll */
double dvdx = m_viewWidth / (double)width();
dvdx *= m_mousePressPosition.x() - e->pos().x();
m_viewLeft = m_mousePressViewLeft + dvdx;
m_viewLeft = qBound(m_graphLeft, m_viewLeft, m_graphRight - m_viewWidth);
m_viewRight = m_viewLeft + m_viewWidth;
/* Vertical scroll */
double dvdy = m_viewHeight / (double)height();
dvdy *= m_mousePressPosition.y() - e->pos().y();
m_viewBottom = m_mousePressViewBottom + dvdy;
m_viewBottom = qBound(m_graphBottom, m_viewBottom, m_graphTop - m_viewHeight);
m_viewTop = m_viewBottom + m_viewHeight;
update();
}
}
void GraphView::mousePressEvent(QMouseEvent *e)
{
m_mousePressPosition = e->pos();
m_mousePressViewLeft = m_viewLeft;
m_mousePressViewBottom = m_viewBottom;
}
void GraphView::mouseDoubleClickEvent(QMouseEvent *e)
{
if (m_selectionState) {
m_selectionState->type = SelectionState::None;
emit selectionChanged();
}
}
void GraphView::setSelectionState(SelectionState* state)
{
m_selectionState = state;
}
void GraphView::setHorizontalView(qint64 start, qint64 end)
{
m_viewLeft = qBound(m_graphLeft, start, m_graphRight - (end - start));
m_viewRight = qBound(m_graphLeft, end, m_graphRight);
m_viewWidth = m_viewRight - m_viewLeft;
update();
}
void GraphView::setVerticalView(qint64 start, qint64 end)
{
m_viewBottom = qBound(m_graphBottom, start, m_graphTop - (end - start));
m_viewTop = qBound(m_graphBottom, end, m_graphTop);
m_viewHeight = m_viewTop - m_viewBottom;
update();
}
void GraphView::setDefaultView(qint64 min, qint64 max)
{
m_graphLeft = min;
m_graphRight = max;
m_viewWidth = max - min;
m_viewWidthMin = 1;
m_viewWidthMax = m_viewWidth;
m_viewLeft = min;
m_viewRight = max;
m_viewHeight = height();
m_viewHeightMin = m_viewHeight;
m_viewHeightMax = m_viewHeight;
m_viewBottom = 0;
m_viewTop = m_viewHeight;
m_graphBottom = 0;
m_graphTop = m_viewHeight;
update();
}
#include "graphview.moc"
|