From dceda36f6e5501bc7b945c3836cca8a85d0d9897 Mon Sep 17 00:00:00 2001 From: thunder2 Date: Sat, 15 Apr 2023 19:28:37 +0200 Subject: [PATCH] Moved QAction for saving an image from customContextMenu of GxsForumThreadWidget and ChatWidget to RSTextBrowser --- retroshare-gui/src/gui/chat/ChatWidget.cpp | 15 +----- retroshare-gui/src/gui/chat/ChatWidget.h | 1 - retroshare-gui/src/gui/chat/ChatWidget.ui | 9 ---- .../src/gui/common/RSTextBrowser.cpp | 47 +++++++++++++++++-- retroshare-gui/src/gui/common/RSTextBrowser.h | 15 +++++- .../gui/gxsforums/GxsForumThreadWidget.cpp | 29 ------------ .../src/gui/gxsforums/GxsForumThreadWidget.h | 3 -- .../src/gui/gxsforums/GxsForumThreadWidget.ui | 12 ----- retroshare-gui/src/lang/retroshare_en.ts | 7 ++- 9 files changed, 63 insertions(+), 75 deletions(-) diff --git a/retroshare-gui/src/gui/chat/ChatWidget.cpp b/retroshare-gui/src/gui/chat/ChatWidget.cpp index 47c15a137..6a0117f00 100644 --- a/retroshare-gui/src/gui/chat/ChatWidget.cpp +++ b/retroshare-gui/src/gui/chat/ChatWidget.cpp @@ -165,7 +165,6 @@ ChatWidget::ChatWidget(QWidget *parent) connect(ui->actionResetFont, SIGNAL(triggered()), this, SLOT(resetFont())); connect(ui->actionQuote, SIGNAL(triggered()), this, SLOT(quote())); connect(ui->actionDropPlacemark, SIGNAL(triggered()), this, SLOT(dropPlacemark())); - connect(ui->actionSave_image, SIGNAL(triggered()), this, SLOT(saveImage())); connect(ui->actionImport_sticker, SIGNAL(triggered()), this, SLOT(saveSticker())); connect(ui->actionShow_Hidden_Images, SIGNAL(triggered()), ui->textBrowser, SLOT(showImages())); ui->actionShow_Hidden_Images->setIcon(ui->textBrowser->getBlockedImage()); @@ -1151,10 +1150,7 @@ void ChatWidget::pasteText(const QString& S) void ChatWidget::contextMenuTextBrowser(QPoint point) { - QMatrix matrix; - matrix.translate(ui->textBrowser->horizontalScrollBar()->value(), ui->textBrowser->verticalScrollBar()->value()); - - QMenu *contextMnu = ui->textBrowser->createStandardContextMenu(matrix.map(point)); + QMenu *contextMnu = ui->textBrowser->createStandardContextMenuFromPoint(point); contextMnu->addSeparator(); contextMnu->addAction(ui->actionClearChatHistory); @@ -1167,9 +1163,7 @@ void ChatWidget::contextMenuTextBrowser(QPoint point) if (! ui->textBrowser->getShowImages()) contextMnu->addAction(ui->actionShow_Hidden_Images); - ui->actionSave_image->setData(point); ui->actionImport_sticker->setData(point); - contextMnu->addAction(ui->actionSave_image); contextMnu->addAction(ui->actionImport_sticker); } @@ -1995,13 +1989,6 @@ void ChatWidget::dropPlacemark() // or not. } -void ChatWidget::saveImage() -{ - QPoint point = ui->actionSave_image->data().toPoint(); - QTextCursor cursor = ui->textBrowser->cursorForPosition(point); - ImageUtil::extractImage(window(), cursor); -} - void ChatWidget::saveSticker() { QPoint point = ui->actionImport_sticker->data().toPoint(); diff --git a/retroshare-gui/src/gui/chat/ChatWidget.h b/retroshare-gui/src/gui/chat/ChatWidget.h index 82fb6a486..e42df06fe 100644 --- a/retroshare-gui/src/gui/chat/ChatWidget.h +++ b/retroshare-gui/src/gui/chat/ChatWidget.h @@ -196,7 +196,6 @@ private slots: void quote(); void dropPlacemark(); - void saveImage(); void saveSticker(); private: diff --git a/retroshare-gui/src/gui/chat/ChatWidget.ui b/retroshare-gui/src/gui/chat/ChatWidget.ui index d46093071..39bd84bb7 100644 --- a/retroshare-gui/src/gui/chat/ChatWidget.ui +++ b/retroshare-gui/src/gui/chat/ChatWidget.ui @@ -1049,15 +1049,6 @@ border-image: url(:/images/closepressed.png) Insert horizontal rule - - - - :/images/document_save.png:/images/document_save.png - - - Save image - - diff --git a/retroshare-gui/src/gui/common/RSTextBrowser.cpp b/retroshare-gui/src/gui/common/RSTextBrowser.cpp index cd2bd988e..449f4e1c6 100644 --- a/retroshare-gui/src/gui/common/RSTextBrowser.cpp +++ b/retroshare-gui/src/gui/common/RSTextBrowser.cpp @@ -22,6 +22,7 @@ #include "RSImageBlockWidget.h" #include "gui/common/FilesDefs.h" +#include "util/imageutil.h" #include //To get RsAccounts @@ -33,6 +34,7 @@ #include #include #include +#include #include @@ -317,18 +319,43 @@ QString RSTextBrowser::anchorForPosition(const QPoint &pos) const return anchor; } -QMenu *RSTextBrowser::createStandardContextMenu() +void RSTextBrowser::addContextMenuAction(QAction *action) { - return createStandardContextMenu(QPoint()); + mContextMenuActions.push_back(action); } -QMenu *RSTextBrowser::createStandardContextMenu(const QPoint &position) + +void RSTextBrowser::contextMenuEvent(QContextMenuEvent *event) { - QMenu *menu = QTextBrowser::createStandardContextMenu(position); + emit calculateContextMenuActions(); + + QMenu *contextMenu = createStandardContextMenuFromPoint(event->pos()); + + QList::iterator it; + for (it = mContextMenuActions.begin(); it != mContextMenuActions.end(); ++it) { + contextMenu->addAction(*it); + } + + contextMenu->exec(QCursor::pos()); + + delete(contextMenu); +} + +QMenu *RSTextBrowser::createStandardContextMenuFromPoint(const QPoint &widgetPos) +{ + QMatrix matrix; + matrix.translate(horizontalScrollBar()->value(), verticalScrollBar()->value()); + + QMenu *menu = QTextBrowser::createStandardContextMenu(matrix.map(widgetPos)); menu->addSeparator(); QAction *a = menu->addAction(FilesDefs::getIconFromQtResourcePath("://icons/textedit/code.png"), tr("View &Source"), this, SLOT(viewSource())); a->setEnabled(!this->document()->isEmpty()); + if (checkImage(widgetPos)) { + a = menu->addAction(FilesDefs::getIconFromQtResourcePath(":/images/document_save.png"), tr("Save image"), this, SLOT(saveImage())); + a->setData(widgetPos); + } + return menu; } @@ -350,3 +377,15 @@ void RSTextBrowser::viewSource() delete dialog; } + +void RSTextBrowser::saveImage() +{ + QAction *action = dynamic_cast(sender()) ; + if (!action) { + return; + } + + QPoint point = action->data().toPoint(); + QTextCursor cursor = cursorForPosition(point); + ImageUtil::extractImage(window(), cursor); +} diff --git a/retroshare-gui/src/gui/common/RSTextBrowser.h b/retroshare-gui/src/gui/common/RSTextBrowser.h index 57f351d2b..04de979c5 100644 --- a/retroshare-gui/src/gui/common/RSTextBrowser.h +++ b/retroshare-gui/src/gui/common/RSTextBrowser.h @@ -49,6 +49,8 @@ public: bool checkImage(QPoint pos) {QString imageStr; return checkImage(pos, imageStr); } QString anchorForPosition(const QPoint &pos) const; + // Add QAction to context menu (action won't be deleted) + void addContextMenuAction(QAction *action); void activateLinkClick(bool active); @@ -58,8 +60,10 @@ public: QVariant textColorQuotes() const { return highlighter->textColorQuotes();} bool getShowImages() const { return mShowImages; } - QMenu *createStandardContextMenu(); - QMenu *createStandardContextMenu(const QPoint &position); + QMenu *createStandardContextMenuFromPoint(const QPoint &widgetPos); + +Q_SIGNALS: + void calculateContextMenuActions(); public slots: void showImages(); @@ -70,9 +74,15 @@ private slots: void linkClicked(const QUrl &url); void destroyImageBlockWidget(); void viewSource(); + void saveImage(); protected: void paintEvent(QPaintEvent *event); + virtual void contextMenuEvent(QContextMenuEvent *event); + +private: + // Hide method from QTextBrowser + using QTextBrowser::createStandardContextMenu; private: QString mPlaceholderText; @@ -80,6 +90,7 @@ private: RSImageBlockWidget *mImageBlockWidget; bool mLinkClickActive; RsSyntaxHighlighter *highlighter; + QList mContextMenuActions; #ifdef RSTEXTBROWSER_CHECKIMAGE_DEBUG QRect mCursorRectStart; QRect mCursorRectLeft; diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp index 5ea514a54..1309848c2 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp +++ b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp @@ -276,7 +276,6 @@ GxsForumThreadWidget::GxsForumThreadWidget(const RsGxsGroupId &forumId, QWidget connect(ui->versions_CB, SIGNAL(currentIndexChanged(int)), this, SLOT(changedVersion())); connect(ui->threadTreeWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(threadListCustomPopupMenu(QPoint))); - connect(ui->postText, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuTextBrowser(QPoint))); connect(ui->forumName, SIGNAL(clicked(QPoint)), this, SLOT(showForumInfo())); ui->subscribeToolButton->hide() ; @@ -303,8 +302,6 @@ GxsForumThreadWidget::GxsForumThreadWidget(const RsGxsGroupId &forumId, QWidget connect(ui->filterLineEdit, SIGNAL(textChanged(QString)), this, SLOT(filterItems(QString))); connect(ui->filterLineEdit, SIGNAL(filterChanged(int)), this, SLOT(filterColumnChanged(int))); - connect(ui->actionSave_image, SIGNAL(triggered()), this, SLOT(saveImage())); - connect(ui->threadedView_TB, SIGNAL(toggled(bool)), this, SLOT(toggleThreadedView(bool))); connect(ui->flatView_TB, SIGNAL(toggled(bool)), this, SLOT(toggleFlatView(bool))); connect(ui->latestPostInThreadView_TB, SIGNAL(toggled(bool)), this, SLOT(toggleLstPostInThreadView(bool))); @@ -793,25 +790,6 @@ void GxsForumThreadWidget::threadListCustomPopupMenu(QPoint /*point*/) contextMnu.exec(QCursor::pos()); } -void GxsForumThreadWidget::contextMenuTextBrowser(QPoint point) -{ - QMatrix matrix; - matrix.translate(ui->postText->horizontalScrollBar()->value(), ui->postText->verticalScrollBar()->value()); - - QMenu *contextMnu = ui->postText->createStandardContextMenu(matrix.map(point)); - - contextMnu->addSeparator(); - - if(ui->postText->checkImage(point)) - { - ui->actionSave_image->setData(point); - contextMnu->addAction(ui->actionSave_image); - } - - contextMnu->exec(ui->postText->viewport()->mapToGlobal(point)); - delete(contextMnu); -} - void GxsForumThreadWidget::headerContextMenuRequested(const QPoint &pos) { QMenu* header_context_menu = new QMenu(tr("Show column"), this); @@ -1823,13 +1801,6 @@ void GxsForumThreadWidget::replyForumMessageData(const RsGxsForumMsg &msg) } } -void GxsForumThreadWidget::saveImage() -{ - QPoint point = ui->actionSave_image->data().toPoint(); - QTextCursor cursor = ui->postText->cursorForPosition(point); - ImageUtil::extractImage(window(), cursor); -} - void GxsForumThreadWidget::toggleThreadedView(bool b) { if(b) changedViewBox(VIEW_THREADED); } void GxsForumThreadWidget::toggleFlatView(bool b) { if(b) changedViewBox(VIEW_FLAT); } void GxsForumThreadWidget::toggleLstPostInThreadView(bool b) { if(b) changedViewBox(VIEW_LAST_POST); } diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.h b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.h index 8e64c2b75..322c64fae 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.h +++ b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.h @@ -108,7 +108,6 @@ protected: private slots: /** Create the context popup menu and it's submenus */ void threadListCustomPopupMenu(QPoint point); - void contextMenuTextBrowser(QPoint point); void headerContextMenuRequested(const QPoint& pos); void showForumInfo(); @@ -134,8 +133,6 @@ private slots: // This method is used to perform an asynchroneous action on the message data. Any of the methods above can be used as parameter. void async_msg_action(const MsgMethod& method); - void saveImage(); - void markMsgAsRead(); void markMsgAsReadChildren(); void markMsgAsUnread(); diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.ui b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.ui index 0682550cb..5a9c51e93 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.ui +++ b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.ui @@ -568,9 +568,6 @@ MS Sans Serif - - Qt::CustomContextMenu - @@ -578,15 +575,6 @@ - - - - :/images/document_save.png:/images/document_save.png - - - Save image - - diff --git a/retroshare-gui/src/lang/retroshare_en.ts b/retroshare-gui/src/lang/retroshare_en.ts index 3b670aeaa..d11bfb453 100644 --- a/retroshare-gui/src/lang/retroshare_en.ts +++ b/retroshare-gui/src/lang/retroshare_en.ts @@ -19289,7 +19289,12 @@ p, li { white-space: pre-wrap; } - + + Save image + + + + Document source