2 changed files with 74 additions and 0 deletions
@ -0,0 +1,69 @@ |
|||||||
|
#include "avatargallery.h" |
||||||
|
#include "avatarmanager.h" |
||||||
|
#include "../imageviewer_window.h" |
||||||
|
#include <QDialog> |
||||||
|
#include <QHBoxLayout> |
||||||
|
#include <QLabel> |
||||||
|
#include <QPointer> |
||||||
|
#include <QPushButton> |
||||||
|
#include <QShortcut> |
||||||
|
#include <QVBoxLayout> |
||||||
|
|
||||||
|
namespace { |
||||||
|
class Gallery : public QDialog { |
||||||
|
public: |
||||||
|
explicit Gallery(AvatarManager* source) : manager(source) { |
||||||
|
setAttribute(Qt::WA_DeleteOnClose); resize(850,720); setWindowTitle(tr("Avatars")); |
||||||
|
auto* layout = new QVBoxLayout(this); viewer = new ImageViewerWindow(this); layout->addWidget(viewer,1); |
||||||
|
status = new QLabel(this); status->setAlignment(Qt::AlignCenter); layout->addWidget(status); |
||||||
|
auto* strip = new QHBoxLayout; strip->addStretch(); previous = new QPushButton("←",this); strip->addWidget(previous); |
||||||
|
for (int i=0;i<5;i++) { |
||||||
|
previews[i] = new QPushButton(this); previews[i]->setFixedSize(64,64); previews[i]->setIconSize({56,56}); |
||||||
|
if (i==2) previews[i]->setStyleSheet("border: 2px solid #77aaff;"); |
||||||
|
strip->addWidget(previews[i]); connect(previews[i], &QPushButton::clicked, this, [this,i] { move(i-2); }); |
||||||
|
} |
||||||
|
next = new QPushButton("→",this); strip->addWidget(next); strip->addStretch(); layout->addLayout(strip); |
||||||
|
connect(previous, &QPushButton::clicked, this, [this] { move(-1); }); |
||||||
|
connect(next, &QPushButton::clicked, this, [this] { move(1); }); |
||||||
|
connect(new QShortcut(QKeySequence(Qt::Key_Left),this), &QShortcut::activated, this, [this] { move(-1); }); |
||||||
|
connect(new QShortcut(QKeySequence(Qt::Key_Right),this), &QShortcut::activated, this, [this] { move(1); }); |
||||||
|
connect(viewer, &ImageViewerWindow::closed, this, &QDialog::close); |
||||||
|
connect(manager, &AvatarManager::galleryChanged, this, [this] { refresh(); }); |
||||||
|
connect(manager, &AvatarManager::galleryError, status, &QLabel::setText); |
||||||
|
connect(manager, &QObject::destroyed, this, &QDialog::close); |
||||||
|
auto* timer = new QTimer(this); timer->setInterval(2000); connect(timer,&QTimer::timeout,this,[this] { refresh(); }); timer->start(); |
||||||
|
} |
||||||
|
~Gallery() override { if (manager) manager->closeGallery(); } |
||||||
|
void move(int delta) { |
||||||
|
if (!manager) return; |
||||||
|
const auto files=manager->gallery(); const int index=files.indexOf(selected)+delta; |
||||||
|
if (index<0 || index>=files.size()) return; |
||||||
|
selected=files[index]; displayed=0; refresh(); |
||||||
|
} |
||||||
|
void refresh() { |
||||||
|
if (!manager) return; |
||||||
|
const auto files=manager->gallery(); if (selected.isEmpty()) selected=manager->galleryCurrent(); |
||||||
|
const int index=files.indexOf(selected); previous->setEnabled(index>0); next->setEnabled(index>=0 && index+1<files.size()); |
||||||
|
if (index<0) return; |
||||||
|
QImage image=manager->galleryImage(selected,true); |
||||||
|
const bool full=!image.isNull(); if (!full) image=manager->galleryImage(selected); |
||||||
|
if (image.cacheKey()!=displayed) { viewer->loadImage(image); displayed=image.cacheKey(); } |
||||||
|
if (full) status->setText(QString("%1 / %2").arg(index+1).arg(files.size())); |
||||||
|
else if (!image.isNull()) status->setText(tr("Loading original…")); |
||||||
|
for (int i=0;i<5;i++) { |
||||||
|
const int item=index+i-2; const bool valid=item>=0 && item<files.size(); previews[i]->setEnabled(valid); |
||||||
|
previews[i]->setIcon(valid ? QIcon(QPixmap::fromImage(manager->galleryImage(files[item]))) : QIcon()); |
||||||
|
} |
||||||
|
} |
||||||
|
private: |
||||||
|
QPointer<AvatarManager> manager; ImageViewerWindow* viewer; QLabel* status; |
||||||
|
QPushButton *previews[5], *previous, *next; QString selected; qint64 displayed=0; |
||||||
|
}; |
||||||
|
QPointer<Gallery> active; |
||||||
|
} |
||||||
|
|
||||||
|
void showAvatarGallery(const QString& channel, quint64 owner) { |
||||||
|
auto* manager=AvatarManager::instance(); if (!manager) return; |
||||||
|
if (active) { delete active.data(); active=nullptr; } |
||||||
|
active=new Gallery(manager); active->show(); manager->openGallery(channel,owner); |
||||||
|
} |
||||||
Loading…
Reference in new issue