File indexing completed on 2024-04-14 03:48:03
0001 // SPDX-License-Identifier: LGPL-2.1-or-later 0002 // 0003 // SPDX-FileCopyrightText: 2012 Dennis Nienhüser <nienhueser@kde.org> 0004 // SPDX-FileCopyrightText: 2012 Bernhard Beschow <bbeschow@cs.tu-berlin.de> 0005 // 0006 0007 #include "SearchInputWidget.h" 0008 0009 #include "GeoDataCoordinates.h" 0010 #include "MarblePlacemarkModel.h" 0011 0012 #include <QCompleter> 0013 #include <QMenu> 0014 0015 namespace Marble { 0016 0017 SearchInputWidget::SearchInputWidget( QWidget *parent ) : 0018 MarbleLineEdit( parent ), 0019 m_completer( new QCompleter( this ) ), 0020 m_areaSearch( false ) 0021 { 0022 updatePlaceholderText(); 0023 QPixmap const decorator = QPixmap(QStringLiteral(":/icons/16x16/edit-find.png")); 0024 Q_ASSERT( !decorator.isNull() ); 0025 setDecorator( decorator ); 0026 0027 connect( this, SIGNAL(clearButtonClicked()), this, SLOT(search()) ); 0028 connect( this, SIGNAL(returnPressed()), this, SLOT(search()) ); 0029 connect( this, SIGNAL(decoratorButtonClicked()), this, SLOT(showDropDownMenu()) ); 0030 0031 m_sortFilter.setSortRole( MarblePlacemarkModel::PopularityIndexRole ); 0032 m_sortFilter.sort( 0, Qt::AscendingOrder ); 0033 m_sortFilter.setDynamicSortFilter( true ); 0034 0035 m_completer->setCompletionRole( Qt::DisplayRole ); 0036 m_completer->setCaseSensitivity( Qt::CaseInsensitive ); 0037 m_completer->setModel( &m_sortFilter ); 0038 setCompleter( m_completer ); 0039 connect( m_completer, SIGNAL(activated(QModelIndex)), this, SLOT(centerOnSearchSuggestion(QModelIndex)) ); 0040 } 0041 0042 void SearchInputWidget::setCompletionModel( QAbstractItemModel *completionModel ) 0043 { 0044 m_sortFilter.setSourceModel( completionModel ); 0045 } 0046 0047 void SearchInputWidget::search() 0048 { 0049 QString const searchTerm = text(); 0050 if ( !searchTerm.isEmpty() ) { 0051 setBusy( true ); 0052 } 0053 emit search( searchTerm, m_areaSearch ? AreaSearch : GlobalSearch ); 0054 } 0055 0056 void SearchInputWidget::disableSearchAnimation() 0057 { 0058 setBusy( false ); 0059 } 0060 0061 void SearchInputWidget::centerOnSearchSuggestion(const QModelIndex &index ) 0062 { 0063 QAbstractItemModel const * model = completer()->completionModel(); 0064 QVariant const value = model->data( index, MarblePlacemarkModel::CoordinateRole ); 0065 GeoDataCoordinates const coordinates = value.value<GeoDataCoordinates>(); 0066 emit centerOn( coordinates ); 0067 } 0068 0069 void SearchInputWidget::showDropDownMenu() 0070 { 0071 QMenu menu( this ); 0072 QAction* globalSearch = menu.addAction( tr( "Global Search" ), this, SLOT(setGlobalSearch()) ); 0073 globalSearch->setCheckable( true ); 0074 globalSearch->setChecked( !m_areaSearch ); 0075 QAction* areaSearch = menu.addAction( tr( "Area Search" ), this, SLOT(setAreaSearch()) ); 0076 areaSearch->setCheckable( true ); 0077 areaSearch->setChecked( m_areaSearch ); 0078 menu.exec( mapToGlobal( QPoint( 0, size().height() ) ) ); 0079 } 0080 0081 void SearchInputWidget::setGlobalSearch() 0082 { 0083 m_areaSearch = false; 0084 updatePlaceholderText(); 0085 } 0086 0087 void SearchInputWidget::setAreaSearch() 0088 { 0089 m_areaSearch = true; 0090 updatePlaceholderText(); 0091 } 0092 0093 void SearchInputWidget::updatePlaceholderText() 0094 { 0095 setPlaceholderText( m_areaSearch ? tr( "Area Search" ) : tr ( "Global Search" ) ); 0096 } 0097 0098 } 0099 0100 #include "moc_SearchInputWidget.cpp"