File: kpixmapregionselectordialog.cpp

package info (click to toggle)
kwidgetsaddons 5.28.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 62,388 kB
  • ctags: 3,611
  • sloc: cpp: 26,647; python: 682; sh: 23; makefile: 6
file content (172 lines) | stat: -rw-r--r-- 5,357 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* This file is part of the KDE libraries
    Copyright (C) 2004 Antonio Larrosa <larrosa@kde.org

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "kpixmapregionselectordialog.h"

#include <QDialogButtonBox>
#include <QDesktopWidget>
#include <QImage>
#include <QLabel>
#include <QVBoxLayout>

#include <kpixmapregionselectorwidget.h>

class KPixmapRegionSelectorDialog::Private
{
public:
    Private(KPixmapRegionSelectorDialog *parent)
        : pixmapSelectorWidget(0), q(parent)
    {
    }

    KPixmapRegionSelectorWidget *pixmapSelectorWidget;
    KPixmapRegionSelectorDialog *q;

    void init()
    {
        //When the image is rotated we need to enforce the maximum width&height into the
        //KPixmapRegionSelectorWidget; in order to avoid the dialog to get out of the screen
        q->connect(pixmapSelectorWidget, SIGNAL(pixmapRotated()), q, SLOT(_k_adjustPixmapSize()));
    }

    void _k_adjustPixmapSize()
    {
        if (pixmapSelectorWidget) {
            //Set maximum size for picture
            QDesktopWidget desktopWidget;
            QRect screen = desktopWidget.availableGeometry();
            pixmapSelectorWidget->setMaximumWidgetSize(
                (int)(screen.width() * 4.0 / 5), (int)(screen.height() * 4.0 / 5));
        }
    }
};

KPixmapRegionSelectorDialog::KPixmapRegionSelectorDialog(QWidget *parent)
    : QDialog(parent),
      d(new Private(this))
{
    setWindowTitle(tr("Select Region of Image"));

    QVBoxLayout *boxLayout = new QVBoxLayout(this);

    QLabel *label = new QLabel(tr("Please click and drag on the image to select the region of interest:"), this);
    d->pixmapSelectorWidget = new KPixmapRegionSelectorWidget(this);

    QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
    buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);

    boxLayout->addWidget(label);
    boxLayout->addWidget(d->pixmapSelectorWidget);
    boxLayout->addWidget(buttonBox);

    d->init();
}

KPixmapRegionSelectorDialog::~KPixmapRegionSelectorDialog()
{
    delete d;
}

KPixmapRegionSelectorWidget *KPixmapRegionSelectorDialog::pixmapRegionSelectorWidget() const
{
    return d->pixmapSelectorWidget;
}

void KPixmapRegionSelectorDialog::adjustRegionSelectorWidgetSizeToFitScreen()
{
    d->_k_adjustPixmapSize();
}

QRect KPixmapRegionSelectorDialog::getSelectedRegion(const QPixmap &pixmap, QWidget *parent)
{
    KPixmapRegionSelectorDialog dialog(parent);

    dialog.pixmapRegionSelectorWidget()->setPixmap(pixmap);
    dialog.adjustRegionSelectorWidgetSizeToFitScreen();

    int result = dialog.exec();

    QRect rect;

    if (result == QDialog::Accepted) {
        rect = dialog.pixmapRegionSelectorWidget()->unzoomedSelectedRegion();
    }

    return rect;
}

QRect KPixmapRegionSelectorDialog::getSelectedRegion(const QPixmap &pixmap, int aspectRatioWidth, int aspectRatioHeight, QWidget *parent)
{
    KPixmapRegionSelectorDialog dialog(parent);

    dialog.pixmapRegionSelectorWidget()->setPixmap(pixmap);
    dialog.pixmapRegionSelectorWidget()->setSelectionAspectRatio(aspectRatioWidth, aspectRatioHeight);
    dialog.adjustRegionSelectorWidgetSizeToFitScreen();

    int result = dialog.exec();

    QRect rect;

    if (result == QDialog::Accepted) {
        rect = dialog.pixmapRegionSelectorWidget()->unzoomedSelectedRegion();
    }

    return rect;
}

QImage KPixmapRegionSelectorDialog::getSelectedImage(const QPixmap &pixmap, QWidget *parent)
{
    KPixmapRegionSelectorDialog dialog(parent);

    dialog.pixmapRegionSelectorWidget()->setPixmap(pixmap);
    dialog.adjustRegionSelectorWidgetSizeToFitScreen();

    int result = dialog.exec();

    QImage image;

    if (result == QDialog::Accepted) {
        image = dialog.pixmapRegionSelectorWidget()->selectedImage();
    }

    return image;
}

QImage KPixmapRegionSelectorDialog::getSelectedImage(const QPixmap &pixmap, int aspectRatioWidth, int aspectRatioHeight, QWidget *parent)
{
    KPixmapRegionSelectorDialog dialog(parent);

    dialog.pixmapRegionSelectorWidget()->setPixmap(pixmap);
    dialog.pixmapRegionSelectorWidget()->setSelectionAspectRatio(aspectRatioWidth, aspectRatioHeight);
    dialog.adjustRegionSelectorWidgetSizeToFitScreen();

    int result = dialog.exec();

    QImage image;

    if (result == QDialog::Accepted) {
        image = dialog.pixmapRegionSelectorWidget()->selectedImage();
    }

    return image;
}

#include "moc_kpixmapregionselectordialog.cpp"