Commit 44f8214b authored by Laurent Montel's avatar Laurent Montel 😁

Add image dialog

parent 2ddb11cb
......@@ -7,6 +7,7 @@ set(libcomposereditor_webengine_SRCS
link/composeranchordialog.cpp
link/composerlinkdialog.cpp
utils/composereditorutils.cpp
image/composerimagedialog.cpp
)
ecm_qt_declare_logging_category(libcomposereditor_webengine_SRCS HEADER composereditorwebengine_debug.h IDENTIFIER COMPOSEREDITORWEBENGINE_LOG CATEGORY_NAME log_composereditorwebengine)
......
/*
Copyright (c) 2012-2016 Montel Laurent <montel@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 "composerimagedialog.h"
//#include "extendattributes/extendattributesbutton.h"
#include <kpimtextedit/insertimagewidget.h>
#include <KLocalizedString>
#include <QLineEdit>
#include <KSeparator>
#include <QUrl>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
//#include <QWebElement>
#include <QDialogButtonBox>
#include <QPushButton>
namespace ComposerEditorWebEngine
{
class ComposerImageDialogPrivate
{
public:
ComposerImageDialogPrivate(ComposerImageDialog *qq)
: q(qq)
{
}
void _k_slotOkClicked();
void _k_slotApplyClicked();
void _k_slotWebElementChanged();
void _k_slotEnableButtonOk(bool b);
void initialize();
QString html() const;
void updateImageHtml();
void updateSettings();
//QWebElement webElement;
KPIMTextEdit::InsertImageWidget *imageWidget;
QLineEdit *title;
QLineEdit *alternateTitle;
QPushButton *okButton;
ComposerImageDialog *q;
};
void ComposerImageDialogPrivate::_k_slotEnableButtonOk(bool b)
{
okButton->setEnabled(b);
}
void ComposerImageDialogPrivate::_k_slotWebElementChanged()
{
updateSettings();
}
void ComposerImageDialogPrivate::_k_slotOkClicked()
{
#if 0
if (!webElement.isNull()) {
updateImageHtml();
}
#endif
q->accept();
}
void ComposerImageDialogPrivate::_k_slotApplyClicked()
{
#if 0
if (!webElement.isNull()) {
updateImageHtml();
}
#endif
}
void ComposerImageDialogPrivate::updateImageHtml()
{
#if 0
int imageWidth = -1;
int imageHeight = -1;
if (!imageWidget->keepOriginalSize()) {
imageWidth = imageWidget->imageWidth();
imageHeight = imageWidget->imageHeight();
}
if (imageWidth == -1) {
webElement.removeAttribute(QStringLiteral("width"));
} else {
webElement.setAttribute(QStringLiteral("width"), QString::number(imageWidth));
}
if (imageHeight == -1) {
webElement.removeAttribute(QStringLiteral("height"));
} else {
webElement.setAttribute(QStringLiteral("height"), QString::number(imageHeight));
}
QString str(title->text());
if (str.isEmpty()) {
webElement.removeAttribute(QStringLiteral("title"));
} else {
webElement.setAttribute(QStringLiteral("title"), str);
}
str = alternateTitle->text();
if (str.isEmpty()) {
webElement.removeAttribute(QStringLiteral("alt"));
} else {
webElement.setAttribute(QStringLiteral("alt"), str);
}
webElement.setAttribute(QStringLiteral("src"), imageWidget->imageUrl().url());
#endif
}
void ComposerImageDialogPrivate::initialize()
{
//q->setWindowTitle(webElement.isNull() ? i18n("Insert Image") : i18n("Edit Image"));
QDialogButtonBox *buttonBox = new QDialogButtonBox(/*webElement.isNull() ? */QDialogButtonBox::Ok /*: QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Apply*/);
QVBoxLayout *mainLayout = new QVBoxLayout;
q->setLayout(mainLayout);
q->connect(buttonBox, SIGNAL(accepted()), q, SLOT(_k_slotOkClicked()));
q->connect(buttonBox, &QDialogButtonBox::rejected, q, &QDialog::reject);
okButton = buttonBox->button(QDialogButtonBox::Ok);
okButton->setText(i18n("Insert"));
QWidget *w = new QWidget;
QVBoxLayout *lay = new QVBoxLayout;
lay->setMargin(0);
lay->setSpacing(0);
w->setLayout(lay);
imageWidget = new KPIMTextEdit::InsertImageWidget(q);
lay->addWidget(imageWidget);
KSeparator *sep = new KSeparator;
lay->addWidget(sep);
//ToolTip
QHBoxLayout *hbox = new QHBoxLayout;
QLabel *lab = new QLabel(i18n("Tooltip:"));
hbox->addWidget(lab);
title = new QLineEdit;
title->setClearButtonEnabled(true);
hbox->addWidget(title);
lay->addLayout(hbox);
//Alternate text
hbox = new QHBoxLayout;
lab = new QLabel(i18n("Alternate text:"));
hbox->addWidget(lab);
alternateTitle = new QLineEdit;
alternateTitle->setClearButtonEnabled(true);
hbox->addWidget(alternateTitle);
lay->addLayout(hbox);
#if 0
if (!webElement.isNull()) {
ExtendAttributesButton *button = new ExtendAttributesButton(webElement, ExtendAttributesDialog::Image, q);
q->connect(button, SIGNAL(webElementChanged()), q, SLOT(_k_slotWebElementChanged()));
lay->addWidget(button);
q->connect(q, SIGNAL(clicked()), q, SLOT(_k_slotApplyClicked()));
}
#endif
sep = new KSeparator;
lay->addWidget(sep);
q->connect(imageWidget, SIGNAL(enableButtonOk(bool)), q, SLOT(_k_slotEnableButtonOk(bool)));
mainLayout->addWidget(w);
mainLayout->addWidget(buttonBox);
okButton->setEnabled(false);
updateSettings();
}
void ComposerImageDialogPrivate::updateSettings()
{
#if 0
imageWidget->setImageUrl(QUrl(webElement.attribute(QStringLiteral("src"))));
if (webElement.hasAttribute(QStringLiteral("height")) && webElement.hasAttribute(QStringLiteral("width"))) {
imageWidget->setImageWidth(webElement.attribute(QStringLiteral("width")).toInt());
imageWidget->setImageHeight(webElement.attribute(QStringLiteral("height")).toInt());
}
alternateTitle->setText(webElement.attribute(QStringLiteral("alt")));
title->setText(webElement.attribute(QStringLiteral("title")));
#endif
}
QString ComposerImageDialogPrivate::html() const
{
const QUrl url = imageWidget->imageUrl();
int imageWidth = -1;
int imageHeight = -1;
if (!imageWidget->keepOriginalSize()) {
imageWidth = imageWidget->imageWidth();
imageHeight = imageWidget->imageHeight();
}
QString imageHtml = QStringLiteral("<img");
if (imageWidth > 0) {
imageHtml += QStringLiteral(" width=%1").arg(imageWidth);
}
if (imageHeight > 0) {
imageHtml += QStringLiteral(" height=%1").arg(imageHeight);
}
if (!url.isEmpty()) {
imageHtml += QStringLiteral(" src='file://%1'").arg(url.path());
}
QString str = title->text();
if (!str.isEmpty()) {
imageHtml += QStringLiteral(" title='%1'").arg(str);
}
str = alternateTitle->text();
if (!str.isEmpty()) {
imageHtml += QStringLiteral(" alt='%1'").arg(str);
}
imageHtml += QLatin1String(" />");
return imageHtml;
}
ComposerImageDialog::ComposerImageDialog(QWidget *parent)
: QDialog(parent), d(new ComposerImageDialogPrivate(this))
{
d->initialize();
}
#if 0
ComposerImageDialog::ComposerImageDialog(const QWebElement &element, QWidget *parent)
: QDialog(parent), d(new ComposerImageDialogPrivate(this))
{
d->webElement = element;
d->initialize();
}
#endif
ComposerImageDialog::~ComposerImageDialog()
{
delete d;
}
QString ComposerImageDialog::html() const
{
return d->html();
}
}
#include "moc_composerimagedialog.cpp"
/*
Copyright (c) 2012-2016 Montel Laurent <montel@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.
*/
#ifndef COMPOSERIMAGEDIALOG_H
#define COMPOSERIMAGEDIALOG_H
#include <QDialog>
//class QWebElement;
namespace ComposerEditorWebEngine
{
class ComposerImageDialogPrivate;
class ComposerImageDialog : public QDialog
{
Q_OBJECT
public:
explicit ComposerImageDialog(QWidget *parent);
//explicit ComposerImageDialog(const QWebElement &element, QWidget *parent);
~ComposerImageDialog();
/**
* @brief html
* @return string which represents html code
*/
QString html() const;
private:
friend class ComposerImageDialogPrivate;
ComposerImageDialogPrivate *const d;
Q_PRIVATE_SLOT(d, void _k_slotOkClicked())
Q_PRIVATE_SLOT(d, void _k_slotApplyClicked())
Q_PRIVATE_SLOT(d, void _k_slotWebElementChanged())
Q_PRIVATE_SLOT(d, void _k_slotEnableButtonOk(bool b))
};
}
#endif // COMPOSERIMAGEDIALOG_H
/*
Copyright (c) 2013-2016 Montel Laurent <montel@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 "composerimageresizetooltip.h"
#include <KLocalizedString>
#include <QHBoxLayout>
#include <QLabel>
namespace ComposerEditorNG
{
class ComposerImageResizeToolTipPrivate
{
public:
ComposerImageResizeToolTipPrivate(ComposerImageResizeToolTip *qq)
: q(qq)
{
QHBoxLayout *lay = new QHBoxLayout;
lay->setMargin(0);
lay->setSpacing(0);
lab = new QLabel;
lay->addWidget(lab);
q->setLayout(lay);
}
void displaySize(const QSize &s);
QLabel *lab;
ComposerImageResizeToolTip *q;
};
void ComposerImageResizeToolTipPrivate::displaySize(const QSize &s)
{
lab->setText(i18n("%1x%2").arg(s.width()).arg(s.height()));
}
ComposerImageResizeToolTip::ComposerImageResizeToolTip(QWidget *parent)
: QWidget(parent), d(new ComposerImageResizeToolTipPrivate(this))
{
setWindowFlags(Qt::ToolTip);
}
ComposerImageResizeToolTip::~ComposerImageResizeToolTip()
{
delete d;
}
void ComposerImageResizeToolTip::displaySize(const QSize &s)
{
d->displaySize(s);
}
}
/*
Copyright (c) 2013-2016 Montel Laurent <montel@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.
*/
#ifndef COMPOSERIMAGERESIZETOOLTIP_H
#define COMPOSERIMAGERESIZETOOLTIP_H
#include <QWidget>
namespace ComposerEditorNG
{
class ComposerImageResizeToolTipPrivate;
class ComposerImageResizeToolTip : public QWidget
{
Q_OBJECT
public:
explicit ComposerImageResizeToolTip(QWidget *parent);
~ComposerImageResizeToolTip();
void displaySize(const QSize &s);
private:
friend class ComposerImageResizeToolTipPrivate;
ComposerImageResizeToolTipPrivate *const d;
};
}
#endif // COMPOSERIMAGERESIZETOOLTIP_H
/*
Copyright (c) 2012-2016 Montel Laurent <montel@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 "composerimageresizewidget.h"
#include "composerimageresizetooltip.h"
#include <QMouseEvent>
#include <QPainter>
namespace ComposerEditorNG
{
static const int resizeSquareSize = 7;
class ComposerImageResizeWidgetPrivate
{
public:
enum ResizeDirection {
None,
Top,
Bottom,
Left,
Right,
TopLeft,
TopRight,
BottomLeft,
BottomRight
};
ComposerImageResizeWidgetPrivate(ComposerImageResizeWidget *qq, const QWebElement &element)
: q(qq),
imageSizetoolTip(0),
imageElement(element),
direction(None),
mousePressed(false)
{
q->resize(imageElement.geometry().size());
}
void resizeElement(const QPoint &pos);
QSize resizeImage(const QPoint &pos);
void setResizeDirectionCursor(const QPoint &pos);
ResizeDirection resizeDirection(const QPoint &pos);
ComposerImageResizeWidget *q;
ComposerImageResizeToolTip *imageSizetoolTip;
QWebElement imageElement;
ResizeDirection direction;
QPoint firstPosition;
bool mousePressed;
};
void ComposerImageResizeWidgetPrivate::setResizeDirectionCursor(const QPoint &pos)
{
ResizeDirection dir = resizeDirection(pos);
switch (dir) {
case None:
q->setCursor(Qt::ArrowCursor);
break;
case Top:
case Bottom:
q->setCursor(Qt::SizeVerCursor);
break;
case Left:
case Right:
q->setCursor(Qt::SizeHorCursor);
break;
case TopLeft:
case BottomRight:
q->setCursor(Qt::SizeFDiagCursor);
break;
case TopRight:
case BottomLeft:
q->setCursor(Qt::SizeBDiagCursor);
break;
}
}
ComposerImageResizeWidgetPrivate::ResizeDirection ComposerImageResizeWidgetPrivate::resizeDirection(const QPoint &pos)
{
ResizeDirection dir;
const QRect r(imageElement.geometry());
if (QRect(0, 0, resizeSquareSize, resizeSquareSize).contains(pos)) {
dir = TopLeft;
} else if (QRect(0, r.height() - resizeSquareSize, resizeSquareSize, resizeSquareSize).contains(pos)) {
dir = BottomLeft;
} else if (QRect(r.width() - resizeSquareSize, r.height() - resizeSquareSize, resizeSquareSize, resizeSquareSize).contains(pos)) {
dir = BottomRight;
} else if (QRect(r.width() - resizeSquareSize, 0, resizeSquareSize, resizeSquareSize).contains(pos)) {
dir = TopRight;
} else if (QRect(0, 0, r.width(), resizeSquareSize).contains(pos)) {
dir = Top;
} else if (QRect(0, r.height() - resizeSquareSize, r.width(), resizeSquareSize).contains(pos)) {
dir = Bottom;
} else if (QRect(0, 0, resizeSquareSize, r.height()).contains(pos)) {
dir = Left;
} else if (QRect(r.width() - resizeSquareSize, 0, resizeSquareSize, r.height()).contains(pos)) {
dir = Right;
} else if (QRect(r.width(), resizeSquareSize, resizeSquareSize, resizeSquareSize).contains(pos)) {
dir = TopLeft;
} else {
dir = None;
}
return dir;
}
void ComposerImageResizeWidgetPrivate::resizeElement(const QPoint &pos)
{
const QSize size = resizeImage(pos);
if (size.width() != -1) {
imageElement.setAttribute(QStringLiteral("width"), QString::number(size.width()));
}
if (size.height() != -1) {
imageElement.setAttribute(QStringLiteral("height"), QString::number(size.height()));
}
q->resize(size);
}
QSize ComposerImageResizeWidgetPrivate::resizeImage(const QPoint &pos)
{
int width = -1;
int height = -1;
switch (direction) {
case None:
break;
case Top:
height = imageElement.attribute(QStringLiteral("height")).toInt() - pos.y() - firstPosition.y();
break;
case Bottom:
height = imageElement.attribute(QStringLiteral("height")).toInt() + pos.y() - firstPosition.y();
break;
case Left:
width = imageElement.attribute(QStringLiteral("width")).toInt() - pos.x() - firstPosition.x();
break;
case Right:
width = imageElement.attribute(QStringLiteral("width")).toInt() + pos.x() - firstPosition.x();
break;
case TopLeft:
width = imageElement.attribute(QStringLiteral("width")).toInt() - pos.x() - firstPosition.x();
height = imageElement.attribute(QStringLiteral("height")).toInt() - pos.y() - firstPosition.y();
break;
case BottomRight:
width = imageElement.attribute(QStringLiteral("width")).toInt() + pos.x() - firstPosition.x();
height = imageElement.attribute(QStringLiteral("height")).toInt() + pos.y() - firstPosition.y();
break;
case TopRight:
height = imageElement.attribute(QStringLiteral("height")).toInt() - pos.y() - firstPosition.y();
width = imageElement.attribute(QStringLiteral("width")).toInt() + pos.x() - firstPosition.x();
break;
case BottomLeft: