1 /* SPDX-FileCopyrightText: 2020-2021 Tobias Leupold <tobias.leupold@gmx.de>
2 
3    SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-KDE-Accepted-GPL
4 */
5 
6 // Local includes
7 #include "PreviewWidget.h"
8 #include "SharedObjects.h"
9 #include "CoordinatesFormatter.h"
10 #include "ImagePreview.h"
11 #include "Coordinates.h"
12 
13 // KDE includes
14 #include <KLocalizedString>
15 
16 // Qt includes
17 #include <QVBoxLayout>
18 #include <QDebug>
19 #include <QLabel>
20 #include <QLocale>
21 #include <QGridLayout>
22 #include <QDateTime>
23 
PreviewWidget(SharedObjects * sharedObjects,QWidget * parent)24 PreviewWidget::PreviewWidget(SharedObjects *sharedObjects, QWidget *parent)
25     : QWidget(parent),
26       m_formatter(sharedObjects->coordinatesFormatter()),
27       m_locale(sharedObjects->locale())
28 {
29     auto *layout = new QVBoxLayout(this);
30 
31     auto *infoLayout = new QGridLayout;
32     layout->addLayout(infoLayout);
33 
34     auto *pathLabel = new QLabel(i18n("Image:"));
35     pathLabel->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
36     infoLayout->addWidget(pathLabel, 0, 0);
37 
38     m_path = new QLabel;
39     m_path->setWordWrap(true);
40     m_path->setTextInteractionFlags(Qt::TextSelectableByMouse);
41     infoLayout->addWidget(m_path, 0, 1);
42 
43     m_dateTimeLabel = new QLabel(i18n("Date/Time:"));
44     m_dateTimeLabel->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
45     infoLayout->addWidget(m_dateTimeLabel, 1, 0);
46 
47     m_date = new QLabel;
48     m_date->setTextInteractionFlags(Qt::TextSelectableByMouse);
49     m_date->setWordWrap(true);
50     infoLayout->addWidget(m_date, 1, 1);
51 
52     auto *coordinatesLabel = new QLabel(i18n("Coordinates:"));
53     coordinatesLabel->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
54     infoLayout->addWidget(coordinatesLabel, 2, 0);
55 
56     m_coordinates = new QLabel;
57     m_coordinates->setTextInteractionFlags(Qt::TextSelectableByMouse);
58     m_coordinates->setWordWrap(true);
59     infoLayout->addWidget(m_coordinates, 2, 1);
60 
61     m_preview = new ImagePreview;
62     layout->addWidget(m_preview);
63 
64     m_matchString[KGeoTag::NotMatched] = i18n("read from file");
65     m_matchString[KGeoTag::ExactMatch] = i18n("exact match");
66     m_matchString[KGeoTag::InterpolatedMatch] = i18n("interpolated match");
67     m_matchString[KGeoTag::ManuallySet] = i18n("manually set");
68 }
69 
setImage(const QModelIndex & index)70 void PreviewWidget::setImage(const QModelIndex &index)
71 {
72     m_currentIndex = index;
73     m_currentImage = index.isValid() ? index.data(KGeoTag::PathRole).toString() : QString();
74 
75     if (m_currentImage.isEmpty()) {
76         m_path->clear();
77         m_date->clear();
78         m_coordinates->clear();
79         m_preview->setImage(QModelIndex());
80         return;
81     }
82 
83     m_path->setText(m_currentImage);
84     if (m_cameraClockDeviation == 0) {
85         m_date->setText(index.data(KGeoTag::DateRole).value<QDateTime>()
86                             .toString(m_locale->dateTimeFormat()));
87     } else {
88         m_date->setText(
89             index.data(KGeoTag::DateRole).value<QDateTime>()
90                            .addSecs(m_cameraClockDeviation)
91                            .toString(m_locale->dateTimeFormat()));
92     }
93 
94     const auto coordinates = index.data(KGeoTag::CoordinatesRole).value<Coordinates>();
95     if (coordinates.isSet()) {
96         m_coordinates->setText(i18n("<p>Position: %1, %2; Altitude: %3 m<br/>(%4)</p>",
97             m_formatter->lon(coordinates),
98             m_formatter->lat(coordinates),
99             m_formatter->alt(coordinates),
100             m_matchString.value(index.data(KGeoTag::MatchTypeRole).value<KGeoTag::MatchType>())));
101     } else {
102         m_coordinates->setText(i18n("<i>No coordinates set</i>"));
103     }
104 
105     m_preview->setImage(index);
106 }
107 
currentImage() const108 QString PreviewWidget::currentImage() const
109 {
110     return m_currentImage;
111 }
112 
reload()113 void PreviewWidget::reload()
114 {
115     setImage(m_currentIndex);
116 }
117 
setCameraClockDeviation(int deviation)118 void PreviewWidget::setCameraClockDeviation(int deviation)
119 {
120     if (deviation == 0) {
121         m_dateTimeLabel->setText(i18n("Date/Time:"));
122     } else {
123         m_dateTimeLabel->setText(i18n("Date/Time\n(corrected):"));
124     }
125 
126     m_cameraClockDeviation = deviation;
127     reload();
128 }
129