| 1 | /* |
| 2 | This file is part of the KDE libraries |
| 3 | SPDX-FileCopyrightText: 2004 Antonio Larrosa <larrosa@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "kpixmapregionselectordialog.h" |
| 9 | |
| 10 | #include <QDialogButtonBox> |
| 11 | #include <QGuiApplication> |
| 12 | #include <QImage> |
| 13 | #include <QLabel> |
| 14 | #include <QScreen> |
| 15 | #include <QVBoxLayout> |
| 16 | |
| 17 | #include <kpixmapregionselectorwidget.h> |
| 18 | |
| 19 | class KPixmapRegionSelectorDialogPrivate |
| 20 | { |
| 21 | public: |
| 22 | KPixmapRegionSelectorDialogPrivate(KPixmapRegionSelectorDialog *parent) |
| 23 | : q(parent) |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | KPixmapRegionSelectorWidget *pixmapSelectorWidget = nullptr; |
| 28 | KPixmapRegionSelectorDialog *const q; |
| 29 | |
| 30 | void init() |
| 31 | { |
| 32 | // When the image is rotated we need to enforce the maximum width&height into the |
| 33 | // KPixmapRegionSelectorWidget; in order to avoid the dialog to get out of the screen |
| 34 | q->connect(sender: pixmapSelectorWidget, signal: &KPixmapRegionSelectorWidget::pixmapRotated, context: q, slot: [this]() { |
| 35 | adjustPixmapSize(); |
| 36 | }); |
| 37 | } |
| 38 | |
| 39 | void adjustPixmapSize() |
| 40 | { |
| 41 | if (pixmapSelectorWidget) { |
| 42 | // Set maximum size for picture |
| 43 | QScreen *screen = pixmapSelectorWidget->screen(); |
| 44 | if (screen) { |
| 45 | const QRect screenGeometry = screen->availableGeometry(); |
| 46 | pixmapSelectorWidget->setMaximumWidgetSize(width: (int)(screenGeometry.width() * 4.0 / 5), height: (int)(screenGeometry.height() * 4.0 / 5)); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | KPixmapRegionSelectorDialog::KPixmapRegionSelectorDialog(QWidget *parent) |
| 53 | : QDialog(parent) |
| 54 | , d(new KPixmapRegionSelectorDialogPrivate(this)) |
| 55 | { |
| 56 | setWindowTitle(tr(s: "Select Region of Image" , c: "@title:window" )); |
| 57 | |
| 58 | QVBoxLayout *boxLayout = new QVBoxLayout(this); |
| 59 | |
| 60 | QLabel *label = new QLabel(tr(s: "Please click and drag on the image to select the region of interest:" , c: "@label:chooser" ), this); |
| 61 | d->pixmapSelectorWidget = new KPixmapRegionSelectorWidget(this); |
| 62 | |
| 63 | QDialogButtonBox *buttonBox = new QDialogButtonBox(this); |
| 64 | buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); |
| 65 | connect(sender: buttonBox, signal: &QDialogButtonBox::accepted, context: this, slot: &QDialog::accept); |
| 66 | connect(sender: buttonBox, signal: &QDialogButtonBox::rejected, context: this, slot: &QDialog::reject); |
| 67 | |
| 68 | boxLayout->addWidget(label); |
| 69 | boxLayout->addWidget(d->pixmapSelectorWidget); |
| 70 | boxLayout->addWidget(buttonBox); |
| 71 | |
| 72 | d->init(); |
| 73 | } |
| 74 | |
| 75 | KPixmapRegionSelectorDialog::~KPixmapRegionSelectorDialog() = default; |
| 76 | |
| 77 | KPixmapRegionSelectorWidget *KPixmapRegionSelectorDialog::pixmapRegionSelectorWidget() const |
| 78 | { |
| 79 | return d->pixmapSelectorWidget; |
| 80 | } |
| 81 | |
| 82 | void KPixmapRegionSelectorDialog::adjustRegionSelectorWidgetSizeToFitScreen() |
| 83 | { |
| 84 | d->adjustPixmapSize(); |
| 85 | } |
| 86 | |
| 87 | QRect KPixmapRegionSelectorDialog::getSelectedRegion(const QPixmap &pixmap, QWidget *parent) |
| 88 | { |
| 89 | KPixmapRegionSelectorDialog dialog(parent); |
| 90 | |
| 91 | dialog.pixmapRegionSelectorWidget()->setPixmap(pixmap); |
| 92 | dialog.adjustRegionSelectorWidgetSizeToFitScreen(); |
| 93 | |
| 94 | int result = dialog.exec(); |
| 95 | |
| 96 | QRect rect; |
| 97 | |
| 98 | if (result == QDialog::Accepted) { |
| 99 | rect = dialog.pixmapRegionSelectorWidget()->unzoomedSelectedRegion(); |
| 100 | } |
| 101 | |
| 102 | return rect; |
| 103 | } |
| 104 | |
| 105 | QRect KPixmapRegionSelectorDialog::getSelectedRegion(const QPixmap &pixmap, int aspectRatioWidth, int aspectRatioHeight, QWidget *parent) |
| 106 | { |
| 107 | KPixmapRegionSelectorDialog dialog(parent); |
| 108 | |
| 109 | dialog.pixmapRegionSelectorWidget()->setPixmap(pixmap); |
| 110 | dialog.pixmapRegionSelectorWidget()->setSelectionAspectRatio(width: aspectRatioWidth, height: aspectRatioHeight); |
| 111 | dialog.adjustRegionSelectorWidgetSizeToFitScreen(); |
| 112 | |
| 113 | int result = dialog.exec(); |
| 114 | |
| 115 | QRect rect; |
| 116 | |
| 117 | if (result == QDialog::Accepted) { |
| 118 | rect = dialog.pixmapRegionSelectorWidget()->unzoomedSelectedRegion(); |
| 119 | } |
| 120 | |
| 121 | return rect; |
| 122 | } |
| 123 | |
| 124 | QImage KPixmapRegionSelectorDialog::getSelectedImage(const QPixmap &pixmap, QWidget *parent) |
| 125 | { |
| 126 | KPixmapRegionSelectorDialog dialog(parent); |
| 127 | |
| 128 | dialog.pixmapRegionSelectorWidget()->setPixmap(pixmap); |
| 129 | dialog.adjustRegionSelectorWidgetSizeToFitScreen(); |
| 130 | |
| 131 | int result = dialog.exec(); |
| 132 | |
| 133 | QImage image; |
| 134 | |
| 135 | if (result == QDialog::Accepted) { |
| 136 | image = dialog.pixmapRegionSelectorWidget()->selectedImage(); |
| 137 | } |
| 138 | |
| 139 | return image; |
| 140 | } |
| 141 | |
| 142 | QImage KPixmapRegionSelectorDialog::getSelectedImage(const QPixmap &pixmap, int aspectRatioWidth, int aspectRatioHeight, QWidget *parent) |
| 143 | { |
| 144 | KPixmapRegionSelectorDialog dialog(parent); |
| 145 | |
| 146 | dialog.pixmapRegionSelectorWidget()->setPixmap(pixmap); |
| 147 | dialog.pixmapRegionSelectorWidget()->setSelectionAspectRatio(width: aspectRatioWidth, height: aspectRatioHeight); |
| 148 | dialog.adjustRegionSelectorWidgetSizeToFitScreen(); |
| 149 | |
| 150 | int result = dialog.exec(); |
| 151 | |
| 152 | QImage image; |
| 153 | |
| 154 | if (result == QDialog::Accepted) { |
| 155 | image = dialog.pixmapRegionSelectorWidget()->selectedImage(); |
| 156 | } |
| 157 | |
| 158 | return image; |
| 159 | } |
| 160 | |
| 161 | #include "moc_kpixmapregionselectordialog.cpp" |
| 162 | |