VirtualBox

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

Last change on this file since 101561 was 101561, checked in by vboxsync, 16 months ago

FE/Qt: bugref:10450: Get rid of Qt5 stuff; This one is about position/globalPosition stuff.

  • 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 101561 2023-10-23 16:25:05Z 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 const QPoint gPos = pMouseEvent->globalPosition().toPoint();
301 if (pMouseEvent->buttons() == Qt::LeftButton)
302 {
303 if (m_previousMousePosition != QPoint(-1, -1))
304 emit sigDragging(gPos - m_previousMousePosition);
305 m_previousMousePosition = gPos;
306 UICursor::setCursor(m_pDragMoveLabel, Qt::ClosedHandCursor);
307 }
308 }
309 else if (pEvent->type() == QEvent::MouseButtonRelease)
310 {
311 m_previousMousePosition = QPoint(-1, -1);
312 UICursor::setCursor(m_pDragMoveLabel, Qt::CrossCursor);
313 }
314 }
315 return QIWithRetranslateUI<QWidget>::eventFilter(pObject, pEvent);
316}
317
318void UIFindInPageWidget::keyPressEvent(QKeyEvent *pEvent)
319{
320 switch (pEvent->key())
321 {
322 case Qt::Key_Escape:
323 emit sigClose();
324 return;
325 break;
326 case Qt::Key_Down:
327 emit sigSelectNextMatch();
328 return;
329 break;
330 case Qt::Key_Up:
331 emit sigSelectPreviousMatch();
332 return;
333 break;
334 default:
335 QIWithRetranslateUI<QWidget>::keyPressEvent(pEvent);
336 break;
337 }
338}
339
340void UIFindInPageWidget::prepare()
341{
342 setAutoFillBackground(true);
343 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
344
345 QHBoxLayout *pLayout = new QHBoxLayout(this);
346 m_pSearchLineEdit = new UISearchLineEdit;
347 AssertReturnVoid(pLayout && m_pSearchLineEdit);
348 setFocusProxy(m_pSearchLineEdit);
349 QFontMetrics fontMetric(m_pSearchLineEdit->font());
350#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
351 setMinimumSize(40 * fontMetric.horizontalAdvance("x"),
352 fontMetric.height() +
353 qApp->style()->pixelMetric(QStyle::PM_LayoutBottomMargin) +
354 qApp->style()->pixelMetric(QStyle::PM_LayoutTopMargin));
355
356#else
357 setMinimumSize(40 * fontMetric.width("x"),
358 fontMetric.height() +
359 qApp->style()->pixelMetric(QStyle::PM_LayoutBottomMargin) +
360 qApp->style()->pixelMetric(QStyle::PM_LayoutTopMargin));
361#endif
362 connect(m_pSearchLineEdit, &UISearchLineEdit::textChanged,
363 this, &UIFindInPageWidget::sigSearchTextChanged);
364
365 m_pDragMoveLabel = new QLabel;
366 AssertReturnVoid(m_pDragMoveLabel);
367 m_pDragMoveLabel->installEventFilter(this);
368 m_pDragMoveLabel->setPixmap(QPixmap(":/drag_move_16px.png"));
369 pLayout->addWidget(m_pDragMoveLabel);
370
371
372 pLayout->setSpacing(0);
373 pLayout->addWidget(m_pSearchLineEdit);
374
375 m_pPreviousButton = new QIToolButton;
376 m_pNextButton = new QIToolButton;
377 m_pCloseButton = new QIToolButton;
378
379 pLayout->addWidget(m_pPreviousButton);
380 pLayout->addWidget(m_pNextButton);
381 pLayout->addWidget(m_pCloseButton);
382
383 m_pPreviousButton->setIcon(UIIconPool::iconSet(":/arrow_up_10px.png"));
384 m_pNextButton->setIcon(UIIconPool::iconSet(":/arrow_down_10px.png"));
385 m_pCloseButton->setIcon(UIIconPool::iconSet(":/close_16px.png"));
386
387 connect(m_pPreviousButton, &QIToolButton::pressed, this, &UIFindInPageWidget::sigSelectPreviousMatch);
388 connect(m_pNextButton, &QIToolButton::pressed, this, &UIFindInPageWidget::sigSelectNextMatch);
389 connect(m_pCloseButton, &QIToolButton::pressed, this, &UIFindInPageWidget::sigClose);
390}
391
392void UIFindInPageWidget::retranslateUi()
393{
394}
395
396
397/*********************************************************************************************************************************
398* UIHelpViewer implementation. *
399*********************************************************************************************************************************/
400
401UIHelpViewer::UIHelpViewer(const QHelpEngine *pHelpEngine, QWidget *pParent /* = 0 */)
402 :QIWithRetranslateUI<QTextBrowser>(pParent)
403 , m_pHelpEngine(pHelpEngine)
404 , m_pFindInPageWidget(new UIFindInPageWidget(this))
405 , m_fFindWidgetDragged(false)
406 , m_iMarginForFindWidget(qApp->style()->pixelMetric(QStyle::PM_LayoutLeftMargin))
407 , m_iSelectedMatchIndex(0)
408 , m_iSearchTermLength(0)
409 , m_fOverlayMode(false)
410 , m_pOverlayLabel(0)
411 , m_iZoomPercentage(100)
412{
413 m_iInitialFontPointSize = font().pointSize();
414 setUndoRedoEnabled(true);
415 connect(m_pFindInPageWidget, &UIFindInPageWidget::sigDragging,
416 this, &UIHelpViewer::sltFindWidgetDrag);
417 connect(m_pFindInPageWidget, &UIFindInPageWidget::sigSearchTextChanged,
418 this, &UIHelpViewer::sltFindInPageSearchTextChange);
419
420 connect(m_pFindInPageWidget, &UIFindInPageWidget::sigSelectPreviousMatch,
421 this, &UIHelpViewer::sltSelectPreviousMatch);
422 connect(m_pFindInPageWidget, &UIFindInPageWidget::sigSelectNextMatch,
423 this, &UIHelpViewer::sltSelectNextMatch);
424 connect(m_pFindInPageWidget, &UIFindInPageWidget::sigClose,
425 this, &UIHelpViewer::sltCloseFindInPageWidget);
426
427 m_pFindInPageWidget->setVisible(false);
428
429 m_pOverlayLabel = new QLabel(this);
430 if (m_pOverlayLabel)
431 {
432 m_pOverlayLabel->hide();
433 m_pOverlayLabel->installEventFilter(this);
434 }
435
436 m_pOverlayBlurEffect = new QGraphicsBlurEffect(this);
437 if (m_pOverlayBlurEffect)
438 {
439 viewport()->setGraphicsEffect(m_pOverlayBlurEffect);
440 m_pOverlayBlurEffect->setEnabled(false);
441 m_pOverlayBlurEffect->setBlurRadius(8);
442 }
443 retranslateUi();
444}
445
446QVariant UIHelpViewer::loadResource(int type, const QUrl &name)
447{
448 if (name.scheme() == "qthelp" && m_pHelpEngine)
449 return QVariant(m_pHelpEngine->fileData(name));
450 else
451 return QTextBrowser::loadResource(type, name);
452}
453
454void UIHelpViewer::emitHistoryChangedSignal()
455{
456 emit historyChanged();
457 emit backwardAvailable(true);
458}
459
460#ifdef VBOX_IS_QT6_OR_LATER /* it was setSource before 6.0 */
461void UIHelpViewer::doSetSource(const QUrl &url, QTextDocument::ResourceType type)
462#else
463void UIHelpViewer::setSource(const QUrl &url)
464#endif
465{
466 clearOverlay();
467 if (url.scheme() != "qthelp")
468 return;
469#ifdef VBOX_IS_QT6_OR_LATER /* it was setSource before 6.0 */
470 QTextBrowser::doSetSource(url, type);
471#else
472 QTextBrowser::setSource(url);
473#endif
474 QTextDocument *pDocument = document();
475 if (!pDocument || pDocument->isEmpty())
476 {
477 setText(UIHelpBrowserWidget::tr("<div><p><h3>Not found.</h3>The page <b>%1</b> could not be found.</p></div>").arg(url.toString()));
478 setDocumentTitle(UIHelpBrowserWidget::tr("Not Found"));
479 }
480 if (m_pFindInPageWidget && m_pFindInPageWidget->isVisible())
481 {
482 document()->undo();
483 m_pFindInPageWidget->clearSearchField();
484 }
485 iterateDocumentImages();
486 scaleImages();
487}
488
489void UIHelpViewer::toggleFindInPageWidget(bool fVisible)
490{
491 if (!m_pFindInPageWidget)
492 return;
493
494 /* Closing the find in page widget causes QTextBrowser to jump to the top of the document. This hack puts it back into position: */
495 int iPosition = verticalScrollBar()->value();
496 m_iMarginForFindWidget = verticalScrollBar()->width() +
497 qApp->style()->pixelMetric(QStyle::PM_LayoutLeftMargin);
498 /* Try to position the widget somewhere meaningful initially: */
499 if (!m_fFindWidgetDragged)
500 m_pFindInPageWidget->move(width() - m_iMarginForFindWidget - m_pFindInPageWidget->width(),
501 m_iMarginForFindWidget);
502
503 m_pFindInPageWidget->setVisible(fVisible);
504
505 if (!fVisible)
506 {
507 /* Clear highlights: */
508 setExtraSelections(QList<QTextEdit::ExtraSelection>());
509 m_pFindInPageWidget->clearSearchField();
510 verticalScrollBar()->setValue(iPosition);
511 }
512 else
513 m_pFindInPageWidget->setFocus();
514 emit sigFindInPageWidgetToogle(fVisible);
515}
516
517void UIHelpViewer::reload()
518{
519 setSource(source());
520}
521
522void UIHelpViewer::sltToggleFindInPageWidget(bool fVisible)
523{
524 clearOverlay();
525 toggleFindInPageWidget(fVisible);
526}
527
528void UIHelpViewer::sltCloseFindInPageWidget()
529{
530 sltToggleFindInPageWidget(false);
531}
532
533void UIHelpViewer::setFont(const QFont &font)
534{
535 QIWithRetranslateUI<QTextBrowser>::setFont(font);
536 /* Make sure the font size of the find in widget stays constant: */
537 if (m_pFindInPageWidget)
538 {
539 QFont wFont(font);
540 wFont.setPointSize(m_iInitialFontPointSize);
541 m_pFindInPageWidget->setFont(wFont);
542 }
543}
544
545bool UIHelpViewer::isFindInPageWidgetVisible() const
546{
547 if (m_pFindInPageWidget)
548 return m_pFindInPageWidget->isVisible();
549 return false;
550}
551
552void UIHelpViewer::setZoomPercentage(int iZoomPercentage)
553{
554 m_iZoomPercentage = iZoomPercentage;
555 clearOverlay();
556 scaleFont();
557 scaleImages();
558}
559
560void UIHelpViewer::setHelpFileList(const QList<QUrl> &helpFileList)
561{
562 m_helpFileList = helpFileList;
563 /* File list necessary to get the image data from the help engine: */
564 iterateDocumentImages();
565 scaleImages();
566}
567
568bool UIHelpViewer::hasSelectedText() const
569{
570 return textCursor().hasSelection();
571}
572
573void UIHelpViewer::contextMenuEvent(QContextMenuEvent *event)
574{
575 QMenu menu;
576
577 if (textCursor().hasSelection())
578 {
579 QAction *pCopySelectedTextAction = new QAction(UIHelpBrowserWidget::tr("Copy Selected Text"));
580 connect(pCopySelectedTextAction, &QAction::triggered,
581 this, &UIHelpViewer::copy);
582 menu.addAction(pCopySelectedTextAction);
583 menu.addSeparator();
584 }
585
586 UIContextMenuNavigationAction *pNavigationActions = new UIContextMenuNavigationAction;
587 pNavigationActions->setBackwardAvailable(isBackwardAvailable());
588 pNavigationActions->setForwardAvailable(isForwardAvailable());
589
590 connect(pNavigationActions, &UIContextMenuNavigationAction::sigGoBackward,
591 this, &UIHelpViewer::sigGoBackward);
592 connect(pNavigationActions, &UIContextMenuNavigationAction::sigGoForward,
593 this, &UIHelpViewer::sigGoForward);
594 connect(pNavigationActions, &UIContextMenuNavigationAction::sigGoHome,
595 this, &UIHelpViewer::sigGoHome);
596 connect(pNavigationActions, &UIContextMenuNavigationAction::sigReloadPage,
597 this, &UIHelpViewer::reload);
598 connect(pNavigationActions, &UIContextMenuNavigationAction::sigAddBookmark,
599 this, &UIHelpViewer::sigAddBookmark);
600
601 QAction *pOpenLinkAction = new QAction(UIHelpBrowserWidget::tr("Open Link"));
602 connect(pOpenLinkAction, &QAction::triggered,
603 this, &UIHelpViewer::sltOpenLink);
604
605 QAction *pOpenInNewTabAction = new QAction(UIHelpBrowserWidget::tr("Open Link in New Tab"));
606 connect(pOpenInNewTabAction, &QAction::triggered,
607 this, &UIHelpViewer::sltOpenLinkInNewTab);
608
609 QAction *pCopyLink = new QAction(UIHelpBrowserWidget::tr("Copy Link"));
610 connect(pCopyLink, &QAction::triggered,
611 this, &UIHelpViewer::sltCopyLink);
612
613 QAction *pFindInPage = new QAction(UIHelpBrowserWidget::tr("Find in Page"));
614 pFindInPage->setCheckable(true);
615 if (m_pFindInPageWidget)
616 pFindInPage->setChecked(m_pFindInPageWidget->isVisible());
617 connect(pFindInPage, &QAction::toggled, this, &UIHelpViewer::sltToggleFindInPageWidget);
618
619 menu.addAction(pNavigationActions);
620 menu.addAction(pOpenLinkAction);
621 menu.addAction(pOpenInNewTabAction);
622 menu.addAction(pCopyLink);
623 menu.addAction(pFindInPage);
624
625 QString strAnchor = anchorAt(event->pos());
626 if (!strAnchor.isEmpty())
627 {
628 QString strLink = source().resolved(anchorAt(event->pos())).toString();
629 pOpenLinkAction->setData(strLink);
630 pOpenInNewTabAction->setData(strLink);
631 pCopyLink->setData(strLink);
632 }
633 else
634 {
635 pOpenLinkAction->setEnabled(false);
636 pOpenInNewTabAction->setEnabled(false);
637 pCopyLink->setEnabled(false);
638 }
639
640 menu.exec(event->globalPos());
641}
642
643void UIHelpViewer::resizeEvent(QResizeEvent *pEvent)
644{
645 if (m_fOverlayMode)
646 clearOverlay();
647 /* Make sure the widget stays inside the parent during parent resize: */
648 if (m_pFindInPageWidget)
649 {
650 if (!isRectInside(m_pFindInPageWidget->geometry(), m_iMarginForFindWidget))
651 moveFindWidgetIn(m_iMarginForFindWidget);
652 }
653 QIWithRetranslateUI<QTextBrowser>::resizeEvent(pEvent);
654}
655
656void UIHelpViewer::wheelEvent(QWheelEvent *pEvent)
657{
658 if (m_fOverlayMode && !pEvent)
659 return;
660 /* QTextBrowser::wheelEvent scales font when some modifiers are pressed. We dont want that: */
661 if (pEvent->modifiers() == Qt::NoModifier)
662 QTextBrowser::wheelEvent(pEvent);
663 else if (pEvent->modifiers() & Qt::ControlModifier)
664 {
665 if (pEvent->angleDelta().y() > 0)
666 emit sigZoomRequest(ZoomOperation_In);
667 else if (pEvent->angleDelta().y() < 0)
668 emit sigZoomRequest(ZoomOperation_Out);
669 }
670}
671
672void UIHelpViewer::mouseReleaseEvent(QMouseEvent *pEvent)
673{
674 /* If overlay mode is active just clear it and return: */
675 bool fOverlayMode = m_fOverlayMode;
676 clearOverlay();
677 if (fOverlayMode)
678 return;
679 QString strAnchor = anchorAt(pEvent->position().toPoint());
680
681 if (!strAnchor.isEmpty())
682 {
683 QString strLink = source().resolved(strAnchor).toString();
684 QFileInfo fInfo(strLink);
685 QMimeDatabase base;
686 QMimeType type = base.mimeTypeForFile(fInfo);
687 if (type.isValid() && type.inherits("image/png"))
688 {
689 if (!fOverlayMode)
690 loadImage(source().resolved(strAnchor));
691 return;
692 }
693 if (source().resolved(strAnchor).scheme() != "qthelp" && pEvent->button() == Qt::LeftButton)
694 {
695 uiCommon().openURL(strLink);
696 return;
697 }
698
699 if ((pEvent->modifiers() & Qt::ControlModifier) ||
700 pEvent->button() == Qt::MiddleButton)
701 {
702
703 emit sigOpenLinkInNewTab(strLink, true);
704 return;
705 }
706 }
707 QIWithRetranslateUI<QTextBrowser>::mousePressEvent(pEvent);
708}
709
710void UIHelpViewer::mousePressEvent(QMouseEvent *pEvent)
711{
712 QIWithRetranslateUI<QTextBrowser>::mousePressEvent(pEvent);
713}
714
715void UIHelpViewer::mouseMoveEvent(QMouseEvent *pEvent)
716{
717 /*if (m_fOverlayMode)
718 return;*/
719 QIWithRetranslateUI<QTextBrowser>::mouseMoveEvent(pEvent);
720}
721
722void UIHelpViewer::mouseDoubleClickEvent(QMouseEvent *pEvent)
723{
724 clearOverlay();
725 QIWithRetranslateUI<QTextBrowser>::mouseDoubleClickEvent(pEvent);
726}
727
728void UIHelpViewer::paintEvent(QPaintEvent *pEvent)
729{
730 QIWithRetranslateUI<QTextBrowser>::paintEvent(pEvent);
731 QPainter painter(viewport());
732 foreach(const DocumentImage &image, m_imageMap)
733 {
734 QRect rect = cursorRect(image.m_textCursor);
735 QPixmap newPixmap = image.m_pixmap.scaledToWidth(image.m_fScaledWidth, Qt::SmoothTransformation);
736 QRectF imageRect(rect.x() - newPixmap.width(), rect.y(), newPixmap.width(), newPixmap.height());
737
738 int iMargin = 3;
739 QRectF fillRect(imageRect.x() - iMargin, imageRect.y() - iMargin,
740 imageRect.width() + 2 * iMargin, imageRect.height() + 2 * iMargin);
741 /** @todo I need to find the default color somehow and replace hard coded Qt::white. */
742 painter.fillRect(fillRect, Qt::white);
743 painter.drawPixmap(imageRect, newPixmap, newPixmap.rect());
744 }
745}
746
747bool UIHelpViewer::eventFilter(QObject *pObject, QEvent *pEvent)
748{
749 if (pObject == m_pOverlayLabel)
750 {
751 if (pEvent->type() == QEvent::MouseButtonPress ||
752 pEvent->type() == QEvent::MouseButtonDblClick)
753 clearOverlay();
754 }
755 return QIWithRetranslateUI<QTextBrowser>::eventFilter(pObject, pEvent);
756}
757
758void UIHelpViewer::keyPressEvent(QKeyEvent *pEvent)
759{
760 if (pEvent && pEvent->key() == Qt::Key_Escape)
761 clearOverlay();
762 if (pEvent && pEvent->modifiers() &Qt::ControlModifier)
763 {
764 switch (pEvent->key())
765 {
766 case Qt::Key_Equal:
767 emit sigZoomRequest(ZoomOperation_In);
768 break;
769 case Qt::Key_Minus:
770 emit sigZoomRequest(ZoomOperation_Out);
771 break;
772 case Qt::Key_0:
773 emit sigZoomRequest(ZoomOperation_Reset);
774 break;
775 default:
776 break;
777 }
778 }
779 QIWithRetranslateUI<QTextBrowser>::keyPressEvent(pEvent);
780}
781
782void UIHelpViewer::retranslateUi()
783{
784}
785
786void UIHelpViewer::moveFindWidgetIn(int iMargin)
787{
788 if (!m_pFindInPageWidget)
789 return;
790
791 QRect rect = m_pFindInPageWidget->geometry();
792 if (rect.left() < iMargin)
793 rect.translate(-rect.left() + iMargin, 0);
794 if (rect.right() > width() - iMargin)
795 rect.translate((width() - iMargin - rect.right()), 0);
796 if (rect.top() < iMargin)
797 rect.translate(0, -rect.top() + iMargin);
798
799 if (rect.bottom() > height() - iMargin)
800 rect.translate(0, (height() - iMargin - rect.bottom()));
801 m_pFindInPageWidget->setGeometry(rect);
802 m_pFindInPageWidget->update();
803}
804
805bool UIHelpViewer::isRectInside(const QRect &rect, int iMargin) const
806{
807 if (rect.left() < iMargin || rect.top() < iMargin)
808 return false;
809 if (rect.right() > width() - iMargin || rect.bottom() > height() - iMargin)
810 return false;
811 return true;
812}
813
814void UIHelpViewer::findAllMatches(const QString &searchString)
815{
816 QTextDocument *pDocument = document();
817 AssertReturnVoid(pDocument);
818
819 m_matchedCursorPosition.clear();
820 if (searchString.isEmpty())
821 return;
822 QTextCursor cursor(pDocument);
823 QTextDocument::FindFlags flags;
824 while (!cursor.isNull() && !cursor.atEnd())
825 {
826 cursor = pDocument->find(searchString, cursor, flags);
827 if (!cursor.isNull())
828 m_matchedCursorPosition << cursor.position() - searchString.length();
829 }
830}
831
832void UIHelpViewer::highlightFinds(int iSearchTermLength)
833{
834 QList<QTextEdit::ExtraSelection> extraSelections;
835 for (int i = 0; i < m_matchedCursorPosition.size(); ++i)
836 {
837 QTextEdit::ExtraSelection selection;
838 QTextCursor cursor = textCursor();
839 cursor.setPosition(m_matchedCursorPosition[i]);
840 cursor.setPosition(m_matchedCursorPosition[i] + iSearchTermLength, QTextCursor::KeepAnchor);
841 QTextCharFormat format = cursor.charFormat();
842 format.setBackground(Qt::yellow);
843
844 selection.cursor = cursor;
845 selection.format = format;
846 extraSelections.append(selection);
847 }
848 setExtraSelections(extraSelections);
849}
850
851void UIHelpViewer::selectMatch(int iMatchIndex, int iSearchStringLength)
852{
853 QTextCursor cursor = textCursor();
854 /* Move the cursor to the beginning of the matched string: */
855 cursor.setPosition(m_matchedCursorPosition.at(iMatchIndex), QTextCursor::MoveAnchor);
856 /* Move the cursor to the end of the matched string while keeping the anchor at the begining thus selecting the text: */
857 cursor.setPosition(m_matchedCursorPosition.at(iMatchIndex) + iSearchStringLength, QTextCursor::KeepAnchor);
858 ensureCursorVisible();
859 setTextCursor(cursor);
860}
861
862void UIHelpViewer::sltOpenLinkInNewTab()
863{
864 QAction *pSender = qobject_cast<QAction*>(sender());
865 if (!pSender)
866 return;
867 QUrl url = pSender->data().toUrl();
868 if (url.isValid())
869 emit sigOpenLinkInNewTab(url, false);
870}
871
872void UIHelpViewer::sltOpenLink()
873{
874 QAction *pSender = qobject_cast<QAction*>(sender());
875 if (!pSender)
876 return;
877 QUrl url = pSender->data().toUrl();
878 if (url.isValid())
879 setSource(url);
880}
881
882void UIHelpViewer::sltCopyLink()
883{
884 QAction *pSender = qobject_cast<QAction*>(sender());
885 if (!pSender)
886 return;
887 QUrl url = pSender->data().toUrl();
888 if (url.isValid())
889 {
890 QClipboard *pClipboard = QApplication::clipboard();
891 if (pClipboard)
892 pClipboard->setText(url.toString());
893 }
894}
895
896void UIHelpViewer::sltFindWidgetDrag(const QPoint &delta)
897{
898 if (!m_pFindInPageWidget)
899 return;
900 QRect geo = m_pFindInPageWidget->geometry();
901 geo.translate(delta);
902
903 /* Allow the move if m_pFindInPageWidget stays inside after the move: */
904 if (isRectInside(geo, m_iMarginForFindWidget))
905 m_pFindInPageWidget->move(m_pFindInPageWidget->pos() + delta);
906 m_fFindWidgetDragged = true;
907 update();
908}
909
910void UIHelpViewer::sltFindInPageSearchTextChange(const QString &strSearchText)
911{
912 m_iSearchTermLength = strSearchText.length();
913 findAllMatches(strSearchText);
914 highlightFinds(m_iSearchTermLength);
915 selectMatch(0, m_iSearchTermLength);
916 if (m_pFindInPageWidget)
917 m_pFindInPageWidget->setMatchCountAndCurrentIndex(m_matchedCursorPosition.size(), 0);
918}
919
920void UIHelpViewer::sltSelectPreviousMatch()
921{
922 m_iSelectedMatchIndex = m_iSelectedMatchIndex <= 0 ? m_matchedCursorPosition.size() - 1 : (m_iSelectedMatchIndex - 1);
923 selectMatch(m_iSelectedMatchIndex, m_iSearchTermLength);
924 if (m_pFindInPageWidget)
925 m_pFindInPageWidget->setMatchCountAndCurrentIndex(m_matchedCursorPosition.size(), m_iSelectedMatchIndex);
926}
927
928void UIHelpViewer::sltSelectNextMatch()
929{
930 m_iSelectedMatchIndex = m_iSelectedMatchIndex >= m_matchedCursorPosition.size() - 1 ? 0 : (m_iSelectedMatchIndex + 1);
931 selectMatch(m_iSelectedMatchIndex, m_iSearchTermLength);
932 if (m_pFindInPageWidget)
933 m_pFindInPageWidget->setMatchCountAndCurrentIndex(m_matchedCursorPosition.size(), m_iSelectedMatchIndex);
934}
935
936void UIHelpViewer::iterateDocumentImages()
937{
938 m_imageMap.clear();
939 QTextCursor cursor = textCursor();
940 cursor.movePosition(QTextCursor::Start);
941 while (!cursor.atEnd())
942 {
943 cursor.movePosition(QTextCursor::NextCharacter);
944 if (cursor.charFormat().isImageFormat())
945 {
946 QTextImageFormat imageFormat = cursor.charFormat().toImageFormat();
947 /* There seems to be two cursors per image. Use the first one: */
948 if (m_imageMap.contains(imageFormat.name()))
949 continue;
950 QHash<QString, DocumentImage>::iterator iterator = m_imageMap.insert(imageFormat.name(), DocumentImage());
951 DocumentImage &image = iterator.value();
952 image.m_fInitialWidth = imageFormat.width();
953 image.m_strName = imageFormat.name();
954 image.m_textCursor = cursor;
955 QUrl imageFileUrl;
956 foreach (const QUrl &fileUrl, m_helpFileList)
957 {
958 if (fileUrl.toString().contains(imageFormat.name(), Qt::CaseInsensitive))
959 {
960 imageFileUrl = fileUrl;
961 break;
962 }
963 }
964 if (imageFileUrl.isValid())
965 {
966 QByteArray fileData = m_pHelpEngine->fileData(imageFileUrl);
967 if (!fileData.isEmpty())
968 image.m_pixmap.loadFromData(fileData,"PNG");
969 }
970 }
971 }
972}
973
974void UIHelpViewer::scaleFont()
975{
976 QFont mFont = font();
977 mFont.setPointSize(m_iInitialFontPointSize * m_iZoomPercentage / 100.);
978 setFont(mFont);
979}
980
981void UIHelpViewer::scaleImages()
982{
983 for (QHash<QString, DocumentImage>::iterator iterator = m_imageMap.begin();
984 iterator != m_imageMap.end(); ++iterator)
985 {
986 DocumentImage &image = *iterator;
987 QTextCursor cursor = image.m_textCursor;
988 QTextCharFormat format = cursor.charFormat();
989 if (!format.isImageFormat())
990 continue;
991 QTextImageFormat imageFormat = format.toImageFormat();
992 image.m_fScaledWidth = image.m_fInitialWidth * m_iZoomPercentage / 100.;
993 imageFormat.setWidth(image.m_fScaledWidth);
994 cursor.deletePreviousChar();
995 cursor.deleteChar();
996 cursor.insertImage(imageFormat);
997 }
998}
999
1000void UIHelpViewer::clearOverlay()
1001{
1002 AssertReturnVoid(m_pOverlayLabel);
1003
1004 if (!m_fOverlayMode)
1005 return;
1006 m_overlayPixmap = QPixmap();
1007 m_fOverlayMode = false;
1008 if (m_pOverlayBlurEffect)
1009 m_pOverlayBlurEffect->setEnabled(false);
1010 m_pOverlayLabel->hide();
1011}
1012
1013void UIHelpViewer::enableOverlay()
1014{
1015 AssertReturnVoid(m_pOverlayLabel);
1016 m_fOverlayMode = true;
1017 if (m_pOverlayBlurEffect)
1018 m_pOverlayBlurEffect->setEnabled(true);
1019 toggleFindInPageWidget(false);
1020
1021 /* Scale the image to 1:1 as long as it fits into avaible space (minus some margins and scrollbar sizes): */
1022 int vWidth = 0;
1023 if (verticalScrollBar() && verticalScrollBar()->isVisible())
1024 vWidth = verticalScrollBar()->width();
1025 int hMargin = qApp->style()->pixelMetric(QStyle::PM_LayoutLeftMargin) +
1026 qApp->style()->pixelMetric(QStyle::PM_LayoutRightMargin) + vWidth;
1027
1028 int hHeight = 0;
1029 if (horizontalScrollBar() && horizontalScrollBar()->isVisible())
1030 hHeight = horizontalScrollBar()->height();
1031 int vMargin = qApp->style()->pixelMetric(QStyle::PM_LayoutTopMargin) +
1032 qApp->style()->pixelMetric(QStyle::PM_LayoutBottomMargin) + hHeight;
1033
1034 QSize size(qMin(width() - hMargin, m_overlayPixmap.width()),
1035 qMin(height() - vMargin, m_overlayPixmap.height()));
1036 m_pOverlayLabel->setPixmap(m_overlayPixmap.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation));
1037 m_pOverlayLabel->show();
1038
1039 /* Center the label: */
1040 int x = 0.5 * (width() - vWidth - m_pOverlayLabel->width());
1041 int y = 0.5 * (height() - hHeight - m_pOverlayLabel->height());
1042 m_pOverlayLabel->move(x, y);
1043}
1044
1045void UIHelpViewer::loadImage(const QUrl &imageFileUrl)
1046{
1047 clearOverlay();
1048 /* Dont zoom into image if mouse button released after a mouse drag: */
1049 if (textCursor().hasSelection())
1050 return;
1051 if (!imageFileUrl.isValid())
1052 return;
1053 QByteArray fileData = m_pHelpEngine->fileData(imageFileUrl);
1054 if (!fileData.isEmpty())
1055 {
1056 m_overlayPixmap.loadFromData(fileData,"PNG");
1057 if (!m_overlayPixmap.isNull())
1058 enableOverlay();
1059 }
1060}
1061
1062
1063#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