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
|
#include "graphaxiswidget.h"
#include <QMouseEvent>
GraphAxisWidget::GraphAxisWidget(QWidget* parent) :
QWidget(parent),
m_selectable(None),
m_selectionState(NULL)
{
}
bool GraphAxisWidget::hasSelection()
{
if (!m_selectionState) {
return false;
}
if (m_selectionState->type == SelectionState::Horizontal && m_orientation == GraphAxisWidget::Horizontal) {
return true;
}
if (m_selectionState->type == SelectionState::Vertical && m_orientation == GraphAxisWidget::Vertical) {
return true;
}
return false;
}
void GraphAxisWidget::setSelectable(SelectionStyle selectable)
{
m_selectable = selectable;
}
void GraphAxisWidget::setSelectionState(SelectionState* state)
{
m_selectionState = state;
}
void GraphAxisWidget::setOrientation(Orientation v)
{
m_orientation = v;
if (m_orientation == Horizontal) {
setMinimumWidth(60);
} else {
setMinimumHeight(60);
}
}
void GraphAxisWidget::mouseMoveEvent(QMouseEvent *e)
{
if (m_selectable == None) {
return;
}
int pos, max;
if (m_orientation == Horizontal) {
pos = e->x();
max = width();
} else {
pos = e->y();
max = height();
}
double value = m_valueEnd - m_valueBegin;
value *= pos / (double)max;
value += m_valueBegin;
if (e->buttons().testFlag(Qt::LeftButton)) {
m_selectionState->start = qMin<qint64>(m_mousePressValue, value);
m_selectionState->end = qMax<qint64>(m_mousePressValue, value);
m_selectionState->type = m_orientation == Horizontal ? SelectionState::Horizontal : SelectionState::Vertical;
emit selectionChanged();
update();
}
}
void GraphAxisWidget::mousePressEvent(QMouseEvent *e)
{
if (m_selectable == None) {
return;
}
int pos, max;
if (m_orientation == Horizontal) {
pos = e->x();
max = width();
} else {
pos = e->y();
max = height();
}
double value = m_valueEnd - m_valueBegin;
value *= pos / (double)max;
value += m_valueBegin;
m_mousePressPosition = e->pos();
m_mousePressValue = value;
}
void GraphAxisWidget::mouseReleaseEvent(QMouseEvent *e)
{
if (m_selectable == None) {
return;
}
int dx = qAbs(m_mousePressPosition.x() - e->x());
int dy = qAbs(m_mousePressPosition.y() - e->y());
if (dx + dy < 2) {
m_selectionState->type = SelectionState::None;
emit selectionChanged();
}
}
void GraphAxisWidget::setRange(qint64 min, qint64 max)
{
m_valueMin = min;
m_valueMax = max;
update();
}
void GraphAxisWidget::setView(qint64 start, qint64 end)
{
m_valueBegin = start;
m_valueEnd = end;
update();
}
#include "graphaxiswidget.moc"
|