Editor: refine image-insert dialog

This commit is contained in:
Le Tan 2018-06-08 20:01:37 +08:00
parent e59a23cda6
commit 4549919e68
2 changed files with 16 additions and 32 deletions

View File

@ -1,8 +1,7 @@
#include <QtWidgets> #include <QtWidgets>
#include <QValidator> #include <QValidator>
#include <QRegExp> #include <QRegExp>
#include <QDebug>
#include <QTimer>
#include "vinsertimagedialog.h" #include "vinsertimagedialog.h"
#include "utils/vutils.h" #include "utils/vutils.h"
#include "vmetawordlineedit.h" #include "vmetawordlineedit.h"
@ -16,8 +15,7 @@ VInsertImageDialog::VInsertImageDialog(const QString &p_title,
QWidget *p_parent) QWidget *p_parent)
: QDialog(p_parent), : QDialog(p_parent),
m_image(NULL), m_image(NULL),
m_browsable(p_browsable), m_browsable(p_browsable)
m_timer(NULL)
{ {
setupUI(p_title, p_imageTitle, p_imagePath); setupUI(p_title, p_imageTitle, p_imagePath);
@ -25,24 +23,9 @@ VInsertImageDialog::VInsertImageDialog(const QString &p_title,
this, &VInsertImageDialog::handleInputChanged); this, &VInsertImageDialog::handleInputChanged);
if (m_browsable) { if (m_browsable) {
m_timer = new QTimer(this); connect(m_pathEdit, &VLineEdit::editingFinished,
m_timer->setSingleShot(true);
m_timer->setInterval(500 /* ms */);
connect(m_timer, &QTimer::timeout,
this, &VInsertImageDialog::handlePathEditChanged); this, &VInsertImageDialog::handlePathEditChanged);
connect(m_pathEdit, &VLineEdit::textChanged,
this, [this]() {
m_timer->stop();
setImage(QImage());
if (m_pathEdit->text().isEmpty()) {
return;
}
m_timer->start();
});
connect(browseBtn, &QPushButton::clicked, connect(browseBtn, &QPushButton::clicked,
this, &VInsertImageDialog::handleBrowseBtnClicked); this, &VInsertImageDialog::handleBrowseBtnClicked);
@ -131,7 +114,7 @@ void VInsertImageDialog::handleBrowseBtnClicked()
{ {
static QString lastPath = QDir::homePath(); static QString lastPath = QDir::homePath();
QString filePath = QFileDialog::getOpenFileName(this, tr("Select The Image To Be Inserted"), QString filePath = QFileDialog::getOpenFileName(this, tr("Select The Image To Be Inserted"),
lastPath, tr("Images (*.png *.xpm *.jpg *.bmp *.gif)")); lastPath, tr("Images (*.png *.xpm *.jpg *.bmp *.gif *.svg)"));
if (filePath.isEmpty()) { if (filePath.isEmpty()) {
return; return;
} }
@ -141,7 +124,7 @@ void VInsertImageDialog::handleBrowseBtnClicked()
m_imageType = ImageType::LocalFile; m_imageType = ImageType::LocalFile;
m_pathEdit->setText(filePath); setPath(filePath);
m_imageTitleEdit->setFocus(); m_imageTitleEdit->setFocus();
} }
@ -212,7 +195,6 @@ void VInsertImageDialog::fetchImageFromClipboard()
setImage(im); setImage(im);
m_imageType = ImageType::ImageData; m_imageType = ImageType::ImageData;
qDebug() << "fetch image data from clipboard to insert";
return; return;
} else if (mimeData->hasUrls()) { } else if (mimeData->hasUrls()) {
QList<QUrl> urls = mimeData->urls(); QList<QUrl> urls = mimeData->urls();
@ -227,9 +209,9 @@ void VInsertImageDialog::fetchImageFromClipboard()
if (url.isValid()) { if (url.isValid()) {
if (url.isLocalFile()) { if (url.isLocalFile()) {
m_pathEdit->setText(url.toLocalFile()); setPath(url.toLocalFile());
} else { } else {
m_pathEdit->setText(url.toString()); setPath(url.toString());
} }
} }
} }
@ -238,7 +220,7 @@ void VInsertImageDialog::handlePathEditChanged()
{ {
QString text = m_pathEdit->text(); QString text = m_pathEdit->text();
QUrl url = QUrl::fromUserInput(text); QUrl url = QUrl::fromUserInput(text);
if (!url.isValid()) { if (text.isEmpty() || !url.isValid()) {
setImage(QImage()); setImage(QImage());
return; return;
} }
@ -248,7 +230,6 @@ void VInsertImageDialog::handlePathEditChanged()
image = VUtils::imageFromFile(url.toLocalFile()); image = VUtils::imageFromFile(url.toLocalFile());
setImage(image); setImage(image);
m_imageType = ImageType::LocalFile; m_imageType = ImageType::LocalFile;
qDebug() << "fetch local file image to insert" << text;
} else { } else {
setImage(QImage()); setImage(QImage());
m_imageType = ImageType::ImageData; m_imageType = ImageType::ImageData;
@ -256,8 +237,13 @@ void VInsertImageDialog::handlePathEditChanged()
connect(downloader, &VDownloader::downloadFinished, connect(downloader, &VDownloader::downloadFinished,
this, &VInsertImageDialog::imageDownloaded); this, &VInsertImageDialog::imageDownloaded);
downloader->download(url.toString()); downloader->download(url.toString());
qDebug() << "try to fetch network image to insert" << text;
} }
handleInputChanged(); handleInputChanged();
} }
void VInsertImageDialog::setPath(const QString &p_path)
{
m_pathEdit->setText(p_path);
handlePathEditChanged();
}

View File

@ -11,7 +11,6 @@ class VLineEdit;
class VMetaWordLineEdit; class VMetaWordLineEdit;
class QPushButton; class QPushButton;
class QDialogButtonBox; class QDialogButtonBox;
class QTimer;
class VInsertImageDialog : public QDialog class VInsertImageDialog : public QDialog
{ {
@ -58,6 +57,8 @@ private:
void fetchImageFromClipboard(); void fetchImageFromClipboard();
void setPath(const QString &p_path);
VMetaWordLineEdit *m_imageTitleEdit; VMetaWordLineEdit *m_imageTitleEdit;
VLineEdit *m_pathEdit; VLineEdit *m_pathEdit;
QPushButton *browseBtn; QPushButton *browseBtn;
@ -70,9 +71,6 @@ private:
bool m_browsable; bool m_browsable;
ImageType m_imageType; ImageType m_imageType;
// Timer for path edit change.
QTimer *m_timer;
}; };
inline VInsertImageDialog::ImageType VInsertImageDialog::getImageType() const inline VInsertImageDialog::ImageType VInsertImageDialog::getImageType() const