From 5fed37365f1d32f900814527bc8eb514986f2abd Mon Sep 17 00:00:00 2001 From: "anthony.bilinski" Date: Fri, 15 Sep 2017 15:27:45 -0700 Subject: [PATCH] feat(paste): Implement pasting images from clipboard Fixes #1290 --- src/widget/form/chatform.cpp | 11 ++++---- src/widget/form/chatform.h | 2 +- src/widget/tool/chattextedit.cpp | 46 +++++++++++++++++++++++++++----- src/widget/tool/chattextedit.h | 2 ++ 4 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index 2f7d182bf..e4dc265d7 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -178,6 +178,7 @@ ChatForm::ChatForm(Friend* chatFriend) connect(msgEdit, &ChatTextEdit::enterPressed, this, &ChatForm::onSendTriggered); connect(msgEdit, &ChatTextEdit::textChanged, this, &ChatForm::onTextEditChanged); + connect(msgEdit, &ChatTextEdit::pasteImage, this, &ChatForm::sendImage); connect(statusMessageLabel, &CroppingLabel::customContextMenuRequested, this, [&](const QPoint& pos) { if (!statusMessageLabel->text().isEmpty()) { @@ -783,19 +784,19 @@ void ChatForm::doScreenshot() { // note: grabber is self-managed and will destroy itself when done ScreenshotGrabber* grabber = new ScreenshotGrabber; - connect(grabber, &ScreenshotGrabber::screenshotTaken, this, &ChatForm::onScreenshotTaken); + connect(grabber, &ScreenshotGrabber::screenshotTaken, this, &ChatForm::sendImage); grabber->showGrabber(); - // Create dir for screenshots - QDir(Settings::getInstance().getAppDataDirPath()).mkpath("screenshots"); } -void ChatForm::onScreenshotTaken(const QPixmap& pixmap) +void ChatForm::sendImage(const QPixmap& pixmap) { + QDir(Settings::getInstance().getAppDataDirPath()).mkpath("images"); + // use ~ISO 8601 for screenshot timestamp, considering FS limitations // https://en.wikipedia.org/wiki/ISO_8601 // Windows has to be supported, thus filename can't have `:` in it :/ // Format should be: `qTox_Screenshot_yyyy-MM-dd HH-mm-ss.zzz.png` - QString filepath = QString("%1screenshots%2qTox_Screenshot_%3.png") + QString filepath = QString("%1images%2qTox_Image_%3.png") .arg(Settings::getInstance().getAppDataDirPath()) .arg(QDir::separator()) .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH-mm-ss.zzz")); diff --git a/src/widget/form/chatform.h b/src/widget/form/chatform.h index 489096abf..c1fba2b16 100644 --- a/src/widget/form/chatform.h +++ b/src/widget/form/chatform.h @@ -97,7 +97,7 @@ private slots: void onLoadHistory(); void onUpdateTime(); void onScreenshotClicked(); - void onScreenshotTaken(const QPixmap& pixmap); + void sendImage(const QPixmap& pixmap); void doScreenshot(); void onCopyStatusMessage(); void onExportChat(); diff --git a/src/widget/tool/chattextedit.cpp b/src/widget/tool/chattextedit.cpp index 2ce72cf18..d02030c26 100644 --- a/src/widget/tool/chattextedit.cpp +++ b/src/widget/tool/chattextedit.cpp @@ -18,8 +18,13 @@ */ #include "chattextedit.h" + #include "src/widget/translator.h" + +#include +#include #include +#include ChatTextEdit::ChatTextEdit(QWidget* parent) : QTextEdit(parent) @@ -39,23 +44,30 @@ ChatTextEdit::~ChatTextEdit() void ChatTextEdit::keyPressEvent(QKeyEvent* event) { int key = event->key(); - if ((key == Qt::Key_Enter || key == Qt::Key_Return) && !(event->modifiers() & Qt::ShiftModifier)) + if ((key == Qt::Key_Enter || key == Qt::Key_Return) && !(event->modifiers() & Qt::ShiftModifier)) { emit enterPressed(); - else if (key == Qt::Key_Tab) { + return; + } + if (key == Qt::Key_Tab) { if (event->modifiers()) event->ignore(); else { emit tabPressed(); event->ignore(); } - } else if (key == Qt::Key_Up && this->toPlainText().isEmpty()) { + return; + } + if (key == Qt::Key_Up && this->toPlainText().isEmpty()) { this->setText(lastMessage); this->setFocus(); this->moveCursor(QTextCursor::MoveOperation::End, QTextCursor::MoveMode::MoveAnchor); - } else { - emit keyPressed(); - QTextEdit::keyPressEvent(event); + return; } + if (event->matches(QKeySequence::Paste) && pasteIfImage(event)) { + return; + } + emit keyPressed(); + QTextEdit::keyPressEvent(event); } void ChatTextEdit::setLastMessage(QString lm) @@ -72,3 +84,25 @@ void ChatTextEdit::sendKeyEvent(QKeyEvent* event) { emit keyPressEvent(event); } + +bool ChatTextEdit::pasteIfImage(QKeyEvent* event) +{ + const QClipboard* const clipboard = QApplication::clipboard(); + if (!clipboard) { + return false; + } + + const QMimeData* const mimeData = clipboard->mimeData(); + if (!mimeData || !mimeData->hasImage()) { + return false; + } + + const QPixmap pixmap(clipboard->pixmap()); + if (pixmap.isNull()) { + return false; + } + + emit pasteImage(pixmap); + return true; +} + diff --git a/src/widget/tool/chattextedit.h b/src/widget/tool/chattextedit.h index 8f77a51f4..97e886830 100644 --- a/src/widget/tool/chattextedit.h +++ b/src/widget/tool/chattextedit.h @@ -35,12 +35,14 @@ signals: void enterPressed(); void tabPressed(); void keyPressed(); + void pasteImage(const QPixmap& pixmap); protected: virtual void keyPressEvent(QKeyEvent* event) final override; private: void retranslateUi(); + bool pasteIfImage(QKeyEvent* event); private: QString lastMessage;