VirtualBox

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

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette