]> 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 * Adds this action to the mainWindow's toolbar and saves the change
46 * in the users ui configuration file.
47 * @return true if successful. Otherwise false.
48 */
49 bool addToToolbarAndSave(KXmlGuiWindow *mainWindow);
50
51 /**
52 * The secondary UrlNavigator is only created on-demand. Such an action is not necessary
53 * for the primary UrlNavigator which is created preemptively.
54 *
55 * This method should preferably only be called when:
56 * - Split view is activated in the active tab
57 * OR
58 * - A switch to a tab that is already in split view mode is occuring
59 */
60 void createSecondaryUrlNavigator();
61
62 /**
63 * Notify the primary UrlNavigator of changes in geometry of the ViewContainer it tries to be
64 * aligned with. Only call this method if there is no secondary UrlNavigator.
65 */
66 void followViewContainerGeometry(int globalXOfPrimary, int widthOfPrimary);
67 /**
68 * Notify this widget of changes in geometry of the ViewContainers it tries to be
69 * aligned with.
70 */
71 void followViewContainersGeometry(int globalXOfPrimary, int widthOfPrimary,
72 int globalXOfSecondary, int widthOfSecondary);
73
74 /**
75 * @return the primary UrlNavigator.
76 */
77 DolphinUrlNavigator *primaryUrlNavigator() const;
78 /**
79 * @return the secondary UrlNavigator and nullptr if it doesn't exist.
80 */
81 DolphinUrlNavigator *secondaryUrlNavigator() const;
82
83 /**
84 * Change the visibility of the secondary UrlNavigator including spacing.
85 * @param visible Setting this to false will completely hide the secondary side of this
86 * WidgetAction's QSplitter making the QSplitter effectively disappear.
87 */
88 void setSecondaryNavigatorVisible(bool visible);
89
90 private:
91 /**
92 * Adjusts the width of the spacings used to align the UrlNavigators with ViewContainers.
93 * This can only work nicely if up-to-date geometry of ViewContainers is cached so
94 * followViewContainersGeometry() has to have been called at least once before.
95 */
96 void adjustSpacing();
97
98 /**
99 * In Left-to-right languages the Primary side will be the left one.
100 */
101 enum Side {
102 Primary,
103 Secondary
104 };
105 /**
106 * Used to create the navigatorWidgets for both sides of the QSplitter.
107 */
108 QWidget *createNavigatorWidget(Side side) const;
109
110 /**
111 * Used to retrieve the emptyTrashButtons for the navigatorWidgets on both sides.
112 */
113 QPushButton *emptyTrashButton(Side side);
114
115 /**
116 * Creates a new empty trash button.
117 * @param urlNavigator Only when this UrlNavigator shows the trash directory
118 * will the the button be visible.
119 * @param parent Aside from the usual QObject deletion mechanisms,
120 * this parameter influences the positioning of dialog windows
121 * pertaining to this trash button.
122 */
123 QPushButton *newEmptyTrashButton(const DolphinUrlNavigator *urlNavigator, QWidget *parent) const;
124
125 enum Position {
126 Leading,
127 Trailing
128 };
129 /**
130 * Used to retrieve both the leading and trailing spacing for the navigatorWidgets
131 * on both sides. A secondary leading spacing does not exist.
132 */
133 QWidget *spacing(Side side, Position position) const;
134
135 /**
136 * Sets this action's text depending on the amount of visible UrlNavigators.
137 */
138 void updateText();
139
140 /**
141 * The defaultWidget() of this QWidgetAction.
142 */
143 std::unique_ptr<QSplitter> m_splitter;
144
145 /**
146 * adjustSpacing() has to be called slightly later than when urlChanged is emitted.
147 * This timer bridges that time.
148 */
149 std::unique_ptr<QTimer> m_adjustSpacingTimer;
150
151 // cached values
152 int m_globalXOfSplitter;
153 int m_globalXOfPrimary;
154 int m_widthOfPrimary;
155 int m_globalXOfSecondary;
156 int m_widthOfSecondary;
157 };
158
159 #endif // DOLPHINNAVIGATORSWIDGETACTION_H