| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/messagetextbrowser.h" |
| 4 | |
| 5 | #include "miscellaneous/application.h" |
| 6 | #include "miscellaneous/externaltool.h" |
| 7 | #include "miscellaneous/iconfactory.h" |
| 8 | #include "network-web/networkfactory.h" |
| 9 | |
| 10 | #include <QContextMenuEvent> |
| 11 | #include <QFileIconProvider> |
| 12 | |
| 13 | MessageTextBrowser::MessageTextBrowser(QWidget* parent) : QTextBrowser(parent) { |
| 14 | setAutoFillBackground(true); |
| 15 | setFrameShape(QFrame::Shape::StyledPanel); |
| 16 | setFrameShadow(QFrame::Shadow::Plain); |
| 17 | setTabChangesFocus(true); |
| 18 | setOpenLinks(false); |
| 19 | viewport()->setAutoFillBackground(true); |
| 20 | } |
| 21 | |
| 22 | QVariant MessageTextBrowser::loadResource(int type, const QUrl& name) { |
| 23 | Q_UNUSED(name) |
| 24 | |
| 25 | switch (type) { |
| 26 | case QTextDocument::ResourceType::ImageResource: { |
| 27 | if (qApp->settings()->value(GROUP(Messages), SETTING(Messages::DisplayImagePlaceholders)).toBool()) { |
| 28 | if (m_imagePlaceholder.isNull()) { |
| 29 | m_imagePlaceholder = qApp->icons() |
| 30 | ->miscPixmap(QSL("image-placeholder")) |
| 31 | .scaledToWidth(20, Qt::TransformationMode::FastTransformation); |
| 32 | } |
| 33 | |
| 34 | return m_imagePlaceholder; |
| 35 | } |
| 36 | else { |
| 37 | return QVariant(); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | default: |
| 42 | return QTextBrowser::loadResource(type, name); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | QSize MessageTextBrowser::sizeHint() const { |
| 47 | auto doc_size = document()->size().toSize(); |
| 48 | |
| 49 | doc_size.setHeight(doc_size.height() + contentsMargins().top() + contentsMargins().bottom()); |
| 50 | return doc_size; |
| 51 | } |
| 52 | |
| 53 | void MessageTextBrowser::contextMenuEvent(QContextMenuEvent* event) { |
| 54 | event->accept(); |
| 55 | |
| 56 | auto* menu = createStandardContextMenu(); |
| 57 | |
| 58 | if (menu == nullptr) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | auto anchor = anchorAt(event->pos()); |
| 63 | |
| 64 | if (!anchor.isEmpty()) { |
| 65 | QFileIconProvider icon_provider; |
| 66 | QMenu* menu_ext_tools = new QMenu(tr("Open with external tool"), menu); |
| 67 | auto tools = ExternalTool::toolsFromSettings(); |
| 68 | |
| 69 | menu_ext_tools->setIcon(qApp->icons()->fromTheme(QSL("document-open"))); |
| 70 | |
| 71 | for (const ExternalTool& tool : qAsConst(tools)) { |
| 72 | QAction* act_tool = new QAction(QFileInfo(tool.executable()).fileName(), menu_ext_tools); |
| 73 | |
| 74 | act_tool->setIcon(icon_provider.icon(QFileInfo(tool.executable()))); |
| 75 | act_tool->setToolTip(tool.executable()); |
| 76 | act_tool->setData(QVariant::fromValue(tool)); |
| 77 | menu_ext_tools->addAction(act_tool); |
| 78 | |
| 79 | connect(act_tool, &QAction::triggered, this, [act_tool, anchor]() { |
| 80 | act_tool->data().value<ExternalTool>().run(anchor); |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | if (menu_ext_tools->actions().isEmpty()) { |
| 85 | QAction* act_not_tools = new QAction("No external tools activated"); |
| 86 | |
| 87 | act_not_tools->setEnabled(false); |
| 88 | menu_ext_tools->addAction(act_not_tools); |
| 89 | } |
| 90 | |
| 91 | menu->addMenu(menu_ext_tools); |
| 92 | } |
| 93 | |
| 94 | menu->popup(event->globalPos()); |
| 95 | } |
| 96 | |
| 97 | void MessageTextBrowser::wheelEvent(QWheelEvent* event) { |
| 98 | QTextBrowser::wheelEvent(event); |
| 99 | qApp->settings()->setValue(GROUP(Messages), Messages::PreviewerFontStandard, font().toString()); |
| 100 | } |
| 101 | |
| 102 | void MessageTextBrowser::resizeEvent(QResizeEvent* event) { |
| 103 | // Notify parents about changed geometry. |
| 104 | updateGeometry(); |
| 105 | QTextBrowser::resizeEvent(event); |
| 106 | } |