File indexing completed on 2025-02-16 13:11:48
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2004 Antonio Larrosa <larrosa@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "kpixmapregionselectordialog.h" 0009 0010 #include <QDialogButtonBox> 0011 #include <QGuiApplication> 0012 #include <QImage> 0013 #include <QLabel> 0014 #include <QScreen> 0015 #include <QVBoxLayout> 0016 0017 #include <kpixmapregionselectorwidget.h> 0018 0019 class KPixmapRegionSelectorDialogPrivate 0020 { 0021 public: 0022 KPixmapRegionSelectorDialogPrivate(KPixmapRegionSelectorDialog *parent) 0023 : q(parent) 0024 { 0025 } 0026 0027 KPixmapRegionSelectorWidget *pixmapSelectorWidget = nullptr; 0028 KPixmapRegionSelectorDialog *const q; 0029 0030 void init() 0031 { 0032 //When the image is rotated we need to enforce the maximum width&height into the 0033 //KPixmapRegionSelectorWidget; in order to avoid the dialog to get out of the screen 0034 q->connect(pixmapSelectorWidget, &KPixmapRegionSelectorWidget::pixmapRotated, q, [this]() { 0035 adjustPixmapSize(); 0036 }); 0037 } 0038 0039 void adjustPixmapSize() 0040 { 0041 if (pixmapSelectorWidget) { 0042 // Set maximum size for picture 0043 QScreen *screen = pixmapSelectorWidget->screen(); 0044 if (screen) { 0045 const QRect screenGeometry = screen->availableGeometry(); 0046 pixmapSelectorWidget->setMaximumWidgetSize((int)(screenGeometry.width() * 4.0 / 5), (int)(screenGeometry.height() * 4.0 / 5)); 0047 } 0048 } 0049 } 0050 }; 0051 0052 KPixmapRegionSelectorDialog::KPixmapRegionSelectorDialog(QWidget *parent) 0053 : QDialog(parent) 0054 , d(new KPixmapRegionSelectorDialogPrivate(this)) 0055 { 0056 setWindowTitle(tr("Select Region of Image", "@title:window")); 0057 0058 QVBoxLayout *boxLayout = new QVBoxLayout(this); 0059 0060 QLabel *label = new QLabel(tr("Please click and drag on the image to select the region of interest:", "@label:chooser"), this); 0061 d->pixmapSelectorWidget = new KPixmapRegionSelectorWidget(this); 0062 0063 QDialogButtonBox *buttonBox = new QDialogButtonBox(this); 0064 buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); 0065 connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); 0066 connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0067 0068 boxLayout->addWidget(label); 0069 boxLayout->addWidget(d->pixmapSelectorWidget); 0070 boxLayout->addWidget(buttonBox); 0071 0072 d->init(); 0073 } 0074 0075 KPixmapRegionSelectorDialog::~KPixmapRegionSelectorDialog() = default; 0076 0077 KPixmapRegionSelectorWidget *KPixmapRegionSelectorDialog::pixmapRegionSelectorWidget() const 0078 { 0079 return d->pixmapSelectorWidget; 0080 } 0081 0082 void KPixmapRegionSelectorDialog::adjustRegionSelectorWidgetSizeToFitScreen() 0083 { 0084 d->adjustPixmapSize(); 0085 } 0086 0087 QRect KPixmapRegionSelectorDialog::getSelectedRegion(const QPixmap &pixmap, QWidget *parent) 0088 { 0089 KPixmapRegionSelectorDialog dialog(parent); 0090 0091 dialog.pixmapRegionSelectorWidget()->setPixmap(pixmap); 0092 dialog.adjustRegionSelectorWidgetSizeToFitScreen(); 0093 0094 int result = dialog.exec(); 0095 0096 QRect rect; 0097 0098 if (result == QDialog::Accepted) { 0099 rect = dialog.pixmapRegionSelectorWidget()->unzoomedSelectedRegion(); 0100 } 0101 0102 return rect; 0103 } 0104 0105 QRect KPixmapRegionSelectorDialog::getSelectedRegion(const QPixmap &pixmap, int aspectRatioWidth, int aspectRatioHeight, QWidget *parent) 0106 { 0107 KPixmapRegionSelectorDialog dialog(parent); 0108 0109 dialog.pixmapRegionSelectorWidget()->setPixmap(pixmap); 0110 dialog.pixmapRegionSelectorWidget()->setSelectionAspectRatio(aspectRatioWidth, aspectRatioHeight); 0111 dialog.adjustRegionSelectorWidgetSizeToFitScreen(); 0112 0113 int result = dialog.exec(); 0114 0115 QRect rect; 0116 0117 if (result == QDialog::Accepted) { 0118 rect = dialog.pixmapRegionSelectorWidget()->unzoomedSelectedRegion(); 0119 } 0120 0121 return rect; 0122 } 0123 0124 QImage KPixmapRegionSelectorDialog::getSelectedImage(const QPixmap &pixmap, QWidget *parent) 0125 { 0126 KPixmapRegionSelectorDialog dialog(parent); 0127 0128 dialog.pixmapRegionSelectorWidget()->setPixmap(pixmap); 0129 dialog.adjustRegionSelectorWidgetSizeToFitScreen(); 0130 0131 int result = dialog.exec(); 0132 0133 QImage image; 0134 0135 if (result == QDialog::Accepted) { 0136 image = dialog.pixmapRegionSelectorWidget()->selectedImage(); 0137 } 0138 0139 return image; 0140 } 0141 0142 QImage KPixmapRegionSelectorDialog::getSelectedImage(const QPixmap &pixmap, int aspectRatioWidth, int aspectRatioHeight, QWidget *parent) 0143 { 0144 KPixmapRegionSelectorDialog dialog(parent); 0145 0146 dialog.pixmapRegionSelectorWidget()->setPixmap(pixmap); 0147 dialog.pixmapRegionSelectorWidget()->setSelectionAspectRatio(aspectRatioWidth, aspectRatioHeight); 0148 dialog.adjustRegionSelectorWidgetSizeToFitScreen(); 0149 0150 int result = dialog.exec(); 0151 0152 QImage image; 0153 0154 if (result == QDialog::Accepted) { 0155 image = dialog.pixmapRegionSelectorWidget()->selectedImage(); 0156 } 0157 0158 return image; 0159 } 0160 0161 #include "moc_kpixmapregionselectordialog.cpp"