10 #include <QGraphicsLineItem> 11 #include <QGraphicsPolygonItem> 12 #include <QGraphicsScene> 13 #include <QGraphicsSceneMouseEvent> 14 #include <QGraphicsView> 16 #include "ViewProfileDivider.h" 18 const double ARROW_WIDTH = 4.0;
19 const double ARROW_HEIGHT = 5.0;
20 const double DIVIDER_WIDTH = 0.0;
21 const int PADDLE_HEIGHT = 10;
22 const int PADDLE_WIDTH = 10;
23 const double SHADED_AREA_OPACITY = 0.4;
24 const int X_INITIAL = 0;
26 const QColor ARROW_COLOR (Qt::NoPen);
27 const QColor SHADED_AREA_COLOR = QColor (220, 220, 220);
28 const QColor DIVIDER_COLOR = QColor (140, 140, 255);
35 bool isLowerBoundary) :
36 QGraphicsRectItem (X_INITIAL,
44 m_sceneWidth (sceneWidth),
45 m_sceneHeight (sceneHeight),
46 m_isLowerBoundary (isLowerBoundary)
52 setPen (QPen (DIVIDER_COLOR));
53 setBrush (QBrush (QColor (140, 255, 140)));
56 setFlags (QGraphicsItem::ItemIsMovable |
57 QGraphicsItem::ItemSendsGeometryChanges);
58 setCursor (Qt::OpenHandCursor);
62 m_arrow =
new QGraphicsPolygonItem (
this);
65 m_shadedArea =
new QGraphicsRectItem (X_INITIAL,
69 m_shadedArea->setOpacity (SHADED_AREA_OPACITY);
70 m_shadedArea->setBrush (QBrush (SHADED_AREA_COLOR));
71 m_shadedArea->setPen (Qt::NoPen);
72 m_shadedArea->setZValue (0.0);
73 scene.addItem (m_shadedArea);
78 m_divider =
new QGraphicsLineItem (X_INITIAL,
81 2 * SLOP + sceneHeight);
82 m_divider->setPen (QPen (QBrush (DIVIDER_COLOR), DIVIDER_WIDTH));
83 m_divider->setZValue (1.0);
84 scene.addItem (m_divider);
89 if (change == ItemPositionChange && scene ()) {
92 QPointF newPos = QPointF (value.toPointF().x(), 0.0) + m_startDragPos;
93 double newX = newPos.x();
94 newX = qMax (newX, 0.0);
95 newX = qMin (newX, (
double) m_sceneWidth);
97 newPos -= m_startDragPos;
101 updateGeometryDivider();
102 updateGeometryNonPaddle ();
109 return QGraphicsRectItem::itemChange (change, value);
115 m_startDragPos = QPointF (rect().x () + rect().width () / 2.0,
116 rect().y () + rect().height () / 2.0);
119 void ViewProfileDivider::sendSignalMoved ()
121 if (m_isLowerBoundary) {
133 m_xScene = m_sceneWidth * (x - xLow) / (xHigh - xLow);
136 updateGeometryPaddle ();
137 updateGeometryDivider ();
138 updateGeometryNonPaddle ();
141 double xLeft = rect().left() + rect().width() / 2.0 - ARROW_WIDTH / 2.0;
142 double xRight = rect().left() + rect().width() / 2.0 + ARROW_WIDTH / 2.0;
143 double yTop = rect().top() + rect().height() / 2.0 - ARROW_HEIGHT / 2.0;
144 double yMiddle = rect().top() + rect().height() / 2.0;
145 double yBottom = rect().top() + rect().height() / 2.0 + ARROW_HEIGHT / 2.0;
147 QPolygonF polygonArrow;
148 if (m_isLowerBoundary) {
151 polygonArrow.push_front (QPointF (xLeft, yTop));
152 polygonArrow.push_front (QPointF (xRight, yMiddle));
153 polygonArrow.push_front (QPointF (xLeft, yBottom));
158 polygonArrow.push_front (QPointF (xRight, yTop));
159 polygonArrow.push_front (QPointF (xLeft, yMiddle));
160 polygonArrow.push_front (QPointF (xRight, yBottom));
162 m_arrow->setPolygon (polygonArrow);
163 m_arrow->setPen (QPen (Qt::black));
164 m_arrow->setBrush (QBrush (ARROW_COLOR));
167 void ViewProfileDivider::slotOtherMoved(
double xSceneOther)
169 m_xSceneOther = xSceneOther;
170 updateGeometryNonPaddle ();
173 void ViewProfileDivider::updateGeometryDivider ()
175 m_divider->setLine (m_xScene,
178 2 * SLOP + m_sceneHeight);
181 void ViewProfileDivider::updateGeometryNonPaddle()
183 if (m_isLowerBoundary) {
184 if (m_xScene <= m_xSceneOther) {
187 m_shadedArea->setRect (-SLOP,
190 2 * SLOP + m_sceneHeight);
195 m_shadedArea->setRect (m_xSceneOther,
197 m_xScene - m_xSceneOther,
198 2 * SLOP + m_sceneHeight);
203 if (m_xSceneOther <= m_xScene) {
206 m_shadedArea->setRect (m_xScene,
208 SLOP + m_sceneWidth - m_xScene,
209 2 * SLOP + m_sceneHeight);
215 m_shadedArea->setRect (m_xSceneOther,
218 2 * SLOP + m_sceneHeight);
223 void ViewProfileDivider::updateGeometryPaddle ()
225 setRect (m_xScene - PADDLE_WIDTH / 2,
226 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.