2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2010-03-29 19:16:23 +02:00
|
|
|
|
|
|
|
|
#include "helpviewer.h"
|
|
|
|
|
#include "helpconstants.h"
|
2022-07-11 13:30:24 +02:00
|
|
|
#include "helptr.h"
|
2010-11-16 14:15:23 +01:00
|
|
|
#include "localhelpmanager.h"
|
2010-03-29 19:16:23 +02:00
|
|
|
|
2011-03-30 15:15:15 +02:00
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
|
2021-01-14 13:21:15 +01:00
|
|
|
#include <utils/fadingindicator.h>
|
2011-03-30 15:15:15 +02:00
|
|
|
#include <utils/fileutils.h>
|
2017-01-19 16:44:22 +01:00
|
|
|
#include <utils/temporarydirectory.h>
|
2011-03-30 15:15:15 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QUrl>
|
2010-03-29 19:16:23 +02:00
|
|
|
|
2017-04-24 17:01:10 +02:00
|
|
|
#include <QGuiApplication>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDesktopServices>
|
|
|
|
|
#include <QMouseEvent>
|
2010-03-29 19:16:23 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QHelpEngine>
|
2010-03-29 19:16:23 +02:00
|
|
|
|
|
|
|
|
using namespace Help::Internal;
|
|
|
|
|
|
2010-04-19 19:08:02 +02:00
|
|
|
struct ExtensionMap {
|
|
|
|
|
const char *extension;
|
|
|
|
|
const char *mimeType;
|
2018-04-19 10:47:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static ExtensionMap extensionMap[] = {
|
2017-02-22 15:09:35 +01:00
|
|
|
{".bmp", "image/bmp"},
|
|
|
|
|
{".css", "text/css"},
|
|
|
|
|
{".gif", "image/gif"},
|
|
|
|
|
{".html", "text/html"},
|
|
|
|
|
{".htm", "text/html"},
|
|
|
|
|
{".ico", "image/x-icon"},
|
|
|
|
|
{".jpeg", "image/jpeg"},
|
|
|
|
|
{".jpg", "image/jpeg"},
|
|
|
|
|
{".js", "application/x-javascript"},
|
|
|
|
|
{".mng", "video/x-mng"},
|
|
|
|
|
{".pbm", "image/x-portable-bitmap"},
|
|
|
|
|
{".pgm", "image/x-portable-graymap"},
|
|
|
|
|
{".pdf", "application/pdf"},
|
|
|
|
|
{".png", "image/png"},
|
|
|
|
|
{".ppm", "image/x-portable-pixmap"},
|
|
|
|
|
{".rss", "application/rss+xml"},
|
|
|
|
|
{".svg", "image/svg+xml"},
|
|
|
|
|
{".svgz", "image/svg+xml"},
|
|
|
|
|
{".text", "text/plain"},
|
|
|
|
|
{".tif", "image/tiff"},
|
|
|
|
|
{".tiff", "image/tiff"},
|
|
|
|
|
{".txt", "text/plain"},
|
|
|
|
|
{".xbm", "image/x-xbitmap"},
|
|
|
|
|
{".xml", "text/xml"},
|
|
|
|
|
{".xpm", "image/x-xpm"},
|
|
|
|
|
{".xsl", "text/xsl"},
|
|
|
|
|
{".xhtml", "application/xhtml+xml"},
|
|
|
|
|
{".wml", "text/vnd.wap.wml"},
|
|
|
|
|
{".wmlc", "application/vnd.wap.wmlc"},
|
2018-11-11 10:08:29 +01:00
|
|
|
{"about:blank", nullptr}
|
2010-04-19 19:08:02 +02:00
|
|
|
};
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
HelpViewer::HelpViewer(QWidget *parent)
|
|
|
|
|
: QWidget(parent)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-23 16:53:00 +02:00
|
|
|
HelpViewer::~HelpViewer()
|
|
|
|
|
{
|
|
|
|
|
restoreOverrideCursor();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-14 13:21:15 +01:00
|
|
|
void HelpViewer::setFontZoom(int percentage)
|
|
|
|
|
{
|
|
|
|
|
setScale(percentage / 100.0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-15 15:30:52 +01:00
|
|
|
void HelpViewer::setScrollWheelZoomingEnabled(bool enabled)
|
|
|
|
|
{
|
|
|
|
|
m_scrollWheelZoomingEnabled = enabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HelpViewer::isScrollWheelZoomingEnabled() const
|
|
|
|
|
{
|
|
|
|
|
return m_scrollWheelZoomingEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-09 15:37:01 +02:00
|
|
|
void HelpViewer::setActionVisible(Action action, bool visible)
|
|
|
|
|
{
|
|
|
|
|
if (visible)
|
|
|
|
|
m_visibleActions |= Actions(action);
|
|
|
|
|
else
|
|
|
|
|
m_visibleActions &= ~Actions(action);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HelpViewer::isActionVisible(HelpViewer::Action action)
|
|
|
|
|
{
|
|
|
|
|
return (m_visibleActions & Actions(action)) != 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-29 19:16:23 +02:00
|
|
|
bool HelpViewer::isLocalUrl(const QUrl &url)
|
|
|
|
|
{
|
2016-07-20 10:11:35 +02:00
|
|
|
return url.scheme() == "about" // "No documenation available"
|
|
|
|
|
|| url.scheme() == "qthelp";
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HelpViewer::canOpenPage(const QString &url)
|
|
|
|
|
{
|
2010-04-19 19:08:02 +02:00
|
|
|
return !mimeFromUrl(url).isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-20 16:15:21 +02:00
|
|
|
QString HelpViewer::mimeFromUrl(const QUrl &url)
|
2010-04-19 19:08:02 +02:00
|
|
|
{
|
2010-04-20 16:15:21 +02:00
|
|
|
const QString &path = url.path();
|
|
|
|
|
const int index = path.lastIndexOf(QLatin1Char('.'));
|
|
|
|
|
const QByteArray &ext = path.mid(index).toUtf8().toLower();
|
2010-04-19 19:08:02 +02:00
|
|
|
|
2018-11-11 10:08:29 +01:00
|
|
|
for (const auto &e : extensionMap) {
|
|
|
|
|
if (ext == e.extension)
|
|
|
|
|
return QLatin1String(e.mimeType);
|
2010-04-19 19:08:02 +02:00
|
|
|
}
|
2016-07-21 08:35:30 +02:00
|
|
|
return QString();
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HelpViewer::launchWithExternalApp(const QUrl &url)
|
|
|
|
|
{
|
|
|
|
|
if (isLocalUrl(url)) {
|
2010-06-11 13:11:37 +02:00
|
|
|
const QHelpEngineCore &helpEngine = LocalHelpManager::helpEngine();
|
2010-03-29 19:16:23 +02:00
|
|
|
const QUrl &resolvedUrl = helpEngine.findFile(url);
|
|
|
|
|
if (!resolvedUrl.isValid())
|
|
|
|
|
return false;
|
2018-11-16 08:58:03 +01:00
|
|
|
// Workaround QTBUG-71833
|
|
|
|
|
// QHelpEngineCore::findFile returns a valid url even though the file does not exist
|
|
|
|
|
if (resolvedUrl.scheme() == "about" && resolvedUrl.path() == "blank")
|
|
|
|
|
return false;
|
2010-03-29 19:16:23 +02:00
|
|
|
|
|
|
|
|
const QString& path = resolvedUrl.path();
|
|
|
|
|
if (!canOpenPage(path)) {
|
2017-01-19 16:44:22 +01:00
|
|
|
Utils::TempFileSaver saver(Utils::TemporaryDirectory::masterDirectoryPath()
|
2016-07-20 10:11:35 +02:00
|
|
|
+ "/qtchelp_XXXXXX." + QFileInfo(path).completeSuffix());
|
2011-12-09 09:41:40 +01:00
|
|
|
saver.setAutoRemove(false);
|
2011-03-30 15:15:15 +02:00
|
|
|
if (!saver.hasError())
|
|
|
|
|
saver.write(helpEngine.fileData(resolvedUrl));
|
2020-06-02 09:10:40 +02:00
|
|
|
if (saver.finalize(Core::ICore::dialogParent()))
|
2021-05-18 09:47:07 +02:00
|
|
|
QDesktopServices::openUrl(QUrl(saver.filePath().toString()));
|
2013-10-18 08:43:10 +02:00
|
|
|
return true;
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
2013-10-18 08:43:10 +02:00
|
|
|
return false;
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
2013-10-18 08:43:10 +02:00
|
|
|
QDesktopServices::openUrl(url);
|
|
|
|
|
return true;
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelpViewer::home()
|
|
|
|
|
{
|
2014-10-22 14:47:04 +02:00
|
|
|
setSource(LocalHelpManager::homePage());
|
2010-03-29 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2021-01-14 13:21:15 +01:00
|
|
|
void HelpViewer::scaleUp()
|
|
|
|
|
{
|
|
|
|
|
incrementZoom(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelpViewer::scaleDown()
|
|
|
|
|
{
|
|
|
|
|
incrementZoom(-1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelpViewer::resetScale()
|
|
|
|
|
{
|
|
|
|
|
applyZoom(100);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-15 15:30:52 +01:00
|
|
|
void HelpViewer::wheelEvent(QWheelEvent *event)
|
|
|
|
|
{
|
|
|
|
|
if (m_scrollWheelZoomingEnabled && event->modifiers() == Qt::ControlModifier) {
|
|
|
|
|
event->accept();
|
2021-01-14 13:21:15 +01:00
|
|
|
const int deltaY = event->angleDelta().y();
|
|
|
|
|
if (deltaY != 0)
|
|
|
|
|
incrementZoom(deltaY / 120);
|
|
|
|
|
return;
|
2019-02-15 15:30:52 +01:00
|
|
|
}
|
2021-01-14 13:21:15 +01:00
|
|
|
QWidget::wheelEvent(event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelpViewer::incrementZoom(int steps)
|
|
|
|
|
{
|
|
|
|
|
const int incrementPercentage = 10 * steps; // 10 percent increase by single step
|
|
|
|
|
const int previousZoom = LocalHelpManager::fontZoom();
|
|
|
|
|
applyZoom(previousZoom + incrementPercentage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HelpViewer::applyZoom(int percentage)
|
|
|
|
|
{
|
|
|
|
|
const int newZoom = LocalHelpManager::setFontZoom(percentage);
|
2022-07-11 13:30:24 +02:00
|
|
|
Utils::FadingIndicator::showText(this,
|
|
|
|
|
Tr::tr("Zoom: %1%").arg(newZoom),
|
|
|
|
|
Utils::FadingIndicator::SmallText);
|
2019-02-15 15:30:52 +01:00
|
|
|
}
|
|
|
|
|
|
2011-01-11 13:16:12 +01:00
|
|
|
void HelpViewer::slotLoadStarted()
|
|
|
|
|
{
|
2017-08-23 16:53:00 +02:00
|
|
|
++m_loadOverrideStack;
|
2017-04-24 17:01:10 +02:00
|
|
|
QGuiApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
|
2011-01-11 13:16:12 +01:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 16:54:28 +02:00
|
|
|
void HelpViewer::slotLoadFinished()
|
2011-01-11 13:16:12 +01:00
|
|
|
{
|
2017-08-23 16:53:00 +02:00
|
|
|
restoreOverrideCursor();
|
2014-05-02 16:54:28 +02:00
|
|
|
emit sourceChanged(source());
|
|
|
|
|
emit loadFinished();
|
2011-01-11 13:16:12 +01:00
|
|
|
}
|
|
|
|
|
|
2017-08-23 16:53:00 +02:00
|
|
|
void HelpViewer::restoreOverrideCursor()
|
|
|
|
|
{
|
|
|
|
|
while (m_loadOverrideStack > 0) {
|
|
|
|
|
--m_loadOverrideStack;
|
|
|
|
|
QGuiApplication::restoreOverrideCursor();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-29 19:16:23 +02:00
|
|
|
bool HelpViewer::handleForwardBackwardMouseButtons(QMouseEvent *event)
|
|
|
|
|
{
|
|
|
|
|
if (event->button() == Qt::XButton1) {
|
|
|
|
|
backward();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (event->button() == Qt::XButton2) {
|
|
|
|
|
forward();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|