]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmode/bottombarcontentscontainer.h
SVN_SILENT made messages (.desktop file) - always resolve ours
[dolphin.git] / src / selectionmode / bottombarcontentscontainer.h
1 /*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@kde.org>
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 {
57 return contents() != BottomBar::GeneralContents || m_internalContextMenu;
58 }
59
60 /**
61 * Is called when the BottomBar resizes to let this ContentsContainer know that it should adapt its contents to the new width.
62 * Adapting is done by showing or hiding labels or buttons.
63 */
64 void adaptToNewBarWidth(int newBarWidth);
65
66 public Q_SLOTS:
67 /** Adapts the contents based on the selection in the related view. */
68 void slotSelectionChanged(const KFileItemList &selection, const QUrl &baseUrl);
69
70 Q_SIGNALS:
71 /**
72 * Forwards the errors from the KFileItemAction::error() used for contextual actions.
73 */
74 void error(const QString &errorMessage);
75
76 /**
77 * When it does not make sense to show any specific contents, this signal is emitted and the receiver hides the bar.
78 * 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.
79 */
80 void barVisibilityChangeRequested(bool visible);
81
82 void selectionModeLeavingRequested();
83
84 private:
85 void addCopyContents();
86 void addCopyLocationContents();
87 void addCopyToOtherViewContents();
88 void addCutContents();
89 void addDeleteContents();
90 void addDuplicateContents();
91 /**
92 * Adds the actions of m_generalBarActions as buttons to the bar. An overflow menu button is
93 * created to make sure any amount of actions can be accessed.
94 */
95 void addGeneralContents();
96 void addMoveToOtherViewContents();
97 void addMoveToTrashContents();
98 void addPasteContents();
99 void addRenameContents();
100
101 /**
102 * Deletes every child layout and child widget of this container.
103 */
104 void emptyBarContents();
105
106 /**
107 * @returns A vector containing contextual actions for the given \a selectedItems in the \a baseUrl.
108 * Cut, Copy, Rename and MoveToTrash are always added. Any further contextual actions depend on
109 * \a selectedItems and \a baseUrl.
110 * If there are no \a selectedItems, an empty vector is returned and m_internalContextMenu is deleted.
111 * @param selectedItems The selected items for which contextual actions should be displayed.
112 * @param baseUrl Base URL of the viewport the contextual actions apply to.
113 */
114 std::vector<QAction *> contextActionsFor(const KFileItemList &selectedItems, const QUrl &baseUrl);
115
116 /**
117 * @returns the amount of pixels that can be spared to add more widgets. A negative value might
118 * be returned which signifies that some widgets should be hidden or removed from this bar to
119 * make sure that this BottomBarContentsContainer can fully fit on the BottomBar.
120 */
121 int unusedSpace() const;
122
123 /**
124 * The label isn't that important. This method hides it if there isn't enough room on the bar or
125 * shows it if there is.
126 */
127 void updateExplanatoryLabelVisibility();
128
129 /**
130 * Changes the text and enabled state of the main action button based on the amount of currently
131 * selected items and the state of the current m_mainAction.
132 * The current main action depends on the current barContents.
133 * @param selectedItems the currently selected fileItems.
134 */
135 void updateMainActionButton(const KFileItemList &selectedItems);
136
137 private:
138 /// All the actions that should be available from this bar when in general mode.
139 std::vector<ActionWithWidget> m_generalBarActions;
140 /// The context menu used to retrieve all the actions that are relevant for the current selection.
141 std::unique_ptr<DolphinContextMenu> m_internalContextMenu;
142 /// An object that is necessary to keep around for m_internalContextMenu.
143 KFileItemActions *m_fileItemActions = nullptr;
144
145 /// @see updateMainActionButtonText
146 ActionWithWidget m_mainAction = ActionWithWidget(nullptr);
147 /// The button containing all the actions that don't currently fit into the bar.
148 QPointer<QPushButton> m_overflowButton;
149 /// The actionCollection from which the actions for this bar are retrieved.
150 KActionCollection *m_actionCollection;
151 /// Describes the current contents of the bar.
152 BottomBar::Contents m_contents;
153 /// The main layout of this ContentsContainer that all the buttons and labels are added to.
154 QHBoxLayout *m_layout;
155
156 /// Caches the totalBarWidth as set in adaptToNewWidth(newBarWidth). */
157 int m_barWidth = 0;
158 /// The info label used for some of the Contents. Is hidden for narrow widths.
159 QPointer<QLabel> m_explanatoryLabel;
160 };
161
162 }
163
164 #endif // BOTTOMBARCONTENTSCONTAINER_H