diff --git a/ui/widgets/CMakeLists.txt b/ui/widgets/CMakeLists.txt index c7e47e0..f4267ba 100644 --- a/ui/widgets/CMakeLists.txt +++ b/ui/widgets/CMakeLists.txt @@ -18,6 +18,8 @@ target_sources(squawk PRIVATE newcontact.ui room.cpp room.h + messagetextedit.cpp + messagetextedit.h ) add_subdirectory(vcard) diff --git a/ui/widgets/conversation.cpp b/ui/widgets/conversation.cpp index ea2f722..645ee8b 100644 --- a/ui/widgets/conversation.cpp +++ b/ui/widgets/conversation.cpp @@ -17,6 +17,7 @@ */ #include "conversation.h" +#include "messagetextedit.h" #include "ui_conversation.h" #include @@ -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)); -} diff --git a/ui/widgets/conversation.h b/ui/widgets/conversation.h index 6b5b4bb..3221a5c 100644 --- a/ui/widgets/conversation.h +++ b/ui/widgets/conversation.h @@ -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; diff --git a/ui/widgets/conversation.ui b/ui/widgets/conversation.ui index 483375a..15d1779 100644 --- a/ui/widgets/conversation.ui +++ b/ui/widgets/conversation.ui @@ -351,7 +351,7 @@ - + true @@ -419,6 +419,13 @@ p, li { white-space: pre-wrap; } + + + MessageTextEdit + QTextEdit +
ui/widgets/conversation.h
+
+
diff --git a/ui/widgets/messagetextedit.cpp b/ui/widgets/messagetextedit.cpp new file mode 100644 index 0000000..7741bf0 --- /dev/null +++ b/ui/widgets/messagetextedit.cpp @@ -0,0 +1,27 @@ +#include +#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()); + } else if (source->hasUrls()) { + QList urls = source->urls(); + for (const QUrl& url : qAsConst(urls)) { + if (url.isLocalFile()) { + emit fileInserted(url.toLocalFile()); + } + } + } else { + insertPlainText(source->text()); + } +} diff --git a/ui/widgets/messagetextedit.h b/ui/widgets/messagetextedit.h new file mode 100644 index 0000000..af5ccc1 --- /dev/null +++ b/ui/widgets/messagetextedit.h @@ -0,0 +1,30 @@ +#ifndef MESSAGETEXTEDIT_H +#define MESSAGETEXTEDIT_H + +#include +#include +#include + +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