2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2020 Felix Ernst <fe.a.ernst@gmail.com>
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
8 #ifndef DOLPHINNAVIGATORSWIDGETACTION_H
9 #define DOLPHINNAVIGATORSWIDGETACTION_H
11 #include "dolphinurlnavigator.h"
16 #include <QWidgetAction>
24 * @brief QWidgetAction that allows to use DolphinUrlNavigators in a toolbar.
26 * This class is mainly a container that manages up to two DolphinUrlNavigator objects so they
27 * can be added to a toolbar. It also deals with alignment.
29 * The structure of the defaultWidget() of this QWidgetAction is as follows:
30 * - A QSplitter manages up to two sides which each correspond to one DolphinViewContainer.
31 * The secondary side only exists for split view and is created by
32 * createSecondaryUrlNavigator() when necessary.
33 * - Each side is a QWidget which I call NavigatorWidget with a QHBoxLayout.
34 * - Each NavigatorWidget consists an UrlNavigator, an emptyTrashButton, a
35 * networkFolderButton, and spacing.
36 * - Only the primary navigatorWidget has leading spacing. Both have trailing spacing.
37 * The spacing is there to align the UrlNavigator with its DolphinViewContainer.
39 class DolphinNavigatorsWidgetAction
: public QWidgetAction
44 DolphinNavigatorsWidgetAction(QWidget
*parent
= nullptr);
47 * Adjusts the width of the spacings used to align the UrlNavigators with ViewContainers.
48 * This can only work nicely if up-to-date geometry of ViewContainers is cached so
49 * followViewContainersGeometry() has to have been called at least once before.
54 * The secondary UrlNavigator is only created on-demand. Such an action is not necessary
55 * for the primary UrlNavigator which is created preemptively.
57 * This method should preferably only be called when:
58 * - Split view is activated in the active tab
60 * - A switch to a tab that is already in split view mode is occurring
62 void createSecondaryUrlNavigator();
65 * Notify this widget of changes in geometry of the ViewContainers it tries to be
68 void followViewContainersGeometry(QWidget
*primaryViewContainer
,
69 QWidget
*secondaryViewContainer
= nullptr);
71 bool isInToolbar() const;
74 * @return the primary UrlNavigator.
76 DolphinUrlNavigator
*primaryUrlNavigator() const;
78 * @return the secondary UrlNavigator and nullptr if it doesn't exist.
80 DolphinUrlNavigator
*secondaryUrlNavigator() const;
83 * Change the visibility of the secondary UrlNavigator including spacing.
84 * @param visible Setting this to false will completely hide the secondary side of this
85 * WidgetAction's QSplitter making the QSplitter effectively disappear.
87 void setSecondaryNavigatorVisible(bool visible
);
91 * There should always ever be one navigatorsWidget for this action so
92 * this method always returns the same widget and reparents it.
93 * You normally don't have to use this method directly because
94 * QWidgetAction::requestWidget() is used to obtain the navigatorsWidget
95 * and to steal it from wherever it was prior.
96 * @param parent the new parent of the navigatorsWidget.
98 QWidget
*createWidget(QWidget
*parent
) override
;
100 /** @see QWidgetAction::deleteWidget() */
101 void deleteWidget(QWidget
*widget
) override
;
105 * In Left-to-right languages the Primary side will be the left one.
112 * Used to create the navigatorWidgets for both sides of the QSplitter.
114 QWidget
*createNavigatorWidget(Side side
) const;
117 * Used to retrieve the emptyTrashButtons for the navigatorWidgets on both sides.
119 QPushButton
*emptyTrashButton(Side side
);
122 * Creates a new empty trash button.
123 * @param urlNavigator Only when this UrlNavigator shows the trash directory
124 * will the button be visible.
125 * @param parent Aside from the usual QObject deletion mechanisms,
126 * this parameter influences the positioning of dialog windows
127 * pertaining to this trash button.
129 QPushButton
*newEmptyTrashButton(const DolphinUrlNavigator
*urlNavigator
, QWidget
*parent
) const;
132 * Used to retrieve the networkFolderButtons for the navigatorWidgets on
135 QPushButton
*networkFolderButton(Side side
);
138 * Creates a new add "network folder" button.
139 * @param urlNavigator Only when this UrlNavigator shows the remote directory
140 * will the button be visible.
141 * @param parent The object that should be the button's parent.
143 QPushButton
*newNetworkFolderButton(const DolphinUrlNavigator
*urlNavigator
, QWidget
*parent
) const;
150 * Used to retrieve both the leading and trailing spacing for the navigatorWidgets
151 * on both sides. A secondary leading spacing does not exist.
153 QWidget
*spacing(Side side
, Position position
) const;
156 * Sets this action's text depending on the amount of visible UrlNavigators.
161 * The defaultWidget() of this QWidgetAction.
163 std::unique_ptr
<QSplitter
> m_splitter
;
166 * adjustSpacing() has to be called slightly later than when urlChanged is emitted.
167 * This timer bridges that time.
169 std::unique_ptr
<QTimer
> m_adjustSpacingTimer
;
172 * Extracts the geometry information needed by adjustSpacing() from
173 * ViewContainers. They are also monitored for size changes which
174 * will lead to adjustSpacing() calls.
176 class ViewGeometriesHelper
: public QObject
180 * @param navigatorsWidget The QWidget of the navigatorsWidgetAction.
181 * @param navigatorsWidgetAction is only used to call adjustSpacing() whenever that is
184 ViewGeometriesHelper(QWidget
*navigatorsWidget
, DolphinNavigatorsWidgetAction
*navigatorsWidgetAction
);
187 * Calls m_navigatorsWidgetAction::adjustSpacing() when a watched object is resized.
189 bool eventFilter(QObject
*watched
, QEvent
*event
) override
;
192 * Sets the ViewContainers whose geometry is obtained when viewGeometries() is called.
194 void setViewContainers(QWidget
*primaryViewContainer
,
195 QWidget
*secondaryViewContainer
= nullptr);
198 int globalXOfNavigatorsWidget
;
199 int globalXOfPrimary
;
201 int globalXOfSecondary
;
202 int widthOfSecondary
;
205 * @return a Geometries struct that contains values adjustSpacing() requires.
207 Geometries
viewGeometries();
210 QWidget
*m_navigatorsWidget
;
211 /** Is only used to call adjustSpacing() whenever that is deemed necessary. */
212 DolphinNavigatorsWidgetAction
*m_navigatorsWidgetAction
;
214 QPointer
<QWidget
> m_primaryViewContainer
;
215 QPointer
<QWidget
> m_secondaryViewContainer
;
218 ViewGeometriesHelper m_viewGeometriesHelper
;
221 * Used to check if the window has been resized.
222 * @see ViewGeometriesHelper::eventFilter() for why this is needed.
224 int m_previousWindowWidth
= -1;
227 #endif // DOLPHINNAVIGATORSWIDGETACTION_H