1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

feat(paste): Implement pasting images from clipboard

Fixes #1290
This commit is contained in:
anthony.bilinski 2017-09-15 15:27:45 -07:00
parent c49959711e
commit 5fed37365f
4 changed files with 49 additions and 12 deletions

View File

@ -178,6 +178,7 @@ ChatForm::ChatForm(Friend* chatFriend)
connect(msgEdit, &ChatTextEdit::enterPressed, this, &ChatForm::onSendTriggered); connect(msgEdit, &ChatTextEdit::enterPressed, this, &ChatForm::onSendTriggered);
connect(msgEdit, &ChatTextEdit::textChanged, this, &ChatForm::onTextEditChanged); connect(msgEdit, &ChatTextEdit::textChanged, this, &ChatForm::onTextEditChanged);
connect(msgEdit, &ChatTextEdit::pasteImage, this, &ChatForm::sendImage);
connect(statusMessageLabel, &CroppingLabel::customContextMenuRequested, this, connect(statusMessageLabel, &CroppingLabel::customContextMenuRequested, this,
[&](const QPoint& pos) { [&](const QPoint& pos) {
if (!statusMessageLabel->text().isEmpty()) { if (!statusMessageLabel->text().isEmpty()) {
@ -783,19 +784,19 @@ void ChatForm::doScreenshot()
{ {
// note: grabber is self-managed and will destroy itself when done // note: grabber is self-managed and will destroy itself when done
ScreenshotGrabber* grabber = new ScreenshotGrabber; ScreenshotGrabber* grabber = new ScreenshotGrabber;
connect(grabber, &ScreenshotGrabber::screenshotTaken, this, &ChatForm::onScreenshotTaken); connect(grabber, &ScreenshotGrabber::screenshotTaken, this, &ChatForm::sendImage);
grabber->showGrabber(); 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 // use ~ISO 8601 for screenshot timestamp, considering FS limitations
// https://en.wikipedia.org/wiki/ISO_8601 // https://en.wikipedia.org/wiki/ISO_8601
// Windows has to be supported, thus filename can't have `:` in it :/ // 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` // 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(Settings::getInstance().getAppDataDirPath())
.arg(QDir::separator()) .arg(QDir::separator())
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH-mm-ss.zzz")); .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH-mm-ss.zzz"));

View File

@ -97,7 +97,7 @@ private slots:
void onLoadHistory(); void onLoadHistory();
void onUpdateTime(); void onUpdateTime();
void onScreenshotClicked(); void onScreenshotClicked();
void onScreenshotTaken(const QPixmap& pixmap); void sendImage(const QPixmap& pixmap);
void doScreenshot(); void doScreenshot();
void onCopyStatusMessage(); void onCopyStatusMessage();
void onExportChat(); void onExportChat();

View File

@ -18,8 +18,13 @@
*/ */
#include "chattextedit.h" #include "chattextedit.h"
#include "src/widget/translator.h" #include "src/widget/translator.h"
#include <QApplication>
#include <QClipboard>
#include <QKeyEvent> #include <QKeyEvent>
#include <QMimeData>
ChatTextEdit::ChatTextEdit(QWidget* parent) ChatTextEdit::ChatTextEdit(QWidget* parent)
: QTextEdit(parent) : QTextEdit(parent)
@ -39,23 +44,30 @@ ChatTextEdit::~ChatTextEdit()
void ChatTextEdit::keyPressEvent(QKeyEvent* event) void ChatTextEdit::keyPressEvent(QKeyEvent* event)
{ {
int key = event->key(); 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(); emit enterPressed();
else if (key == Qt::Key_Tab) { return;
}
if (key == Qt::Key_Tab) {
if (event->modifiers()) if (event->modifiers())
event->ignore(); event->ignore();
else { else {
emit tabPressed(); emit tabPressed();
event->ignore(); event->ignore();
} }
} else if (key == Qt::Key_Up && this->toPlainText().isEmpty()) { return;
}
if (key == Qt::Key_Up && this->toPlainText().isEmpty()) {
this->setText(lastMessage); this->setText(lastMessage);
this->setFocus(); this->setFocus();
this->moveCursor(QTextCursor::MoveOperation::End, QTextCursor::MoveMode::MoveAnchor); this->moveCursor(QTextCursor::MoveOperation::End, QTextCursor::MoveMode::MoveAnchor);
} else { return;
emit keyPressed();
QTextEdit::keyPressEvent(event);
} }
if (event->matches(QKeySequence::Paste) && pasteIfImage(event)) {
return;
}
emit keyPressed();
QTextEdit::keyPressEvent(event);
} }
void ChatTextEdit::setLastMessage(QString lm) void ChatTextEdit::setLastMessage(QString lm)
@ -72,3 +84,25 @@ void ChatTextEdit::sendKeyEvent(QKeyEvent* event)
{ {
emit keyPressEvent(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;
}

View File

@ -35,12 +35,14 @@ signals:
void enterPressed(); void enterPressed();
void tabPressed(); void tabPressed();
void keyPressed(); void keyPressed();
void pasteImage(const QPixmap& pixmap);
protected: protected:
virtual void keyPressEvent(QKeyEvent* event) final override; virtual void keyPressEvent(QKeyEvent* event) final override;
private: private:
void retranslateUi(); void retranslateUi();
bool pasteIfImage(QKeyEvent* event);
private: private:
QString lastMessage; QString lastMessage;