File indexing completed on 2025-01-05 03:59:47

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2012-01-04
0007  * Description : a message box to manage camera items
0008  *
0009  * SPDX-FileCopyrightText: 2012-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "cameramessagebox.h"
0016 
0017 // Qt includes
0018 
0019 #include <QPainter>
0020 #include <QPointer>
0021 #include <QCheckBox>
0022 #include <QGroupBox>
0023 #include <QLayout>
0024 #include <QLabel>
0025 #include <QHeaderView>
0026 #include <QApplication>
0027 #include <QStyle>
0028 #include <QUrl>
0029 #include <QIcon>
0030 #include <QDialog>
0031 #include <QPushButton>
0032 #include <QMessageBox>
0033 #include <QDialogButtonBox>
0034 
0035 // KDE includes
0036 
0037 #include <klocalizedstring.h>
0038 
0039 // Local includes
0040 
0041 #include "dmessagebox.h"
0042 #include "digikam_debug.h"
0043 
0044 namespace Digikam
0045 {
0046 
0047 class Q_DECL_HIDDEN CameraItem::Private
0048 {
0049 
0050 public:
0051 
0052     explicit Private()
0053       : hasThumb(false)
0054     {
0055     }
0056 
0057     bool        hasThumb;
0058 
0059     CamItemInfo info;
0060 };
0061 
0062 CameraItem::CameraItem(QTreeWidget* const parent, const CamItemInfo& info)
0063     : QTreeWidgetItem(parent),
0064       d              (new Private)
0065 {
0066     d->info = info;
0067     setThumb(QIcon::fromTheme(QLatin1String("view-preview")).pixmap(parent->iconSize().width(), QIcon::Disabled), false);
0068     setText(1, d->info.name);
0069 }
0070 
0071 CameraItem::~CameraItem()
0072 {
0073     delete d;
0074 }
0075 
0076 bool CameraItem::hasValidThumbnail() const
0077 {
0078     return d->hasThumb;
0079 }
0080 
0081 CamItemInfo CameraItem::info() const
0082 {
0083     return d->info;
0084 }
0085 
0086 void CameraItem::setThumb(const QPixmap& pix, bool hasThumb)
0087 {
0088     int iconSize = treeWidget()->iconSize().width();
0089     QPixmap pixmap(iconSize + 2, iconSize + 2);
0090     pixmap.fill(Qt::transparent);
0091     QPainter p(&pixmap);
0092     p.drawPixmap((pixmap.width()  / 2)  - (pix.width() / 2),
0093                  (pixmap.height() / 2) - (pix.height() / 2), pix);
0094 
0095     QIcon icon = QIcon(pixmap);
0096 
0097     //  We make sure the preview icon stays the same regardless of the role
0098 
0099     icon.addPixmap(pixmap, QIcon::Selected, QIcon::On);
0100     icon.addPixmap(pixmap, QIcon::Selected, QIcon::Off);
0101     icon.addPixmap(pixmap, QIcon::Active,   QIcon::On);
0102     icon.addPixmap(pixmap, QIcon::Active,   QIcon::Off);
0103     icon.addPixmap(pixmap, QIcon::Normal,   QIcon::On);
0104     icon.addPixmap(pixmap, QIcon::Normal,   QIcon::Off);
0105     setIcon(0, icon);
0106 
0107     d->hasThumb = hasThumb;
0108 }
0109 
0110 //----------------------------------------------------------------------------
0111 
0112 class Q_DECL_HIDDEN CameraItemList::Private
0113 {
0114 
0115 public:
0116 
0117     explicit Private()
0118         : iconSize(64),
0119           ctrl    (nullptr)
0120     {
0121     }
0122 
0123     const int         iconSize;
0124 
0125     CameraThumbsCtrl* ctrl;
0126 };
0127 
0128 CameraItemList::CameraItemList(QWidget* const parent)
0129     : QTreeWidget(parent),
0130       d          (new Private)
0131 {
0132     setColumnCount(2);
0133     setRootIsDecorated(false);
0134     setUniformRowHeights(true);
0135     setAllColumnsShowFocus(true);
0136     setSelectionMode(QAbstractItemView::SingleSelection);
0137     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
0138     setIconSize(QSize(d->iconSize, d->iconSize));
0139     setHeaderLabels(QStringList() << i18nc("@title", "Thumb") << i18nc("@title", "File Name"));
0140     header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
0141     header()->setSectionResizeMode(1, QHeaderView::Stretch);
0142 }
0143 
0144 CameraItemList::~CameraItemList()
0145 {
0146     delete d;
0147 }
0148 
0149 void CameraItemList::setItems(const CamItemInfoList& items)
0150 {
0151     Q_FOREACH (const CamItemInfo& info, items)
0152     {
0153         new CameraItem(this, info);
0154     }
0155 }
0156 
0157 void CameraItemList::setThumbCtrl(CameraThumbsCtrl* const ctrl)
0158 {
0159     d->ctrl = ctrl;
0160 
0161     connect(d->ctrl, SIGNAL(signalThumbInfoReady(CamItemInfo)),
0162             this, SLOT(slotThumbnailLoaded(CamItemInfo)));
0163 }
0164 
0165 void CameraItemList::slotThumbnailLoaded(const CamItemInfo& info)
0166 {
0167     QTreeWidgetItemIterator it(this);
0168     bool                    valid;
0169     CachedItem              citem;
0170 
0171     while (*it)
0172     {
0173         CameraItem* const item = dynamic_cast<CameraItem*>(*it);
0174 
0175         if (item && item->info().url() == info.url())
0176         {
0177             valid = d->ctrl->getThumbInfo(info, citem);
0178             item->setThumb(citem.second.scaled(d->iconSize, d->iconSize, Qt::KeepAspectRatio), valid);
0179             return;
0180         }
0181 
0182         ++it;
0183     }
0184 }
0185 
0186 void CameraItemList::drawRow(QPainter* p, const QStyleOptionViewItem& opt, const QModelIndex& index) const
0187 {
0188     CameraItem* const item = dynamic_cast<CameraItem*>(itemFromIndex(index));
0189 
0190     if (item && !item->hasValidThumbnail())
0191     {
0192         CachedItem citem;
0193         bool valid = d->ctrl->getThumbInfo(item->info(), citem);
0194         item->setThumb(citem.second.scaled(d->iconSize, d->iconSize, Qt::KeepAspectRatio), valid);
0195     }
0196 
0197     QTreeWidget::drawRow(p, opt, index);
0198 }
0199 
0200 // --------------------------------------------------------------------------------------------------------
0201 
0202 void CameraMessageBox::informationList(CameraThumbsCtrl* const ctrl,
0203                                        QWidget* const parent,
0204                                        const QString& caption,
0205                                        const QString& text,
0206                                        const CamItemInfoList& items,
0207                                        const QString& dontShowAgainName)
0208 {
0209     CameraItemList* const listWidget = new CameraItemList();
0210     listWidget->setThumbCtrl(ctrl);
0211     listWidget->setItems(items);
0212 
0213     DMessageBox::showInformationWidget(QMessageBox::Information, parent, caption, text, listWidget, dontShowAgainName);
0214 }
0215 
0216 int CameraMessageBox::warningContinueCancelList(CameraThumbsCtrl* const ctrl,
0217                                                 QWidget* const parent,
0218                                                 const QString& caption,
0219                                                 const QString& text,
0220                                                 const CamItemInfoList& items,
0221                                                 const QString& dontAskAgainName)
0222 {
0223     CameraItemList* const listWidget = new CameraItemList();
0224     listWidget->setThumbCtrl(ctrl);
0225     listWidget->setItems(items);
0226 
0227     return (DMessageBox::showContinueCancelWidget(QMessageBox::Warning, parent, caption, text, listWidget, dontAskAgainName));
0228 }
0229 
0230 } // namespace Digikam
0231 
0232 #include "moc_cameramessagebox.cpp"