Added the first overlay of the tooltips, with some test data.

The tooltips now can:
1 - be moved around the canvas
2 - dynamically expand / retreat when a new tooltip is added.

Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2013-05-07 15:44:54 -03:00 committed by Dirk Hohndel
parent 5e4f06e6ad
commit c928b9cb94
2 changed files with 175 additions and 3 deletions

View file

@ -7,6 +7,9 @@
#include <QBrush>
#include <QDebug>
#include <QLineF>
#include <QSettings>
#include <QIcon>
#include <QPropertyAnimation>
#include "../color.h"
#include "../display.h"
@ -146,13 +149,21 @@ static void plot_set_scale(scale_mode_t scale)
void ProfileGraphicsView::plot(struct dive *dive)
{
// Clear the items before drawing this dive.
qDeleteAll(scene()->items());
scene()->clear();
if(!dive)
return;
QSettings s;
s.beginGroup("ProfileMap");
QPointF toolTipPos = s.value("tooltip_position", QPointF(0,0)).toPointF();
s.endGroup();
toolTip = new ToolTipItem();
toolTip->setPos(toolTipPos);
scene()->addItem(toolTip);
struct plot_info *pi;
struct divecomputer *dc = &dive->dc;
@ -363,7 +374,8 @@ void ProfileGraphicsView::plot_one_event(struct graphics_context *gc, struct plo
}
//attach_tooltip(x-6, y, 12, 12, buffer, ev);
triangle->setToolTip(name);
//triangle->setToolTip(name);
toolTip->addToolTip(name);
}
void ProfileGraphicsView::plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
@ -605,6 +617,16 @@ void ProfileGraphicsView::plot_text(struct graphics_context *gc, text_render_opt
scene()->addItem(item);
}
void ProfileGraphicsView::addToolTip(const QString& text, const QIcon& icon)
{
toolTip->addToolTip(text, icon);
}
void ProfileGraphicsView::removeToolTip(const QString& text)
{
toolTip->removeToolTip(text);
}
void ProfileGraphicsView::resizeEvent(QResizeEvent *event)
{
@ -614,3 +636,109 @@ void ProfileGraphicsView::resizeEvent(QResizeEvent *event)
QRectF r = scene()->sceneRect();
fitInView ( r.x() - 50, r.y() -50, r.width() + 100, r.height() + 100); // do a little bit of spacing;
}
void ToolTipItem::addToolTip(const QString& toolTip, const QIcon& icon)
{
qDebug() << "Tooltip Adicionado" << toolTip;
QGraphicsPixmapItem *iconItem = 0;
if (!icon.isNull()) {
iconItem = new QGraphicsPixmapItem(icon.pixmap(ICON_SMALL,ICON_SMALL), this);
iconItem->setPos( 4, toolTips.keys().size() * ICON_SMALL + 4);
}
QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(toolTip, this);
textItem->setPos( 4 + ICON_SMALL + 4, toolTips.keys().size() * ICON_SMALL + 4);
textItem->setPen(QPen(Qt::white, 1));
textItem->setBrush(QBrush(Qt::white));
textItem->setFlag(ItemIgnoresTransformations);
toolTips[toolTip] = qMakePair(iconItem, textItem);
expand();
}
void ToolTipItem::removeToolTip(const QString& toolTip)
{
ToolTip toBeRemoved = toolTips[toolTip];
delete toBeRemoved.first;
delete toBeRemoved.second;
expand();
}
void ToolTipItem::clear()
{
Q_FOREACH(ToolTip t, toolTips) {
delete t.first;
delete t.second;
}
toolTips.clear();
expand();
}
void ToolTipItem::setRect(const QRectF& r)
{
// qDeleteAll(childItems());
if (background) {
childItems().removeAt(childItems().indexOf(background));
delete background;
}
rectangle = r;
setBrush(QBrush(Qt::white));
setPen(QPen(Qt::black, 0.5));
QPainterPath border;
border.addRoundedRect(-2, -2, rectangle.width() + 4, rectangle.height()+ 4, 3, 3);
border.addRoundedRect( 0, 0, rectangle.width(), rectangle.height(), 3, 3);
setPath(border);
QGraphicsRectItem *b = new QGraphicsRectItem(-1, -1, rectangle.width()+1, rectangle.height()+1, this);
b->setFlag(ItemStacksBehindParent);
QColor c = QColor(Qt::black);
c.setAlpha(155);
b->setBrush(c);
background = b;
}
void ToolTipItem::collapse()
{
QRectF newRect = childrenBoundingRect();
QPropertyAnimation *animation = new QPropertyAnimation(this, "rect");
animation->setDuration(100);
animation->setStartValue(boundingRect());
animation->setEndValue(QRect(0, 0, ICON_SMALL, ICON_SMALL ));
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
void ToolTipItem::expand()
{
QRectF newRect = childrenBoundingRect();
newRect = QRect(0, 0, newRect.width() + 8, newRect.height() + 8);
if (newRect != boundingRect()) {
QRectF newRect = childrenBoundingRect();
QPropertyAnimation *animation = new QPropertyAnimation(this, "rect");
animation->setDuration(100);
animation->setStartValue(boundingRect());
animation->setEndValue(newRect);
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
}
ToolTipItem::ToolTipItem(QGraphicsItem* parent): QGraphicsPathItem(parent), background(0)
{
setRect(QRectF(0,0,ICON_SMALL, ICON_SMALL));
setFlag(QGraphicsItem::ItemIgnoresTransformations);
setFlag(QGraphicsItem::ItemIsMovable);
}
void ToolTipStatusHandler::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
QGraphicsItem::mousePressEvent(event);
}
ToolTipStatusHandler::ToolTipStatusHandler(QObject* parent): QObject(parent), QGraphicsEllipseItem()
{
}

View file

@ -2,17 +2,59 @@
#define PROFILEGRAPHICS_H
#include <QGraphicsView>
#include <QGraphicsItem>
#include <QIcon>
struct text_render_options;
struct graphics_context;
struct plot_info;
typedef struct text_render_options text_render_options_t;
class ToolTipItem;
class ToolTipStatusHandler;
class ToolTipStatusHandler :public QObject, public QGraphicsEllipseItem {
public:
explicit ToolTipStatusHandler(QObject* parent = 0);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent* event);
};
class ToolTipItem :public QObject, public QGraphicsPathItem {
Q_OBJECT
Q_PROPERTY(QRectF rect READ boundingRect WRITE setRect)
public:
enum Status {COLLAPSED, EXPANDED};
enum {ICON_SMALL = 16, ICON_MEDIUM = 24, ICON_BIG = 32};
explicit ToolTipItem(QGraphicsItem* parent = 0);
void collapse();
void expand();
void clear();
void addToolTip(const QString& toolTip, const QIcon& icon = QIcon());
void removeToolTip(const QString& toolTip);
public Q_SLOTS:
void setRect(const QRectF& rect);
private:
typedef QPair<QGraphicsPixmapItem*, QGraphicsSimpleTextItem*> ToolTip;
enum Status status;
QMap<QString, ToolTip > toolTips;
QGraphicsRectItem *background;
QRectF rectangle;
};
class ProfileGraphicsView : public QGraphicsView {
Q_OBJECT
public:
ProfileGraphicsView(QWidget* parent = 0);
void plot(struct dive *d);
void addToolTip(const QString& text, const QIcon& icon = QIcon());
void removeToolTip(const QString& text);
protected:
void resizeEvent(QResizeEvent *event);
@ -25,6 +67,8 @@ private:
QPen defaultPen;
QBrush defaultBrush;
ToolTipItem *toolTip;
};
#endif