File indexing completed on 2024-12-15 04:00:59

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "better_toolbox_widget.hpp"
0008 
0009 #include <QVBoxLayout>
0010 #include <QToolButton>
0011 #include <QButtonGroup>
0012 #include <QStyle>
0013 #include <QFrame>
0014 #include <QEvent>
0015 
0016 class glaxnimate::android::BetterToolboxWidget::Private
0017 {
0018 public:
0019     struct Item
0020     {
0021         QToolButton* title = nullptr;
0022         QWidget* widget = nullptr;
0023         QWidget* line = nullptr;
0024     };
0025 
0026     QWidget* make_line()
0027     {
0028         QFrame *line = new QFrame();
0029         line->setFrameShape(QFrame::HLine);
0030         line->setFrameShadow(QFrame::Sunken);
0031         return line;
0032     }
0033 
0034     std::vector<Item> items;
0035     QVBoxLayout* layout;
0036     QButtonGroup group;
0037     int max_width = -1;
0038 };
0039 
0040 glaxnimate::android::BetterToolboxWidget::BetterToolboxWidget(QWidget *parent)
0041     : QWidget(parent), d(std::make_unique<Private>())
0042 {
0043     d->layout = new QVBoxLayout(this);
0044     d->layout->setMargin(0);
0045     d->layout->setSpacing(0);
0046     setLayout(d->layout);
0047     d->group.setExclusive(true);
0048     d->layout->addWidget(d->make_line());
0049 }
0050 
0051 glaxnimate::android::BetterToolboxWidget::~BetterToolboxWidget()
0052 {
0053 
0054 }
0055 
0056 QWidget *glaxnimate::android::BetterToolboxWidget::addItem(const QIcon &icon, const QString &text)
0057 {
0058     auto widget = new QWidget(this);
0059     addItem(widget, icon, text);
0060     return widget;
0061 }
0062 
0063 void glaxnimate::android::BetterToolboxWidget::addItem(QWidget *widget, const QIcon &icon, const QString &text)
0064 {
0065     int extent = style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, nullptr);
0066     QSize sz(extent, extent);
0067 
0068     auto icon_label = new QToolButton(this);
0069     icon_label->setIcon(icon);
0070     icon_label->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
0071     icon_label->setIconSize(sz);
0072     icon_label->setText(text);
0073     icon_label->setCheckable(true);
0074     icon_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
0075     d->group.addButton(icon_label);
0076     icon_label->setChecked(d->items.empty());
0077     d->layout->addWidget(icon_label);
0078     d->layout->addWidget(d->make_line());
0079 
0080     d->layout->addWidget(widget);
0081 //    d->max_width = qMax(widget->sizeHint().width(), d->max_width);
0082     widget->setVisible(icon_label->isChecked());
0083 //    auto sp = widget->sizePolicy();
0084 //    sp.setHorizontalPolicy(QSizePolicy::Expanding);
0085 //    widget->setSizePolicy(sp);
0086     connect(icon_label, &QToolButton::toggled, widget, &QWidget::setVisible);
0087 
0088     QWidget* line = d->make_line();
0089     d->layout->addWidget(line);
0090     line->setVisible(icon_label->isChecked());
0091     connect(icon_label, &QToolButton::toggled, line, &QWidget::setVisible);
0092 
0093     d->items.push_back({icon_label, widget, line});
0094 }
0095 
0096 void glaxnimate::android::BetterToolboxWidget::addItem(QWidget *widget, const QString &text)
0097 {
0098     addItem(widget, {}, text);
0099 }
0100 
0101 void glaxnimate::android::BetterToolboxWidget::setItemText(int index, const QString &text)
0102 {
0103     d->items[index].title->setText(text);
0104 }
0105 
0106 int glaxnimate::android::BetterToolboxWidget::count() const
0107 {
0108     return d->items.size();
0109 }
0110 
0111 QSize glaxnimate::android::BetterToolboxWidget::sizeHint() const
0112 {
0113     QSize hint = QWidget::sizeHint();
0114     if ( d->max_width == -1 )
0115     {
0116         d->max_width = hint.width();
0117         for ( const auto & item : d->items )
0118             d->max_width = qMax(item.widget->sizeHint().width(), d->max_width);
0119     }
0120 
0121     return {d->max_width, hint.height()};
0122 }
0123 
0124 bool glaxnimate::android::BetterToolboxWidget::event(QEvent *event)
0125 {
0126     if ( event->type() == QEvent::LayoutRequest )
0127         d->max_width = -1;
0128 
0129     return QWidget::event(event);
0130 }