optimize: use QTextEdit::insertFromMimeData to process pasting images #60

Closed
shunf4 wants to merge 1 commits from opt/mime_paste_img into messageFeed
6 changed files with 79 additions and 35 deletions

View File

@ -18,6 +18,8 @@ target_sources(squawk PRIVATE
newcontact.ui
room.cpp
room.h
messagetextedit.cpp
messagetextedit.h
)
add_subdirectory(vcard)

View File

@ -17,6 +17,7 @@
*/
#include "conversation.h"
#include "messagetextedit.h"
#include "ui_conversation.h"
#include <QDebug>
@ -51,7 +52,6 @@ Conversation::Conversation(bool muc, Models::Account* acc, Models::Element* el,
delegate(new MessageDelegate(this)),
manualSliderChange(false),
tsb(QApplication::style()->styleHint(QStyle::SH_ScrollBar_Transient) == 1),
pasteImageAction(new QAction(tr("Paste Image"), this)),
shadow(10, 1, Qt::black, this),
contextMenu(new QMenu())
{
@ -80,7 +80,8 @@ Conversation::Conversation(bool muc, Models::Account* acc, Models::Element* el,
statusLabel = m_ui->statusLabel;
connect(&ker, &KeyEnterReceiver::enterPressed, this, &Conversation::onEnterPressed);
connect(&ker, &KeyEnterReceiver::imagePasted, this, &Conversation::onImagePasted);
connect(m_ui->messageEditor, &MessageTextEdit::imageInserted, this, &Conversation::onImageInserted);
connect(m_ui->messageEditor, &MessageTextEdit::fileInserted, this, &Conversation::onFileInserted);
connect(m_ui->sendButton, &QPushButton::clicked, this, &Conversation::onEnterPressed);
connect(m_ui->attachButton, &QPushButton::clicked, this, &Conversation::onAttach);
connect(m_ui->clearButton, &QPushButton::clicked, this, &Conversation::onClearButton);
@ -88,10 +89,8 @@ Conversation::Conversation(bool muc, Models::Account* acc, Models::Element* el,
this, &Conversation::onTextEditDocSizeChanged);
m_ui->messageEditor->installEventFilter(&ker);
m_ui->messageEditor->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_ui->messageEditor, &QTextEdit::customContextMenuRequested, this, &Conversation::onMessageEditorContext);
connect(pasteImageAction, &QAction::triggered, this, &Conversation::onImagePasted);
//line->setAutoFillBackground(false);
//if (testAttribute(Qt::WA_TranslucentBackground)) {
@ -192,20 +191,10 @@ bool KeyEnterReceiver::eventFilter(QObject* obj, QEvent* event)
}
}
}
if (k == Qt::Key_V && key->modifiers() & Qt::CTRL) {
if (Conversation::checkClipboardImage()) {
emit imagePasted();
return true;
}
}
}
return QObject::eventFilter(obj, event);
}
bool Conversation::checkClipboardImage() {
return !QApplication::clipboard()->image().isNull();
}
QString Conversation::getPalResource() const
{
return activePalResource;
@ -237,9 +226,8 @@ void Conversation::onEnterPressed()
}
}
void Conversation::onImagePasted()
void Conversation::onImageInserted(const QImage& image)
{
QImage image = QApplication::clipboard()->image();
if (image.isNull()) {
return;
}
@ -255,6 +243,11 @@ void Conversation::onImagePasted()
// See Core::NetworkAccess::onUploadFinished.
}
void Conversation::onFileInserted(const QString& localPath)
{
addAttachedFile(localPath);
}
void Conversation::onAttach()
{
QFileDialog* d = new QFileDialog(this, tr("Chose a file to send"));
@ -484,14 +477,3 @@ void Conversation::onFeedContext(const QPoint& pos)
}
}
}
void Conversation::onMessageEditorContext(const QPoint& pos)
{
pasteImageAction->setEnabled(Conversation::checkClipboardImage());
QMenu *editorMenu = m_ui->messageEditor->createStandardContextMenu();
editorMenu->addSeparator();
editorMenu->addAction(pasteImageAction);
editorMenu->exec(this->m_ui->messageEditor->mapToGlobal(pos));
}

View File

@ -60,7 +60,6 @@ protected:
signals:
void enterPressed();
void imagePasted();
};
class Conversation : public QWidget
@ -78,7 +77,6 @@ public:
void setPalResource(const QString& res);
virtual void setAvatar(const QString& path);
void setFeedFrames(bool top, bool right, bool bottom, bool left);
static bool checkClipboardImage();
signals:
void sendMessage(const Shared::Message& message);
@ -104,7 +102,8 @@ protected:
protected slots:
void onEnterPressed();
void onImagePasted();
void onImageInserted(const QImage& image);
void onFileInserted(const QString& localPath);
void onAttach();
void onFileSelected();
void onBadgeClose();
@ -114,7 +113,6 @@ protected slots:
void onFeedMessage(const Shared::Message& msg);
void positionShadow();
void onFeedContext(const QPoint &pos);
void onMessageEditorContext(const QPoint &pos);
public:
const bool isMuc;
@ -136,8 +134,6 @@ protected:
MessageDelegate* delegate;
bool manualSliderChange;
bool tsb; //transient scroll bars
QAction* pasteImageAction;
ShadowOverlay shadow;
QMenu* contextMenu;

View File

@ -351,7 +351,7 @@
</widget>
</item>
<item>
<widget class="QTextEdit" name="messageEditor">
<widget class="MessageTextEdit" name="messageEditor">
<property name="enabled">
<bool>true</bool>
</property>
@ -419,6 +419,13 @@ p, li { white-space: pre-wrap; }
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>MessageTextEdit</class>
<extends>QTextEdit</extends>
<header location="global">ui/widgets/conversation.h</header>
</customwidget>
</customwidgets>
<resources>
<include location="../../resources/resources.qrc"/>
</resources>

View File

@ -0,0 +1,27 @@
#include <QMimeData>
#include "messagetextedit.h"
bool MessageTextEdit::canInsertFromMimeData(const QMimeData* source) const
{
return source->hasImage() || source->hasUrls() || source->hasText();
}
void MessageTextEdit::insertFromMimeData(const QMimeData* source)
{
if (source == nullptr) {
return;
}
if (source->hasImage()) {
emit imageInserted(source->imageData().value<QImage>());
} else if (source->hasUrls()) {
QList<QUrl> urls = source->urls();
for (const QUrl& url : qAsConst(urls)) {
if (url.isLocalFile()) {
emit fileInserted(url.toLocalFile());
}
}
} else {
insertPlainText(source->text());
}
}

View File

@ -0,0 +1,30 @@
#ifndef MESSAGETEXTEDIT_H
#define MESSAGETEXTEDIT_H
#include <QTextEdit>
#include <QObject>
#include <QWidget>
namespace Ui
{
class MessageTextEdit;
}
class MessageTextEdit : public QTextEdit
{
Q_OBJECT
public:
explicit MessageTextEdit(QWidget *parent = nullptr) : QTextEdit(parent) {}
explicit MessageTextEdit(const QString &text, QWidget *parent = nullptr) : QTextEdit(text, parent) {}
signals:
void imageInserted(const QImage& image);
void fileInserted(const QString& localPath);
protected:
bool canInsertFromMimeData(const QMimeData* source) const override;
void insertFromMimeData(const QMimeData* source) override;
};
#endif // MESSAGETEXTEDIT_H