1 | /* $Id: UISearchLineEdit.cpp 78459 2019-05-10 07:31:41Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIsearchLineEdit class definitions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /* Qt includes */
|
---|
19 | #include <QApplication>
|
---|
20 | #include <QPainter>
|
---|
21 |
|
---|
22 | /* GUI includes: */
|
---|
23 | #include "UISearchLineEdit.h"
|
---|
24 |
|
---|
25 | UISearchLineEdit::UISearchLineEdit(QWidget *pParent /* = 0 */)
|
---|
26 | :QLineEdit(pParent)
|
---|
27 | , m_iMatchCount(0)
|
---|
28 | , m_iScrollToIndex(-1)
|
---|
29 | , m_fMark(true)
|
---|
30 | , m_unmarkColor(palette().color(QPalette::Base))
|
---|
31 | , m_markColor(QColor(m_unmarkColor.red(),
|
---|
32 | 0.5 * m_unmarkColor.green(),
|
---|
33 | 0.5 * m_unmarkColor.blue()))
|
---|
34 | {
|
---|
35 | }
|
---|
36 |
|
---|
37 | void UISearchLineEdit::paintEvent(QPaintEvent *pEvent)
|
---|
38 | {
|
---|
39 | QLineEdit::paintEvent(pEvent);
|
---|
40 |
|
---|
41 | /* No search terms. no search. nothing to show here: */
|
---|
42 | if (text().isEmpty())
|
---|
43 | {
|
---|
44 | colorBackground(false);
|
---|
45 | return;
|
---|
46 | }
|
---|
47 | /* Draw the total match count and the current scrolled item's index on the right hand side of the line edit: */
|
---|
48 | QPainter painter(this);
|
---|
49 | QFont pfont = font();
|
---|
50 | QString strText = QString("%1/%2").arg(QString::number(m_iScrollToIndex + 1)).arg(QString::number(m_iMatchCount));
|
---|
51 | QSize textSize(QApplication::fontMetrics().width(strText),
|
---|
52 | QApplication::fontMetrics().height());
|
---|
53 |
|
---|
54 | /* Dont draw anything if we dont have enough space: */
|
---|
55 | if (textSize.width() > 0.5 * width())
|
---|
56 | return;
|
---|
57 | int iTopMargin = (height() - textSize.height()) / 2;
|
---|
58 | int iRightMargin = iTopMargin;
|
---|
59 |
|
---|
60 | QColor fontColor(Qt::darkGray);
|
---|
61 | painter.setPen(fontColor);
|
---|
62 | painter.setFont(pfont);
|
---|
63 |
|
---|
64 | painter.drawText(QRect(width() - textSize.width() - iRightMargin, iTopMargin, textSize.width(), textSize.height()),
|
---|
65 | Qt::AlignCenter | Qt::AlignVCenter, strText);
|
---|
66 | colorBackground(m_iMatchCount == 0);
|
---|
67 | }
|
---|
68 |
|
---|
69 | void UISearchLineEdit::setMatchCount(int iMatchCount)
|
---|
70 | {
|
---|
71 | if (m_iMatchCount == iMatchCount)
|
---|
72 | return;
|
---|
73 | m_iMatchCount = iMatchCount;
|
---|
74 | repaint();
|
---|
75 | }
|
---|
76 |
|
---|
77 | void UISearchLineEdit::setScroolToIndex(int iScrollToIndex)
|
---|
78 | {
|
---|
79 | if (m_iScrollToIndex == iScrollToIndex)
|
---|
80 | return;
|
---|
81 | m_iScrollToIndex = iScrollToIndex;
|
---|
82 | repaint();
|
---|
83 | }
|
---|
84 |
|
---|
85 | void UISearchLineEdit::reset()
|
---|
86 | {
|
---|
87 | clear();
|
---|
88 | m_iMatchCount = 0;
|
---|
89 | m_iScrollToIndex = 0;
|
---|
90 | colorBackground(false);
|
---|
91 | }
|
---|
92 |
|
---|
93 | void UISearchLineEdit::colorBackground(bool fWarning)
|
---|
94 | {
|
---|
95 | QPalette mPalette = palette();
|
---|
96 | /** Make sure we reset color. */
|
---|
97 | if (!fWarning || !m_fMark)
|
---|
98 | {
|
---|
99 | mPalette.setColor(QPalette::Base, m_unmarkColor);
|
---|
100 | setPalette(mPalette);
|
---|
101 | return;
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (m_fMark && fWarning)
|
---|
105 | {
|
---|
106 | mPalette.setColor(QPalette::Base, m_markColor);
|
---|
107 | setPalette(mPalette);
|
---|
108 | return;
|
---|
109 | }
|
---|
110 | }
|
---|