|
@@ -2,44 +2,76 @@
|
|
|
#include <QValidator>
|
|
#include <QValidator>
|
|
|
#include <QRegExp>
|
|
#include <QRegExp>
|
|
|
#include <QDebug>
|
|
#include <QDebug>
|
|
|
|
|
+#include <QTimer>
|
|
|
#include "vinsertimagedialog.h"
|
|
#include "vinsertimagedialog.h"
|
|
|
#include "utils/vutils.h"
|
|
#include "utils/vutils.h"
|
|
|
#include "vlineedit.h"
|
|
#include "vlineedit.h"
|
|
|
|
|
+#include "vdownloader.h"
|
|
|
|
|
|
|
|
-VInsertImageDialog::VInsertImageDialog(const QString &title, const QString &defaultImageTitle,
|
|
|
|
|
- const QString &defaultPath, QWidget *parent)
|
|
|
|
|
- : QDialog(parent), title(title), defaultImageTitle(defaultImageTitle), defaultPath(defaultPath),
|
|
|
|
|
- image(NULL)
|
|
|
|
|
|
|
+VInsertImageDialog::VInsertImageDialog(const QString &p_title,
|
|
|
|
|
+ const QString &p_imageTitle,
|
|
|
|
|
+ const QString &p_imagePath,
|
|
|
|
|
+ bool p_browsable,
|
|
|
|
|
+ QWidget *p_parent)
|
|
|
|
|
+ : QDialog(p_parent),
|
|
|
|
|
+ m_image(NULL),
|
|
|
|
|
+ m_browsable(p_browsable),
|
|
|
|
|
+ m_timer(NULL)
|
|
|
{
|
|
{
|
|
|
- setupUI();
|
|
|
|
|
|
|
+ setupUI(p_title, p_imageTitle, p_imagePath);
|
|
|
|
|
|
|
|
connect(m_imageTitleEdit, &QLineEdit::textChanged,
|
|
connect(m_imageTitleEdit, &QLineEdit::textChanged,
|
|
|
this, &VInsertImageDialog::handleInputChanged);
|
|
this, &VInsertImageDialog::handleInputChanged);
|
|
|
- connect(pathEdit, &QLineEdit::textChanged,
|
|
|
|
|
- this, &VInsertImageDialog::handleInputChanged);
|
|
|
|
|
- connect(browseBtn, &QPushButton::clicked,
|
|
|
|
|
- this, &VInsertImageDialog::handleBrowseBtnClicked);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if (m_browsable) {
|
|
|
|
|
+ m_timer = new QTimer(this);
|
|
|
|
|
+ m_timer->setSingleShot(true);
|
|
|
|
|
+ m_timer->setInterval(500 /* ms */);
|
|
|
|
|
+ connect(m_timer, &QTimer::timeout,
|
|
|
|
|
+ this, &VInsertImageDialog::handlePathEditChanged);
|
|
|
|
|
+
|
|
|
|
|
+ connect(m_pathEdit, &QLineEdit::textChanged,
|
|
|
|
|
+ this, [this]() {
|
|
|
|
|
+ m_timer->stop();
|
|
|
|
|
+
|
|
|
|
|
+ setImage(QImage());
|
|
|
|
|
+ if (m_pathEdit->text().isEmpty()) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ m_timer->start();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ connect(browseBtn, &QPushButton::clicked,
|
|
|
|
|
+ this, &VInsertImageDialog::handleBrowseBtnClicked);
|
|
|
|
|
+
|
|
|
|
|
+ fetchImageFromClipboard();
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
handleInputChanged();
|
|
handleInputChanged();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
VInsertImageDialog::~VInsertImageDialog()
|
|
VInsertImageDialog::~VInsertImageDialog()
|
|
|
{
|
|
{
|
|
|
- if (image) {
|
|
|
|
|
- delete image;
|
|
|
|
|
- image = NULL;
|
|
|
|
|
|
|
+ if (m_image) {
|
|
|
|
|
+ delete m_image;
|
|
|
|
|
+ m_image = NULL;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void VInsertImageDialog::setupUI()
|
|
|
|
|
|
|
+void VInsertImageDialog::setupUI(const QString &p_title,
|
|
|
|
|
+ const QString &p_imageTitle,
|
|
|
|
|
+ const QString &p_imagePath)
|
|
|
{
|
|
{
|
|
|
- pathLabel = new QLabel(tr("&From:"));
|
|
|
|
|
- pathEdit = new QLineEdit(defaultPath);
|
|
|
|
|
- pathLabel->setBuddy(pathEdit);
|
|
|
|
|
|
|
+ QLabel *pathLabel = new QLabel(tr("&From:"));
|
|
|
|
|
+ m_pathEdit = new QLineEdit(p_imagePath);
|
|
|
|
|
+ pathLabel->setBuddy(m_pathEdit);
|
|
|
browseBtn = new QPushButton(tr("&Browse"));
|
|
browseBtn = new QPushButton(tr("&Browse"));
|
|
|
|
|
+ m_pathEdit->setReadOnly(!m_browsable);
|
|
|
|
|
+ browseBtn->setEnabled(m_browsable);
|
|
|
|
|
|
|
|
- imageTitleLabel = new QLabel(tr("&Image title:"));
|
|
|
|
|
- m_imageTitleEdit = new VLineEdit(defaultImageTitle);
|
|
|
|
|
|
|
+ QLabel *imageTitleLabel = new QLabel(tr("&Image title:"));
|
|
|
|
|
+ m_imageTitleEdit = new VLineEdit(p_imageTitle);
|
|
|
m_imageTitleEdit->selectAll();
|
|
m_imageTitleEdit->selectAll();
|
|
|
imageTitleLabel->setBuddy(m_imageTitleEdit);
|
|
imageTitleLabel->setBuddy(m_imageTitleEdit);
|
|
|
QValidator *validator = new QRegExpValidator(QRegExp(VUtils::c_imageTitleRegExp),
|
|
QValidator *validator = new QRegExpValidator(QRegExp(VUtils::c_imageTitleRegExp),
|
|
@@ -48,7 +80,7 @@ void VInsertImageDialog::setupUI()
|
|
|
|
|
|
|
|
QGridLayout *topLayout = new QGridLayout();
|
|
QGridLayout *topLayout = new QGridLayout();
|
|
|
topLayout->addWidget(pathLabel, 0, 0);
|
|
topLayout->addWidget(pathLabel, 0, 0);
|
|
|
- topLayout->addWidget(pathEdit, 0, 1);
|
|
|
|
|
|
|
+ topLayout->addWidget(m_pathEdit, 0, 1);
|
|
|
topLayout->addWidget(browseBtn, 0, 2);
|
|
topLayout->addWidget(browseBtn, 0, 2);
|
|
|
topLayout->addWidget(imageTitleLabel, 1, 0);
|
|
topLayout->addWidget(imageTitleLabel, 1, 0);
|
|
|
topLayout->addWidget(m_imageTitleEdit, 1, 1, 1, 2);
|
|
topLayout->addWidget(m_imageTitleEdit, 1, 1, 1, 2);
|
|
@@ -71,28 +103,19 @@ void VInsertImageDialog::setupUI()
|
|
|
mainLayout->addWidget(imagePreviewLabel);
|
|
mainLayout->addWidget(imagePreviewLabel);
|
|
|
setLayout(mainLayout);
|
|
setLayout(mainLayout);
|
|
|
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
|
|
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
|
|
|
- setWindowTitle(title);
|
|
|
|
|
|
|
+ setWindowTitle(p_title);
|
|
|
|
|
|
|
|
m_imageTitleEdit->setFocus();
|
|
m_imageTitleEdit->setFocus();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void VInsertImageDialog::handleInputChanged()
|
|
void VInsertImageDialog::handleInputChanged()
|
|
|
{
|
|
{
|
|
|
- bool pathOk = true;
|
|
|
|
|
- if (pathEdit->isVisible() && !pathEdit->isReadOnly()) {
|
|
|
|
|
- QString path = pathEdit->text();
|
|
|
|
|
- if (path.isEmpty()
|
|
|
|
|
- || !VUtils::checkPathLegal(path)) {
|
|
|
|
|
- pathOk = false;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
QString title = m_imageTitleEdit->getEvaluatedText();
|
|
QString title = m_imageTitleEdit->getEvaluatedText();
|
|
|
QRegExp reg(VUtils::c_imageTitleRegExp);
|
|
QRegExp reg(VUtils::c_imageTitleRegExp);
|
|
|
bool titleOk = reg.exactMatch(title);
|
|
bool titleOk = reg.exactMatch(title);
|
|
|
|
|
|
|
|
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
|
|
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
|
|
|
- okBtn->setEnabled(pathOk && titleOk);
|
|
|
|
|
|
|
+ okBtn->setEnabled(titleOk && m_image);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
QString VInsertImageDialog::getImageTitleInput() const
|
|
QString VInsertImageDialog::getImageTitleInput() const
|
|
@@ -102,7 +125,7 @@ QString VInsertImageDialog::getImageTitleInput() const
|
|
|
|
|
|
|
|
QString VInsertImageDialog::getPathInput() const
|
|
QString VInsertImageDialog::getPathInput() const
|
|
|
{
|
|
{
|
|
|
- return pathEdit->text();
|
|
|
|
|
|
|
+ return m_pathEdit->text();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void VInsertImageDialog::handleBrowseBtnClicked()
|
|
void VInsertImageDialog::handleBrowseBtnClicked()
|
|
@@ -117,34 +140,39 @@ void VInsertImageDialog::handleBrowseBtnClicked()
|
|
|
// Update lastPath
|
|
// Update lastPath
|
|
|
lastPath = QFileInfo(filePath).path();
|
|
lastPath = QFileInfo(filePath).path();
|
|
|
|
|
|
|
|
- pathEdit->setText(filePath);
|
|
|
|
|
- QImage image(filePath);
|
|
|
|
|
- if (image.isNull()) {
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- setImage(image);
|
|
|
|
|
|
|
+ m_imageType = ImageType::LocalFile;
|
|
|
|
|
+
|
|
|
|
|
+ m_pathEdit->setText(filePath);
|
|
|
|
|
+
|
|
|
|
|
+ m_imageTitleEdit->setFocus();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void VInsertImageDialog::setImage(const QImage &image)
|
|
void VInsertImageDialog::setImage(const QImage &image)
|
|
|
{
|
|
{
|
|
|
if (image.isNull()) {
|
|
if (image.isNull()) {
|
|
|
- qWarning() << "set Null image";
|
|
|
|
|
|
|
+ imagePreviewLabel->setVisible(false);
|
|
|
|
|
+ if (m_image) {
|
|
|
|
|
+ delete m_image;
|
|
|
|
|
+ m_image = NULL;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ handleInputChanged();
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int width = 512 * VUtils::calculateScaleFactor();
|
|
int width = 512 * VUtils::calculateScaleFactor();
|
|
|
QSize previewSize(width, width);
|
|
QSize previewSize(width, width);
|
|
|
- if (!this->image) {
|
|
|
|
|
- this->image = new QImage(image);
|
|
|
|
|
|
|
+ if (!m_image) {
|
|
|
|
|
+ m_image = new QImage(image);
|
|
|
} else {
|
|
} else {
|
|
|
- *(this->image) = image;
|
|
|
|
|
|
|
+ *m_image = image;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
QPixmap pixmap;
|
|
QPixmap pixmap;
|
|
|
if (image.width() > width || image.height() > width) {
|
|
if (image.width() > width || image.height() > width) {
|
|
|
- pixmap = QPixmap::fromImage(this->image->scaled(previewSize, Qt::KeepAspectRatio));
|
|
|
|
|
|
|
+ pixmap = QPixmap::fromImage(m_image->scaled(previewSize, Qt::KeepAspectRatio));
|
|
|
} else {
|
|
} else {
|
|
|
- pixmap = QPixmap::fromImage(*(this->image));
|
|
|
|
|
|
|
+ pixmap = QPixmap::fromImage(*m_image);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
imagePreviewLabel->setPixmap(pixmap);
|
|
imagePreviewLabel->setPixmap(pixmap);
|
|
@@ -153,26 +181,87 @@ void VInsertImageDialog::setImage(const QImage &image)
|
|
|
handleInputChanged();
|
|
handleInputChanged();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void VInsertImageDialog::setBrowseable(bool browseable, bool visible)
|
|
|
|
|
-{
|
|
|
|
|
- pathEdit->setReadOnly(!browseable);
|
|
|
|
|
- browseBtn->setEnabled(browseable);
|
|
|
|
|
-
|
|
|
|
|
- pathLabel->setVisible(visible);
|
|
|
|
|
- pathEdit->setVisible(visible);
|
|
|
|
|
- browseBtn->setVisible(visible);
|
|
|
|
|
-
|
|
|
|
|
- handleInputChanged();
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
void VInsertImageDialog::imageDownloaded(const QByteArray &data)
|
|
void VInsertImageDialog::imageDownloaded(const QByteArray &data)
|
|
|
{
|
|
{
|
|
|
|
|
+ m_imageType = ImageType::ImageData;
|
|
|
setImage(QImage::fromData(data));
|
|
setImage(QImage::fromData(data));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
QImage VInsertImageDialog::getImage() const
|
|
QImage VInsertImageDialog::getImage() const
|
|
|
{
|
|
{
|
|
|
- if (!image) {
|
|
|
|
|
|
|
+ if (!m_image) {
|
|
|
return QImage();
|
|
return QImage();
|
|
|
- } else return *image;
|
|
|
|
|
|
|
+ } else return *m_image;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void VInsertImageDialog::fetchImageFromClipboard()
|
|
|
|
|
+{
|
|
|
|
|
+ if (!m_browsable || !m_pathEdit->text().isEmpty()) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Q_ASSERT(!m_image);
|
|
|
|
|
+
|
|
|
|
|
+ QClipboard *clipboard = QApplication::clipboard();
|
|
|
|
|
+ const QMimeData *mimeData = clipboard->mimeData();
|
|
|
|
|
+
|
|
|
|
|
+ QUrl url;
|
|
|
|
|
+
|
|
|
|
|
+ if (mimeData->hasImage()) {
|
|
|
|
|
+ QImage im = qvariant_cast<QImage>(mimeData->imageData());
|
|
|
|
|
+ if (im.isNull()) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ setImage(im);
|
|
|
|
|
+ m_imageType = ImageType::ImageData;
|
|
|
|
|
+ qDebug() << "fetch image data from clipboard to insert";
|
|
|
|
|
+ return;
|
|
|
|
|
+ } else if (mimeData->hasUrls()) {
|
|
|
|
|
+ QList<QUrl> urls = mimeData->urls();
|
|
|
|
|
+ if (urls.size() != 1) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ url = urls[0];
|
|
|
|
|
+ } else if (mimeData->hasText()) {
|
|
|
|
|
+ url = QUrl(mimeData->text());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (url.isValid()) {
|
|
|
|
|
+ if (url.isLocalFile()) {
|
|
|
|
|
+ m_pathEdit->setText(url.toLocalFile());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ m_pathEdit->setText(url.toString());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void VInsertImageDialog::handlePathEditChanged()
|
|
|
|
|
+{
|
|
|
|
|
+ QString text = m_pathEdit->text();
|
|
|
|
|
+ QUrl url = QUrl::fromUserInput(text);
|
|
|
|
|
+ if (!url.isValid()) {
|
|
|
|
|
+ setImage(QImage());
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ QImage image(text);
|
|
|
|
|
+ if (image.isNull()) {
|
|
|
|
|
+ setImage(QImage());
|
|
|
|
|
+ // Try to treat it as network image.
|
|
|
|
|
+ m_imageType = ImageType::ImageData;
|
|
|
|
|
+ VDownloader *downloader = new VDownloader(this);
|
|
|
|
|
+ connect(downloader, &VDownloader::downloadFinished,
|
|
|
|
|
+ this, &VInsertImageDialog::imageDownloaded);
|
|
|
|
|
+ downloader->download(url.toString());
|
|
|
|
|
+ qDebug() << "try to fetch network image to insert" << text;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // Local image path.
|
|
|
|
|
+ setImage(image);
|
|
|
|
|
+ m_imageType = ImageType::LocalFile;
|
|
|
|
|
+ qDebug() << "fetch local file image to insert" << text;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ handleInputChanged();
|
|
|
}
|
|
}
|