1 | /* $Id: UIHelpViewer.cpp 87158 2021-01-04 11:12:01Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIHelpBrowserWidget class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /* Qt includes: */
|
---|
19 | #include <QClipboard>
|
---|
20 | #include <QComboBox>
|
---|
21 | #include <QDateTime>
|
---|
22 | #include <QDir>
|
---|
23 | #include <QFont>
|
---|
24 | #include <QHBoxLayout>
|
---|
25 | #if defined(RT_OS_LINUX) && defined(VBOX_WITH_DOCS_QHELP) && (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0))
|
---|
26 | #include <QtHelp/QHelpEngine>
|
---|
27 | #include <QtHelp/QHelpContentWidget>
|
---|
28 | #include <QtHelp/QHelpIndexWidget>
|
---|
29 | #include <QtHelp/QHelpSearchEngine>
|
---|
30 | #include <QtHelp/QHelpSearchQueryWidget>
|
---|
31 | #include <QtHelp/QHelpSearchResultWidget>
|
---|
32 | #endif
|
---|
33 | #include <QLabel>
|
---|
34 | #include <QListWidget>
|
---|
35 | #include <QMenu>
|
---|
36 | #include <QMouseEvent>
|
---|
37 | #include <QPainter>
|
---|
38 | #include <QPixmap>
|
---|
39 | #include <QtPrintSupport/QPrintDialog>
|
---|
40 | #include <QtPrintSupport/QPrinter>
|
---|
41 | #include <QPushButton>
|
---|
42 | #include <QSpacerItem>
|
---|
43 | #include <QSplitter>
|
---|
44 | #include <QTextBrowser>
|
---|
45 | #include <QVBoxLayout>
|
---|
46 | #include <QWidgetAction>
|
---|
47 | #ifdef RT_OS_SOLARIS
|
---|
48 | # include <QFontDatabase>
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | /* GUI includes: */
|
---|
52 | #include "UICommon.h"
|
---|
53 | #include "QIAdvancedSlider.h"
|
---|
54 | #include "QIFileDialog.h"
|
---|
55 | #include "QITabWidget.h"
|
---|
56 | #include "QIToolBar.h"
|
---|
57 | #include "QIToolButton.h"
|
---|
58 | #include "UIActionPool.h"
|
---|
59 | #include "UIExtraDataManager.h"
|
---|
60 | #include "UIHelpViewer.h"
|
---|
61 | #include "UIHelpBrowserWidget.h"
|
---|
62 | #include "UIIconPool.h"
|
---|
63 | #include "UIMessageCenter.h"
|
---|
64 | #include "UISearchLineEdit.h"
|
---|
65 |
|
---|
66 |
|
---|
67 | /* COM includes: */
|
---|
68 | #include "CSystemProperties.h"
|
---|
69 |
|
---|
70 | #if defined(RT_OS_LINUX) && defined(VBOX_WITH_DOCS_QHELP) && (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0))
|
---|
71 |
|
---|
72 |
|
---|
73 | /*********************************************************************************************************************************
|
---|
74 | * UIContextMenuNavigationAction definition. *
|
---|
75 | *********************************************************************************************************************************/
|
---|
76 | class UIContextMenuNavigationAction : public QWidgetAction
|
---|
77 | {
|
---|
78 |
|
---|
79 | Q_OBJECT;
|
---|
80 |
|
---|
81 | signals:
|
---|
82 |
|
---|
83 | void sigGoBackward();
|
---|
84 | void sigGoForward();
|
---|
85 | void sigGoHome();
|
---|
86 | void sigAddBookmark();
|
---|
87 |
|
---|
88 | public:
|
---|
89 |
|
---|
90 | UIContextMenuNavigationAction(QObject *pParent = 0);
|
---|
91 | void setBackwardAvailable(bool fAvailable);
|
---|
92 | void setForwardAvailable(bool fAvailable);
|
---|
93 |
|
---|
94 | protected:
|
---|
95 |
|
---|
96 |
|
---|
97 | private slots:
|
---|
98 |
|
---|
99 |
|
---|
100 | private:
|
---|
101 |
|
---|
102 | void prepare();
|
---|
103 | QIToolButton *m_pBackwardButton;
|
---|
104 | QIToolButton *m_pForwardButton;
|
---|
105 | QIToolButton *m_pHomeButton;
|
---|
106 | QIToolButton *m_pAddBookmarkButton;
|
---|
107 | };
|
---|
108 |
|
---|
109 | /*********************************************************************************************************************************
|
---|
110 | * UIFindInPageWidget definition. *
|
---|
111 | *********************************************************************************************************************************/
|
---|
112 | class UIFindInPageWidget : public QIWithRetranslateUI<QWidget>
|
---|
113 | {
|
---|
114 |
|
---|
115 | Q_OBJECT;
|
---|
116 |
|
---|
117 | signals:
|
---|
118 |
|
---|
119 | void sigDragging(const QPoint &delta);
|
---|
120 | void sigSearchTextChanged(const QString &strSearchText);
|
---|
121 | void sigSelectNextMatch();
|
---|
122 | void sigSelectPreviousMatch();
|
---|
123 | void sigClose();
|
---|
124 |
|
---|
125 | public:
|
---|
126 |
|
---|
127 | UIFindInPageWidget(QWidget *pParent = 0);
|
---|
128 | void setMatchCountAndCurrentIndex(int iTotalMatchCount, int iCurrentlyScrolledIndex);
|
---|
129 | void clearSearchField();
|
---|
130 |
|
---|
131 | protected:
|
---|
132 |
|
---|
133 | virtual bool eventFilter(QObject *pObject, QEvent *pEvent) /* override */;
|
---|
134 |
|
---|
135 | private:
|
---|
136 |
|
---|
137 | void prepare();
|
---|
138 | void retranslateUi();
|
---|
139 | UISearchLineEdit *m_pSearchLineEdit;
|
---|
140 | QIToolButton *m_pNextButton;
|
---|
141 | QIToolButton *m_pPreviousButton;
|
---|
142 | QIToolButton *m_pCloseButton;
|
---|
143 | QLabel *m_pDragMoveLabel;
|
---|
144 | QPoint m_previousMousePosition;
|
---|
145 | };
|
---|
146 |
|
---|
147 |
|
---|
148 | /*********************************************************************************************************************************
|
---|
149 | * UIContextMenuNavigationAction implementation. *
|
---|
150 | *********************************************************************************************************************************/
|
---|
151 | UIContextMenuNavigationAction::UIContextMenuNavigationAction(QObject *pParent /* = 0 */)
|
---|
152 | :QWidgetAction(pParent)
|
---|
153 | , m_pBackwardButton(0)
|
---|
154 | , m_pForwardButton(0)
|
---|
155 | , m_pHomeButton(0)
|
---|
156 | , m_pAddBookmarkButton(0)
|
---|
157 | {
|
---|
158 | prepare();
|
---|
159 | }
|
---|
160 |
|
---|
161 | void UIContextMenuNavigationAction::setBackwardAvailable(bool fAvailable)
|
---|
162 | {
|
---|
163 | if (m_pBackwardButton)
|
---|
164 | m_pBackwardButton->setEnabled(fAvailable);
|
---|
165 | }
|
---|
166 |
|
---|
167 | void UIContextMenuNavigationAction::setForwardAvailable(bool fAvailable)
|
---|
168 | {
|
---|
169 | if (m_pForwardButton)
|
---|
170 | m_pForwardButton->setEnabled(fAvailable);
|
---|
171 | }
|
---|
172 |
|
---|
173 | void UIContextMenuNavigationAction::prepare()
|
---|
174 | {
|
---|
175 | QWidget *pWidget = new QWidget;
|
---|
176 | setDefaultWidget(pWidget);
|
---|
177 | QHBoxLayout *pMainLayout = new QHBoxLayout(pWidget);
|
---|
178 | AssertReturnVoid(pMainLayout);
|
---|
179 |
|
---|
180 | m_pBackwardButton = new QIToolButton;
|
---|
181 | m_pForwardButton = new QIToolButton;
|
---|
182 | m_pHomeButton = new QIToolButton;
|
---|
183 | m_pAddBookmarkButton = new QIToolButton;
|
---|
184 |
|
---|
185 | AssertReturnVoid(m_pBackwardButton &&
|
---|
186 | m_pForwardButton &&
|
---|
187 | m_pHomeButton);
|
---|
188 | m_pForwardButton->setEnabled(false);
|
---|
189 | m_pBackwardButton->setEnabled(false);
|
---|
190 | m_pHomeButton->setIcon(UIIconPool::iconSet(":/help_browser_home_32px.png"));
|
---|
191 | m_pForwardButton->setIcon(UIIconPool::iconSet(":/help_browser_forward_32px.png", ":/help_browser_forward_disabled_32px.png"));
|
---|
192 | m_pBackwardButton->setIcon(UIIconPool::iconSet(":/help_browser_backward_32px.png", ":/help_browser_backward_disabled_32px.png"));
|
---|
193 | m_pAddBookmarkButton->setIcon(UIIconPool::iconSet(":/help_browser_add_bookmark.png"));
|
---|
194 |
|
---|
195 | pMainLayout->addWidget(m_pBackwardButton);
|
---|
196 | pMainLayout->addWidget(m_pForwardButton);
|
---|
197 | pMainLayout->addWidget(m_pHomeButton);
|
---|
198 | pMainLayout->addWidget(m_pAddBookmarkButton);
|
---|
199 | pMainLayout->setContentsMargins(0, 0, 0, 0);
|
---|
200 | //pMainLayout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Fixed));
|
---|
201 |
|
---|
202 | connect(m_pBackwardButton, &QIToolButton::pressed,
|
---|
203 | this, &UIContextMenuNavigationAction::sigGoBackward);
|
---|
204 | connect(m_pForwardButton, &QIToolButton::pressed,
|
---|
205 | this, &UIContextMenuNavigationAction::sigGoForward);
|
---|
206 | connect(m_pHomeButton, &QIToolButton::pressed,
|
---|
207 | this, &UIContextMenuNavigationAction::sigGoHome);
|
---|
208 | connect(m_pAddBookmarkButton, &QIToolButton::pressed,
|
---|
209 | this, &UIContextMenuNavigationAction::sigAddBookmark);
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | /*********************************************************************************************************************************
|
---|
214 | * UIFindInPageWidget implementation. *
|
---|
215 | *********************************************************************************************************************************/
|
---|
216 | UIFindInPageWidget::UIFindInPageWidget(QWidget *pParent /* = 0 */)
|
---|
217 | : QIWithRetranslateUI<QWidget>(pParent)
|
---|
218 | , m_pSearchLineEdit(0)
|
---|
219 | , m_pNextButton(0)
|
---|
220 | , m_pPreviousButton(0)
|
---|
221 | , m_pCloseButton(0)
|
---|
222 | , m_previousMousePosition(-1, -1)
|
---|
223 | {
|
---|
224 | prepare();
|
---|
225 | }
|
---|
226 |
|
---|
227 | void UIFindInPageWidget::setMatchCountAndCurrentIndex(int iTotalMatchCount, int iCurrentlyScrolledIndex)
|
---|
228 | {
|
---|
229 | if (!m_pSearchLineEdit)
|
---|
230 | return;
|
---|
231 | m_pSearchLineEdit->setMatchCount(iTotalMatchCount);
|
---|
232 | m_pSearchLineEdit->setScrollToIndex(iCurrentlyScrolledIndex);
|
---|
233 | }
|
---|
234 |
|
---|
235 | void UIFindInPageWidget::clearSearchField()
|
---|
236 | {
|
---|
237 | if (!m_pSearchLineEdit)
|
---|
238 | return;
|
---|
239 | m_pSearchLineEdit->blockSignals(true);
|
---|
240 | m_pSearchLineEdit->reset();
|
---|
241 | m_pSearchLineEdit->blockSignals(false);
|
---|
242 | }
|
---|
243 |
|
---|
244 | bool UIFindInPageWidget::eventFilter(QObject *pObject, QEvent *pEvent)
|
---|
245 | {
|
---|
246 | if (pObject == m_pDragMoveLabel)
|
---|
247 | {
|
---|
248 | if (pEvent->type() == QEvent::Enter)
|
---|
249 | m_pDragMoveLabel->setCursor(Qt::CrossCursor);
|
---|
250 | else if (pEvent->type() == QEvent::Leave)
|
---|
251 | {
|
---|
252 | if (parentWidget())
|
---|
253 | m_pDragMoveLabel->setCursor(parentWidget()->cursor());
|
---|
254 | }
|
---|
255 | else if (pEvent->type() == QEvent::MouseMove)
|
---|
256 | {
|
---|
257 | QMouseEvent *pMouseEvent = static_cast<QMouseEvent*>(pEvent);
|
---|
258 | if (pMouseEvent->buttons() == Qt::LeftButton)
|
---|
259 | {
|
---|
260 | if (m_previousMousePosition != QPoint(-1, -1))
|
---|
261 | emit sigDragging(pMouseEvent->globalPos() - m_previousMousePosition);
|
---|
262 | m_previousMousePosition = pMouseEvent->globalPos();
|
---|
263 | m_pDragMoveLabel->setCursor(Qt::ClosedHandCursor);
|
---|
264 | }
|
---|
265 | }
|
---|
266 | else if (pEvent->type() == QEvent::MouseButtonRelease)
|
---|
267 | {
|
---|
268 | m_previousMousePosition = QPoint(-1, -1);
|
---|
269 | m_pDragMoveLabel->setCursor(Qt::CrossCursor);
|
---|
270 | }
|
---|
271 | }
|
---|
272 | return QIWithRetranslateUI<QWidget>::eventFilter(pObject, pEvent);
|
---|
273 | }
|
---|
274 |
|
---|
275 | void UIFindInPageWidget::prepare()
|
---|
276 | {
|
---|
277 | setAutoFillBackground(true);
|
---|
278 | setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
|
---|
279 |
|
---|
280 | QHBoxLayout *pLayout = new QHBoxLayout(this);
|
---|
281 | m_pSearchLineEdit = new UISearchLineEdit;
|
---|
282 | AssertReturnVoid(pLayout && m_pSearchLineEdit);
|
---|
283 | setFocusProxy(m_pSearchLineEdit);
|
---|
284 | QFontMetrics fontMetric(m_pSearchLineEdit->font());
|
---|
285 | setMinimumSize(40 * fontMetric.width("x"),
|
---|
286 | fontMetric.height() +
|
---|
287 | qApp->style()->pixelMetric(QStyle::PM_LayoutBottomMargin) +
|
---|
288 | qApp->style()->pixelMetric(QStyle::PM_LayoutTopMargin));
|
---|
289 |
|
---|
290 | connect(m_pSearchLineEdit, &UISearchLineEdit::textChanged,
|
---|
291 | this, &UIFindInPageWidget::sigSearchTextChanged);
|
---|
292 |
|
---|
293 | m_pDragMoveLabel = new QLabel;
|
---|
294 | AssertReturnVoid(m_pDragMoveLabel);
|
---|
295 | m_pDragMoveLabel->installEventFilter(this);
|
---|
296 | m_pDragMoveLabel->setPixmap(QPixmap(":/drag_move_16px.png"));
|
---|
297 | pLayout->addWidget(m_pDragMoveLabel);
|
---|
298 |
|
---|
299 |
|
---|
300 | pLayout->setSpacing(0);
|
---|
301 | pLayout->addWidget(m_pSearchLineEdit);
|
---|
302 |
|
---|
303 | m_pPreviousButton = new QIToolButton;
|
---|
304 | m_pNextButton = new QIToolButton;
|
---|
305 | m_pCloseButton = new QIToolButton;
|
---|
306 |
|
---|
307 | pLayout->addWidget(m_pPreviousButton);
|
---|
308 | pLayout->addWidget(m_pNextButton);
|
---|
309 | pLayout->addWidget(m_pCloseButton);
|
---|
310 |
|
---|
311 | m_pPreviousButton->setIcon(UIIconPool::iconSet(":/arrow_up_10px.png"));
|
---|
312 | m_pNextButton->setIcon(UIIconPool::iconSet(":/arrow_down_10px.png"));
|
---|
313 | m_pCloseButton->setIcon(UIIconPool::iconSet(":/close_16px.png"));
|
---|
314 |
|
---|
315 | connect(m_pPreviousButton, &QIToolButton::pressed, this, &UIFindInPageWidget::sigSelectPreviousMatch);
|
---|
316 | connect(m_pNextButton, &QIToolButton::pressed, this, &UIFindInPageWidget::sigSelectNextMatch);
|
---|
317 | connect(m_pCloseButton, &QIToolButton::pressed, this, &UIFindInPageWidget::sigClose);
|
---|
318 | }
|
---|
319 |
|
---|
320 | void UIFindInPageWidget::retranslateUi()
|
---|
321 | {
|
---|
322 | }
|
---|
323 |
|
---|
324 |
|
---|
325 | /*********************************************************************************************************************************
|
---|
326 | * UIHelpViewer implementation. *
|
---|
327 | *********************************************************************************************************************************/
|
---|
328 |
|
---|
329 | UIHelpViewer::UIHelpViewer(const QHelpEngine *pHelpEngine, QWidget *pParent /* = 0 */)
|
---|
330 | :QIWithRetranslateUI<QTextBrowser>(pParent)
|
---|
331 | , m_pHelpEngine(pHelpEngine)
|
---|
332 | , m_pFindInPageWidget(new UIFindInPageWidget(this))
|
---|
333 | , m_fFindWidgetDragged(false)
|
---|
334 | , m_iMarginForFindWidget(qApp->style()->pixelMetric(QStyle::PM_LayoutLeftMargin))
|
---|
335 | , m_iSelectedMatchIndex(0)
|
---|
336 | , m_iSearchTermLength(0)
|
---|
337 | {
|
---|
338 | m_iInitialFontPointSize = font().pointSize();
|
---|
339 | setUndoRedoEnabled(true);
|
---|
340 | connect(m_pFindInPageWidget, &UIFindInPageWidget::sigDragging,
|
---|
341 | this, &UIHelpViewer::sltHandleFindWidgetDrag);
|
---|
342 | connect(m_pFindInPageWidget, &UIFindInPageWidget::sigSearchTextChanged,
|
---|
343 | this, &UIHelpViewer::sltHandleFindInPageSearchTextChange);
|
---|
344 |
|
---|
345 | connect(m_pFindInPageWidget, &UIFindInPageWidget::sigSelectPreviousMatch,
|
---|
346 | this, &UIHelpViewer::sltSelectPreviousMatch);
|
---|
347 | connect(m_pFindInPageWidget, &UIFindInPageWidget::sigSelectNextMatch,
|
---|
348 | this, &UIHelpViewer::sltSelectNextMatch);
|
---|
349 | connect(m_pFindInPageWidget, &UIFindInPageWidget::sigClose,
|
---|
350 | this, &UIHelpViewer::sigCloseFindInPageWidget);
|
---|
351 |
|
---|
352 | m_pFindInPageWidget->setVisible(false);
|
---|
353 | retranslateUi();
|
---|
354 | }
|
---|
355 |
|
---|
356 | QVariant UIHelpViewer::loadResource(int type, const QUrl &name)
|
---|
357 | {
|
---|
358 | if (name.scheme() == "qthelp" && m_pHelpEngine)
|
---|
359 | return QVariant(m_pHelpEngine->fileData(name));
|
---|
360 | else
|
---|
361 | return QTextBrowser::loadResource(type, name);
|
---|
362 | }
|
---|
363 |
|
---|
364 | void UIHelpViewer::emitHistoryChangedSignal()
|
---|
365 | {
|
---|
366 | emit historyChanged();
|
---|
367 | emit backwardAvailable(true);
|
---|
368 | }
|
---|
369 |
|
---|
370 | void UIHelpViewer::setSource(const QUrl &url)
|
---|
371 | {
|
---|
372 | QTextBrowser::setSource(url);
|
---|
373 | QTextDocument *pDocument = document();
|
---|
374 | if (!pDocument || pDocument->isEmpty())
|
---|
375 | {
|
---|
376 | setText(tr("<div><p><h3>404. Not found.</h3>The page <b>%1</b> could not be found.</p></div>").arg(url.toString()));
|
---|
377 | }
|
---|
378 | if (m_pFindInPageWidget && m_pFindInPageWidget->isVisible())
|
---|
379 | {
|
---|
380 | document()->undo();
|
---|
381 | m_pFindInPageWidget->clearSearchField();
|
---|
382 | }
|
---|
383 | }
|
---|
384 |
|
---|
385 | void UIHelpViewer::toggleFindInPageWidget(bool fVisible)
|
---|
386 | {
|
---|
387 | if (!m_pFindInPageWidget)
|
---|
388 | return;
|
---|
389 | /* Try to position the widget somewhere meaningful initially: */
|
---|
390 | if (!m_fFindWidgetDragged)
|
---|
391 | m_pFindInPageWidget->move(width() - m_iMarginForFindWidget - m_pFindInPageWidget->width(),
|
---|
392 | m_iMarginForFindWidget);
|
---|
393 |
|
---|
394 | m_pFindInPageWidget->setVisible(fVisible);
|
---|
395 |
|
---|
396 | if (!fVisible)
|
---|
397 | {
|
---|
398 | document()->undo();
|
---|
399 | m_pFindInPageWidget->clearSearchField();
|
---|
400 | }
|
---|
401 | else
|
---|
402 | m_pFindInPageWidget->setFocus();
|
---|
403 | }
|
---|
404 |
|
---|
405 | int UIHelpViewer::initialFontPointSize() const
|
---|
406 | {
|
---|
407 | return m_iInitialFontPointSize;
|
---|
408 | }
|
---|
409 |
|
---|
410 | void UIHelpViewer::setFont(const QFont &font)
|
---|
411 | {
|
---|
412 | QIWithRetranslateUI<QTextBrowser>::setFont(font);
|
---|
413 | /* Make sure the font size of the find in widget stays constant: */
|
---|
414 | if (m_pFindInPageWidget)
|
---|
415 | {
|
---|
416 | QFont wFont(font);
|
---|
417 | wFont.setPointSize(m_iInitialFontPointSize);
|
---|
418 | m_pFindInPageWidget->setFont(wFont);
|
---|
419 | }
|
---|
420 | }
|
---|
421 |
|
---|
422 | void UIHelpViewer::contextMenuEvent(QContextMenuEvent *event)
|
---|
423 | {
|
---|
424 | QMenu pMenu;
|
---|
425 |
|
---|
426 | UIContextMenuNavigationAction *pNavigationActions = new UIContextMenuNavigationAction;
|
---|
427 | pNavigationActions->setBackwardAvailable(isBackwardAvailable());
|
---|
428 | pNavigationActions->setForwardAvailable(isForwardAvailable());
|
---|
429 |
|
---|
430 | connect(pNavigationActions, &UIContextMenuNavigationAction::sigGoBackward,
|
---|
431 | this, &UIHelpViewer::sigGoBackward);
|
---|
432 | connect(pNavigationActions, &UIContextMenuNavigationAction::sigGoForward,
|
---|
433 | this, &UIHelpViewer::sigGoForward);
|
---|
434 | connect(pNavigationActions, &UIContextMenuNavigationAction::sigGoHome,
|
---|
435 | this, &UIHelpViewer::sigGoHome);
|
---|
436 | connect(pNavigationActions, &UIContextMenuNavigationAction::sigAddBookmark,
|
---|
437 | this, &UIHelpViewer::sigAddBookmark);
|
---|
438 |
|
---|
439 | QAction *pOpenLinkAction = new QAction(UIHelpBrowserWidget::tr("Open Link"));
|
---|
440 | connect(pOpenLinkAction, &QAction::triggered,
|
---|
441 | this, &UIHelpViewer::sltHandleOpenLink);
|
---|
442 |
|
---|
443 | QAction *pOpenInNewTabAction = new QAction(UIHelpBrowserWidget::tr("Open Link in New Tab"));
|
---|
444 | connect(pOpenInNewTabAction, &QAction::triggered,
|
---|
445 | this, &UIHelpViewer::sltHandleOpenLinkInNewTab);
|
---|
446 |
|
---|
447 | QAction *pCopyLink = new QAction(UIHelpBrowserWidget::tr("Copy Link"));
|
---|
448 | connect(pCopyLink, &QAction::triggered,
|
---|
449 | this, &UIHelpViewer::sltHandleCopyLink);
|
---|
450 |
|
---|
451 | pMenu.addAction(pNavigationActions);
|
---|
452 | pMenu.addAction(pOpenLinkAction);
|
---|
453 | pMenu.addAction(pOpenInNewTabAction);
|
---|
454 | pMenu.addAction(pCopyLink);
|
---|
455 |
|
---|
456 | QString strAnchor = anchorAt(event->pos());
|
---|
457 | if (!strAnchor.isEmpty())
|
---|
458 | {
|
---|
459 | QString strLink = source().resolved(anchorAt(event->pos())).toString();
|
---|
460 | pOpenLinkAction->setData(strLink);
|
---|
461 | pOpenInNewTabAction->setData(strLink);
|
---|
462 | pCopyLink->setData(strLink);
|
---|
463 | }
|
---|
464 | else
|
---|
465 | {
|
---|
466 | pOpenLinkAction->setEnabled(false);
|
---|
467 | pOpenInNewTabAction->setEnabled(false);
|
---|
468 | pCopyLink->setEnabled(false);
|
---|
469 | }
|
---|
470 | pMenu.exec(event->globalPos());
|
---|
471 | }
|
---|
472 |
|
---|
473 | void UIHelpViewer::resizeEvent(QResizeEvent *pEvent)
|
---|
474 | {
|
---|
475 | /* Make sure the widget stays inside the parent during parent resize: */
|
---|
476 | if (m_pFindInPageWidget)
|
---|
477 | {
|
---|
478 | if (!isRectInside(m_pFindInPageWidget->geometry(), m_iMarginForFindWidget))
|
---|
479 | moveFindWidgetIn(m_iMarginForFindWidget);
|
---|
480 | }
|
---|
481 | QIWithRetranslateUI<QTextBrowser>::resizeEvent(pEvent);
|
---|
482 | }
|
---|
483 |
|
---|
484 | void UIHelpViewer::wheelEvent(QWheelEvent *pEvent)
|
---|
485 | {
|
---|
486 | int iPreviousSize = font().pointSize();
|
---|
487 | QTextBrowser::wheelEvent(pEvent);
|
---|
488 | /* Don't allow font size to get too large or small: */
|
---|
489 | if (font().pointSize() >= UIHelpBrowserWidget::fontScaleMinMax.second * m_iInitialFontPointSize ||
|
---|
490 | font().pointSize() <= UIHelpBrowserWidget::fontScaleMinMax.first * m_iInitialFontPointSize)
|
---|
491 | {
|
---|
492 | QFont mFont = font();
|
---|
493 | mFont.setPointSize(iPreviousSize);
|
---|
494 | setFont(mFont);
|
---|
495 | }
|
---|
496 | else
|
---|
497 | emit sigFontPointSizeChanged(font().pointSize());
|
---|
498 | }
|
---|
499 |
|
---|
500 | void UIHelpViewer::retranslateUi()
|
---|
501 | {
|
---|
502 | }
|
---|
503 |
|
---|
504 | void UIHelpViewer::moveFindWidgetIn(int iMargin)
|
---|
505 | {
|
---|
506 | if (!m_pFindInPageWidget)
|
---|
507 | return;
|
---|
508 |
|
---|
509 | QRect rect = m_pFindInPageWidget->geometry();
|
---|
510 | if (rect.left() < iMargin)
|
---|
511 | rect.translate(-rect.left() + iMargin, 0);
|
---|
512 | if (rect.right() > width() - iMargin)
|
---|
513 | rect.translate((width() - iMargin - rect.right()), 0);
|
---|
514 | if (rect.top() < iMargin)
|
---|
515 | rect.translate(0, -rect.top() + iMargin);
|
---|
516 |
|
---|
517 | if (rect.bottom() > height() - iMargin)
|
---|
518 | rect.translate(0, (height() - iMargin - rect.bottom()));
|
---|
519 | m_pFindInPageWidget->setGeometry(rect);
|
---|
520 | m_pFindInPageWidget->update();
|
---|
521 | }
|
---|
522 |
|
---|
523 | bool UIHelpViewer::isRectInside(const QRect &rect, int iMargin) const
|
---|
524 | {
|
---|
525 | if (rect.left() < iMargin || rect.top() < iMargin)
|
---|
526 | return false;
|
---|
527 | if (rect.right() > width() - iMargin || rect.bottom() > height() - iMargin)
|
---|
528 | return false;
|
---|
529 | return true;
|
---|
530 | }
|
---|
531 |
|
---|
532 | void UIHelpViewer::findAllMatches(const QString &searchString)
|
---|
533 | {
|
---|
534 | QTextDocument *pDocument = document();
|
---|
535 | AssertReturnVoid(pDocument);
|
---|
536 |
|
---|
537 | m_matchedCursorPosition.clear();
|
---|
538 | if (searchString.isEmpty())
|
---|
539 | return;
|
---|
540 | QTextCursor cursor(pDocument);
|
---|
541 | QTextDocument::FindFlags flags;
|
---|
542 | int iMatchCount = 0;
|
---|
543 | while (!cursor.isNull() && !cursor.atEnd())
|
---|
544 | {
|
---|
545 | cursor = pDocument->find(searchString, cursor, flags);
|
---|
546 | if (!cursor.isNull())
|
---|
547 | {
|
---|
548 | m_matchedCursorPosition << cursor.position() - searchString.length();
|
---|
549 | ++iMatchCount;
|
---|
550 | }
|
---|
551 | }
|
---|
552 | }
|
---|
553 |
|
---|
554 | void UIHelpViewer::highlightFinds(int iSearchTermLength)
|
---|
555 | {
|
---|
556 | QTextDocument* pDocument = document();
|
---|
557 | AssertReturnVoid(pDocument);
|
---|
558 | /* Clear previous highlight: */
|
---|
559 | pDocument->undo();
|
---|
560 |
|
---|
561 | QTextCursor highlightCursor(pDocument);
|
---|
562 | QTextCursor cursor(pDocument);
|
---|
563 | cursor.beginEditBlock();
|
---|
564 | for (int i = 0; i < m_matchedCursorPosition.size(); ++i)
|
---|
565 | {
|
---|
566 | highlightCursor.setPosition(m_matchedCursorPosition[i]);
|
---|
567 |
|
---|
568 | QTextCharFormat colorFormat(highlightCursor.charFormat());
|
---|
569 | colorFormat.setBackground(Qt::yellow);
|
---|
570 |
|
---|
571 | highlightCursor.setPosition(m_matchedCursorPosition[i] + iSearchTermLength, QTextCursor::KeepAnchor);
|
---|
572 | if (!highlightCursor.isNull())
|
---|
573 | highlightCursor.setCharFormat(colorFormat);
|
---|
574 | }
|
---|
575 | cursor.endEditBlock();
|
---|
576 | }
|
---|
577 |
|
---|
578 | void UIHelpViewer::selectMatch(int iMatchIndex, int iSearchStringLength)
|
---|
579 | {
|
---|
580 | QTextCursor cursor = textCursor();
|
---|
581 | /* Move the cursor to the beginning of the matched string: */
|
---|
582 | cursor.setPosition(m_matchedCursorPosition.at(iMatchIndex), QTextCursor::MoveAnchor);
|
---|
583 | /* Move the cursor to the end of the matched string while keeping the anchor at the begining thus selecting the text: */
|
---|
584 | cursor.setPosition(m_matchedCursorPosition.at(iMatchIndex) + iSearchStringLength, QTextCursor::KeepAnchor);
|
---|
585 | ensureCursorVisible();
|
---|
586 | setTextCursor(cursor);
|
---|
587 | }
|
---|
588 |
|
---|
589 | void UIHelpViewer::sltHandleOpenLinkInNewTab()
|
---|
590 | {
|
---|
591 | QAction *pSender = qobject_cast<QAction*>(sender());
|
---|
592 | if (!pSender)
|
---|
593 | return;
|
---|
594 | QUrl url = pSender->data().toUrl();
|
---|
595 | if (url.isValid())
|
---|
596 | emit sigOpenLinkInNewTab(url);
|
---|
597 | }
|
---|
598 |
|
---|
599 | void UIHelpViewer::sltHandleOpenLink()
|
---|
600 | {
|
---|
601 | QAction *pSender = qobject_cast<QAction*>(sender());
|
---|
602 | if (!pSender)
|
---|
603 | return;
|
---|
604 | QUrl url = pSender->data().toUrl();
|
---|
605 | if (url.isValid())
|
---|
606 | QTextBrowser::setSource(url);
|
---|
607 | }
|
---|
608 |
|
---|
609 | void UIHelpViewer::sltHandleCopyLink()
|
---|
610 | {
|
---|
611 | QAction *pSender = qobject_cast<QAction*>(sender());
|
---|
612 | if (!pSender)
|
---|
613 | return;
|
---|
614 | QUrl url = pSender->data().toUrl();
|
---|
615 | if (url.isValid())
|
---|
616 | {
|
---|
617 | QClipboard *pClipboard = QApplication::clipboard();
|
---|
618 | if (pClipboard)
|
---|
619 | pClipboard->setText(url.toString());
|
---|
620 | }
|
---|
621 | }
|
---|
622 |
|
---|
623 | void UIHelpViewer::sltHandleFindWidgetDrag(const QPoint &delta)
|
---|
624 | {
|
---|
625 | if (!m_pFindInPageWidget)
|
---|
626 | return;
|
---|
627 | QRect geo = m_pFindInPageWidget->geometry();
|
---|
628 | geo.translate(delta);
|
---|
629 |
|
---|
630 | /* Allow the move if m_pFindInPageWidget stays inside after the move: */
|
---|
631 | if (isRectInside(geo, m_iMarginForFindWidget))
|
---|
632 | m_pFindInPageWidget->move(m_pFindInPageWidget->pos() + delta);
|
---|
633 | m_fFindWidgetDragged = true;
|
---|
634 | update();
|
---|
635 | }
|
---|
636 |
|
---|
637 | void UIHelpViewer::sltHandleFindInPageSearchTextChange(const QString &strSearchText)
|
---|
638 | {
|
---|
639 | m_iSearchTermLength = strSearchText.length();
|
---|
640 | findAllMatches(strSearchText);
|
---|
641 | highlightFinds(m_iSearchTermLength);
|
---|
642 | //scrollToMatch(int iMatchIndex);
|
---|
643 | selectMatch(0, m_iSearchTermLength);
|
---|
644 | if (m_pFindInPageWidget)
|
---|
645 | m_pFindInPageWidget->setMatchCountAndCurrentIndex(m_matchedCursorPosition.size(), 0);
|
---|
646 | }
|
---|
647 |
|
---|
648 | void UIHelpViewer::sltSelectPreviousMatch()
|
---|
649 | {
|
---|
650 | m_iSelectedMatchIndex = m_iSelectedMatchIndex <= 0 ? m_matchedCursorPosition.size() - 1 : (m_iSelectedMatchIndex - 1);
|
---|
651 | selectMatch(m_iSelectedMatchIndex, m_iSearchTermLength);
|
---|
652 | if (m_pFindInPageWidget)
|
---|
653 | m_pFindInPageWidget->setMatchCountAndCurrentIndex(m_matchedCursorPosition.size(), m_iSelectedMatchIndex);
|
---|
654 | }
|
---|
655 |
|
---|
656 | void UIHelpViewer::sltSelectNextMatch()
|
---|
657 | {
|
---|
658 | m_iSelectedMatchIndex = m_iSelectedMatchIndex >= m_matchedCursorPosition.size() - 1 ? 0 : (m_iSelectedMatchIndex + 1);
|
---|
659 | selectMatch(m_iSelectedMatchIndex, m_iSearchTermLength);
|
---|
660 | if (m_pFindInPageWidget)
|
---|
661 | m_pFindInPageWidget->setMatchCountAndCurrentIndex(m_matchedCursorPosition.size(), m_iSelectedMatchIndex);
|
---|
662 | }
|
---|
663 |
|
---|
664 | #include "UIHelpViewer.moc"
|
---|
665 |
|
---|
666 | #endif /*#if defined(RT_OS_LINUX) && defined(VBOX_WITH_DOCS_QHELP) && (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0))*/
|
---|