subsurface/qt-ui/profile/divepixmapitem.cpp
Tomaz Canabrava 7bf63c5b0b Add a new 'show' Animation, and use it on the close button.
The error with 'ungrabMouse' warning that we got was because
we were removing an item that had the mouse grab instead of
waiting a few milisseconds so it won't be the mouse grabber
anymore.

So I'v used the Animations::hide() to get rid of it, and
since it worked well, I'v also added a Animations::show()
method to display it in a good fade-in way.

Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-07-30 14:15:09 -07:00

127 lines
3 KiB
C++

#include "divepixmapitem.h"
#include "animationfunctions.h"
#include <divepicturewidget.h>
#include <preferences.h>
#include <QPen>
#include <QBrush>
#include <QGraphicsDropShadowEffect>
#include <QDesktopServices>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsView>
#include <QUrl>
DivePixmapItem::DivePixmapItem(QObject *parent) : QObject(parent), QGraphicsPixmapItem()
{
}
DiveButtonItem::DiveButtonItem(QObject *parent): DivePixmapItem(parent)
{
}
void DiveButtonItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsItem::mousePressEvent(event);
emit clicked();
}
// If we have many many pictures on screen, maybe a shared-pixmap would be better to
// paint on screen, but for now, this.
CloseButtonItem::CloseButtonItem(QObject *parent): DiveButtonItem(parent)
{
static QPixmap p = QPixmap(":trash");
setPixmap(p);
setFlag(ItemIgnoresTransformations);
}
void CloseButtonItem::hide()
{
DiveButtonItem::hide();
}
void CloseButtonItem::show()
{
DiveButtonItem::show();
}
DivePictureItem::DivePictureItem(QObject *parent): DivePixmapItem(parent)
{
setFlag(ItemIgnoresTransformations);
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
setAcceptsHoverEvents(true);
#else
setAcceptHoverEvents(true);
#endif
setScale(0.2);
connect(PreferencesDialog::instance(), SIGNAL(settingsChanged()), this, SLOT(settingsChanged()));
setVisible(prefs.show_pictures_in_profile);
}
void DivePictureItem::settingsChanged()
{
setVisible(prefs.show_pictures_in_profile);
}
void DivePictureItem::setPixmap(const QPixmap &pix)
{
DivePixmapItem::setPixmap(pix);
QRectF r = boundingRect();
QGraphicsRectItem *rect = new QGraphicsRectItem(0 - 10, 0 -10, r.width() + 20, r.height() + 20, this);
rect->setPen(Qt::NoPen);
rect->setBrush(QColor(Qt::white));
rect->setFlag(ItemStacksBehindParent);
rect->setZValue(-1);
QGraphicsRectItem *shadow = new QGraphicsRectItem(rect->boundingRect(), this);
shadow->setPos(5,5);
shadow->setPen(Qt::NoPen);
shadow->setBrush(QColor(Qt::lightGray));
shadow->setFlag(ItemStacksBehindParent);
shadow->setZValue(-2);
}
CloseButtonItem *button = NULL;
void DivePictureItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
Animations::scaleTo(this, 1.0);
setZValue(5);
if(!button) {
button = new CloseButtonItem();
button->setScale(0.2);
button->setZValue(7);
scene()->addItem(button);
}
button->setPos(mapToScene(0,0));
button->setOpacity(0);
button->show();
Animations::show(button);
button->disconnect();
connect(button, SIGNAL(clicked()), this, SLOT(removePicture()));
}
void DivePictureItem::setFileUrl(const QString &s)
{
fileUrl = s;
}
void DivePictureItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
Animations::scaleTo(this, 0.2);
setZValue(0);
if(button)
button->hide();
}
void DivePictureItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QDesktopServices::openUrl(QUrl::fromLocalFile(fileUrl));
}
void DivePictureItem::removePicture()
{
Animations::hide(button);
hide();
DivePictureModel::instance()->removePicture(fileUrl);
}