[26715] | 1 | /* $Id: UISearchLineEdit.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
[25178] | 2 | /** @file
|
---|
[77702] | 3 | * VBox Qt GUI - UIsearchLineEdit class definitions.
|
---|
[25178] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2009-2023 Oracle and/or its affiliates.
|
---|
[25178] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[25178] | 26 | */
|
---|
| 27 |
|
---|
[77702] | 28 | /* Qt includes */
|
---|
| 29 | #include <QApplication>
|
---|
| 30 | #include <QPainter>
|
---|
[25178] | 31 |
|
---|
[71355] | 32 | /* GUI includes: */
|
---|
[77702] | 33 | #include "UISearchLineEdit.h"
|
---|
[71355] | 34 |
|
---|
[77702] | 35 | UISearchLineEdit::UISearchLineEdit(QWidget *pParent /* = 0 */)
|
---|
| 36 | :QLineEdit(pParent)
|
---|
| 37 | , m_iMatchCount(0)
|
---|
| 38 | , m_iScrollToIndex(-1)
|
---|
[78060] | 39 | , m_fMark(true)
|
---|
| 40 | , m_unmarkColor(palette().color(QPalette::Base))
|
---|
| 41 | , m_markColor(QColor(m_unmarkColor.red(),
|
---|
[86816] | 42 | 0.7 * m_unmarkColor.green(),
|
---|
| 43 | 0.7 * m_unmarkColor.blue()))
|
---|
[25178] | 44 | {
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[77702] | 47 | void UISearchLineEdit::paintEvent(QPaintEvent *pEvent)
|
---|
[25178] | 48 | {
|
---|
[77702] | 49 | QLineEdit::paintEvent(pEvent);
|
---|
[25178] | 50 |
|
---|
[77723] | 51 | /* No search terms. no search. nothing to show here: */
|
---|
[77702] | 52 | if (text().isEmpty())
|
---|
[78060] | 53 | {
|
---|
| 54 | colorBackground(false);
|
---|
[77702] | 55 | return;
|
---|
[78060] | 56 | }
|
---|
[77723] | 57 | /* Draw the total match count and the current scrolled item's index on the right hand side of the line edit: */
|
---|
[77702] | 58 | QPainter painter(this);
|
---|
| 59 | QFont pfont = font();
|
---|
| 60 | QString strText = QString("%1/%2").arg(QString::number(m_iScrollToIndex + 1)).arg(QString::number(m_iMatchCount));
|
---|
[93998] | 61 | #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
|
---|
| 62 | QSize textSize(QApplication::fontMetrics().horizontalAdvance(strText),
|
---|
| 63 | QApplication::fontMetrics().height());
|
---|
| 64 | #else
|
---|
[77702] | 65 | QSize textSize(QApplication::fontMetrics().width(strText),
|
---|
| 66 | QApplication::fontMetrics().height());
|
---|
[93998] | 67 | #endif
|
---|
[25178] | 68 |
|
---|
[77702] | 69 | /* Dont draw anything if we dont have enough space: */
|
---|
| 70 | if (textSize.width() > 0.5 * width())
|
---|
| 71 | return;
|
---|
| 72 | int iTopMargin = (height() - textSize.height()) / 2;
|
---|
| 73 | int iRightMargin = iTopMargin;
|
---|
[25178] | 74 |
|
---|
[86816] | 75 | QColor fontColor(Qt::black);
|
---|
[77702] | 76 | painter.setPen(fontColor);
|
---|
| 77 | painter.setFont(pfont);
|
---|
[25178] | 78 |
|
---|
[77702] | 79 | painter.drawText(QRect(width() - textSize.width() - iRightMargin, iTopMargin, textSize.width(), textSize.height()),
|
---|
| 80 | Qt::AlignCenter | Qt::AlignVCenter, strText);
|
---|
[78060] | 81 | colorBackground(m_iMatchCount == 0);
|
---|
[25178] | 82 | }
|
---|
| 83 |
|
---|
[77702] | 84 | void UISearchLineEdit::setMatchCount(int iMatchCount)
|
---|
[25178] | 85 | {
|
---|
[77702] | 86 | if (m_iMatchCount == iMatchCount)
|
---|
| 87 | return;
|
---|
| 88 | m_iMatchCount = iMatchCount;
|
---|
| 89 | repaint();
|
---|
[25178] | 90 | }
|
---|
| 91 |
|
---|
[86821] | 92 | void UISearchLineEdit::setScrollToIndex(int iScrollToIndex)
|
---|
[25178] | 93 | {
|
---|
[77702] | 94 | if (m_iScrollToIndex == iScrollToIndex)
|
---|
| 95 | return;
|
---|
| 96 | m_iScrollToIndex = iScrollToIndex;
|
---|
| 97 | repaint();
|
---|
[25178] | 98 | }
|
---|
[78060] | 99 |
|
---|
[78459] | 100 | void UISearchLineEdit::reset()
|
---|
| 101 | {
|
---|
| 102 | clear();
|
---|
| 103 | m_iMatchCount = 0;
|
---|
| 104 | m_iScrollToIndex = 0;
|
---|
| 105 | colorBackground(false);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[78060] | 108 | void UISearchLineEdit::colorBackground(bool fWarning)
|
---|
| 109 | {
|
---|
[88862] | 110 | QPalette mPalette = QApplication::palette();
|
---|
[78060] | 111 | /** Make sure we reset color. */
|
---|
| 112 | if (!fWarning || !m_fMark)
|
---|
| 113 | {
|
---|
| 114 | mPalette.setColor(QPalette::Base, m_unmarkColor);
|
---|
| 115 | setPalette(mPalette);
|
---|
| 116 | return;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | if (m_fMark && fWarning)
|
---|
| 120 | {
|
---|
| 121 | mPalette.setColor(QPalette::Base, m_markColor);
|
---|
| 122 | setPalette(mPalette);
|
---|
| 123 | return;
|
---|
| 124 | }
|
---|
| 125 | }
|
---|