13 #include "ui_GoToDialog.h"
27 #include <QAbstractListModel>
29 #include <QPushButton>
35 class TargetModel :
public QAbstractListModel
38 TargetModel( MarbleModel* marbleModel,
QObject * parent = 0 );
40 virtual int rowCount (
const QModelIndex & parent = QModelIndex() )
const;
42 virtual QVariant data (
const QModelIndex & index,
int role = Qt::DisplayRole )
const;
44 void setShowRoutingItems(
bool show );
47 QVariant currentLocationData (
int role )
const;
49 QVariant routeData (
const QVector<GeoDataPlacemark> &via,
int index,
int role )
const;
51 QVariant homeData (
int role )
const;
53 QVariant bookmarkData (
int index,
int role )
const;
55 QVector<GeoDataPlacemark> viaPoints()
const;
57 MarbleModel *
const m_marbleModel;
59 QVector<GeoDataPlacemark*> m_bookmarks;
61 bool m_hasCurrentLocation;
63 bool m_showRoutingItems;
66 class GoToDialogPrivate :
public Ui::GoTo
71 GeoDataCoordinates m_coordinates;
73 MarbleModel *
const m_marbleModel;
75 TargetModel m_targetModel;
77 SearchRunnerManager m_runnerManager;
79 GeoDataDocument *m_searchResult;
81 GeoDataTreeModel m_searchResultModel;
83 QTimer m_progressTimer;
87 QVector<QIcon> m_progressAnimation;
89 GoToDialogPrivate( GoToDialog* parent, MarbleModel* marbleModel );
91 void saveSelection(
const QModelIndex &index );
93 void createProgressAnimation();
97 void updateSearchResult( QVector<GeoDataPlacemark*> placemarks );
99 void updateSearchMode();
101 void updateProgress();
103 void stopProgressAnimation();
105 void updateResultMessage(
int results );
108 TargetModel::TargetModel( MarbleModel *marbleModel,
QObject * parent ) :
109 QAbstractListModel( parent ),
110 m_marbleModel( marbleModel ),
111 m_hasCurrentLocation( false ),
112 m_showRoutingItems( true )
114 BookmarkManager* manager = m_marbleModel->bookmarkManager();
115 foreach( GeoDataFolder * folder, manager->folders() ) {
116 QVector<GeoDataPlacemark*> bookmarks = folder->placemarkList();
117 QVector<GeoDataPlacemark*>::const_iterator iter = bookmarks.constBegin();
118 QVector<GeoDataPlacemark*>::const_iterator end = bookmarks.constEnd();
120 for ( ; iter != end; ++iter ) {
121 m_bookmarks.push_back( *iter );
125 PositionTracking*
tracking = m_marbleModel->positionTracking();
129 QVector<GeoDataPlacemark> TargetModel::viaPoints()
const
131 if ( !m_showRoutingItems ) {
132 return QVector<GeoDataPlacemark>();
135 RouteRequest* request = m_marbleModel->routingManager()->routeRequest();
136 QVector<GeoDataPlacemark> result;
137 for (
int i = 0; i < request->size(); ++i ) {
138 if ( request->at( i ).longitude() != 0.0 || request->at( i ).latitude() != 0.0 ) {
139 GeoDataPlacemark placemark;
140 placemark.setCoordinate( request->at( i ) );
141 placemark.setName( request->name( i ) );
142 result.push_back( placemark );
148 int TargetModel::rowCount (
const QModelIndex & parent )
const
151 if ( !parent.isValid() ) {
152 result += m_hasCurrentLocation ? 1 : 0;
153 result += viaPoints().size();
155 result += m_bookmarks.size();
162 QVariant TargetModel::currentLocationData (
int role )
const
164 PositionTracking* tracking = m_marbleModel->positionTracking();
166 GeoDataCoordinates currentLocation = tracking->currentLocation();
168 case Qt::DisplayRole:
return tr(
"Current Location: %1" ).arg( currentLocation.toString() ) ;
169 case Qt::DecorationRole:
return QIcon(
":/icons/gps.png" );
171 return qVariantFromValue( currentLocation );
179 QVariant TargetModel::routeData (
const QVector<GeoDataPlacemark> &via,
int index,
int role )
const
181 RouteRequest* request = m_marbleModel->routingManager()->routeRequest();
183 case Qt::DisplayRole:
return via.at( index ).name();
184 case Qt::DecorationRole:
return QIcon( request->pixmap( index ) );
186 const GeoDataCoordinates coordinates = via.at( index ).coordinate();
187 return qVariantFromValue( coordinates );
194 QVariant TargetModel::homeData (
int role )
const
197 case Qt::DisplayRole:
return tr(
"Home" );
198 case Qt::DecorationRole:
return QIcon(
":/icons/go-home.png" );
200 qreal lon( 0.0 ), lat( 0.0 );
202 m_marbleModel->home( lon, lat, zoom );
204 return qVariantFromValue( coordinates );
211 QVariant TargetModel::bookmarkData (
int index,
int role )
const
214 case Qt::DisplayRole: {
215 GeoDataFolder* folder =
dynamic_cast<GeoDataFolder*
>( m_bookmarks[index]->parent() );
216 Q_ASSERT( folder &&
"Internal bookmark representation has changed. Please report this as a bug at http://bugs.kde.org." );
218 return QString(folder->name() + QLatin1String(
" / ") + m_bookmarks[index]->name());
221 case Qt::DecorationRole:
return QIcon(
":/icons/bookmarks.png" );
229 QVariant TargetModel::data (
const QModelIndex & index,
int role )
const
231 if ( index.isValid() && index.row() >= 0 && index.row() < rowCount() ) {
232 int row = index.row();
233 bool const isCurrentLocation = row == 0 && m_hasCurrentLocation;
234 int homeOffset = m_hasCurrentLocation ? 1 : 0;
235 QVector<GeoDataPlacemark> via = viaPoints();
236 bool const isRoute = row >= homeOffset && row < homeOffset + via.size();
238 if ( isCurrentLocation ) {
239 return currentLocationData( role );
240 }
else if ( isRoute ) {
241 int routeIndex = row - homeOffset;
242 Q_ASSERT( routeIndex >= 0 && routeIndex < via.size() );
243 return routeData( via, routeIndex, role );
245 int bookmarkIndex = row - homeOffset - via.size();
246 if ( bookmarkIndex == 0 ) {
247 return homeData( role );
250 Q_ASSERT( bookmarkIndex >= 0 && bookmarkIndex < m_bookmarks.size() );
251 return bookmarkData( bookmarkIndex, role );
259 void TargetModel::setShowRoutingItems(
bool show )
261 m_showRoutingItems = show;
266 void GoToDialogPrivate::createProgressAnimation()
269 int const iconSize = smallScreen ? 32 : 16;
272 qreal
const h = iconSize / 2.0;
273 qreal
const q = h / 2.0;
275 qreal
const r = d / 2.0;
278 QImage canvas( iconSize, iconSize, QImage::Format_ARGB32 );
280 painter.setRenderHint( QPainter::Antialiasing,
true );
281 painter.setPen( QColor ( Qt::gray ) );
282 painter.setBrush( QColor( Qt::white ) );
285 for(
double t = 0.0; t < 2 *
M_PI; t += M_PI / 8.0 ) {
286 canvas.fill( Qt::transparent );
287 QRectF firstCircle( h - r + q * cos( t ), h - r + q * sin( t ), d, d );
288 QRectF secondCircle( h - r + q * cos( t + M_PI ), h - r + q * sin( t + M_PI ), d, d );
289 painter.drawEllipse( firstCircle );
290 painter.drawEllipse( secondCircle );
291 m_progressAnimation.push_back( QIcon( QPixmap::fromImage( canvas ) ) );
295 GoToDialogPrivate::GoToDialogPrivate( GoToDialog* parent, MarbleModel* marbleModel ) :
297 m_marbleModel( marbleModel ),
298 m_targetModel( marbleModel ),
299 m_runnerManager( marbleModel ),
300 m_searchResult( new GeoDataDocument ),
305 m_progressTimer.setInterval( 100 );
308 void GoToDialogPrivate::saveSelection(
const QModelIndex &index )
310 if ( searchButton->isChecked() && m_searchResult->size() ) {
312 m_coordinates = coordinates.value<GeoDataCoordinates>();
315 m_coordinates = coordinates.value<GeoDataCoordinates>();
320 void GoToDialogPrivate::startSearch()
322 QString
const searchTerm = searchLineEdit->text().trimmed();
323 if ( searchTerm.isEmpty() ) {
327 m_runnerManager.findPlacemarks( searchTerm );
328 if ( m_progressAnimation.isEmpty() ) {
329 createProgressAnimation();
331 m_progressTimer.start();
332 progressButton->setVisible(
true );
333 searchLineEdit->setEnabled(
false );
334 updateResultMessage( 0 );
337 void GoToDialogPrivate::updateSearchResult( QVector<GeoDataPlacemark*> placemarks )
339 m_searchResultModel.setRootDocument( 0 );
340 m_searchResult->clear();
341 foreach (GeoDataPlacemark *placemark, placemarks) {
342 m_searchResult->append(
new GeoDataPlacemark( *placemark ) );
344 m_searchResultModel.setRootDocument( m_searchResult );
345 bookmarkListView->setModel( &m_searchResultModel );
346 updateResultMessage( m_searchResultModel.rowCount() );
351 d( new GoToDialogPrivate( this, marbleModel ) )
354 setAttribute( Qt::WA_Maemo5StackedWindow );
355 setWindowFlags( Qt::Window );
356 #endif // Q_WS_MAEMO_5
358 #if QT_VERSION >= 0x40700
359 d->searchLineEdit->setPlaceholderText( tr(
"Address or search term" ) );
362 d->m_searchResultModel.setRootDocument( d->m_searchResult );
363 d->bookmarkListView->setModel( &d->m_targetModel );
364 connect( d->bookmarkListView, SIGNAL(activated(QModelIndex)),
365 this, SLOT(saveSelection(QModelIndex)) );
366 connect( d->searchLineEdit, SIGNAL(returnPressed()),
367 this, SLOT(startSearch()) );
368 d->buttonBox->button( QDialogButtonBox::Close )->setAutoDefault(
false );
369 connect( d->searchButton, SIGNAL(clicked(
bool)),
370 this, SLOT(updateSearchMode()) );
371 connect( d->browseButton, SIGNAL(clicked(
bool)),
372 this, SLOT(updateSearchMode()) );
373 connect( &d->m_progressTimer, SIGNAL(timeout()),
374 this, SLOT(updateProgress()) );
375 connect( d->progressButton, SIGNAL(clicked(
bool)),
376 this, SLOT(stopProgressAnimation()) );
377 d->updateSearchMode();
378 d->progressButton->setVisible(
false );
380 connect( &d->m_runnerManager, SIGNAL(searchResultChanged(QVector<GeoDataPlacemark*>)),
381 this, SLOT(updateSearchResult(QVector<GeoDataPlacemark*>)) );
382 connect( &d->m_runnerManager, SIGNAL(searchFinished(QString)),
383 this, SLOT(stopProgressAnimation()) );
393 return d->m_coordinates;
398 d->m_targetModel.setShowRoutingItems( show );
403 d->browseButton->setVisible( enabled );
404 d->searchButton->setVisible( enabled );
406 d->searchButton->setChecked(
false );
407 d->updateSearchMode();
411 void GoToDialogPrivate::updateSearchMode()
413 bool const searchEnabled = searchButton->isChecked();
414 searchLineEdit->setVisible( searchEnabled );
415 descriptionLabel->setVisible( searchEnabled );
416 progressButton->setVisible( searchEnabled && m_progressTimer.isActive() );
417 if ( searchEnabled ) {
418 bookmarkListView->setModel( &m_searchResultModel );
419 searchLineEdit->setFocus();
421 bookmarkListView->setModel( &m_targetModel );
425 void GoToDialogPrivate::updateProgress()
427 if ( !m_progressAnimation.isEmpty() ) {
428 m_currentFrame = ( m_currentFrame + 1 ) % m_progressAnimation.size();
429 QIcon frame = m_progressAnimation[m_currentFrame];
430 progressButton->setIcon( frame );
434 void GoToDialogPrivate::stopProgressAnimation()
436 searchLineEdit->setEnabled(
true );
437 m_progressTimer.stop();
438 updateResultMessage( bookmarkListView->model()->rowCount() );
439 progressButton->setVisible(
false );
442 void GoToDialogPrivate::updateResultMessage(
int results )
444 descriptionLabel->setText( QObject::tr(
"%n results found.",
"Number of search results", results ) );
449 #include "GoToDialog.moc"
A 3d point representation.
This file contains the headers for MarbleModel.
void setSearchEnabled(bool enabled)
Toggle whether the dialog can be used to search for placemarks.
void setShowRoutingItems(bool show)
Toggle whether routing items (source, destination and via points) are visible.
GoToDialog(MarbleModel *marbleModel, QWidget *parent=0, Qt::WindowFlags f=0)
GeoDataCoordinates coordinates() const
Returns the position of the item selected by the user, or a default constructed GeoDataLookAt if the ...
static MarbleGlobal * getInstance()
The GeoDataCoordinates coordinate.
The data model (not based on QAbstractModel) for a MarbleWidget.
Profiles profiles() const
const QSize iconSize(16, 16)