Warning, file /office/skrooge/skgbasegui/skgwidgetselector.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 /** @file
0007  * A widget selector.
0008  *
0009  * @author Stephane MANKOWSKI / Guillaume DE BURE
0010  */
0011 #include "skgwidgetselector.h"
0012 #include "skgdefine.h"
0013 
0014 #include <qtoolbutton.h>
0015 
0016 SKGWidgetSelector::SKGWidgetSelector(QWidget* iParent)
0017     : QWidget(iParent), m_currentMode(-1), m_alwaysOneOpen(false)
0018 {
0019     ui.setupUi(this);
0020 }
0021 
0022 SKGWidgetSelector::~SKGWidgetSelector()
0023     = default;
0024 
0025 void SKGWidgetSelector::addButton(const QIcon& iIcon, const QString& iTitle, const QString& iToolTip, QWidget* iWidgets)
0026 {
0027     SKGListQWidget list;
0028     list.push_back(iWidgets);
0029     addButton(iIcon, iTitle, iToolTip, list);
0030 }
0031 
0032 void SKGWidgetSelector::addButton(const QIcon& iIcon, const QString& iTitle, const QString& iToolTip, const SKGWidgetSelector::SKGListQWidget& iListOfShownWidgets)
0033 {
0034     // Create button
0035     auto btn = new QToolButton(this);
0036     btn->setCheckable(true);
0037     btn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
0038     btn->setAutoRaise(true);
0039     btn->setText(iTitle);
0040     btn->setToolTip(iToolTip);
0041     btn->setIcon(iIcon);
0042 
0043     ui.horizontalLayout->insertWidget(m_listButton.count(), btn);
0044 
0045     connect(btn, &QToolButton::clicked, this, &SKGWidgetSelector::onButtonClicked);
0046 
0047     // Memorize items
0048     m_listButton.push_back(btn);
0049 
0050     // Hide widgets
0051     SKGWidgetSelector::SKGListQWidget list;
0052     for (auto w : qAsConst(iListOfShownWidgets)) {
0053         if (w != nullptr) {
0054             // Addition of an intermediate widget
0055             /*auto widget = new QWidget(w->parentWidget());
0056                 widget->setObjectName(w->objectName()+"_intermediate");
0057                 auto horizontalLayout = new QHBoxLayout(widget);
0058                 horizontalLayout->setSpacing(0);
0059                 horizontalLayout->setContentsMargins(0, 0, 0, 0);
0060                 horizontalLayout->setObjectName(w->objectName()+"_horizontalLayout");
0061             w->setParent(widget);
0062                 horizontalLayout->addWidget(w);*/
0063 
0064             list.push_back(w);
0065             w->hide();
0066         }
0067     }
0068     m_listWidgets.push_back(list);
0069 }
0070 
0071 int SKGWidgetSelector::getSelectedMode() const
0072 {
0073     return m_currentMode;
0074 }
0075 
0076 void SKGWidgetSelector::setSelectedMode(int iMode)
0077 {
0078     if (iMode != m_currentMode) {
0079         // Hide current widgets
0080         if (m_currentMode >= 0) {
0081             m_listButton.at(m_currentMode)->setChecked(false);
0082             SKGListQWidget list = m_listWidgets.at(m_currentMode);
0083 
0084             for (auto w : qAsConst(list)) {
0085                 if (w != nullptr) {
0086                     /*if (iMode == -1) {
0087                         auto anim1 = new QPropertyAnimation(w, "geometry");
0088                     QRect r=w->geometry();
0089                          m_originGeometries[w]=r;
0090                                          // anim1->setStartValue(m_originGeometries[w]);
0091                          r.setY(r.y()+r.height());
0092                          m_endGeometries[w]=r;
0093                                          anim1->setEndValue(m_endGeometries[w]);
0094                                          anim1->setEasingCurve(QEasingCurve::InOutSine);
0095                                          anim1->setDuration(500);
0096                                          anim1->start(QAbstractAnimation::DeleteWhenStopped);
0097                                          connect(anim1, &QPropertyAnimation::finished, w, &QWidget::hide);
0098 
0099                                      } else */
0100                     w->hide();
0101                 }
0102             }
0103         }
0104 
0105         // Set current mode
0106         // int previousmode = m_currentMode;
0107         m_currentMode = iMode;
0108         if (m_currentMode >= m_listWidgets.count()) {
0109             m_currentMode = -1;
0110         }
0111 
0112         // Show widgets
0113         if (m_currentMode >= 0) {
0114             m_listButton.at(m_currentMode)->setChecked(true);
0115             for (auto w : qAsConst(m_listWidgets.at(m_currentMode))) {
0116                 if (w != nullptr) {
0117                     w->show();
0118                     /*if (previousmode == -1) {
0119                         auto anim1 = new QPropertyAnimation(w, "geometry");
0120                         // anim1->setStartValue(m_endGeometries[w]);
0121                         anim1->setEndValue(m_originGeometries[w]);
0122                         anim1->setEasingCurve(QEasingCurve::InOutSine);
0123                         anim1->setDuration(500);
0124                         anim1->start(QAbstractAnimation::DeleteWhenStopped);
0125                     }*/
0126                 }
0127             }
0128         }
0129 
0130         if (m_currentMode < -1) {
0131             this->hide();
0132         }
0133 
0134         emit selectedModeChanged(m_currentMode);
0135     }
0136 }
0137 
0138 void SKGWidgetSelector::setEnabledMode(int iMode, bool iEnabled)
0139 {
0140     if (iMode >= 0 && iMode < m_listButton.count()) {
0141         m_listButton.at(iMode)->setEnabled(iEnabled);
0142     }
0143 }
0144 
0145 void SKGWidgetSelector::onButtonClicked()
0146 {
0147     auto* clickedButton = qobject_cast<QToolButton*>(sender());
0148     int newMode = m_listButton.indexOf(clickedButton);
0149     if (m_currentMode == newMode) {
0150         if (getAlwaysOneOpen()) {
0151             --newMode;
0152             if (newMode == -1) {
0153                 newMode = m_listButton.count() - 1;
0154             }
0155         } else {
0156             newMode = -1;
0157         }
0158     }
0159 
0160     setSelectedMode(newMode);
0161 }
0162 
0163 bool SKGWidgetSelector::getAlwaysOneOpen() const
0164 {
0165     return m_alwaysOneOpen;
0166 }
0167 void SKGWidgetSelector::setAlwaysOneOpen(bool iMode)
0168 {
0169     if (m_alwaysOneOpen != iMode) {
0170         m_alwaysOneOpen = iMode;
0171         Q_EMIT alwaysOneOpenChanged();
0172     }
0173 }
0174 
0175 
0176