• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.12
  • kdeedu
  • marble
  • src
  • plasmarunner
plasmarunner.cpp
Go to the documentation of this file.
1 // Copyright 2011 Friedrich W. H. Kossebau <kossebau@kde.org>
2 //
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this program. If not, see <http://www.gnu.org/licenses/>.
15 
16 #include "plasmarunner.h"
17 
18 // Marble
19 #include <GeoDataCoordinates.h>
20 #include <GeoDataFolder.h>
21 #include <GeoDataPlacemark.h>
22 #include <BookmarkManager.h>
23 #include <GeoDataTreeModel.h>
24 
25 // KDE
26 #include <KProcess>
27 #include <KIcon>
28 #include <KLocale>
29 #include <KGlobal>
30 
31 
32 namespace Marble
33 {
34 
35 static const int minContainsMatchLength = 3;
36 
37 
38 PlasmaRunner::PlasmaRunner(QObject *parent, const QVariantList &args)
39  : AbstractRunner(parent, args)
40 {
41  KLocale* locale = KGlobal::locale();
42  locale->insertCatalog(QLatin1String("marble"));
43  locale->insertCatalog(QLatin1String("marble_qt"));
44  // load catalog manually, as it does not (yet) match the name of the plugin lib
45  // TODO: fix catalog name after branching of 1.4
46  locale->insertCatalog(QLatin1String("plasma_runner_marblerunner"));
47 
48  setIgnoredTypes(Plasma::RunnerContext::NetworkLocation |
49  Plasma::RunnerContext::FileSystem |
50  Plasma::RunnerContext::Help);
51 
52  QList<Plasma::RunnerSyntax> syntaxes;
53  syntaxes << Plasma::RunnerSyntax(QLatin1String(":q:"),
54  i18n("Shows the coordinates :q: in OpenStreetMap with Marble."));
55  syntaxes << Plasma::RunnerSyntax(QLatin1String(":q:"),
56  i18n("Shows the geo bookmark containing :q: in OpenStreetMap with Marble."));
57  setSyntaxes(syntaxes);
58 }
59 
60 void PlasmaRunner::match(Plasma::RunnerContext &context)
61 {
62  QList<Plasma::QueryMatch> matches;
63 
64  const QString query = context.query();
65 
66  bool success = false;
67  // TODO: how to estimate that input is in Degree, not Radian?
68  GeoDataCoordinates coordinates = GeoDataCoordinates::fromString(query, success);
69 
70  if (success) {
71  const QVariant coordinatesData = QVariantList()
72  << QVariant(coordinates.longitude(GeoDataCoordinates::Degree))
73  << QVariant(coordinates.latitude(GeoDataCoordinates::Degree))
74  << QVariant(0.1); // TODO: make this distance value configurable
75 
76  Plasma::QueryMatch match(this);
77  match.setIcon(KIcon(QLatin1String("marble")));
78  match.setText(i18n("Show the coordinates %1 in OpenStreetMap with Marble", query));
79  match.setData(coordinatesData);
80  match.setId(query);
81  match.setRelevance(1.0);
82  match.setType(Plasma::QueryMatch::ExactMatch);
83 
84  matches << match;
85  }
86 
87  // TODO: BookmarkManager does not yet listen to updates, also does not sync between processes :(
88  // So for now always load on demand, even if expensive possibly
89  BookmarkManager bookmarkManager(new GeoDataTreeModel);
90  bookmarkManager.loadFile( QLatin1String("bookmarks/bookmarks.kml") );
91 
92  foreach (GeoDataFolder* folder, bookmarkManager.folders()) {
93  collectMatches(matches, query, folder);
94  }
95 
96  if ( ! matches.isEmpty() ) {
97  context.addMatches(query, matches);
98  }
99 }
100 
101 void PlasmaRunner::collectMatches(QList<Plasma::QueryMatch> &matches,
102  const QString &query, const GeoDataFolder *folder)
103 {
104  const QString queryLower = query.toLower();
105 
106  QVector<GeoDataFeature*>::const_iterator it = folder->constBegin();
107  QVector<GeoDataFeature*>::const_iterator end = folder->constEnd();
108 
109  for (; it != end; ++it) {
110  GeoDataFolder *folder = dynamic_cast<GeoDataFolder*>(*it);
111  if ( folder ) {
112  collectMatches(matches, query, folder);
113  continue;
114  }
115 
116  GeoDataPlacemark *placemark = dynamic_cast<GeoDataPlacemark*>( *it );
117  if ( placemark ) {
118  // For short query strings only match exactly, to get a sane number of matches
119  if (query.length() < minContainsMatchLength) {
120  if ( placemark->name().toLower() != queryLower &&
121  ( placemark->descriptionIsCDATA() || // TODO: support also with CDATA
122  placemark->description().toLower() != queryLower ) ) {
123  continue;
124  }
125  } else {
126  if ( ! placemark->name().toLower().contains(queryLower) &&
127  ( placemark->descriptionIsCDATA() || // TODO: support also with CDATA
128  ! placemark->description().toLower().contains(queryLower) ) ) {
129  continue;
130  }
131  }
132 
133  const GeoDataCoordinates coordinates = placemark->coordinate();
134  const qreal lon = coordinates.longitude(GeoDataCoordinates::Degree);
135  const qreal lat = coordinates.latitude(GeoDataCoordinates::Degree);
136  const QVariant coordinatesData = QVariantList()
137  << QVariant(lon)
138  << QVariant(lat)
139  << QVariant(placemark->lookAt()->range()*METER2KM);
140 
141  Plasma::QueryMatch match(this);
142  match.setIcon(KIcon(QLatin1String("marble")));
143  match.setText(placemark->name());
144  match.setSubtext(i18n("Show in OpenStreetMap with Marble"));
145  match.setData(coordinatesData);
146  match.setId(placemark->name()+QString::number(lat)+QString::number(lon));
147  match.setRelevance(1.0);
148  match.setType(Plasma::QueryMatch::ExactMatch);
149 
150  matches << match;
151  }
152  }
153 }
154 
155 void PlasmaRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
156 {
157  Q_UNUSED(context)
158 
159  const QVariantList data = match.data().toList();
160 
161  const QString latLon =
162  QString::fromUtf8("%L1").arg(data.at(1).toReal()) +
163  QString::fromUtf8(" %L1").arg(data.at(0).toReal());
164 
165  const QString distance = data.at(2).toString();
166 
167  const QStringList parameters = QStringList()
168  << QLatin1String( "--latlon" )
169  << latLon
170  << QLatin1String( "--distance" )
171  << distance
172  << QLatin1String( "--map" )
173  << QLatin1String( "earth/openstreetmap/openstreetmap.dgml" );
174  KProcess::startDetached( QLatin1String("marble"), parameters );
175 }
176 
177 }
GeoDataCoordinates.h
Marble::GeoDataCoordinates
A 3d point representation.
Definition: GeoDataCoordinates.h:52
Marble::BookmarkManager::loadFile
bool loadFile(const QString &relativeFilePath)
load bookmark file as GeoDataDocument and return true if loaded successfully else false ...
Definition: BookmarkManager.cpp:87
Marble::GeoDataTreeModel
The representation of GeoData in a model This class represents all available data given by kml-data f...
Definition: GeoDataTreeModel.h:32
Marble::PlasmaRunner::PlasmaRunner
PlasmaRunner(QObject *parent, const QVariantList &args)
Definition: plasmarunner.cpp:38
plasmarunner.h
Marble::GeoDataContainer::constBegin
QVector< GeoDataFeature * >::ConstIterator constBegin() const
Definition: GeoDataContainer.cpp:234
Marble::PlasmaRunner::match
void match(Plasma::RunnerContext &context)
Definition: plasmarunner.cpp:60
Marble::GeoDataCoordinates::latitude
qreal latitude(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
retrieves the latitude of the GeoDataCoordinates object use the unit parameter to switch between Radi...
Definition: GeoDataCoordinates.cpp:751
QObject
Marble::GeoDataCoordinates::Degree
Definition: GeoDataCoordinates.h:66
BookmarkManager.h
Marble::BookmarkManager
This class is responsible for loading the book mark objects from the files and various book mark oper...
Definition: BookmarkManager.h:35
Marble::GeoDataContainer::constEnd
QVector< GeoDataFeature * >::ConstIterator constEnd() const
Definition: GeoDataContainer.cpp:239
Marble::BookmarkManager::folders
QVector< GeoDataFolder * > folders() const
return Vector of folders
Definition: BookmarkManager.cpp:165
Marble::GeoDataFolder
Definition: GeoDataFolder.h:50
GeoDataPlacemark.h
GeoDataTreeModel.h
Marble::METER2KM
const qreal METER2KM
Definition: MarbleGlobal.h:205
Marble::GeoDataCoordinates::longitude
qreal longitude(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
retrieves the longitude of the GeoDataCoordinates object use the unit parameter to switch between Rad...
Definition: GeoDataCoordinates.cpp:739
GeoDataFolder.h
Marble::GeoDataCoordinates::fromString
static GeoDataCoordinates fromString(const QString &string, bool &successful)
try to parse the string into a coordinate pair
Definition: GeoDataCoordinates.cpp:909
Marble::PlasmaRunner::run
void run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
Definition: plasmarunner.cpp:155
Marble::minContainsMatchLength
static const int minContainsMatchLength
Definition: plasmarunner.cpp:35
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:52 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal