diff --git a/Map/MapView.cpp b/Map/MapView.cpp index 4e1ac089..52fe5e8e 100644 --- a/Map/MapView.cpp +++ b/Map/MapView.cpp @@ -1,344 +1,365 @@ /* Copyright (C) 2014-2019 The KPhotoAlbum Development Team This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License or (at your option) version 3 or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of version 3 of the license. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ // Local includes #include "MapView.h" -#include "ImageManager/ThumbnailCache.h" #include "Logging.h" -// Marble includes -#include -#include -#include +#include +#include +#include +#include -// Qt includes +#include +#include +#include +#include +#include #include #include +#include #include #include #include #include #include - -// KDE includes -#include -#include -#include -#include -#include +#include +#include +#include namespace { const QString MAPVIEW_FLOATER_VISIBLE_CONFIG_PREFIX = QStringLiteral("MarbleFloaterVisible "); const QStringList MAPVIEW_RENDER_POSITION({ QStringLiteral("HOVERS_ABOVE_SURFACE") }); const QVector WANTED_FLOATERS { QStringLiteral("Compass"), QStringLiteral("Scale Bar"), QStringLiteral("Navigation"), QStringLiteral("Overview Map") }; } Map::MapView::MapView(QWidget *parent, UsageType type) : QWidget(parent) { if (type == UsageType::MapViewWindow) { setWindowFlags(Qt::Window); setAttribute(Qt::WA_DeleteOnClose); } QVBoxLayout *layout = new QVBoxLayout(this); m_statusLabel = new QLabel; m_statusLabel->setAlignment(Qt::AlignCenter); m_statusLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); m_statusLabel->hide(); layout->addWidget(m_statusLabel); m_mapWidget = new Marble::MarbleWidget; m_mapWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); m_mapWidget->setProjection(Marble::Mercator); m_mapWidget->setMapThemeId(QStringLiteral("earth/openstreetmap/openstreetmap.dgml")); #ifdef MARBLE_HAS_regionSelected_NEW connect(m_mapWidget, &Marble::MarbleWidget::regionSelected, this, &Map::MapView::updateRegionSelection); #else connect(m_mapWidget, &Marble::MarbleWidget::regionSelected, this, &Map::MapView::updateRegionSelectionOld); #endif m_mapWidget->addLayer(this); layout->addWidget(m_mapWidget); m_mapWidget->show(); QHBoxLayout *controlLayout = new QHBoxLayout; layout->addLayout(controlLayout); // KPA's control buttons m_kpaButtons = new QWidget; QHBoxLayout *kpaButtonsLayout = new QHBoxLayout(m_kpaButtons); controlLayout->addWidget(m_kpaButtons); QPushButton *saveButton = new QPushButton; saveButton->setFlat(true); saveButton->setIcon(QPixmap(SmallIcon(QStringLiteral("media-floppy")))); saveButton->setToolTip(i18n("Save the current map settings")); kpaButtonsLayout->addWidget(saveButton); connect(saveButton, &QPushButton::clicked, this, &MapView::saveSettings); m_setLastCenterButton = new QPushButton; m_setLastCenterButton->setFlat(true); m_setLastCenterButton->setIcon(QPixmap(SmallIcon(QStringLiteral("go-first")))); m_setLastCenterButton->setToolTip(i18n("Go to last map position")); kpaButtonsLayout->addWidget(m_setLastCenterButton); connect(m_setLastCenterButton, &QPushButton::clicked, this, &MapView::setLastCenter); QPushButton *showThumbnails = new QPushButton; showThumbnails->setFlat(true); showThumbnails->setIcon(QPixmap(SmallIcon(QStringLiteral("view-preview")))); showThumbnails->setToolTip(i18n("Show thumbnails")); kpaButtonsLayout->addWidget(showThumbnails); m_showThumbnails = true; showThumbnails->setCheckable(true); showThumbnails->setChecked(false); connect(showThumbnails, &QPushButton::clicked, this, &MapView::setShowThumbnails); // Marble floater control buttons m_floaters = new QWidget; QHBoxLayout *floatersLayout = new QHBoxLayout(m_floaters); controlLayout->addStretch(); controlLayout->addWidget(m_floaters); KSharedConfigPtr config = KSharedConfig::openConfig(); KConfigGroup group = config->group(QStringLiteral("MapView")); for (const Marble::RenderPlugin *plugin : m_mapWidget->renderPlugins()) { if (plugin->renderType() != Marble::RenderPlugin::PanelRenderType) { continue; } const QString name = plugin->name(); if (!WANTED_FLOATERS.contains(name)) { continue; } QPushButton *button = new QPushButton; button->setCheckable(true); button->setFlat(true); button->setChecked(plugin->action()->isChecked()); button->setToolTip(plugin->description()); button->setProperty("floater", name); QPixmap icon = plugin->action()->icon().pixmap(QSize(20, 20)); if (icon.isNull()) { icon = QPixmap(20, 20); icon.fill(Qt::white); } button->setIcon(icon); connect(plugin->action(), &QAction::toggled, button, &QPushButton::setChecked); connect(button, &QPushButton::toggled, plugin->action(), &QAction::setChecked); floatersLayout->addWidget(button); const QVariant checked = group.readEntry(MAPVIEW_FLOATER_VISIBLE_CONFIG_PREFIX + name, true); button->setChecked(checked.toBool()); } m_pin = QPixmap(QStandardPaths::locate(QStandardPaths::DataLocation, QStringLiteral("pics/pin.png"))); } void Map::MapView::clear() { m_images.clear(); m_markersBox.clear(); m_regionSelected = false; } void Map::MapView::addImage(DB::ImageInfoPtr image) { qCDebug(MapLog) << "Adding image" << image->label(); m_images.append(image); // Update the viewport for zoomToMarkers() if (m_markersBox.isEmpty()) { m_markersBox.setEast(image->coordinates().lon(), Marble::GeoDataCoordinates::Degree); m_markersBox.setWest(image->coordinates().lon(), Marble::GeoDataCoordinates::Degree); m_markersBox.setNorth(image->coordinates().lat(), Marble::GeoDataCoordinates::Degree); m_markersBox.setSouth(image->coordinates().lat(), Marble::GeoDataCoordinates::Degree); } else { if (m_markersBox.east(Marble::GeoDataCoordinates::Degree) < image->coordinates().lon()) { m_markersBox.setEast(image->coordinates().lon(), Marble::GeoDataCoordinates::Degree); } if (m_markersBox.west(Marble::GeoDataCoordinates::Degree) > image->coordinates().lon()) { m_markersBox.setWest(image->coordinates().lon(), Marble::GeoDataCoordinates::Degree); } if (m_markersBox.north(Marble::GeoDataCoordinates::Degree) < image->coordinates().lat()) { m_markersBox.setNorth(image->coordinates().lat(), Marble::GeoDataCoordinates::Degree); } if (m_markersBox.south(Marble::GeoDataCoordinates::Degree) > image->coordinates().lat()) { m_markersBox.setSouth(image->coordinates().lat(), Marble::GeoDataCoordinates::Degree); } } } +void Map::MapView::addImages(const DB::ImageSearchInfo &searchInfo) +{ + QElapsedTimer timer; + timer.start(); + displayStatus(MapStatus::Loading); + DB::FileNameList images = DB::ImageDB::instance()->search(searchInfo); + int count = 0; + int total = 0; + for (const auto &imageInfo : images) { + DB::ImageInfoPtr image = imageInfo.info(); + total++; + if (image->coordinates().hasCoordinates()) { + count++; + addImage(image); + } + } + displayStatus(MapStatus::SearchCoordinates); + qCDebug(TimingLog) << "MapView::addImages(): added" << count << "of" << total << "images in" + << timer.elapsed() << "ms."; +} + void Map::MapView::zoomToMarkers() { m_mapWidget->centerOn(m_markersBox); } void Map::MapView::setCenter(const DB::ImageInfoPtr image) { m_lastCenter = image->coordinates(); setLastCenter(); } void Map::MapView::saveSettings() { KSharedConfigPtr config = KSharedConfig::openConfig(); KConfigGroup group = config->group(QStringLiteral("MapView")); for (const QPushButton *button : m_floaters->findChildren()) { group.writeEntry(MAPVIEW_FLOATER_VISIBLE_CONFIG_PREFIX + button->property("floater").toString(), button->isChecked()); } config->sync(); QMessageBox::information(this, i18n("Map view"), i18n("Settings saved!")); } void Map::MapView::setShowThumbnails(bool state) { m_showThumbnails = state; m_mapWidget->reloadMap(); } void Map::MapView::displayStatus(MapStatus status) { switch (status) { case MapStatus::Loading: m_statusLabel->setText(i18n("Loading coordinates from the images ...")); m_statusLabel->show(); m_mapWidget->hide(); m_regionSelected = false; m_setLastCenterButton->setEnabled(false); break; case MapStatus::ImageHasCoordinates: m_statusLabel->hide(); m_regionSelected = false; m_mapWidget->show(); m_setLastCenterButton->show(); m_setLastCenterButton->setEnabled(true); break; case MapStatus::ImageHasNoCoordinates: m_statusLabel->setText(i18n("This image does not contain geographic coordinates.")); m_statusLabel->show(); m_mapWidget->hide(); m_setLastCenterButton->show(); m_setLastCenterButton->setEnabled(false); break; case MapStatus::SomeImagesHaveNoCoordinates: m_statusLabel->setText(i18n("Some of the selected images do not contain geographic " "coordinates.")); m_statusLabel->show(); m_regionSelected = false; m_mapWidget->show(); m_setLastCenterButton->show(); m_setLastCenterButton->setEnabled(true); break; case MapStatus::SearchCoordinates: m_statusLabel->setText(i18n("Search for geographic coordinates.")); m_statusLabel->show(); m_mapWidget->show(); m_mapWidget->centerOn(0.0, 0.0); m_setLastCenterButton->hide(); break; case MapStatus::NoImagesHaveNoCoordinates: m_statusLabel->setText(i18n("None of the selected images contain geographic " "coordinates.")); m_statusLabel->show(); m_mapWidget->hide(); m_setLastCenterButton->show(); m_setLastCenterButton->setEnabled(false); break; } emit displayStatusChanged(status); } void Map::MapView::setLastCenter() { m_mapWidget->centerOn(m_lastCenter.lon(), m_lastCenter.lat()); } void Map::MapView::updateRegionSelection(const Marble::GeoDataLatLonBox &selection) { m_regionSelected = true; m_regionSelection = selection; emit signalRegionSelectionChanged(); } #ifndef MARBLE_HAS_regionSelected_NEW void Map::MapView::updateRegionSelectionOld(const QList &selection) { Q_ASSERT(selection.length() == 4); // see also: https://cgit.kde.org/marble.git/commit/?id=ec1f7f554e9f6ca248b4a3b01dbf08507870687e Marble::GeoDataLatLonBox sel { selection.at(1), selection.at(3), selection.at(2), selection.at(0), Marble::GeoDataCoordinates::Degree }; updateRegionSelection(sel); } #endif Map::GeoCoordinates::Pair Map::MapView::getRegionSelection() const { return GeoCoordinates::makePair(m_regionSelection.west(Marble::GeoDataCoordinates::Degree), m_regionSelection.north(Marble::GeoDataCoordinates::Degree), m_regionSelection.east(Marble::GeoDataCoordinates::Degree), m_regionSelection.south(Marble::GeoDataCoordinates::Degree)); } bool Map::MapView::regionSelected() const { return m_regionSelected; } QStringList Map::MapView::renderPosition() const { // we only ever paint on the same layer: return MAPVIEW_RENDER_POSITION; } bool Map::MapView::render(Marble::GeoPainter *painter, Marble::ViewportParams *, const QString &renderPos, Marble::GeoSceneLayer *) { Q_ASSERT(renderPos == renderPosition().first()); for (const DB::ImageInfoPtr &image : m_images) { const Marble::GeoDataCoordinates pos(image->coordinates().lon(), image->coordinates().lat(), image->coordinates().alt(), Marble::GeoDataCoordinates::Degree); if (m_showThumbnails) { // FIXME(l3u) Maybe we should cache the scaled thumbnails? painter->drawPixmap(pos, ImageManager::ThumbnailCache::instance()->lookup(image->fileName()).scaled(QSize(40, 40), Qt::KeepAspectRatio)); } else { painter->drawPixmap(pos, m_pin); } } return true; } // vi:expandtab:tabstop=4 shiftwidth=4: diff --git a/Map/MapView.h b/Map/MapView.h index 52b35729..c9df7522 100644 --- a/Map/MapView.h +++ b/Map/MapView.h @@ -1,175 +1,183 @@ /* Copyright (C) 2014-2019 The KPhotoAlbum Development Team This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License or (at your option) version 3 or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of version 3 of the license. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef MAPVIEW_H #define MAPVIEW_H #include "config-kpa-marble.h" #include "GeoCoordinates.h" #include #include #include #include #include #include #include #include -// Marble classes +namespace DB +{ +class ImageSearchInfo; +} namespace Marble { class MarbleWidget; } -// Qt classes class QLabel; class QPushButton; namespace Map { /** * UsageType: determines whether the widget is used as a standalone widget * or within another widget (e.g. the AnnotationDialog). * @see Viewer::ViewerWidget::UsageType */ enum class UsageType { InlineMapView, MapViewWindow }; /** * MapStatus: determines the visibility and text of the status label and the visibility of the * map, depending on the availability of coordinates of the image(s) that are displayed. */ enum class MapStatus { Loading, ImageHasCoordinates, ImageHasNoCoordinates, NoImagesHaveNoCoordinates, SomeImagesHaveNoCoordinates, SearchCoordinates }; class MapView : public QWidget, public Marble::LayerInterface { Q_OBJECT public: explicit MapView(QWidget *parent = nullptr, UsageType type = UsageType::InlineMapView); ~MapView() override = default; /** * Removes all images from the map. */ void clear(); /** * Add an image to the map. */ void addImage(DB::ImageInfoPtr image); + /** + * @brief addImages adds images matching a search info to the map. + * @param searchInfo + */ + void addImages(const DB::ImageSearchInfo &searchInfo); + /** * Sets the map's zoom so that all images on the map are visible. * If no images have been added, the zoom is not altered. */ void zoomToMarkers(); /** * Sets the state of the "Show Thumbnails" button on the map's control widget. */ void setShowThumbnails(bool state); /** * This sets the status label text and it's visibility, as well as the visibilty of the map * itself to the state indicated by the given MapStatus. */ void displayStatus(MapStatus status); GeoCoordinates::Pair getRegionSelection() const; bool regionSelected() const; // LayerInterface: /** * @brief renderPosition tells the LayerManager what layers we (currently) want to paint on. * Part of the LayerInterface; called by the LayerManager. * @return */ QStringList renderPosition() const override; /** * @brief Render all markers onto the marbleWidget. * Part of the LayerInterface; called by the LayerManager. * @param painter the painter used by the LayerManager * @param viewport * @param renderPos the layer name * @param layer always \c nullptr * @return \c true (return value is discarded by LayerManager::renderLayers()) */ bool render(Marble::GeoPainter *painter, Marble::ViewportParams *, const QString &renderPos, Marble::GeoSceneLayer *) override; Q_SIGNALS: void signalRegionSelectionChanged(); void displayStatusChanged(MapStatus); public slots: /** * Centers the map on the coordinates of the given image. */ void setCenter(const DB::ImageInfoPtr image); private slots: void saveSettings(); void setLastCenter(); void updateRegionSelection(const Marble::GeoDataLatLonBox &selection); #ifndef MARBLE_HAS_regionSelected_NEW // remove once we don't care about Marble v17.12.3 and older anymore void updateRegionSelectionOld(const QList &selection); #endif private: // Variables Marble::MarbleWidget *m_mapWidget; QLabel *m_statusLabel; QPushButton *m_setLastCenterButton; GeoCoordinates m_lastCenter; QWidget *m_kpaButtons; QWidget *m_floaters; // FIXME(jzarl): dirty hack to get it working // if this should work efficiently with a large number of images, // some spatially aware data structure probably needs to be used // (e.g. binning images by location) QList m_images; Marble::GeoDataLatLonBox m_markersBox; bool m_showThumbnails; QPixmap m_pin; Marble::GeoDataLatLonBox m_regionSelection; bool m_regionSelected = false; }; } #endif // MAPVIEW_H // vi:expandtab:tabstop=4 shiftwidth=4: