7#include "insertimagewidget.h"
10#include <KLocalizedString>
12#include <KUrlRequester>
16#include <QImageReader>
18#include <QMimeDatabase>
24static inline QString resolveAlias(
const QString &name)
31class InsertImageWidgetPrivate
34 explicit InsertImageWidgetPrivate(InsertImageWidget *qq)
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;
43 QStringList lstMimeTypes;
46 for (
const QByteArray &ba : mimetypes) {
49 lstMimeTypes << resolvedAlias;
52 imageUrlRequester->setMimeTypeFilters(lstMimeTypes);
53 imageUrlRequester->setWindowTitle(
i18nc(
"@title:window",
"Add Image"));
54 imageUrlRequester->setMode(KFile::File);
56 _k_slotUrlChanged(str);
60 hbox->addWidget(imageUrlRequester);
61 lab->setBuddy(imageUrlRequester);
64 keepOriginalSize =
new QCheckBox(
i18nc(
"@option:check",
"Keep Original Size"));
66 _k_slotKeepOriginalSizeClicked(b);
68 keepOriginalSize->setChecked(
true);
69 lay->addWidget(keepOriginalSize);
71 keepImageRatio =
new QCheckBox(
i18nc(
"@option:check",
"Keep Image Ratio"));
72 keepImageRatio->setChecked(
true);
73 keepImageRatio->setEnabled(
false);
74 lay->addWidget(keepImageRatio);
76 hbox =
new QHBoxLayout;
77 lab =
new QLabel(
i18nc(
"@label:textbox",
"Width:"));
80 width->setMaximum(99999);
81 width->setEnabled(
false);
83 width->setSuffix(
i18n(
" px"));
86 _k_slotImageWidthChanged(val);
89 hbox->addWidget(width);
92 hbox =
new QHBoxLayout;
93 lab =
new QLabel(
i18nc(
"@label:textbox",
"Height:"));
94 height =
new QSpinBox;
95 height->setMinimum(1);
97 height->setMaximum(99999);
98 height->setEnabled(
false);
99 height->setSuffix(
i18n(
" px"));
100 lab->setBuddy(height);
102 _k_slotImageHeightChanged(val);
104 hbox->addWidget(lab);
105 hbox->addWidget(height);
106 lay->addLayout(hbox);
108 auto sep =
new KSeparator;
111 hbox =
new QHBoxLayout;
112 lab =
new QLabel(
i18nc(
"@label:textbox",
"Image Preview:"));
113 hbox->addWidget(lab);
114 preview =
new QLabel;
116 preview->setFixedSize(50, 50);
117 hbox->addWidget(preview);
118 lay->addLayout(hbox);
121 void _k_slotKeepOriginalSizeClicked(
bool);
122 void _k_slotUrlChanged(
const QString &);
123 void _k_slotImageWidthChanged(
int);
124 void _k_slotImageHeightChanged(
int);
126 qreal imageRatio = -1;
127 QCheckBox *keepOriginalSize =
nullptr;
128 QCheckBox *keepImageRatio =
nullptr;
129 QSpinBox *width =
nullptr;
130 QSpinBox *height =
nullptr;
131 KUrlRequester *imageUrlRequester =
nullptr;
132 QLabel *preview =
nullptr;
133 InsertImageWidget *
const q;
136void InsertImageWidgetPrivate::_k_slotKeepOriginalSizeClicked(
bool checked)
138 height->setEnabled(!checked);
139 width->setEnabled(!checked);
140 keepImageRatio->setEnabled(!checked);
142 _k_slotUrlChanged(imageUrlRequester->text());
145void InsertImageWidgetPrivate::_k_slotUrlChanged(
const QString &text)
148 if (!image.isNull()) {
149 height->setValue(image.height());
150 width->setValue(image.width());
152 imageRatio = (double)((
double)image.height() / (
double)image.width());
161void InsertImageWidgetPrivate::_k_slotImageWidthChanged(
int value)
163 if (keepImageRatio->isChecked() && !keepOriginalSize->isChecked()) {
164 if (imageRatio != -1) {
165 height->blockSignals(
true);
166 height->setValue(value * imageRatio);
167 height->blockSignals(
false);
172void InsertImageWidgetPrivate::_k_slotImageHeightChanged(
int value)
174 if (keepImageRatio->isChecked() && !keepOriginalSize->isChecked()) {
175 if (imageRatio != -1) {
176 width->blockSignals(
true);
177 width->setValue(value / imageRatio);
178 width->blockSignals(
false);
183InsertImageWidget::InsertImageWidget(QWidget *parent)
185 , d(new InsertImageWidgetPrivate(this))
189InsertImageWidget::~InsertImageWidget() =
default;
191int InsertImageWidget::imageWidth()
const
193 return d->width->value();
196int InsertImageWidget::imageHeight()
const
198 return d->height->value();
201void InsertImageWidget::setImageWidth(
int value)
203 d->width->setValue(value);
206void InsertImageWidget::setImageHeight(
int value)
208 d->height->setValue(value);
211QUrl InsertImageWidget::imageUrl()
const
213 return d->imageUrlRequester->
url();
216void InsertImageWidget::setImageUrl(
const QUrl &url)
218 d->imageUrlRequester->setUrl(url);
221bool InsertImageWidget::keepOriginalSize()
const
223 return d->keepOriginalSize->isChecked();
227#include "moc_insertimagewidget.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QList< QByteArray > supportedMimeTypes()
void textChanged(const QString &text)
qsizetype count() const const
void reserve(qsizetype size)
QMimeType mimeTypeForName(const QString &nameOrAlias) const const
QPixmap fromImage(QImage &&image, Qt::ImageConversionFlags flags)
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
QString trimmed() const const
QString url(FormattingOptions options) const const