VirtualBox

Changeset 70554 in vbox for trunk


Ignore:
Timestamp:
Jan 12, 2018 12:29:25 PM (7 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
120234
Message:

FE/Qt bugref:9072 Adding line numbers area to log viewer

Location:
trunk/src/VBox/Frontends/VirtualBox
Files:
3 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/Makefile.kmk

    r70539 r70554  
    341341        src/logviewer/UIVMLogViewerPanel.h \
    342342        src/logviewer/UIVMLogViewerSearchPanel.h \
     343        src/logviewer/UIVMLogViewerTextEdit.h \
    343344        src/logviewer/UIVMLogViewerWidget.h \
    344345        src/medium/UIMediumDetailsWidget.h \
     
    548549        src/globals/UIMainEventListener.cpp \
    549550        src/globals/UIThreadPool.cpp \
    550         src/logviewer/UIVMLogPage.cpp \
    551551        src/logviewer/UIVMLogViewerFilterPanel.cpp \
     552        src/logviewer/UIVMLogViewerTextEdit.cpp \
    552553        src/medium/UIMediumEnumerator.cpp \
    553554        src/runtime/UIActionPoolRuntime.cpp \
     
    660661        src/logviewer/UIVMLogViewerPanel.cpp \
    661662        src/logviewer/UIVMLogViewerSearchPanel.cpp \
     663        src/logviewer/UIVMLogViewerTextEdit.cpp \
    662664        src/logviewer/UIVMLogViewerWidget.cpp \
    663665        src/medium/UIMediumDefs.cpp \
  • trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogPage.cpp

    r70549 r70554  
    2828# endif
    2929# include <QPainter>
    30 # include <QPlainTextEdit>
    3130# include <QScrollBar>
    3231# include <QTextBlock>
    3332
    3433/* GUI includes: */
    35 # include "QIFileDialog.h"
    36 # include "QITabWidget.h"
    37 # include "UIExtraDataManager.h"
    38 # include "UIIconPool.h"
    39 # include "UIMessageCenter.h"
    4034# include "UIVMLogPage.h"
    41 # include "UIVMLogViewerBookmarksPanel.h"
    42 # include "UIVMLogViewerFilterPanel.h"
    43 # include "UIVMLogViewerSearchPanel.h"
    44 # include "UIToolBar.h"
    45 
    46 /* COM includes: */
    47 # include "CSystemProperties.h"
    48 
    49 # include "VBoxGlobal.h"
     35# include "UIVMLogViewerTextEdit.h"
    5036
    5137#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
    52 
    53 
    54 /** We use a modified scrollbar style for our QPlainTextEdits to get the
    55     markings on the scrollbars correctly. The default scrollbarstyle does not
    56     reveal the height of the pushbuttons on the scrollbar (on either side of it, with arrow on them)
    57     to compute the marking locations correctly. Thus we turn these push buttons off: */
    58 const QString verticalScrollBarStyle("QScrollBar:vertical {"
    59                                      "border: 1px ridge grey; "
    60                                      "margin: 0px 0px 0 0px;}"
    61                                      "QScrollBar::handle:vertical {"
    62                                      "min-height: 10px;"
    63                                      "background: grey;}"
    64                                      "QScrollBar::add-line:vertical {"
    65                                      "width: 0px;}"
    66                                      "QScrollBar::sub-line:vertical {"
    67                                      "width: 0px;}");
    68 
    69 const QString horizontalScrollBarStyle("QScrollBar:horizontal {"
    70                                        "border: 1px ridge grey; "
    71                                        "margin: 0px 0px 0 0px;}"
    72                                        "QScrollBar::handle:horizontal {"
    73                                        "min-height: 10px;"
    74                                        "background: grey;}"
    75                                        "QScrollBar::add-line:horizontal {"
    76                                        "height: 0px;}"
    77                                        "QScrollBar::sub-line:horizontal {"
    78                                        "height: 0px;}");
    79 
    80 class UIIndicatorScrollBar : public QScrollBar
    81 {
    82     Q_OBJECT;
    83 
    84 public:
    85 
    86     UIIndicatorScrollBar(QWidget *parent = 0)
    87         :QScrollBar(parent)
    88     {
    89         setStyleSheet(verticalScrollBarStyle);
    90     }
    91 
    92     void setMarkingsVector(const QVector<float> &vector)
    93     {
    94         m_markingsVector = vector;
    95     }
    96 
    97     void clearMarkingsVector()
    98     {
    99         m_markingsVector.clear();
    100     }
    101 
    102 protected:
    103 
    104     virtual void paintEvent(QPaintEvent *pEvent) /* override */
    105     {
    106         QScrollBar::paintEvent(pEvent);
    107         /* Put a red line to marking position: */
    108         for (int i = 0; i < m_markingsVector.size(); ++i)
    109         {
    110             QPointF p1 = QPointF(0, m_markingsVector[i] * height());
    111             QPointF p2 = QPointF(width(), m_markingsVector[i] * height());
    112 
    113             QPainter painter(this);
    114             painter.setRenderHint(QPainter::Antialiasing, true);
    115             painter.setPen(QPen(QColor(255, 0, 0, 75), 1.1f));
    116             painter.drawLine(p1, p2);
    117         }
    118     }
    119 
    120 private:
    121 
    122     /* Stores the relative (to scrollbar's height) positions of markings,
    123        where we draw a horizontal line. Values are in [0.0, 1.0]*/
    124     QVector<float> m_markingsVector;
    125 };
    126 
    127 /* Sub-class QPlainTextEdit for some addtional context menu items: */
    128 class UIVMLogViewerTextEdit : public QPlainTextEdit
    129 {
    130     Q_OBJECT;
    131 
    132 signals:
    133 
    134     void sigContextMenuBookmarkAction(LogBookmark bookmark);
    135 
    136 public:
    137     UIVMLogViewerTextEdit(QWidget* parent = 0)
    138         :QPlainTextEdit(parent)
    139     {
    140         //setStyleSheet("background-color: rgba(240, 240, 240, 75%) ");
    141     }
    142 
    143 protected:
    144 
    145     void contextMenuEvent(QContextMenuEvent *pEvent)
    146     {
    147         QMenu *menu = createStandardContextMenu();
    148         QAction *pAction = menu->addAction(tr("Bookmark"));
    149         QTextBlock block = cursorForPosition(pEvent->pos()).block();
    150         m_iContextMenuBookmark.first = block.firstLineNumber();
    151         m_iContextMenuBookmark.second = block.text();
    152 
    153         if (pAction)
    154             connect(pAction, &QAction::triggered, this, &UIVMLogViewerTextEdit::sltBookmark);
    155 
    156         menu->exec(pEvent->globalPos());
    157 
    158         if (pAction)
    159             disconnect(pAction, &QAction::triggered, this, &UIVMLogViewerTextEdit::sltBookmark);
    160 
    161         delete menu;
    162     }
    163 
    164     virtual void mousePressEvent(QMouseEvent *pEvent)
    165     {
    166         QPlainTextEdit::mousePressEvent(pEvent);
    167     }
    168 
    169 private slots:
    170 
    171     void sltBookmark()
    172     {
    173         emit sigContextMenuBookmarkAction(m_iContextMenuBookmark);
    174     }
    175 
    176 private:
    177 
    178     /* Line number and text at the context menu position */
    179     LogBookmark m_iContextMenuBookmark;
    180 };
    18138
    18239
     
    18441    : QIWithRetranslateUI<QWidget>(pParent)
    18542    , m_pMainLayout(0)
    186     , m_pPlainTextEdit(0)
     43    , m_pTextEdit(0)
    18744    , m_tabIndex(tabIndex)
    18845{
     
    19754int UIVMLogPage::defaultLogPageWidth() const
    19855{
    199     if (!m_pPlainTextEdit)
     56    if (!m_pTextEdit)
    20057        return 0;
    20158
    20259    /* Compute a width for 132 characters plus scrollbar and frame width: */
    203     int iDefaultWidth = m_pPlainTextEdit->fontMetrics().width(QChar('x')) * 132 +
    204                         m_pPlainTextEdit->verticalScrollBar()->width() +
    205                         m_pPlainTextEdit->frameWidth() * 2;
     60    int iDefaultWidth = m_pTextEdit->fontMetrics().width(QChar('x')) * 132 +
     61                        m_pTextEdit->verticalScrollBar()->width() +
     62                        m_pTextEdit->frameWidth() * 2;
    20663
    20764    return iDefaultWidth;
     
    22279    m_pMainLayout->setContentsMargins(0, 0, 0, 0);
    22380
    224     m_pPlainTextEdit = new UIVMLogViewerTextEdit(this);
    225     m_pMainLayout->addWidget(m_pPlainTextEdit);
    226 
    227     m_pPlainTextEdit->setVerticalScrollBar(new UIIndicatorScrollBar());
    228     m_pPlainTextEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    229     QScrollBar *pHorizontalScrollBar = m_pPlainTextEdit->horizontalScrollBar();
    230     if (pHorizontalScrollBar)
    231         pHorizontalScrollBar->setStyleSheet(horizontalScrollBarStyle);
    232 
    233 #if defined(RT_OS_SOLARIS)
    234     /* Use system fixed-width font on Solaris hosts as the Courier family fonts don't render well. */
    235     QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
    236 #else
    237     QFont font;
    238     font.setFamily("Courier New,courier");
    239 #endif
    240     m_pPlainTextEdit->setFont(font);
    241     m_pPlainTextEdit->setWordWrapMode(QTextOption::NoWrap);
    242     m_pPlainTextEdit->setReadOnly(true);
    243     connect(qobject_cast<UIVMLogViewerTextEdit*>(m_pPlainTextEdit), &UIVMLogViewerTextEdit::sigContextMenuBookmarkAction,
     81    m_pTextEdit = new UIVMLogViewerTextEdit(this);
     82    m_pMainLayout->addWidget(m_pTextEdit);
     83
     84    connect(qobject_cast<UIVMLogViewerTextEdit*>(m_pTextEdit), &UIVMLogViewerTextEdit::sigContextMenuBookmarkAction,
    24485            this, &UIVMLogPage::sltAddBookmark);
    24586}
     
    24788QPlainTextEdit *UIVMLogPage::textEdit()
    24889{
    249     return m_pPlainTextEdit;
     90    return m_pTextEdit;
    25091}
    25192
    25293QTextDocument* UIVMLogPage::document()
    25394{
    254     if (!m_pPlainTextEdit)
     95    if (!m_pTextEdit)
    25596        return 0;
    256     return m_pPlainTextEdit->document();
     97    return m_pTextEdit->document();
    25798}
    25899
     
    297138void UIVMLogPage::setTextEdit(const QString &strText)
    298139{
    299     m_pPlainTextEdit->setPlainText(strText);
     140    m_pTextEdit->setPlainText(strText);
    300141    /* Move the cursor position to end: */
    301     QTextCursor cursor = m_pPlainTextEdit->textCursor();
     142    QTextCursor cursor = m_pTextEdit->textCursor();
    302143    cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
    303     m_pPlainTextEdit->setTextCursor(cursor);
     144    m_pTextEdit->setTextCursor(cursor);
    304145    repaint();
    305146}
     
    307148void UIVMLogPage::markForError()
    308149{
    309     QPalette pal = m_pPlainTextEdit->palette();
     150    QPalette pal = m_pTextEdit->palette();
    310151    pal.setColor(QPalette::Base, pal.color(QPalette::Window));
    311     m_pPlainTextEdit->setPalette(pal);
     152    m_pTextEdit->setPalette(pal);
    312153}
    313154
    314155void UIVMLogPage::setScrollBarMarkingsVector(const QVector<float> &vector)
    315156{
    316     if (!m_pPlainTextEdit)
    317         return;
    318     UIIndicatorScrollBar* scrollBar = qobject_cast<UIIndicatorScrollBar*>(m_pPlainTextEdit->verticalScrollBar());
    319     if (scrollBar)
    320         scrollBar->setMarkingsVector(vector);
     157    if (!m_pTextEdit)
     158        return;
     159    m_pTextEdit->setScrollBarMarkingsVector(vector);
    321160    repaint();
    322161}
     
    324163void UIVMLogPage::clearScrollBarMarkingsVector()
    325164{
    326     if (!m_pPlainTextEdit)
    327         return;
    328     UIIndicatorScrollBar* scrollBar = qobject_cast<UIIndicatorScrollBar*>(m_pPlainTextEdit->verticalScrollBar());
    329     if (scrollBar)
    330         scrollBar->clearMarkingsVector();
     165    if (!m_pTextEdit)
     166        return;
     167    m_pTextEdit->clearScrollBarMarkingsVector();
    331168    repaint();
    332169}
     
    334171void UIVMLogPage::documentUndo()
    335172{
    336     if (!m_pPlainTextEdit)
    337         return;
    338     if (m_pPlainTextEdit->document())
    339         m_pPlainTextEdit->document()->undo();
     173    if (!m_pTextEdit)
     174        return;
     175    if (m_pTextEdit->document())
     176        m_pTextEdit->document()->undo();
    340177}
    341178
     
    355192void UIVMLogPage::scrollToBookmark(int bookmarkIndex)
    356193{
    357     if(!m_pPlainTextEdit)
     194    if(!m_pTextEdit)
    358195        return;
    359196    if (bookmarkIndex >= m_bookmarkVector.size())
     
    361198
    362199    int lineNumber = m_bookmarkVector.at(bookmarkIndex).first;
    363     QTextDocument* document = m_pPlainTextEdit->document();
     200    QTextDocument* document = m_pTextEdit->document();
    364201    if(!document)
    365202        return;
    366203
    367204    QTextCursor cursor(document->findBlockByLineNumber(lineNumber));
    368     m_pPlainTextEdit->setTextCursor(cursor);
     205    m_pTextEdit->moveCursor(QTextCursor::End);
     206    m_pTextEdit->setTextCursor(cursor);
     207
    369208}
    370209
     
    393232    // sltCreateBookmarkAtLine(bookmark);
    394233//}
    395 
    396 
    397 #include "UIVMLogPage.moc"
  • trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogPage.h

    r70549 r70554  
    2828#include "QIWithRetranslateUI.h"
    2929
    30 /* COM includes: */
    31 #include "COMEnums.h"
    32 #include "CMachine.h"
    33 
    3430/* Forward declarations: */
    3531class QITabWidget;
     32class QHBoxLayout;
    3633class QPlainTextEdit;
    37 class QHBoxLayout;
    38 class UIToolBar;
    39 class UIVMLogViewerBookmarksPanel;
    40 class UIVMLogViewerFilterPanel;
    41 class UIVMLogViewerPanel;
    42 class UIVMLogViewerSearchPanel;
     34class UIVMLogViewerTextEdit;
    4335
    4436/* Type definitions: */
     
    109101
    110102    QHBoxLayout    *m_pMainLayout;
    111     QPlainTextEdit *m_pPlainTextEdit;
     103    UIVMLogViewerTextEdit *m_pTextEdit;
    112104    /** Stores the log file (unmodified) content. */
    113105    QString         m_strLog;
  • trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogViewerTextEdit.cpp

    r70549 r70554  
    2121
    2222/* Qt includes: */
    23 # include <QDateTime>
    24 # include <QDir>
    25 # include <QVBoxLayout>
    2623# if defined(RT_OS_SOLARIS)
    2724#  include <QFontDatabase>
    2825# endif
     26# include <QMenu>
    2927# include <QPainter>
    3028# include <QPlainTextEdit>
     
    3331
    3432/* GUI includes: */
    35 # include "QIFileDialog.h"
    36 # include "QITabWidget.h"
    37 # include "UIExtraDataManager.h"
    38 # include "UIIconPool.h"
    39 # include "UIMessageCenter.h"
    40 # include "UIVMLogPage.h"
    41 # include "UIVMLogViewerBookmarksPanel.h"
    42 # include "UIVMLogViewerFilterPanel.h"
    43 # include "UIVMLogViewerSearchPanel.h"
    44 # include "UIToolBar.h"
    45 
    46 /* COM includes: */
    47 # include "CSystemProperties.h"
    48 
    49 # include "VBoxGlobal.h"
     33# include "UIVMLogViewerTextEdit.h"
    5034
    5135#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
    52 
    5336
    5437/** We use a modified scrollbar style for our QPlainTextEdits to get the
     
    125108};
    126109
    127 /* Sub-class QPlainTextEdit for some addtional context menu items: */
    128 class UIVMLogViewerTextEdit : public QPlainTextEdit
    129 {
    130     Q_OBJECT;
    131 
    132 signals:
    133 
    134     void sigContextMenuBookmarkAction(LogBookmark bookmark);
    135 
     110class UILineNumberArea : public QWidget
     111{
    136112public:
    137     UIVMLogViewerTextEdit(QWidget* parent = 0)
    138         :QPlainTextEdit(parent)
    139     {
    140         //setStyleSheet("background-color: rgba(240, 240, 240, 75%) ");
     113    UILineNumberArea(UIVMLogViewerTextEdit *textEdit)
     114        :QWidget(textEdit)
     115        , m_pTextEdit(textEdit){}
     116
     117    QSize sizeHint() const override
     118    {
     119        return QSize(m_pTextEdit->lineNumberAreaWidth(), 0);
    141120    }
    142121
    143122protected:
    144123
    145     void contextMenuEvent(QContextMenuEvent *pEvent)
    146     {
    147         QMenu *menu = createStandardContextMenu();
    148         QAction *pAction = menu->addAction(tr("Bookmark"));
    149         QTextBlock block = cursorForPosition(pEvent->pos()).block();
    150         m_iContextMenuBookmark.first = block.firstLineNumber();
    151         m_iContextMenuBookmark.second = block.text();
    152 
    153         if (pAction)
    154             connect(pAction, &QAction::triggered, this, &UIVMLogViewerTextEdit::sltBookmark);
    155 
    156         menu->exec(pEvent->globalPos());
    157 
    158         if (pAction)
    159             disconnect(pAction, &QAction::triggered, this, &UIVMLogViewerTextEdit::sltBookmark);
    160 
    161         delete menu;
    162     }
    163 
    164     virtual void mousePressEvent(QMouseEvent *pEvent)
    165     {
    166         QPlainTextEdit::mousePressEvent(pEvent);
    167     }
    168 
    169 private slots:
    170 
    171     void sltBookmark()
    172     {
    173         emit sigContextMenuBookmarkAction(m_iContextMenuBookmark);
     124    void paintEvent(QPaintEvent *event) override
     125    {
     126        m_pTextEdit->lineNumberAreaPaintEvent(event);
    174127    }
    175128
    176129private:
    177 
    178     /* Line number and text at the context menu position */
    179     LogBookmark m_iContextMenuBookmark;
     130    UIVMLogViewerTextEdit *m_pTextEdit;
    180131};
    181132
    182133
    183 UIVMLogPage::UIVMLogPage(QWidget *pParent /* = 0 */, int tabIndex /*= -1 */)
    184     : QIWithRetranslateUI<QWidget>(pParent)
    185     , m_pMainLayout(0)
    186     , m_pPlainTextEdit(0)
    187     , m_tabIndex(tabIndex)
    188 {
     134UIVMLogViewerTextEdit::UIVMLogViewerTextEdit(QWidget* parent /* = 0 */)
     135    :QPlainTextEdit(parent)
     136    , m_pLineNumberArea(0)
     137{
     138
     139    //setStyleSheet("background-color: rgba(240, 240, 240, 75%) ");
    189140    prepare();
    190141}
    191142
    192 UIVMLogPage::~UIVMLogPage()
    193 {
    194     cleanup();
    195 }
    196 
    197 int UIVMLogPage::defaultLogPageWidth() const
    198 {
    199     if (!m_pPlainTextEdit)
    200         return 0;
    201 
    202     /* Compute a width for 132 characters plus scrollbar and frame width: */
    203     int iDefaultWidth = m_pPlainTextEdit->fontMetrics().width(QChar('x')) * 132 +
    204                         m_pPlainTextEdit->verticalScrollBar()->width() +
    205                         m_pPlainTextEdit->frameWidth() * 2;
    206 
    207     return iDefaultWidth;
    208 }
    209 
    210 
    211 void UIVMLogPage::prepare()
     143void UIVMLogViewerTextEdit::prepare()
    212144{
    213145    prepareWidgets();
    214     retranslateUi();
    215 }
    216 
    217 void UIVMLogPage::prepareWidgets()
    218 {
    219     m_pMainLayout = new QHBoxLayout();
    220     setLayout(m_pMainLayout);
    221     m_pMainLayout->setSpacing(0);
    222     m_pMainLayout->setContentsMargins(0, 0, 0, 0);
    223 
    224     m_pPlainTextEdit = new UIVMLogViewerTextEdit(this);
    225     m_pMainLayout->addWidget(m_pPlainTextEdit);
    226 
    227     m_pPlainTextEdit->setVerticalScrollBar(new UIIndicatorScrollBar());
    228     m_pPlainTextEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    229     QScrollBar *pHorizontalScrollBar = m_pPlainTextEdit->horizontalScrollBar();
     146}
     147
     148void UIVMLogViewerTextEdit::prepareWidgets()
     149{
     150    m_pLineNumberArea = new UILineNumberArea(this);
     151
     152    connect(this, &UIVMLogViewerTextEdit::blockCountChanged, this, &UIVMLogViewerTextEdit::sltUpdateLineNumberAreaWidth);
     153    connect(this, &UIVMLogViewerTextEdit::updateRequest, this, &UIVMLogViewerTextEdit::sltUpdateLineNumberArea);
     154    sltUpdateLineNumberAreaWidth(0);
     155
     156    setVerticalScrollBar(new UIIndicatorScrollBar());
     157    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
     158    QScrollBar *pHorizontalScrollBar = horizontalScrollBar();
    230159    if (pHorizontalScrollBar)
    231160        pHorizontalScrollBar->setStyleSheet(horizontalScrollBarStyle);
     161
    232162
    233163#if defined(RT_OS_SOLARIS)
     
    238168    font.setFamily("Courier New,courier");
    239169#endif
    240     m_pPlainTextEdit->setFont(font);
    241     m_pPlainTextEdit->setWordWrapMode(QTextOption::NoWrap);
    242     m_pPlainTextEdit->setReadOnly(true);
    243     connect(qobject_cast<UIVMLogViewerTextEdit*>(m_pPlainTextEdit), &UIVMLogViewerTextEdit::sigContextMenuBookmarkAction,
    244             this, &UIVMLogPage::sltAddBookmark);
    245 }
    246 
    247 QPlainTextEdit *UIVMLogPage::textEdit()
    248 {
    249     return m_pPlainTextEdit;
    250 }
    251 
    252 QTextDocument* UIVMLogPage::document()
    253 {
    254     if (!m_pPlainTextEdit)
    255         return 0;
    256     return m_pPlainTextEdit->document();
    257 }
    258 
    259 void UIVMLogPage::setTabIndex(int index)
    260 {
    261     m_tabIndex = index;
    262 }
    263 
    264 int UIVMLogPage::tabIndex()  const
    265 {
    266     return m_tabIndex;
    267 }
    268 
    269 void UIVMLogPage::retranslateUi()
    270 {
    271 }
    272 
    273 void UIVMLogPage::cleanup()
    274 {
    275 }
    276 
    277 void UIVMLogPage::setLogString(const QString &strLog)
    278 {
    279     m_strLog = strLog;
    280 }
    281 
    282 const QString& UIVMLogPage::logString() const
    283 {
    284     return m_strLog;
    285 }
    286 
    287 void UIVMLogPage::setFileName(const QString &strFileName)
    288 {
    289     m_strFileName = strFileName;
    290 }
    291 
    292 const QString& UIVMLogPage::fileName() const
    293 {
    294     return m_strFileName;
    295 }
    296 
    297 void UIVMLogPage::setTextEdit(const QString &strText)
    298 {
    299     m_pPlainTextEdit->setPlainText(strText);
    300     /* Move the cursor position to end: */
    301     QTextCursor cursor = m_pPlainTextEdit->textCursor();
    302     cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
    303     m_pPlainTextEdit->setTextCursor(cursor);
    304     repaint();
    305 }
    306 
    307 void UIVMLogPage::markForError()
    308 {
    309     QPalette pal = m_pPlainTextEdit->palette();
    310     pal.setColor(QPalette::Base, pal.color(QPalette::Window));
    311     m_pPlainTextEdit->setPalette(pal);
    312 }
    313 
    314 void UIVMLogPage::setScrollBarMarkingsVector(const QVector<float> &vector)
    315 {
    316     if (!m_pPlainTextEdit)
    317         return;
    318     UIIndicatorScrollBar* scrollBar = qobject_cast<UIIndicatorScrollBar*>(m_pPlainTextEdit->verticalScrollBar());
    319     if (scrollBar)
    320         scrollBar->setMarkingsVector(vector);
    321     repaint();
    322 }
    323 
    324 void UIVMLogPage::clearScrollBarMarkingsVector()
    325 {
    326     if (!m_pPlainTextEdit)
    327         return;
    328     UIIndicatorScrollBar* scrollBar = qobject_cast<UIIndicatorScrollBar*>(m_pPlainTextEdit->verticalScrollBar());
    329     if (scrollBar)
    330         scrollBar->clearMarkingsVector();
    331     repaint();
    332 }
    333 
    334 void UIVMLogPage::documentUndo()
    335 {
    336     if (!m_pPlainTextEdit)
    337         return;
    338     if (m_pPlainTextEdit->document())
    339         m_pPlainTextEdit->document()->undo();
    340 }
    341 
    342 
    343 void UIVMLogPage::deleteBookmark(int index)
    344 {
    345     if (m_bookmarkVector.size() <= index)
    346          return;
    347     m_bookmarkVector.remove(index, 1);
    348 }
    349 
    350 void UIVMLogPage::deleteAllBookmarks()
    351 {
    352     m_bookmarkVector.clear();
    353 }
    354 
    355 void UIVMLogPage::scrollToBookmark(int bookmarkIndex)
    356 {
    357     if(!m_pPlainTextEdit)
    358         return;
    359     if (bookmarkIndex >= m_bookmarkVector.size())
    360         return;
    361 
    362     int lineNumber = m_bookmarkVector.at(bookmarkIndex).first;
    363     QTextDocument* document = m_pPlainTextEdit->document();
    364     if(!document)
    365         return;
    366 
    367     QTextCursor cursor(document->findBlockByLineNumber(lineNumber));
    368     m_pPlainTextEdit->setTextCursor(cursor);
    369 }
    370 
    371 const QVector<LogBookmark>& UIVMLogPage::bookmarkVector() const
    372 {
    373     return m_bookmarkVector;
    374 }
    375 
    376 void UIVMLogPage::sltAddBookmark(LogBookmark bookmark)
    377 {
    378     m_bookmarkVector.push_back(bookmark);
    379     emit sigBookmarksUpdated();
    380 }
    381 // void UIVMLogViewerWidget::sltCreateBookmarkAtCurrent()
    382 // {
    383     // if (!currentTextEdit())
    384     //     return;
    385     // QWidget* viewport = currentTextEdit()->viewport();
    386     // if (!viewport)
    387     //     return;
    388     // QPoint point(0.5 * viewport->width(), 0.5 * viewport->height());
    389     // QTextBlock block = currentTextEdit()->cursorForPosition(point).block();
    390     // LogBookmark bookmark;
    391     // bookmark.first = block.firstLineNumber();
    392     // bookmark.second = block.text();
    393     // sltCreateBookmarkAtLine(bookmark);
    394 //}
    395 
    396 
    397 #include "UIVMLogPage.moc"
     170    setFont(font);
     171    setWordWrapMode(QTextOption::NoWrap);
     172    setReadOnly(true);
     173    if(m_pLineNumberArea)
     174        m_pLineNumberArea->setFont(font);
     175
     176}
     177
     178int UIVMLogViewerTextEdit::lineNumberAreaWidth()
     179{
     180    int digits = 1;
     181    int max = qMax(1, blockCount());
     182    while (max >= 10) {
     183        max /= 10;
     184        ++digits;
     185    }
     186
     187    int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
     188
     189    return space;
     190}
     191
     192void UIVMLogViewerTextEdit::lineNumberAreaPaintEvent(QPaintEvent *event)
     193{
     194    QPainter painter(m_pLineNumberArea);
     195    painter.fillRect(event->rect(), Qt::lightGray);
     196    QTextBlock block = firstVisibleBlock();
     197    int blockNumber = block.blockNumber();
     198    int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
     199    int bottom = top + (int) blockBoundingRect(block).height();
     200    while (block.isValid() && top <= event->rect().bottom()) {
     201        if (block.isVisible() && bottom >= event->rect().top()) {
     202            QString number = QString::number(blockNumber + 1);
     203            painter.setPen(Qt::black);
     204            painter.drawText(0, top, m_pLineNumberArea->width(), m_pLineNumberArea->fontMetrics().lineSpacing(),
     205                             Qt::AlignRight, number);
     206        }
     207        block = block.next();
     208        top = bottom;
     209        bottom = top + (int) blockBoundingRect(block).height();
     210        ++blockNumber;
     211    }
     212}
     213
     214void UIVMLogViewerTextEdit::contextMenuEvent(QContextMenuEvent *pEvent)
     215{
     216    QMenu *menu = createStandardContextMenu();
     217    QAction *pAction = menu->addAction(tr("Bookmark"));
     218    QTextBlock block = cursorForPosition(pEvent->pos()).block();
     219    m_iContextMenuBookmark.first = block.firstLineNumber();
     220    m_iContextMenuBookmark.second = block.text();
     221
     222    if (pAction)
     223        connect(pAction, &QAction::triggered, this, &UIVMLogViewerTextEdit::sltBookmark);
     224
     225    menu->exec(pEvent->globalPos());
     226
     227    if (pAction)
     228        disconnect(pAction, &QAction::triggered, this, &UIVMLogViewerTextEdit::sltBookmark);
     229
     230    delete menu;
     231}
     232
     233void UIVMLogViewerTextEdit::resizeEvent(QResizeEvent *pEvent)
     234{
     235    QPlainTextEdit::resizeEvent(pEvent);
     236
     237    QRect cr = contentsRect();
     238    m_pLineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
     239}
     240
     241
     242void UIVMLogViewerTextEdit::sltUpdateLineNumberAreaWidth(int /* newBlockCount */)
     243{
     244    setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
     245}
     246
     247void UIVMLogViewerTextEdit::sltUpdateLineNumberArea(const QRect &rect, int dy)
     248{
     249    if (dy)
     250        m_pLineNumberArea->scroll(0, dy);
     251    else
     252        m_pLineNumberArea->update(0, rect.y(), m_pLineNumberArea->width(), rect.height());
     253
     254    if (rect.contains(viewport()->rect()))
     255        sltUpdateLineNumberAreaWidth(0);
     256}
     257
     258void UIVMLogViewerTextEdit::sltBookmark()
     259{
     260    emit sigContextMenuBookmarkAction(m_iContextMenuBookmark);
     261}
     262
     263void UIVMLogViewerTextEdit::setScrollBarMarkingsVector(const QVector<float> &vector)
     264{
     265    UIIndicatorScrollBar* vScrollBar = qobject_cast<UIIndicatorScrollBar*>(verticalScrollBar());
     266    if(vScrollBar)
     267        vScrollBar->setMarkingsVector(vector);
     268}
     269
     270void UIVMLogViewerTextEdit::clearScrollBarMarkingsVector()
     271{
     272    UIIndicatorScrollBar* vScrollBar = qobject_cast<UIIndicatorScrollBar*>(verticalScrollBar());
     273    if(vScrollBar)
     274        vScrollBar->clearMarkingsVector();
     275}
     276
     277
     278#include "UIVMLogViewerTextEdit.moc"
  • trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogViewerTextEdit.h

    r70549 r70554  
    1616 */
    1717
    18 #ifndef ___UIVMLogPage_h___
    19 #define ___UIVMLogPage_h___
     18#ifndef ___UIVMLogViewerTextEdit_h___
     19#define ___UIVMLogViewerTextEdit_h___
    2020
    2121/* Qt includes: */
    22 #include <QWidget>
    23 /* #include <QMap> */
     22#include <QPlainTextEdit>
    2423#include <QPair>
    2524
    26 /* GUI includes: */
    27 #include "QIManagerDialog.h"
    28 #include "QIWithRetranslateUI.h"
    2925
    30 /* COM includes: */
    31 #include "COMEnums.h"
    32 #include "CMachine.h"
    33 
    34 /* Forward declarations: */
    35 class QITabWidget;
    36 class QPlainTextEdit;
    37 class QHBoxLayout;
    38 class UIToolBar;
    39 class UIVMLogViewerBookmarksPanel;
    40 class UIVMLogViewerFilterPanel;
    41 class UIVMLogViewerPanel;
    42 class UIVMLogViewerSearchPanel;
    43 
    44 /* Type definitions: */
    45 /** first is line number, second is block text */
    46 typedef QPair<int, QString> LogBookmark;
    47 
    48 class UIVMLogPage  : public QIWithRetranslateUI<QWidget>
     26/* QPlainTextEdit extension for some addtional context menu items,
     27   a special scrollbar, line number area, and bookmark support: */
     28class UIVMLogViewerTextEdit : public QPlainTextEdit
    4929{
    5030    Q_OBJECT;
     
    5232signals:
    5333
    54     void sigBookmarksUpdated();
     34    void sigContextMenuBookmarkAction(QPair<int, QString> bookmark);
    5535
    5636public:
    5737
    58     UIVMLogPage(QWidget *pParent = 0, int tabIndex = -1);
    59     /** Destructs the VM Log-Viewer. */
    60     ~UIVMLogPage();
    61     /** Returns the width of the current log page. return 0 if there is no current log page: */
    62     int defaultLogPageWidth() const;
    63 
    64     QPlainTextEdit *textEdit();
    65     QTextDocument  *document();
    66 
    67     void setTabIndex(int index);
    68     int tabIndex()  const;
    69 
    70     /* Only to be called when log file is re-read. */
    71     void setLogString(const QString &strLog);
    72     const QString& logString() const;
    73 
    74     void setFileName(const QString &strFileName);
    75     const QString& fileName() const;
    76 
    77     /** Ses plaintextEdit's text. Note that the text we
    78         show currently might be different than
    79         m_strLog. For example during filtering. */
    80     void setTextEdit(const QString &strText);
    81 
    82     /** Marks the plain text edit When we dont have a log content. */
    83     void markForError();
    84 
     38    UIVMLogViewerTextEdit(QWidget* parent = 0);
     39    int  lineNumberAreaWidth();
     40    void lineNumberAreaPaintEvent(QPaintEvent *event);
     41    /** Forwards the call to scroll bar class */
    8542    void setScrollBarMarkingsVector(const QVector<float> &vector);
     43    /** Forwards the call to scroll bar class */
    8644    void clearScrollBarMarkingsVector();
    87 
    88     /** Undos the changes done to textDocument */
    89     void documentUndo();
    90 
    91     void deleteBookmark(int index);
    92 
    93     const QVector<LogBookmark>& bookmarkVector() const;
    94     void deleteAllBookmarks();
    95     /** Scrolls the plain text edit to the bookmark with index @a bookmarkIndex. */
    96     void scrollToBookmark(int bookmarkIndex);
    9745
    9846protected:
    9947
     48    void contextMenuEvent(QContextMenuEvent *pEvent);
     49    void resizeEvent(QResizeEvent *pEvent);
     50
    10051private slots:
    10152
    102     void sltAddBookmark(LogBookmark bookmark);
     53    void sltBookmark();
     54    void sltUpdateLineNumberAreaWidth(int newBlockCount);
     55    void sltUpdateLineNumberArea(const QRect &, int);
    10356
    10457private:
     58
    10559    void prepare();
    10660    void prepareWidgets();
    107     void cleanup();
    108     void retranslateUi();
    10961
    110     QHBoxLayout    *m_pMainLayout;
    111     QPlainTextEdit *m_pPlainTextEdit;
    112     /** Stores the log file (unmodified) content. */
    113     QString         m_strLog;
    114     /** Stores full path and name of the log file. */
    115     QString         m_strFileName;
    116     /** This is the index of the tab containing this widget in UIVMLogViewerWidget. */
    117     int             m_tabIndex;
    118 
    119     QVector<LogBookmark> m_bookmarkVector;
    120 
     62    /* Line number and text at the context menu position */
     63    QPair<int, QString>  m_iContextMenuBookmark;
     64    QWidget             *m_pLineNumberArea;
    12165};
    12266
     67
     68
     69
    12370#endif /* !___UIVMLogPage_h___ */
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