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
17SearchInputWidget::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
42void SearchInputWidget::setCompletionModel( QAbstractItemModel *completionModel )
43{
44 m_sortFilter.setSourceModel( completionModel );
45}
46
47void 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
56void SearchInputWidget::disableSearchAnimation()
57{
58 setBusy( false );
59}
60
61void 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
69void 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
81void SearchInputWidget::setGlobalSearch()
82{
83 m_areaSearch = false;
84 updatePlaceholderText();
85}
86
87void SearchInputWidget::setAreaSearch()
88{
89 m_areaSearch = true;
90 updatePlaceholderText();
91}
92
93void SearchInputWidget::updatePlaceholderText()
94{
95 setPlaceholderText( m_areaSearch ? tr( "Area Search" ) : tr ( "Global Search" ) );
96}
97
98}
99
100#include "moc_SearchInputWidget.cpp"
Binds a QML item to a specific geodetic location in screen coordinates.
@ GlobalSearch
Search a whole planet.
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 Fri Jun 21 2024 12:00:07 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.