-
Notifications
You must be signed in to change notification settings - Fork 65
Implement #132: Support pasting images into room #138
Changes from 1 commit
1a2cc88
0e178cc
af238f7
41b887b
4e8034c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,10 +16,13 @@ | |
*/ | ||
|
||
#include <QAbstractTextDocumentLayout> | ||
#include <QApplication> | ||
#include <QClipboard> | ||
#include <QDebug> | ||
#include <QFile> | ||
#include <QFileDialog> | ||
#include <QImageReader> | ||
#include <QMimeData> | ||
#include <QPainter> | ||
#include <QStyleOption> | ||
|
||
|
@@ -31,6 +34,7 @@ static constexpr size_t INPUT_HISTORY_SIZE = 127; | |
FilteredTextEdit::FilteredTextEdit(QWidget *parent) | ||
: QTextEdit{parent} | ||
, history_index_{0} | ||
, isImage_{false} | ||
{ | ||
connect(document()->documentLayout(), | ||
&QAbstractTextDocumentLayout::documentSizeChanged, | ||
|
@@ -93,12 +97,50 @@ FilteredTextEdit::keyPressEvent(QKeyEvent *event) | |
} | ||
break; | ||
} | ||
case Qt::Key_V: { | ||
if (event->modifiers() == Qt::ControlModifier) { | ||
insertFromMimeData(QApplication::clipboard()->mimeData()); | ||
} | ||
break; | ||
} | ||
default: | ||
QTextEdit::keyPressEvent(event); | ||
break; | ||
} | ||
} | ||
|
||
bool | ||
FilteredTextEdit::canInsertFromMimeData(const QMimeData *source) const | ||
{ | ||
return (source->hasImage() || source->hasText() || | ||
QTextEdit::canInsertFromMimeData(source)); | ||
} | ||
|
||
void | ||
FilteredTextEdit::insertFromMimeData(const QMimeData *source) | ||
{ | ||
if (source->hasImage()) { | ||
QImage img = qvariant_cast<QImage>(source->imageData()); | ||
pastedImagePath_ = QDir::tempPath() + '/' + "nheko_pasted_img.png"; | ||
|
||
// Save image into temporary path to be loaded later. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no reason for this to hit disk, and doing so introduces a number of bugs. Leave the image in memory. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason I write to disk is because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should probably refactor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll create a new pull request with the requested changes. |
||
QFile file{pastedImagePath_}; | ||
if (!file.open(QIODevice::WriteOnly)) { | ||
qDebug() << "Failed to create temporary image file"; | ||
return; | ||
} | ||
if (!img.save(file.fileName(), "PNG")) { | ||
qDebug() << "Failed to save image data"; | ||
return; | ||
} | ||
|
||
isImage_ = true; | ||
textCursor().insertImage(pastedImagePath_); | ||
} else { | ||
QTextEdit::insertFromMimeData(source); | ||
} | ||
} | ||
|
||
void | ||
FilteredTextEdit::stopTyping() | ||
{ | ||
|
@@ -155,6 +197,9 @@ FilteredTextEdit::submit() | |
} else { | ||
command(name, args); | ||
} | ||
} else if (isImage_) { | ||
emit image(pastedImagePath_); | ||
isImage_ = false; | ||
} else { | ||
message(std::move(text)); | ||
} | ||
|
@@ -229,6 +274,9 @@ TextInputWidget::TextInputWidget(QWidget *parent) | |
connect(sendFileBtn_, SIGNAL(clicked()), this, SLOT(openFileSelection())); | ||
connect(input_, &FilteredTextEdit::message, this, &TextInputWidget::sendTextMessage); | ||
connect(input_, &FilteredTextEdit::command, this, &TextInputWidget::command); | ||
connect(input_, &FilteredTextEdit::image, this, [=](QString name) { | ||
emit uploadImage(name); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this can be written |
||
connect(emojiBtn_, | ||
SIGNAL(emojiSelected(const QString &)), | ||
this, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoding this is incorrect. You cannot make assumptions about what keybind any given system uses to express "paste." Try using
QKeySequence::Copy
instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On further review, given that Qt handles MIME clipboards in the first place, it's actually not obvious to me that handling pasting by hand like this is even necessary at all, given the other changes in this PR. If it truly is, some comments explaining why Qt's default behavior is inadequate would be nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unsure of what you mean. If you mean, that Qt has built in support out-of-the-box for pasting images into a QTextEdit, then I have not seen any document which suggests that. The fact that
QTextEdit::insertFromMimeData
andQTextEdit::canInsertFromMimeData
exist suggest that it does not have built in support. However, this is just an educated guess.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to guess. What happens if you disable this hardcoding but retain the overrides to those methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. I removed the explicit call to
insertFromMimeData()
(the entire case in the switch statement), and kept just the overrides. It still works.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary hardcoded handling of paste fixed in
41b887b
and0e178cc