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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2015-08-21
0007  * Description : a bar to indicate pending metadata
0008  *
0009  * SPDX-FileCopyrightText: 2015 by Veaceslav Munteanu <veaceslav dot munteanu90 at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "metadatastatusbar.h"
0016 
0017 // Qt includes
0018 
0019 #include <QToolButton>
0020 #include <QHBoxLayout>
0021 #include <QApplication>
0022 #include <QStyle>
0023 #include <QDebug>
0024 
0025 // KDE includes
0026 
0027 #include <klocalizedstring.h>
0028 
0029 // Local includes
0030 
0031 #include "dexpanderbox.h"
0032 #include "metaenginesettings.h"
0033 
0034 namespace Digikam
0035 {
0036 
0037 class Q_DECL_HIDDEN MetadataStatusBar::Private
0038 {
0039 public:
0040 
0041     explicit Private()
0042       : number  (0),
0043         enabled (true),
0044         info    (nullptr),
0045         applyBtn(nullptr)
0046     {
0047     }
0048 
0049     int               number;
0050     bool              enabled;
0051 
0052     DAdjustableLabel* info;
0053     QToolButton*      applyBtn;
0054 };
0055 
0056 MetadataStatusBar::MetadataStatusBar(QWidget* const parent)
0057     : QWidget(parent),
0058       d(new Private)
0059 {
0060     QHBoxLayout* const vlay = new QHBoxLayout(this);
0061     d->applyBtn             = new QToolButton(this);
0062     d->applyBtn->setIcon(QIcon::fromTheme(QLatin1String("view-refresh")));
0063     d->applyBtn->setToolTip(i18n("Apply pending changes to metadata"));
0064     d->applyBtn->setFocusPolicy(Qt::NoFocus);
0065     d->applyBtn->setAutoRaise(true);
0066     d->applyBtn->setDisabled(true);
0067 
0068     d->info                 = new DAdjustableLabel(this);
0069     d->info->setContextMenuPolicy(Qt::NoContextMenu);
0070     d->info->setAutoFillBackground(true);
0071     d->info->setFocusPolicy(Qt::NoFocus);
0072     d->info->setAdjustedText(i18n("No pending metadata synchronization"));
0073 
0074     d->info->setWhatsThis(i18n("If lazy synchronization is enabled in metadata settings, "
0075                                "the status bar will display the number of items waiting for synchronization"));
0076 
0077     vlay->addWidget(d->applyBtn);
0078     vlay->addWidget(d->info);
0079     vlay->setSpacing(qMin(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing),
0080                              QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing)));
0081     vlay->setContentsMargins(QMargins());
0082     vlay->setStretchFactor(d->info, 10);
0083 
0084     connect(MetaEngineSettings::instance(), SIGNAL(signalSettingsChanged()),
0085             this, SLOT(slotSettingsChanged()));
0086 
0087     connect(d->applyBtn, SIGNAL(released()),
0088             MetadataHubMngr::instance(), SLOT(slotApplyPending()));
0089 
0090     connect(MetadataHubMngr::instance(), SIGNAL(signalPendingMetadata(int)),
0091             this, SLOT(slotSetPendingItems(int)));
0092 
0093     connect(ProgressManager::instance(), SIGNAL(progressItemAdded(ProgressItem*)),
0094             this, SLOT(slotAddedProgressItem(ProgressItem*)));
0095 
0096     connect(ProgressManager::instance(), SIGNAL(progressItemCompleted(ProgressItem*)),
0097             this, SLOT(slotCompletedProgressItem(ProgressItem*)));
0098 
0099     if (MetaEngineSettings::instance()->settings().useLazySync)
0100     {
0101         show();
0102     }
0103     else
0104     {
0105         hide();
0106     }
0107 }
0108 
0109 MetadataStatusBar::~MetadataStatusBar()
0110 {
0111     delete d;
0112 }
0113 
0114 void MetadataStatusBar::slotSettingsChanged()
0115 {
0116     if (MetaEngineSettings::instance()->settings().useLazySync)
0117     {
0118         show();
0119     }
0120     else
0121     {
0122         hide();
0123     }
0124 }
0125 
0126 void MetadataStatusBar::slotSetPendingItems(int number)
0127 {
0128     d->number = number;
0129 
0130     if (d->number == 0)
0131     {
0132         d->info->setAdjustedText(i18n("No pending metadata synchronization"));
0133         d->applyBtn->setDisabled(true);
0134     }
0135     else
0136     {
0137         d->info->setAdjustedText(i18np("1 file awaits synchronization",
0138                                        "%1 files await synchronization",
0139                                        d->number));
0140         d->applyBtn->setEnabled(d->enabled);
0141     }
0142 }
0143 
0144 void MetadataStatusBar::slotAddedProgressItem(ProgressItem* item)
0145 {
0146     if (item->id() == QLatin1String("MetadataSynchronizer"))
0147     {
0148         d->applyBtn->setEnabled(false);
0149         d->info->setEnabled(false);
0150         d->enabled = false;
0151     }
0152 }
0153 
0154 void MetadataStatusBar::slotCompletedProgressItem(ProgressItem* item)
0155 {
0156     if (item->id() == QLatin1String("MetadataSynchronizer"))
0157     {
0158         d->applyBtn->setEnabled(d->number > 0);
0159         d->info->setEnabled(true);
0160         d->enabled = true;
0161     }
0162 }
0163 
0164 } // namespace Digikam
0165 
0166 #include "moc_metadatastatusbar.cpp"