]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinnavigatorswidgetaction.h
Merge branch 'release/20.12'
[dolphin.git] / src / dolphinnavigatorswidgetaction.h
1 /*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2020 Felix Ernst <fe.a.ernst@gmail.com>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8 #ifndef DOLPHINNAVIGATORSWIDGETACTION_H
9 #define DOLPHINNAVIGATORSWIDGETACTION_H
10
11 #include "dolphinurlnavigator.h"
12
13 #include <QSplitter>
14 #include <QTimer>
15 #include <QWidgetAction>
16
17 #include <memory>
18
19 class KXmlGuiWindow;
20 class QPushButton;
21
22 /**
23 * @brief QWidgetAction that allows to use DolphinUrlNavigators in a toolbar.
24 *
25 * This class is mainly a container that manages up to two DolphinUrlNavigator objects so they
26 * can be added to a toolbar. It also deals with alignment.
27 *
28 * The structure of the defaultWidget() of this QWidgetAction is as follows:
29 * - A QSplitter manages up to two sides which each correspond to one DolphinViewContainer.
30 * The secondary side only exists for split view and is created by
31 * createSecondaryUrlNavigator() when necessary.
32 * - Each side is a QWidget which I call NavigatorWidget with a QHBoxLayout.
33 * - Each NavigatorWidget consists an UrlNavigator, an emptyTrashButton and spacing.
34 * - Only the primary navigatorWidget has leading spacing. Both have trailing spacing.
35 * The spacing is there to align the UrlNavigator with its DolphinViewContainer.
36 */
37 class DolphinNavigatorsWidgetAction : public QWidgetAction
38 {
39 Q_OBJECT
40
41 public:
42 DolphinNavigatorsWidgetAction(QWidget *parent = nullptr);
43
44 /**
45 * The secondary UrlNavigator is only created on-demand. Such an action is not necessary
46 * for the primary UrlNavigator which is created preemptively.
47 *
48 * This method should preferably only be called when:
49 * - Split view is activated in the active tab
50 * OR
51 * - A switch to a tab that is already in split view mode is occuring
52 */
53 void createSecondaryUrlNavigator();
54
55 /**
56 * Notify the primary UrlNavigator of changes in geometry of the ViewContainer it tries to be
57 * aligned with. Only call this method if there is no secondary UrlNavigator.
58 */
59 void followViewContainerGeometry(int globalXOfPrimary, int widthOfPrimary);
60 /**
61 * Notify this widget of changes in geometry of the ViewContainers it tries to be
62 * aligned with.
63 */
64 void followViewContainersGeometry(int globalXOfPrimary, int widthOfPrimary,
65 int globalXOfSecondary, int widthOfSecondary);
66
67 bool isInToolbar() const;
68
69 /**
70 * @return the primary UrlNavigator.
71 */
72 DolphinUrlNavigator *primaryUrlNavigator() const;
73 /**
74 * @return the secondary UrlNavigator and nullptr if it doesn't exist.
75 */
76 DolphinUrlNavigator *secondaryUrlNavigator() const;
77
78 /**
79 * Change the visibility of the secondary UrlNavigator including spacing.
80 * @param visible Setting this to false will completely hide the secondary side of this
81 * WidgetAction's QSplitter making the QSplitter effectively disappear.
82 */
83 void setSecondaryNavigatorVisible(bool visible);
84
85 protected:
86 /**
87 * There should always ever be one navigatorsWidget for this action so
88 * this method always returns the same widget and reparents it.
89 * You normally don't have to use this method directly because
90 * QWidgetAction::requestWidget() is used to obtain the navigatorsWidget
91 * and to steal it from whereever it was prior.
92 * @param parent the new parent of the navigatorsWidget.
93 */
94 QWidget *createWidget(QWidget *parent) override;
95
96 /** @see QWidgetAction::deleteWidget() */
97 void deleteWidget(QWidget *widget) override;
98
99 private:
100 /**
101 * Adjusts the width of the spacings used to align the UrlNavigators with ViewContainers.
102 * This can only work nicely if up-to-date geometry of ViewContainers is cached so
103 * followViewContainersGeometry() has to have been called at least once before.
104 */
105 void adjustSpacing();
106
107 /**
108 * In Left-to-right languages the Primary side will be the left one.
109 */
110 enum Side {
111 Primary,
112 Secondary
113 };
114 /**
115 * Used to create the navigatorWidgets for both sides of the QSplitter.
116 */
117 QWidget *createNavigatorWidget(Side side) const;
118
119 /**
120 * Used to retrieve the emptyTrashButtons for the navigatorWidgets on both sides.
121 */
122 QPushButton *emptyTrashButton(Side side);
123
124 /**
125 * Creates a new empty trash button.
126 * @param urlNavigator Only when this UrlNavigator shows the trash directory
127 * will the the button be visible.
128 * @param parent Aside from the usual QObject deletion mechanisms,
129 * this parameter influences the positioning of dialog windows
130 * pertaining to this trash button.
131 */
132 QPushButton *newEmptyTrashButton(const DolphinUrlNavigator *urlNavigator, QWidget *parent) const;
133
134 enum Position {
135 Leading,
136 Trailing
137 };
138 /**
139 * Used to retrieve both the leading and trailing spacing for the navigatorWidgets
140 * on both sides. A secondary leading spacing does not exist.
141 */
142 QWidget *spacing(Side side, Position position) const;
143
144 /**
145 * Sets this action's text depending on the amount of visible UrlNavigators.
146 */
147 void updateText();
148
149 /**
150 * The defaultWidget() of this QWidgetAction.
151 */
152 std::unique_ptr<QSplitter> m_splitter;
153
154 /**
155 * adjustSpacing() has to be called slightly later than when urlChanged is emitted.
156 * This timer bridges that time.
157 */
158 std::unique_ptr<QTimer> m_adjustSpacingTimer;
159
160 // cached values
161 int m_globalXOfSplitter;
162 int m_globalXOfPrimary;
163 int m_widthOfPrimary;
164 int m_globalXOfSecondary;
165 int m_widthOfSecondary;
166 };
167
168 #endif // DOLPHINNAVIGATORSWIDGETACTION_H