VirtualBox

Changeset 86800 in vbox for trunk/src/VBox/Frontends


Ignore:
Timestamp:
Nov 3, 2020 6:50:12 PM (4 years ago)
Author:
vboxsync
Message:

FE/Qt: bugref:9831. Adding a new context menu item text browser to open links on a new tab

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/helpbrowser/UIHelpBrowserWidget.cpp

    r86799 r86800  
    7575*********************************************************************************************************************************/
    7676
    77 class UIHelpBrowserViewer : public QTextBrowser
     77class UIHelpBrowserViewer : public QIWithRetranslateUI<QTextBrowser>
    7878{
    7979    Q_OBJECT;
     80
     81signals:
     82
     83    void sigOpenLinkInNewTab(const QUrl &url);
    8084
    8185public:
     
    8892public slots:
    8993
    90     //virtual void setSource(const QUrl &name) /* override */;
     94
     95protected:
     96
     97    void contextMenuEvent(QContextMenuEvent *event) /* override */;
     98
     99private slots:
     100
     101    void sltHandleOpenInNewTab();
    91102
    92103private:
    93104
     105    void retranslateUi();
    94106    const QHelpEngine* m_pHelpEngine;
     107    QString m_strOpenInNewTab;
    95108};
    96109
     
    106119
    107120    void sigSourceChanged(const QUrl &url);
     121    void sigTitleUpdate(const QString &strTitle);
     122    void sigOpenLinkInNewTab(const QUrl &url);
    108123
    109124public:
     
    114129    QUrl source() const;
    115130    void setSource(const QUrl &url);
     131    QString documentTitle() const;
    116132
    117133private slots:
     
    170186    /* Return the list of urls of all open tabs as QStringList. */
    171187    QStringList tabUrlList();
     188    void addNewTab(const QUrl &initialUrl);
     189
     190private slots:
     191
     192    void sltHandletabTitleChange(const QString &strTitle);
     193    void sltHandleOpenLinkInNewTab(const QUrl &url);
    172194
    173195private:
     
    178200    QUrl m_homeUrl;
    179201    QStringList m_savedUrlList;
     202    bool m_fSwitchToNewTab;
    180203};
    181204
     
    223246}
    224247
     248QString UIHelpBrowserTab::documentTitle() const
     249{
     250    if (!m_pContentViewer)
     251        return QString();
     252    return m_pContentViewer->documentTitle();
     253}
     254
    225255void UIHelpBrowserTab::prepare(const QUrl &initialUrl)
    226256{
     
    247277    connect(m_pContentViewer, &UIHelpBrowserViewer::anchorClicked,
    248278        this, &UIHelpBrowserTab::sltAnchorClicked);
    249 
     279    connect(m_pContentViewer, &UIHelpBrowserViewer::sigOpenLinkInNewTab,
     280        this, &UIHelpBrowserTab::sigOpenLinkInNewTab);
    250281    m_pContentViewer->setSource(initialUrl, m_strPageNotFoundText);
    251282}
     
    357388        m_pForwardAction->setEnabled(m_pContentViewer->isForwardAvailable());
    358389
     390    emit sigTitleUpdate(m_pContentViewer->historyTitle(0));
    359391}
    360392
     
    391423
    392424UIHelpBrowserViewer::UIHelpBrowserViewer(const QHelpEngine *pHelpEngine, QWidget *pParent /* = 0 */)
    393     :QTextBrowser(pParent)
     425    :QIWithRetranslateUI<QTextBrowser>(pParent)
    394426    , m_pHelpEngine(pHelpEngine)
    395427{
    396428    Q_UNUSED(pHelpEngine);
     429    retranslateUi();
    397430}
    398431
     
    419452}
    420453
     454void UIHelpBrowserViewer::contextMenuEvent(QContextMenuEvent *event)
     455{
     456    QMenu *pMenu = createStandardContextMenu();
     457    QString strAnchor = anchorAt(event->pos());
     458    if (!strAnchor.isEmpty())
     459    {
     460        QString strLink = source().resolved(anchorAt(event->pos())).toString();
     461
     462        QAction *pOpenInNewTabAction = new QAction(m_strOpenInNewTab);
     463        pOpenInNewTabAction->setData(strLink);
     464        connect(pOpenInNewTabAction, &QAction::triggered,
     465                this, &UIHelpBrowserViewer::sltHandleOpenInNewTab);
     466        pMenu->addAction(pOpenInNewTabAction);
     467    }
     468    pMenu->exec(event->globalPos());
     469    delete pMenu;
     470}
     471
     472void UIHelpBrowserViewer::retranslateUi()
     473{
     474    m_strOpenInNewTab = UIHelpBrowserWidget::tr("Open Link in New Tab");
     475}
     476
     477void UIHelpBrowserViewer::sltHandleOpenInNewTab()
     478{
     479    QAction *pSender = qobject_cast<QAction*>(sender());
     480    if (!pSender)
     481        return;
     482    QUrl url = pSender->data().toUrl();
     483    if (url.isValid())
     484        emit sigOpenLinkInNewTab(url);
     485}
    421486
    422487/*********************************************************************************************************************************
     
    430495    , m_homeUrl(homeUrl)
    431496    , m_savedUrlList(urlList)
     497    , m_fSwitchToNewTab(true)
    432498{
    433499    prepare();
     500}
     501
     502void UIHelpBrowserTabManager::addNewTab(const QUrl &initialUrl)
     503{
     504   UIHelpBrowserTab *pTabWidget = new  UIHelpBrowserTab(m_pHelpEngine, m_homeUrl, initialUrl);
     505   AssertReturnVoid(pTabWidget);
     506   int index = addTab(pTabWidget, pTabWidget->documentTitle());
     507   connect(pTabWidget, &UIHelpBrowserTab::sigSourceChanged,
     508           this, &UIHelpBrowserTabManager::sigSourceChanged);
     509   connect(pTabWidget, &UIHelpBrowserTab::sigTitleUpdate,
     510           this, &UIHelpBrowserTabManager::sltHandletabTitleChange);
     511   connect(pTabWidget, &UIHelpBrowserTab::sigOpenLinkInNewTab,
     512           this, &UIHelpBrowserTabManager::sltHandleOpenLinkInNewTab);
     513   if (m_fSwitchToNewTab)
     514       setCurrentIndex(index);
    434515}
    435516
     
    441522    if (!m_savedUrlList.isEmpty())
    442523        initialUrl = m_savedUrlList[0];
    443     UIHelpBrowserTab *pTabWidget = new  UIHelpBrowserTab(m_pHelpEngine, m_homeUrl, initialUrl);
    444     addTab(pTabWidget, QString());
    445     connect(pTabWidget, &UIHelpBrowserTab::sigSourceChanged,
    446             this, &UIHelpBrowserTabManager::sigSourceChanged);
     524    addNewTab(initialUrl);
    447525}
    448526
     
    474552    }
    475553    return list;
     554}
     555
     556void UIHelpBrowserTabManager::sltHandletabTitleChange(const QString &strTitle)
     557{
     558    for (int i = 0; i < count(); ++i)
     559    {
     560        if (sender() == widget(i))
     561        {
     562            setTabText(i, strTitle);
     563            continue;
     564        }
     565    }
     566}
     567
     568void UIHelpBrowserTabManager::sltHandleOpenLinkInNewTab(const QUrl &url)
     569{
     570    if (url.isValid())
     571        addNewTab(url);
    476572}
    477573
     
    858954    m_pContentWidget->scrollTo(index, QAbstractItemView::EnsureVisible);
    859955    m_pContentWidget->expand(index);
    860 
    861956}
    862957
Note: See TracChangeset for help on using the changeset viewer.

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