VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/helpbrowser/UIHelpViewer.cpp@ 99910

Last change on this file since 99910 was 99910, checked in by vboxsync, 19 months ago

FE/Qt: bugref:10451. Removing endif guards and some more refactoring.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 35.2 KB
Line 
1/* $Id: UIHelpViewer.cpp 99910 2023-05-22 17:15:24Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIHelpViewer class implementation.
4 */
5
6/*
7 * Copyright (C) 2010-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28/* Qt includes: */
29#include <QClipboard>
30#include <QtGlobal>
31#include <QtHelp/QHelpEngine>
32#include <QtHelp/QHelpContentWidget>
33#include <QtHelp/QHelpIndexWidget>
34#include <QtHelp/QHelpSearchEngine>
35#include <QtHelp/QHelpSearchQueryWidget>
36#include <QtHelp/QHelpSearchResultWidget>
37#include <QLabel>
38#include <QMenu>
39#include <QHBoxLayout>
40#include <QGraphicsBlurEffect>
41#include <QLabel>
42#include <QMimeDatabase>
43#include <QPainter>
44#include <QScrollBar>
45#include <QTextBlock>
46#include <QWidgetAction>
47#ifdef RT_OS_SOLARIS
48# include <QFontDatabase>
49#endif
50
51/* GUI includes: */
52#include "QIToolButton.h"
53#include "UICursor.h"
54#include "UICommon.h"
55#include "UIHelpViewer.h"
56#include "UIHelpBrowserWidget.h"
57#include "UIIconPool.h"
58#include "UISearchLineEdit.h"
59
60/* COM includes: */
61#include "COMEnums.h"
62#include "CSystemProperties.h"
63
64
65/*********************************************************************************************************************************
66* UIContextMenuNavigationAction definition. *
67*********************************************************************************************************************************/
68class UIContextMenuNavigationAction : public QWidgetAction
69{
70
71 Q_OBJECT;
72
73signals:
74
75 void sigGoBackward();
76 void sigGoForward();
77 void sigGoHome();
78 void sigReloadPage();
79 void sigAddBookmark();
80
81public:
82
83 UIContextMenuNavigationAction(QObject *pParent = 0);
84 void setBackwardAvailable(bool fAvailable);
85 void setForwardAvailable(bool fAvailable);
86
87private slots:
88
89 void sltGoBackward();
90 void sltGoForward();
91 void sltGoHome();
92 void sltReloadPage();
93 void sltAddBookmark();
94
95private:
96
97 void prepare();
98 QIToolButton *m_pBackwardButton;
99 QIToolButton *m_pForwardButton;
100 QIToolButton *m_pHomeButton;
101 QIToolButton *m_pReloadPageButton;
102 QIToolButton *m_pAddBookmarkButton;
103};
104
105/*********************************************************************************************************************************
106* UIFindInPageWidget definition. *
107*********************************************************************************************************************************/
108class UIFindInPageWidget : public QIWithRetranslateUI<QWidget>
109{
110
111 Q_OBJECT;
112
113signals:
114
115 void sigDragging(const QPoint &delta);
116 void sigSearchTextChanged(const QString &strSearchText);
117 void sigSelectNextMatch();
118 void sigSelectPreviousMatch();
119 void sigClose();
120
121public:
122
123 UIFindInPageWidget(QWidget *pParent = 0);
124 void setMatchCountAndCurrentIndex(int iTotalMatchCount, int iCurrentlyScrolledIndex);
125 void clearSearchField();
126
127protected:
128
129 virtual bool eventFilter(QObject *pObject, QEvent *pEvent) RT_OVERRIDE;
130 virtual void keyPressEvent(QKeyEvent *pEvent) RT_OVERRIDE;
131
132private:
133
134 void prepare();
135 void retranslateUi();
136 UISearchLineEdit *m_pSearchLineEdit;
137 QIToolButton *m_pNextButton;
138 QIToolButton *m_pPreviousButton;
139 QIToolButton *m_pCloseButton;
140 QLabel *m_pDragMoveLabel;
141 QPoint m_previousMousePosition;
142};
143
144
145/*********************************************************************************************************************************
146* UIContextMenuNavigationAction implementation. *
147*********************************************************************************************************************************/
148UIContextMenuNavigationAction::UIContextMenuNavigationAction(QObject *pParent /* = 0 */)
149 :QWidgetAction(pParent)
150 , m_pBackwardButton(0)
151 , m_pForwardButton(0)
152 , m_pHomeButton(0)
153 , m_pReloadPageButton(0)
154 , m_pAddBookmarkButton(0)
155{
156 prepare();
157}
158
159void UIContextMenuNavigationAction::setBackwardAvailable(bool fAvailable)
160{
161 if (m_pBackwardButton)
162 m_pBackwardButton->setEnabled(fAvailable);
163}
164
165void UIContextMenuNavigationAction::setForwardAvailable(bool fAvailable)
166{
167 if (m_pForwardButton)
168 m_pForwardButton->setEnabled(fAvailable);
169}
170
171void UIContextMenuNavigationAction::sltGoBackward()
172{
173 emit sigGoBackward();
174 emit triggered();
175}
176
177void UIContextMenuNavigationAction::sltGoForward()
178{
179 emit sigGoForward();
180 emit triggered();
181}
182
183void UIContextMenuNavigationAction::sltGoHome()
184{
185 emit sigGoHome();
186 emit triggered();
187}
188
189void UIContextMenuNavigationAction::sltReloadPage()
190{
191 emit sigReloadPage();
192 emit triggered();
193}
194
195void UIContextMenuNavigationAction::sltAddBookmark()
196{
197 emit sigAddBookmark();
198 emit triggered();
199}
200
201void UIContextMenuNavigationAction::prepare()
202{
203 QWidget *pWidget = new QWidget;
204 setDefaultWidget(pWidget);
205 QHBoxLayout *pMainLayout = new QHBoxLayout(pWidget);
206 AssertReturnVoid(pMainLayout);
207
208 m_pBackwardButton = new QIToolButton;
209 m_pForwardButton = new QIToolButton;
210 m_pHomeButton = new QIToolButton;
211 m_pReloadPageButton = new QIToolButton;
212 m_pAddBookmarkButton = new QIToolButton;
213
214 AssertReturnVoid(m_pBackwardButton &&
215 m_pForwardButton &&
216 m_pHomeButton &&
217 m_pReloadPageButton);
218
219 m_pForwardButton->setEnabled(false);
220 m_pBackwardButton->setEnabled(false);
221 m_pHomeButton->setIcon(UIIconPool::iconSet(":/help_browser_home_16px.png", ":/help_browser_home_disabled_16px.png"));
222 m_pReloadPageButton->setIcon(UIIconPool::iconSet(":/help_browser_reload_16px.png", ":/help_browser_reload_disabled_16px.png"));
223 m_pForwardButton->setIcon(UIIconPool::iconSet(":/help_browser_forward_16px.png", ":/help_browser_forward_disabled_16px.png"));
224 m_pBackwardButton->setIcon(UIIconPool::iconSet(":/help_browser_backward_16px.png", ":/help_browser_backward_disabled_16px.png"));
225 m_pAddBookmarkButton->setIcon(UIIconPool::iconSet(":/help_browser_add_bookmark_16px.png", ":/help_browser_add_bookmark_disabled_16px.png"));
226
227 m_pHomeButton->setToolTip(UIHelpBrowserWidget::tr("Return to Start Page"));
228 m_pReloadPageButton->setToolTip(UIHelpBrowserWidget::tr("Reload the Current Page"));
229 m_pForwardButton->setToolTip(UIHelpBrowserWidget::tr("Go Forward to Next Page"));
230 m_pBackwardButton->setToolTip(UIHelpBrowserWidget::tr("Go Back to Previous Page"));
231 m_pAddBookmarkButton->setToolTip(UIHelpBrowserWidget::tr("Add a New Bookmark"));
232
233 pMainLayout->addWidget(m_pBackwardButton);
234 pMainLayout->addWidget(m_pForwardButton);
235 pMainLayout->addWidget(m_pHomeButton);
236 pMainLayout->addWidget(m_pReloadPageButton);
237 pMainLayout->addWidget(m_pAddBookmarkButton);
238 pMainLayout->setContentsMargins(0, 0, 0, 0);
239
240 connect(m_pBackwardButton, &QIToolButton::pressed,
241 this, &UIContextMenuNavigationAction::sltGoBackward);
242 connect(m_pForwardButton, &QIToolButton::pressed,
243 this, &UIContextMenuNavigationAction::sltGoForward);
244 connect(m_pHomeButton, &QIToolButton::pressed,
245 this, &UIContextMenuNavigationAction::sltGoHome);
246 connect(m_pReloadPageButton, &QIToolButton::pressed,
247 this, &UIContextMenuNavigationAction::sltReloadPage);
248 connect(m_pAddBookmarkButton, &QIToolButton::pressed,
249 this, &UIContextMenuNavigationAction::sltAddBookmark);
250 connect(m_pReloadPageButton, &QIToolButton::pressed,
251 this, &UIContextMenuNavigationAction::sltAddBookmark);
252}
253
254
255/*********************************************************************************************************************************
256* UIFindInPageWidget implementation. *
257*********************************************************************************************************************************/
258UIFindInPageWidget::UIFindInPageWidget(QWidget *pParent /* = 0 */)
259 : QIWithRetranslateUI<QWidget>(pParent)
260 , m_pSearchLineEdit(0)
261 , m_pNextButton(0)
262 , m_pPreviousButton(0)
263 , m_pCloseButton(0)
264 , m_previousMousePosition(-1, -1)
265{
266 prepare();
267}
268
269void UIFindInPageWidget::setMatchCountAndCurrentIndex(int iTotalMatchCount, int iCurrentlyScrolledIndex)
270{
271 if (!m_pSearchLineEdit)
272 return;
273 m_pSearchLineEdit->setMatchCount(iTotalMatchCount);
274 m_pSearchLineEdit->setScrollToIndex(iCurrentlyScrolledIndex);
275}
276
277void UIFindInPageWidget::clearSearchField()
278{
279 if (!m_pSearchLineEdit)
280 return;
281 m_pSearchLineEdit->blockSignals(true);
282 m_pSearchLineEdit->reset();
283 m_pSearchLineEdit->blockSignals(false);
284}
285
286bool UIFindInPageWidget::eventFilter(QObject *pObject, QEvent *pEvent)
287{
288 if (pObject == m_pDragMoveLabel)
289 {
290 if (pEvent->type() == QEvent::Enter)
291 UICursor::setCursor(m_pDragMoveLabel, Qt::CrossCursor);
292 else if (pEvent->type() == QEvent::Leave)
293 {
294 if (parentWidget())
295 UICursor::setCursor(m_pDragMoveLabel, parentWidget()->cursor());
296 }
297 else if (pEvent->type() == QEvent::MouseMove)
298 {
299 QMouseEvent *pMouseEvent = static_cast<QMouseEvent*>(pEvent);
300 if (pMouseEvent->buttons() == Qt::LeftButton)
301 {
302 if (m_previousMousePosition != QPoint(-1, -1))
303 emit sigDragging(pMouseEvent->globalPos() - m_previousMousePosition);
304 m_previousMousePosition = pMouseEvent->globalPos();
305 UICursor::setCursor(m_pDragMoveLabel, Qt::ClosedHandCursor);
306 }
307 }
308 else if (pEvent->type() == QEvent::MouseButtonRelease)
309 {
310 m_previousMousePosition = QPoint(-1, -1);
311 UICursor::setCursor(m_pDragMoveLabel, Qt::CrossCursor);
312 }
313 }
314 return QIWithRetranslateUI<QWidget>::eventFilter(pObject, pEvent);
315}
316
317void UIFindInPageWidget::keyPressEvent(QKeyEvent *pEvent)
318{
319 switch (pEvent->key())
320 {
321 case Qt::Key_Escape:
322 emit sigClose();
323 return;
324 break;
325 case Qt::Key_Down:
326 emit sigSelectNextMatch();
327 return;
328 break;
329 case Qt::Key_Up:
330 emit sigSelectPreviousMatch();
331 return;
332 break;
333 default:
334 QIWithRetranslateUI<QWidget>::keyPressEvent(pEvent);
335 break;
336 }
337}
338
339void UIFindInPageWidget::prepare()
340{
341 setAutoFillBackground(true);
342 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
343
344 QHBoxLayout *pLayout = new QHBoxLayout(this);
345 m_pSearchLineEdit = new UISearchLineEdit;
346 AssertReturnVoid(pLayout && m_pSearchLineEdit);
347 setFocusProxy(m_pSearchLineEdit);
348 QFontMetrics fontMetric(m_pSearchLineEdit->font());
349#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
350 setMinimumSize(40 * fontMetric.horizontalAdvance("x"),
351 fontMetric.height() +
352 qApp->style()->pixelMetric(QStyle::PM_LayoutBottomMargin) +
353 qApp->style()->pixelMetric(QStyle::PM_LayoutTopMargin));
354
355#else
356 setMinimumSize(40 * fontMetric.width("x"),
357 fontMetric.height() +
358 qApp->style()->pixelMetric(QStyle::PM_LayoutBottomMargin) +
359 qApp->style()->pixelMetric(QStyle::PM_LayoutTopMargin));
360#endif
361 connect(m_pSearchLineEdit, &UISearchLineEdit::textChanged,
362 this, &UIFindInPageWidget::sigSearchTextChanged);
363
364 m_pDragMoveLabel = new QLabel;
365 AssertReturnVoid(m_pDragMoveLabel);
366 m_pDragMoveLabel->installEventFilter(this);
367 m_pDragMoveLabel->setPixmap(QPixmap(":/drag_move_16px.png"));
368 pLayout->addWidget(m_pDragMoveLabel);
369
370
371 pLayout->setSpacing(0);
372 pLayout->addWidget(m_pSearchLineEdit);
373
374 m_pPreviousButton = new QIToolButton;
375 m_pNextButton = new QIToolButton;
376 m_pCloseButton = new QIToolButton;
377
378 pLayout->addWidget(m_pPreviousButton);
379 pLayout->addWidget(m_pNextButton);
380 pLayout->addWidget(m_pCloseButton);
381
382 m_pPreviousButton->setIcon(UIIconPool::iconSet(":/arrow_up_10px.png"));
383 m_pNextButton->setIcon(UIIconPool::iconSet(":/arrow_down_10px.png"));
384 m_pCloseButton->setIcon(UIIconPool::iconSet(":/close_16px.png"));
385
386 connect(m_pPreviousButton, &QIToolButton::pressed, this, &UIFindInPageWidget::sigSelectPreviousMatch);
387 connect(m_pNextButton, &QIToolButton::pressed, this, &UIFindInPageWidget::sigSelectNextMatch);
388 connect(m_pCloseButton, &QIToolButton::pressed, this, &UIFindInPageWidget::sigClose);
389}
390
391void UIFindInPageWidget::retranslateUi()
392{
393}
394
395
396/*********************************************************************************************************************************
397* UIHelpViewer implementation. *
398*********************************************************************************************************************************/
399
400UIHelpViewer::UIHelpViewer(const QHelpEngine *pHelpEngine, QWidget *pParent /* = 0 */)
401 :QIWithRetranslateUI<QTextBrowser>(pParent)
402 , m_pHelpEngine(pHelpEngine)
403 , m_pFindInPageWidget(new UIFindInPageWidget(this))
404 , m_fFindWidgetDragged(false)
405 , m_iMarginForFindWidget(qApp->style()->pixelMetric(QStyle::PM_LayoutLeftMargin))
406 , m_iSelectedMatchIndex(0)
407 , m_iSearchTermLength(0)
408 , m_fOverlayMode(false)
409 , m_pOverlayLabel(0)
410 , m_iZoomPercentage(100)
411{
412 m_iInitialFontPointSize = font().pointSize();
413 setUndoRedoEnabled(true);
414 connect(m_pFindInPageWidget, &UIFindInPageWidget::sigDragging,
415 this, &UIHelpViewer::sltFindWidgetDrag);
416 connect(m_pFindInPageWidget, &UIFindInPageWidget::sigSearchTextChanged,
417 this, &UIHelpViewer::sltFindInPageSearchTextChange);
418
419 connect(m_pFindInPageWidget, &UIFindInPageWidget::sigSelectPreviousMatch,
420 this, &UIHelpViewer::sltSelectPreviousMatch);
421 connect(m_pFindInPageWidget, &UIFindInPageWidget::sigSelectNextMatch,
422 this, &UIHelpViewer::sltSelectNextMatch);
423 connect(m_pFindInPageWidget, &UIFindInPageWidget::sigClose,
424 this, &UIHelpViewer::sltCloseFindInPageWidget);
425
426 m_pFindInPageWidget->setVisible(false);
427
428 m_pOverlayLabel = new QLabel(this);
429 if (m_pOverlayLabel)
430 {
431 m_pOverlayLabel->hide();
432 m_pOverlayLabel->installEventFilter(this);
433 }
434
435 m_pOverlayBlurEffect = new QGraphicsBlurEffect(this);
436 if (m_pOverlayBlurEffect)
437 {
438 viewport()->setGraphicsEffect(m_pOverlayBlurEffect);
439 m_pOverlayBlurEffect->setEnabled(false);
440 m_pOverlayBlurEffect->setBlurRadius(8);
441 }
442 retranslateUi();
443}
444
445QVariant UIHelpViewer::loadResource(int type, const QUrl &name)
446{
447 if (name.scheme() == "qthelp" && m_pHelpEngine)
448 return QVariant(m_pHelpEngine->fileData(name));
449 else
450 return QTextBrowser::loadResource(type, name);
451}
452
453void UIHelpViewer::emitHistoryChangedSignal()
454{
455 emit historyChanged();
456 emit backwardAvailable(true);
457}
458
459#ifdef VBOX_IS_QT6_OR_LATER /* it was setSource before 6.0 */
460void UIHelpViewer::doSetSource(const QUrl &url, QTextDocument::ResourceType type)
461#else
462void UIHelpViewer::setSource(const QUrl &url)
463#endif
464{
465 clearOverlay();
466 if (url.scheme() != "qthelp")
467 return;
468#ifdef VBOX_IS_QT6_OR_LATER /* it was setSource before 6.0 */
469 QTextBrowser::doSetSource(url, type);
470#else
471 QTextBrowser::setSource(url);
472#endif
473 QTextDocument *pDocument = document();
474 if (!pDocument || pDocument->isEmpty())
475 {
476 setText(UIHelpBrowserWidget::tr("<div><p><h3>Not found.</h3>The page <b>%1</b> could not be found.</p></div>").arg(url.toString()));
477 setDocumentTitle(UIHelpBrowserWidget::tr("Not Found"));
478 }
479 if (m_pFindInPageWidget && m_pFindInPageWidget->isVisible())
480 {
481 document()->undo();
482 m_pFindInPageWidget->clearSearchField();
483 }
484 iterateDocumentImages();
485 scaleImages();
486}
487
488void UIHelpViewer::toggleFindInPageWidget(bool fVisible)
489{
490 if (!m_pFindInPageWidget)
491 return;
492
493 /* Closing the find in page widget causes QTextBrowser to jump to the top of the document. This hack puts it back into position: */
494 int iPosition = verticalScrollBar()->value();
495 m_iMarginForFindWidget = verticalScrollBar()->width() +
496 qApp->style()->pixelMetric(QStyle::PM_LayoutLeftMargin);
497 /* Try to position the widget somewhere meaningful initially: */
498 if (!m_fFindWidgetDragged)
499 m_pFindInPageWidget->move(width() - m_iMarginForFindWidget - m_pFindInPageWidget->width(),
500 m_iMarginForFindWidget);
501
502 m_pFindInPageWidget->setVisible(fVisible);
503
504 if (!fVisible)
505 {
506 /* Clear highlights: */
507 setExtraSelections(QList<QTextEdit::ExtraSelection>());
508 m_pFindInPageWidget->clearSearchField();
509 verticalScrollBar()->setValue(iPosition);
510 }
511 else
512 m_pFindInPageWidget->setFocus();
513 emit sigFindInPageWidgetToogle(fVisible);
514}
515
516void UIHelpViewer::reload()
517{
518 setSource(source());
519}
520
521void UIHelpViewer::sltToggleFindInPageWidget(bool fVisible)
522{
523 clearOverlay();
524 toggleFindInPageWidget(fVisible);
525}
526
527void UIHelpViewer::sltCloseFindInPageWidget()
528{
529 sltToggleFindInPageWidget(false);
530}
531
532void UIHelpViewer::setFont(const QFont &font)
533{
534 QIWithRetranslateUI<QTextBrowser>::setFont(font);
535 /* Make sure the font size of the find in widget stays constant: */
536 if (m_pFindInPageWidget)
537 {
538 QFont wFont(font);
539 wFont.setPointSize(m_iInitialFontPointSize);
540 m_pFindInPageWidget->setFont(wFont);
541 }
542}
543
544bool UIHelpViewer::isFindInPageWidgetVisible() const
545{
546 if (m_pFindInPageWidget)
547 return m_pFindInPageWidget->isVisible();
548 return false;
549}
550
551void UIHelpViewer::setZoomPercentage(int iZoomPercentage)
552{
553 m_iZoomPercentage = iZoomPercentage;
554 clearOverlay();
555 scaleFont();
556 scaleImages();
557}
558
559void UIHelpViewer::setHelpFileList(const QList<QUrl> &helpFileList)
560{
561 m_helpFileList = helpFileList;
562 /* File list necessary to get the image data from the help engine: */
563 iterateDocumentImages();
564 scaleImages();
565}
566
567bool UIHelpViewer::hasSelectedText() const
568{
569 return textCursor().hasSelection();
570}
571
572void UIHelpViewer::contextMenuEvent(QContextMenuEvent *event)
573{
574 QMenu menu;
575
576 if (textCursor().hasSelection())
577 {
578 QAction *pCopySelectedTextAction = new QAction(UIHelpBrowserWidget::tr("Copy Selected Text"));
579 connect(pCopySelectedTextAction, &QAction::triggered,
580 this, &UIHelpViewer::copy);
581 menu.addAction(pCopySelectedTextAction);
582 menu.addSeparator();
583 }
584
585 UIContextMenuNavigationAction *pNavigationActions = new UIContextMenuNavigationAction;
586 pNavigationActions->setBackwardAvailable(isBackwardAvailable());
587 pNavigationActions->setForwardAvailable(isForwardAvailable());
588
589 connect(pNavigationActions, &UIContextMenuNavigationAction::sigGoBackward,
590 this, &UIHelpViewer::sigGoBackward);
591 connect(pNavigationActions, &UIContextMenuNavigationAction::sigGoForward,
592 this, &UIHelpViewer::sigGoForward);
593 connect(pNavigationActions, &UIContextMenuNavigationAction::sigGoHome,
594 this, &UIHelpViewer::sigGoHome);
595 connect(pNavigationActions, &UIContextMenuNavigationAction::sigReloadPage,
596 this, &UIHelpViewer::reload);
597 connect(pNavigationActions, &UIContextMenuNavigationAction::sigAddBookmark,
598 this, &UIHelpViewer::sigAddBookmark);
599
600 QAction *pOpenLinkAction = new QAction(UIHelpBrowserWidget::tr("Open Link"));
601 connect(pOpenLinkAction, &QAction::triggered,
602 this, &UIHelpViewer::sltOpenLink);
603
604 QAction *pOpenInNewTabAction = new QAction(UIHelpBrowserWidget::tr("Open Link in New Tab"));
605 connect(pOpenInNewTabAction, &QAction::triggered,
606 this, &UIHelpViewer::sltOpenLinkInNewTab);
607
608 QAction *pCopyLink = new QAction(UIHelpBrowserWidget::tr("Copy Link"));
609 connect(pCopyLink, &QAction::triggered,
610 this, &UIHelpViewer::sltCopyLink);
611
612 QAction *pFindInPage = new QAction(UIHelpBrowserWidget::tr("Find in Page"));
613 pFindInPage->setCheckable(true);
614 if (m_pFindInPageWidget)
615 pFindInPage->setChecked(m_pFindInPageWidget->isVisible());
616 connect(pFindInPage, &QAction::toggled, this, &UIHelpViewer::sltToggleFindInPageWidget);
617
618 menu.addAction(pNavigationActions);
619 menu.addAction(pOpenLinkAction);
620 menu.addAction(pOpenInNewTabAction);
621 menu.addAction(pCopyLink);
622 menu.addAction(pFindInPage);
623
624 QString strAnchor = anchorAt(event->pos());
625 if (!strAnchor.isEmpty())
626 {
627 QString strLink = source().resolved(anchorAt(event->pos())).toString();
628 pOpenLinkAction->setData(strLink);
629 pOpenInNewTabAction->setData(strLink);
630 pCopyLink->setData(strLink);
631 }
632 else
633 {
634 pOpenLinkAction->setEnabled(false);
635 pOpenInNewTabAction->setEnabled(false);
636 pCopyLink->setEnabled(false);
637 }
638
639 menu.exec(event->globalPos());
640}
641
642void UIHelpViewer::resizeEvent(QResizeEvent *pEvent)
643{
644 if (m_fOverlayMode)
645 clearOverlay();
646 /* Make sure the widget stays inside the parent during parent resize: */
647 if (m_pFindInPageWidget)
648 {
649 if (!isRectInside(m_pFindInPageWidget->geometry(), m_iMarginForFindWidget))
650 moveFindWidgetIn(m_iMarginForFindWidget);
651 }
652 QIWithRetranslateUI<QTextBrowser>::resizeEvent(pEvent);
653}
654
655void UIHelpViewer::wheelEvent(QWheelEvent *pEvent)
656{
657 if (m_fOverlayMode && !pEvent)
658 return;
659 /* QTextBrowser::wheelEvent scales font when some modifiers are pressed. We dont want that: */
660 if (pEvent->modifiers() == Qt::NoModifier)
661 QTextBrowser::wheelEvent(pEvent);
662 else if (pEvent->modifiers() & Qt::ControlModifier)
663 {
664 if (pEvent->angleDelta().y() > 0)
665 emit sigZoomRequest(ZoomOperation_In);
666 else if (pEvent->angleDelta().y() < 0)
667 emit sigZoomRequest(ZoomOperation_Out);
668 }
669}
670
671void UIHelpViewer::mouseReleaseEvent(QMouseEvent *pEvent)
672{
673 /* If overlay mode is active just clear it and return: */
674 bool fOverlayMode = m_fOverlayMode;
675 clearOverlay();
676 if (fOverlayMode)
677 return;
678 QString strAnchor = anchorAt(pEvent->pos());
679
680 if (!strAnchor.isEmpty())
681 {
682 QString strLink = source().resolved(strAnchor).toString();
683 QFileInfo fInfo(strLink);
684 QMimeDatabase base;
685 QMimeType type = base.mimeTypeForFile(fInfo);
686 if (type.isValid() && type.inherits("image/png"))
687 {
688 if (!fOverlayMode)
689 loadImage(source().resolved(strAnchor));
690 return;
691 }
692 if (source().resolved(strAnchor).scheme() != "qthelp" && pEvent->button() == Qt::LeftButton)
693 {
694 uiCommon().openURL(strLink);
695 return;
696 }
697
698 if ((pEvent->modifiers() & Qt::ControlModifier) ||
699 pEvent->button() == Qt::MiddleButton)
700 {
701
702 emit sigOpenLinkInNewTab(strLink, true);
703 return;
704 }
705 }
706 QIWithRetranslateUI<QTextBrowser>::mousePressEvent(pEvent);
707}
708
709void UIHelpViewer::mousePressEvent(QMouseEvent *pEvent)
710{
711 QIWithRetranslateUI<QTextBrowser>::mousePressEvent(pEvent);
712}
713
714void UIHelpViewer::mouseMoveEvent(QMouseEvent *pEvent)
715{
716 /*if (m_fOverlayMode)
717 return;*/
718 QIWithRetranslateUI<QTextBrowser>::mouseMoveEvent(pEvent);
719}
720
721void UIHelpViewer::mouseDoubleClickEvent(QMouseEvent *pEvent)
722{
723 clearOverlay();
724 QIWithRetranslateUI<QTextBrowser>::mouseDoubleClickEvent(pEvent);
725}
726
727void UIHelpViewer::paintEvent(QPaintEvent *pEvent)
728{
729 QIWithRetranslateUI<QTextBrowser>::paintEvent(pEvent);
730 QPainter painter(viewport());
731 foreach(const DocumentImage &image, m_imageMap)
732 {
733 QRect rect = cursorRect(image.m_textCursor);
734 QPixmap newPixmap = image.m_pixmap.scaledToWidth(image.m_fScaledWidth, Qt::SmoothTransformation);
735 QRectF imageRect(rect.x() - newPixmap.width(), rect.y(), newPixmap.width(), newPixmap.height());
736
737 int iMargin = 3;
738 QRectF fillRect(imageRect.x() - iMargin, imageRect.y() - iMargin,
739 imageRect.width() + 2 * iMargin, imageRect.height() + 2 * iMargin);
740 /** @todo I need to find the default color somehow and replace hard coded Qt::white. */
741 painter.fillRect(fillRect, Qt::white);
742 painter.drawPixmap(imageRect, newPixmap, newPixmap.rect());
743 }
744}
745
746bool UIHelpViewer::eventFilter(QObject *pObject, QEvent *pEvent)
747{
748 if (pObject == m_pOverlayLabel)
749 {
750 if (pEvent->type() == QEvent::MouseButtonPress ||
751 pEvent->type() == QEvent::MouseButtonDblClick)
752 clearOverlay();
753 }
754 return QIWithRetranslateUI<QTextBrowser>::eventFilter(pObject, pEvent);
755}
756
757void UIHelpViewer::keyPressEvent(QKeyEvent *pEvent)
758{
759 if (pEvent && pEvent->key() == Qt::Key_Escape)
760 clearOverlay();
761 if (pEvent && pEvent->modifiers() &Qt::ControlModifier)
762 {
763 switch (pEvent->key())
764 {
765 case Qt::Key_Equal:
766 emit sigZoomRequest(ZoomOperation_In);
767 break;
768 case Qt::Key_Minus:
769 emit sigZoomRequest(ZoomOperation_Out);
770 break;
771 case Qt::Key_0:
772 emit sigZoomRequest(ZoomOperation_Reset);
773 break;
774 default:
775 break;
776 }
777 }
778 QIWithRetranslateUI<QTextBrowser>::keyPressEvent(pEvent);
779}
780
781void UIHelpViewer::retranslateUi()
782{
783}
784
785void UIHelpViewer::moveFindWidgetIn(int iMargin)
786{
787 if (!m_pFindInPageWidget)
788 return;
789
790 QRect rect = m_pFindInPageWidget->geometry();
791 if (rect.left() < iMargin)
792 rect.translate(-rect.left() + iMargin, 0);
793 if (rect.right() > width() - iMargin)
794 rect.translate((width() - iMargin - rect.right()), 0);
795 if (rect.top() < iMargin)
796 rect.translate(0, -rect.top() + iMargin);
797
798 if (rect.bottom() > height() - iMargin)
799 rect.translate(0, (height() - iMargin - rect.bottom()));
800 m_pFindInPageWidget->setGeometry(rect);
801 m_pFindInPageWidget->update();
802}
803
804bool UIHelpViewer::isRectInside(const QRect &rect, int iMargin) const
805{
806 if (rect.left() < iMargin || rect.top() < iMargin)
807 return false;
808 if (rect.right() > width() - iMargin || rect.bottom() > height() - iMargin)
809 return false;
810 return true;
811}
812
813void UIHelpViewer::findAllMatches(const QString &searchString)
814{
815 QTextDocument *pDocument = document();
816 AssertReturnVoid(pDocument);
817
818 m_matchedCursorPosition.clear();
819 if (searchString.isEmpty())
820 return;
821 QTextCursor cursor(pDocument);
822 QTextDocument::FindFlags flags;
823 int iMatchCount = 0;
824 while (!cursor.isNull() && !cursor.atEnd())
825 {
826 cursor = pDocument->find(searchString, cursor, flags);
827 if (!cursor.isNull())
828 {
829 m_matchedCursorPosition << cursor.position() - searchString.length();
830 ++iMatchCount;
831 }
832 }
833}
834
835void UIHelpViewer::highlightFinds(int iSearchTermLength)
836{
837 QList<QTextEdit::ExtraSelection> extraSelections;
838 for (int i = 0; i < m_matchedCursorPosition.size(); ++i)
839 {
840 QTextEdit::ExtraSelection selection;
841 QTextCursor cursor = textCursor();
842 cursor.setPosition(m_matchedCursorPosition[i]);
843 cursor.setPosition(m_matchedCursorPosition[i] + iSearchTermLength, QTextCursor::KeepAnchor);
844 QTextCharFormat format = cursor.charFormat();
845 format.setBackground(Qt::yellow);
846
847 selection.cursor = cursor;
848 selection.format = format;
849 extraSelections.append(selection);
850 }
851 setExtraSelections(extraSelections);
852}
853
854void UIHelpViewer::selectMatch(int iMatchIndex, int iSearchStringLength)
855{
856 QTextCursor cursor = textCursor();
857 /* Move the cursor to the beginning of the matched string: */
858 cursor.setPosition(m_matchedCursorPosition.at(iMatchIndex), QTextCursor::MoveAnchor);
859 /* Move the cursor to the end of the matched string while keeping the anchor at the begining thus selecting the text: */
860 cursor.setPosition(m_matchedCursorPosition.at(iMatchIndex) + iSearchStringLength, QTextCursor::KeepAnchor);
861 ensureCursorVisible();
862 setTextCursor(cursor);
863}
864
865void UIHelpViewer::sltOpenLinkInNewTab()
866{
867 QAction *pSender = qobject_cast<QAction*>(sender());
868 if (!pSender)
869 return;
870 QUrl url = pSender->data().toUrl();
871 if (url.isValid())
872 emit sigOpenLinkInNewTab(url, false);
873}
874
875void UIHelpViewer::sltOpenLink()
876{
877 QAction *pSender = qobject_cast<QAction*>(sender());
878 if (!pSender)
879 return;
880 QUrl url = pSender->data().toUrl();
881 if (url.isValid())
882 setSource(url);
883}
884
885void UIHelpViewer::sltCopyLink()
886{
887 QAction *pSender = qobject_cast<QAction*>(sender());
888 if (!pSender)
889 return;
890 QUrl url = pSender->data().toUrl();
891 if (url.isValid())
892 {
893 QClipboard *pClipboard = QApplication::clipboard();
894 if (pClipboard)
895 pClipboard->setText(url.toString());
896 }
897}
898
899void UIHelpViewer::sltFindWidgetDrag(const QPoint &delta)
900{
901 if (!m_pFindInPageWidget)
902 return;
903 QRect geo = m_pFindInPageWidget->geometry();
904 geo.translate(delta);
905
906 /* Allow the move if m_pFindInPageWidget stays inside after the move: */
907 if (isRectInside(geo, m_iMarginForFindWidget))
908 m_pFindInPageWidget->move(m_pFindInPageWidget->pos() + delta);
909 m_fFindWidgetDragged = true;
910 update();
911}
912
913void UIHelpViewer::sltFindInPageSearchTextChange(const QString &strSearchText)
914{
915 m_iSearchTermLength = strSearchText.length();
916 findAllMatches(strSearchText);
917 highlightFinds(m_iSearchTermLength);
918 selectMatch(0, m_iSearchTermLength);
919 if (m_pFindInPageWidget)
920 m_pFindInPageWidget->setMatchCountAndCurrentIndex(m_matchedCursorPosition.size(), 0);
921}
922
923void UIHelpViewer::sltSelectPreviousMatch()
924{
925 m_iSelectedMatchIndex = m_iSelectedMatchIndex <= 0 ? m_matchedCursorPosition.size() - 1 : (m_iSelectedMatchIndex - 1);
926 selectMatch(m_iSelectedMatchIndex, m_iSearchTermLength);
927 if (m_pFindInPageWidget)
928 m_pFindInPageWidget->setMatchCountAndCurrentIndex(m_matchedCursorPosition.size(), m_iSelectedMatchIndex);
929}
930
931void UIHelpViewer::sltSelectNextMatch()
932{
933 m_iSelectedMatchIndex = m_iSelectedMatchIndex >= m_matchedCursorPosition.size() - 1 ? 0 : (m_iSelectedMatchIndex + 1);
934 selectMatch(m_iSelectedMatchIndex, m_iSearchTermLength);
935 if (m_pFindInPageWidget)
936 m_pFindInPageWidget->setMatchCountAndCurrentIndex(m_matchedCursorPosition.size(), m_iSelectedMatchIndex);
937}
938
939void UIHelpViewer::iterateDocumentImages()
940{
941 m_imageMap.clear();
942 QTextCursor cursor = textCursor();
943 cursor.movePosition(QTextCursor::Start);
944 while (!cursor.atEnd())
945 {
946 cursor.movePosition(QTextCursor::NextCharacter);
947 if (cursor.charFormat().isImageFormat())
948 {
949 QTextImageFormat imageFormat = cursor.charFormat().toImageFormat();
950 /* There seems to be two cursors per image. Use the first one: */
951 if (m_imageMap.contains(imageFormat.name()))
952 continue;
953 QHash<QString, DocumentImage>::iterator iterator = m_imageMap.insert(imageFormat.name(), DocumentImage());
954 DocumentImage &image = iterator.value();
955 image.m_fInitialWidth = imageFormat.width();
956 image.m_strName = imageFormat.name();
957 image.m_textCursor = cursor;
958 QUrl imageFileUrl;
959 foreach (const QUrl &fileUrl, m_helpFileList)
960 {
961 if (fileUrl.toString().contains(imageFormat.name(), Qt::CaseInsensitive))
962 {
963 imageFileUrl = fileUrl;
964 break;
965 }
966 }
967 if (imageFileUrl.isValid())
968 {
969 QByteArray fileData = m_pHelpEngine->fileData(imageFileUrl);
970 if (!fileData.isEmpty())
971 image.m_pixmap.loadFromData(fileData,"PNG");
972 }
973 }
974 }
975}
976
977void UIHelpViewer::scaleFont()
978{
979 QFont mFont = font();
980 mFont.setPointSize(m_iInitialFontPointSize * m_iZoomPercentage / 100.);
981 setFont(mFont);
982}
983
984void UIHelpViewer::scaleImages()
985{
986 for (QHash<QString, DocumentImage>::iterator iterator = m_imageMap.begin();
987 iterator != m_imageMap.end(); ++iterator)
988 {
989 DocumentImage &image = *iterator;
990 QTextCursor cursor = image.m_textCursor;
991 QTextCharFormat format = cursor.charFormat();
992 if (!format.isImageFormat())
993 continue;
994 QTextImageFormat imageFormat = format.toImageFormat();
995 image.m_fScaledWidth = image.m_fInitialWidth * m_iZoomPercentage / 100.;
996 imageFormat.setWidth(image.m_fScaledWidth);
997 cursor.deletePreviousChar();
998 cursor.deleteChar();
999 cursor.insertImage(imageFormat);
1000 }
1001}
1002
1003void UIHelpViewer::clearOverlay()
1004{
1005 AssertReturnVoid(m_pOverlayLabel);
1006
1007 if (!m_fOverlayMode)
1008 return;
1009 m_overlayPixmap = QPixmap();
1010 m_fOverlayMode = false;
1011 if (m_pOverlayBlurEffect)
1012 m_pOverlayBlurEffect->setEnabled(false);
1013 m_pOverlayLabel->hide();
1014}
1015
1016void UIHelpViewer::enableOverlay()
1017{
1018 AssertReturnVoid(m_pOverlayLabel);
1019 m_fOverlayMode = true;
1020 if (m_pOverlayBlurEffect)
1021 m_pOverlayBlurEffect->setEnabled(true);
1022 toggleFindInPageWidget(false);
1023
1024 /* Scale the image to 1:1 as long as it fits into avaible space (minus some margins and scrollbar sizes): */
1025 int vWidth = 0;
1026 if (verticalScrollBar() && verticalScrollBar()->isVisible())
1027 vWidth = verticalScrollBar()->width();
1028 int hMargin = qApp->style()->pixelMetric(QStyle::PM_LayoutLeftMargin) +
1029 qApp->style()->pixelMetric(QStyle::PM_LayoutRightMargin) + vWidth;
1030
1031 int hHeight = 0;
1032 if (horizontalScrollBar() && horizontalScrollBar()->isVisible())
1033 hHeight = horizontalScrollBar()->height();
1034 int vMargin = qApp->style()->pixelMetric(QStyle::PM_LayoutTopMargin) +
1035 qApp->style()->pixelMetric(QStyle::PM_LayoutBottomMargin) + hHeight;
1036
1037 QSize size(qMin(width() - hMargin, m_overlayPixmap.width()),
1038 qMin(height() - vMargin, m_overlayPixmap.height()));
1039 m_pOverlayLabel->setPixmap(m_overlayPixmap.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation));
1040 m_pOverlayLabel->show();
1041
1042 /* Center the label: */
1043 int x = 0.5 * (width() - vWidth - m_pOverlayLabel->width());
1044 int y = 0.5 * (height() - hHeight - m_pOverlayLabel->height());
1045 m_pOverlayLabel->move(x, y);
1046}
1047
1048void UIHelpViewer::loadImage(const QUrl &imageFileUrl)
1049{
1050 clearOverlay();
1051 /* Dont zoom into image if mouse button released after a mouse drag: */
1052 if (textCursor().hasSelection())
1053 return;
1054 if (!imageFileUrl.isValid())
1055 return;
1056 QByteArray fileData = m_pHelpEngine->fileData(imageFileUrl);
1057 if (!fileData.isEmpty())
1058 {
1059 m_overlayPixmap.loadFromData(fileData,"PNG");
1060 if (!m_overlayPixmap.isNull())
1061 enableOverlay();
1062 }
1063}
1064
1065
1066#include "UIHelpViewer.moc"
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette