1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2021-09-27
 * Description : Showfoto stack view item
 *
 * SPDX-FileCopyrightText: 2021-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "showfotostackviewitem.h"

// Qt includes

#include <QIcon>
#include <QPainter>
#include <QLocale>
#include <QDateTime>
#include <QMimeDatabase>
#include <QFileInfo>

// KDE includes

#include <klocalizedstring.h>

// Local include

#include "digikam_debug.h"
#include "digikam_globals.h"
#include "showfotostackviewlist.h"
#include "showfotoitemsortsettings.h"
#include "itempropertiestab.h"
#include "drawdecoder.h"

using namespace Digikam;

namespace ShowFoto
{

ShowfotoStackViewItem::ShowfotoStackViewItem(ShowfotoStackViewList* const parent)
    : QTreeWidgetItem(parent)
{
    setDisabled(false);
    setSelected(false);
}

void ShowfotoStackViewItem::setInfo(const ShowfotoItemInfo& info)<--- Shadow argument
{
    m_info                 = info;
    setText(ShowfotoStackViewList::FileName, m_info.name);

    QDateTime dt           = (m_info.ctime.isValid() ? m_info.ctime : m_info.dtime);
    QString str            = asUserDateTime(dt);
    setText(ShowfotoStackViewList::FileDate, str);
    setData(ShowfotoStackViewList::FileDate, Qt::UserRole, dt);

    QFileInfo fileInfo(m_info.name);
    QString rawFilesExt    = DRawDecoder::rawFiles();
    QString ext            = fileInfo.suffix().toUpper();

    if (!ext.isEmpty() && rawFilesExt.toUpper().contains(ext))
    {
        setText(ShowfotoStackViewList::FileType, i18nc("@info: item properties", "RAW Image"));
    }
    else
    {
        setText(ShowfotoStackViewList::FileType, QMimeDatabase().mimeTypeForFile(fileInfo).comment());
    }

    str                    = ItemPropertiesTab::humanReadableBytesCount(m_info.size);
    setText(ShowfotoStackViewList::FileSize, str);
    setData(ShowfotoStackViewList::FileSize, Qt::UserRole, info.size);
}

ShowfotoItemInfo ShowfotoStackViewItem::info() const
{
    return m_info;
}

void ShowfotoStackViewItem::setThumbnail(const QPixmap& thumb)
{
    QPixmap pix = thumb.scaled(treeWidget()->iconSize(), Qt::KeepAspectRatio,
                                                         Qt::FastTransformation);

    QPixmap icon(treeWidget()->iconSize());
    icon.fill(Qt::transparent);
    QPainter p(&icon);
    p.drawPixmap((icon.width()  - pix.width() ) / 2,
                 (icon.height() - pix.height()) / 2,
                 pix);

    setIcon(0, icon);
}

bool ShowfotoStackViewItem::operator<(const QTreeWidgetItem& other) const
{
    ShowfotoStackViewList* const parent = dynamic_cast<ShowfotoStackViewList*>(treeWidget());

    if (!parent)
    {
        return false;
    }

    int result                     = 0;
    int column                     = parent->sortColumn();
    Qt::SortOrder currentSortOrder = (Qt::SortOrder)parent->sortOrder();

    switch (column)
    {
        case ShowfotoStackViewList::FileSize:
        {
            result = (ShowfotoItemSortSettings::compareByOrder(data(ShowfotoStackViewList::FileSize, Qt::UserRole).toInt(),
                                                               other.data(ShowfotoStackViewList::FileSize, Qt::UserRole).toInt(),
                                                               currentSortOrder));
            break;
        }

        case ShowfotoStackViewList::FileType:
        {
            result = (ShowfotoItemSortSettings::naturalCompare(text(ShowfotoStackViewList::FileType),
                                                               other.text(ShowfotoStackViewList::FileType),
                                                               currentSortOrder,
                                                               Qt::CaseSensitive));
            break;
        }

        case ShowfotoStackViewList::FileDate:
        {
            result = (ShowfotoItemSortSettings::compareByOrder(data(ShowfotoStackViewList::FileDate, Qt::UserRole).toDateTime(),
                                                               other.data(ShowfotoStackViewList::FileDate, Qt::UserRole).toDateTime(),
                                                               currentSortOrder));
            break;
        }

        default:    // ShowfotoStackViewList::FileName
        {
            result = (ShowfotoItemSortSettings::naturalCompare(text(ShowfotoStackViewList::FileName),
                                                               other.text(ShowfotoStackViewList::FileName),
                                                               currentSortOrder,
                                                               Qt::CaseSensitive));
            break;
        }
    }

    return (result < 0);
}

} // namespace ShowFoto