| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #include "ImagePreviewDialog.h"
- #include "TitleBar.h"
- #include "UiHelpers.h"
- #include <QFile>
- #include <QFileDialog>
- #include <QFrame>
- #include <QIODevice>
- #include <QHBoxLayout>
- #include <QLabel>
- #include <QPushButton>
- #include <QScrollArea>
- #include <QVBoxLayout>
- ImagePreviewDialog::ImagePreviewDialog(const QPixmap &image, const QString &name,
- const QByteArray &bytes, QWidget *parent, bool canForward,
- bool canFavorite)
- : QDialog(parent)
- {
- setObjectName(QStringLiteral("ChromeWindow"));
- setWindowTitle(name.isEmpty() ? QStringLiteral("图片") : name);
- UiHelpers::setupFramelessDialog(this);
- const qreal dpr = image.devicePixelRatio() > 0 ? image.devicePixelRatio() : 1.0;
- const int iw = qMax(1, qRound(image.width() / dpr));
- const int ih = qMax(1, qRound(image.height() / dpr));
- const int maxW = 560;
- const int maxH = 480;
- QSize shown(iw, ih);
- shown.scale(maxW, maxH, Qt::KeepAspectRatio);
- const int winW = qBound(320, shown.width() + 24, 600);
- const int winH = qBound(280, shown.height() + 108, 620);
- setFixedSize(winW, winH);
- auto *root = new QVBoxLayout(this);
- root->setContentsMargins(0, 0, 0, 0);
- root->setSpacing(0);
- auto *bar = new TitleBar(this);
- bar->setTitle(name.isEmpty() ? QStringLiteral("图片") : name);
- bar->setTheme(TitleBar::Light);
- bar->setMaximizable(false);
- bar->bind(this, false);
- root->addWidget(bar);
- auto *area = new QScrollArea(this);
- area->setWidgetResizable(true);
- area->setFrameShape(QFrame::NoFrame);
- area->setStyleSheet(QStringLiteral("background:#1A1A1A; border:none;"));
- auto *img = new QLabel(area);
- img->setAlignment(Qt::AlignCenter);
- img->setStyleSheet(QStringLiteral("background:transparent;"));
- const QPixmap fit = image.scaled(qRound(shown.width() * dpr), qRound(shown.height() * dpr),
- Qt::KeepAspectRatio, Qt::SmoothTransformation);
- QPixmap display = fit;
- display.setDevicePixelRatio(dpr);
- img->setPixmap(display);
- area->setWidget(img);
- root->addWidget(area, 1);
- auto *btns = new QWidget(this);
- btns->setFixedHeight(48);
- btns->setStyleSheet(QStringLiteral("background:#FFFFFF; border-top:1px solid #E5E5E5;"));
- auto *bLay = new QHBoxLayout(btns);
- bLay->setContentsMargins(16, 8, 16, 8);
- auto *save = UiHelpers::ghostButton(QStringLiteral("保存"), btns);
- save->setFixedSize(88, 32);
- auto *fwd = UiHelpers::ghostButton(QStringLiteral("转发"), btns);
- fwd->setFixedSize(88, 32);
- fwd->setVisible(canForward);
- auto *fav = UiHelpers::ghostButton(QStringLiteral("收藏"), btns);
- fav->setFixedSize(88, 32);
- fav->setVisible(canFavorite);
- auto *ok = UiHelpers::primaryButton(QStringLiteral("关闭"), btns);
- ok->setFixedSize(88, 32);
- bLay->addWidget(save);
- if (canForward)
- bLay->addWidget(fwd);
- if (canFavorite)
- bLay->addWidget(fav);
- bLay->addStretch();
- bLay->addWidget(ok);
- root->addWidget(btns);
- connect(ok, &QPushButton::clicked, this, &QDialog::accept);
- connect(fwd, &QPushButton::clicked, this, [this]() {
- m_forward = true;
- accept();
- });
- connect(fav, &QPushButton::clicked, this, [this]() {
- m_favorite = true;
- accept();
- });
- connect(save, &QPushButton::clicked, this, [this, name, bytes]() {
- const QString path = QFileDialog::getSaveFileName(
- this, QStringLiteral("保存图片"), name.isEmpty() ? QStringLiteral("image.jpg") : name);
- if (path.isEmpty())
- return;
- QFile f(path);
- if (!f.open(QIODevice::WriteOnly) || f.write(bytes) != bytes.size())
- {
- UiHelpers::showToast(this, QStringLiteral("保存失败"), true);
- return;
- }
- UiHelpers::showToast(this, QStringLiteral("已保存"));
- });
- }
|