Move the divetooltipitem to its own file.

This is needed so we can share the dive tooltip item with the
new and old profile at the same time. Next few commits will be
setting the functionality of the tooltip item on the new one.

Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2014-02-05 14:25:28 -02:00 committed by Dirk Hohndel
parent ddd7d2edcc
commit c1ed9babc7
5 changed files with 269 additions and 240 deletions

View file

@ -0,0 +1,209 @@
#include "divetooltipitem.h"
#include <QPropertyAnimation>
#include <QGraphicsSceneMouseEvent>
#include <QPen>
#include <QBrush>
#include <QGraphicsScene>
#include <QSettings>
#include <QGraphicsView>
#define PORT_IN_PROGRESS 1
#ifdef PORT_IN_PROGRESS
#include "display.h"
#endif
void ToolTipItem::addToolTip(const QString& toolTip, const QIcon& icon)
{
QGraphicsPixmapItem *iconItem = 0;
double yValue = title->boundingRect().height() + SPACING;
Q_FOREACH(ToolTip t, toolTips) {
yValue += t.second->boundingRect().height();
}
if (!icon.isNull()) {
iconItem = new QGraphicsPixmapItem(icon.pixmap(ICON_SMALL,ICON_SMALL), this);
iconItem->setPos(SPACING, yValue);
}
QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(toolTip, this);
textItem->setPos(SPACING + ICON_SMALL + SPACING, yValue);
textItem->setBrush(QBrush(Qt::white));
textItem->setFlag(ItemIgnoresTransformations);
toolTips.push_back(qMakePair(iconItem, textItem));
expand();
}
void ToolTipItem::refresh(struct graphics_context *gc, QPointF pos)
{
clear();
int time = (pos.x() * gc->maxtime) / gc->maxx;
char buffer[500];
get_plot_details(gc, time, buffer, 500);
addToolTip(QString(buffer));
QList<QGraphicsItem*> items = scene()->items(pos, Qt::IntersectsItemShape, Qt::DescendingOrder, transform());
Q_FOREACH(QGraphicsItem *item, items) {
if (!item->toolTip().isEmpty())
addToolTip(item->toolTip());
}
}
void ToolTipItem::clear()
{
Q_FOREACH(ToolTip t, toolTips) {
delete t.first;
delete t.second;
}
toolTips.clear();
}
void ToolTipItem::setRect(const QRectF& r)
{
// qDeleteAll(childItems());
delete background;
rectangle = r;
setBrush(QBrush(Qt::white));
setPen(QPen(Qt::black, 0.5));
// Creates a 2pixels border
QPainterPath border;
border.addRoundedRect(-4, -4, rectangle.width() + 8, rectangle.height() + 10, 3, 3);
border.addRoundedRect(-1, -1, rectangle.width() + 3, rectangle.height() + 4, 3, 3);
setPath(border);
QPainterPath bg;
bg.addRoundedRect(-1, -1, rectangle.width() + 3, rectangle.height() + 4, 3, 3);
QColor c = QColor(Qt::black);
c.setAlpha(155);
QGraphicsPathItem *b = new QGraphicsPathItem(bg, this);
b->setFlag(ItemStacksBehindParent);
b->setFlag(ItemIgnoresTransformations);
b->setBrush(c);
b->setPen(QPen(QBrush(Qt::transparent), 0));
b->setZValue(-10);
background = b;
updateTitlePosition();
}
void ToolTipItem::collapse()
{
QPropertyAnimation *animation = new QPropertyAnimation(this, "rect");
animation->setDuration(100);
animation->setStartValue(nextRectangle);
animation->setEndValue(QRect(0, 0, ICON_SMALL, ICON_SMALL));
animation->start(QAbstractAnimation::DeleteWhenStopped);
clear();
status = COLLAPSED;
}
void ToolTipItem::expand()
{
if (!title)
return;
double width = 0, height = title->boundingRect().height() + SPACING;
Q_FOREACH(ToolTip t, toolTips) {
if (t.second->boundingRect().width() > width)
width = t.second->boundingRect().width();
height += t.second->boundingRect().height();
}
/* Left padding, Icon Size, space, right padding */
width += SPACING + ICON_SMALL + SPACING + SPACING;
if (width < title->boundingRect().width() + SPACING*2)
width = title->boundingRect().width() + SPACING*2;
if (height < ICON_SMALL)
height = ICON_SMALL;
nextRectangle.setWidth(width);
nextRectangle.setHeight(height);
QPropertyAnimation *animation = new QPropertyAnimation(this, "rect");
animation->setDuration(100);
animation->setStartValue(rectangle);
animation->setEndValue(nextRectangle);
animation->start(QAbstractAnimation::DeleteWhenStopped);
status = EXPANDED;
}
ToolTipItem::ToolTipItem(QGraphicsItem* parent): QGraphicsPathItem(parent), background(0)
{
title = new QGraphicsSimpleTextItem(tr("Information"), this);
separator = new QGraphicsLineItem(this);
setFlags(ItemIgnoresTransformations | ItemIsMovable | ItemClipsChildrenToShape);
status = COLLAPSED;
updateTitlePosition();
setZValue(99);
}
ToolTipItem::~ToolTipItem()
{
clear();
}
void ToolTipItem::updateTitlePosition()
{
if (rectangle.width() < title->boundingRect().width() + SPACING*4) {
QRectF newRect = rectangle;
newRect.setWidth(title->boundingRect().width() + SPACING*4);
newRect.setHeight((newRect.height() && isExpanded()) ? newRect.height() : ICON_SMALL);
setRect(newRect);
}
title->setPos(boundingRect().width()/2 - title->boundingRect().width()/2 -1, 0);
title->setFlag(ItemIgnoresTransformations);
title->setPen(QPen(Qt::white, 1));
title->setBrush(Qt::white);
if (toolTips.size() > 0) {
double x1 = 3;
double y1 = title->pos().y() + SPACING/2 + title->boundingRect().height();
double x2 = boundingRect().width() - 10;
double y2 = y1;
separator->setLine(x1, y1, x2, y2);
separator->setFlag(ItemIgnoresTransformations);
separator->setPen(QPen(Qt::white));
separator->show();
} else {
separator->hide();
}
}
bool ToolTipItem::isExpanded() {
return status == EXPANDED;
}
void ToolTipItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
persistPos();
QGraphicsPathItem::mouseReleaseEvent(event);
}
void ToolTipItem::persistPos()
{
QPoint currentPos = scene()->views().at(0)->mapFromScene(pos());
QSettings s;
s.beginGroup("ProfileMap");
s.setValue("tooltip_position", currentPos);
s.endGroup();
}
void ToolTipItem::readPos()
{
QSettings s;
s.beginGroup("ProfileMap");
QPointF value = scene()->views().at(0)->mapToScene(
s.value("tooltip_position").toPoint()
);
if (!scene()->sceneRect().contains(value)) {
value = QPointF(0,0);
}
setPos(value);
}

View file

@ -0,0 +1,54 @@
#ifndef DIVETOOLTIPITEM_H
#define DIVETOOLTIPITEM_H
#include <QGraphicsPathItem>
#include <QVector>
#include <QPair>
#include <QRectF>
#include <QIcon>
class QGraphicsLineItem;
class QGraphicsSimpleTextItem;
class QGraphicsPixmapItem;
struct graphics_context;
/* To use a tooltip, simply ->setToolTip on the QGraphicsItem that you want
* or, if it's a "global" tooltip, set it on the mouseMoveEvent of the ProfileGraphicsView.
*/
class ToolTipItem :public QObject, public QGraphicsPathItem
{
Q_OBJECT
void updateTitlePosition();
Q_PROPERTY(QRectF rect READ boundingRect WRITE setRect)
public:
enum Status{COLLAPSED, EXPANDED};
enum {ICON_SMALL = 16, ICON_MEDIUM = 24, ICON_BIG = 32, SPACING=4};
explicit ToolTipItem(QGraphicsItem* parent = 0);
virtual ~ToolTipItem();
void collapse();
void expand();
void clear();
void addToolTip(const QString& toolTip, const QIcon& icon = QIcon());
void refresh(struct graphics_context* gc, QPointF pos);
bool isExpanded();
void persistPos();
void readPos();
void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
public slots:
void setRect(const QRectF& rect);
private:
typedef QPair<QGraphicsPixmapItem*, QGraphicsSimpleTextItem*> ToolTip;
QVector<ToolTip> toolTips;
QGraphicsPathItem *background;
QGraphicsLineItem *separator;
QGraphicsSimpleTextItem *title;
Status status;
QRectF rectangle;
QRectF nextRectangle;
};
#endif

View file

@ -28,6 +28,7 @@
#include "../helpers.h" #include "../helpers.h"
#include "../planner.h" #include "../planner.h"
#include "../gettextfromc.h" #include "../gettextfromc.h"
#include "profile/divetooltipitem.h"
#include <libdivecomputer/parser.h> #include <libdivecomputer/parser.h>
#include <libdivecomputer/version.h> #include <libdivecomputer/version.h>
@ -1448,204 +1449,6 @@ void ProfileGraphicsView::on_scaleAction()
zoomed_plot = !zoomed_plot; zoomed_plot = !zoomed_plot;
refresh(); refresh();
} }
void ToolTipItem::addToolTip(const QString& toolTip, const QIcon& icon)
{
QGraphicsPixmapItem *iconItem = 0;
double yValue = title->boundingRect().height() + SPACING;
Q_FOREACH(ToolTip t, toolTips) {
yValue += t.second->boundingRect().height();
}
if (!icon.isNull()) {
iconItem = new QGraphicsPixmapItem(icon.pixmap(ICON_SMALL,ICON_SMALL), this);
iconItem->setPos(SPACING, yValue);
}
QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(toolTip, this);
textItem->setPos(SPACING + ICON_SMALL + SPACING, yValue);
textItem->setBrush(QBrush(Qt::white));
textItem->setFlag(ItemIgnoresTransformations);
toolTips.push_back(qMakePair(iconItem, textItem));
expand();
}
void ToolTipItem::refresh(struct graphics_context *gc, QPointF pos)
{
clear();
int time = (pos.x() * gc->maxtime) / gc->maxx;
char buffer[500];
get_plot_details(gc, time, buffer, 500);
addToolTip(QString(buffer));
QList<QGraphicsItem*> items = scene()->items(pos, Qt::IntersectsItemShape, Qt::DescendingOrder, transform());
Q_FOREACH(QGraphicsItem *item, items) {
if (!item->toolTip().isEmpty())
addToolTip(item->toolTip());
}
}
void ToolTipItem::clear()
{
Q_FOREACH(ToolTip t, toolTips) {
delete t.first;
delete t.second;
}
toolTips.clear();
}
void ToolTipItem::setRect(const QRectF& r)
{
// qDeleteAll(childItems());
delete background;
rectangle = r;
setBrush(QBrush(Qt::white));
setPen(QPen(Qt::black, 0.5));
// Creates a 2pixels border
QPainterPath border;
border.addRoundedRect(-4, -4, rectangle.width() + 8, rectangle.height() + 10, 3, 3);
border.addRoundedRect(-1, -1, rectangle.width() + 3, rectangle.height() + 4, 3, 3);
setPath(border);
QPainterPath bg;
bg.addRoundedRect(-1, -1, rectangle.width() + 3, rectangle.height() + 4, 3, 3);
QColor c = QColor(Qt::black);
c.setAlpha(155);
QGraphicsPathItem *b = new QGraphicsPathItem(bg, this);
b->setFlag(ItemStacksBehindParent);
b->setFlags(ItemIgnoresTransformations);
b->setBrush(c);
b->setPen(QPen(QBrush(Qt::transparent), 0));
b->setZValue(-10);
background = b;
updateTitlePosition();
}
void ToolTipItem::collapse()
{
QPropertyAnimation *animation = new QPropertyAnimation(this, "rect");
animation->setDuration(100);
animation->setStartValue(nextRectangle);
animation->setEndValue(QRect(0, 0, ICON_SMALL, ICON_SMALL));
animation->start(QAbstractAnimation::DeleteWhenStopped);
clear();
status = COLLAPSED;
}
void ToolTipItem::expand()
{
if (!title)
return;
double width = 0, height = title->boundingRect().height() + SPACING;
Q_FOREACH(ToolTip t, toolTips) {
if (t.second->boundingRect().width() > width)
width = t.second->boundingRect().width();
height += t.second->boundingRect().height();
}
/* Left padding, Icon Size, space, right padding */
width += SPACING + ICON_SMALL + SPACING + SPACING;
if (width < title->boundingRect().width() + SPACING*2)
width = title->boundingRect().width() + SPACING*2;
if (height < ICON_SMALL)
height = ICON_SMALL;
nextRectangle.setWidth(width);
nextRectangle.setHeight(height);
QPropertyAnimation *animation = new QPropertyAnimation(this, "rect");
animation->setDuration(100);
animation->setStartValue(rectangle);
animation->setEndValue(nextRectangle);
animation->start(QAbstractAnimation::DeleteWhenStopped);
status = EXPANDED;
}
ToolTipItem::ToolTipItem(QGraphicsItem* parent): QGraphicsPathItem(parent), background(0)
{
title = new QGraphicsSimpleTextItem(tr("Information"), this);
separator = new QGraphicsLineItem(this);
setFlags(ItemIgnoresTransformations | ItemIsMovable | ItemClipsChildrenToShape);
status = COLLAPSED;
updateTitlePosition();
setZValue(99);
}
ToolTipItem::~ToolTipItem()
{
clear();
}
void ToolTipItem::updateTitlePosition()
{
if (rectangle.width() < title->boundingRect().width() + SPACING*4) {
QRectF newRect = rectangle;
newRect.setWidth(title->boundingRect().width() + SPACING*4);
newRect.setHeight((newRect.height() && isExpanded()) ? newRect.height() : ICON_SMALL);
setRect(newRect);
}
title->setPos(boundingRect().width()/2 - title->boundingRect().width()/2 -1, 0);
title->setFlag(ItemIgnoresTransformations);
title->setPen(QPen(Qt::white, 1));
title->setBrush(Qt::white);
if (toolTips.size() > 0) {
double x1 = 3;
double y1 = title->pos().y() + SPACING/2 + title->boundingRect().height();
double x2 = boundingRect().width() - 10;
double y2 = y1;
separator->setLine(x1, y1, x2, y2);
separator->setFlag(ItemIgnoresTransformations);
separator->setPen(QPen(Qt::white));
separator->show();
} else {
separator->hide();
}
}
bool ToolTipItem::isExpanded() {
return status == EXPANDED;
}
void ToolTipItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
persistPos();
QGraphicsPathItem::mouseReleaseEvent(event);
}
void ToolTipItem::persistPos()
{
QPoint currentPos = scene()->views().at(0)->mapFromScene(pos());
QSettings s;
s.beginGroup("ProfileMap");
s.setValue("tooltip_position", currentPos);
s.endGroup();
}
void ToolTipItem::readPos()
{
QSettings s;
s.beginGroup("ProfileMap");
QPointF value = scene()->views().at(0)->mapToScene(
s.value("tooltip_position").toPoint()
);
if (!scene()->sceneRect().contains(value)) {
value = QPointF(0,0);
}
setPos(value);
}
QColor EventItem::getColor(const color_indice_t i) QColor EventItem::getColor(const color_indice_t i)
{ {
return profile_color[i].at((isGrayscale) ? 1 : 0); return profile_color[i].at((isGrayscale) ? 1 : 0);

View file

@ -12,47 +12,8 @@
struct graphics_context; struct graphics_context;
struct plot_info; struct plot_info;
/* To use a tooltip, simply ->setToolTip on the QGraphicsItem that you want
* or, if it's a "global" tooltip, set it on the mouseMoveEvent of the ProfileGraphicsView.
*/
class ToolTipItem :public QObject, public QGraphicsPathItem
{
Q_OBJECT
void updateTitlePosition();
Q_PROPERTY(QRectF rect READ boundingRect WRITE setRect)
public:
enum Status{COLLAPSED, EXPANDED};
enum {ICON_SMALL = 16, ICON_MEDIUM = 24, ICON_BIG = 32, SPACING=4};
explicit ToolTipItem(QGraphicsItem* parent = 0);
virtual ~ToolTipItem();
void collapse();
void expand();
void clear();
void addToolTip(const QString& toolTip, const QIcon& icon = QIcon());
void refresh(struct graphics_context* gc, QPointF pos);
bool isExpanded();
void persistPos();
void readPos();
void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
public slots:
void setRect(const QRectF& rect);
private:
typedef QPair<QGraphicsPixmapItem*, QGraphicsSimpleTextItem*> ToolTip;
QVector<ToolTip> toolTips;
QGraphicsPathItem *background;
QGraphicsLineItem *separator;
QGraphicsSimpleTextItem *title;
Status status;
QRectF rectangle;
QRectF nextRectangle;
};
class RulerItem; class RulerItem;
class ToolTipItem;
class RulerNodeItem : public QObject, public QGraphicsEllipseItem class RulerNodeItem : public QObject, public QGraphicsEllipseItem
{ {

View file

@ -73,7 +73,8 @@ HEADERS = \
qt-ui/profile/divecartesianaxis.h \ qt-ui/profile/divecartesianaxis.h \
qt-ui/profile/diveplotdatamodel.h \ qt-ui/profile/diveplotdatamodel.h \
qt-ui/profile/diveprofileitem.h \ qt-ui/profile/diveprofileitem.h \
qt-ui/profile/diveeventitem.h qt-ui/profile/diveeventitem.h \
qt-ui/profile/divetooltipitem.h
SOURCES = \ SOURCES = \
deco.c \ deco.c \
@ -134,7 +135,8 @@ SOURCES = \
qt-ui/profile/divecartesianaxis.cpp \ qt-ui/profile/divecartesianaxis.cpp \
qt-ui/profile/diveplotdatamodel.cpp \ qt-ui/profile/diveplotdatamodel.cpp \
qt-ui/profile/diveprofileitem.cpp \ qt-ui/profile/diveprofileitem.cpp \
qt-ui/profile/diveeventitem.cpp qt-ui/profile/diveeventitem.cpp \
qt-ui/profile/divetooltipitem.cpp
linux*: SOURCES += linux.c linux*: SOURCES += linux.c
mac: SOURCES += macos.c mac: SOURCES += macos.c