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::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"));

View File

@ -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();

View File

@ -18,8 +18,13 @@
*/
#include "chattextedit.h"
#include "src/widget/translator.h"
#include <QApplication>
#include <QClipboard>
#include <QKeyEvent>
#include <QMimeData>
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;
}

View File

@ -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;