summaryrefslogtreecommitdiffstats
path: root/qt-ui/profile/divepixmapitem.cpp
blob: 3fb06adb9f7176c3070af1da9f9e2830bf6914db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#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->show();
	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()
{
	/* this is a WIP, it doesn't really *removes* anything, merely hides it.
	 * good workaround, I still need to figure out how to activelly remove
	 * it from the model. */
	button->hide();
	hide();
}