1 | /* $Id: UISearchLineEdit.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIsearchLineEdit class definitions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
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
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* Qt includes */
|
---|
29 | #include <QApplication>
|
---|
30 | #include <QPainter>
|
---|
31 |
|
---|
32 | /* GUI includes: */
|
---|
33 | #include "UISearchLineEdit.h"
|
---|
34 |
|
---|
35 | UISearchLineEdit::UISearchLineEdit(QWidget *pParent /* = 0 */)
|
---|
36 | :QLineEdit(pParent)
|
---|
37 | , m_iMatchCount(0)
|
---|
38 | , m_iScrollToIndex(-1)
|
---|
39 | , m_fMark(true)
|
---|
40 | , m_unmarkColor(palette().color(QPalette::Base))
|
---|
41 | , m_markColor(QColor(m_unmarkColor.red(),
|
---|
42 | 0.7 * m_unmarkColor.green(),
|
---|
43 | 0.7 * m_unmarkColor.blue()))
|
---|
44 | {
|
---|
45 | }
|
---|
46 |
|
---|
47 | void UISearchLineEdit::paintEvent(QPaintEvent *pEvent)
|
---|
48 | {
|
---|
49 | QLineEdit::paintEvent(pEvent);
|
---|
50 |
|
---|
51 | /* No search terms. no search. nothing to show here: */
|
---|
52 | if (text().isEmpty())
|
---|
53 | {
|
---|
54 | colorBackground(false);
|
---|
55 | return;
|
---|
56 | }
|
---|
57 | /* Draw the total match count and the current scrolled item's index on the right hand side of the line edit: */
|
---|
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));
|
---|
61 | #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
|
---|
62 | QSize textSize(QApplication::fontMetrics().horizontalAdvance(strText),
|
---|
63 | QApplication::fontMetrics().height());
|
---|
64 | #else
|
---|
65 | QSize textSize(QApplication::fontMetrics().width(strText),
|
---|
66 | QApplication::fontMetrics().height());
|
---|
67 | #endif
|
---|
68 |
|
---|
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;
|
---|
74 |
|
---|
75 | QColor fontColor(Qt::black);
|
---|
76 | painter.setPen(fontColor);
|
---|
77 | painter.setFont(pfont);
|
---|
78 |
|
---|
79 | painter.drawText(QRect(width() - textSize.width() - iRightMargin, iTopMargin, textSize.width(), textSize.height()),
|
---|
80 | Qt::AlignCenter | Qt::AlignVCenter, strText);
|
---|
81 | colorBackground(m_iMatchCount == 0);
|
---|
82 | }
|
---|
83 |
|
---|
84 | void UISearchLineEdit::setMatchCount(int iMatchCount)
|
---|
85 | {
|
---|
86 | if (m_iMatchCount == iMatchCount)
|
---|
87 | return;
|
---|
88 | m_iMatchCount = iMatchCount;
|
---|
89 | repaint();
|
---|
90 | }
|
---|
91 |
|
---|
92 | void UISearchLineEdit::setScrollToIndex(int iScrollToIndex)
|
---|
93 | {
|
---|
94 | if (m_iScrollToIndex == iScrollToIndex)
|
---|
95 | return;
|
---|
96 | m_iScrollToIndex = iScrollToIndex;
|
---|
97 | repaint();
|
---|
98 | }
|
---|
99 |
|
---|
100 | void UISearchLineEdit::reset()
|
---|
101 | {
|
---|
102 | clear();
|
---|
103 | m_iMatchCount = 0;
|
---|
104 | m_iScrollToIndex = 0;
|
---|
105 | colorBackground(false);
|
---|
106 | }
|
---|
107 |
|
---|
108 | void UISearchLineEdit::colorBackground(bool fWarning)
|
---|
109 | {
|
---|
110 | QPalette mPalette = QApplication::palette();
|
---|
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 | }
|
---|