Marble

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

KDE's Doxygen guidelines are available online.