]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmode/bottombarcontentscontainer.h
6cb66fcc1bb5f3b72109b5852f83742fced35bee
[dolphin.git] / src / selectionmode / bottombarcontentscontainer.h
1 /*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@zohomail.eu>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8 #ifndef BOTTOMBARCONTENTSCONTAINER_H
9 #define BOTTOMBARCONTENTSCONTAINER_H
10
11 #include "actionwithwidget.h"
12 #include "bottombar.h"
13
14 #include <QPointer>
15 #include <QPushButton>
16 #include <QWidget>
17
18 class DolphinContextMenu;
19 class KActionCollection;
20 class KFileItemActions;
21 class KFileItemList;
22 class QHBoxLayout;
23 class QLabel;
24 class QUrl;
25
26 namespace SelectionMode
27 {
28
29 /**
30 * @brief An internal widget of BottomBar that controls the visible contents/widgets on it.
31 *
32 * This class should only be interacted with from the BottomBar class.
33 * @see BottomBar
34 */
35 class BottomBarContentsContainer : public QWidget
36 {
37 Q_OBJECT
38
39 public:
40 /**
41 * @param actionCollection the collection where the actions for the contents are retrieved from
42 * @param parent the parent widget. Typically a ScrollView within the BottomBar
43 */
44 explicit BottomBarContentsContainer(KActionCollection *actionCollection, QWidget *parent);
45
46 /**
47 * @param contents The kind of contents that should be contained instead.
48 */
49 void resetContents(BottomBar::Contents contents);
50 inline BottomBar::Contents contents() const
51 {
52 return m_contents;
53 };
54
55 inline bool hasSomethingToShow() {
56 return contents() != BottomBar::GeneralContents || m_internalContextMenu;
57 }
58
59 /**
60 * Is called when the BottomBar resizes to let this ContentsContainer know that it should adapt its contents to the new width.
61 * Adapting is done by showing or hiding labels or buttons.
62 */
63 void adaptToNewBarWidth(int newBarWidth);
64
65 public Q_SLOTS:
66 /** Adapts the contents based on the selection in the related view. */
67 void slotSelectionChanged(const KFileItemList &selection, const QUrl &baseUrl);
68
69 Q_SIGNALS:
70 /**
71 * Forwards the errors from the KFileItemAction::error() used for contextual actions.
72 */
73 void error(const QString &errorMessage);
74
75 /**
76 * When it does not make sense to show any specific contents, this signal is emitted and the receiver hides the bar.
77 * Later it might make sense to show it again e.g. because the user selected items. Then this signal is used to request showing of the bar.
78 */
79 void barVisibilityChangeRequested(bool visible);
80
81 void selectionModeLeavingRequested();
82
83 private:
84 void addCopyContents();
85 void addCopyLocationContents();
86 void addCopyToOtherViewContents();
87 void addCutContents();
88 void addDeleteContents();
89 void addDuplicateContents();
90 /**
91 * Adds the actions of m_generalBarActions as buttons to the bar. An overflow menu button is
92 * created to make sure any amount of actions can be accessed.
93 */
94 void addGeneralContents();
95 void addMoveToOtherViewContents();
96 void addMoveToTrashContents();
97 void addPasteContents();
98 void addRenameContents();
99
100 /**
101 * Deletes every child layout and child widget of this container.
102 */
103 void emptyBarContents();
104
105 /**
106 * @returns A vector containing contextual actions for the given \a selectedItems in the \a baseUrl.
107 * Cut, Copy, Rename and MoveToTrash are always added. Any further contextual actions depend on
108 * \a selectedItems and \a baseUrl.
109 * If there are no \a selectedItems, an empty vector is returned and m_internalContextMenu is deleted.
110 * @param selectedItems The selected items for which contextual actions should be displayed.
111 * @param baseUrl Base URL of the viewport the contextual actions apply to.
112 */
113 std::vector<QAction *> contextActionsFor(const KFileItemList &selectedItems, const QUrl &baseUrl);
114
115 /**
116 * @returns the amount of pixels that can be spared to add more widgets. A negative value might
117 * be returned which signifies that some widgets should be hidden or removed from this bar to
118 * make sure that this BottomBarContentsContainer can fully fit on the BottomBar.
119 */
120 int unusedSpace() const;
121
122 /**
123 * The label isn't that important. This method hides it if there isn't enough room on the bar or
124 * shows it if there is.
125 */
126 void updateExplanatoryLabelVisibility();
127
128 /**
129 * Changes the text and enabled state of the main action button based on the amount of currently
130 * selected items and the state of the current m_mainAction.
131 * The current main action depends on the current barContents.
132 * @param selectedItems the currently selected fileItems.
133 */
134 void updateMainActionButton(const KFileItemList &selectedItems);
135
136 private:
137 /// All the actions that should be available from this bar when in general mode.
138 std::vector<ActionWithWidget> m_generalBarActions;
139 /// The context menu used to retrieve all the actions that are relevant for the current selection.
140 std::unique_ptr<DolphinContextMenu> m_internalContextMenu;
141 /// An object that is necessary to keep around for m_internalContextMenu.
142 KFileItemActions *m_fileItemActions = nullptr;
143
144 /// @see updateMainActionButtonText
145 ActionWithWidget m_mainAction = ActionWithWidget(nullptr);
146 /// The button containing all the actions that don't currently fit into the bar.
147 QPointer<QPushButton> m_overflowButton;
148 /// The actionCollection from which the actions for this bar are retrieved.
149 KActionCollection *m_actionCollection;
150 /// Describes the current contents of the bar.
151 BottomBar::Contents m_contents;
152 /// The main layout of this ContentsContainer that all the buttons and labels are added to.
153 QHBoxLayout *m_layout;
154
155 /// Caches the totalBarWidth as set in adaptToNewWidth(newBarWidth). */
156 int m_barWidth = 0;
157 /// The info label used for some of the Contents. Is hidden for narrow widths.
158 QPointer<QLabel> m_explanatoryLabel;
159 };
160
161 }
162
163 #endif // BOTTOMBARCONTENTSCONTAINER_H