Marble

SearchInputWidget.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2012 Dennis Nienhüser <nienhueser@kde.org>
4// SPDX-FileCopyrightText: 2012 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
5//
6
7#include "SearchInputWidget.h"
8
9#include "GeoDataCoordinates.h"
10#include "MarblePlacemarkModel.h"
11
12#include <QCompleter>
13#include <QMenu>
14
15namespace Marble
16{
17
18SearchInputWidget::SearchInputWidget(QWidget *parent)
19 : MarbleLineEdit(parent)
20 , m_completer(new QCompleter(this))
21 , m_areaSearch(false)
22{
23 updatePlaceholderText();
24 QPixmap const decorator = QPixmap(QStringLiteral(":/icons/16x16/edit-find.png"));
25 Q_ASSERT(!decorator.isNull());
26 setDecorator(decorator);
27
28 connect(this, SIGNAL(clearButtonClicked()), this, SLOT(search()));
29 connect(this, SIGNAL(returnPressed()), this, SLOT(search()));
30 connect(this, &MarbleLineEdit::decoratorButtonClicked, this, &SearchInputWidget::showDropDownMenu);
31
32 m_sortFilter.setSortRole(MarblePlacemarkModel::PopularityIndexRole);
33 m_sortFilter.sort(0, Qt::AscendingOrder);
34 m_sortFilter.setDynamicSortFilter(true);
35
36 m_completer->setCompletionRole(Qt::DisplayRole);
37 m_completer->setCaseSensitivity(Qt::CaseInsensitive);
38 m_completer->setModel(&m_sortFilter);
39 setCompleter(m_completer);
40 connect(m_completer, SIGNAL(activated(QModelIndex)), this, SLOT(centerOnSearchSuggestion(QModelIndex)));
41}
42
43void SearchInputWidget::setCompletionModel(QAbstractItemModel *completionModel)
44{
45 m_sortFilter.setSourceModel(completionModel);
46}
47
48void SearchInputWidget::search()
49{
50 QString const searchTerm = text();
51 if (!searchTerm.isEmpty()) {
52 setBusy(true);
53 }
54 Q_EMIT search(searchTerm, m_areaSearch ? AreaSearch : GlobalSearch);
55}
56
57void SearchInputWidget::disableSearchAnimation()
58{
59 setBusy(false);
60}
61
62void SearchInputWidget::centerOnSearchSuggestion(const QModelIndex &index)
63{
64 QAbstractItemModel const *model = completer()->completionModel();
65 QVariant const value = model->data(index, MarblePlacemarkModel::CoordinateRole);
66 auto const coordinates = value.value<GeoDataCoordinates>();
67 Q_EMIT centerOn(coordinates);
68}
69
70void SearchInputWidget::showDropDownMenu()
71{
72 QMenu menu(this);
73 QAction *globalSearch = menu.addAction(tr("Global Search"), this, SLOT(setGlobalSearch()));
74 globalSearch->setCheckable(true);
75 globalSearch->setChecked(!m_areaSearch);
76 QAction *areaSearch = menu.addAction(tr("Area Search"), this, SLOT(setAreaSearch()));
77 areaSearch->setCheckable(true);
78 areaSearch->setChecked(m_areaSearch);
79 menu.exec(mapToGlobal(QPoint(0, size().height())));
80}
81
82void SearchInputWidget::setGlobalSearch()
83{
84 m_areaSearch = false;
85 updatePlaceholderText();
86}
87
88void SearchInputWidget::setAreaSearch()
89{
90 m_areaSearch = true;
91 updatePlaceholderText();
92}
93
94void SearchInputWidget::updatePlaceholderText()
95{
96 setPlaceholderText(m_areaSearch ? tr("Area Search") : tr("Global Search"));
97}
98
99}
100
101#include "moc_SearchInputWidget.cpp"
Binds a QML item to a specific geodetic location in screen coordinates.
virtual QVariant data(const QModelIndex &index, int role) const const=0
void setCheckable(bool)
void setChecked(bool)
bool isNull() const const
bool isEmpty() const const
CaseInsensitive
DisplayRole
AscendingOrder
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:15:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.