File indexing completed on 2024-04-28 15:39:07

0001 // SPDX-FileCopyrightText: 2020-2022 Tobias Leupold <tl at stonemx dot de>
0002 //
0003 // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 
0005 // Local includes
0006 #include "PreviewWidget.h"
0007 #include "SharedObjects.h"
0008 #include "CoordinatesFormatter.h"
0009 #include "ImagePreview.h"
0010 #include "Coordinates.h"
0011 
0012 // KDE includes
0013 #include <KLocalizedString>
0014 
0015 // Qt includes
0016 #include <QVBoxLayout>
0017 #include <QDebug>
0018 #include <QLabel>
0019 #include <QLocale>
0020 #include <QGridLayout>
0021 #include <QDateTime>
0022 #include <QToolButton>
0023 #include <QDesktopServices>
0024 #include <QUrl>
0025 
0026 PreviewWidget::PreviewWidget(SharedObjects *sharedObjects, QWidget *parent)
0027     : QWidget(parent),
0028       m_formatter(sharedObjects->coordinatesFormatter()),
0029       m_locale(sharedObjects->locale())
0030 {
0031     auto *layout = new QVBoxLayout(this);
0032 
0033     auto *infoLayout = new QGridLayout;
0034     layout->addLayout(infoLayout);
0035 
0036     auto *pathLabel = new QLabel(i18n("Image:"));
0037     pathLabel->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
0038     infoLayout->addWidget(pathLabel, 0, 0);
0039 
0040     m_path = new QLabel;
0041     m_path->setWordWrap(true);
0042     m_path->setTextInteractionFlags(Qt::TextSelectableByMouse);
0043     infoLayout->addWidget(m_path, 0, 1);
0044 
0045     m_openExternally = new QToolButton;
0046     m_openExternally->setStyleSheet(QStringLiteral("border: none;"));
0047     m_openExternally->setIcon(QIcon::fromTheme(QStringLiteral("file-zoom-in")));
0048     m_openExternally->setToolTip(i18n("Open with default viewer"));
0049     m_openExternally->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
0050     infoLayout->addWidget(m_openExternally, 0, 2);
0051     m_openExternally->hide();
0052     connect(m_openExternally, &QToolButton::clicked, this, &PreviewWidget::openExternally);
0053 
0054     m_dateTimeLabel = new QLabel(i18n("Date/Time:"));
0055     m_dateTimeLabel->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
0056     infoLayout->addWidget(m_dateTimeLabel, 1, 0);
0057 
0058     m_date = new QLabel;
0059     m_date->setTextInteractionFlags(Qt::TextSelectableByMouse);
0060     m_date->setWordWrap(true);
0061     infoLayout->addWidget(m_date, 1, 1, 1, 2);
0062 
0063     auto *coordinatesLabel = new QLabel(i18n("Coordinates:"));
0064     coordinatesLabel->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
0065     infoLayout->addWidget(coordinatesLabel, 2, 0);
0066 
0067     m_coordinates = new QLabel;
0068     m_coordinates->setTextInteractionFlags(Qt::TextSelectableByMouse);
0069     m_coordinates->setWordWrap(true);
0070     infoLayout->addWidget(m_coordinates, 2, 1, 1, 2);
0071 
0072     m_preview = new ImagePreview;
0073     layout->addWidget(m_preview);
0074 
0075     m_matchString[KGeoTag::NotMatched] = i18n("read from file");
0076     m_matchString[KGeoTag::ExactMatch] = i18n("exact match");
0077     m_matchString[KGeoTag::InterpolatedMatch] = i18n("interpolated match");
0078     m_matchString[KGeoTag::ManuallySet] = i18n("manually set");
0079 }
0080 
0081 void PreviewWidget::setImage(const QModelIndex &index)
0082 {
0083     m_currentIndex = index;
0084     m_currentImage = index.isValid() ? index.data(KGeoTag::PathRole).toString() : QString();
0085 
0086     if (m_currentImage.isEmpty()) {
0087         m_openExternally->hide();
0088         m_path->clear();
0089         m_date->clear();
0090         m_coordinates->clear();
0091         m_preview->setImage(QModelIndex());
0092         return;
0093     }
0094 
0095     m_openExternally->show();
0096     m_path->setText(m_currentImage);
0097     if (m_cameraClockDeviation == 0) {
0098         m_date->setText(index.data(KGeoTag::DateRole).value<QDateTime>()
0099                             .toString(m_locale->dateTimeFormat()));
0100     } else {
0101         m_date->setText(
0102             index.data(KGeoTag::DateRole).value<QDateTime>()
0103                            .addSecs(m_cameraClockDeviation)
0104                            .toString(m_locale->dateTimeFormat()));
0105     }
0106 
0107     const auto coordinates = index.data(KGeoTag::CoordinatesRole).value<Coordinates>();
0108     if (coordinates.isSet()) {
0109         m_coordinates->setText(i18n("<p>Position: %1, %2; Altitude: %3 m<br/>(%4)</p>",
0110             m_formatter->lon(coordinates),
0111             m_formatter->lat(coordinates),
0112             m_formatter->alt(coordinates),
0113             m_matchString.value(index.data(KGeoTag::MatchTypeRole).value<KGeoTag::MatchType>())));
0114     } else {
0115         m_coordinates->setText(i18n("<i>No coordinates set</i>"));
0116     }
0117 
0118     m_preview->setImage(index);
0119 }
0120 
0121 QString PreviewWidget::currentImage() const
0122 {
0123     return m_currentImage;
0124 }
0125 
0126 void PreviewWidget::reload()
0127 {
0128     setImage(m_currentIndex);
0129 }
0130 
0131 void PreviewWidget::setCameraClockDeviation(int deviation)
0132 {
0133     if (deviation == 0) {
0134         m_dateTimeLabel->setText(i18n("Date/Time:"));
0135     } else {
0136         m_dateTimeLabel->setText(i18n("Date/Time\n(corrected):"));
0137     }
0138 
0139     m_cameraClockDeviation = deviation;
0140     reload();
0141 }
0142 
0143 void PreviewWidget::openExternally()
0144 {
0145     QDesktopServices::openUrl(QUrl::fromLocalFile(m_currentImage));
0146 }