File indexing completed on 2024-05-19 04:35:27
0001 /*************************************************************************** 0002 * Copyright (C) 2020 by Simone Gaiarin <simgunz@gmail.com> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify * 0005 * it under the terms of the GNU General Public License as published by * 0006 * the Free Software Foundation; either version 2 of the License, or * 0007 * (at your option) any later version. * 0008 ***************************************************************************/ 0009 0010 #include <QAction> 0011 #include <QHBoxLayout> 0012 #include <QLabel> 0013 #include <QLayout> 0014 #include <QList> 0015 #include <QToolBar> 0016 #include <QToolButton> 0017 #include <QVBoxLayout> 0018 #include <QWidget> 0019 0020 #include "actionbar.h" 0021 0022 class ActionBarWidget : public QWidget 0023 { 0024 Q_OBJECT 0025 0026 public: 0027 explicit ActionBarWidget(QToolBar *parent); 0028 void recreateButtons(const QList<QAction *> &actions); 0029 0030 private Q_SLOTS: 0031 void onOrientationChanged(Qt::Orientation orientation); 0032 }; 0033 0034 ActionBarWidget::ActionBarWidget(QToolBar *parent) 0035 : QWidget::QWidget(parent) 0036 { 0037 QLayout *layout; 0038 if (parent->orientation() == Qt::Vertical) { 0039 layout = new QVBoxLayout(); 0040 } else { 0041 layout = new QHBoxLayout(); 0042 } 0043 setLayout(layout); 0044 connect(parent, &QToolBar::orientationChanged, this, &ActionBarWidget::onOrientationChanged); 0045 } 0046 0047 void ActionBarWidget::recreateButtons(const QList<QAction *> &actions) 0048 { 0049 QToolBar *parentToolbar = qobject_cast<QToolBar *>(parentWidget()); 0050 if (!parentToolbar) { 0051 return; 0052 } 0053 for (auto &toolButton : findChildren<QToolButton *>()) { 0054 layout()->removeWidget(toolButton); 0055 delete toolButton; 0056 } 0057 for (const auto &action : actions) { 0058 QToolButton *toolButton = new QToolButton(this); 0059 toolButton->setAutoRaise(true); 0060 toolButton->setFocusPolicy(Qt::NoFocus); 0061 toolButton->setIconSize(parentToolbar->iconSize()); 0062 toolButton->setToolButtonStyle(parentToolbar->toolButtonStyle()); 0063 toolButton->setDefaultAction(action); 0064 layout()->addWidget(toolButton); 0065 layout()->setAlignment(toolButton, Qt::AlignCenter); 0066 connect(parentToolbar, &QToolBar::iconSizeChanged, toolButton, &QToolButton::setIconSize); 0067 connect(parentToolbar, &QToolBar::toolButtonStyleChanged, toolButton, &QToolButton::setToolButtonStyle); 0068 } 0069 } 0070 0071 void ActionBarWidget::onOrientationChanged(Qt::Orientation orientation) 0072 { 0073 QLayout *newLayout; 0074 if (orientation == Qt::Vertical) { 0075 newLayout = new QVBoxLayout(); 0076 } else { 0077 newLayout = new QHBoxLayout(); 0078 } 0079 QLayout *oldLayout = layout(); 0080 for (auto &toolButton : findChildren<QToolButton *>()) { 0081 oldLayout->removeWidget(toolButton); 0082 newLayout->addWidget(toolButton); 0083 newLayout->setAlignment(toolButton, Qt::AlignCenter); 0084 } 0085 delete oldLayout; 0086 setLayout(newLayout); 0087 } 0088 0089 ActionBar::ActionBar(QObject *parent) 0090 : QWidgetAction(parent) 0091 { 0092 } 0093 0094 QWidget *ActionBar::createWidget(QWidget *parent) 0095 { 0096 QToolBar *parentToolbar = qobject_cast<QToolBar *>(parent); 0097 if (!parentToolbar) { 0098 return new QWidget(); 0099 } 0100 ActionBarWidget *widget = new ActionBarWidget(parentToolbar); 0101 widget->recreateButtons(m_actions); 0102 return widget; 0103 } 0104 0105 void ActionBar::addAction(QAction *action) 0106 { 0107 m_actions.append(action); 0108 } 0109 0110 void ActionBar::insertAction(int pos, QAction *action) 0111 { 0112 m_actions.insert(pos, action); 0113 } 0114 0115 void ActionBar::removeAction(QAction *action) 0116 { 0117 m_actions.removeAll(action); 0118 } 0119 0120 void ActionBar::recreateWidgets() 0121 { 0122 const auto widgets = createdWidgets(); 0123 for (auto *widget : widgets) { 0124 auto *actionBarWidget = qobject_cast<ActionBarWidget *>(widget); 0125 if (actionBarWidget) { 0126 actionBarWidget->recreateButtons(m_actions); 0127 } 0128 } 0129 } 0130 0131 #include "actionbar.moc"