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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2013-05-13
0007  * Description : Table view column helpers: Audio/video 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_audiovideo.h"
0017 
0018 // Qt includes
0019 
0020 #include <QLocale>
0021 
0022 // KDE includes
0023 
0024 #include <klocalizedstring.h>
0025 
0026 // Local includes
0027 
0028 #include "digikam_debug.h"
0029 #include "coredbinfocontainers.h"
0030 #include "dmetadata.h"
0031 #include "iteminfo.h"
0032 
0033 namespace Digikam
0034 {
0035 
0036 namespace TableViewColumns
0037 {
0038 
0039 ColumnAudioVideoProperties::ColumnAudioVideoProperties(TableViewShared* const tableViewShared,
0040                                                        const TableViewColumnConfiguration& pConfiguration,
0041                                                        const SubColumn pSubColumn,
0042                                                        QObject* const parent)
0043     : TableViewColumn(tableViewShared, pConfiguration, parent),
0044       subColumn      (pSubColumn)
0045 {
0046 }
0047 
0048 ColumnAudioVideoProperties::~ColumnAudioVideoProperties()
0049 {
0050 }
0051 
0052 QStringList ColumnAudioVideoProperties::getSubColumns()
0053 {
0054     QStringList columns;
0055     columns << QLatin1String("audiobitrate")
0056             << QLatin1String("audiochanneltype")
0057             << QLatin1String("audioCodec")
0058             << QLatin1String("duration")
0059             << QLatin1String("framerate")
0060             << QLatin1String("videocodec");
0061 
0062     return columns;
0063 }
0064 
0065 TableViewColumnDescription ColumnAudioVideoProperties::getDescription()
0066 {
0067     TableViewColumnDescription description(QLatin1String("audiovideo-properties"), i18n("Audio/video properties"));
0068     description.setIcon(QLatin1String("video-x-generic"));
0069 
0070     description.addSubColumn(TableViewColumnDescription(QLatin1String("audiobitrate"),     i18n("Audio bitrate")));
0071     description.addSubColumn(TableViewColumnDescription(QLatin1String("audiochanneltype"), i18n("Audio channel type")));
0072     description.addSubColumn(TableViewColumnDescription(QLatin1String("audioCodec"),       i18n("Audio Codec")));
0073     description.addSubColumn(TableViewColumnDescription(QLatin1String("duration"),         i18n("Duration")));
0074     description.addSubColumn(TableViewColumnDescription(QLatin1String("framerate"),        i18n("Frame rate")));
0075     description.addSubColumn(TableViewColumnDescription(QLatin1String("videocodec"),       i18n("Video codec")));
0076 
0077     return description;
0078 }
0079 
0080 QString ColumnAudioVideoProperties::getTitle() const
0081 {
0082     switch (subColumn)
0083     {
0084         case SubColumnAudioBitRate:
0085         {
0086             return i18n("Audio bitrate");
0087         }
0088 
0089         case SubColumnAudioChannelType:
0090         {
0091             return i18n("Audio channel type");
0092         }
0093 
0094         case SubColumnAudioCodec:
0095         {
0096             return i18n("Audio Codec");
0097         }
0098 
0099         case SubColumnDuration:
0100         {
0101             return i18n("Duration");
0102         }
0103 
0104         case SubColumnFrameRate:
0105         {
0106             return i18n("Frame rate");
0107         }
0108 
0109         case SubColumnVideoCodec:
0110         {
0111             return i18n("Video codec");
0112         }
0113     }
0114 
0115     return QString();
0116 }
0117 
0118 TableViewColumn::ColumnFlags ColumnAudioVideoProperties::getColumnFlags() const
0119 {
0120     ColumnFlags flags(ColumnNoFlags);
0121 
0122     /// @todo AudioChannelType contains "Mono" or "2", have to check for custom sorting
0123 
0124     if (
0125         (subColumn == SubColumnAudioBitRate) ||
0126         (subColumn == SubColumnDuration)     ||
0127         (subColumn == SubColumnFrameRate)
0128        )
0129     {
0130         flags |= ColumnCustomSorting;
0131     }
0132 
0133     return flags;
0134 }
0135 
0136 QVariant ColumnAudioVideoProperties::data(TableViewModel::Item* const item, const int role) const
0137 {
0138     if (role != Qt::DisplayRole)
0139     {
0140         return QVariant();
0141     }
0142 
0143     switch (subColumn)
0144     {
0145         case SubColumnAudioBitRate:
0146         {
0147             bool ok;
0148             const int audioBitRate = s->tableViewModel->itemDatabaseFieldRaw(item,
0149                                      DatabaseFields::Set(DatabaseFields::AudioBitRate)).toInt(&ok);
0150 
0151             if (!ok)
0152             {
0153                 return QString();
0154             }
0155 
0156             const QString audioBitRateString = QLocale().toString(audioBitRate);
0157 
0158             return audioBitRateString;
0159         }
0160 
0161         case SubColumnAudioChannelType:
0162         {
0163             const QString audioChannelType = s->tableViewModel->itemDatabaseFieldRaw(item,
0164                                              DatabaseFields::Set(DatabaseFields::AudioChannelType)).toString();
0165             return audioChannelType;
0166         }
0167 
0168         case SubColumnAudioCodec:
0169         {
0170             const QString audioCodec = s->tableViewModel->itemDatabaseFieldRaw(item,
0171                                        DatabaseFields::Set(DatabaseFields::AudioCodec)).toString();
0172             return audioCodec;
0173         }
0174 
0175         case SubColumnDuration:
0176         {
0177             // duration is in milliseconds
0178 
0179             const QString duration = s->tableViewModel->itemDatabaseFieldRaw(item,
0180                                      DatabaseFields::Set(DatabaseFields::Duration)).toString();
0181             QString durationString = duration;
0182             bool ok                = false;
0183             const int durationVal  = duration.toInt(&ok);
0184 
0185             if (ok)
0186             {
0187                 unsigned int r, d, h, m, s, f;
0188                 r = qAbs(durationVal);
0189                 d = r / 86400000;
0190                 r = r % 86400000;
0191                 h = r / 3600000;
0192                 r = r % 3600000;
0193                 m = r / 60000;
0194                 r = r % 60000;
0195                 s = r / 1000;
0196                 f = r % 1000;
0197 
0198                 durationString = QString().asprintf("%d.%02d:%02d:%02d.%03d", d, h, m, s, f);
0199             }
0200 
0201             return durationString;
0202         }
0203 
0204         case SubColumnFrameRate:
0205         {
0206             bool ok;
0207             const double frameRate = s->tableViewModel->itemDatabaseFieldRaw(item,
0208                                      DatabaseFields::Set(DatabaseFields::FrameRate)).toDouble(&ok);
0209 
0210             if (!ok)
0211             {
0212                 return QString();
0213             }
0214 
0215             const QString frameRateString = QLocale().toString(frameRate);
0216 
0217             return frameRateString;
0218         }
0219 
0220         case SubColumnVideoCodec:
0221         {
0222             const QString videoCodec = s->tableViewModel->itemDatabaseFieldRaw(item,
0223                                        DatabaseFields::Set(DatabaseFields::VideoCodec)).toString();
0224             return videoCodec;
0225         }
0226     }
0227 
0228     return QVariant();
0229 }
0230 
0231 TableViewColumn::ColumnCompareResult ColumnAudioVideoProperties::compare(TableViewModel::Item* const itemA,
0232                                                                          TableViewModel::Item* const itemB) const
0233 {
0234     /// @todo All the values used here are actually returned as strings in the QVariants, but should be stored as int/double
0235 
0236     switch (subColumn)
0237     {
0238         case SubColumnAudioBitRate:
0239         {
0240             const QVariant variantA = s->tableViewModel->itemDatabaseFieldRaw(itemA,
0241                                       DatabaseFields::Set(DatabaseFields::AudioBitRate));
0242             const QVariant variantB = s->tableViewModel->itemDatabaseFieldRaw(itemB,
0243                                       DatabaseFields::Set(DatabaseFields::AudioBitRate));
0244             bool okA;
0245             const int audioBitRateA = variantA.toInt(&okA);
0246             bool okB;
0247             const int audioBitRateB = variantB.toDouble(&okB);
0248 
0249             ColumnCompareResult result;
0250 
0251             if (!compareHelperBoolFailCheck(okA, okB, &result))
0252             {
0253                 return result;
0254             }
0255 
0256             return compareHelper<int>(audioBitRateA, audioBitRateB);
0257         }
0258 
0259         case SubColumnDuration:
0260         {
0261             const QVariant variantA = s->tableViewModel->itemDatabaseFieldRaw(itemA,
0262                                       DatabaseFields::Set(DatabaseFields::Duration));
0263             const QVariant variantB = s->tableViewModel->itemDatabaseFieldRaw(itemB,
0264                                       DatabaseFields::Set(DatabaseFields::Duration));
0265             bool okA;
0266             const double durationA = variantA.toDouble(&okA);
0267             bool okB;
0268             const double durationB = variantB.toDouble(&okB);
0269 
0270             ColumnCompareResult result;
0271 
0272             if (!compareHelperBoolFailCheck(okA, okB, &result))
0273             {
0274                 return result;
0275             }
0276 
0277             return compareHelper<double>(durationA, durationB);
0278         }
0279 
0280         case SubColumnFrameRate:
0281         {
0282             const QVariant variantA = s->tableViewModel->itemDatabaseFieldRaw(itemA,
0283                                       DatabaseFields::Set(DatabaseFields::FrameRate));
0284             const QVariant variantB = s->tableViewModel->itemDatabaseFieldRaw(itemB,
0285                                       DatabaseFields::Set(DatabaseFields::FrameRate));
0286             bool okA;
0287             const double frameRateA = variantA.toDouble(&okA);
0288             bool okB;
0289             const double frameRateB = variantB.toDouble(&okB);
0290 
0291             ColumnCompareResult result;
0292 
0293             if (!compareHelperBoolFailCheck(okA, okB, &result))
0294             {
0295                 return result;
0296             }
0297 
0298             return compareHelper<double>(frameRateA, frameRateB);
0299         }
0300 
0301         default:
0302         {
0303             qCWarning(DIGIKAM_GENERAL_LOG) << "item: unimplemented comparison, subColumn=" << subColumn;
0304 
0305             return CmpEqual;
0306         }
0307     }
0308 }
0309 
0310 void ColumnAudioVideoProperties::setConfiguration(const TableViewColumnConfiguration& newConfiguration)
0311 {
0312     configuration = newConfiguration;
0313 
0314     Q_EMIT signalAllDataChanged();
0315 }
0316 
0317 } // namespace TableViewColumns
0318 
0319 } // namespace Digikam
0320 
0321 #include "moc_tableview_column_audiovideo.cpp"