1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2021-09-27
7  * Description : Showfoto stack view item
8  *
9  * Copyright (C) 2021 by Gilles Caulier <caulier dot gilles at gmail 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 "showfotostackviewitem.h"
25 
26 // Qt includes
27 
28 #include <QIcon>
29 #include <QPainter>
30 #include <QPixmap>
31 #include <QLocale>
32 #include <QDateTime>
33 #include <QMimeDatabase>
34 #include <QFileInfo>
35 
36 // KDE includes
37 
38 #include <klocalizedstring.h>
39 
40 // Local include
41 
42 #include "digikam_debug.h"
43 #include "showfotostackviewlist.h"
44 #include "showfotoitemsortsettings.h"
45 #include "itempropertiestab.h"
46 #include "drawdecoder.h"
47 
48 using namespace Digikam;
49 
50 namespace ShowFoto
51 {
52 
ShowfotoStackViewItem(ShowfotoStackViewList * const parent)53 ShowfotoStackViewItem::ShowfotoStackViewItem(ShowfotoStackViewList* const parent)
54     : QTreeWidgetItem(parent)
55 {
56     setDisabled(false);
57     setSelected(false);
58 }
59 
~ShowfotoStackViewItem()60 ShowfotoStackViewItem::~ShowfotoStackViewItem()
61 {
62 }
63 
setInfo(const ShowfotoItemInfo & info)64 void ShowfotoStackViewItem::setInfo(const ShowfotoItemInfo& info)
65 {
66     m_info                 = info;
67     setText(ShowfotoStackViewList::FileName, m_info.name);
68 
69     QDateTime dt           = (m_info.ctime.isValid() ? m_info.ctime : m_info.dtime);
70     QString str            = QLocale().toString(dt, QLocale::ShortFormat);
71     setText(ShowfotoStackViewList::FileDate, str);
72     setData(ShowfotoStackViewList::FileDate, Qt::UserRole, dt);
73 
74     QFileInfo fileInfo(m_info.name);
75     QString rawFilesExt    = DRawDecoder::rawFiles();
76     QString ext            = fileInfo.suffix().toUpper();
77 
78     if (!ext.isEmpty() && rawFilesExt.toUpper().contains(ext))
79     {
80         setText(ShowfotoStackViewList::FileType, i18nc("@info: item properties", "RAW Image"));
81     }
82     else
83     {
84         setText(ShowfotoStackViewList::FileType, QMimeDatabase().mimeTypeForFile(fileInfo).comment());
85     }
86 
87     QString localeFileSize = QLocale().toString(info.size);
88     str                    = ItemPropertiesTab::humanReadableBytesCount(m_info.size);
89     setText(ShowfotoStackViewList::FileSize, str);
90     setData(ShowfotoStackViewList::FileSize, Qt::UserRole, info.size);
91 }
92 
info() const93 ShowfotoItemInfo ShowfotoStackViewItem::info() const
94 {
95     return m_info;
96 }
97 
setThumbnail(const QPixmap & thumb)98 void ShowfotoStackViewItem::setThumbnail(const QPixmap& thumb)
99 {
100     QPixmap pix = thumb.scaled(treeWidget()->iconSize(), Qt::KeepAspectRatio,
101                                                          Qt::FastTransformation);
102 
103     QPixmap icon(treeWidget()->iconSize());
104     icon.fill(Qt::transparent);
105     QPainter p(&icon);
106     p.drawPixmap((icon.width()  - pix.width() ) / 2,
107                  (icon.height() - pix.height()) / 2,
108                  pix);
109 
110     setIcon(0, icon);
111 }
112 
operator <(const QTreeWidgetItem & other) const113 bool ShowfotoStackViewItem::operator<(const QTreeWidgetItem& other) const
114 {
115     ShowfotoStackViewList* const parent = dynamic_cast<ShowfotoStackViewList*>(treeWidget());
116 
117     if (!parent)
118     {
119         return false;
120     }
121 
122     int result                     = 0;
123     int column                     = parent->sortColumn();
124     Qt::SortOrder currentSortOrder = (Qt::SortOrder)parent->sortOrder();
125 
126     switch (column)
127     {
128         case ShowfotoStackViewList::FileSize:
129         {
130             result = (ShowfotoItemSortSettings::compareByOrder(data(ShowfotoStackViewList::FileSize, Qt::UserRole).toInt(),
131                                                                other.data(ShowfotoStackViewList::FileSize, Qt::UserRole).toInt(),
132                                                                currentSortOrder));
133             break;
134         }
135 
136         case ShowfotoStackViewList::FileType:
137         {
138             result = (ShowfotoItemSortSettings::naturalCompare(text(ShowfotoStackViewList::FileType),
139                                                                other.text(ShowfotoStackViewList::FileType),
140                                                                currentSortOrder,
141                                                                Qt::CaseSensitive));
142             break;
143         }
144 
145         case ShowfotoStackViewList::FileDate:
146         {
147             result = (ShowfotoItemSortSettings::compareByOrder(data(ShowfotoStackViewList::FileDate, Qt::UserRole).toDateTime(),
148                                                                other.data(ShowfotoStackViewList::FileDate, Qt::UserRole).toDateTime(),
149                                                                currentSortOrder));
150             break;
151         }
152 
153         default:    // ShowfotoStackViewList::FileName
154         {
155             result = (ShowfotoItemSortSettings::naturalCompare(text(ShowfotoStackViewList::FileName),
156                                                                other.text(ShowfotoStackViewList::FileName),
157                                                                currentSortOrder,
158                                                                Qt::CaseSensitive));
159             break;
160         }
161     }
162 
163     return (result < 0);
164 }
165 
166 } // namespace ShowFoto
167