VirtualBox

Changeset 78462 in vbox


Ignore:
Timestamp:
May 10, 2019 2:56:17 PM (6 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
130491
Message:

FE/Qt: bugref:9072. Some cleaning in the log viewer.

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

Legend:

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

    r77078 r78462  
    342342        m_pTextEdit->setCurrentFont(font);
    343343}
    344 
    345 void UIVMLogPage::setSearchResultOverlayShowHide(bool fShow)
    346 {
    347     if (m_pTextEdit)
    348         m_pTextEdit->setSearchResultOverlayShowHide(fShow);
    349 }
    350 
    351 void UIVMLogPage::setSearchMatchCount(int iMatchCount)
    352 {
    353     if (m_pTextEdit)
    354         m_pTextEdit->setSearchMatchCount(iMatchCount);
    355 }
  • trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogPage.h

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

    r78459 r78462  
    8282    , m_pMatchWholeWordCheckBox(0)
    8383    , m_pHighlightAllCheckBox(0)
    84     , m_iSearchCursorPosition(0)
    85     , m_iMatchCount(0)
    8684{
    8785    /* Prepare: */
     
    9189void UIVMLogViewerSearchPanel::refresh()
    9290{
    93     m_iSearchCursorPosition = 0;
    9491    /* We start the search from the end of the doc. assuming log's end is more interesting: */
    95     search(BackwardSearch, true);
     92    performSearch(BackwardSearch, true);
    9693    emit sigHighlightingUpdated();
    9794}
     
    9996void UIVMLogViewerSearchPanel::reset()
    10097{
    101     m_iSearchCursorPosition = 0;
     98    m_iSelectedMatchIndex = 0;
    10299    m_matchLocationVector.clear();
    103     setMatchCount(0);
     100    m_matchedCursorPosition.clear();
    104101    if (m_pSearchEditor)
    105102        m_pSearchEditor->reset();
     
    118115int UIVMLogViewerSearchPanel::matchCount() const
    119116{
    120     return m_iMatchCount;
     117    return m_matchedCursorPosition.size();
    121118}
    122119
     
    140137    m_pPreviousButton->setEnabled(!strSearchString.isEmpty());
    141138
    142 
    143139    /* If search-string is not empty: */
    144140    if (!strSearchString.isEmpty())
    145141    {
    146142        /* Reset the position to force the search restart from the document's end: */
    147         m_iSearchCursorPosition = 0;
    148         search(BackwardSearch, true);
     143        performSearch(BackwardSearch, true);
    149144        emit sigHighlightingUpdated();
    150145        return;
    151146    }
     147
    152148    /* If search-string is empty, reset cursor position: */
    153149    if (!viewer())
     
    165161        pBrowser->setTextCursor(cursor);
    166162    }
    167     m_iSearchCursorPosition = -1;
    168     setMatchCount(0);
     163    m_matchedCursorPosition.clear();
     164    m_matchLocationVector.clear();
     165    clearHighlighting();
    169166    emit sigSearchUpdated();
    170     clearHighlighting();
    171167}
    172168
     
    201197{
    202198    refresh();
     199}
     200
     201void UIVMLogViewerSearchPanel::sltSelectNextPreviousMatch()
     202{
     203    moveSelection(sender() == m_pNextButton);
    203204}
    204205
     
    282283{
    283284    connect(m_pSearchEditor, &UIVMLogViewerSearchField::textChanged, this, &UIVMLogViewerSearchPanel::sltSearchTextChanged);
    284     connect(m_pNextButton, &QIToolButton::clicked, this, &UIVMLogViewerSearchPanel::findNext);
    285     connect(m_pPreviousButton, &QIToolButton::clicked, this, &UIVMLogViewerSearchPanel::findPrevious);
     285    connect(m_pNextButton, &QIToolButton::clicked, this, &UIVMLogViewerSearchPanel::sltSelectNextPreviousMatch);
     286    connect(m_pPreviousButton, &QIToolButton::clicked, this, &UIVMLogViewerSearchPanel::sltSelectNextPreviousMatch);
    286287
    287288    connect(m_pHighlightAllCheckBox, &QCheckBox::stateChanged,
     
    413414        /* Select all the text: */
    414415        m_pSearchEditor->selectAll();
    415         m_pSearchEditor->setMatchCount(m_iMatchCount);
    416     }
    417 }
    418 
    419 void UIVMLogViewerSearchPanel::search(SearchDirection direction, bool highlight)
     416        m_pSearchEditor->setMatchCount(m_matchedCursorPosition.size());
     417    }
     418}
     419
     420void UIVMLogViewerSearchPanel::performSearch(SearchDirection , bool )
    420421{
    421422    QPlainTextEdit *pTextEdit = textEdit();
     
    429430
    430431    const QString &searchString = m_pSearchEditor->text();
    431     setMatchCount(countMatches(pDocument, searchString));
    432432    emit sigSearchUpdated();
    433433
     
    435435        return;
    436436
    437     QTextCursor endCursor(pDocument);
    438     endCursor.movePosition(QTextCursor::End);
    439     QTextCursor startCursor(pDocument);
    440 
     437    findAll(pDocument, searchString);
     438    m_iSelectedMatchIndex = 0;
     439    selectMatch(m_iSelectedMatchIndex, searchString);
     440    if (m_pSearchEditor)
     441    {
     442        m_pSearchEditor->setMatchCount(m_matchedCursorPosition.size());
     443        m_pSearchEditor->setScroolToIndex(m_matchedCursorPosition.empty() ? -1 : 0);
     444    }
    441445    if (m_pHighlightAllCheckBox->isChecked())
    442     {
    443         if (highlight)
    444             highlightAll(pDocument, searchString);
    445     }
    446     else
    447         m_matchLocationVector.clear();
    448 
    449     QTextCursor resultCursor(pDocument);
    450     int startPosition = m_iSearchCursorPosition;
    451     if (direction == BackwardSearch)
    452         startPosition -= searchString.length();
    453     resultCursor = pDocument->find(searchString, startPosition, constructFindFlags(direction));
    454 
    455     /* Decide whether to wrap around or to end the search */
    456     if (resultCursor.isNull())
    457     {
    458         /* End the search if we search the whole document with no find: */
    459         if ((direction == ForwardSearch && startPosition == startCursor.position()) ||
    460             (direction == BackwardSearch && startPosition == endCursor.position()))
    461         {
    462             return;
    463         }
    464         /* Wrap the search */
    465         if (direction == ForwardSearch)
    466         {
    467             m_iSearchCursorPosition = startCursor.position();
    468             search(ForwardSearch, false);
    469             return;
    470         }
    471         else
    472         {
    473             /* Set the search position away from the end position to be
    474                able to find the string at the end of the document: */
    475             m_iSearchCursorPosition = endCursor.position() + searchString.length();
    476             search(BackwardSearch, false);
    477             return;
    478         }
    479     }
    480     pTextEdit->setTextCursor(resultCursor);
    481     m_iSearchCursorPosition = resultCursor.position();
    482 }
    483 
    484 void UIVMLogViewerSearchPanel::findNext()
    485 {
    486     search(ForwardSearch, false);
    487 }
    488 
    489 void UIVMLogViewerSearchPanel::findPrevious()
    490 {
    491     search(BackwardSearch, false);
     446        highlightAll(pDocument, searchString);
    492447}
    493448
     
    496451    if (!viewer())
    497452        return;
    498     m_matchLocationVector.clear();
    499453    QTextDocument* pDocument = textDocument();
    500454    if (pDocument)
    501455        pDocument->undo();
    502 
    503456    emit sigHighlightingUpdated();
    504457}
     
    518471    cursor.beginEditBlock();
    519472    colorFormat.setBackground(Qt::yellow);
    520     int lineCount = pDocument->lineCount();
    521     QTextDocument::FindFlags flags = constructFindFlags(ForwardSearch);
    522     while (!highlightCursor.isNull() && !highlightCursor.atEnd())
    523     {
    524         /* Hightlighting searches is always from the top of the document forward: */
    525         highlightCursor = pDocument->find(searchString, highlightCursor, flags);
     473    for (int i = 0; i < m_matchedCursorPosition.size(); ++i)
     474    {
     475        highlightCursor.setPosition(m_matchedCursorPosition[i]);
     476        highlightCursor.setPosition(m_matchedCursorPosition[i] + searchString.length(), QTextCursor::KeepAnchor);
     477
    526478        if (!highlightCursor.isNull())
    527479        {
    528480            highlightCursor.mergeCharFormat(colorFormat);
     481        }
     482    }
     483    cursor.endEditBlock();
     484}
     485
     486void UIVMLogViewerSearchPanel::findAll(QTextDocument *pDocument, const QString &searchString)
     487{
     488    if (!pDocument)
     489        return;
     490    m_matchedCursorPosition.clear();
     491    m_matchLocationVector.clear();
     492    if (searchString.isEmpty())
     493        return;
     494    QTextCursor cursor(pDocument);
     495    QTextDocument::FindFlags flags = constructFindFlags(ForwardSearch);
     496    int lineCount = pDocument->lineCount();
     497    while (!cursor.isNull() && !cursor.atEnd())
     498    {
     499        cursor = pDocument->find(searchString, cursor, flags);
     500
     501        if (!cursor.isNull())
     502        {
     503            m_matchedCursorPosition << cursor.position() - searchString.length();
    529504            /* The following assumes we have single line blocks only: */
    530             int cursorLine = pDocument->findBlock(highlightCursor.position()).blockNumber();
     505            int cursorLine = pDocument->findBlock(cursor.position()).blockNumber();
    531506            if (lineCount != 0)
    532507                m_matchLocationVector.push_back(cursorLine / static_cast<float>(lineCount));
    533508        }
    534509    }
    535     cursor.endEditBlock();
     510}
     511
     512void UIVMLogViewerSearchPanel::selectMatch(int iMatchIndex, const QString &searchString)
     513{
     514    if (!textEdit())
     515        return;
     516    if (searchString.isEmpty())
     517        return;
     518    if (iMatchIndex < 0 || iMatchIndex >= m_matchedCursorPosition.size())
     519        return;
     520
     521    QTextCursor cursor = textEdit()->textCursor();
     522    /* Move the cursor to the beginning of the matched string: */
     523    cursor.setPosition(m_matchedCursorPosition.at(iMatchIndex), QTextCursor::MoveAnchor);
     524    /* Move the cursor to the end of the matched string while keeping the anchor at the begining thus selecting the text: */
     525    cursor.setPosition(m_matchedCursorPosition.at(iMatchIndex)+searchString.length(), QTextCursor::KeepAnchor);
     526    textEdit()->setTextCursor(cursor);
     527    return;
     528}
     529
     530void UIVMLogViewerSearchPanel::moveSelection(bool fForward)
     531{
     532    if (fForward)
     533        m_iSelectedMatchIndex = m_iSelectedMatchIndex >= m_matchedCursorPosition.size() - 1 ? 0 : (m_iSelectedMatchIndex + 1);
     534    else
     535        m_iSelectedMatchIndex = m_iSelectedMatchIndex <= 0 ? m_matchedCursorPosition.size() - 1 : (m_iSelectedMatchIndex - 1);
     536    selectMatch(m_iSelectedMatchIndex, m_pSearchEditor->text());
     537    if (m_pSearchEditor)
     538        m_pSearchEditor->setScroolToIndex(m_iSelectedMatchIndex);
    536539}
    537540
     
    555558}
    556559
    557 void UIVMLogViewerSearchPanel::setMatchCount(int iCount)
    558 {
    559     if (m_iMatchCount == iCount)
    560         return;
    561     m_iMatchCount = iCount;
    562     if (m_pSearchEditor)
    563         m_pSearchEditor->setMatchCount(iCount);
    564 }
    565 
    566560QTextDocument::FindFlags UIVMLogViewerSearchPanel::constructFindFlags(SearchDirection eDirection) const
    567561{
  • trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogViewerSearchPanel.h

    r78459 r78462  
    8282    void sltCaseSentitiveCheckBox();
    8383    void sltMatchWholeWordCheckBox();
    84     /** Forward search routine wrapper. */
    85     void findNext();
    86     /** Backward search routine wrapper. */
    87     void findPrevious();
     84    void sltSelectNextPreviousMatch();
    8885
    8986private:
     
    9895      * @param  highlight      if false highlight function is not called
    9996                               thus we avoid calling highlighting for the same string repeatedly. */
    100     void search(SearchDirection eDirection, bool highlight);
     97    void performSearch(SearchDirection eDirection, bool highlight);
    10198    void highlightAll(QTextDocument *pDocument, const QString &searchString);
     99    void findAll(QTextDocument *pDocument, const QString &searchString);
     100    void selectMatch(int iMatchIndex, const QString &searchString);
     101    void moveSelection(bool fForward);
     102
    102103    /** Constructs the find flags for QTextDocument::find function. */
    103104    QTextDocument::FindFlags constructFindFlags(SearchDirection eDirection) const;
    104105    /** Searches the whole document and return the number of matches to the current search term. */
    105106    int countMatches(QTextDocument *pDocument, const QString &searchString) const;
    106     void setMatchCount(int iCount);
    107107    /** Holds the instance of search-editor we create. */
    108108    UISearchLineEdit *m_pSearchEditor;
     
    114114    QCheckBox    *m_pMatchWholeWordCheckBox;
    115115    QCheckBox    *m_pHighlightAllCheckBox;
    116 
    117     /** Holds the position where we start the next search. */
    118     int          m_iSearchCursorPosition;
    119     /** Holds the number of the matches for the string. 0 for no matches. */
    120     int          m_iMatchCount;
    121 
    122     /** Stores relative positions of the lines of the matches. The values are [0,1]
     116    /** Stores relative positions of the lines of the matches wrt. total # of lines. The values are in [0,1]
    123117        0 being the first line 1 being the last. */
    124118    QVector<float> m_matchLocationVector;
     119    /** Document positions of the cursors within th document for all matches. */
     120    QVector<int>   m_matchedCursorPosition;
     121    /** The index of the curently selected item within m_matchedCursorPosition. */
     122    int            m_iSelectedMatchIndex;
    125123};
    126124
  • trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogViewerTextEdit.cpp

    r77275 r78462  
    193193    , m_bWrapLines(true)
    194194    , m_bHasContextMenu(false)
    195     , m_fShowSearchResultOverlay(0)
    196     , m_iMatchCount(0)
    197195{
    198196    configure();
     
    241239    if (m_pLineNumberArea)
    242240        m_pLineNumberArea->setFont(font);
    243 }
    244 
    245 void UIVMLogViewerTextEdit::setSearchResultOverlayShowHide(bool fShow)
    246 {
    247     if (m_fShowSearchResultOverlay == fShow)
    248         return;
    249     m_fShowSearchResultOverlay = fShow;
    250     if (viewport())
    251         viewport()->repaint();
    252 }
    253 
    254 void UIVMLogViewerTextEdit::setSearchMatchCount(int iMatchCount)
    255 {
    256     if (m_iMatchCount == iMatchCount)
    257         return;
    258     m_iMatchCount = iMatchCount;
    259     if (m_fShowSearchResultOverlay && viewport())
    260         viewport()->repaint();
    261241}
    262242
     
    386366}
    387367
    388 void UIVMLogViewerTextEdit::paintEvent(QPaintEvent *pEvent)
    389 {
    390     QIWithRetranslateUI<QPlainTextEdit>::paintEvent(pEvent);
    391 
    392     /* Draw an overlay with text in it to show the number of search matches: */
    393     if (viewport() && (m_fShowSearchResultOverlay || m_bShownTextIsFiltered))
    394     {
    395         QPainter painter(viewport());
    396         QColor rectColor = viewport()->palette().color(QPalette::Active, QPalette::Dark);
    397         double fontScale = 1.5;
    398         rectColor.setAlpha(200);
    399 
    400         QString strText;
    401         if (m_fShowSearchResultOverlay)
    402             strText = QString("%1 %2").arg(QString::number(m_iMatchCount)).arg(UIVMLogViewerWidget::tr("Matches Found"));
    403         if (m_bShownTextIsFiltered)
    404         {
    405             if (!strText.isEmpty())
    406                 strText.append(" / ");
    407             strText.append(UIVMLogViewerWidget::tr("Filtered"));
    408         }
    409         /* Space between the text and rectangle border: */
    410         QSize textMargin(5, 5);
    411         /* Space between the rectangle and viewport edges: */
    412         QSize rectMargin(2, 2);
    413 
    414         QSize rectSize(fontScale * QApplication::fontMetrics().width(strText) + textMargin.width(),
    415                        fontScale * QApplication::fontMetrics().height() + textMargin.height());
    416         QPoint topLeft(viewport()->rect().width() - rectSize.width() - rectMargin.width(),
    417                        viewport()->rect().height() - rectSize.height() - rectMargin.height());
    418         painter.fillRect(topLeft.x(),topLeft.y(), rectSize.width(), rectSize.height(), rectColor);
    419 
    420 
    421         QFont pfont = QApplication::font();
    422         QColor fontColor(QPalette::WindowText);
    423         painter.setPen(fontColor);
    424         if (pfont.pixelSize() != -1)
    425             pfont.setPixelSize(fontScale * pfont.pixelSize());
    426         else
    427             pfont.setPointSize(fontScale * pfont.pointSize());
    428         painter.setFont(pfont);
    429 
    430         painter.drawText(QRect(topLeft, rectSize), Qt::AlignCenter | Qt::AlignVCenter, strText);
    431     }
    432 }
    433 
    434368void UIVMLogViewerTextEdit::sltUpdateLineNumberAreaWidth(int /* newBlockCount */)
    435369{
  • trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogViewerTextEdit.h

    r77275 r78462  
    6969    void setCurrentFont(QFont font);
    7070
    71     void setSearchResultOverlayShowHide(bool fShow);
    72     void setSearchMatchCount(int iMatchCount);
    73 
    7471protected:
    7572
     
    7875    virtual void mouseMoveEvent(QMouseEvent *pEvent) /* override */;
    7976    virtual void leaveEvent(QEvent * pEvent) /* override */;
    80     virtual void paintEvent(QPaintEvent *pEvent) /* override */;
    8177    virtual void retranslateUi() /* override */;
    8278
     
    117113    friend class UILineNumberArea;
    118114    bool         m_bHasContextMenu;
    119     bool         m_fShowSearchResultOverlay;
    120     int          m_iMatchCount;
    121 };
     115 };
    122116
    123117
  • trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogViewerWidget.cpp

    r78459 r78462  
    310310    if (!m_pSearchPanel || !currentLogPage())
    311311        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->matchCount());
    315312}
    316313
     
    767764            pLogPage->markForError();
    768765        }
    769         pLogPage->setSearchResultOverlayShowHide(m_pSearchPanel->isVisible());
    770         pLogPage->setSearchMatchCount(m_pSearchPanel->matchCount());
    771766        pLogPage->setScrollBarMarkingsVector(m_pSearchPanel->matchLocationVector());
    772767    }
     
    872867    m_visiblePanelsList.removeOne(panel);
    873868    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     }
    881869}
    882870
     
    893881    m_visiblePanelsList.push_back(panel);
    894882    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     }
    903883}
    904884
Note: See TracChangeset for help on using the changeset viewer.

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