00001
00002
00003
00004
00005
00006
00007 #include <QBrush>
00008 #include <QCursor>
00009 #include <QDebug>
00010 #include <QGraphicsLineItem>
00011 #include <QGraphicsPolygonItem>
00012 #include <QGraphicsScene>
00013 #include <QGraphicsSceneMouseEvent>
00014 #include <QGraphicsView>
00015 #include <QPen>
00016 #include "ViewProfileDivider.h"
00017
00018 const double ARROW_WIDTH = 4.0;
00019 const double ARROW_HEIGHT = 5.0;
00020 const double DIVIDER_WIDTH = 0.0;
00021 const int PADDLE_HEIGHT = 10;
00022 const int PADDLE_WIDTH = 10;
00023 const double SHADED_AREA_OPACITY = 0.4;
00024 const int X_INITIAL = 0;
00025 const int SLOP = 2;
00026 const QColor ARROW_COLOR (Qt::NoPen);
00027 const QColor SHADED_AREA_COLOR = QColor (220, 220, 220);
00028 const QColor DIVIDER_COLOR = QColor (140, 140, 255);
00029
00030 ViewProfileDivider::ViewProfileDivider (QGraphicsScene &scene,
00031 QGraphicsView &view,
00032 int sceneWidth,
00033 int sceneHeight,
00034 int yCenter,
00035 bool isLowerBoundary) :
00036 QGraphicsRectItem (X_INITIAL,
00037 0,
00038 PADDLE_WIDTH,
00039 PADDLE_HEIGHT),
00040 m_view (view),
00041 m_yCenter (yCenter),
00042 m_divider (0),
00043 m_shadedArea (0),
00044 m_sceneWidth (sceneWidth),
00045 m_sceneHeight (sceneHeight),
00046 m_isLowerBoundary (isLowerBoundary)
00047 {
00048
00049
00050
00051 setVisible (true);
00052 setPen (QPen (DIVIDER_COLOR));
00053 setBrush (QBrush (QColor (140, 255, 140)));
00054 setOpacity (1.0);
00055 scene.addItem (this);
00056 setFlags (QGraphicsItem::ItemIsMovable |
00057 QGraphicsItem::ItemSendsGeometryChanges);
00058 setCursor (Qt::OpenHandCursor);
00059 setZValue (2.0);
00060
00061
00062 m_arrow = new QGraphicsPolygonItem (this);
00063
00064
00065 m_shadedArea = new QGraphicsRectItem (X_INITIAL,
00066 0,
00067 0,
00068 sceneHeight - 1);
00069 m_shadedArea->setOpacity (SHADED_AREA_OPACITY);
00070 m_shadedArea->setBrush (QBrush (SHADED_AREA_COLOR));
00071 m_shadedArea->setPen (Qt::NoPen);
00072 m_shadedArea->setZValue (0.0);
00073 scene.addItem (m_shadedArea);
00074
00075
00076
00077
00078 m_divider = new QGraphicsLineItem (X_INITIAL,
00079 -SLOP,
00080 X_INITIAL,
00081 2 * SLOP + sceneHeight);
00082 m_divider->setPen (QPen (QBrush (DIVIDER_COLOR), DIVIDER_WIDTH));
00083 m_divider->setZValue (1.0);
00084 scene.addItem (m_divider);
00085 }
00086
00087 QVariant ViewProfileDivider::itemChange (GraphicsItemChange change, const QVariant &value)
00088 {
00089 if (change == ItemPositionChange && scene ()) {
00090
00091
00092 QPointF newPos = QPointF (value.toPointF().x(), 0.0) + m_startDragPos;
00093 double newX = newPos.x();
00094 newX = qMax (newX, 0.0);
00095 newX = qMin (newX, (double) m_sceneWidth);
00096 newPos.setX (newX);
00097 newPos -= m_startDragPos;
00098
00099
00100 m_xScene = newX;
00101 updateGeometryDivider();
00102 updateGeometryNonPaddle ();
00103
00104 sendSignalMoved ();
00105
00106 return newPos;
00107 }
00108
00109 return QGraphicsRectItem::itemChange (change, value);
00110 }
00111
00112 void ViewProfileDivider::mousePressEvent(QGraphicsSceneMouseEvent * )
00113 {
00114
00115 m_startDragPos = QPointF (rect().x () + rect().width () / 2.0,
00116 rect().y () + rect().height () / 2.0);
00117 }
00118
00119 void ViewProfileDivider::sendSignalMoved ()
00120 {
00121 if (m_isLowerBoundary) {
00122 emit signalMovedLow (m_xScene);
00123 } else {
00124 emit signalMovedHigh (m_xScene);
00125 }
00126 }
00127
00128 void ViewProfileDivider::setX (double x,
00129 double xLow,
00130 double xHigh)
00131 {
00132
00133 m_xScene = m_sceneWidth * (x - xLow) / (xHigh - xLow);
00134 sendSignalMoved ();
00135
00136 updateGeometryPaddle ();
00137 updateGeometryDivider ();
00138 updateGeometryNonPaddle ();
00139
00140
00141 double xLeft = rect().left() + rect().width() / 2.0 - ARROW_WIDTH / 2.0;
00142 double xRight = rect().left() + rect().width() / 2.0 + ARROW_WIDTH / 2.0;
00143 double yTop = rect().top() + rect().height() / 2.0 - ARROW_HEIGHT / 2.0;
00144 double yMiddle = rect().top() + rect().height() / 2.0;
00145 double yBottom = rect().top() + rect().height() / 2.0 + ARROW_HEIGHT / 2.0;
00146
00147 QPolygonF polygonArrow;
00148 if (m_isLowerBoundary) {
00149
00150
00151 polygonArrow.push_front (QPointF (xLeft, yTop));
00152 polygonArrow.push_front (QPointF (xRight, yMiddle));
00153 polygonArrow.push_front (QPointF (xLeft, yBottom));
00154
00155 } else {
00156
00157
00158 polygonArrow.push_front (QPointF (xRight, yTop));
00159 polygonArrow.push_front (QPointF (xLeft, yMiddle));
00160 polygonArrow.push_front (QPointF (xRight, yBottom));
00161 }
00162 m_arrow->setPolygon (polygonArrow);
00163 m_arrow->setPen (QPen (Qt::black));
00164 m_arrow->setBrush (QBrush (ARROW_COLOR));
00165 }
00166
00167 void ViewProfileDivider::slotOtherMoved(double xSceneOther)
00168 {
00169 m_xSceneOther = xSceneOther;
00170 updateGeometryNonPaddle ();
00171 }
00172
00173 void ViewProfileDivider::updateGeometryDivider ()
00174 {
00175 m_divider->setLine (m_xScene,
00176 -SLOP,
00177 m_xScene,
00178 2 * SLOP + m_sceneHeight);
00179 }
00180
00181 void ViewProfileDivider::updateGeometryNonPaddle()
00182 {
00183 if (m_isLowerBoundary) {
00184 if (m_xScene <= m_xSceneOther) {
00185
00186
00187 m_shadedArea->setRect (-SLOP,
00188 -SLOP,
00189 SLOP + m_xScene,
00190 2 * SLOP + m_sceneHeight);
00191
00192 } else {
00193
00194
00195 m_shadedArea->setRect (m_xSceneOther,
00196 -SLOP,
00197 m_xScene - m_xSceneOther,
00198 2 * SLOP + m_sceneHeight);
00199
00200 }
00201 } else {
00202
00203 if (m_xSceneOther <= m_xScene) {
00204
00205
00206 m_shadedArea->setRect (m_xScene,
00207 -SLOP,
00208 SLOP + m_sceneWidth - m_xScene,
00209 2 * SLOP + m_sceneHeight);
00210
00211 } else {
00212
00213
00214
00215 m_shadedArea->setRect (m_xSceneOther,
00216 -SLOP,
00217 0,
00218 2 * SLOP + m_sceneHeight);
00219 }
00220 }
00221 }
00222
00223 void ViewProfileDivider::updateGeometryPaddle ()
00224 {
00225 setRect (m_xScene - PADDLE_WIDTH / 2,
00226 m_yCenter - PADDLE_HEIGHT / 2,
00227 PADDLE_WIDTH,
00228 PADDLE_HEIGHT);
00229 }