KPimTextEdit

insertimagewidget.cpp
1/*
2 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5
6*/
7#include "insertimagewidget.h"
8
9#include <KLineEdit>
10#include <KLocalizedString>
11#include <KSeparator>
12#include <KUrlRequester>
13
14#include <QCheckBox>
15#include <QHBoxLayout>
16#include <QImageReader>
17#include <QLabel>
18#include <QMimeDatabase>
19#include <QSpinBox>
20#include <QVBoxLayout>
21
22namespace KPIMTextEdit
23{
24static inline QString resolveAlias(const QString &name)
25{
27 const QString str = db.mimeTypeForName(name).name();
28 return str;
29}
30
31class InsertImageWidgetPrivate
32{
33public:
34 explicit InsertImageWidgetPrivate(InsertImageWidget *qq)
35 : q(qq)
36 {
37 auto lay = new QVBoxLayout(q);
38 lay->setContentsMargins({});
39 auto hbox = new QHBoxLayout;
40 auto lab = new QLabel(i18nc("@label:textbox", "Image Location:"));
41 imageUrlRequester = new KUrlRequester;
42
43 QStringList lstMimeTypes;
45 lstMimeTypes.reserve(mimetypes.count());
46 for (const QByteArray &ba : mimetypes) {
47 const QString resolvedAlias = resolveAlias(QString::fromUtf8(ba));
48 if (!resolvedAlias.isEmpty()) {
49 lstMimeTypes << resolvedAlias;
50 }
51 }
52 imageUrlRequester->setMimeTypeFilters(lstMimeTypes);
53 imageUrlRequester->setWindowTitle(i18nc("@title:window", "Add Image"));
54 imageUrlRequester->setMode(KFile::File);
55 q->connect(imageUrlRequester->lineEdit(), &KLineEdit::textChanged, q, [this](const QString &str) {
56 _k_slotUrlChanged(str);
57 });
58
59 hbox->addWidget(lab);
60 hbox->addWidget(imageUrlRequester);
61 lab->setBuddy(imageUrlRequester);
62 lay->addLayout(hbox);
63
64 keepOriginalSize = new QCheckBox(i18nc("@option:check", "Keep Original Size"));
65 q->connect(keepOriginalSize, &QCheckBox::clicked, q, [this](bool b) {
66 _k_slotKeepOriginalSizeClicked(b);
67 });
68 keepOriginalSize->setChecked(true);
69 lay->addWidget(keepOriginalSize);
70
71 keepImageRatio = new QCheckBox(i18nc("@option:check", "Keep Image Ratio"));
72 keepImageRatio->setChecked(true);
73 keepImageRatio->setEnabled(false);
74 lay->addWidget(keepImageRatio);
75
76 hbox = new QHBoxLayout;
77 lab = new QLabel(i18nc("@label:textbox", "Width:"));
78 width = new QSpinBox;
79 width->setMinimum(1);
80 width->setMaximum(99999);
81 width->setEnabled(false);
82 width->setSuffix(i18n(" px"));
83 lab->setBuddy(width);
84 q->connect(width, &QSpinBox::valueChanged, q, [this](int val) {
85 _k_slotImageWidthChanged(val);
86 });
87 hbox->addWidget(lab);
88 hbox->addWidget(width);
89 lay->addLayout(hbox);
90
91 hbox = new QHBoxLayout;
92 lab = new QLabel(i18nc("@label:textbox", "Height:"));
93 height = new QSpinBox;
94 height->setMinimum(1);
95 height->setMaximum(99999);
96 height->setEnabled(false);
97 height->setSuffix(i18n(" px"));
98 lab->setBuddy(height);
99 q->connect(height, &QSpinBox::valueChanged, q, [this](int val) {
100 _k_slotImageHeightChanged(val);
101 });
102 hbox->addWidget(lab);
103 hbox->addWidget(height);
104 lay->addLayout(hbox);
105
106 auto sep = new KSeparator;
107 lay->addWidget(sep);
108
109 hbox = new QHBoxLayout;
110 lab = new QLabel(i18nc("@label:textbox", "Image Preview:"));
111 hbox->addWidget(lab);
112 preview = new QLabel;
113 preview->setFrameStyle(QFrame::Box);
114 preview->setFixedSize(50, 50);
115 hbox->addWidget(preview);
116 lay->addLayout(hbox);
117 }
118
119 void _k_slotKeepOriginalSizeClicked(bool);
120 void _k_slotUrlChanged(const QString &);
121 void _k_slotImageWidthChanged(int);
122 void _k_slotImageHeightChanged(int);
123
124 qreal imageRatio = -1;
125 QCheckBox *keepOriginalSize = nullptr;
126 QCheckBox *keepImageRatio = nullptr;
127 QSpinBox *width = nullptr;
128 QSpinBox *height = nullptr;
129 KUrlRequester *imageUrlRequester = nullptr;
130 QLabel *preview = nullptr;
131 InsertImageWidget *const q;
132};
133
134void InsertImageWidgetPrivate::_k_slotKeepOriginalSizeClicked(bool checked)
135{
136 height->setEnabled(!checked);
137 width->setEnabled(!checked);
138 keepImageRatio->setEnabled(!checked);
139 // Update default size
140 _k_slotUrlChanged(imageUrlRequester->text());
141}
142
143void InsertImageWidgetPrivate::_k_slotUrlChanged(const QString &text)
144{
145 QImage image(text);
146 if (!image.isNull()) {
147 height->setValue(image.height());
148 width->setValue(image.width());
149
150 imageRatio = (double)((double)image.height() / (double)image.width());
151 preview->setPixmap(QPixmap::fromImage(image));
152 } else {
153 preview->clear();
154 imageRatio = -1;
155 }
156 Q_EMIT q->enableButtonOk(!text.trimmed().isEmpty());
157}
158
159void InsertImageWidgetPrivate::_k_slotImageWidthChanged(int value)
160{
161 if (keepImageRatio->isChecked() && !keepOriginalSize->isChecked()) {
162 if (imageRatio != -1) {
163 height->blockSignals(true);
164 height->setValue(value * imageRatio);
165 height->blockSignals(false);
166 }
167 }
168}
169
170void InsertImageWidgetPrivate::_k_slotImageHeightChanged(int value)
171{
172 if (keepImageRatio->isChecked() && !keepOriginalSize->isChecked()) {
173 if (imageRatio != -1) {
174 width->blockSignals(true);
175 width->setValue(value / imageRatio);
176 width->blockSignals(false);
177 }
178 }
179}
180
181InsertImageWidget::InsertImageWidget(QWidget *parent)
182 : QWidget(parent)
183 , d(new InsertImageWidgetPrivate(this))
184{
185}
186
187InsertImageWidget::~InsertImageWidget() = default;
188
189int InsertImageWidget::imageWidth() const
190{
191 return d->width->value();
192}
193
194int InsertImageWidget::imageHeight() const
195{
196 return d->height->value();
197}
198
199void InsertImageWidget::setImageWidth(int value)
200{
201 d->width->setValue(value);
202}
203
204void InsertImageWidget::setImageHeight(int value)
205{
206 d->height->setValue(value);
207}
208
209QUrl InsertImageWidget::imageUrl() const
210{
211 return d->imageUrlRequester->url();
212}
213
214void InsertImageWidget::setImageUrl(const QUrl &url)
215{
216 d->imageUrlRequester->setUrl(url);
217}
218
219bool InsertImageWidget::keepOriginalSize() const
220{
221 return d->keepOriginalSize->isChecked();
222}
223}
224
225#include "moc_insertimagewidget.cpp"
void setMode(KFile::Modes mode)
KLineEdit * lineEdit() const
void setMimeTypeFilters(const QStringList &mimeTypes)
QString text() const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void setChecked(bool)
void clicked(bool checked)
void setFrameStyle(int style)
QList< QByteArray > supportedMimeTypes()
void clear()
void setPixmap(const QPixmap &)
void textChanged(const QString &text)
qsizetype count() const const
void reserve(qsizetype size)
QMimeType mimeTypeForName(const QString &nameOrAlias) const const
bool blockSignals(bool block)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QPixmap fromImage(QImage &&image, Qt::ImageConversionFlags flags)
void setMaximum(int max)
void setMinimum(int min)
void setSuffix(const QString &suffix)
void setValue(int val)
void valueChanged(int i)
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
QString trimmed() const const
QString url(FormattingOptions options) const const
void setEnabled(bool)
void setFixedSize(const QSize &s)
void setWindowTitle(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:55:51 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.