File indexing completed on 2025-01-19 03:57:32

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2021-09-27
0007  * Description : Showfoto stack view item
0008  *
0009  * SPDX-FileCopyrightText: 2021-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "showfotostackviewitem.h"
0016 
0017 // Qt includes
0018 
0019 #include <QIcon>
0020 #include <QPainter>
0021 #include <QLocale>
0022 #include <QDateTime>
0023 #include <QMimeDatabase>
0024 #include <QFileInfo>
0025 
0026 // KDE includes
0027 
0028 #include <klocalizedstring.h>
0029 
0030 // Local include
0031 
0032 #include "digikam_debug.h"
0033 #include "showfotostackviewlist.h"
0034 #include "showfotoitemsortsettings.h"
0035 #include "itempropertiestab.h"
0036 #include "drawdecoder.h"
0037 
0038 using namespace Digikam;
0039 
0040 namespace ShowFoto
0041 {
0042 
0043 ShowfotoStackViewItem::ShowfotoStackViewItem(ShowfotoStackViewList* const parent)
0044     : QTreeWidgetItem(parent)
0045 {
0046     setDisabled(false);
0047     setSelected(false);
0048 }
0049 
0050 ShowfotoStackViewItem::~ShowfotoStackViewItem()
0051 {
0052 }
0053 
0054 void ShowfotoStackViewItem::setInfo(const ShowfotoItemInfo& info)
0055 {
0056     m_info                 = info;
0057     setText(ShowfotoStackViewList::FileName, m_info.name);
0058 
0059     QDateTime dt           = (m_info.ctime.isValid() ? m_info.ctime : m_info.dtime);
0060     QString str            = QLocale().toString(dt, QLocale::ShortFormat);
0061     setText(ShowfotoStackViewList::FileDate, str);
0062     setData(ShowfotoStackViewList::FileDate, Qt::UserRole, dt);
0063 
0064     QFileInfo fileInfo(m_info.name);
0065     QString rawFilesExt    = DRawDecoder::rawFiles();
0066     QString ext            = fileInfo.suffix().toUpper();
0067 
0068     if (!ext.isEmpty() && rawFilesExt.toUpper().contains(ext))
0069     {
0070         setText(ShowfotoStackViewList::FileType, i18nc("@info: item properties", "RAW Image"));
0071     }
0072     else
0073     {
0074         setText(ShowfotoStackViewList::FileType, QMimeDatabase().mimeTypeForFile(fileInfo).comment());
0075     }
0076 
0077     QString localeFileSize = QLocale().toString(info.size);
0078     str                    = ItemPropertiesTab::humanReadableBytesCount(m_info.size);
0079     setText(ShowfotoStackViewList::FileSize, str);
0080     setData(ShowfotoStackViewList::FileSize, Qt::UserRole, info.size);
0081 }
0082 
0083 ShowfotoItemInfo ShowfotoStackViewItem::info() const
0084 {
0085     return m_info;
0086 }
0087 
0088 void ShowfotoStackViewItem::setThumbnail(const QPixmap& thumb)
0089 {
0090     QPixmap pix = thumb.scaled(treeWidget()->iconSize(), Qt::KeepAspectRatio,
0091                                                          Qt::FastTransformation);
0092 
0093     QPixmap icon(treeWidget()->iconSize());
0094     icon.fill(Qt::transparent);
0095     QPainter p(&icon);
0096     p.drawPixmap((icon.width()  - pix.width() ) / 2,
0097                  (icon.height() - pix.height()) / 2,
0098                  pix);
0099 
0100     setIcon(0, icon);
0101 }
0102 
0103 bool ShowfotoStackViewItem::operator<(const QTreeWidgetItem& other) const
0104 {
0105     ShowfotoStackViewList* const parent = dynamic_cast<ShowfotoStackViewList*>(treeWidget());
0106 
0107     if (!parent)
0108     {
0109         return false;
0110     }
0111 
0112     int result                     = 0;
0113     int column                     = parent->sortColumn();
0114     Qt::SortOrder currentSortOrder = (Qt::SortOrder)parent->sortOrder();
0115 
0116     switch (column)
0117     {
0118         case ShowfotoStackViewList::FileSize:
0119         {
0120             result = (ShowfotoItemSortSettings::compareByOrder(data(ShowfotoStackViewList::FileSize, Qt::UserRole).toInt(),
0121                                                                other.data(ShowfotoStackViewList::FileSize, Qt::UserRole).toInt(),
0122                                                                currentSortOrder));
0123             break;
0124         }
0125 
0126         case ShowfotoStackViewList::FileType:
0127         {
0128             result = (ShowfotoItemSortSettings::naturalCompare(text(ShowfotoStackViewList::FileType),
0129                                                                other.text(ShowfotoStackViewList::FileType),
0130                                                                currentSortOrder,
0131                                                                Qt::CaseSensitive));
0132             break;
0133         }
0134 
0135         case ShowfotoStackViewList::FileDate:
0136         {
0137             result = (ShowfotoItemSortSettings::compareByOrder(data(ShowfotoStackViewList::FileDate, Qt::UserRole).toDateTime(),
0138                                                                other.data(ShowfotoStackViewList::FileDate, Qt::UserRole).toDateTime(),
0139                                                                currentSortOrder));
0140             break;
0141         }
0142 
0143         default:    // ShowfotoStackViewList::FileName
0144         {
0145             result = (ShowfotoItemSortSettings::naturalCompare(text(ShowfotoStackViewList::FileName),
0146                                                                other.text(ShowfotoStackViewList::FileName),
0147                                                                currentSortOrder,
0148                                                                Qt::CaseSensitive));
0149             break;
0150         }
0151     }
0152 
0153     return (result < 0);
0154 }
0155 
0156 } // namespace ShowFoto