VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/manager/chooser/UIChooserSearchWidget.cpp@ 103538

Last change on this file since 103538 was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 KB
Line 
1/* $Id: UIChooserSearchWidget.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIChooserSearchWidget class implementation.
4 */
5
6/*
7 * Copyright (C) 2012-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 <QStyle>
30#include <QVBoxLayout>
31
32/* GUI includes: */
33#include "QILineEdit.h"
34#include "QIToolButton.h"
35#include "UIChooserDefs.h"
36#include "UIChooserSearchWidget.h"
37#include "UIIconPool.h"
38#include "UISearchLineEdit.h"
39
40UIChooserSearchWidget::UIChooserSearchWidget(QWidget *pParent)
41 : QIWithRetranslateUI<QWidget>(pParent)
42 , m_pLineEdit(0)
43 , m_pMainLayout(0)
44 , m_pScrollToNextMatchButton(0)
45 , m_pScrollToPreviousMatchButton(0)
46 , m_pCloseButton(0)
47{
48 /* Have a background. In some cases having no background causes strange artefacts in Cinnamon themes: */
49 setAutoFillBackground(true);
50 prepareWidgets();
51 prepareConnections();
52 retranslateUi();
53}
54
55void UIChooserSearchWidget::setMatchCount(int iMatchCount)
56{
57 if (!m_pLineEdit)
58 return;
59 m_pLineEdit->setMatchCount(iMatchCount);
60}
61
62void UIChooserSearchWidget::setScroolToIndex(int iScrollToIndex)
63{
64 if (!m_pLineEdit)
65 return;
66 m_pLineEdit->setScrollToIndex(iScrollToIndex);
67}
68
69void UIChooserSearchWidget::appendToSearchString(const QString &strSearchText)
70{
71 if (!m_pLineEdit)
72 return;
73 m_pLineEdit->setText(m_pLineEdit->text().append(strSearchText));
74}
75
76void UIChooserSearchWidget::redoSearch()
77{
78 if (!m_pLineEdit)
79 return;
80 sltHandleSearchTermChange(m_pLineEdit->text());
81}
82
83void UIChooserSearchWidget::prepareWidgets()
84{
85 m_pMainLayout = new QHBoxLayout;
86 if (!m_pMainLayout)
87 return;
88
89#ifdef VBOX_WS_MAC
90 m_pMainLayout->setContentsMargins(0, 5, 0, 5);
91 m_pMainLayout->setSpacing(2);
92#else
93 m_pMainLayout->setContentsMargins(qApp->style()->pixelMetric(QStyle::PM_LayoutLeftMargin) / 2,
94 qApp->style()->pixelMetric(QStyle::PM_LayoutTopMargin) / 4,
95 qApp->style()->pixelMetric(QStyle::PM_LayoutRightMargin) / 2,
96 qApp->style()->pixelMetric(QStyle::PM_LayoutBottomMargin) / 4);
97 m_pMainLayout->setSpacing(qApp->style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing) / 2);
98#endif
99
100 m_pCloseButton = new QIToolButton;
101 if (m_pCloseButton)
102 {
103 m_pCloseButton->setIcon(UIIconPool::iconSet(":/close_16px.png"));
104 m_pMainLayout->addWidget(m_pCloseButton, 0, Qt::AlignLeft);
105 }
106
107 m_pLineEdit = new UISearchLineEdit;
108 if (m_pLineEdit)
109 {
110 m_pMainLayout->addWidget(m_pLineEdit);
111 m_pLineEdit->installEventFilter(this);
112 setFocusProxy(m_pLineEdit);
113 }
114
115 m_pScrollToPreviousMatchButton = new QIToolButton;
116 if (m_pScrollToPreviousMatchButton)
117 {
118 m_pScrollToPreviousMatchButton->setIcon(UIIconPool::iconSet(":/log_viewer_search_backward_16px.png",
119 ":/log_viewer_search_backward_disabled_16px.png"));
120 m_pMainLayout->addWidget(m_pScrollToPreviousMatchButton);
121 }
122 m_pScrollToNextMatchButton = new QIToolButton;
123 if (m_pScrollToNextMatchButton)
124 {
125 m_pScrollToNextMatchButton->setIcon(UIIconPool::iconSet(":/log_viewer_search_forward_16px.png",
126 ":/log_viewer_search_forward_disabled_16px.png"));
127 m_pMainLayout->addWidget(m_pScrollToNextMatchButton);
128 }
129
130 setLayout(m_pMainLayout);
131}
132
133void UIChooserSearchWidget::prepareConnections()
134{
135 if (m_pLineEdit)
136 connect(m_pLineEdit, &QILineEdit::textChanged, this, &UIChooserSearchWidget::sltHandleSearchTermChange);
137 if (m_pCloseButton)
138 connect(m_pCloseButton, &QIToolButton::clicked, this, &UIChooserSearchWidget::sltHandleCloseButtonClick);
139 if (m_pScrollToPreviousMatchButton)
140 connect(m_pScrollToPreviousMatchButton, &QIToolButton::clicked, this, &UIChooserSearchWidget::sltHandleScroolToButtonClick);
141 if (m_pScrollToNextMatchButton)
142 connect(m_pScrollToNextMatchButton, &QIToolButton::clicked, this, &UIChooserSearchWidget::sltHandleScroolToButtonClick);
143}
144
145void UIChooserSearchWidget::showEvent(QShowEvent *pEvent)
146{
147 Q_UNUSED(pEvent);
148 if (m_pLineEdit)
149 m_pLineEdit->setFocus();
150}
151
152void UIChooserSearchWidget::hideEvent(QHideEvent *pEvent)
153{
154 Q_UNUSED(pEvent);
155 if (m_pLineEdit)
156 m_pLineEdit->clear();
157}
158
159void UIChooserSearchWidget::retranslateUi()
160{
161 if (m_pScrollToNextMatchButton)
162 m_pScrollToNextMatchButton->setToolTip(tr("Navigate to the next item among the search results"));
163 if (m_pScrollToPreviousMatchButton)
164 m_pScrollToPreviousMatchButton->setToolTip(tr("Navigate to the previous item among the search results"));
165 if (m_pLineEdit)
166 m_pLineEdit->setToolTip(tr("Enter a search term to be used during virtual machine search"));
167 if (m_pCloseButton)
168 m_pCloseButton->setToolTip(tr("Close the search widget"));
169}
170
171bool UIChooserSearchWidget::eventFilter(QObject *pWatched, QEvent *pEvent)
172{
173 /* Handle KeyPress events for m_pLineEdit only: */
174 if ( pWatched == m_pLineEdit
175 && pEvent->type() == QEvent::KeyPress)
176 {
177 QKeyEvent *pKeyEvent = dynamic_cast<QKeyEvent*>(pEvent);
178 if (pKeyEvent)
179 {
180 if (pKeyEvent->key() == Qt::Key_Escape)
181 {
182 emit sigToggleVisibility(false);
183 return true;
184 }
185 else if (pKeyEvent->key() == Qt::Key_Up || pKeyEvent->key() == Qt::Key_Down)
186 {
187 emit sigScrollToMatch(pKeyEvent->key() == Qt::Key_Down);
188 return true;
189 }
190 }
191 }
192
193 /* Call to base-class: */
194 return QIWithRetranslateUI<QWidget>::eventFilter(pWatched, pEvent);
195}
196
197void UIChooserSearchWidget::sltHandleSearchTermChange(const QString &strSearchTerm)
198{
199 if (strSearchTerm.isEmpty())
200 {
201 emit sigToggleVisibility(false);
202 return;
203 }
204 emit sigRedoSearch(strSearchTerm, UIChooserItemSearchFlag_Machine);
205}
206
207void UIChooserSearchWidget::sltHandleScroolToButtonClick()
208{
209 if (sender() == m_pScrollToNextMatchButton)
210 emit sigScrollToMatch(true);
211 else if (sender() == m_pScrollToPreviousMatchButton)
212 emit sigScrollToMatch(false);
213}
214
215void UIChooserSearchWidget::sltHandleCloseButtonClick()
216{
217 emit sigToggleVisibility(false);
218}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use