File indexing completed on 2024-05-05 04:19:13
0001 /* 0002 This file is part of the KDE project 0003 SPDX-FileCopyrightText: 2021 Felix Ernst <fe.a.ernst@gmail.com> 0004 0005 SPDX-License-Identifier: LGPL-2.1-or-later 0006 */ 0007 0008 #ifndef ALIGNWITHSIDEBARWIDGETACTION_H 0009 #define ALIGNWITHSIDEBARWIDGETACTION_H 0010 0011 #include <QPointer> 0012 #include <QToolBar> 0013 #include <QWidgetAction> 0014 0015 /** 0016 * A spacer used to align actions in the toolbar with the sidebar. 0017 */ 0018 class AlignWithSideBarWidgetAction : public QWidgetAction 0019 { 0020 Q_OBJECT 0021 0022 public: 0023 AlignWithSideBarWidgetAction(QObject *parent = nullptr); 0024 0025 /** 0026 * @param sideBar the QWidget this spacer will base its width on to facilitate alignment. 0027 * Can be nullptr. 0028 */ 0029 void setSideBar(QWidget *sideBar); 0030 0031 protected: 0032 /** @see QWidgetAction::createWidget() */ 0033 QWidget *createWidget(QWidget *parent) override; 0034 0035 private: 0036 /** The sideBar to align with. */ 0037 QPointer<QWidget> mSideBar; 0038 }; 0039 0040 /** 0041 * The widget of AlignWithSideBarWidgetAction. 0042 * This class is not meant to be used from outside AlignWithSideBarWidgetAction. 0043 */ 0044 class AligningSpacer : public QWidget 0045 { 0046 Q_OBJECT 0047 0048 public: 0049 AligningSpacer(QWidget *parent); 0050 0051 /** @see AlignWithSideBarWidgetAction::setSideBar() */ 0052 void setSideBar(QWidget *sideBar); 0053 0054 /** 0055 * Used to trigger updateWidth() whenever mSideBar's width changes. 0056 */ 0057 bool eventFilter(QObject * /* watched */, QEvent *event) override; 0058 0059 protected: 0060 /** 0061 * Used to trigger updateWidth() when the containing toolbar is locked/unlocked. 0062 */ 0063 void moveEvent(QMoveEvent * /* moved */) override; 0064 0065 private: 0066 /** 0067 * Having a separator directly after an empty space looks bad unless the separator 0068 * is aligned with a sidebar. This method is used to remove or re-add the separator 0069 * so everything looks nice. 0070 * @param visible Wether a potential separator after this widget should be visible. 0071 * This will always be changed to true if the parent toolbar is vertical. 0072 */ 0073 void setFollowingSeparatorVisible(bool visible); 0074 0075 /** 0076 * Updates the width of the spacer depending on the sizeHint() and sets the visibility 0077 * of a potentially following separator. 0078 */ 0079 void update(); 0080 0081 /** 0082 * Calculates the width of this spacer based on its position, the sideBar width, 0083 * the toolbar orientation and the following action. 0084 * @return the new width in pixels. 0085 */ 0086 int updateWidth(); 0087 0088 private: 0089 /** The SideBar to align with. */ 0090 QPointer<QWidget> mSideBar; 0091 /** The parentWidget() of this widget or nullptr when the parent isn't a QToolBar. */ 0092 QPointer<QToolBar> mToolbar; 0093 /** This spacer removes following separators if they would look bad/pointless. */ 0094 bool mWasSeparatorRemoved = false; 0095 }; 0096 0097 #endif // ALIGNWITHSIDEBARWIDGETACTION_H