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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2013-02-12
0007  * Description : Table view column helpers
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_columnfactory.h"
0017 
0018 // KDE include
0019 
0020 #include <kconfiggroup.h>
0021 
0022 // Local includes
0023 
0024 #include "digikam_debug.h"
0025 #include "itemfiltermodel.h"
0026 #include "iteminfo.h"
0027 #include "tableview_columns.h"
0028 
0029 namespace Digikam
0030 {
0031 
0032 TableViewColumn::TableViewColumn(TableViewShared* const tableViewShared,
0033                                  const TableViewColumnConfiguration& pConfiguration,
0034                                  QObject* const parent)
0035     : QObject      (parent),
0036       s            (tableViewShared),
0037       configuration(pConfiguration)
0038 {
0039 }
0040 
0041 TableViewColumn::~TableViewColumn()
0042 {
0043 }
0044 
0045 // ---------------------------------------------------------------------------------------------
0046 
0047 TableViewColumnFactory::TableViewColumnFactory(TableViewShared* const tableViewShared,
0048                                                QWidget* const parent)
0049     : QObject        (parent),
0050       m_displayWidget(parent),
0051       s              (tableViewShared)
0052 {
0053 }
0054 
0055 TableViewColumn* TableViewColumnFactory::getColumn(const Digikam::TableViewColumnConfiguration& columnConfiguration)
0056 {
0057     TableViewColumn* newColumn = nullptr;
0058 
0059     if (TableViewColumns::ColumnThumbnail::CreateFromConfiguration(s, columnConfiguration, &newColumn, m_displayWidget))
0060     {
0061         return newColumn;
0062     }
0063 
0064     if (TableViewColumns::ColumnDigikamProperties::CreateFromConfiguration<TableViewColumns::ColumnDigikamProperties>(s, columnConfiguration, &newColumn, this))
0065     {
0066         return newColumn;
0067     }
0068 
0069     if (TableViewColumns::ColumnPhotoProperties::CreateFromConfiguration<TableViewColumns::ColumnPhotoProperties>(s, columnConfiguration, &newColumn, this))
0070     {
0071         return newColumn;
0072     }
0073 
0074     if (TableViewColumns::ColumnFileProperties::CreateFromConfiguration<TableViewColumns::ColumnFileProperties>(s, columnConfiguration, &newColumn, this))
0075     {
0076         return newColumn;
0077     }
0078 
0079     if (TableViewColumns::ColumnGeoProperties::CreateFromConfiguration<TableViewColumns::ColumnGeoProperties>(s, columnConfiguration, &newColumn, this))
0080     {
0081         return newColumn;
0082     }
0083 
0084     if (TableViewColumns::ColumnItemProperties::CreateFromConfiguration<TableViewColumns::ColumnItemProperties>(s, columnConfiguration, &newColumn, this))
0085     {
0086         return newColumn;
0087     }
0088 
0089     if (TableViewColumns::ColumnAudioVideoProperties::CreateFromConfiguration<TableViewColumns::ColumnAudioVideoProperties>(s, columnConfiguration, &newColumn, this))
0090     {
0091         return newColumn;
0092     }
0093 
0094     return nullptr;
0095 }
0096 
0097 QList<TableViewColumnDescription> TableViewColumnFactory::getColumnDescriptionList()
0098 {
0099     QList<TableViewColumnDescription> descriptionList;
0100 
0101     descriptionList << TableViewColumns::ColumnThumbnail::getDescription();
0102     descriptionList << TableViewColumns::ColumnFileProperties::getDescription();
0103     descriptionList << TableViewColumns::ColumnItemProperties::getDescription();
0104     descriptionList << TableViewColumns::ColumnPhotoProperties::getDescription();
0105     descriptionList << TableViewColumns::ColumnDigikamProperties::getDescription();
0106     descriptionList << TableViewColumns::ColumnGeoProperties::getDescription();
0107     descriptionList << TableViewColumns::ColumnAudioVideoProperties::getDescription();
0108 
0109     return descriptionList;
0110 }
0111 
0112 // ---------------------------------------------------------------------------------------------
0113 
0114 bool TableViewColumn::paint(QPainter* const painter, const QStyleOptionViewItem& option, TableViewModel::Item* const item) const
0115 {
0116     Q_UNUSED(painter)
0117     Q_UNUSED(option)
0118     Q_UNUSED(item)
0119 
0120     return false;
0121 }
0122 
0123 QSize TableViewColumn::sizeHint(const QStyleOptionViewItem& option, TableViewModel::Item* const item) const
0124 {
0125     Q_UNUSED(option)
0126     Q_UNUSED(item)
0127 
0128     return QSize();
0129 }
0130 
0131 QVariant TableViewColumn::data(TableViewModel::Item* const item, const int role) const
0132 {
0133     Q_UNUSED(item)
0134     Q_UNUSED(role)
0135 
0136     return QVariant();
0137 }
0138 
0139 TableViewColumn::ColumnFlags TableViewColumn::getColumnFlags() const
0140 {
0141     return ColumnNoFlags;
0142 }
0143 
0144 TableViewColumnConfiguration TableViewColumn::getConfiguration() const
0145 {
0146     return configuration;
0147 }
0148 
0149 /**
0150  * This function should never be called, because subclasses have to do the comparison on their own. But it can not be
0151  * pure, since then every subclass which does not do custom comparison would have to implement an empty stub.
0152  */
0153 TableViewColumn::ColumnCompareResult TableViewColumn::compare(TableViewModel::Item* const itemA, TableViewModel::Item* const itemB) const
0154 {
0155     Q_UNUSED(itemA)
0156     Q_UNUSED(itemB)
0157 
0158     qCWarning(DIGIKAM_GENERAL_LOG) << "Unimplemented custom comparison. Make sure getColumnFlags() does not return ColumnCustomSorting.";
0159 
0160     return CmpEqual;
0161 }
0162 
0163 TableViewColumnConfigurationWidget* TableViewColumn::getConfigurationWidget(QWidget* const parentWidget) const
0164 {
0165     Q_UNUSED(parentWidget)
0166 
0167     return nullptr;
0168 }
0169 
0170 void TableViewColumn::setConfiguration(const TableViewColumnConfiguration& newConfiguration)
0171 {
0172     Q_UNUSED(newConfiguration)
0173 }
0174 
0175 void TableViewColumn::updateThumbnailSize()
0176 {
0177 }
0178 
0179 bool TableViewColumn::compareHelperBoolFailCheck(const bool okA, const bool okB, ColumnCompareResult* const result)
0180 {
0181     if (okA && okB)
0182     {
0183         return true;
0184     }
0185 
0186     if (okA && !okB)
0187     {
0188         *result = CmpABiggerB;
0189 
0190         return false;
0191     }
0192 
0193     if (okB && !okA)
0194     {
0195         *result = CmpALessB;
0196 
0197         return false;
0198     }
0199 
0200     *result = CmpEqual;
0201 
0202     return false;
0203 }
0204 
0205 bool TableViewColumn::columnAffectedByChangeset(const ImageChangeset& /*imageChangeset*/) const
0206 {
0207     return true;
0208 }
0209 
0210 // ---------------------------------------------------------------------------------------------
0211 
0212 TableViewColumnProfile::TableViewColumnProfile()
0213 {
0214 }
0215 
0216 TableViewColumnProfile::~TableViewColumnProfile()
0217 {
0218 }
0219 
0220 void TableViewColumnProfile::loadSettings(const KConfigGroup& configGroup)
0221 {
0222     name               = configGroup.readEntry("Profile Name", QString());
0223     headerState        = configGroup.readEntry("Header State", QByteArray());
0224     const int nColumns = configGroup.readEntry("Column Count", int(0));
0225 
0226     for (int i = 0 ; i < nColumns ; ++i)
0227     {
0228         /// @todo check for invalid column configurations
0229 
0230         const QString configSubGroupName = QString::fromUtf8("Column %1").arg(i);
0231         const KConfigGroup subGroup      = configGroup.group(configSubGroupName);
0232 
0233         /// @todo move loading into TableViewColumnConfiguration
0234 
0235         TableViewColumnConfiguration columnConfiguration;
0236         columnConfiguration.loadSettings(subGroup);
0237 
0238         columnConfigurationList << columnConfiguration;
0239     }
0240 
0241     if (columnConfigurationList.isEmpty())
0242     {
0243         // no data loaded, create default entries
0244 
0245         /// @todo Set the default sorting column to something other than the thumbnail.
0246 
0247         TableViewColumnDescription::List allColumns = TableViewColumnFactory::getColumnDescriptionList();
0248 
0249         TableViewColumnDescription nextDesc;
0250 
0251         if (TableViewColumnDescription::FindInListById(allColumns, QLatin1String("thumbnail"), &nextDesc))
0252         {
0253             columnConfigurationList << nextDesc.toConfiguration();
0254         }
0255 
0256         if (TableViewColumnDescription::FindInListById(allColumns, QLatin1String("filename"), &nextDesc))
0257         {
0258             columnConfigurationList << nextDesc.toConfiguration();
0259         }
0260 
0261         if (TableViewColumnDescription::FindInListById(allColumns, QLatin1String("digikam-rating"), &nextDesc))
0262         {
0263             columnConfigurationList << nextDesc.toConfiguration();
0264         }
0265 
0266         if (TableViewColumnDescription::FindInListById(allColumns, QLatin1String("itemcreationdatetime"), &nextDesc))
0267         {
0268             columnConfigurationList << nextDesc.toConfiguration();
0269         }
0270 
0271         if (TableViewColumnDescription::FindInListById(allColumns, QLatin1String("digikam-title"), &nextDesc))
0272         {
0273             columnConfigurationList << nextDesc.toConfiguration();
0274         }
0275 
0276         if (TableViewColumnDescription::FindInListById(allColumns, QLatin1String("digikam-tags"), &nextDesc))
0277         {
0278             columnConfigurationList << nextDesc.toConfiguration();
0279         }
0280     }
0281 }
0282 
0283 void TableViewColumnProfile::saveSettings(KConfigGroup& configGroup)
0284 {
0285     configGroup.writeEntry(QLatin1String("Profile Name"), name);
0286     const int nColumns = columnConfigurationList.count();
0287     configGroup.writeEntry(QLatin1String("Column Count"), nColumns);
0288     configGroup.writeEntry(QLatin1String("Header State"), headerState);
0289 
0290     for (int i = 0 ; i < nColumns ; ++i)
0291     {
0292         const QString configSubGroupName                        = QString::fromUtf8("Column %1").arg(i);
0293         KConfigGroup subGroup                                   = configGroup.group(configSubGroupName);
0294         const TableViewColumnConfiguration& columnConfiguration = columnConfigurationList.at(i);;
0295         columnConfiguration.saveSettings(subGroup);
0296     }
0297 }
0298 
0299 // ---------------------------------------------------------------------------------------------
0300 
0301 void TableViewColumnConfiguration::loadSettings(const KConfigGroup& configGroup)
0302 {
0303     columnId            = configGroup.readEntry(QLatin1String("Column Id"), QString());
0304     const int nSettings = configGroup.readEntry(QLatin1String("NSettings"), int(0));
0305 
0306     for (int i = 0 ; i < nSettings ; ++i)
0307     {
0308         const QString& key   = configGroup.readEntry(QString::fromUtf8("Key %1").arg(i),   QString());
0309         const QString& value = configGroup.readEntry(QString::fromUtf8("Value %1").arg(i), QString());
0310 
0311         if (!key.isEmpty())
0312         {
0313             columnSettings.insert(key, value);
0314         }
0315     }
0316 }
0317 
0318 void TableViewColumnConfiguration::saveSettings(KConfigGroup& configGroup) const
0319 {
0320     configGroup.writeEntry(QLatin1String("Column Id"), columnId);
0321 
0322     const int nSettings = columnSettings.count();
0323     configGroup.writeEntry(QLatin1String("NSettings"), nSettings);
0324 
0325     QHashIterator<QString, QString> settingsIterator(columnSettings);
0326 
0327     for (int i = 0 ; settingsIterator.hasNext() ; ++i)
0328     {
0329         settingsIterator.next();
0330         configGroup.writeEntry(QString::fromUtf8("Key %1").arg(i),   settingsIterator.key());
0331         configGroup.writeEntry(QString::fromUtf8("Value %1").arg(i), settingsIterator.value());
0332     }
0333 }
0334 
0335 // ---------------------------------------------------------------------------------------------
0336 
0337 TableViewColumnConfigurationWidget::TableViewColumnConfigurationWidget(TableViewShared* const sharedObject,
0338                                                                        const TableViewColumnConfiguration& currentConfiguration,
0339                                                                        QWidget* const parent)
0340     : QWidget(parent),
0341       s(sharedObject),
0342       configuration(currentConfiguration)
0343 {
0344 }
0345 
0346 TableViewColumnConfigurationWidget::~TableViewColumnConfigurationWidget()
0347 {
0348 }
0349 
0350 } // namespace Digikam
0351 
0352 #include "moc_tableview_columnfactory.cpp"