VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/medium/UIMediumSearchWidget.cpp@ 104158

Last change on this file since 104158 was 103988, checked in by vboxsync, 9 months ago

FE/Qt. bugref:10624. Adding missing override keywords gcc has found.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.5 KB
Line 
1/* $Id: UIMediumSearchWidget.cpp 103988 2024-03-21 13:49:47Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIMediumSearchWidget class implementation.
4 */
5
6/*
7 * Copyright (C) 2006-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 <QComboBox>
30#include <QHBoxLayout>
31#include <QLineEdit>
32#include <QPushButton>
33#include <QPainter>
34#include <QRegularExpression>
35
36/* GUI includes: */
37#include "QIToolButton.h"
38#include "QITreeWidget.h"
39#include "UIIconPool.h"
40#include "UIMediumItem.h"
41#include "UIMediumSearchWidget.h"
42#include "UISearchLineEdit.h"
43
44#ifdef VBOX_WS_MAC
45# include "UIWindowMenuManager.h"
46#endif /* VBOX_WS_MAC */
47
48
49/*********************************************************************************************************************************
50* FilterByNameUUID definition/implementation. *
51*********************************************************************************************************************************/
52
53class FilterByNameUUID : public QITreeWidgetItemFilter
54{
55
56public:
57
58 FilterByNameUUID(UIMediumSearchWidget::SearchType enmSearchType, const QString &strSearchTerm)
59 : m_enmSearchType(enmSearchType)
60 , m_strSearchTerm(strSearchTerm){}
61 virtual ~FilterByNameUUID(){}
62 virtual bool operator()(QTreeWidgetItem *pItem) const RT_OVERRIDE RT_FINAL
63 {
64 if (!pItem || m_strSearchTerm.isEmpty())
65 return false;
66 if (pItem->type() != QITreeWidgetItem::ItemType)
67 return false;
68
69 UIMediumItem *pMediumItem = dynamic_cast<UIMediumItem*>(pItem);
70 if (!pMediumItem)
71 return false;
72 QString strValue;
73 if (m_enmSearchType == UIMediumSearchWidget::SearchByUUID)
74 strValue = pMediumItem->id().toString();
75 else if (m_enmSearchType == UIMediumSearchWidget::SearchByName)
76 strValue = pMediumItem->name();
77 else
78 return false;
79 QRegularExpression searchRegEx = QRegularExpression::fromWildcard(m_strSearchTerm, Qt::CaseInsensitive,
80 QRegularExpression::UnanchoredWildcardConversion);
81 if (strValue.contains(searchRegEx))
82 return true;
83 return false;
84 }
85
86private:
87
88 UIMediumSearchWidget::SearchType m_enmSearchType;
89 QString m_strSearchTerm;
90};
91
92
93/*********************************************************************************************************************************
94* UIMediumSearchWidget implementation . *
95*********************************************************************************************************************************/
96
97UIMediumSearchWidget::UIMediumSearchWidget(QWidget *pParent)
98 :QIWithRetranslateUI<QWidget>(pParent)
99 , m_pSearchComboxBox(0)
100 , m_pSearchTermLineEdit(0)
101 , m_pShowNextMatchButton(0)
102 , m_pShowPreviousMatchButton(0)
103 , m_pTreeWidget(0)
104 , m_iScrollToIndex(-1)
105{
106 prepareWidgets();
107}
108
109void UIMediumSearchWidget::prepareWidgets()
110{
111 QHBoxLayout *pLayout = new QHBoxLayout;
112 setLayout(pLayout);
113 pLayout->setContentsMargins(0, 0, 0, 0);
114 pLayout->setSpacing(0);
115
116 m_pSearchComboxBox = new QComboBox;
117 if (m_pSearchComboxBox)
118 {
119 m_pSearchComboxBox->setEditable(false);
120 m_pSearchComboxBox->insertItem(SearchByName, "Search By Name");
121 m_pSearchComboxBox->insertItem(SearchByUUID, "Search By UUID");
122 connect(m_pSearchComboxBox, &QComboBox::currentIndexChanged,
123 this, &UIMediumSearchWidget::sigPerformSearch);
124 pLayout->addWidget(m_pSearchComboxBox);
125 }
126
127 m_pSearchTermLineEdit = new UISearchLineEdit;
128 if (m_pSearchTermLineEdit)
129 {
130 m_pSearchTermLineEdit->setClearButtonEnabled(false);
131 pLayout->addWidget(m_pSearchTermLineEdit);
132 connect(m_pSearchTermLineEdit, &QLineEdit::textChanged,
133 this, &UIMediumSearchWidget::sigPerformSearch);
134 }
135
136 m_pShowPreviousMatchButton = new QIToolButton;
137 if (m_pShowPreviousMatchButton)
138 {
139 m_pShowPreviousMatchButton->setIcon(UIIconPool::iconSet(":/log_viewer_search_backward_16px.png", ":/log_viewer_search_backward_disabled_16px.png"));
140 connect(m_pShowPreviousMatchButton, &QIToolButton::clicked, this, &UIMediumSearchWidget::sltShowPreviousMatchingItem);
141 pLayout->addWidget(m_pShowPreviousMatchButton);
142 }
143 m_pShowNextMatchButton = new QIToolButton;
144 if (m_pShowNextMatchButton)
145 {
146 m_pShowNextMatchButton->setIcon(UIIconPool::iconSet(":/log_viewer_search_forward_16px.png", ":/log_viewer_search_forward_disabled_16px.png"));
147 connect(m_pShowNextMatchButton, &QIToolButton::clicked, this, &UIMediumSearchWidget:: sltShowNextMatchingItem);
148 pLayout->addWidget(m_pShowNextMatchButton);
149 }
150
151 retranslateUi();
152}
153
154UIMediumSearchWidget::SearchType UIMediumSearchWidget::searchType() const
155{
156 if (!m_pSearchComboxBox || m_pSearchComboxBox->currentIndex() >= static_cast<int>(SearchByMax))
157 return SearchByMax;
158 return static_cast<SearchType>(m_pSearchComboxBox->currentIndex());
159}
160
161QString UIMediumSearchWidget::searchTerm() const
162{
163 if (!m_pSearchTermLineEdit)
164 return QString();
165 return m_pSearchTermLineEdit->text();
166}
167
168void UIMediumSearchWidget::search(QITreeWidget* pTreeWidget, bool fGotoNext /* = true */)
169{
170 if (!pTreeWidget)
171 return;
172
173 m_pTreeWidget = pTreeWidget;
174 QList<QTreeWidgetItem*> allItems = pTreeWidget->filterItems(QITreeWidgetItemFilter());
175 markUnmarkItems(allItems, false);
176
177 m_matchedItemList = pTreeWidget->filterItems(FilterByNameUUID(searchType(), searchTerm()));
178 markUnmarkItems(m_matchedItemList, true);
179 if (!m_matchedItemList.isEmpty())
180 {
181 m_iScrollToIndex = -1;
182 if (fGotoNext)
183 goToNextPrevious(true);
184 }
185 else
186 m_iScrollToIndex = -1;
187 updateSearchLineEdit(m_matchedItemList.size(), m_iScrollToIndex);
188}
189
190void UIMediumSearchWidget::retranslateUi()
191{
192 if (m_pSearchComboxBox)
193 {
194 m_pSearchComboxBox->setItemText(SearchByName, tr("Search By Name"));
195 m_pSearchComboxBox->setItemText(SearchByUUID, tr("Search By UUID"));
196 m_pSearchComboxBox->setToolTip(tr("Select the search type"));
197 }
198 if (m_pSearchTermLineEdit)
199 m_pSearchTermLineEdit->setToolTip(tr("Enter the search term and press Enter/Return"));
200 if (m_pShowPreviousMatchButton)
201 m_pShowPreviousMatchButton->setToolTip(tr("Show the previous item matching the search term"));
202 if (m_pShowNextMatchButton)
203 m_pShowNextMatchButton->setToolTip(tr("Show the next item matching the search term"));
204}
205
206void UIMediumSearchWidget::showEvent(QShowEvent *pEvent)
207{
208 if (m_pSearchTermLineEdit)
209 m_pSearchTermLineEdit->setFocus();
210 QIWithRetranslateUI<QWidget>::showEvent(pEvent);
211}
212
213void UIMediumSearchWidget::markUnmarkItems(QList<QTreeWidgetItem*> &itemList, bool fMark)
214{
215 foreach (QTreeWidgetItem* pItem, itemList)
216 {
217 if (pItem->type() != QITreeWidgetItem::ItemType)
218 continue;
219 UIMediumItem *pMediumItem = static_cast<UIMediumItem*>(pItem);
220 if (!pMediumItem)
221 continue;
222 QFont font = pMediumItem->font(0);
223 font.setBold(fMark);
224 pMediumItem->setFont(0, font);
225
226 if (!fMark)
227 setUnderlineItemText(pMediumItem, false);
228 }
229}
230
231void UIMediumSearchWidget::setUnderlineItemText(QTreeWidgetItem* pItem, bool fUnderline)
232{
233 if (!pItem)
234 return;
235 QFont font = pItem->font(0);
236 font.setUnderline(fUnderline);
237 pItem->setFont(0, font);
238}
239
240void UIMediumSearchWidget::goToNextPrevious(bool fNext)
241{
242 if (!m_pTreeWidget || m_matchedItemList.isEmpty())
243 return;
244
245 if (m_iScrollToIndex >= 0 && m_iScrollToIndex < m_matchedItemList.size())
246 setUnderlineItemText(m_matchedItemList[m_iScrollToIndex], false);
247
248 if (fNext)
249 ++m_iScrollToIndex;
250 else
251 --m_iScrollToIndex;
252
253 if (m_iScrollToIndex >= m_matchedItemList.size())
254 m_iScrollToIndex = 0;
255 if (m_iScrollToIndex < 0)
256 m_iScrollToIndex = m_matchedItemList.size() - 1;
257
258 setUnderlineItemText(m_matchedItemList[m_iScrollToIndex], true);
259 m_pTreeWidget->scrollTo(m_pTreeWidget->itemIndex(m_matchedItemList[m_iScrollToIndex]), QAbstractItemView::PositionAtCenter);
260 updateSearchLineEdit(m_matchedItemList.size(), m_iScrollToIndex);
261}
262
263void UIMediumSearchWidget::sltShowNextMatchingItem()
264{
265 goToNextPrevious(true);
266}
267
268void UIMediumSearchWidget::sltShowPreviousMatchingItem()
269{
270 goToNextPrevious(false);
271}
272
273void UIMediumSearchWidget::updateSearchLineEdit(int iMatchCount, int iScrollToIndex)
274{
275 if (!m_pSearchTermLineEdit)
276 return;
277 m_pSearchTermLineEdit->setMatchCount(iMatchCount);
278 m_pSearchTermLineEdit->setScrollToIndex(iScrollToIndex);
279}
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