File indexing completed on 2025-01-05 03:57:58

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2021-09-27
0007  * Description : Side Bar Widget for the Showfoto stack view.
0008  *
0009  * SPDX-FileCopyrightText: 2021-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 "showfotostackviewsidebar.h"
0016 
0017 // Qt includes
0018 
0019 #include <QApplication>
0020 #include <QStyle>
0021 #include <QIcon>
0022 #include <QUndoStack>
0023 #include <QVBoxLayout>
0024 #include <QFileInfo>
0025 #include <QDir>
0026 #include <QTimer>
0027 #include <QSplitter>
0028 
0029 // KDE includes
0030 
0031 #include <kconfiggroup.h>
0032 #include <klocalizedstring.h>
0033 
0034 // Local includes
0035 
0036 #include "digikam_debug.h"
0037 #include "digikam_globals.h"
0038 #include "showfoto.h"
0039 #include "showfotostackviewlist.h"
0040 #include "showfotothumbnailbar.h"
0041 #include "showfotostackviewfavorites.h"
0042 
0043 namespace ShowFoto
0044 {
0045 
0046 class Q_DECL_HIDDEN ShowfotoStackViewSideBar::Private
0047 {
0048 
0049 public:
0050 
0051     explicit Private()
0052       : pluginFingerPrint(QLatin1String("DPlugin::Generic::View")),
0053         view             (nullptr),
0054         favts            (nullptr),
0055         splitter         (nullptr),
0056         sortOrder        (Qt::AscendingOrder),
0057         role             (ShowfotoStackViewList::FileName)
0058     {
0059     }
0060 
0061     const QString                        pluginFingerPrint;         ///< Identify plugins category to host in stack-view
0062     static const QString                 configIconSizeEntry;
0063     static const QString                 configSplitterStateEntry;
0064 
0065     ShowfotoStackViewList*               view;
0066     ShowfotoStackViewFavorites*          favts;
0067     QSplitter*                           splitter;
0068 
0069     QList<DPluginAction*>                dpluginActions;            ///< List of identified DPlugins actions.
0070     QList<QAction*>                      pluginActions;             ///< List of Qt actions from identified Dplugins.
0071     Qt::SortOrder                        sortOrder;
0072     ShowfotoStackViewList::StackViewRole role;
0073 };
0074 
0075 const QString ShowfotoStackViewSideBar::Private::configIconSizeEntry(QLatin1String("Icon Size"));
0076 const QString ShowfotoStackViewSideBar::Private::configSplitterStateEntry(QLatin1String("Splitter State"));
0077 
0078 ShowfotoStackViewSideBar::ShowfotoStackViewSideBar(Showfoto* const parent)
0079     : QWidget          (parent),
0080       StateSavingObject(this),
0081       d                (new Private)
0082 {
0083     setObjectName(QLatin1String("ShowfotoStackView Sidebar"));
0084 
0085     // --- Populate the view
0086 
0087     d->view                    = new ShowfotoStackViewList(this);
0088 
0089     d->favts                   = new ShowfotoStackViewFavorites(this);
0090 
0091     d->splitter                = new QSplitter(Qt::Vertical, this);
0092     d->splitter->addWidget(d->view);
0093     d->splitter->addWidget(d->favts);
0094     d->splitter->setStretchFactor(0, 10);
0095     d->splitter->setStretchFactor(1, 3);
0096 
0097     QVBoxLayout* const layout  = new QVBoxLayout(this);
0098     layout->addWidget(d->splitter);
0099     layout->setContentsMargins(QMargins());
0100 
0101     // --- Setup connections
0102 
0103     connect(d->favts, SIGNAL(signalLoadContentsFromFiles(QStringList,QString)),
0104             this, SIGNAL(signalLoadContentsFromFiles(QStringList,QString)));
0105 
0106     connect(d->view, SIGNAL(signalAddFavorite()),
0107             this, SIGNAL(signalAddFavorite()));
0108 
0109     connect(d->view, SIGNAL(signalClearItemsList()),
0110             this, SIGNAL(signalClearItemsList()));
0111 
0112     connect(d->view, SIGNAL(signalRemoveItemInfos(QList<ShowfotoItemInfo>)),
0113             this, SIGNAL(signalRemoveItemInfos(QList<ShowfotoItemInfo>)));
0114 
0115     connect(d->view, SIGNAL(signalShowfotoItemInfoActivated(ShowfotoItemInfo)),
0116             this, SIGNAL(signalShowfotoItemInfoActivated(ShowfotoItemInfo)));
0117 
0118     connect(d->view, SIGNAL(signalItemListChanged(int)),
0119             d->favts, SLOT(slotItemListChanged(int)));
0120 }
0121 
0122 ShowfotoStackViewSideBar::~ShowfotoStackViewSideBar()
0123 {
0124     delete d;
0125 }
0126 
0127 void ShowfotoStackViewSideBar::setThumbbar(ShowfotoThumbnailBar* const thumbbar)
0128 {
0129     d->view->setThumbbar(thumbbar);
0130 }
0131 
0132 QList<QUrl> ShowfotoStackViewSideBar::urls() const
0133 {
0134     return d->view->urls();
0135 }
0136 
0137 QUrl ShowfotoStackViewSideBar::currentUrl() const
0138 {
0139     return d->view->currentUrl();
0140 }
0141 
0142 void ShowfotoStackViewSideBar::setSortOrder(int order)
0143 {
0144     d->sortOrder = (Qt::SortOrder)order;
0145     d->view->sortItems(d->role, d->sortOrder);
0146 }
0147 
0148 int ShowfotoStackViewSideBar::sortOrder() const
0149 {
0150     return d->sortOrder;
0151 }
0152 
0153 void ShowfotoStackViewSideBar::setSortRole(int role)
0154 {
0155     d->role = (ShowfotoStackViewList::StackViewRole)role;
0156     d->view->sortItems(d->role, d->sortOrder);
0157 }
0158 
0159 int ShowfotoStackViewSideBar::sortRole() const
0160 {
0161     return d->role;
0162 }
0163 
0164 int ShowfotoStackViewSideBar::iconSize() const
0165 {
0166     return d->view->iconSize().width();
0167 }
0168 
0169 void ShowfotoStackViewSideBar::registerPluginActions(const QList<DPluginAction*>& actions)
0170 {
0171    d->dpluginActions = actions;
0172 
0173    Q_FOREACH (QAction* const dpact, d->dpluginActions)
0174    {
0175        QAction* const act = new QAction(dpact->text(), this);
0176        act->setObjectName(dpact->objectName());
0177        act->setIcon(dpact->icon());
0178        act->setToolTip(dpact->toolTip());
0179        act->setData(d->pluginFingerPrint);
0180 
0181        connect(act, SIGNAL(triggered(bool)),
0182                this, SLOT(slotPluginActionTriggered()));
0183 
0184        d->pluginActions << act;
0185     }
0186 }
0187 
0188 void ShowfotoStackViewSideBar::slotPluginActionTriggered()
0189 {
0190     QAction* const act = dynamic_cast<QAction*>(sender());
0191 
0192     if (act)
0193     {
0194         Q_FOREACH (QAction* const dpact, d->dpluginActions)
0195         {
0196             if (act->objectName() == dpact->objectName())
0197             {
0198                 d->favts->loadContents();
0199                 QTimer::singleShot(1000, dpact, SLOT(trigger()));
0200 
0201                 return;
0202             }
0203         }
0204     }
0205 }
0206 
0207 QList<QAction*> ShowfotoStackViewSideBar::pluginActions() const
0208 {
0209     return d->pluginActions;
0210 }
0211 
0212 const QIcon ShowfotoStackViewSideBar::getIcon()
0213 {
0214     return QIcon::fromTheme(QLatin1String("layer-visible-on"));
0215 }
0216 
0217 const QString ShowfotoStackViewSideBar::getCaption()
0218 {
0219     return i18nc("@title: stack-view list on items", "Stack");
0220 }
0221 
0222 void ShowfotoStackViewSideBar::doLoadState()
0223 {
0224     KConfigGroup group = getConfigGroup();
0225 
0226     d->favts->readSettings();
0227 
0228     int iconSize       = group.readEntry(entryName(d->configIconSizeEntry),      (int)ShowfotoStackViewList::SizeSmall);
0229     d->view->setIconSize(QSize(iconSize, iconSize));
0230 
0231     QByteArray state   = group.readEntry(entryName(d->configSplitterStateEntry), QByteArray());
0232 
0233     if (!state.isEmpty())
0234     {
0235         d->splitter->restoreState(QByteArray::fromBase64(state));
0236     }
0237 }
0238 
0239 void ShowfotoStackViewSideBar::doSaveState()
0240 {
0241     KConfigGroup group = getConfigGroup();
0242 
0243     d->favts->saveSettings();
0244 
0245     group.writeEntry(entryName(d->configIconSizeEntry),      d->view->iconSize().width());
0246     group.writeEntry(entryName(d->configSplitterStateEntry), d->splitter->saveState().toBase64());
0247     group.sync();
0248 }
0249 
0250 } // namespace ShowFoto
0251 
0252 #include "moc_showfotostackviewsidebar.cpp"