VirtualBox

Changeset 98312 in vbox for trunk/src


Ignore:
Timestamp:
Jan 26, 2023 12:52:40 PM (2 years ago)
Author:
vboxsync
Message:

FE/Qt: Implement Copy action for our QIRichTextLabel widget; Default one QTextEdit action was not triggerable by shortcut and was not respecting selection context.

Location:
trunk/src/VBox/Frontends/VirtualBox/src/extensions
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/extensions/QIRichTextLabel.cpp

    r98103 r98312  
    2828/* Qt includes: */
    2929#include <QAccessibleWidget>
     30#include <QAction>
     31#include <QApplication>
     32#include <QClipboard>
    3033#include <QtMath>
    3134#include <QUrl>
     
    101104
    102105QIRichTextLabel::QIRichTextLabel(QWidget *pParent)
    103     : QWidget(pParent)
     106    : QIWithRetranslateUI<QWidget>(pParent)
    104107    , m_pTextBrowser()
     108    , m_pActionCopy(0)
     109    , m_fCopyAvailable(false)
    105110    , m_iMinimumTextWidth(0)
    106111{
     
    124129            /* Configure text-browser: */
    125130            m_pTextBrowser->setReadOnly(true);
    126             m_pTextBrowser->setFocusPolicy(Qt::NoFocus);
     131            m_pTextBrowser->setFocusPolicy(Qt::ClickFocus);
    127132            m_pTextBrowser->setFrameShape(QFrame::NoFrame);
    128133            m_pTextBrowser->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    129134            m_pTextBrowser->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
     135            m_pTextBrowser->setContextMenuPolicy(Qt::ActionsContextMenu);
    130136            m_pTextBrowser->setOpenExternalLinks(true);
    131137
     
    140146            /* Setup connections finally: */
    141147            connect(m_pTextBrowser, &QTextBrowser::anchorClicked, this, &QIRichTextLabel::sigLinkClicked);
     148            connect(m_pTextBrowser, &QTextBrowser::copyAvailable, this, &QIRichTextLabel::sltHandleCopyAvailable);
     149
     150            /* Create context-menu copy action for text-browser: */
     151            m_pActionCopy = new QAction(m_pTextBrowser);
     152            if (m_pActionCopy)
     153            {
     154                m_pActionCopy->setShortcut(QKeySequence(QKeySequence::Copy));
     155                m_pActionCopy->setShortcutContext(Qt::WidgetShortcut);
     156                connect(m_pActionCopy, &QAction::triggered, this, &QIRichTextLabel::copy);
     157                m_pTextBrowser->addAction(m_pActionCopy);
     158            }
    142159        }
    143160
     
    145162        pMainLayout->addWidget(m_pTextBrowser);
    146163    }
     164
     165    /* Apply language settings: */
     166    retranslateUi();
    147167}
    148168
     
    246266    setMinimumTextWidth(m_iMinimumTextWidth == 0 ? newSize.width() : m_iMinimumTextWidth);
    247267}
     268
     269void QIRichTextLabel::copy()
     270{
     271    // WORKAROUND:
     272    // We should distinguish whether copy() is available or not.
     273    //  If it is, we can use QTextBrowser::copy() directly to
     274    //  copy selected part of text.  Otherwise we have to use
     275    //  QTextBrowser::toPlainText() to get the whole desirable
     276    //  text and put it to QClipboard ourselves.
     277    if (m_fCopyAvailable)
     278        m_pTextBrowser->copy();
     279    else
     280    {
     281        /* Copy the current text to the global and selection clipboards: */
     282        const QString strText = m_pTextBrowser->toPlainText();
     283        QApplication::clipboard()->setText(strText, QClipboard::Clipboard);
     284        QApplication::clipboard()->setText(strText, QClipboard::Selection);
     285    }
     286}
     287
     288void QIRichTextLabel::retranslateUi()
     289{
     290    if (m_pActionCopy)
     291        m_pActionCopy->setText(tr("&Copy"));
     292}
  • trunk/src/VBox/Frontends/VirtualBox/src/extensions/QIRichTextLabel.h

    r98103 r98312  
    3636
    3737/* GUI includes: */
     38#include "QIWithRetranslateUI.h"
    3839#include "UILibraryDefs.h"
     40
     41/* Forward declarations: */
     42class QAction;
    3943
    4044/** QLabel analog to reflect rich-text,
    4145 ** based on private QTextBrowser functionality. */
    42 class SHARED_LIBRARY_STUFF QIRichTextLabel : public QWidget
     46class SHARED_LIBRARY_STUFF QIRichTextLabel : public QIWithRetranslateUI<QWidget>
    4347{
    4448    Q_OBJECT;
     
    8892    void setText(const QString &strText);
    8993
     94    /** Copies text-browser text into clipboard. */
     95    void copy();
     96
     97protected:
     98
     99    /** Handles translation event. */
     100    virtual void retranslateUi() RT_OVERRIDE;
     101
     102private slots:
     103
     104    /** Handles the fact of text-browser text copy available.
     105      * @param  fYes  Brings whether some text is selected and can
     106      *               be copied directly by QTextBrowser::copy() call. */
     107    void sltHandleCopyAvailable(bool fYes) { m_fCopyAvailable = fYes; }
     108
    90109private:
    91110
    92111    /** Holds the text-browser instance. */
    93112    QTextBrowser *m_pTextBrowser;
     113
     114    /** Holds the context-menu Copy action instance. */
     115    QAction *m_pActionCopy;
     116    /** Holds whether text-browser text copy is available. */
     117    bool     m_fCopyAvailable;
    94118
    95119    /** Holds the minimum text-width. */
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