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
Showing only changes of commit 9aba09f919 - Show all commits

View File

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

View File

@ -17,6 +17,7 @@
*/ */
#include "conversation.h" #include "conversation.h"
#include "messagetextedit.h"
#include "ui_conversation.h" #include "ui_conversation.h"
#include <QDebug> #include <QDebug>
@ -51,7 +52,6 @@ Conversation::Conversation(bool muc, Models::Account* acc, Models::Element* el,
delegate(new MessageDelegate(this)), delegate(new MessageDelegate(this)),
manualSliderChange(false), manualSliderChange(false),
tsb(QApplication::style()->styleHint(QStyle::SH_ScrollBar_Transient) == 1), tsb(QApplication::style()->styleHint(QStyle::SH_ScrollBar_Transient) == 1),
pasteImageAction(new QAction(tr("Paste Image"), this)),
shadow(10, 1, Qt::black, this), shadow(10, 1, Qt::black, this),
contextMenu(new QMenu()) contextMenu(new QMenu())
{ {
@ -80,7 +80,8 @@ Conversation::Conversation(bool muc, Models::Account* acc, Models::Element* el,
statusLabel = m_ui->statusLabel; statusLabel = m_ui->statusLabel;
connect(&ker, &KeyEnterReceiver::enterPressed, this, &Conversation::onEnterPressed); 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->sendButton, &QPushButton::clicked, this, &Conversation::onEnterPressed);
connect(m_ui->attachButton, &QPushButton::clicked, this, &Conversation::onAttach); connect(m_ui->attachButton, &QPushButton::clicked, this, &Conversation::onAttach);
connect(m_ui->clearButton, &QPushButton::clicked, this, &Conversation::onClearButton); 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); this, &Conversation::onTextEditDocSizeChanged);
m_ui->messageEditor->installEventFilter(&ker); 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); //line->setAutoFillBackground(false);
//if (testAttribute(Qt::WA_TranslucentBackground)) { //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); return QObject::eventFilter(obj, event);
} }
bool Conversation::checkClipboardImage() {
return !QApplication::clipboard()->image().isNull();
}
QString Conversation::getPalResource() const QString Conversation::getPalResource() const
{ {
return activePalResource; 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()) { if (image.isNull()) {
return; return;
} }
@ -255,6 +243,11 @@ void Conversation::onImagePasted()
// See Core::NetworkAccess::onUploadFinished. // See Core::NetworkAccess::onUploadFinished.
} }
void Conversation::onFileInserted(const QString& localPath)
{
addAttachedFile(localPath);
}
void Conversation::onAttach() void Conversation::onAttach()
{ {
QFileDialog* d = new QFileDialog(this, tr("Chose a file to send")); 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: signals:
void enterPressed(); void enterPressed();
void imagePasted();
}; };
class Conversation : public QWidget class Conversation : public QWidget
@ -78,7 +77,6 @@ public:
void setPalResource(const QString& res); void setPalResource(const QString& res);
virtual void setAvatar(const QString& path); virtual void setAvatar(const QString& path);
void setFeedFrames(bool top, bool right, bool bottom, bool left); void setFeedFrames(bool top, bool right, bool bottom, bool left);
static bool checkClipboardImage();
signals: signals:
void sendMessage(const Shared::Message& message); void sendMessage(const Shared::Message& message);
@ -104,7 +102,8 @@ protected:
protected slots: protected slots:
void onEnterPressed(); void onEnterPressed();
void onImagePasted(); void onImageInserted(const QImage& image);
void onFileInserted(const QString& localPath);
void onAttach(); void onAttach();
void onFileSelected(); void onFileSelected();
void onBadgeClose(); void onBadgeClose();
@ -114,7 +113,6 @@ protected slots:
void onFeedMessage(const Shared::Message& msg); void onFeedMessage(const Shared::Message& msg);
void positionShadow(); void positionShadow();
void onFeedContext(const QPoint &pos); void onFeedContext(const QPoint &pos);
void onMessageEditorContext(const QPoint &pos);
public: public:
const bool isMuc; const bool isMuc;
@ -137,8 +135,6 @@ protected:
bool manualSliderChange; bool manualSliderChange;
bool tsb; //transient scroll bars bool tsb; //transient scroll bars
QAction* pasteImageAction;
ShadowOverlay shadow; ShadowOverlay shadow;
QMenu* contextMenu; QMenu* contextMenu;
}; };

View File

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