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

marble

  • kde-4.x
  • kdeedu
  • marble
  • src
  • lib
  • marble
BookmarkManager.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2010 Gaurav Gupta <[email protected]>
9 // Copyright 2012 Thibaut Gridel <[email protected]>
10 //
11 
12 #include "BookmarkManager.h"
13 #include "BookmarkManager_p.h"
14 #include "GeoDataParser.h"
15 #include "GeoDataContainer.h"
16 #include "GeoDataDocument.h"
17 #include "GeoDataFolder.h"
18 #include "GeoDataPlacemark.h"
19 #include "GeoDataTreeModel.h"
20 #include "GeoDataDocumentWriter.h"
21 #include "GeoDataIconStyle.h"
22 #include "KmlElementDictionary.h"
23 #include "MarbleDebug.h"
24 #include "MarbleDirs.h"
25 #include "StyleBuilder.h"
26 #include <QFile>
27 
28 namespace Marble
29 {
30 
31 BookmarkManagerPrivate::BookmarkManagerPrivate( GeoDataTreeModel *treeModel ) :
32  m_treeModel( treeModel ),
33  m_bookmarkDocument( nullptr ),
34  m_bookmarkFileRelativePath( "bookmarks/bookmarks.kml" ),
35  m_styleBuilder(nullptr)
36 {
37  resetBookmarkDocument();
38 }
39 
40 BookmarkManagerPrivate::~BookmarkManagerPrivate()
41 {
42  Q_ASSERT( m_bookmarkDocument && "BookmarkManagerPrivate::m_bookmarkDocument is 0. Please report a Marble bug at https://bugs.kde.org" );
43  if ( m_bookmarkDocument )
44  {
45  m_treeModel->removeDocument( m_bookmarkDocument );
46  }
47  delete m_bookmarkDocument;
48 }
49 
50 void BookmarkManagerPrivate::resetBookmarkDocument()
51 {
52  if ( m_bookmarkDocument ) {
53  m_treeModel->removeDocument( m_bookmarkDocument );
54  delete m_bookmarkDocument;
55  }
56 
57  GeoDataFolder* folder = new GeoDataFolder;
58  folder->setName( QObject::tr( "Default" ) );
59 
60  m_bookmarkDocument = new GeoDataDocument;
61  m_bookmarkDocument->setDocumentRole( BookmarkDocument );
62  m_bookmarkDocument->setName( QObject::tr("Bookmarks") );
63  m_bookmarkDocument->append( folder );
64  m_treeModel->addDocument( m_bookmarkDocument );
65 }
66 
67 void BookmarkManagerPrivate::setVisualCategory( GeoDataContainer *container ) {
68  for( GeoDataFolder* folder: container->folderList() ) {
69  setVisualCategory( folder );
70  }
71  for( GeoDataPlacemark* placemark: container->placemarkList() ) {
72  placemark->setVisualCategory(GeoDataPlacemark::Bookmark);
73  placemark->setZoomLevel( 1 );
74  }
75 
76 }
77 
78 BookmarkManager::BookmarkManager( GeoDataTreeModel *treeModel, QObject *parent ) :
79  QObject( parent ),
80  d( new BookmarkManagerPrivate( treeModel ) )
81 {
82 }
83 
84 BookmarkManager::~BookmarkManager()
85 {
86  delete d;
87 }
88 
89 QString BookmarkManager::bookmarkFile() const
90 {
91  return MarbleDirs::path( d->m_bookmarkFileRelativePath );
92 }
93 
94 bool BookmarkManager::loadFile( const QString &relativeFilePath )
95 {
96  d->m_bookmarkFileRelativePath = relativeFilePath;
97  QString absoluteFilePath = bookmarkFile();
98 
99  mDebug() << Q_FUNC_INFO << "Loading Bookmark File:" << absoluteFilePath;
100 
101  if (absoluteFilePath.isEmpty())
102  return false;
103 
104  if ( relativeFilePath.isNull() )
105  return false;
106 
107  GeoDataDocument *document = openFile( absoluteFilePath );
108  bool recover = false;
109  if ( !document ) {
110  mDebug() << "Could not parse file" << absoluteFilePath;
111  mDebug() << "This could be caused by a previous broken bookmark file. Trying to recover.";
113  recover = true;
114  // return false;
115  }
116 
117  d->m_treeModel->removeDocument( d->m_bookmarkDocument );
118  delete d->m_bookmarkDocument;
119  d->m_bookmarkDocument = document;
120 
121  if ( recover ) {
122  d->resetBookmarkDocument();
123  updateBookmarkFile();
124  } else {
125  Q_ASSERT( d->m_bookmarkDocument && "d->m_bookmarkDocument is 0 but must not be. Please report a bug at https://bugs.kde.org" );
126  d->m_treeModel->addDocument( d->m_bookmarkDocument );
127  }
128  ensureDefaultFolder();
129 
130  emit bookmarksChanged();
131  return true;
132 }
133 
134 void BookmarkManager::addBookmark( GeoDataContainer *container, const GeoDataPlacemark &placemark )
135 {
136  GeoDataPlacemark *bookmark = new GeoDataPlacemark( placemark );
137  bookmark->setVisualCategory(GeoDataPlacemark::Bookmark);
138  bookmark->setZoomLevel( 1 );
139  if (bookmark->name().isEmpty()) {
140  bookmark->setName(bookmark->coordinate().toString(GeoDataCoordinates::Decimal).trimmed());
141  }
142  if (d->m_styleBuilder && bookmark->style()->iconStyle().iconPath().isEmpty()) {
143  StyleParameters style;
144  style.placemark = bookmark;
145  bookmark->setStyle(GeoDataStyle::Ptr(new GeoDataStyle(*d->m_styleBuilder->createStyle(style))));
146  }
147  d->m_treeModel->addFeature( container, bookmark );
148 
149  updateBookmarkFile();
150 }
151 
152 void BookmarkManager::updateBookmark( GeoDataPlacemark *bookmark )
153 {
154  d->m_treeModel->updateFeature( bookmark );
155 }
156 
157 void BookmarkManager::removeBookmark( GeoDataPlacemark *bookmark )
158 {
159  d->m_treeModel->removeFeature( bookmark );
160  delete bookmark;
161  updateBookmarkFile();
162 }
163 
164 GeoDataPlacemark* BookmarkManager::bookmarkAt(GeoDataContainer *container, const GeoDataCoordinates &coordinate)
165 {
166  for ( GeoDataFolder *folder: container->folderList() ) {
167  GeoDataPlacemark *placemark = bookmarkAt(folder, coordinate);
168  if ( placemark )
169  return placemark;
170  }
171 
172  for ( GeoDataPlacemark *placemark: container->placemarkList() ) {
173  if ( placemark->coordinate() == coordinate )
174  return placemark;
175  }
176 
177  return Q_NULLPTR;
178 }
179 
180 GeoDataDocument * BookmarkManager::document()
181 {
182  return d->m_bookmarkDocument;
183 }
184 
185 const GeoDataDocument * BookmarkManager::document() const
186 {
187  return d->m_bookmarkDocument;
188 }
189 
190 bool BookmarkManager::showBookmarks() const
191 {
192  return d->m_bookmarkDocument->isVisible();
193 }
194 
195 void BookmarkManager::setShowBookmarks( bool visible )
196 {
197  d->m_bookmarkDocument->setVisible( visible );
198  d->m_treeModel->updateFeature( d->m_bookmarkDocument );
199 }
200 
201 QVector<GeoDataFolder*> BookmarkManager::folders() const
202 {
203  return d->m_bookmarkDocument->folderList();
204 }
205 
206 GeoDataFolder* BookmarkManager::addNewBookmarkFolder( GeoDataContainer *container, const QString &name )
207 {
208  //If name is empty string
209  if ( name.isEmpty() ) {
210  mDebug() << "Folder with empty name is not acceptable, please give it another name" ;
211  return Q_NULLPTR;
212  }
213 
214  //If folder with same name already exist
215  QVector<GeoDataFolder*> folderList = container->folderList();
216 
217  QVector<GeoDataFolder*>::const_iterator i = folderList.constBegin();
218  QVector<GeoDataFolder*>::const_iterator end = folderList.constEnd();
219  for ( ; i != end; ++i ) {
220  if ( name == ( *i )->name() ) {
221  mDebug() << "Folder with same name already exist, please give it another name";
222  return *i;
223  }
224  }
225 
226  GeoDataFolder *bookmarkFolder = new GeoDataFolder();
227  bookmarkFolder->setName( name );
228 
229  d->m_treeModel->addFeature( container, bookmarkFolder );
230  updateBookmarkFile();
231 
232  return bookmarkFolder;
233 }
234 
235 void BookmarkManager::renameBookmarkFolder( GeoDataFolder *folder, const QString &name )
236 {
237  folder->setName( name );
238  d->m_treeModel->updateFeature( folder );
239 }
240 
241 void BookmarkManager::removeBookmarkFolder( GeoDataFolder *folder )
242 {
243  d->m_treeModel->removeFeature( folder );
244  delete folder;
245 }
246 
247 void BookmarkManager::ensureDefaultFolder()
248 {
249  if ( d->m_bookmarkDocument->size() == 0 ) {
250  addNewBookmarkFolder( d->m_bookmarkDocument, tr("Default") );
251  }
252 }
253 
254 void BookmarkManager::removeAllBookmarks()
255 {
256  d->resetBookmarkDocument();
257  updateBookmarkFile();
258 }
259 
260 void BookmarkManager::setStyleBuilder(const StyleBuilder *styleBuilder)
261 {
262  d->m_styleBuilder = styleBuilder;
263 }
264 
265 bool BookmarkManager::updateBookmarkFile()
266 {
267  const QString absoluteLocalFilePath = MarbleDirs::localPath() + QLatin1Char('/') + d->m_bookmarkFileRelativePath;
268 
269  if ( ! d->m_bookmarkFileRelativePath.isNull() ) {
270  QFile file( absoluteLocalFilePath );
271  if ( !file.exists() ) {
272  // Extracting directory of file : for bookmarks it will be MarbleDirs::localPath()+/bookmarks/
273  QFileInfo fileInfo( absoluteLocalFilePath );
274  QString directoryPath = fileInfo.path();
275 
276  //Creating all directories, which doesn't exist
277  QDir directory( MarbleDirs::localPath() );
278  directory.mkpath( directoryPath );
279  }
280 
281  if (!GeoDataDocumentWriter::write(absoluteLocalFilePath, *d->m_bookmarkDocument)) {
282  mDebug() << "Could not write the bookmarks file" << absoluteLocalFilePath;
283  file.close();
284  return false;
285  }
286  emit bookmarksChanged();
287  file.close();
288  return true;
289  }
290  return false;
291 }
292 
293 GeoDataDocument* BookmarkManager::openFile( const QString &fileName )
294 {
295  GeoDataParser parser( GeoData_KML );
296  QFile file( fileName );
297 
298  if ( !file.exists() ) {
299  return nullptr;
300  }
301 
302  if ( !file.open( QIODevice::ReadOnly ) || !parser.read( &file ) ) {
303  mDebug() << "Could not open/parse file" << fileName;
304  return nullptr;
305  }
306 
307  GeoDataDocument *result = dynamic_cast<GeoDataDocument*>( parser.releaseDocument() );
308  if ( !result ) {
309  return nullptr;
310  }
311 
312  result->setDocumentRole( BookmarkDocument );
313  for( GeoDataFolder* folder: result->folderList() ) {
314  BookmarkManagerPrivate::setVisualCategory( folder );
315  }
316 
317  return result;
318 }
319 
320 }
321 
322 #include "moc_BookmarkManager.cpp"
GeoDataDocumentWriter.h
GeoDataDocument.h
Marble::BookmarkManagerPrivate::m_bookmarkFileRelativePath
QString m_bookmarkFileRelativePath
Definition: BookmarkManager_p.h:39
Marble::GeoDataCoordinates
A 3d point representation.
Definition: GeoDataCoordinates.h:45
Marble::GeoDataTreeModel::addFeature
int addFeature(GeoDataContainer *parent, GeoDataFeature *feature, int row=-1)
Definition: GeoDataTreeModel.cpp:706
Marble::GeoDataDocument
A container for Features, Styles and in the future Schemas.
Definition: GeoDataDocument.h:63
Marble::BookmarkManager::renameBookmarkFolder
void renameBookmarkFolder(GeoDataFolder *folder, const QString &name)
Definition: BookmarkManager.cpp:235
Marble::BookmarkManager::loadFile
bool loadFile(const QString &relativeFilePath)
load bookmark file as GeoDataDocument and return true if loaded successfully else false ...
Definition: BookmarkManager.cpp:94
Marble::GeoDataTreeModel
The representation of GeoData in a model This class represents all available data given by kml-data f...
Definition: GeoDataTreeModel.h:33
Marble::BookmarkManager::addBookmark
void addBookmark(GeoDataContainer *folder, const GeoDataPlacemark &bookmark)
add bookmark in a folder
Definition: BookmarkManager.cpp:134
Marble::GeoDataDocument::setDocumentRole
void setDocumentRole(DocumentRole role)
Definition: GeoDataDocument.cpp:119
Marble::MarbleDirs::path
static QString path(const QString &relativePath)
Definition: MarbleDirs.cpp:55
GeoDataIconStyle.h
Marble::BookmarkManagerPrivate::m_styleBuilder
const StyleBuilder * m_styleBuilder
Definition: BookmarkManager_p.h:40
GeoDataContainer.h
Marble::BookmarkManager::addNewBookmarkFolder
GeoDataFolder * addNewBookmarkFolder(GeoDataContainer *container, const QString &name)
add a folder
Definition: BookmarkManager.cpp:206
Marble::BookmarkManager::updateBookmark
void updateBookmark(GeoDataPlacemark *bookmark)
Definition: BookmarkManager.cpp:152
QVector::constEnd
const_iterator constEnd() const
Marble::GeoDataTreeModel::removeFeature
bool removeFeature(GeoDataContainer *parent, int index)
Definition: GeoDataTreeModel.cpp:740
Marble::GeoDataFeature::isVisible
bool isVisible() const
Return whether this feature is visible or not.
Definition: GeoDataFeature.cpp:355
Marble::StyleParameters
Definition: StyleBuilder.h:29
Marble::GeoDataContainer
A base class that can hold GeoDataFeatures.
Definition: GeoDataContainer.h:47
Marble::GeoDataCoordinates::Decimal
"Decimal" notation (base-10)
Definition: GeoDataCoordinates.h:73
Marble::BookmarkManagerPrivate::m_bookmarkDocument
GeoDataDocument * m_bookmarkDocument
Definition: BookmarkManager_p.h:37
Marble::GeoDataFeature::style
QSharedPointer< const GeoDataStyle > style() const
Return the style assigned to the placemark, or a default style if none has been set.
Definition: GeoDataFeature.cpp:420
GeoDataParser.h
Marble::MarbleDirs::localPath
static QString localPath()
Definition: MarbleDirs.cpp:238
Marble::GeoDataPlacemark::Bookmark
Definition: GeoDataPlacemark.h:151
Marble::StyleParameters::placemark
const GeoDataPlacemark * placemark
Definition: StyleBuilder.h:34
MarbleDebug.h
QObject::tr
QString tr(const char *sourceText, const char *disambiguation, int n)
Marble::BookmarkDocument
Definition: GeoDataDocument.h:43
Marble::GeoDataTreeModel::addDocument
int addDocument(GeoDataDocument *document)
Definition: GeoDataTreeModel.cpp:735
QFile
Marble::StyleBuilder::createStyle
GeoDataStyle::ConstPtr createStyle(const StyleParameters &parameters) const
Definition: StyleBuilder.cpp:2065
BookmarkManager.h
Marble::BookmarkManager::bookmarksChanged
void bookmarksChanged()
One or more bookmarks were added or removed.
QString::isNull
bool isNull() const
Marble::BookmarkManager::setShowBookmarks
void setShowBookmarks(bool visible)
Definition: BookmarkManager.cpp:195
Marble::GeoData_KML
Definition: GeoDataParser.h:36
BookmarkManager_p.h
Marble::BookmarkManagerPrivate::~BookmarkManagerPrivate
~BookmarkManagerPrivate()
Definition: BookmarkManager.cpp:40
Marble::BookmarkManager::BookmarkManager
BookmarkManager(GeoDataTreeModel *treeModel, QObject *parent=nullptr)
Definition: BookmarkManager.cpp:78
Marble::BookmarkManagerPrivate::setVisualCategory
static void setVisualCategory(GeoDataContainer *container)
Definition: BookmarkManager.cpp:67
Marble::BookmarkManager::removeBookmark
void removeBookmark(GeoDataPlacemark *bookmark)
Definition: BookmarkManager.cpp:157
Marble::GeoDataStyle
an addressable style group
Definition: GeoDataStyle.h:55
Marble::GeoDataDocumentWriter::write
static bool write(QIODevice *device, const GeoDataDocument &document, const QString &documentIdentifier)
Write the content of the given GeoDataDocument to the given I/O device.
Definition: GeoDataDocumentWriter.cpp:26
Marble::BookmarkManager::bookmarkAt
GeoDataPlacemark * bookmarkAt(GeoDataContainer *container, const GeoDataCoordinates &coordinate)
checks all the bookmarks in container recursively and returns pointer to the one having the same coor...
Definition: BookmarkManager.cpp:164
Marble::GeoDataFeature::setName
void setName(const QString &value)
Set a new name for this feature.
Definition: GeoDataFeature.cpp:196
QSharedPointer
Marble::BookmarkManager::removeAllBookmarks
void removeAllBookmarks()
remove all folders and bookmarks except default folder
Definition: BookmarkManager.cpp:254
StyleBuilder.h
KmlElementDictionary.h
Marble::GeoDataTreeModel::removeDocument
void removeDocument(int index)
Definition: GeoDataTreeModel.cpp:789
QObject
Marble::BookmarkManager::document
GeoDataDocument * document()
Definition: BookmarkManager.cpp:180
Marble::BookmarkManager::folders
QVector< GeoDataFolder * > folders() const
return Vector of folders
Definition: BookmarkManager.cpp:201
MarbleDirs.h
Marble::GeoDataContainer::folderList
QVector< GeoDataFolder * > folderList() const
A convenience function that returns all folders in this container.
Definition: GeoDataContainer.cpp:133
QString::isEmpty
bool isEmpty() const
QString::trimmed
QString trimmed() const
Marble::StyleBuilder
Definition: StyleBuilder.h:39
Marble::BookmarkManagerPrivate::m_treeModel
GeoDataTreeModel *const m_treeModel
Definition: BookmarkManager_p.h:35
Marble::GeoDataContainer::size
int size() const
size of the container
Definition: GeoDataContainer.cpp:292
QString
Marble::GeoDataTreeModel::updateFeature
void updateFeature(GeoDataFeature *feature)
Definition: GeoDataTreeModel.cpp:781
Marble::GeoDataFolder
A container that is used to arrange other GeoDataFeatures.
Definition: GeoDataFolder.h:49
GeoDataPlacemark.h
GeoDataTreeModel.h
Marble::BookmarkManager::ensureDefaultFolder
void ensureDefaultFolder()
checks that there is at least one folder
Definition: BookmarkManager.cpp:247
Marble::GeoDataContainer::append
void append(GeoDataFeature *other)
add an element
Definition: GeoDataContainer.cpp:211
QFileInfo
QLatin1Char
Marble::BookmarkManagerPrivate::BookmarkManagerPrivate
BookmarkManagerPrivate(GeoDataTreeModel *treeModel)
Definition: BookmarkManager.cpp:31
Marble::GeoDataPlacemark::coordinate
GeoDataCoordinates coordinate(const QDateTime &dateTime=QDateTime(), bool *iconAtCoordinates=nullptr) const
Return the coordinates of the placemark at time dateTime as a GeoDataCoordinates. ...
Definition: GeoDataPlacemark.cpp:220
Marble::GeoDataFeature::setVisible
void setVisible(bool value)
Set a new value for visibility.
Definition: GeoDataFeature.cpp:361
QDir
GeoDataFolder.h
Marble::GeoDataFeature::setStyle
void setStyle(const QSharedPointer< GeoDataStyle > &style)
Sets the style of the placemark.
Definition: GeoDataFeature.cpp:436
QVector::constBegin
const_iterator constBegin() const
QVector
Marble::GeoDataFeature::name
QString name() const
The name of the feature.
Definition: GeoDataFeature.cpp:190
Marble::BookmarkManagerPrivate
Definition: BookmarkManager_p.h:24
Marble::GeoDataCoordinates::toString
QString toString() const
return a string representation of the coordinate this is a convenience function which uses the defaul...
Definition: GeoDataCoordinates.cpp:396
Marble::BookmarkManagerPrivate::resetBookmarkDocument
void resetBookmarkDocument()
Definition: BookmarkManager.cpp:50
Marble::GeoDataPlacemark::setVisualCategory
void setVisualCategory(GeoDataVisualCategory index)
Sets the symbol category of the placemark.
Definition: GeoDataPlacemark.cpp:145
Marble::BookmarkManager::~BookmarkManager
~BookmarkManager() override
Definition: BookmarkManager.cpp:84
Marble::BookmarkManager::bookmarkFile
QString bookmarkFile() const
return bookmark file path
Definition: BookmarkManager.cpp:89
Marble::GeoDataFeature::setZoomLevel
void setZoomLevel(int index)
Sets the popularity index of the placemark.
Definition: GeoDataFeature.cpp:504
Marble::BookmarkManager::removeBookmarkFolder
void removeBookmarkFolder(GeoDataFolder *folder)
Definition: BookmarkManager.cpp:241
Marble::BookmarkManager::setStyleBuilder
void setStyleBuilder(const StyleBuilder *styleBuilder)
Definition: BookmarkManager.cpp:260
Marble::GeoDataPlacemark
a class representing a point of interest on the map
Definition: GeoDataPlacemark.h:53
Marble::mDebug
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:36
Marble::BookmarkManager::showBookmarks
bool showBookmarks() const
Definition: BookmarkManager.cpp:190
coordinate
Coordinate coordinate
Definition: tools/osm-addresses/OsmParser.cpp:40
Marble::GeoDataContainer::placemarkList
QVector< GeoDataPlacemark * > placemarkList() const
A convenience function that returns all placemarks in this container.
Definition: GeoDataContainer.cpp:151
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Fri Dec 13 2019 01:46:57 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
  •   KmPlot
  • libkeduvocdocument
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   src
  •   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