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-28
0007  * Description : Table view column helpers: Digikam 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_digikam.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 "coredbfields.h"
0030 #include "albummanager.h"
0031 #include "digikam_globals.h"
0032 #include "iteminfo.h"
0033 
0034 namespace Digikam
0035 {
0036 
0037 namespace TableViewColumns
0038 {
0039 
0040 ColumnDigikamProperties::ColumnDigikamProperties(TableViewShared* const tableViewShared,
0041                                                  const TableViewColumnConfiguration& pConfiguration,
0042                                                  const SubColumn pSubColumn,
0043                                                  QObject* const parent)
0044     : TableViewColumn(tableViewShared, pConfiguration, parent),
0045       subColumn      (pSubColumn)
0046 {
0047 }
0048 
0049 ColumnDigikamProperties::~ColumnDigikamProperties()
0050 {
0051 }
0052 
0053 QStringList ColumnDigikamProperties::getSubColumns()
0054 {
0055     QStringList columns;
0056     columns << QLatin1String("digikam-rating")
0057             << QLatin1String("digikam-picklabel")
0058             << QLatin1String("digikam-colorlabel")
0059             << QLatin1String("digikam-title")
0060             << QLatin1String("digikam-caption")
0061             << QLatin1String("digikam-tags");
0062 
0063     return columns;
0064 }
0065 
0066 TableViewColumnDescription ColumnDigikamProperties::getDescription()
0067 {
0068     TableViewColumnDescription description(QLatin1String("digikam-properties"), i18nc("@title: tableview", "digiKam properties"));
0069     description.setIcon(QLatin1String("edit-text-frame-update"));
0070 
0071     description.addSubColumn(TableViewColumnDescription(QLatin1String("digikam-rating"),     i18nc("@title: tableview", "Rating")).setIcon(QLatin1String("draw-star")));
0072     description.addSubColumn(TableViewColumnDescription(QLatin1String("digikam-picklabel"),  i18nc("@title: tableview", "Pick label")).setIcon(QLatin1String("flag")));
0073     description.addSubColumn(TableViewColumnDescription(QLatin1String("digikam-colorlabel"), i18nc("@title: tableview", "Color label")));
0074 
0075     /// @todo This column will show the 'default' title. Add a configuration dialog to choose different languages.
0076 
0077     description.addSubColumn(TableViewColumnDescription(QLatin1String("digikam-title"),      i18nc("@title: tableview", "Title")));
0078 
0079     /// @todo This column will show the 'default' caption. Add a configuration dialog to choose different languages.
0080 
0081     description.addSubColumn(TableViewColumnDescription(QLatin1String("digikam-caption"),    i18nc("@title: tableview", "Caption")));
0082 
0083     description.addSubColumn(TableViewColumnDescription(QLatin1String("digikam-tags"),       i18nc("@title: tableview", "Tags")).setIcon(QLatin1String("tag")));
0084 
0085     return description;
0086 }
0087 
0088 QString ColumnDigikamProperties::getTitle() const
0089 {
0090     switch (subColumn)
0091     {
0092         case SubColumnRating:
0093         {
0094             return i18nc("@info: tableview", "Rating");
0095         }
0096 
0097         case SubColumnPickLabel:
0098         {
0099             return i18nc("@info: tableview", "Pick label");
0100         }
0101 
0102         case SubColumnColorLabel:
0103         {
0104             return i18nc("@info: tableview", "Color label");
0105         }
0106 
0107         case SubColumnTitle:
0108         {
0109             return i18nc("@info: tableview", "Title");
0110         }
0111 
0112         case SubColumnCaption:
0113         {
0114             return i18nc("@info: tableview", "Caption");
0115         }
0116 
0117         case SubColumnTags:
0118         {
0119             return i18nc("@info: tableview", "Tags");
0120         }
0121     }
0122 
0123     return QString();
0124 }
0125 
0126 TableViewColumn::ColumnFlags ColumnDigikamProperties::getColumnFlags() const
0127 {
0128     ColumnFlags flags(ColumnNoFlags);
0129 
0130     if (
0131         (subColumn == SubColumnRating)    ||
0132         (subColumn == SubColumnPickLabel) ||
0133         (subColumn == SubColumnColorLabel)
0134        )
0135     {
0136         flags |= ColumnCustomSorting;
0137     }
0138 
0139     return flags;
0140 }
0141 
0142 QVariant ColumnDigikamProperties::data(TableViewModel::Item* const item, const int role) const
0143 {
0144     if (
0145         (role != Qt::DisplayRole)       &&
0146         (role != Qt::TextAlignmentRole) &&
0147         (role != Qt::ForegroundRole)
0148        )
0149     {
0150         return QVariant();
0151     }
0152 
0153     if (role == Qt::TextAlignmentRole)
0154     {
0155         switch (subColumn)
0156         {
0157             case SubColumnRating:
0158             {
0159                 return QVariant(Qt::Alignment(Qt::AlignCenter));
0160             }
0161 
0162             default:
0163             {
0164                 return QVariant();
0165             }
0166         }
0167     }
0168 
0169     if (role == Qt::ForegroundRole)
0170     {
0171         switch (subColumn)
0172         {
0173             case SubColumnPickLabel:
0174             {
0175                 const ItemInfo info       = s->tableViewModel->infoFromItem(item);
0176                 const PickLabel pickLabel = PickLabel(info.pickLabel());
0177                 QColor labelColor;
0178 
0179                 switch (pickLabel)
0180                 {
0181                     case NoPickLabel:
0182                     {
0183                         labelColor = Qt::darkGray;
0184                         break;
0185                     }
0186 
0187                     case RejectedLabel:
0188                     {
0189                         labelColor = Qt::red;
0190                         break;
0191                     }
0192 
0193                     case PendingLabel:
0194                     {
0195                         // yellow is too hard to read
0196 
0197                         labelColor = Qt::darkYellow;
0198                         break;
0199                     }
0200 
0201                     case AcceptedLabel:
0202                     {
0203                         // green is too hard to read
0204 
0205                         labelColor = Qt::darkGreen;
0206                         break;
0207                     }
0208 
0209                     default:
0210                     {
0211                         break;
0212                     }
0213                 }
0214 
0215                 QBrush labelBrush(labelColor);
0216 
0217                 return QVariant::fromValue(labelBrush);
0218             }
0219 
0220             case SubColumnColorLabel:
0221             {
0222                 const ItemInfo info        = s->tableViewModel->infoFromItem(item);
0223                 const ColorLabel colorLabel = ColorLabel(info.colorLabel());
0224                 QColor labelColor;
0225 
0226                 switch (colorLabel)
0227                 {
0228                     case NoColorLabel:
0229                     {
0230                         labelColor = Qt::lightGray;
0231                         break;
0232                     }
0233 
0234                     case RedLabel:
0235                     {
0236                         labelColor = Qt::red;
0237                         break;
0238                     }
0239 
0240                     case OrangeLabel:
0241                     {
0242                         labelColor = QColor(0xff, 0x80, 0x00);
0243                         break;
0244                     }
0245 
0246                     case YellowLabel:
0247                     {
0248                         labelColor = Qt::darkYellow;
0249                         break;
0250                     }
0251 
0252                     case GreenLabel:
0253                     {
0254                         labelColor = Qt::darkGreen;
0255                         break;
0256                     }
0257 
0258                     case BlueLabel:
0259                     {
0260                         labelColor = Qt::darkBlue;
0261                         break;
0262                     }
0263 
0264                     case MagentaLabel:
0265                     {
0266                         labelColor = Qt::magenta;
0267                         break;
0268                     }
0269 
0270                     case GrayLabel:
0271                     {
0272                         labelColor = Qt::darkGray;
0273                         break;
0274                     }
0275 
0276                     case BlackLabel:
0277                     {
0278                         labelColor = Qt::black;
0279                         break;
0280                     }
0281 
0282                     case WhiteLabel:
0283                     {
0284                         labelColor = Qt::white;
0285                         break;
0286                     }
0287 
0288                     default:
0289                     {
0290                         break;
0291                     }
0292                 }
0293 
0294                 QBrush labelBrush(labelColor);
0295 
0296                 return QVariant::fromValue(labelBrush);
0297             }
0298 
0299             default:
0300             {
0301                 return QVariant();
0302             }
0303         }
0304     }
0305 
0306     const ItemInfo info = s->tableViewModel->infoFromItem(item);
0307 
0308     /**
0309      * @todo Also display the pick label icon?
0310      *       Make display of text/icon configurable.
0311      */
0312 
0313     switch (subColumn)
0314     {
0315         case SubColumnRating:
0316         {
0317             const int itemRating = info.rating();
0318 
0319             if (itemRating <= 0)
0320             {
0321                 // no rating
0322 
0323                 return QString();
0324             }
0325 
0326             return QLocale().toString(itemRating);
0327         }
0328 
0329         case SubColumnPickLabel:
0330         {
0331             const PickLabel pickLabel = PickLabel(info.pickLabel());
0332             QString labelString;
0333 
0334             switch (pickLabel)
0335             {
0336                 case NoPickLabel:
0337                 {
0338                     labelString = i18nc("@info: tableview", "None");
0339                     break;
0340                 }
0341 
0342                 case RejectedLabel:
0343                 {
0344                     labelString = i18nc("@info: tableview", "Rejected");
0345                     break;
0346                 }
0347 
0348                 case PendingLabel:
0349                 {
0350                     labelString = i18nc("@info: tableview", "Pending");
0351                     break;
0352                 }
0353 
0354                 case AcceptedLabel:
0355                 {
0356                     labelString = i18nc("@info: tableview", "Accepted");
0357                     break;
0358                 }
0359 
0360                 default:
0361                 {
0362                     break;
0363                 }
0364             }
0365 
0366             return labelString;
0367         }
0368 
0369         case SubColumnColorLabel:
0370         {
0371             const ColorLabel colorLabel = ColorLabel(info.colorLabel());
0372             QString labelString;
0373 
0374             switch (colorLabel)
0375             {
0376                 case NoColorLabel:
0377                 {
0378                     labelString = i18nc("@info: tableview", "None");
0379                     break;
0380                 }
0381 
0382                 case RedLabel:
0383                 {
0384                     labelString = i18nc("@info: tableview", "Red");
0385                     break;
0386                 }
0387 
0388                 case OrangeLabel:
0389                 {
0390                     labelString = i18nc("@info: tableview", "Orange");
0391                     break;
0392                 }
0393 
0394                 case YellowLabel:
0395                 {
0396                     labelString = i18nc("@info: tableview", "Yellow");
0397                     break;
0398                 }
0399 
0400                 case GreenLabel:
0401                 {
0402                     labelString = i18nc("@info: tableview", "Green");
0403                     break;
0404                 }
0405 
0406                 case BlueLabel:
0407                 {
0408                     labelString = i18nc("@info: tableview", "Blue");
0409                     break;
0410                 }
0411 
0412                 case MagentaLabel:
0413                 {
0414                     labelString = i18nc("@info: tableview", "Magenta");
0415                     break;
0416                 }
0417 
0418                 case GrayLabel:
0419                 {
0420                     labelString = i18nc("@info: tableview", "Gray");
0421                     break;
0422                 }
0423 
0424                 case BlackLabel:
0425                 {
0426                     labelString = i18nc("@info: tableview", "Black");
0427                     break;
0428                 }
0429 
0430                 case WhiteLabel:
0431                 {
0432                     labelString = i18nc("@info: tableview", "White");
0433                     break;
0434                 }
0435 
0436                 default:
0437                 {
0438                     break;
0439                 }
0440             }
0441 
0442             return labelString;
0443         }
0444 
0445         case SubColumnTitle:
0446         {
0447             const QString title = info.title();
0448 
0449             return title;
0450         }
0451 
0452         case SubColumnCaption:
0453         {
0454             const QString caption = info.comment();
0455 
0456             return caption;
0457         }
0458 
0459         case SubColumnTags:
0460         {
0461             QStringList tagPaths = AlbumManager::instance()->tagPaths(info.tagIds(), false);
0462             tagPaths.sort();
0463 
0464             Q_FOREACH (TableViewColumn* const column, s->tableViewModel->getColumnObjects())
0465             {
0466                 if (column->getConfiguration().columnId == QLatin1String("thumbnail"))
0467                 {
0468                     return tagPaths.join(QLatin1Char('\n'));
0469                 }
0470             }
0471 
0472             return tagPaths.join(QLatin1String(", "));
0473         }
0474 
0475     }
0476 
0477     return QVariant();
0478 }
0479 
0480 TableViewColumn::ColumnCompareResult ColumnDigikamProperties::compare(TableViewModel::Item* const itemA,
0481                                                                       TableViewModel::Item* const itemB) const
0482 {
0483     const ItemInfo infoA = s->tableViewModel->infoFromItem(itemA);
0484     const ItemInfo infoB = s->tableViewModel->infoFromItem(itemB);
0485 
0486     switch (subColumn)
0487     {
0488         case SubColumnRating:
0489         {
0490             /// @todo Handle un-rated vs rated items differently?
0491 
0492             const int ratingA = infoA.rating();
0493             const int ratingB = infoB.rating();
0494 
0495             return compareHelper<int>(ratingA, ratingB);
0496         }
0497 
0498         case SubColumnPickLabel:
0499         {
0500             /// @todo Handle un-rated vs rated items differently?
0501 
0502             const int pickLabelA = infoA.pickLabel();
0503             const int pickLabelB = infoB.pickLabel();
0504 
0505             return compareHelper<int>(pickLabelA, pickLabelB);
0506         }
0507 
0508         case SubColumnColorLabel:
0509         {
0510             /// @todo Handle un-rated vs rated items differently?
0511 
0512             const int colorLabelA = infoA.colorLabel();
0513             const int colorLabelB = infoB.colorLabel();
0514 
0515             return compareHelper<int>(colorLabelA, colorLabelB);
0516         }
0517 
0518         default:
0519         {
0520             qCWarning(DIGIKAM_GENERAL_LOG) << "item: unimplemented comparison, subColumn=" << subColumn;
0521 
0522             return CmpEqual;
0523         }
0524     }
0525 }
0526 
0527 bool Digikam::TableViewColumns::ColumnDigikamProperties::columnAffectedByChangeset(const Digikam::ImageChangeset& imageChangeset) const
0528 {
0529     switch (subColumn)
0530     {
0531         case SubColumnTitle:
0532         case SubColumnCaption:
0533         case SubColumnTags:
0534         {
0535             return true;
0536             /// @todo These are not the right flags for these columns
0537 /*
0538             return imageChangeset.changes() & DatabaseFields::ItemCommentsAll;
0539 */
0540         }
0541 
0542         case SubColumnRating:
0543         {
0544             return (imageChangeset.changes() & DatabaseFields::Rating);
0545         }
0546 
0547         case SubColumnPickLabel:
0548         {
0549             return (imageChangeset.changes() & DatabaseFields::PickLabel);
0550         }
0551 
0552         case SubColumnColorLabel:
0553         {
0554             return (imageChangeset.changes() & DatabaseFields::ColorLabel);
0555         }
0556     }
0557 
0558     return false;
0559 }
0560 
0561 } // namespace TableViewColumns
0562 
0563 } // namespace Digikam
0564 
0565 #include "moc_tableview_column_digikam.cpp"