KWidgetsAddons

kpixmapregionselectordialog.cpp
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
19class KPixmapRegionSelectorDialogPrivate
20{
21public:
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(pixmapSelectorWidget, &KPixmapRegionSelectorWidget::pixmapRotated, q, [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((int)(screenGeometry.width() * 4.0 / 5), (int)(screenGeometry.height() * 4.0 / 5));
47 }
48 }
49 }
50};
51
54 , d(new KPixmapRegionSelectorDialogPrivate(this))
55{
56 setWindowTitle(tr("Select Region of Image", "@title:window"));
57
58 QVBoxLayout *boxLayout = new QVBoxLayout(this);
59
60 QLabel *label = new QLabel(tr("Please click and drag on the image to select the region of interest:", "@label:chooser"), this);
61 d->pixmapSelectorWidget = new KPixmapRegionSelectorWidget(this);
62
63 QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
67
68 boxLayout->addWidget(label);
69 boxLayout->addWidget(d->pixmapSelectorWidget);
70 boxLayout->addWidget(buttonBox);
71
72 d->init();
73}
74
76
81
86
104
105QRect KPixmapRegionSelectorDialog::getSelectedRegion(const QPixmap &pixmap, int aspectRatioWidth, int aspectRatioHeight, QWidget *parent)
106{
108
109 dialog.pixmapRegionSelectorWidget()->setPixmap(pixmap);
110 dialog.pixmapRegionSelectorWidget()->setSelectionAspectRatio(aspectRatioWidth, aspectRatioHeight);
112
113 int result = dialog.exec();
114
115 QRect rect;
116
117 if (result == QDialog::Accepted) {
119 }
120
121 return rect;
122}
123
125{
127
128 dialog.pixmapRegionSelectorWidget()->setPixmap(pixmap);
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
142QImage KPixmapRegionSelectorDialog::getSelectedImage(const QPixmap &pixmap, int aspectRatioWidth, int aspectRatioHeight, QWidget *parent)
143{
145
146 dialog.pixmapRegionSelectorWidget()->setPixmap(pixmap);
147 dialog.pixmapRegionSelectorWidget()->setSelectionAspectRatio(aspectRatioWidth, aspectRatioHeight);
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"
KPixmapRegionSelectorDialog(QWidget *parent=nullptr)
The constructor of an empty KPixmapRegionSelectorDialog, you have to call later the setPixmap method ...
static QImage getSelectedImage(const QPixmap &pixmap, QWidget *parent=nullptr)
Creates a modal dialog, lets the user to select a region of the pixmap and returns when the dialog is...
~KPixmapRegionSelectorDialog() override
The destructor of the dialog.
static QRect getSelectedRegion(const QPixmap &pixmap, QWidget *parent=nullptr)
Creates a modal dialog, lets the user to select a region of the pixmap and returns when the dialog is...
KPixmapRegionSelectorWidget * pixmapRegionSelectorWidget() const
KPixmapRegionSelectorWidget is a widget that shows a picture and provides the user with a friendly wa...
QRect unzoomedSelectedRegion() const
Returns the selected region ( in unzoomed, original pixmap coordinates )
void setPixmap(const QPixmap &pixmap)
Sets the pixmap which will be shown for the user to select a region from.
void setSelectionAspectRatio(int width, int height)
Sets the aspect ration that the selected subimage should have.
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
QDialog(QWidget *parent, Qt::WindowFlags f)
virtual void accept()
virtual int exec()
virtual void reject()
int result() const const
void setStandardButtons(StandardButtons buttons)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
int height() const const
int width() const const
QWidget(QWidget *parent, Qt::WindowFlags f)
void setWindowTitle(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 28 2025 12:02:04 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.