Fix the positioning of the Labels using the new DiveTextItem

This uses a combination of items on the canvas which makes it easier to
position it where I want.

This also broke the other texts because I forgot about them. I will
fix that on the next commit.

Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2014-01-19 17:19:00 -02:00 committed by Dirk Hohndel
parent 72b5bbce6e
commit 9d2344d01b
5 changed files with 71 additions and 29 deletions

View file

@ -27,7 +27,7 @@ void DiveCartesianAxis::setTextColor(const QColor& color)
textColor = color; textColor = color;
} }
DiveCartesianAxis::DiveCartesianAxis() : orientation(LeftToRight) DiveCartesianAxis::DiveCartesianAxis() : orientation(LeftToRight), showTicks(true), showText(true)
{ {
} }
@ -50,16 +50,30 @@ void DiveCartesianAxis::updateTicks()
double steps = (max - min) / interval; double steps = (max - min) / interval;
double currValue = min; double currValue = min;
if (!showTicks && !ticks.empty()){
qDeleteAll(ticks);
ticks.clear();
}
if(!showText && !labels.empty()){
qDeleteAll(labels);
labels.clear();
}
// Remove the uneeded Ticks / Texts. // Remove the uneeded Ticks / Texts.
if (!ticks.isEmpty() && ticks.size() > steps) { if (showTicks && !ticks.isEmpty() && ticks.size() > steps) {
while (ticks.size() > steps) { while (ticks.size() > steps) {
DiveLineItem *removedLine = ticks.takeLast(); DiveLineItem *removedLine = ticks.takeLast();
removedLine->animatedHide(); removedLine->animatedHide();
}
}
if (!labels.isEmpty() && labels.size() > steps) {
while (labels.size() > steps) {
DiveTextItem *removedText = labels.takeLast(); DiveTextItem *removedText = labels.takeLast();
removedText->animatedHide(); removedText->animatedHide();
} }
} }
// Move the remaining Ticks / Text to it's corerct position // Move the remaining Ticks / Text to it's corerct position
// Regartind the possibly new values for the Axis // Regartind the possibly new values for the Axis
qreal begin, stepSize; qreal begin, stepSize;
@ -78,7 +92,6 @@ void DiveCartesianAxis::updateTicks()
} }
stepSize = stepSize / steps; stepSize = stepSize / steps;
for (int i = 0, count = ticks.size(); i < count; i++, currValue += interval) { for (int i = 0, count = ticks.size(); i < count; i++, currValue += interval) {
qreal childPos; qreal childPos;
if (orientation == TopToBottom || orientation == LeftToRight) { if (orientation == TopToBottom || orientation == LeftToRight) {
@ -104,31 +117,56 @@ void DiveCartesianAxis::updateTicks()
} else { } else {
childPos = begin - i * stepSize; childPos = begin - i * stepSize;
} }
DiveLineItem *item = new DiveLineItem(this); DiveLineItem *item = NULL;
DiveTextItem *label = NULL;
if (showTicks){
item = new DiveLineItem(this);
item->setPen(pen()); item->setPen(pen());
ticks.push_back(item); ticks.push_back(item);
}
DiveTextItem *label = new DiveTextItem(this); if (showText){
label = new DiveTextItem(this);
label->setText(textForValue(currValue)); label->setText(textForValue(currValue));
label->setBrush(QBrush(textColor)); label->setBrush(QBrush(textColor));
}
labels.push_back(label); labels.push_back(label);
if (orientation == RightToLeft || orientation == LeftToRight) { if (orientation == RightToLeft || orientation == LeftToRight) {
if (showTicks){
item->setLine(0, 0, 0, tickSize); item->setLine(0, 0, 0, tickSize);
item->setPos(scene()->sceneRect().width() + 10, m.y1() + tickSize); // position it outside of the scene item->setPos(scene()->sceneRect().width() + 10, m.y1() + tickSize); // position it outside of the scene
item->animateMoveTo(childPos, m.y1() + tickSize); // anim it to scene. item->animateMoveTo(childPos, m.y1() + tickSize); // anim it to scene.
}
if(showText){
label->setAlignment(Qt::AlignBottom | Qt::AlignHCenter); label->setAlignment(Qt::AlignBottom | Qt::AlignHCenter);
label->setPos(scene()->sceneRect().width() + 10, m.y1() + tickSize); // position it outside of the scene); label->setPos(scene()->sceneRect().width() + 10, m.y1() + tickSize); // position it outside of the scene);
label->animateMoveTo(childPos, m.y1() + tickSize); label->animateMoveTo(childPos, m.y1() + tickSize);
}
} else { } else {
if(showTicks){
item->setLine(0, 0, tickSize, 0); item->setLine(0, 0, tickSize, 0);
item->setPos(m.x1() - tickSize, scene()->sceneRect().height() + 10); item->setPos(m.x1() - tickSize, scene()->sceneRect().height() + 10);
item->animateMoveTo(m.x1() - tickSize, childPos); item->animateMoveTo(m.x1() - tickSize, childPos);
}
if(showText){
label->setAlignment(Qt::AlignVCenter| Qt::AlignRight); label->setAlignment(Qt::AlignVCenter| Qt::AlignRight);
label->setPos(m.x1() - tickSize, scene()->sceneRect().height() + 10); label->setPos(m.x1() - tickSize, scene()->sceneRect().height() + 10);
label->animateMoveTo(m.x1() - tickSize, childPos); label->animateMoveTo(m.x1() - tickSize, childPos);
} }
} }
}
}
void DiveCartesianAxis::setShowText(bool show)
{
showText = show;
updateTicks();
}
void DiveCartesianAxis::setShowTicks(bool show)
{
showTicks = show;
updateTicks();
} }
QString DiveCartesianAxis::textForValue(double value) QString DiveCartesianAxis::textForValue(double value)

View file

@ -29,6 +29,8 @@ public:
qreal posAtValue(qreal value); qreal posAtValue(qreal value);
void setColor(const QColor& color); void setColor(const QColor& color);
void setTextColor(const QColor& color); void setTextColor(const QColor& color);
void setShowTicks(bool show);
void setShowText(bool show);
int unitSystem; int unitSystem;
signals: signals:
void sizeChanged(); void sizeChanged();
@ -43,6 +45,8 @@ protected:
double interval; double interval;
double tickSize; double tickSize;
QColor textColor; QColor textColor;
bool showTicks;
bool showText;
}; };
class DepthAxis : public DiveCartesianAxis { class DepthAxis : public DiveCartesianAxis {

View file

@ -7,6 +7,7 @@
#include <QFontMetrics> #include <QFontMetrics>
#include <QBrush> #include <QBrush>
#include <QPen> #include <QPen>
#include <QDebug>
DiveTextItem::DiveTextItem(QGraphicsItem* parent): QGraphicsItemGroup(parent), DiveTextItem::DiveTextItem(QGraphicsItem* parent): QGraphicsItemGroup(parent),
textBackgroundItem(NULL), textBackgroundItem(NULL),
@ -50,10 +51,10 @@ void DiveTextItem::updateText()
QRectF rect = fm.boundingRect(text); QRectF rect = fm.boundingRect(text);
yPos = (internalAlignFlags & Qt::AlignTop) ? -rect.height() : yPos = (internalAlignFlags & Qt::AlignTop) ? -rect.height() :
(internalAlignFlags & Qt::AlignBottom) ? 0 : (internalAlignFlags & Qt::AlignBottom) ? +rect.height() :
/*(internalAlignFlags & Qt::AlignVCenter ? */ -rect.height() / 2; /*(internalAlignFlags & Qt::AlignVCenter ? */ +rect.height() / 4;
yPos = (internalAlignFlags & Qt::AlignLeft ) ? 0 : xPos = (internalAlignFlags & Qt::AlignLeft ) ? +rect.width() :
(internalAlignFlags & Qt::AlignHCenter) ? -rect.width()/2 : (internalAlignFlags & Qt::AlignHCenter) ? -rect.width()/2 :
/* (internalAlignFlags & Qt::AlignRight) */ -rect.width(); /* (internalAlignFlags & Qt::AlignRight) */ -rect.width();

View file

@ -294,12 +294,10 @@ void ProfileWidget2::plotDives(QList<dive*> dives)
profileYAxis->updateTicks(); profileYAxis->updateTicks();
temperatureAxis->setMinimum(pInfo.mintemp); temperatureAxis->setMinimum(pInfo.mintemp);
temperatureAxis->setMaximum(pInfo.maxtemp); temperatureAxis->setMaximum(pInfo.maxtemp);
//temperatureAxis->updateTicks();
timeAxis->setMaximum(maxtime); timeAxis->setMaximum(maxtime);
timeAxis->updateTicks(); timeAxis->updateTicks();
cylinderPressureAxis->setMinimum(pInfo.minpressure); cylinderPressureAxis->setMinimum(pInfo.minpressure);
cylinderPressureAxis->setMaximum(pInfo.maxpressure); cylinderPressureAxis->setMaximum(pInfo.maxpressure);
cylinderPressureAxis->updateTicks();
meanDepth->animateMoveTo(3, profileYAxis->posAtValue(pInfo.meandepth)); meanDepth->animateMoveTo(3, profileYAxis->posAtValue(pInfo.meandepth));
dataModel->setDive(current_dive, pInfo); dataModel->setDive(current_dive, pInfo);

View file

@ -362,6 +362,7 @@ QColor ProfileGraphicsView::getColor(const color_indice_t i)
void ProfileGraphicsView::plot(struct dive *d, bool forceRedraw) void ProfileGraphicsView::plot(struct dive *d, bool forceRedraw)
{ {
return;
struct divecomputer *dc = NULL; struct divecomputer *dc = NULL;
if (d) if (d)