VirtualBox

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

Last change on this file since 102493 was 101571, checked in by vboxsync, 12 months ago

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

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 35.0 KB
Line 
1/* $Id: UIHelpViewer.cpp 101571 2023-10-24 00:48:20Z 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
460void UIHelpViewer::doSetSource(const QUrl &url, QTextDocument::ResourceType type)
461{
462 clearOverlay();
463 if (url.scheme() != "qthelp")
464 return;
465 QTextBrowser::doSetSource(url, type);
466 QTextDocument *pDocument = document();
467 if (!pDocument || pDocument->isEmpty())
468 {
469 setText(UIHelpBrowserWidget::tr("<div><p><h3>Not found.</h3>The page <b>%1</b> could not be found.</p></div>").arg(url.toString()));
470 setDocumentTitle(UIHelpBrowserWidget::tr("Not Found"));
471 }
472 if (m_pFindInPageWidget && m_pFindInPageWidget->isVisible())
473 {
474 document()->undo();
475 m_pFindInPageWidget->clearSearchField();
476 }
477 iterateDocumentImages();
478 scaleImages();
479}
480
481void UIHelpViewer::toggleFindInPageWidget(bool fVisible)
482{
483 if (!m_pFindInPageWidget)
484 return;
485
486 /* Closing the find in page widget causes QTextBrowser to jump to the top of the document. This hack puts it back into position: */
487 int iPosition = verticalScrollBar()->value();
488 m_iMarginForFindWidget = verticalScrollBar()->width() +
489 qApp->style()->pixelMetric(QStyle::PM_LayoutLeftMargin);
490 /* Try to position the widget somewhere meaningful initially: */
491 if (!m_fFindWidgetDragged)
492 m_pFindInPageWidget->move(width() - m_iMarginForFindWidget - m_pFindInPageWidget->width(),
493 m_iMarginForFindWidget);
494
495 m_pFindInPageWidget->setVisible(fVisible);
496
497 if (!fVisible)
498 {
499 /* Clear highlights: */
500 setExtraSelections(QList<QTextEdit::ExtraSelection>());
501 m_pFindInPageWidget->clearSearchField();
502 verticalScrollBar()->setValue(iPosition);
503 }
504 else
505 m_pFindInPageWidget->setFocus();
506 emit sigFindInPageWidgetToogle(fVisible);
507}
508
509void UIHelpViewer::reload()
510{
511 setSource(source());
512}
513
514void UIHelpViewer::sltToggleFindInPageWidget(bool fVisible)
515{
516 clearOverlay();
517 toggleFindInPageWidget(fVisible);
518}
519
520void UIHelpViewer::sltCloseFindInPageWidget()
521{
522 sltToggleFindInPageWidget(false);
523}
524
525void UIHelpViewer::setFont(const QFont &font)
526{
527 QIWithRetranslateUI<QTextBrowser>::setFont(font);
528 /* Make sure the font size of the find in widget stays constant: */
529 if (m_pFindInPageWidget)
530 {
531 QFont wFont(font);
532 wFont.setPointSize(m_iInitialFontPointSize);
533 m_pFindInPageWidget->setFont(wFont);
534 }
535}
536
537bool UIHelpViewer::isFindInPageWidgetVisible() const
538{
539 if (m_pFindInPageWidget)
540 return m_pFindInPageWidget->isVisible();
541 return false;
542}
543
544void UIHelpViewer::setZoomPercentage(int iZoomPercentage)
545{
546 m_iZoomPercentage = iZoomPercentage;
547 clearOverlay();
548 scaleFont();
549 scaleImages();
550}
551
552void UIHelpViewer::setHelpFileList(const QList<QUrl> &helpFileList)
553{
554 m_helpFileList = helpFileList;
555 /* File list necessary to get the image data from the help engine: */
556 iterateDocumentImages();
557 scaleImages();
558}
559
560bool UIHelpViewer::hasSelectedText() const
561{
562 return textCursor().hasSelection();
563}
564
565void UIHelpViewer::contextMenuEvent(QContextMenuEvent *event)
566{
567 QMenu menu;
568
569 if (textCursor().hasSelection())
570 {
571 QAction *pCopySelectedTextAction = new QAction(UIHelpBrowserWidget::tr("Copy Selected Text"));
572 connect(pCopySelectedTextAction, &QAction::triggered,
573 this, &UIHelpViewer::copy);
574 menu.addAction(pCopySelectedTextAction);
575 menu.addSeparator();
576 }
577
578 UIContextMenuNavigationAction *pNavigationActions = new UIContextMenuNavigationAction;
579 pNavigationActions->setBackwardAvailable(isBackwardAvailable());
580 pNavigationActions->setForwardAvailable(isForwardAvailable());
581
582 connect(pNavigationActions, &UIContextMenuNavigationAction::sigGoBackward,
583 this, &UIHelpViewer::sigGoBackward);
584 connect(pNavigationActions, &UIContextMenuNavigationAction::sigGoForward,
585 this, &UIHelpViewer::sigGoForward);
586 connect(pNavigationActions, &UIContextMenuNavigationAction::sigGoHome,
587 this, &UIHelpViewer::sigGoHome);
588 connect(pNavigationActions, &UIContextMenuNavigationAction::sigReloadPage,
589 this, &UIHelpViewer::reload);
590 connect(pNavigationActions, &UIContextMenuNavigationAction::sigAddBookmark,
591 this, &UIHelpViewer::sigAddBookmark);
592
593 QAction *pOpenLinkAction = new QAction(UIHelpBrowserWidget::tr("Open Link"));
594 connect(pOpenLinkAction, &QAction::triggered,
595 this, &UIHelpViewer::sltOpenLink);
596
597 QAction *pOpenInNewTabAction = new QAction(UIHelpBrowserWidget::tr("Open Link in New Tab"));
598 connect(pOpenInNewTabAction, &QAction::triggered,
599 this, &UIHelpViewer::sltOpenLinkInNewTab);
600
601 QAction *pCopyLink = new QAction(UIHelpBrowserWidget::tr("Copy Link"));
602 connect(pCopyLink, &QAction::triggered,
603 this, &UIHelpViewer::sltCopyLink);
604
605 QAction *pFindInPage = new QAction(UIHelpBrowserWidget::tr("Find in Page"));
606 pFindInPage->setCheckable(true);
607 if (m_pFindInPageWidget)
608 pFindInPage->setChecked(m_pFindInPageWidget->isVisible());
609 connect(pFindInPage, &QAction::toggled, this, &UIHelpViewer::sltToggleFindInPageWidget);
610
611 menu.addAction(pNavigationActions);
612 menu.addAction(pOpenLinkAction);
613 menu.addAction(pOpenInNewTabAction);
614 menu.addAction(pCopyLink);
615 menu.addAction(pFindInPage);
616
617 QString strAnchor = anchorAt(event->pos());
618 if (!strAnchor.isEmpty())
619 {
620 QString strLink = source().resolved(anchorAt(event->pos())).toString();
621 pOpenLinkAction->setData(strLink);
622 pOpenInNewTabAction->setData(strLink);
623 pCopyLink->setData(strLink);
624 }
625 else
626 {
627 pOpenLinkAction->setEnabled(false);
628 pOpenInNewTabAction->setEnabled(false);
629 pCopyLink->setEnabled(false);
630 }
631
632 menu.exec(event->globalPos());
633}
634
635void UIHelpViewer::resizeEvent(QResizeEvent *pEvent)
636{
637 if (m_fOverlayMode)
638 clearOverlay();
639 /* Make sure the widget stays inside the parent during parent resize: */
640 if (m_pFindInPageWidget)
641 {
642 if (!isRectInside(m_pFindInPageWidget->geometry(), m_iMarginForFindWidget))
643 moveFindWidgetIn(m_iMarginForFindWidget);
644 }
645 QIWithRetranslateUI<QTextBrowser>::resizeEvent(pEvent);
646}
647
648void UIHelpViewer::wheelEvent(QWheelEvent *pEvent)
649{
650 if (m_fOverlayMode && !pEvent)
651 return;
652 /* QTextBrowser::wheelEvent scales font when some modifiers are pressed. We dont want that: */
653 if (pEvent->modifiers() == Qt::NoModifier)
654 QTextBrowser::wheelEvent(pEvent);
655 else if (pEvent->modifiers() & Qt::ControlModifier)
656 {
657 if (pEvent->angleDelta().y() > 0)
658 emit sigZoomRequest(ZoomOperation_In);
659 else if (pEvent->angleDelta().y() < 0)
660 emit sigZoomRequest(ZoomOperation_Out);
661 }
662}
663
664void UIHelpViewer::mouseReleaseEvent(QMouseEvent *pEvent)
665{
666 /* If overlay mode is active just clear it and return: */
667 bool fOverlayMode = m_fOverlayMode;
668 clearOverlay();
669 if (fOverlayMode)
670 return;
671 QString strAnchor = anchorAt(pEvent->position().toPoint());
672
673 if (!strAnchor.isEmpty())
674 {
675 QString strLink = source().resolved(strAnchor).toString();
676 QFileInfo fInfo(strLink);
677 QMimeDatabase base;
678 QMimeType type = base.mimeTypeForFile(fInfo);
679 if (type.isValid() && type.inherits("image/png"))
680 {
681 if (!fOverlayMode)
682 loadImage(source().resolved(strAnchor));
683 return;
684 }
685 if (source().resolved(strAnchor).scheme() != "qthelp" && pEvent->button() == Qt::LeftButton)
686 {
687 uiCommon().openURL(strLink);
688 return;
689 }
690
691 if ((pEvent->modifiers() & Qt::ControlModifier) ||
692 pEvent->button() == Qt::MiddleButton)
693 {
694
695 emit sigOpenLinkInNewTab(strLink, true);
696 return;
697 }
698 }
699 QIWithRetranslateUI<QTextBrowser>::mousePressEvent(pEvent);
700}
701
702void UIHelpViewer::mousePressEvent(QMouseEvent *pEvent)
703{
704 QIWithRetranslateUI<QTextBrowser>::mousePressEvent(pEvent);
705}
706
707void UIHelpViewer::mouseMoveEvent(QMouseEvent *pEvent)
708{
709 /*if (m_fOverlayMode)
710 return;*/
711 QIWithRetranslateUI<QTextBrowser>::mouseMoveEvent(pEvent);
712}
713
714void UIHelpViewer::mouseDoubleClickEvent(QMouseEvent *pEvent)
715{
716 clearOverlay();
717 QIWithRetranslateUI<QTextBrowser>::mouseDoubleClickEvent(pEvent);
718}
719
720void UIHelpViewer::paintEvent(QPaintEvent *pEvent)
721{
722 QIWithRetranslateUI<QTextBrowser>::paintEvent(pEvent);
723 QPainter painter(viewport());
724 foreach(const DocumentImage &image, m_imageMap)
725 {
726 QRect rect = cursorRect(image.m_textCursor);
727 QPixmap newPixmap = image.m_pixmap.scaledToWidth(image.m_fScaledWidth, Qt::SmoothTransformation);
728 QRectF imageRect(rect.x() - newPixmap.width(), rect.y(), newPixmap.width(), newPixmap.height());
729
730 int iMargin = 3;
731 QRectF fillRect(imageRect.x() - iMargin, imageRect.y() - iMargin,
732 imageRect.width() + 2 * iMargin, imageRect.height() + 2 * iMargin);
733 /** @todo I need to find the default color somehow and replace hard coded Qt::white. */
734 painter.fillRect(fillRect, Qt::white);
735 painter.drawPixmap(imageRect, newPixmap, newPixmap.rect());
736 }
737}
738
739bool UIHelpViewer::eventFilter(QObject *pObject, QEvent *pEvent)
740{
741 if (pObject == m_pOverlayLabel)
742 {
743 if (pEvent->type() == QEvent::MouseButtonPress ||
744 pEvent->type() == QEvent::MouseButtonDblClick)
745 clearOverlay();
746 }
747 return QIWithRetranslateUI<QTextBrowser>::eventFilter(pObject, pEvent);
748}
749
750void UIHelpViewer::keyPressEvent(QKeyEvent *pEvent)
751{
752 if (pEvent && pEvent->key() == Qt::Key_Escape)
753 clearOverlay();
754 if (pEvent && pEvent->modifiers() &Qt::ControlModifier)
755 {
756 switch (pEvent->key())
757 {
758 case Qt::Key_Equal:
759 emit sigZoomRequest(ZoomOperation_In);
760 break;
761 case Qt::Key_Minus:
762 emit sigZoomRequest(ZoomOperation_Out);
763 break;
764 case Qt::Key_0:
765 emit sigZoomRequest(ZoomOperation_Reset);
766 break;
767 default:
768 break;
769 }
770 }
771 QIWithRetranslateUI<QTextBrowser>::keyPressEvent(pEvent);
772}
773
774void UIHelpViewer::retranslateUi()
775{
776}
777
778void UIHelpViewer::moveFindWidgetIn(int iMargin)
779{
780 if (!m_pFindInPageWidget)
781 return;
782
783 QRect rect = m_pFindInPageWidget->geometry();
784 if (rect.left() < iMargin)
785 rect.translate(-rect.left() + iMargin, 0);
786 if (rect.right() > width() - iMargin)
787 rect.translate((width() - iMargin - rect.right()), 0);
788 if (rect.top() < iMargin)
789 rect.translate(0, -rect.top() + iMargin);
790
791 if (rect.bottom() > height() - iMargin)
792 rect.translate(0, (height() - iMargin - rect.bottom()));
793 m_pFindInPageWidget->setGeometry(rect);
794 m_pFindInPageWidget->update();
795}
796
797bool UIHelpViewer::isRectInside(const QRect &rect, int iMargin) const
798{
799 if (rect.left() < iMargin || rect.top() < iMargin)
800 return false;
801 if (rect.right() > width() - iMargin || rect.bottom() > height() - iMargin)
802 return false;
803 return true;
804}
805
806void UIHelpViewer::findAllMatches(const QString &searchString)
807{
808 QTextDocument *pDocument = document();
809 AssertReturnVoid(pDocument);
810
811 m_matchedCursorPosition.clear();
812 if (searchString.isEmpty())
813 return;
814 QTextCursor cursor(pDocument);
815 QTextDocument::FindFlags flags;
816 while (!cursor.isNull() && !cursor.atEnd())
817 {
818 cursor = pDocument->find(searchString, cursor, flags);
819 if (!cursor.isNull())
820 m_matchedCursorPosition << cursor.position() - searchString.length();
821 }
822}
823
824void UIHelpViewer::highlightFinds(int iSearchTermLength)
825{
826 QList<QTextEdit::ExtraSelection> extraSelections;
827 for (int i = 0; i < m_matchedCursorPosition.size(); ++i)
828 {
829 QTextEdit::ExtraSelection selection;
830 QTextCursor cursor = textCursor();
831 cursor.setPosition(m_matchedCursorPosition[i]);
832 cursor.setPosition(m_matchedCursorPosition[i] + iSearchTermLength, QTextCursor::KeepAnchor);
833 QTextCharFormat format = cursor.charFormat();
834 format.setBackground(Qt::yellow);
835
836 selection.cursor = cursor;
837 selection.format = format;
838 extraSelections.append(selection);
839 }
840 setExtraSelections(extraSelections);
841}
842
843void UIHelpViewer::selectMatch(int iMatchIndex, int iSearchStringLength)
844{
845 QTextCursor cursor = textCursor();
846 /* Move the cursor to the beginning of the matched string: */
847 cursor.setPosition(m_matchedCursorPosition.at(iMatchIndex), QTextCursor::MoveAnchor);
848 /* Move the cursor to the end of the matched string while keeping the anchor at the begining thus selecting the text: */
849 cursor.setPosition(m_matchedCursorPosition.at(iMatchIndex) + iSearchStringLength, QTextCursor::KeepAnchor);
850 ensureCursorVisible();
851 setTextCursor(cursor);
852}
853
854void UIHelpViewer::sltOpenLinkInNewTab()
855{
856 QAction *pSender = qobject_cast<QAction*>(sender());
857 if (!pSender)
858 return;
859 QUrl url = pSender->data().toUrl();
860 if (url.isValid())
861 emit sigOpenLinkInNewTab(url, false);
862}
863
864void UIHelpViewer::sltOpenLink()
865{
866 QAction *pSender = qobject_cast<QAction*>(sender());
867 if (!pSender)
868 return;
869 QUrl url = pSender->data().toUrl();
870 if (url.isValid())
871 setSource(url);
872}
873
874void UIHelpViewer::sltCopyLink()
875{
876 QAction *pSender = qobject_cast<QAction*>(sender());
877 if (!pSender)
878 return;
879 QUrl url = pSender->data().toUrl();
880 if (url.isValid())
881 {
882 QClipboard *pClipboard = QApplication::clipboard();
883 if (pClipboard)
884 pClipboard->setText(url.toString());
885 }
886}
887
888void UIHelpViewer::sltFindWidgetDrag(const QPoint &delta)
889{
890 if (!m_pFindInPageWidget)
891 return;
892 QRect geo = m_pFindInPageWidget->geometry();
893 geo.translate(delta);
894
895 /* Allow the move if m_pFindInPageWidget stays inside after the move: */
896 if (isRectInside(geo, m_iMarginForFindWidget))
897 m_pFindInPageWidget->move(m_pFindInPageWidget->pos() + delta);
898 m_fFindWidgetDragged = true;
899 update();
900}
901
902void UIHelpViewer::sltFindInPageSearchTextChange(const QString &strSearchText)
903{
904 m_iSearchTermLength = strSearchText.length();
905 findAllMatches(strSearchText);
906 highlightFinds(m_iSearchTermLength);
907 selectMatch(0, m_iSearchTermLength);
908 if (m_pFindInPageWidget)
909 m_pFindInPageWidget->setMatchCountAndCurrentIndex(m_matchedCursorPosition.size(), 0);
910}
911
912void UIHelpViewer::sltSelectPreviousMatch()
913{
914 m_iSelectedMatchIndex = m_iSelectedMatchIndex <= 0 ? m_matchedCursorPosition.size() - 1 : (m_iSelectedMatchIndex - 1);
915 selectMatch(m_iSelectedMatchIndex, m_iSearchTermLength);
916 if (m_pFindInPageWidget)
917 m_pFindInPageWidget->setMatchCountAndCurrentIndex(m_matchedCursorPosition.size(), m_iSelectedMatchIndex);
918}
919
920void UIHelpViewer::sltSelectNextMatch()
921{
922 m_iSelectedMatchIndex = m_iSelectedMatchIndex >= m_matchedCursorPosition.size() - 1 ? 0 : (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::iterateDocumentImages()
929{
930 m_imageMap.clear();
931 QTextCursor cursor = textCursor();
932 cursor.movePosition(QTextCursor::Start);
933 while (!cursor.atEnd())
934 {
935 cursor.movePosition(QTextCursor::NextCharacter);
936 if (cursor.charFormat().isImageFormat())
937 {
938 QTextImageFormat imageFormat = cursor.charFormat().toImageFormat();
939 /* There seems to be two cursors per image. Use the first one: */
940 if (m_imageMap.contains(imageFormat.name()))
941 continue;
942 QHash<QString, DocumentImage>::iterator iterator = m_imageMap.insert(imageFormat.name(), DocumentImage());
943 DocumentImage &image = iterator.value();
944 image.m_fInitialWidth = imageFormat.width();
945 image.m_strName = imageFormat.name();
946 image.m_textCursor = cursor;
947 QUrl imageFileUrl;
948 foreach (const QUrl &fileUrl, m_helpFileList)
949 {
950 if (fileUrl.toString().contains(imageFormat.name(), Qt::CaseInsensitive))
951 {
952 imageFileUrl = fileUrl;
953 break;
954 }
955 }
956 if (imageFileUrl.isValid())
957 {
958 QByteArray fileData = m_pHelpEngine->fileData(imageFileUrl);
959 if (!fileData.isEmpty())
960 image.m_pixmap.loadFromData(fileData,"PNG");
961 }
962 }
963 }
964}
965
966void UIHelpViewer::scaleFont()
967{
968 QFont mFont = font();
969 mFont.setPointSize(m_iInitialFontPointSize * m_iZoomPercentage / 100.);
970 setFont(mFont);
971}
972
973void UIHelpViewer::scaleImages()
974{
975 for (QHash<QString, DocumentImage>::iterator iterator = m_imageMap.begin();
976 iterator != m_imageMap.end(); ++iterator)
977 {
978 DocumentImage &image = *iterator;
979 QTextCursor cursor = image.m_textCursor;
980 QTextCharFormat format = cursor.charFormat();
981 if (!format.isImageFormat())
982 continue;
983 QTextImageFormat imageFormat = format.toImageFormat();
984 image.m_fScaledWidth = image.m_fInitialWidth * m_iZoomPercentage / 100.;
985 imageFormat.setWidth(image.m_fScaledWidth);
986 cursor.deletePreviousChar();
987 cursor.deleteChar();
988 cursor.insertImage(imageFormat);
989 }
990}
991
992void UIHelpViewer::clearOverlay()
993{
994 AssertReturnVoid(m_pOverlayLabel);
995
996 if (!m_fOverlayMode)
997 return;
998 m_overlayPixmap = QPixmap();
999 m_fOverlayMode = false;
1000 if (m_pOverlayBlurEffect)
1001 m_pOverlayBlurEffect->setEnabled(false);
1002 m_pOverlayLabel->hide();
1003}
1004
1005void UIHelpViewer::enableOverlay()
1006{
1007 AssertReturnVoid(m_pOverlayLabel);
1008 m_fOverlayMode = true;
1009 if (m_pOverlayBlurEffect)
1010 m_pOverlayBlurEffect->setEnabled(true);
1011 toggleFindInPageWidget(false);
1012
1013 /* Scale the image to 1:1 as long as it fits into avaible space (minus some margins and scrollbar sizes): */
1014 int vWidth = 0;
1015 if (verticalScrollBar() && verticalScrollBar()->isVisible())
1016 vWidth = verticalScrollBar()->width();
1017 int hMargin = qApp->style()->pixelMetric(QStyle::PM_LayoutLeftMargin) +
1018 qApp->style()->pixelMetric(QStyle::PM_LayoutRightMargin) + vWidth;
1019
1020 int hHeight = 0;
1021 if (horizontalScrollBar() && horizontalScrollBar()->isVisible())
1022 hHeight = horizontalScrollBar()->height();
1023 int vMargin = qApp->style()->pixelMetric(QStyle::PM_LayoutTopMargin) +
1024 qApp->style()->pixelMetric(QStyle::PM_LayoutBottomMargin) + hHeight;
1025
1026 QSize size(qMin(width() - hMargin, m_overlayPixmap.width()),
1027 qMin(height() - vMargin, m_overlayPixmap.height()));
1028 m_pOverlayLabel->setPixmap(m_overlayPixmap.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation));
1029 m_pOverlayLabel->show();
1030
1031 /* Center the label: */
1032 int x = 0.5 * (width() - vWidth - m_pOverlayLabel->width());
1033 int y = 0.5 * (height() - hHeight - m_pOverlayLabel->height());
1034 m_pOverlayLabel->move(x, y);
1035}
1036
1037void UIHelpViewer::loadImage(const QUrl &imageFileUrl)
1038{
1039 clearOverlay();
1040 /* Dont zoom into image if mouse button released after a mouse drag: */
1041 if (textCursor().hasSelection())
1042 return;
1043 if (!imageFileUrl.isValid())
1044 return;
1045 QByteArray fileData = m_pHelpEngine->fileData(imageFileUrl);
1046 if (!fileData.isEmpty())
1047 {
1048 m_overlayPixmap.loadFromData(fileData,"PNG");
1049 if (!m_overlayPixmap.isNull())
1050 enableOverlay();
1051 }
1052}
1053
1054
1055#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