File indexing completed on 2025-01-05 04:00:07

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2008-03-14
0007  * Description : User interface for searches
0008  *
0009  * SPDX-FileCopyrightText: 2008-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "visibilitycontroller.h"
0016 
0017 namespace Digikam
0018 {
0019 
0020 class Q_DECL_HIDDEN VisibilityController::Private
0021 {
0022 public:
0023 
0024     explicit Private()
0025       : status         (VisibilityController::Unknown),
0026         containerWidget(nullptr)
0027     {
0028     }
0029 
0030     VisibilityController::Status status;
0031     QList<VisibilityObject*>     objects;
0032     QWidget*                     containerWidget;
0033 };
0034 
0035 class Q_DECL_HIDDEN VisibilityWidgetWrapper : public QObject,
0036                                               public VisibilityObject
0037 {
0038     Q_OBJECT
0039 
0040 public:
0041 
0042     VisibilityWidgetWrapper(VisibilityController* const parent, QWidget* const widget)
0043         : QObject (parent),
0044           m_widget(widget)
0045     {
0046     }
0047 
0048     void setVisible(bool visible) override
0049     {
0050         m_widget->setVisible(visible);
0051     }
0052 
0053     bool isVisible() override
0054     {
0055         return m_widget->isVisible();
0056     }
0057 
0058     QWidget* m_widget;
0059 };
0060 
0061 // -------------------------------------------------------------------------------------
0062 
0063 VisibilityController::VisibilityController(QObject* const parent)
0064     : QObject(parent),
0065       d      (new Private)
0066 {
0067 }
0068 
0069 VisibilityController::~VisibilityController()
0070 {
0071     delete d;
0072 }
0073 
0074 void VisibilityController::addObject(VisibilityObject* const object)
0075 {
0076     d->objects << object;
0077 
0078     // create clean state
0079 
0080     if (d->status == Unknown)
0081     {
0082         if (object->isVisible())
0083         {
0084             d->status = Shown;
0085         }
0086         else
0087         {
0088             d->status = Hidden;
0089         }
0090     }
0091 
0092     // set state on object
0093 
0094     if ((d->status == Shown) || (d->status == Showing))
0095     {
0096         object->setVisible(true);
0097     }
0098     else
0099     {
0100         object->setVisible(false);
0101     }
0102 }
0103 
0104 void VisibilityController::addWidget(QWidget* const widget)
0105 {
0106     addObject(new VisibilityWidgetWrapper(this, widget));
0107 }
0108 
0109 void VisibilityController::setContainerWidget(QWidget* const widget)
0110 {
0111     d->containerWidget = widget;
0112 }
0113 
0114 void VisibilityController::setVisible(bool shallBeVisible)
0115 {
0116     if (shallBeVisible)
0117     {
0118         if ((d->status == Shown) || (d->status == Showing))
0119         {
0120             return;
0121         }
0122 
0123         d->status = Showing;
0124         beginStatusChange();
0125     }
0126     else
0127     {
0128         if ((d->status == Hidden) || (d->status == Hiding))
0129         {
0130             return;
0131         }
0132 
0133         d->status = Hiding;
0134         beginStatusChange();
0135     }
0136 }
0137 
0138 void VisibilityController::show()
0139 {
0140     setVisible(true);
0141 }
0142 
0143 void VisibilityController::hide()
0144 {
0145     setVisible(false);
0146 }
0147 
0148 void VisibilityController::triggerVisibility()
0149 {
0150     if ((d->status == Shown) || (d->status == Showing) || (d->status == Unknown))
0151     {
0152         setVisible(false);
0153     }
0154     else
0155     {
0156         setVisible(true);
0157     }
0158 }
0159 
0160 bool VisibilityController::isVisible() const
0161 {
0162     if ((d->status == Shown) || (d->status == Showing))
0163     {
0164         return true;
0165     }
0166     else
0167     {
0168         return false;
0169     }
0170 }
0171 
0172 void VisibilityController::beginStatusChange()
0173 {
0174     allSteps();
0175 }
0176 
0177 void VisibilityController::step()
0178 {
0179     if      (d->status == Showing)
0180     {
0181         Q_FOREACH (VisibilityObject* const o, d->objects)
0182         {
0183             if (!o->isVisible())
0184             {   // cppcheck-suppress useStlAlgorithm
0185                 o->setVisible(true);
0186                 return;
0187             }
0188         }
0189     }
0190     else if (d->status == Hiding)
0191     {
0192         Q_FOREACH (VisibilityObject* const o, d->objects)
0193         {
0194             if (o->isVisible())
0195             {   // cppcheck-suppress useStlAlgorithm
0196                 o->setVisible(false);
0197                 return;
0198             }
0199         }
0200     }
0201 }
0202 
0203 void VisibilityController::allSteps()
0204 {
0205     if      (d->status == Showing)
0206     {
0207         if (d->containerWidget)
0208         {
0209             d->containerWidget->setUpdatesEnabled(false);
0210         }
0211 
0212         Q_FOREACH (VisibilityObject* const o, d->objects)
0213         {
0214             o->setVisible(true);
0215         }
0216 
0217         if (d->containerWidget)
0218         {
0219             d->containerWidget->setUpdatesEnabled(true);
0220         }
0221     }
0222     else if (d->status == Hiding)
0223     {
0224         if (d->containerWidget)
0225         {
0226             d->containerWidget->setUpdatesEnabled(false);
0227         }
0228 
0229         Q_FOREACH (VisibilityObject* const o, d->objects)
0230         {
0231             o->setVisible(false);
0232         }
0233 
0234         if (d->containerWidget)
0235         {
0236             d->containerWidget->setUpdatesEnabled(true);
0237         }
0238     }
0239 }
0240 
0241 } // namespace Digikam
0242 
0243 #include "visibilitycontroller.moc"
0244 
0245 #include "moc_visibilitycontroller.cpp"