File indexing completed on 2025-01-19 03:50:48

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2013-02-25
0007  * Description : Table view column helpers: File properties
0008  *
0009  * SPDX-FileCopyrightText: 2017-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2013      by Michael G. Hansen <mike at mghansen dot de>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "tableview_column_file.h"
0017 
0018 // Qt includes
0019 
0020 #include <QFormLayout>
0021 #include <QComboBox>
0022 #include <QLocale>
0023 #include <QDir>
0024 
0025 // KDE includes
0026 
0027 #include <klocalizedstring.h>
0028 
0029 // Local includes
0030 
0031 #include "digikam_debug.h"
0032 #include "itempropertiestab.h"
0033 #include "iteminfo.h"
0034 
0035 namespace Digikam
0036 {
0037 
0038 namespace TableViewColumns
0039 {
0040 
0041 ColumnFileProperties::ColumnFileProperties(TableViewShared* const tableViewShared,
0042                                            const TableViewColumnConfiguration& pConfiguration,
0043                                            const SubColumn pSubColumn,
0044                                            QObject* const parent)
0045     : TableViewColumn(tableViewShared, pConfiguration, parent),
0046       subColumn      (pSubColumn)
0047 {
0048 }
0049 
0050 TableViewColumnDescription ColumnFileProperties::getDescription()
0051 {
0052     TableViewColumnDescription description(QLatin1String("file-properties"), i18n("File properties"));
0053     description.setIcon(QLatin1String("dialog-information"));
0054 
0055     description.addSubColumn(TableViewColumnDescription(QLatin1String("filename"),         i18n("Filename")));
0056     description.addSubColumn(TableViewColumnDescription(QLatin1String("filepath"),         i18n("Path")));
0057     description.addSubColumn(TableViewColumnDescription(QLatin1String("filesize"),         i18n("Size")));
0058     description.addSubColumn(TableViewColumnDescription(QLatin1String("filelastmodified"), i18n("Last modified")));
0059 
0060     return description;
0061 }
0062 
0063 QStringList ColumnFileProperties::getSubColumns()
0064 {
0065     QStringList columns;
0066     columns << QLatin1String("filename")
0067             << QLatin1String("filepath")
0068             << QLatin1String("filesize")
0069             << QLatin1String("filelastmodified");
0070 
0071     return columns;
0072 }
0073 
0074 QString ColumnFileProperties::getTitle() const
0075 {
0076     switch (subColumn)
0077     {
0078         case SubColumnName:
0079         {
0080             return i18n("Filename");
0081         }
0082 
0083         case SubColumnFilePath:
0084         {
0085             return i18n("Path");
0086         }
0087 
0088         case SubColumnSize:
0089         {
0090             return i18n("Size");
0091         }
0092 
0093         case SubColumnLastModified:
0094         {
0095             return i18n("Last modified");
0096         }
0097     }
0098 
0099     return QString();
0100 }
0101 
0102 TableViewColumn::ColumnFlags ColumnFileProperties::getColumnFlags() const
0103 {
0104     if (
0105         (subColumn == SubColumnSize) ||
0106         (subColumn == SubColumnLastModified)
0107        )
0108     {
0109         return (ColumnCustomSorting | ColumnHasConfigurationWidget);
0110     }
0111 
0112     return ColumnNoFlags;
0113 }
0114 
0115 QVariant ColumnFileProperties::data(TableViewModel::Item* const item, const int role) const
0116 {
0117     if (
0118         (role != Qt::DisplayRole) &&
0119         (role != Qt::TextAlignmentRole)
0120        )
0121     {
0122         return QVariant();
0123     }
0124 
0125     if (role == Qt::TextAlignmentRole)
0126     {
0127         switch (subColumn)
0128         {
0129             case SubColumnSize:
0130             {
0131                 return QVariant(Qt::Alignment(Qt::AlignRight | Qt::AlignVCenter));
0132             }
0133 
0134             default:
0135             {
0136                 return QVariant();
0137             }
0138         }
0139     }
0140 
0141     const ItemInfo info = s->tableViewModel->infoFromItem(item);
0142 
0143     switch (subColumn)
0144     {
0145         case SubColumnName:
0146         {
0147             return info.fileUrl().fileName();
0148         }
0149 
0150         case SubColumnFilePath:
0151         {
0152             return QDir::toNativeSeparators(info.fileUrl().toLocalFile());
0153         }
0154 
0155         case SubColumnSize:
0156         {
0157             /// @todo Add configuration options for SI-prefixes
0158             /// @todo Use an enum instead to avoid lots of string comparisons
0159 
0160             const QString formatKey = configuration.getSetting(QLatin1String("format"), QLatin1String("human"));
0161 
0162             if (formatKey == QLatin1String("human"))
0163             {
0164                 return ItemPropertiesTab::humanReadableBytesCount(info.fileSize());
0165             }
0166             else
0167             {
0168                 // formatKey == "plain"
0169 
0170                 return QLocale().toString(info.fileSize());
0171             }
0172 
0173             break;
0174         }
0175 
0176         case SubColumnLastModified:
0177         {
0178             const QDateTime lastModifiedTime = info.modDateTime();
0179 
0180             return QLocale().toString(lastModifiedTime, QLocale::ShortFormat);
0181         }
0182     }
0183 
0184     return QVariant();
0185 }
0186 
0187 TableViewColumn::ColumnCompareResult ColumnFileProperties::compare(TableViewModel::Item* const itemA, TableViewModel::Item* const itemB) const
0188 {
0189     const ItemInfo infoA = s->tableViewModel->infoFromItem(itemA);
0190     const ItemInfo infoB = s->tableViewModel->infoFromItem(itemB);
0191 
0192     switch (subColumn)
0193     {
0194         case SubColumnSize:
0195         {
0196             const int sizeA = infoA.fileSize();
0197             const int sizeB = infoB.fileSize();
0198 
0199             return compareHelper<int>(sizeA, sizeB);
0200         }
0201 
0202         case SubColumnLastModified:
0203         {
0204             const QDateTime dtA = infoA.modDateTime();
0205             const QDateTime dtB = infoB.modDateTime();
0206 
0207             return compareHelper<QDateTime>(dtA, dtB);
0208         }
0209 
0210         default:
0211         {
0212             qCWarning(DIGIKAM_GENERAL_LOG) << "file: unimplemented comparison, subColumn=" << subColumn;
0213 
0214             return CmpEqual;
0215         }
0216     }
0217 }
0218 
0219 // ---------------------------------------------------------------------------------------
0220 
0221 ColumnFileConfigurationWidget::ColumnFileConfigurationWidget(TableViewShared* const sharedObject,
0222                                                              const TableViewColumnConfiguration& columnConfiguration,
0223                                                              QWidget* const parentWidget)
0224     : TableViewColumnConfigurationWidget(sharedObject, columnConfiguration, parentWidget),
0225       subColumn(ColumnFileProperties::SubColumnName),
0226       selectorSizeType(nullptr)
0227 {
0228     ColumnFileProperties::getSubColumnIndex<ColumnFileProperties>(configuration.columnId, &subColumn);
0229 
0230     switch (subColumn)
0231     {
0232         case ColumnFileProperties::SubColumnSize:
0233         {
0234             QFormLayout* const box1 = new QFormLayout();
0235             selectorSizeType        = new QComboBox(this);
0236             selectorSizeType->addItem(i18nc("@item: tableview", "Human readable"), QLatin1String("human"));
0237             selectorSizeType->addItem(i18nc("@item: tableview", "Plain"),          QLatin1String("plain"));
0238             box1->addRow(i18n("Display format"), selectorSizeType);
0239 
0240             setLayout(box1);
0241 
0242             const int index = selectorSizeType->findData(configuration.getSetting(QLatin1String("format"), QLatin1String("human")));
0243             selectorSizeType->setCurrentIndex((index >= 0) ? index : 0);
0244             break;
0245         }
0246 
0247         default:
0248         {
0249             break;
0250         }
0251     }
0252 }
0253 
0254 ColumnFileConfigurationWidget::~ColumnFileConfigurationWidget()
0255 {
0256 }
0257 
0258 TableViewColumnConfiguration ColumnFileConfigurationWidget::getNewConfiguration()
0259 {
0260     const QString formatKey = selectorSizeType->itemData(selectorSizeType->currentIndex()).toString();
0261     configuration.columnSettings.insert(QLatin1String("format"), formatKey);
0262 
0263     return configuration;
0264 }
0265 
0266 void ColumnFileProperties::setConfiguration(const TableViewColumnConfiguration& newConfiguration)
0267 {
0268     configuration = newConfiguration;
0269 
0270     Q_EMIT signalAllDataChanged();
0271 }
0272 
0273 TableViewColumnConfigurationWidget* ColumnFileProperties::getConfigurationWidget(QWidget* const parentWidget) const
0274 {
0275     return new ColumnFileConfigurationWidget(s, configuration, parentWidget);
0276 }
0277 
0278 } // namespace TableViewColumns
0279 
0280 } // namespace Digikam
0281 
0282 
0283 #include "moc_tableview_column_file.cpp"