external tools for nonwebengine viewer

This commit is contained in:
Martin Rotter 2021-03-22 12:05:10 +01:00
parent 04a87c8a23
commit 830939f5e6
2 changed files with 49 additions and 0 deletions

View File

@ -3,9 +3,13 @@
#include "gui/messagetextbrowser.h"
#include "miscellaneous/application.h"
#include "miscellaneous/externaltool.h"
#include "miscellaneous/iconfactory.h"
#include "network-web/networkfactory.h"
#include <QContextMenuEvent>
#include <QFileIconProvider>
MessageTextBrowser::MessageTextBrowser(QWidget* parent) : QTextBrowser(parent) {
setAutoFillBackground(true);
setFrameShape(QFrame::StyledPanel);
@ -44,6 +48,50 @@ QSize MessageTextBrowser::sizeHint() const {
return doc_size;
}
void MessageTextBrowser::contextMenuEvent(QContextMenuEvent* event) {
event->accept();
auto* menu = createStandardContextMenu();
if (menu == nullptr) {
return;
}
auto anchor = anchorAt(event->pos());
if (!anchor.isEmpty()) {
QFileIconProvider icon_provider;
QMenu* menu_ext_tools = new QMenu(tr("Open with external tool"), menu);
auto tools = ExternalTool::toolsFromSettings();
menu_ext_tools->setIcon(qApp->icons()->fromTheme(QSL("document-open")));
for (const ExternalTool& tool : qAsConst(tools)) {
QAction* act_tool = new QAction(QFileInfo(tool.executable()).fileName(), menu_ext_tools);
act_tool->setIcon(icon_provider.icon(tool.executable()));
act_tool->setToolTip(tool.executable());
act_tool->setData(QVariant::fromValue(tool));
menu_ext_tools->addAction(act_tool);
connect(act_tool, &QAction::triggered, this, [act_tool, anchor]() {
act_tool->data().value<ExternalTool>().run(anchor);
});
}
if (menu_ext_tools->actions().isEmpty()) {
QAction* act_not_tools = new QAction("No external tools activated");
act_not_tools->setEnabled(false);
menu_ext_tools->addAction(act_not_tools);
}
menu->addMenu(menu_ext_tools);
}
menu->popup(event->globalPos());
}
void MessageTextBrowser::wheelEvent(QWheelEvent* event) {
QTextBrowser::wheelEvent(event);
qApp->settings()->setValue(GROUP(Messages), Messages::PreviewerFontStandard, font().toString());

View File

@ -18,6 +18,7 @@ class MessageTextBrowser : public QTextBrowser {
virtual QSize sizeHint() const;
protected:
virtual void contextMenuEvent(QContextMenuEvent* event);
virtual void wheelEvent(QWheelEvent* event);
virtual void resizeEvent (QResizeEvent* event);