mirror of
https://github.com/subsurface/subsurface.git
synced 2024-12-02 23:20:20 +00:00
Adds real support for ToolTips.
This patch changes the Event drawing so it can display tooltips. It is now the responsibility of the item to show / hide a tooltip. A bit of code-refactoring got on here too because I was using only QGraphicsItem calls and I wanted to use a hover in / hover out event to show / hide the tooltip. Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
c928b9cb94
commit
d590cb9519
2 changed files with 160 additions and 60 deletions
|
@ -10,6 +10,7 @@
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
#include <QIcon>
|
#include <QIcon>
|
||||||
#include <QPropertyAnimation>
|
#include <QPropertyAnimation>
|
||||||
|
#include <QGraphicsSceneHoverEvent>
|
||||||
|
|
||||||
#include "../color.h"
|
#include "../color.h"
|
||||||
#include "../display.h"
|
#include "../display.h"
|
||||||
|
@ -323,32 +324,13 @@ void ProfileGraphicsView::plot_one_event(struct graphics_context *gc, struct plo
|
||||||
}
|
}
|
||||||
|
|
||||||
/* draw a little triangular marker and attach tooltip */
|
/* draw a little triangular marker and attach tooltip */
|
||||||
QPolygonF poly;
|
|
||||||
poly.push_back(QPointF(-8, 16));
|
|
||||||
poly.push_back(QPointF(8, 16));
|
|
||||||
poly.push_back(QPointF(0, 0));
|
|
||||||
poly.push_back(QPointF(-8, 16));
|
|
||||||
|
|
||||||
int x = SCALEX(gc, ev->time.seconds);
|
int x = SCALEX(gc, ev->time.seconds);
|
||||||
int y = SCALEY(gc, depth);
|
int y = SCALEY(gc, depth);
|
||||||
|
|
||||||
QPen pen = defaultPen;
|
EventItem *item = new EventItem();
|
||||||
pen.setBrush(QBrush(profile_color[ALERT_BG].first()));
|
item->setPos(x, y);
|
||||||
|
scene()->addItem(item);
|
||||||
QGraphicsPolygonItem *triangle = new QGraphicsPolygonItem();
|
|
||||||
triangle->setPolygon(poly);
|
|
||||||
triangle->setBrush(QBrush(profile_color[ALERT_BG].first()));
|
|
||||||
triangle->setPen(pen);
|
|
||||||
triangle->setFlag(QGraphicsItem::ItemIgnoresTransformations);
|
|
||||||
triangle->setPos(x, y);
|
|
||||||
|
|
||||||
QGraphicsLineItem *line = new QGraphicsLineItem(0,5,0,10, triangle);
|
|
||||||
line->setPen(defaultPen);
|
|
||||||
|
|
||||||
QGraphicsEllipseItem *ball = new QGraphicsEllipseItem(-1, 12, 2,2, triangle);
|
|
||||||
ball->setBrush(QBrush(Qt::black));
|
|
||||||
|
|
||||||
scene()->addItem(triangle);
|
|
||||||
|
|
||||||
/* we display the event on screen - so translate */
|
/* we display the event on screen - so translate */
|
||||||
QString name = tr(ev->name);
|
QString name = tr(ev->name);
|
||||||
|
@ -373,9 +355,8 @@ void ProfileGraphicsView::plot_one_event(struct graphics_context *gc, struct plo
|
||||||
ev->flags == SAMPLE_FLAGS_END ? tr("Starts with space!", " end") : "";
|
ev->flags == SAMPLE_FLAGS_END ? tr("Starts with space!", " end") : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
//attach_tooltip(x-6, y, 12, 12, buffer, ev);
|
item->setToolTipController(toolTip);
|
||||||
//triangle->setToolTip(name);
|
item->addToolTip(name);
|
||||||
toolTip->addToolTip(name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProfileGraphicsView::plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
|
void ProfileGraphicsView::plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
|
||||||
|
@ -639,16 +620,16 @@ void ProfileGraphicsView::resizeEvent(QResizeEvent *event)
|
||||||
|
|
||||||
void ToolTipItem::addToolTip(const QString& toolTip, const QIcon& icon)
|
void ToolTipItem::addToolTip(const QString& toolTip, const QIcon& icon)
|
||||||
{
|
{
|
||||||
qDebug() << "Tooltip Adicionado" << toolTip;
|
|
||||||
|
|
||||||
QGraphicsPixmapItem *iconItem = 0;
|
QGraphicsPixmapItem *iconItem = 0;
|
||||||
|
double yValue = title->boundingRect().height() + SPACING + toolTips.keys().size() * ICON_SMALL + SPACING;
|
||||||
|
|
||||||
if (!icon.isNull()) {
|
if (!icon.isNull()) {
|
||||||
iconItem = new QGraphicsPixmapItem(icon.pixmap(ICON_SMALL,ICON_SMALL), this);
|
iconItem = new QGraphicsPixmapItem(icon.pixmap(ICON_SMALL,ICON_SMALL), this);
|
||||||
iconItem->setPos( 4, toolTips.keys().size() * ICON_SMALL + 4);
|
iconItem->setPos( SPACING, yValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(toolTip, this);
|
QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(toolTip, this);
|
||||||
textItem->setPos( 4 + ICON_SMALL + 4, toolTips.keys().size() * ICON_SMALL + 4);
|
textItem->setPos( SPACING + ICON_SMALL + SPACING, yValue);
|
||||||
textItem->setPen(QPen(Qt::white, 1));
|
textItem->setPen(QPen(Qt::white, 1));
|
||||||
textItem->setBrush(QBrush(Qt::white));
|
textItem->setBrush(QBrush(Qt::white));
|
||||||
textItem->setFlag(ItemIgnoresTransformations);
|
textItem->setFlag(ItemIgnoresTransformations);
|
||||||
|
@ -662,6 +643,22 @@ void ToolTipItem::removeToolTip(const QString& toolTip)
|
||||||
ToolTip toBeRemoved = toolTips[toolTip];
|
ToolTip toBeRemoved = toolTips[toolTip];
|
||||||
delete toBeRemoved.first;
|
delete toBeRemoved.first;
|
||||||
delete toBeRemoved.second;
|
delete toBeRemoved.second;
|
||||||
|
toolTips.remove(toolTip);
|
||||||
|
|
||||||
|
int toolTipIndex = 0;
|
||||||
|
|
||||||
|
// We removed a toolTip, let's move the others to the correct location
|
||||||
|
Q_FOREACH(ToolTip t, toolTips){
|
||||||
|
double yValue = title->boundingRect().height() + SPACING + toolTipIndex * ICON_SMALL + SPACING;
|
||||||
|
|
||||||
|
// Icons can be null.
|
||||||
|
if (t.first)
|
||||||
|
t.first->setPos(SPACING, yValue);
|
||||||
|
|
||||||
|
t.second->setPos(SPACING + ICON_SMALL + SPACING, yValue);
|
||||||
|
toolTipIndex++;
|
||||||
|
}
|
||||||
|
|
||||||
expand();
|
expand();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -700,6 +697,7 @@ void ToolTipItem::setRect(const QRectF& r)
|
||||||
b->setBrush(c);
|
b->setBrush(c);
|
||||||
background = b;
|
background = b;
|
||||||
|
|
||||||
|
updateTitlePosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -715,30 +713,125 @@ void ToolTipItem::collapse()
|
||||||
|
|
||||||
void ToolTipItem::expand()
|
void ToolTipItem::expand()
|
||||||
{
|
{
|
||||||
QRectF newRect = childrenBoundingRect();
|
QRectF currentRect = rectangle;
|
||||||
newRect = QRect(0, 0, newRect.width() + 8, newRect.height() + 8);
|
QRectF nextRectangle;
|
||||||
if (newRect != boundingRect()) {
|
|
||||||
QRectF newRect = childrenBoundingRect();
|
double width = 0;
|
||||||
|
Q_FOREACH(ToolTip t, toolTips) {
|
||||||
|
if (t.second->boundingRect().width() > width)
|
||||||
|
width = t.second->boundingRect().width();
|
||||||
|
}
|
||||||
|
|
||||||
|
double height = toolTips.count() * 18 + title->boundingRect().height() + SPACING;
|
||||||
|
/* 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");
|
QPropertyAnimation *animation = new QPropertyAnimation(this, "rect");
|
||||||
animation->setDuration(100);
|
animation->setDuration(100);
|
||||||
animation->setStartValue(boundingRect());
|
animation->setStartValue(rectangle);
|
||||||
animation->setEndValue(newRect);
|
animation->setEndValue(nextRectangle);
|
||||||
animation->start(QAbstractAnimation::DeleteWhenStopped);
|
animation->start(QAbstractAnimation::DeleteWhenStopped);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ToolTipItem::ToolTipItem(QGraphicsItem* parent): QGraphicsPathItem(parent), background(0)
|
ToolTipItem::ToolTipItem(QGraphicsItem* parent): QGraphicsPathItem(parent), background(0)
|
||||||
{
|
{
|
||||||
setRect(QRectF(0,0,ICON_SMALL, ICON_SMALL));
|
title = new QGraphicsSimpleTextItem(tr("Information"), this);
|
||||||
setFlag(QGraphicsItem::ItemIgnoresTransformations);
|
separator = new QGraphicsLineItem(this);
|
||||||
setFlag(QGraphicsItem::ItemIsMovable);
|
|
||||||
|
setFlag(ItemIgnoresTransformations);
|
||||||
|
setFlag(ItemIsMovable);
|
||||||
|
|
||||||
|
updateTitlePosition();
|
||||||
|
setZValue(99);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ToolTipStatusHandler::mousePressEvent(QGraphicsSceneMouseEvent* event)
|
void ToolTipItem::updateTitlePosition()
|
||||||
{
|
{
|
||||||
QGraphicsItem::mousePressEvent(event);
|
if (rectangle.width() < title->boundingRect().width() + SPACING*4 ){
|
||||||
|
QRectF newRect = rectangle;
|
||||||
|
newRect.setWidth(title->boundingRect().width() + SPACING*4);
|
||||||
|
newRect.setHeight( newRect.height() ? newRect.height() : ICON_SMALL );
|
||||||
|
setRect(newRect);
|
||||||
}
|
}
|
||||||
|
|
||||||
ToolTipStatusHandler::ToolTipStatusHandler(QObject* parent): QObject(parent), QGraphicsEllipseItem()
|
title->setPos(boundingRect().width()/2 -title->boundingRect().width()/2, 0);
|
||||||
{
|
title->setFlag(ItemIgnoresTransformations);
|
||||||
|
title->setPen(QPen(Qt::white, 1));
|
||||||
|
title->setBrush(Qt::white);
|
||||||
|
|
||||||
|
if (toolTips.size() > 0){
|
||||||
|
double x1 = 0;
|
||||||
|
double y1 = title->pos().y() + SPACING/2 + title->boundingRect().height();
|
||||||
|
double x2 = boundingRect().width() - 4;
|
||||||
|
double y2 = y1;
|
||||||
|
|
||||||
|
separator->setLine(x1, y1, x2, y2);
|
||||||
|
separator->setFlag(ItemIgnoresTransformations);
|
||||||
|
separator->setPen(QPen(Qt::white));
|
||||||
|
}else{
|
||||||
|
separator->setLine(QLineF());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
EventItem::EventItem(QGraphicsItem* parent): QGraphicsPolygonItem(parent)
|
||||||
|
{
|
||||||
|
setFlag(ItemIgnoresTransformations);
|
||||||
|
setFlag(ItemIsFocusable);
|
||||||
|
setAcceptHoverEvents(true);
|
||||||
|
|
||||||
|
QPolygonF poly;
|
||||||
|
poly.push_back(QPointF(-8, 16));
|
||||||
|
poly.push_back(QPointF(8, 16));
|
||||||
|
poly.push_back(QPointF(0, 0));
|
||||||
|
poly.push_back(QPointF(-8, 16));
|
||||||
|
|
||||||
|
QPen defaultPen ;
|
||||||
|
defaultPen.setJoinStyle(Qt::RoundJoin);
|
||||||
|
defaultPen.setCapStyle(Qt::RoundCap);
|
||||||
|
defaultPen.setWidth(2);
|
||||||
|
|
||||||
|
QPen pen = defaultPen;
|
||||||
|
pen.setBrush(QBrush(profile_color[ALERT_BG].first()));
|
||||||
|
|
||||||
|
setPolygon(poly);
|
||||||
|
setBrush(QBrush(profile_color[ALERT_BG].first()));
|
||||||
|
setPen(pen);
|
||||||
|
|
||||||
|
QGraphicsLineItem *line = new QGraphicsLineItem(0,5,0,10, this);
|
||||||
|
line->setPen(QPen(Qt::black, 2));
|
||||||
|
|
||||||
|
QGraphicsEllipseItem *ball = new QGraphicsEllipseItem(-1, 12, 2,2, this);
|
||||||
|
ball->setBrush(QBrush(Qt::black));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void EventItem::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
|
||||||
|
{
|
||||||
|
controller->addToolTip(text, icon);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EventItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
|
||||||
|
{
|
||||||
|
controller->removeToolTip(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EventItem::addToolTip(const QString& t, const QIcon& i)
|
||||||
|
{
|
||||||
|
text = t;
|
||||||
|
icon = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EventItem::setToolTipController(ToolTipItem* c)
|
||||||
|
{
|
||||||
|
controller = c;
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,24 +10,14 @@ struct graphics_context;
|
||||||
struct plot_info;
|
struct plot_info;
|
||||||
typedef struct text_render_options text_render_options_t;
|
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 {
|
class ToolTipItem :public QObject, public QGraphicsPathItem {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
void updateTitlePosition();
|
||||||
Q_PROPERTY(QRectF rect READ boundingRect WRITE setRect)
|
Q_PROPERTY(QRectF rect READ boundingRect WRITE setRect)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
enum Status{COLLAPSED, EXPANDED};
|
enum Status{COLLAPSED, EXPANDED};
|
||||||
enum {ICON_SMALL = 16, ICON_MEDIUM = 24, ICON_BIG = 32};
|
enum {ICON_SMALL = 16, ICON_MEDIUM = 24, ICON_BIG = 32, SPACING=4};
|
||||||
|
|
||||||
explicit ToolTipItem(QGraphicsItem* parent = 0);
|
explicit ToolTipItem(QGraphicsItem* parent = 0);
|
||||||
|
|
||||||
|
@ -42,12 +32,30 @@ public Q_SLOTS:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
typedef QPair<QGraphicsPixmapItem*, QGraphicsSimpleTextItem*> ToolTip;
|
typedef QPair<QGraphicsPixmapItem*, QGraphicsSimpleTextItem*> ToolTip;
|
||||||
enum Status status;
|
|
||||||
QMap<QString, ToolTip > toolTips;
|
QMap<QString, ToolTip > toolTips;
|
||||||
QGraphicsRectItem *background;
|
QGraphicsRectItem *background;
|
||||||
|
QGraphicsLineItem *separator;
|
||||||
|
QGraphicsSimpleTextItem *title;
|
||||||
|
|
||||||
QRectF rectangle;
|
QRectF rectangle;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class EventItem : public QGraphicsPolygonItem{
|
||||||
|
public:
|
||||||
|
explicit EventItem(QGraphicsItem* parent = 0);
|
||||||
|
void addToolTip(const QString& text,const QIcon& icon = QIcon());
|
||||||
|
void setToolTipController(ToolTipItem *controller);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void hoverEnterEvent(QGraphicsSceneHoverEvent* event);
|
||||||
|
void hoverLeaveEvent(QGraphicsSceneHoverEvent* event);
|
||||||
|
|
||||||
|
private:
|
||||||
|
ToolTipItem *controller;
|
||||||
|
QString text;
|
||||||
|
QIcon icon;
|
||||||
|
};
|
||||||
|
|
||||||
class ProfileGraphicsView : public QGraphicsView {
|
class ProfileGraphicsView : public QGraphicsView {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
|
@ -70,5 +78,4 @@ private:
|
||||||
ToolTipItem *toolTip;
|
ToolTipItem *toolTip;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue