VirtualBox

Changeset 72153 in vbox for trunk/src


Ignore:
Timestamp:
May 8, 2018 7:00:29 AM (7 years ago)
Author:
vboxsync
Message:

FE/Qt: bugref:9072 Refactor UIVMFilterLineEdit to split its definition and implementation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogViewerFilterPanel.cpp

    r71638 r72153  
    4747#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
    4848
    49 /* UIVMFilterLineEdit class is used to display and modify the list of filter terms.
    50    the terms are displayed as words with spaces in between and it is possible to
    51    remove these terms one by one by selecting them or completely by the clearAll button
    52    located on the right side of the line edit: */
     49
     50/*********************************************************************************************************************************
     51*   UIVMFilterLineEdit definition.                                                                                               *
     52*********************************************************************************************************************************/
     53
     54/** UIVMFilterLineEdit class is used to display and modify the list of filter terms.
     55 *  the terms are displayed as words with spaces in between and it is possible to
     56 *  remove these terms one by one by selecting them or completely by the clearAll button
     57 *  located on the right side of the line edit: */
    5358class UIVMFilterLineEdit : public QLineEdit
    5459{
     
    6267public:
    6368
    64     UIVMFilterLineEdit(QWidget *parent = 0)
    65         :QLineEdit(parent)
    66         , m_pRemoveTermButton(0)
    67         , m_iRemoveTermButtonSize(16)
    68         , m_iTrailingSpaceCount(1)
    69     {
    70         setReadOnly(true);
    71         home(false);
    72         createButtons();
    73        /** Try to guess the width of the space between filter terms so that remove button
    74                 we display when a term is selected does not hide the next/previous word: */
    75         int spaceWidth = fontMetrics().width(' ');
    76         if (spaceWidth != 0)
    77             m_iTrailingSpaceCount = (m_iRemoveTermButtonSize / spaceWidth) + 1;
    78     }
    79 
    80     void addFilterTerm(const QString& filterTermString)
    81     {
    82         if (text().isEmpty())
    83             insert(filterTermString);
    84         else
    85         {
    86             QString newString(filterTermString);
    87             QString space(m_iTrailingSpaceCount, QChar(' '));
    88             insert(newString.prepend(space));
    89         }
    90     }
    91 
    92     void clearAll()
    93     {
    94         if (text().isEmpty())
    95             return;
    96         sltClearAll();
    97     }
     69    UIVMFilterLineEdit(QWidget *parent = 0);
     70    void addFilterTerm(const QString& filterTermString);
     71    void clearAll();
    9872
    9973protected:
    10074
    101     /* Overload the mouseXXXEvent to control how selection is done: */
    102     virtual void        mouseDoubleClickEvent(QMouseEvent *){}
    103     virtual void        mouseMoveEvent(QMouseEvent *){}
    104     virtual void        mousePressEvent(QMouseEvent * event)
    105     {
    106         /* Simulate double mouse click to select a word with a single click: */
    107         QLineEdit::mouseDoubleClickEvent(event);
    108     }
    109 
     75    /* Delete mouseDoubleClick and mouseMoveEvent implementations of the base class */
     76    virtual void        mouseDoubleClickEvent(QMouseEvent *) /* override */{}
     77    virtual void        mouseMoveEvent(QMouseEvent *) /* override */{}
     78    /* Override the mousePressEvent to control how selection is done: */
     79    virtual void        mousePressEvent(QMouseEvent * event) /* override */;
    11080    virtual void        mouseReleaseEvent(QMouseEvent *){}
    111     virtual void paintEvent(QPaintEvent *event)
    112     {
    113         QLineEdit::paintEvent(event);
    114         int clearButtonSize = height();
    115         m_pClearAllButton->setGeometry(width() - clearButtonSize, 0, clearButtonSize, clearButtonSize);
    116         /* If we have a selected term move the m_pRemoveTermButton to the end of the
    117            or start of the word (depending on the location of the word within line edit itself: */
    118         if (hasSelectedText())
    119         {
    120             m_pRemoveTermButton->show();
    121             int buttonY = 0.5 * (height() - 16);
    122             int buttonSize = m_iRemoveTermButtonSize;
    123             int charWidth = fontMetrics().width('x');
    124             int buttonLeft = cursorRect().right() - 0.5 * charWidth;
    125             /* If buttonLeft is in far left of the line edit, move the
    126                button to left side of the selected word: */
    127             if (buttonLeft + buttonSize  >=  width() - clearButtonSize)
    128             {
    129                 int selectionWidth = charWidth * selectedText().length();
    130                 buttonLeft -= (selectionWidth + buttonSize);
    131             }
    132             m_pRemoveTermButton->setGeometry(buttonLeft, buttonY, buttonSize, buttonSize);
    133         }
    134         else
    135             m_pRemoveTermButton->hide();
    136     }
     81    virtual void        paintEvent(QPaintEvent *event);
    13782
    13883private slots:
    13984
    14085    /* Nofifies the listeners that selected word (filter term) has been removed: */
    141     void sltRemoveFilterTerm()
    142     {
    143         if (!hasSelectedText())
    144             return;
    145         emit sigFilterTermRemoved(selectedText());
    146         /* Remove the string from text() including the trailing space: */
    147         setText(text().remove(selectionStart(), selectedText().length() + m_iTrailingSpaceCount));
    148     }
    149 
     86    void sltRemoveFilterTerm();
    15087    /* The whole content is removed. Listeners are notified: */
    151     void sltClearAll()
    152     {
    153         /* Check if we have some text to avoid recursive calls: */
    154         if (text().isEmpty())
    155             return;
    156 
    157         clear();
    158         emit sigClearAll();
    159     }
     88    void sltClearAll();
    16089
    16190private:
    16291
    163     void createButtons()
    164     {
    165         m_pRemoveTermButton = new QToolButton(this);
    166         if (m_pRemoveTermButton)
    167         {
    168             m_pRemoveTermButton->setIcon(m_pRemoveTermButton->style()->standardIcon(QStyle::SP_TitleBarCloseButton));
    169             m_pRemoveTermButton->hide();
    170             connect(m_pRemoveTermButton, &QToolButton::clicked, this, &UIVMFilterLineEdit::sltRemoveFilterTerm);
    171         }
    172 
    173         m_pClearAllButton = new QToolButton(this);
    174         if (m_pClearAllButton)
    175         {
    176             m_pClearAllButton->setIcon(m_pRemoveTermButton->style()->standardIcon(QStyle::SP_LineEditClearButton));
    177             connect(m_pClearAllButton, &QToolButton::clicked, this, &UIVMFilterLineEdit::sltClearAll);
    178         }
    179     }
    180 
     92    void         createButtons();
    18193    QToolButton *m_pRemoveTermButton;
    18294    QToolButton *m_pClearAllButton;
     
    18496    int          m_iTrailingSpaceCount;
    18597};
     98
     99
     100/*********************************************************************************************************************************
     101*   UIVMFilterLineEdit implementation.                                                                                           *
     102*********************************************************************************************************************************/
     103
     104UIVMFilterLineEdit::UIVMFilterLineEdit(QWidget *parent /*= 0*/)
     105    :QLineEdit(parent)
     106    , m_pRemoveTermButton(0)
     107    , m_iRemoveTermButtonSize(16)
     108    , m_iTrailingSpaceCount(1)
     109{
     110    setReadOnly(true);
     111    home(false);
     112    createButtons();
     113    /** Try to guess the width of the space between filter terms so that remove button
     114        we display when a term is selected does not hide the next/previous word: */
     115    int spaceWidth = fontMetrics().width(' ');
     116    if (spaceWidth != 0)
     117        m_iTrailingSpaceCount = (m_iRemoveTermButtonSize / spaceWidth) + 1;
     118}
     119
     120void UIVMFilterLineEdit::addFilterTerm(const QString& filterTermString)
     121{
     122    if (text().isEmpty())
     123        insert(filterTermString);
     124    else
     125    {
     126        QString newString(filterTermString);
     127        QString space(m_iTrailingSpaceCount, QChar(' '));
     128        insert(newString.prepend(space));
     129    }
     130}
     131
     132void UIVMFilterLineEdit::clearAll()
     133{
     134    if (text().isEmpty())
     135        return;
     136    sltClearAll();
     137}
     138
     139void UIVMFilterLineEdit::mousePressEvent(QMouseEvent * event)
     140{
     141    /* Simulate double mouse click to select a word with a single click: */
     142    QLineEdit::mouseDoubleClickEvent(event);
     143}
     144
     145void UIVMFilterLineEdit::paintEvent(QPaintEvent *event)
     146{
     147    QLineEdit::paintEvent(event);
     148    int clearButtonSize = height();
     149    m_pClearAllButton->setGeometry(width() - clearButtonSize, 0, clearButtonSize, clearButtonSize);
     150    /* If we have a selected term move the m_pRemoveTermButton to the end of the
     151       or start of the word (depending on the location of the word within line edit itself: */
     152    if (hasSelectedText())
     153    {
     154        m_pRemoveTermButton->show();
     155        int buttonY = 0.5 * (height() - 16);
     156        int buttonSize = m_iRemoveTermButtonSize;
     157        int charWidth = fontMetrics().width('x');
     158        int buttonLeft = cursorRect().right() - 0.5 * charWidth;
     159        /* If buttonLeft is in far left of the line edit, move the
     160           button to left side of the selected word: */
     161        if (buttonLeft + buttonSize  >=  width() - clearButtonSize)
     162        {
     163            int selectionWidth = charWidth * selectedText().length();
     164            buttonLeft -= (selectionWidth + buttonSize);
     165        }
     166        m_pRemoveTermButton->setGeometry(buttonLeft, buttonY, buttonSize, buttonSize);
     167    }
     168    else
     169        m_pRemoveTermButton->hide();
     170}
     171
     172void UIVMFilterLineEdit::sltRemoveFilterTerm()
     173{
     174    if (!hasSelectedText())
     175        return;
     176    emit sigFilterTermRemoved(selectedText());
     177    /* Remove the string from text() including the trailing space: */
     178    setText(text().remove(selectionStart(), selectedText().length() + m_iTrailingSpaceCount));
     179}
     180
     181void UIVMFilterLineEdit::sltClearAll()
     182{
     183    /* Check if we have some text to avoid recursive calls: */
     184    if (text().isEmpty())
     185        return;
     186
     187    clear();
     188    emit sigClearAll();
     189}
     190
     191void UIVMFilterLineEdit::createButtons()
     192{
     193    m_pRemoveTermButton = new QToolButton(this);
     194    if (m_pRemoveTermButton)
     195    {
     196        m_pRemoveTermButton->setIcon(m_pRemoveTermButton->style()->standardIcon(QStyle::SP_TitleBarCloseButton));
     197        m_pRemoveTermButton->hide();
     198        connect(m_pRemoveTermButton, &QToolButton::clicked, this, &UIVMFilterLineEdit::sltRemoveFilterTerm);
     199    }
     200
     201    m_pClearAllButton = new QToolButton(this);
     202    if (m_pClearAllButton)
     203    {
     204        m_pClearAllButton->setIcon(m_pRemoveTermButton->style()->standardIcon(QStyle::SP_LineEditClearButton));
     205        connect(m_pClearAllButton, &QToolButton::clicked, this, &UIVMFilterLineEdit::sltClearAll);
     206    }
     207}
    186208
    187209UIVMLogViewerFilterPanel::UIVMLogViewerFilterPanel(QWidget *pParent, UIVMLogViewerWidget *pViewer)
     
    541563
    542564#include "UIVMLogViewerFilterPanel.moc"
    543 
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