4 #include <QGraphicsLineItem>
5 #include <QGraphicsPolygonItem>
6 #include <QGraphicsScene>
7 #include <QGraphicsSceneMouseEvent>
8 #include <QGraphicsView>
10 #include "ViewProfileDivider.h"
12 const double ARROW_WIDTH = 4.0;
13 const double ARROW_HEIGHT = 5.0;
14 const double DIVIDER_WIDTH = 0.0;
15 const int PADDLE_HEIGHT = 10;
16 const int PADDLE_WIDTH = 10;
17 const double SHADED_AREA_OPACITY = 0.4;
18 const int X_INITIAL = 0;
20 const QColor ARROW_COLOR (Qt::NoPen);
21 const QColor SHADED_AREA_COLOR = QColor (220, 220, 220);
22 const QColor DIVIDER_COLOR = QColor (140, 140, 255);
29 bool isLowerBoundary) :
30 QGraphicsRectItem (X_INITIAL,
38 m_sceneWidth (sceneWidth),
39 m_sceneHeight (sceneHeight),
40 m_isLowerBoundary (isLowerBoundary)
46 setPen (QPen (DIVIDER_COLOR));
47 setBrush (QBrush (QColor (140, 255, 140)));
50 setFlags (QGraphicsItem::ItemIsMovable |
51 QGraphicsItem::ItemSendsGeometryChanges);
52 setCursor (Qt::OpenHandCursor);
56 m_arrow =
new QGraphicsPolygonItem (
this);
59 m_shadedArea =
new QGraphicsRectItem (X_INITIAL,
63 m_shadedArea->setOpacity (SHADED_AREA_OPACITY);
64 m_shadedArea->setBrush (QBrush (SHADED_AREA_COLOR));
65 m_shadedArea->setPen (Qt::NoPen);
66 m_shadedArea->setZValue (0.0);
67 scene.addItem (m_shadedArea);
72 m_divider =
new QGraphicsLineItem (X_INITIAL,
75 2 * SLOP + sceneHeight);
76 m_divider->setPen (QPen (QBrush (DIVIDER_COLOR), DIVIDER_WIDTH));
77 m_divider->setZValue (1.0);
78 scene.addItem (m_divider);
83 if (change == ItemPositionChange && scene ()) {
86 QPointF newPos = QPointF (value.toPointF().x(), 0.0) + m_startDragPos;
87 double newX = newPos.x();
88 newX = qMax (newX, 0.0);
89 newX = qMin (newX, (
double) m_sceneWidth);
91 newPos -= m_startDragPos;
95 updateGeometryDivider();
96 updateGeometryNonPaddle ();
103 return QGraphicsRectItem::itemChange (change, value);
109 m_startDragPos = QPointF (rect().x () + rect().width () / 2.0,
110 rect().y () + rect().height () / 2.0);
113 void ViewProfileDivider::sendSignalMoved ()
115 if (m_isLowerBoundary) {
127 m_xScene = m_sceneWidth * (x - xLow) / (xHigh - xLow);
130 updateGeometryPaddle ();
131 updateGeometryDivider ();
132 updateGeometryNonPaddle ();
135 double xLeft = rect().left() + rect().width() / 2.0 - ARROW_WIDTH / 2.0;
136 double xRight = rect().left() + rect().width() / 2.0 + ARROW_WIDTH / 2.0;
137 double yTop = rect().top() + rect().height() / 2.0 - ARROW_HEIGHT / 2.0;
138 double yMiddle = rect().top() + rect().height() / 2.0;
139 double yBottom = rect().top() + rect().height() / 2.0 + ARROW_HEIGHT / 2.0;
141 QPolygonF polygonArrow;
142 if (m_isLowerBoundary) {
145 polygonArrow.push_front (QPointF (xLeft, yTop));
146 polygonArrow.push_front (QPointF (xRight, yMiddle));
147 polygonArrow.push_front (QPointF (xLeft, yBottom));
152 polygonArrow.push_front (QPointF (xRight, yTop));
153 polygonArrow.push_front (QPointF (xLeft, yMiddle));
154 polygonArrow.push_front (QPointF (xRight, yBottom));
156 m_arrow->setPolygon (polygonArrow);
157 m_arrow->setPen (QPen (Qt::black));
158 m_arrow->setBrush (QBrush (ARROW_COLOR));
161 void ViewProfileDivider::slotOtherMoved(
double xSceneOther)
163 m_xSceneOther = xSceneOther;
164 updateGeometryNonPaddle ();
167 void ViewProfileDivider::updateGeometryDivider ()
169 m_divider->setLine (m_xScene,
172 2 * SLOP + m_sceneHeight);
175 void ViewProfileDivider::updateGeometryNonPaddle()
177 if (m_isLowerBoundary) {
178 if (m_xScene <= m_xSceneOther) {
181 m_shadedArea->setRect (-SLOP,
184 2 * SLOP + m_sceneHeight);
189 m_shadedArea->setRect (m_xSceneOther,
191 m_xScene - m_xSceneOther,
192 2 * SLOP + m_sceneHeight);
197 if (m_xSceneOther <= m_xScene) {
200 m_shadedArea->setRect (m_xScene,
202 SLOP + m_sceneWidth - m_xScene,
203 2 * SLOP + m_sceneHeight);
209 m_shadedArea->setRect (m_xSceneOther,
212 2 * SLOP + m_sceneHeight);
217 void ViewProfileDivider::updateGeometryPaddle ()
219 setRect (m_xScene - PADDLE_WIDTH / 2,
220 m_yCenter - PADDLE_HEIGHT / 2,
void signalMovedHigh(double xSceneOther)
Signal used when divider is dragged and m_isLowerBoundary is false.
void setX(double x, double xLow, double xHigh)
Set the position by specifying the new x coordinate.
ViewProfileDivider(QGraphicsScene &scene, QGraphicsView &view, int sceneWidth, int sceneHeight, int yCenter, bool isLowerBoundary)
Single constructor.
void signalMovedLow(double xSceneOther)
Signal used when divider is dragged and m_isLowerBoundary is true.
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value)
Intercept changes so divider movement can be restricted to horizontal direction only.
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event)
Save paddle position at start of click-and-drag.