This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thumbnails_container.cpp
120 lines (102 loc) · 3.19 KB
/
thumbnails_container.cpp
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
#include "thumbnails_container.h"
#include "thumbnail_loader.h"
#include <QDebug>
#include <QSizePolicy>
#include <QWheelEvent>
#include <QScrollBar>
using namespace qtuimage;
QSharedPointer<QIcon> ThumbnailButton::defaultIcon = nullptr;
const QIcon &ThumbnailButton::getDefaultIcon()
{
if (!defaultIcon)
{
auto *pixmap = new QPixmap(ThumbnailLoader::thumbnailSize, ThumbnailLoader::thumbnailSize);
pixmap->fill(Qt::gray);
defaultIcon = QSharedPointer<QIcon>(new QIcon(*pixmap));
delete pixmap;
}
return *defaultIcon;
}
ThumbnailButton::ThumbnailButton(QWidget *parent, const QString &_path, QSharedPointer<QPixmap> pixmap)
: QPushButton(parent),
path(_path)
{
if (pixmap)
setIcon(QIcon(*pixmap));
else
setIcon(getDefaultIcon());
setStyleSheet("border-radius: 0px; border: none;");
setIconSize(QSize(ThumbnailLoader::thumbnailSize, ThumbnailLoader::thumbnailSize));
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
connect(this, &QPushButton::pressed, this, &ThumbnailButton::onPressed);
}
void ThumbnailButton::onPressed()
{
emit selected(path.toString());
}
QSize ThumbnailButton::sizeHint(void) const
{
return QSize(ThumbnailLoader::thumbnailSize, ThumbnailLoader::thumbnailSize);
}
ThumbnailsContainer::ThumbnailsContainer(QWidget *parent, QStringList &_paths)
: QScrollArea(parent),
paths(_paths),
layout(new QHBoxLayout(this))
{
setVisible(false);
QWidget *const w = new QWidget(this);
layout->setContentsMargins(0, 0, 0, 0);
w->setLayout(layout);
setWidgetResizable(true);
setWidget(w);
setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setBackgroundRole(QPalette::Dark);
}
QSize ThumbnailsContainer::sizeHint(void) const
{
return QSize(containerCellWidth * paths.size(), containerHeight);
}
void ThumbnailsContainer::wheelEvent(QWheelEvent *event)
{
auto *const hsb = horizontalScrollBar();
hsb->setSliderPosition(hsb->sliderPosition() - event->angleDelta().y() / 4);
emit scrolled(hsb->sliderPosition()/(64+layout->spacing()));
}
void ThumbnailsContainer::addThumbnail(const QString &path, QSharedPointer<QPixmap> pixmap)
{
auto *b = new ThumbnailButton(this, path, pixmap);
buttons[path] = b;
layout->addWidget(b);
connect(b, &ThumbnailButton::selected, this, &ThumbnailsContainer::onSelected);
}
void ThumbnailsContainer::setIcon(const QString &path, QSharedPointer<QPixmap> pixmap)
{
buttons[path]->setIcon(QIcon(*pixmap));
}
void ThumbnailsContainer::onSelected(const QString &path)
{
emit selected(path);
}
void ThumbnailsContainer::onPathsChanged()
{
for (const auto &newKey : paths)
{
if (buttons.count(newKey) == 0)
addThumbnail(newKey);
}
QStringList removed;
for (auto old = buttons.begin(); old != buttons.end(); old++)
{
const auto &oldKey = old.key();
if (paths.count(oldKey) == 0)
removed.push_back(oldKey);
}
for (const auto &removedKey : removed)
{
auto *b = buttons[removedKey];
layout->removeWidget(b);
buttons.remove(removedKey);
delete b;
}
}