VirtualBox

Changeset 77078 in vbox


Ignore:
Timestamp:
Jan 31, 2019 3:34:30 PM (6 years ago)
Author:
vboxsync
Message:

FE/Qt: bugref:9072 Replace the info label of the search panel with an overlay on the text edit.

Location:
trunk/src/VBox/Frontends/VirtualBox/src/logviewer
Files:
8 edited

Legend:

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

    r76606 r77078  
    339339void UIVMLogPage::setCurrentFont(QFont font)
    340340{
    341     if (!m_pTextEdit)
    342         return;
    343     m_pTextEdit->setCurrentFont(font);
    344 }
     341    if (m_pTextEdit)
     342        m_pTextEdit->setCurrentFont(font);
     343}
     344
     345void UIVMLogPage::setSearchResultOverlayShowHide(bool fShow)
     346{
     347    if (m_pTextEdit)
     348        m_pTextEdit->setSearchResultOverlayShowHide(fShow);
     349}
     350
     351void UIVMLogPage::setSearchMatchCount(int iMatchCount)
     352{
     353    if (m_pTextEdit)
     354        m_pTextEdit->setSearchMatchCount(iMatchCount);
     355}
  • trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogPage.h

    r76581 r77078  
    117117    void setCurrentFont(QFont font);
    118118
     119    void setSearchResultOverlayShowHide(bool fShow);
     120    void setSearchMatchCount(int iMatchCount);
     121
    119122private slots:
    120123
  • trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogViewerSearchPanel.cpp

    r77030 r77078  
    8080    , m_pMatchWholeWordCheckBox(0)
    8181    , m_pHighlightAllCheckBox(0)
    82     , m_pWarningIcon(0)
    83     , m_pInfoLabel(0)
    8482    , m_iSearchPosition(0)
    85     , m_iMatchCount(-1)
     83    , m_iMatchCount(0)
    8684{
    8785    /* Prepare: */
     
    10199    m_iSearchPosition = 0;
    102100    m_matchLocationVector.clear();
    103     m_iMatchCount = -1;
    104     configureInfoLabels();
    105 }
    106 
    107 const QVector<float> &UIVMLogViewerSearchPanel::getMatchLocationVector() const
     101    m_iMatchCount = 0;
     102}
     103
     104const QVector<float> &UIVMLogViewerSearchPanel::matchLocationVector() const
    108105{
    109106    return m_matchLocationVector;
     
    113110{
    114111    return "SearchPanel";
     112}
     113
     114int UIVMLogViewerSearchPanel::marchCount() const
     115{
     116    return m_iMatchCount;
    115117}
    116118
     
    160162    }
    161163    m_iSearchPosition = -1;
    162     clearHighlighting(-1);
     164    clearHighlighting();
    163165}
    164166
     
    180182    }
    181183    else
    182     {
    183         /* we need this check not to remove the 'not found' label
    184            when the user toggles with this check-box: */
    185         if (m_iMatchCount != 0)
    186             clearHighlighting(-1);
    187         else
    188             clearHighlighting(0);
    189     }
    190     configureInfoLabels();
     184        clearHighlighting();
     185
    191186    emit sigHighlightingUpdated();
    192187}
     
    255250    }
    256251
    257     /* Create search field layout: */
    258     QHBoxLayout *pSearchErrorLayout = new QHBoxLayout;
    259     if (pSearchErrorLayout)
    260     {
    261         pSearchErrorLayout->setContentsMargins(0, 0, 0, 0);
    262 #ifdef VBOX_WS_MAC
    263         pSearchErrorLayout->setSpacing(5);
    264 #else
    265         pSearchErrorLayout->setSpacing(qApp->style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing) / 2);
    266 #endif
    267 
    268         /* Create warning-icon: */
    269         m_pWarningIcon = new QLabel;
    270         if (m_pWarningIcon)
    271         {
    272             m_pWarningIcon->hide();
    273             QIcon icon = UIIconPool::defaultIcon(UIIconPool::UIDefaultIconType_MessageBoxWarning, this);
    274             if (!icon.isNull())
    275                 m_pWarningIcon->setPixmap(icon.pixmap(16, 16));
    276             pSearchErrorLayout->addWidget(m_pWarningIcon);
    277         }
    278 
    279         /* Create warning-label: */
    280         m_pInfoLabel = new QLabel;
    281         if (m_pInfoLabel)
    282         {
    283             m_pInfoLabel->hide();
    284             pSearchErrorLayout->addWidget(m_pInfoLabel);
    285         }
    286 
    287         mainLayout()->addLayout(pSearchErrorLayout);
    288     }
    289 
    290252    /* Create case-sensitive check-box: */
    291253    m_pCaseSensitiveCheckBox = new QCheckBox;
     
    341303    m_pHighlightAllCheckBox->setText(UIVMLogViewerWidget::tr("&Highlight All"));
    342304    m_pHighlightAllCheckBox->setToolTip(UIVMLogViewerWidget::tr("When checked, all occurence of the search text are highlighted"));
    343 
    344     if (m_iMatchCount == 0)
    345         m_pInfoLabel->setText(UIVMLogViewerWidget::tr("String not found"));
    346     else if (m_iMatchCount > 0)
    347         m_pInfoLabel->setText(UIVMLogViewerWidget::tr("%1 Matches Found").arg(m_iMatchCount));
    348305}
    349306
     
    460417
    461418    const QString &searchString = m_pSearchEditor->text();
     419    m_iMatchCount = countMatches(pDocument, searchString);
     420    emit sigSearchUpdated();
     421
    462422    if (searchString.isEmpty())
    463423        return;
     
    473433    }
    474434    else
    475     {
    476         m_iMatchCount = -1;
    477435        m_matchLocationVector.clear();
    478     }
    479436
    480437    QTextCursor resultCursor(pDocument);
     
    491448            (direction == BackwardSearch && startPosition == endCursor.position()))
    492449        {
    493             /* Set the match count 0 here since we did not call highLightAll function: */
    494             if (!m_pHighlightAllCheckBox->isChecked())
    495                 m_iMatchCount = 0;
    496             configureInfoLabels();
    497450            return;
    498451        }
     
    515468    pTextEdit->setTextCursor(resultCursor);
    516469    m_iSearchPosition = resultCursor.position();
    517     configureInfoLabels();
    518470}
    519471
     
    528480}
    529481
    530 void UIVMLogViewerSearchPanel::clearHighlighting(int count)
     482void UIVMLogViewerSearchPanel::clearHighlighting()
    531483{
    532484    if (!viewer())
    533485        return;
    534     m_iMatchCount = count;
    535486    m_matchLocationVector.clear();
    536 
    537487    QTextDocument* pDocument = textDocument();
    538488    if (pDocument)
    539489        pDocument->undo();
    540490
    541     configureInfoLabels();
    542491    emit sigHighlightingUpdated();
    543492}
     
    546495                                            const QString &searchString)
    547496{
    548     clearHighlighting(0);
    549 
     497    clearHighlighting();
    550498    if (!pDocument)
    551499        return;
     
    559507    colorFormat.setBackground(Qt::yellow);
    560508    int lineCount = pDocument->lineCount();
     509    QTextDocument::FindFlags flags = constructFindFlags(ForwardSearch);
    561510    while (!highlightCursor.isNull() && !highlightCursor.atEnd())
    562511    {
    563512        /* Hightlighting searches is always from the top of the document forward: */
    564         highlightCursor = pDocument->find(searchString, highlightCursor, constructFindFlags(ForwardSearch));
     513        highlightCursor = pDocument->find(searchString, highlightCursor, flags);
    565514        if (!highlightCursor.isNull())
    566515        {
    567516            highlightCursor.mergeCharFormat(colorFormat);
    568             ++m_iMatchCount;
    569517            /* The following assumes we have single line blocks only: */
    570518            int cursorLine = pDocument->findBlock(highlightCursor.position()).blockNumber();
     
    576524}
    577525
    578 void UIVMLogViewerSearchPanel::configureInfoLabels()
    579 {
    580     if (!m_pSearchEditor || !m_pWarningIcon || !m_pInfoLabel)
    581         return;
    582 
    583     /* If no match has been found, mark the search editor: */
    584     if (m_iMatchCount == 0)
    585     {
    586         m_pSearchEditor->markError();
    587         m_pWarningIcon->setVisible(true);
    588         m_pInfoLabel->setVisible(true);
    589     }
    590     else if (m_iMatchCount == -1)
    591     {
    592         m_pSearchEditor->unmarkError();
    593         m_pWarningIcon->setVisible(false);
    594         m_pInfoLabel->setVisible(false);
    595     }
    596     else
    597     {
    598         m_pSearchEditor->unmarkError();
    599         m_pWarningIcon->setVisible(false);
    600         m_pInfoLabel->setVisible(true);
    601     }
    602     /* Retranslate to get the label text corectly: */
    603     retranslateUi();
    604 }
    605 
    606 QTextDocument::FindFlags UIVMLogViewerSearchPanel::constructFindFlags(SearchDirection eDirection)
     526int UIVMLogViewerSearchPanel::countMatches(QTextDocument *pDocument, const QString &searchString) const
     527{
     528    if (!pDocument)
     529        return 0;
     530    if (searchString.isEmpty())
     531        return 0;
     532    int count = 0;
     533    QTextCursor cursor(pDocument);
     534    QTextDocument::FindFlags flags = constructFindFlags(ForwardSearch);
     535    while (!cursor.isNull() && !cursor.atEnd())
     536    {
     537        cursor = pDocument->find(searchString, cursor, flags);
     538
     539        if (!cursor.isNull())
     540            ++count;
     541    }
     542    return count;
     543}
     544
     545QTextDocument::FindFlags UIVMLogViewerSearchPanel::constructFindFlags(SearchDirection eDirection) const
    607546{
    608547   QTextDocument::FindFlags findFlags;
  • trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogViewerSearchPanel.h

    r76954 r77078  
    4646
    4747    void sigHighlightingUpdated();
     48    void sigSearchUpdated();
    4849
    4950public:
     
    5556    void refresh();
    5657    void reset();
    57     const QVector<float> &getMatchLocationVector() const;
     58    const QVector<float> &matchLocationVector() const;
    5859    virtual QString panelName() const /* override */;
     60    /** Returns the number of the matches to the current search. */
     61    int marchCount() const;
    5962
    6063protected:
     
    8891
    8992    /** Clear the highlighting */
    90     void clearHighlight();
    91     void clearHighlighting(int count);
     93    void clearHighlighting();
    9294
    9395    /** Search routine.
     
    9799    void search(SearchDirection eDirection, bool highlight);
    98100    void highlightAll(QTextDocument *pDocument, const QString &searchString);
    99     /** Controls the visibility of the warning icon and info labels.
    100      Also marks the search editor in case of no match.*/
    101     void configureInfoLabels();
    102101    /** Constructs the find flags for QTextDocument::find function. */
    103     QTextDocument::FindFlags constructFindFlags(SearchDirection eDirection);
    104 
     102    QTextDocument::FindFlags constructFindFlags(SearchDirection eDirection) const;
     103    /** Searches the whole document and return the number of matches to the current search term. */
     104    int countMatches(QTextDocument *pDocument, const QString &searchString) const;
    105105    /** Holds the instance of search-editor we create. */
    106106    UIVMLogViewerSearchField *m_pSearchEditor;
     
    113113    QCheckBox    *m_pHighlightAllCheckBox;
    114114
    115     /** Holds the instance of warning icon we create. */
    116     QLabel      *m_pWarningIcon;
    117     /** Holds the instance of info label we create. */
    118     QLabel      *m_pInfoLabel;
    119 
    120115    /** Holds the position where we start the next search. */
    121116    int          m_iSearchPosition;
    122     /** Holds the number of the matches for the string.
    123      -1: highLightAll function is not called
    124       0: no matches found
    125       n > 0: n matches found. */
     117    /** Holds the number of the matches for the string. 0 for no matches. */
    126118    int          m_iMatchCount;
    127119    /** Stores relative positions of the lines of the matches. The values are [0,1]
  • trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogViewerTextEdit.cpp

    r76753 r77078  
    193193    , m_bWrapLines(true)
    194194    , m_bHasContextMenu(false)
     195    , m_fShowSearchResultOverlay(0)
     196    , m_iMatchCount(0)
    195197{
    196198    setMouseTracking(true);
    197199    //setStyleSheet("background-color: rgba(240, 240, 240, 75%) ");
    198200    prepare();
    199 
    200 
    201201}
    202202
     
    212212
    213213    connect(this, &UIVMLogViewerTextEdit::blockCountChanged, this, &UIVMLogViewerTextEdit::sltUpdateLineNumberAreaWidth);
    214     connect(this, &UIVMLogViewerTextEdit::updateRequest, this, &UIVMLogViewerTextEdit::sltUpdateLineNumberArea);
     214    connect(this, &UIVMLogViewerTextEdit::updateRequest, this, &UIVMLogViewerTextEdit::sltHandleUpdateRequest);
    215215    sltUpdateLineNumberAreaWidth(0);
    216216
     
    232232    if (m_pLineNumberArea)
    233233        m_pLineNumberArea->setFont(font);
     234}
     235
     236void UIVMLogViewerTextEdit::setSearchResultOverlayShowHide(bool fShow)
     237{
     238    if (m_fShowSearchResultOverlay == fShow)
     239        return;
     240    m_fShowSearchResultOverlay = fShow;
     241    if (viewport())
     242        viewport()->repaint();
     243}
     244
     245void UIVMLogViewerTextEdit::setSearchMatchCount(int iMatchCount)
     246{
     247    if (m_iMatchCount == iMatchCount)
     248        return;
     249    m_iMatchCount = iMatchCount;
     250    if (m_fShowSearchResultOverlay && viewport())
     251        viewport()->repaint();
    234252}
    235253
     
    400418}
    401419
     420void UIVMLogViewerTextEdit::paintEvent(QPaintEvent *pEvent)
     421{
     422    QIWithRetranslateUI<QPlainTextEdit>::paintEvent(pEvent);
     423
     424    /** Draw an overlay with text in it to show the number of search matches: */
     425    if (viewport() && m_fShowSearchResultOverlay)
     426    {
     427        QPainter painter(viewport());
     428        QColor rectColor = viewport()->palette().color(QPalette::Active, QPalette::Dark);
     429        double fontScale = 1.5;
     430        rectColor.setAlpha(200);
     431
     432        QString strText = QString("%1 %2").arg(QString::number(m_iMatchCount)).arg(UIVMLogViewerWidget::tr("Matches Found"));
     433        /** Space between the text and rectangle border. */
     434        QSize textMargin(5, 5);
     435        /** Space between the rectangle and viewport edges. */
     436        QSize rectMargin(2, 2);
     437
     438        QSize rectSize(fontScale * QApplication::fontMetrics().width(strText) + textMargin.width(),
     439                       fontScale * QApplication::fontMetrics().height() + textMargin.height());
     440        QPoint topLeft(viewport()->rect().width() - rectSize.width() - rectMargin.width(),
     441                       viewport()->rect().height() - rectSize.height() - rectMargin.height());
     442        painter.fillRect(topLeft.x(),topLeft.y(), rectSize.width(), rectSize.height(), rectColor);
     443
     444
     445        QFont pfont = QApplication::font();
     446        QColor fontColor(QPalette::WindowText);
     447        painter.setPen(fontColor);
     448        if (pfont.pixelSize() != -1)
     449            pfont.setPixelSize(fontScale * pfont.pixelSize());
     450        else
     451            pfont.setPointSize(fontScale * pfont.pointSize());
     452        painter.setFont(pfont);
     453
     454        painter.drawText(QRect(topLeft, rectSize), Qt::AlignCenter | Qt::AlignVCenter, strText);
     455    }
     456}
     457
    402458void UIVMLogViewerTextEdit::sltUpdateLineNumberAreaWidth(int /* newBlockCount */)
    403459{
     
    405461}
    406462
    407 void UIVMLogViewerTextEdit::sltUpdateLineNumberArea(const QRect &rect, int dy)
     463void UIVMLogViewerTextEdit::sltHandleUpdateRequest(const QRect &rect, int dy)
    408464{
    409465    if (dy)
     
    414470    if (rect.contains(viewport()->rect()))
    415471        sltUpdateLineNumberAreaWidth(0);
     472
     473    if (viewport())
     474        viewport()->repaint();
     475
    416476}
    417477
  • trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogViewerTextEdit.h

    r76581 r77078  
    6767    int  currentVerticalScrollBarValue() const;
    6868    void setCurrentVerticalScrollBarValue(int value);
     69    void setCurrentFont(QFont font);
    6970
    70     void setCurrentFont(QFont font);
     71    void setSearchResultOverlayShowHide(bool fShow);
     72    void setSearchMatchCount(int iMatchCount);
    7173
    7274protected:
     
    7678    virtual void mouseMoveEvent(QMouseEvent *pEvent) /* override */;
    7779    virtual void leaveEvent(QEvent * pEvent) /* override */;
     80    virtual void paintEvent(QPaintEvent *pEvent) /* override */;
    7881    virtual void retranslateUi() /* override */;
    7982
     
    8285    void sltBookmark();
    8386    void sltUpdateLineNumberAreaWidth(int newBlockCount);
    84     void sltUpdateLineNumberArea(const QRect &, int);
     87    void sltHandleUpdateRequest(const QRect &, int);
    8588    int  visibleLineCount();
    8689
     
    107110        the user the text edit's content is filtered (as oppesed to whole log file content.
    108111        And we dont display bookmarks and adding/deleting bookmarks are disabled. */
    109     bool                 m_bShownTextIsFiltered;
    110     bool                 m_bShowLineNumbers;
    111     bool                 m_bWrapLines;
    112     QString              m_strBackgroungText;
     112    bool         m_bShownTextIsFiltered;
     113    bool         m_bShowLineNumbers;
     114    bool         m_bWrapLines;
     115    QString      m_strBackgroungText;
    113116    friend class UILineNumberArea;
    114     bool                 m_bHasContextMenu;
     117    bool         m_bHasContextMenu;
     118    bool         m_fShowSearchResultOverlay;
     119    int          m_iMatchCount;
    115120};
    116121
  • trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogViewerWidget.cpp

    r76977 r77078  
    301301void UIVMLogViewerWidget::sltSearchResultHighLigting()
    302302{
    303     if (!m_pSearchPanel)
    304         return;
    305 
    306     if (!currentLogPage())
    307         return;
    308     currentLogPage()->setScrollBarMarkingsVector(m_pSearchPanel->getMatchLocationVector());
     303    if (!m_pSearchPanel || !currentLogPage())
     304        return;
     305    currentLogPage()->setScrollBarMarkingsVector(m_pSearchPanel->matchLocationVector());
     306}
     307
     308void UIVMLogViewerWidget::sltHandleSearchUpdated()
     309{
     310    if (!m_pSearchPanel || !currentLogPage())
     311        return;
     312    for (int i = 0; i < m_logPageList.size(); ++i)
     313        if (UIVMLogPage *pPage = qobject_cast<UIVMLogPage*>(m_logPageList[i]))
     314            pPage->setSearchMatchCount(m_pSearchPanel->marchCount());
    309315}
    310316
     
    313319    Q_UNUSED(tabIndex);
    314320
    315     resetHighlighthing();
    316     if (m_pSearchPanel)
    317         m_pSearchPanel->reset();
     321    /* Dont refresh the search here as it is refreshed by the filtering mechanism
     322       which is updated as tab current index changes: */
    318323
    319324    /* We keep a separate QVector<LogBookmark> for each log page: */
     
    493498            connect(m_pSearchPanel, &UIVMLogViewerSearchPanel::sigHighlightingUpdated,
    494499                    this, &UIVMLogViewerWidget::sltSearchResultHighLigting);
     500            connect(m_pSearchPanel, &UIVMLogViewerSearchPanel::sigSearchUpdated,
     501                    this, &UIVMLogViewerWidget::sltHandleSearchUpdated);
    495502            connect(m_pSearchPanel, &UIVMLogViewerSearchPanel::sigHidePanel,
    496503                    this, &UIVMLogViewerWidget::sltHandleHidePanel);
     
    760767            pLogPage->markForError();
    761768        }
     769        pLogPage->setSearchResultOverlayShowHide(m_pSearchPanel->isVisible());
     770        pLogPage->setSearchMatchCount(m_pSearchPanel->marchCount());
     771        pLogPage->setScrollBarMarkingsVector(m_pSearchPanel->matchLocationVector());
    762772    }
    763773}
     
    862872    m_visiblePanelsList.removeOne(panel);
    863873    manageEscapeShortCut();
     874    /* Hide the search result overlay on the text edit: */
     875    if (panel == m_pSearchPanel)
     876    {
     877        for (int i = 0; i < m_logPageList.size(); ++i)
     878            if (UIVMLogPage *pPage = qobject_cast<UIVMLogPage*>(m_logPageList[i]))
     879                pPage->setSearchResultOverlayShowHide(false);
     880    }
    864881}
    865882
     
    876893    m_visiblePanelsList.push_back(panel);
    877894    manageEscapeShortCut();
     895
     896    /* Show the search result overlay on the text edit: */
     897    if (panel == m_pSearchPanel)
     898    {
     899        for (int i = 0; i < m_logPageList.size(); ++i)
     900            if (UIVMLogPage *pPage = qobject_cast<UIVMLogPage*>(m_logPageList[i]))
     901                pPage->setSearchResultOverlayShowHide(true);
     902    }
    878903}
    879904
  • trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogViewerWidget.h

    r76977 r77078  
    114114    /** Handles the search result highlight changes. */
    115115    void sltSearchResultHighLigting();
     116    void sltHandleSearchUpdated();
    116117    /** Handles the tab change of the logviewer. */
    117118    void sltTabIndexChange(int tabIndex);
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