ImagePreviewDialog.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "ImagePreviewDialog.h"
  2. #include "TitleBar.h"
  3. #include "UiHelpers.h"
  4. #include <QFile>
  5. #include <QFileDialog>
  6. #include <QFrame>
  7. #include <QIODevice>
  8. #include <QHBoxLayout>
  9. #include <QLabel>
  10. #include <QPushButton>
  11. #include <QScrollArea>
  12. #include <QVBoxLayout>
  13. ImagePreviewDialog::ImagePreviewDialog(const QPixmap &image, const QString &name,
  14. const QByteArray &bytes, QWidget *parent, bool canForward,
  15. bool canFavorite)
  16. : QDialog(parent)
  17. {
  18. setObjectName(QStringLiteral("ChromeWindow"));
  19. setWindowTitle(name.isEmpty() ? QStringLiteral("图片") : name);
  20. UiHelpers::setupFramelessDialog(this);
  21. const qreal dpr = image.devicePixelRatio() > 0 ? image.devicePixelRatio() : 1.0;
  22. const int iw = qMax(1, qRound(image.width() / dpr));
  23. const int ih = qMax(1, qRound(image.height() / dpr));
  24. const int maxW = 560;
  25. const int maxH = 480;
  26. QSize shown(iw, ih);
  27. shown.scale(maxW, maxH, Qt::KeepAspectRatio);
  28. const int winW = qBound(320, shown.width() + 24, 600);
  29. const int winH = qBound(280, shown.height() + 108, 620);
  30. setFixedSize(winW, winH);
  31. auto *root = new QVBoxLayout(this);
  32. root->setContentsMargins(0, 0, 0, 0);
  33. root->setSpacing(0);
  34. auto *bar = new TitleBar(this);
  35. bar->setTitle(name.isEmpty() ? QStringLiteral("图片") : name);
  36. bar->setTheme(TitleBar::Light);
  37. bar->setMaximizable(false);
  38. bar->bind(this, false);
  39. root->addWidget(bar);
  40. auto *area = new QScrollArea(this);
  41. area->setWidgetResizable(true);
  42. area->setFrameShape(QFrame::NoFrame);
  43. area->setStyleSheet(QStringLiteral("background:#1A1A1A; border:none;"));
  44. auto *img = new QLabel(area);
  45. img->setAlignment(Qt::AlignCenter);
  46. img->setStyleSheet(QStringLiteral("background:transparent;"));
  47. const QPixmap fit = image.scaled(qRound(shown.width() * dpr), qRound(shown.height() * dpr),
  48. Qt::KeepAspectRatio, Qt::SmoothTransformation);
  49. QPixmap display = fit;
  50. display.setDevicePixelRatio(dpr);
  51. img->setPixmap(display);
  52. area->setWidget(img);
  53. root->addWidget(area, 1);
  54. auto *btns = new QWidget(this);
  55. btns->setFixedHeight(48);
  56. btns->setStyleSheet(QStringLiteral("background:#FFFFFF; border-top:1px solid #E5E5E5;"));
  57. auto *bLay = new QHBoxLayout(btns);
  58. bLay->setContentsMargins(16, 8, 16, 8);
  59. auto *save = UiHelpers::ghostButton(QStringLiteral("保存"), btns);
  60. save->setFixedSize(88, 32);
  61. auto *fwd = UiHelpers::ghostButton(QStringLiteral("转发"), btns);
  62. fwd->setFixedSize(88, 32);
  63. fwd->setVisible(canForward);
  64. auto *fav = UiHelpers::ghostButton(QStringLiteral("收藏"), btns);
  65. fav->setFixedSize(88, 32);
  66. fav->setVisible(canFavorite);
  67. auto *ok = UiHelpers::primaryButton(QStringLiteral("关闭"), btns);
  68. ok->setFixedSize(88, 32);
  69. bLay->addWidget(save);
  70. if (canForward)
  71. bLay->addWidget(fwd);
  72. if (canFavorite)
  73. bLay->addWidget(fav);
  74. bLay->addStretch();
  75. bLay->addWidget(ok);
  76. root->addWidget(btns);
  77. connect(ok, &QPushButton::clicked, this, &QDialog::accept);
  78. connect(fwd, &QPushButton::clicked, this, [this]() {
  79. m_forward = true;
  80. accept();
  81. });
  82. connect(fav, &QPushButton::clicked, this, [this]() {
  83. m_favorite = true;
  84. accept();
  85. });
  86. connect(save, &QPushButton::clicked, this, [this, name, bytes]() {
  87. const QString path = QFileDialog::getSaveFileName(
  88. this, QStringLiteral("保存图片"), name.isEmpty() ? QStringLiteral("image.jpg") : name);
  89. if (path.isEmpty())
  90. return;
  91. QFile f(path);
  92. if (!f.open(QIODevice::WriteOnly) || f.write(bytes) != bytes.size())
  93. {
  94. UiHelpers::showToast(this, QStringLiteral("保存失败"), true);
  95. return;
  96. }
  97. UiHelpers::showToast(this, QStringLiteral("已保存"));
  98. });
  99. }