Plotted the Scale on the Ruler ( depth / time ) on the planner

Plotted the Scale on the Ruler in the planner.
There's a tiny bit issue - mostly noticiable while resizing,
but it's not a killer.

Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
This commit is contained in:
Tomaz Canabrava 2013-09-16 20:10:12 -03:00
parent 493f366765
commit 242ef056f6
2 changed files with 45 additions and 23 deletions

View file

@ -90,6 +90,7 @@ DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent)
); );
timeLine->setOrientation(Qt::Horizontal); timeLine->setOrientation(Qt::Horizontal);
timeLine->setTickSize(fromPercent(1, Qt::Vertical)); timeLine->setTickSize(fromPercent(1, Qt::Vertical));
timeLine->setTextColor(getColor(TIME_TEXT));
timeLine->updateTicks(); timeLine->updateTicks();
scene()->addItem(timeLine); scene()->addItem(timeLine);
@ -106,6 +107,7 @@ DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent)
depthLine->setOrientation(Qt::Vertical); depthLine->setOrientation(Qt::Vertical);
depthLine->setTickSize(fromPercent(1, Qt::Horizontal)); depthLine->setTickSize(fromPercent(1, Qt::Horizontal));
depthLine->setColor(getColor(DEPTH_GRID)); depthLine->setColor(getColor(DEPTH_GRID));
depthLine->setTextColor(getColor(SAMPLE_DEEP));
depthLine->updateTicks(); depthLine->updateTicks();
scene()->addItem(depthLine); scene()->addItem(depthLine);
@ -633,30 +635,21 @@ void DiveHandler::mousePressEvent(QGraphicsSceneMouseEvent* event)
void Ruler::setMaximum(double maximum) void Ruler::setMaximum(double maximum)
{ {
maxText->setText(QString::number(maximum));
if(orientation == Qt::Horizontal)
maxText->setPos( line().x2(), line().y2() -5 );
else
maxText->setPos( line().x1() - 50, line().y2());
max = maximum; max = maximum;
} }
void Ruler::setMinimum(double minimum) void Ruler::setMinimum(double minimum)
{ {
minText->setText(QString::number(minimum));
if(orientation == Qt::Horizontal)
minText->setPos( line().x1(), line().y2() -5 );
else
minText->setPos( line().x1() - 50, line().y1());
min = minimum; min = minimum;
} }
Ruler::Ruler() : orientation(Qt::Horizontal), void Ruler::setTextColor(const QColor& color)
minText(new QGraphicsSimpleTextItem(this)), {
maxText(new QGraphicsSimpleTextItem(this)) textColor = color;
}
Ruler::Ruler() : orientation(Qt::Horizontal)
{ {
minText->setFlag(QGraphicsItem::ItemIgnoresTransformations);
maxText->setFlag(QGraphicsItem::ItemIgnoresTransformations);
} }
void Ruler::setOrientation(Qt::Orientation o) void Ruler::setOrientation(Qt::Orientation o)
@ -671,33 +664,61 @@ void Ruler::updateTicks()
{ {
qDeleteAll(ticks); qDeleteAll(ticks);
ticks.clear(); ticks.clear();
qDeleteAll(labels);
labels.clear();
QLineF m = line(); QLineF m = line();
QGraphicsLineItem *item = NULL; QGraphicsLineItem *item = NULL;
QGraphicsSimpleTextItem *label = NULL;
double steps = (max - min) / interval;
qreal pos;
double currValue = min;
if (orientation == Qt::Horizontal) { if (orientation == Qt::Horizontal) {
double steps = (max - min) / interval;
double stepSize = (m.x2() - m.x1()) / steps; double stepSize = (m.x2() - m.x1()) / steps;
qreal pos; for (pos = m.x1(); pos < m.x2(); pos += stepSize, currValue += interval) {
for (pos = m.x1(); pos < m.x2(); pos += stepSize) {
item = new QGraphicsLineItem(pos, m.y1(), pos, m.y1() + tickSize, this); item = new QGraphicsLineItem(pos, m.y1(), pos, m.y1() + tickSize, this);
item->setPen(pen()); item->setPen(pen());
ticks.push_back(item); ticks.push_back(item);
label = new QGraphicsSimpleTextItem(QString::number(currValue), this);
label->setBrush(QBrush(textColor));
label->setFlag(ItemIgnoresTransformations);
label->setPos(pos - label->boundingRect().width()/2, m.y1() + tickSize + 5);
labels.push_back(label);
} }
item = new QGraphicsLineItem(pos, m.y1(), pos, m.y1() + tickSize, this); item = new QGraphicsLineItem(pos, m.y1(), pos, m.y1() + tickSize, this);
item->setPen(pen()); item->setPen(pen());
ticks.push_back(item); ticks.push_back(item);
label = new QGraphicsSimpleTextItem(QString::number(currValue), this);
label->setBrush(QBrush(textColor));
label->setFlag(ItemIgnoresTransformations);
label->setPos(pos - label->boundingRect().width()/2, m.y1() + tickSize + 5);
labels.push_back(label);
} else { } else {
double steps = (max - min) / interval;
double stepSize = (m.y2() - m.y1()) / steps; double stepSize = (m.y2() - m.y1()) / steps;
qreal pos; for (pos = m.y1(); pos < m.y2(); pos += stepSize, currValue += interval) {
for (pos = m.y1(); pos < m.y2(); pos += stepSize) {
item = new QGraphicsLineItem(m.x1(), pos, m.x1() - tickSize, pos, this); item = new QGraphicsLineItem(m.x1(), pos, m.x1() - tickSize, pos, this);
item->setPen(pen()); item->setPen(pen());
ticks.push_back(item); ticks.push_back(item);
label = new QGraphicsSimpleTextItem(QString::number(currValue), this);
label->setBrush(QBrush(textColor));
label->setFlag(ItemIgnoresTransformations);
label->setPos(m.x2() - 80, pos);
labels.push_back(label);
} }
item = new QGraphicsLineItem(m.x1(), pos, m.x1() - tickSize, pos, this); item = new QGraphicsLineItem(m.x1(), pos, m.x1() - tickSize, pos, this);
item->setPen(pen()); item->setPen(pen());
ticks.push_back(item); ticks.push_back(item);
label = new QGraphicsSimpleTextItem(QString::number(currValue), this);
label->setBrush(QBrush(textColor));
label->setFlag(ItemIgnoresTransformations);
label->setPos(m.x2() - 80, pos);
labels.push_back(label);
} }
} }

View file

@ -104,18 +104,19 @@ public:
qreal percentAt(const QPointF& p); qreal percentAt(const QPointF& p);
qreal posAtValue(qreal value); qreal posAtValue(qreal value);
void setColor(const QColor& color); void setColor(const QColor& color);
void setTextColor(const QColor& color);
private: private:
Qt::Orientation orientation; Qt::Orientation orientation;
QList<QGraphicsLineItem*> ticks; QList<QGraphicsLineItem*> ticks;
QGraphicsSimpleTextItem *minText; QList<QGraphicsSimpleTextItem*> labels;
QGraphicsSimpleTextItem *maxText;
double min; double min;
double max; double max;
double interval; double interval;
double posBegin; double posBegin;
double posEnd; double posEnd;
double tickSize; double tickSize;
QColor textColor;
}; };
class DivePlannerGraphics : public QGraphicsView { class DivePlannerGraphics : public QGraphicsView {