1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 09-08-2013
7  * Description : Showfoto tool tip filler
8  *
9  * Copyright (C) 2013 by Mohamed_Anwer <m_dot_anwer at gmx dot com>
10  *
11  * This program is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation;
14  * either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * ============================================================ */
23 
24 #include "showfototooltipfiller.h"
25 
26 // Qt includes
27 
28 #include <QDateTime>
29 #include <QTextDocument>
30 #include <QLocale>
31 
32 // KDE includes
33 
34 #include <klocalizedstring.h>
35 
36 // Local includes
37 
38 #include "ditemtooltip.h"
39 #include "itempropertiestab.h"
40 #include "showfotoiteminfo.h"
41 #include "showfotosettings.h"
42 
43 using namespace Digikam;
44 
45 namespace ShowFoto
46 {
47 
ShowfotoItemInfoTipContents(const ShowfotoItemInfo & info)48 QString ShowfotoToolTipFiller::ShowfotoItemInfoTipContents(const ShowfotoItemInfo& info)
49 {
50     QString str;
51 
52     ShowfotoSettings* const settings = ShowfotoSettings::instance();
53 
54     DToolTipStyleSheet cnt(settings->getToolTipFont());
55 
56     PhotoInfoContainer photoInfo     = info.photoInfo;
57     QString tip                      = cnt.tipHeader;
58 
59     // -- File properties ----------------------------------------------
60 
61     if (settings->getShowFileName()  ||
62         settings->getShowFileDate()  ||
63         settings->getShowFileSize()  ||
64         settings->getShowFileType()  ||
65         settings->getShowFileDim() )
66     {
67         tip += cnt.headBeg + i18n("File Properties") + cnt.headEnd;
68 
69         if (settings->getShowFileName())
70         {
71             tip += cnt.cellBeg + i18nc("filename", "Name:") + cnt.cellMid;
72             tip += info.name + cnt.cellEnd;
73         }
74 
75         if (settings->getShowFileDate())
76         {
77             QDateTime createdDate  = info.dtime;
78             str                    = QLocale().toString(createdDate, QLocale::ShortFormat);
79             tip                   += cnt.cellBeg + i18n("Date:") + cnt.cellMid + str + cnt.cellEnd;
80         }
81 
82         if (settings->getShowFileSize())
83         {
84             tip                   += cnt.cellBeg + i18n("Size:") + cnt.cellMid;
85             QString localeFileSize = QLocale().toString(info.size);
86             str                    = i18n("%1 (%2)", ItemPropertiesTab::humanReadableBytesCount(info.size), localeFileSize);
87             tip                   += str + cnt.cellEnd;
88         }
89 
90         if (settings->getShowFileType())
91         {
92             tip += cnt.cellBeg + i18n("Type:") + cnt.cellMid + info.mime + cnt.cellEnd;
93         }
94 
95         if (settings->getShowFileDim())
96         {
97             if ((info.width == 0) || (info.height == 0) || (info.width == -1) || (info.height == -1))
98             {
99                 str = i18nc("unknown / invalid image dimension",
100                             "Unknown");
101             }
102             else
103             {
104                 QString mpixels = QLocale().toString(info.width*info.height/1000000.0, 'f', 1);
105                 str = i18nc("width x height (megapixels Mpx)", "%1x%2 (%3Mpx)",
106                             info.width, info.height, mpixels);
107             }
108 
109             tip += cnt.cellBeg + i18n("Dimensions:") + cnt.cellMid + str + cnt.cellEnd;
110         }
111     }
112 
113     // -- Photograph Info -----------------------------------------------------------------------
114 
115     if (settings->getShowPhotoMake()  ||
116         settings->getShowPhotoLens()  ||
117         settings->getShowPhotoFocal() ||
118         settings->getShowPhotoExpo()  ||
119         settings->getShowPhotoFlash() ||
120         settings->getShowPhotoWB()    ||
121         settings->getShowPhotoDate()  ||
122         settings->getShowPhotoMode())
123     {
124         if (!photoInfo.isNull())
125         {
126             QString metaStr;
127             tip += cnt.headBeg + i18n("Photograph Properties") + cnt.headEnd;
128 
129             if (settings->getShowPhotoMake())
130             {
131                 ItemPropertiesTab::shortenedMakeInfo(photoInfo.make);
132                 ItemPropertiesTab::shortenedModelInfo(photoInfo.model);
133 
134                 str = QString::fromUtf8("%1 / %2").arg(photoInfo.make.isEmpty() ? cnt.unavailable : photoInfo.make)
135                                                   .arg(photoInfo.model.isEmpty() ? cnt.unavailable : photoInfo.model);
136 
137                 if (str.length() > cnt.maxStringLength)
138                 {
139                     str = str.left(cnt.maxStringLength-3) + QLatin1String("...");
140                 }
141 
142                 metaStr += cnt.cellBeg + i18n("Make/Model:") + cnt.cellMid + str.toHtmlEscaped() + cnt.cellEnd;
143             }
144 
145             if (settings->getShowPhotoLens())
146             {
147                 str          = photoInfo.lens.isEmpty() ? cnt.unavailable : photoInfo.lens;
148                 QString lens = i18nc("camera lens", "Lens:");
149 
150                 if (str.length() > cnt.maxStringLength)
151                 {
152                     int space = str.lastIndexOf(QLatin1Char(' '), cnt.maxStringLength);
153 
154                     if (space == -1)
155                     {
156                         space = cnt.maxStringLength;
157                     }
158 
159                     metaStr += cnt.cellBeg + lens + cnt.cellMid + str.left(space).toHtmlEscaped() + cnt.cellEnd;
160 
161                     str  = str.mid(space+1);
162                     lens = QString();
163                 }
164 
165                 if (str.length() > cnt.maxStringLength)
166                 {
167                     str = str.left(cnt.maxStringLength-3) + QLatin1String("...");
168                 }
169 
170                 metaStr += cnt.cellBeg + lens + cnt.cellMid + str.toHtmlEscaped() + cnt.cellEnd;
171             }
172 
173             if (settings->getShowPhotoDate())
174             {
175                 if (info.ctime.isValid())
176                 {
177                     QDateTime createdDate  = info.ctime;
178                     str                    = QLocale().toString(createdDate, QLocale::ShortFormat);
179                     tip                   += cnt.cellBeg + i18n("Date:") + cnt.cellMid + str + cnt.cellEnd;
180                 }
181                 else
182                 {
183                     metaStr += cnt.cellBeg + i18nc("creation date of the image",
184                                                    "Created:") + cnt.cellMid + cnt.unavailable.toHtmlEscaped() + cnt.cellEnd;
185                 }
186             }
187 
188             if (settings->getShowPhotoFocal())
189             {
190                 str = photoInfo.aperture.isEmpty() ? cnt.unavailable : photoInfo.aperture;
191 
192                 if (photoInfo.focalLength35mm.isEmpty())
193                 {
194                     str += QString::fromUtf8(" / %1").arg(photoInfo.focalLength.isEmpty() ? cnt.unavailable : photoInfo.focalLength);
195                 }
196                 else
197                 {
198                     str += QString::fromUtf8(" / %1").arg(i18n("%1 (%2)",photoInfo.focalLength, photoInfo.focalLength35mm));
199                 }
200 
201                 if (str.length() > cnt.maxStringLength)
202                 {
203                     str = str.left(cnt.maxStringLength-3) + QLatin1String("...");
204                 }
205 
206                 metaStr += cnt.cellBeg + i18n("Aperture/Focal:") + cnt.cellMid + str.toHtmlEscaped() + cnt.cellEnd;
207             }
208 
209             if (settings->getShowPhotoExpo())
210             {
211                 str = QString::fromUtf8("%1 / %2").arg(photoInfo.exposureTime.isEmpty() ? cnt.unavailable : photoInfo.exposureTime)
212                                                   .arg(photoInfo.sensitivity.isEmpty()  ? cnt.unavailable : i18n("%1 ISO",photoInfo.sensitivity));
213 
214                 if (str.length() > cnt.maxStringLength)
215                 {
216                     str = str.left(cnt.maxStringLength-3) + QLatin1String("...");
217                 }
218 
219                 metaStr += cnt.cellBeg + i18n("Exposure/Sensitivity:") + cnt.cellMid + str.toHtmlEscaped() + cnt.cellEnd;
220             }
221 
222             if (settings->getShowPhotoMode())
223             {
224                 if      (photoInfo.exposureMode.isEmpty() && photoInfo.exposureProgram.isEmpty())
225                 {
226                     str = cnt.unavailable;
227                 }
228                 else if (!photoInfo.exposureMode.isEmpty() && photoInfo.exposureProgram.isEmpty())
229                 {
230                     str = photoInfo.exposureMode;
231                 }
232                 else if (photoInfo.exposureMode.isEmpty() && !photoInfo.exposureProgram.isEmpty())
233                 {
234                     str = photoInfo.exposureProgram;
235                 }
236                 else
237                 {
238                     str = QString::fromUtf8("%1 / %2").arg(photoInfo.exposureMode).arg(photoInfo.exposureProgram);
239                 }
240 
241                 if (str.length() > cnt.maxStringLength)
242                 {
243                     str = str.left(cnt.maxStringLength-3) + QLatin1String("...");
244                 }
245 
246                 metaStr += cnt.cellBeg + i18n("Mode/Program:") + cnt.cellMid + str.toHtmlEscaped() + cnt.cellEnd;
247             }
248 
249             if (settings->getShowPhotoFlash())
250             {
251                 str = photoInfo.flash.isEmpty() ? cnt.unavailable : photoInfo.flash;
252 
253                 if (str.length() > cnt.maxStringLength)
254                 {
255                     str = str.left(cnt.maxStringLength-3) + QLatin1String("...");
256                 }
257 
258                 metaStr += cnt.cellBeg + i18nc("camera flash settings",
259                                                "Flash:") + cnt.cellMid + str.toHtmlEscaped() + cnt.cellEnd;
260             }
261 
262             if (settings->getShowPhotoWB())
263             {
264                 str = photoInfo.whiteBalance.isEmpty() ? cnt.unavailable : photoInfo.whiteBalance;
265 
266                 if (str.length() > cnt.maxStringLength)
267                 {
268                     str = str.left(cnt.maxStringLength-3) + QLatin1String("...");
269                 }
270 
271                 metaStr += cnt.cellBeg + i18n("White Balance:") + cnt.cellMid + str.toHtmlEscaped() + cnt.cellEnd;
272             }
273 
274             tip += metaStr;
275         }
276     }
277 
278     tip += cnt.tipFooter;
279 
280     return tip;
281 }
282 
283 } // namespace ShowFoto
284