]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/chip.h
Clazy fix
[dolphin.git] / src / search / chip.h
1 /*
2 SPDX-FileCopyrightText: 2025 Felix Ernst <felixernst@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6
7 #ifndef CHIP_H
8 #define CHIP_H
9
10 #include "dolphinquery.h"
11 #include "selectors/dateselector.h"
12 #include "selectors/filetypeselector.h"
13 #include "selectors/minimumratingselector.h"
14 #include "selectors/tagsselector.h"
15 #include "updatablestateinterface.h"
16
17 #include <QComboBox>
18 #include <QLayout>
19 #include <QToolButton>
20 #include <QWidget>
21
22 #include <type_traits>
23
24 namespace Search
25 {
26
27 /**
28 * @brief The non-template base class for the template class Chip.
29 *
30 * @see Chip below.
31 *
32 * Template classes cannot have Qt signals. This class works around that by being a non-template class which the template Chip class then inherits from.
33 * This base class contains all non-template logic of Chip.
34 */
35 class ChipBase : public QWidget, public UpdatableStateInterface
36 {
37 Q_OBJECT
38
39 public:
40 ChipBase(std::shared_ptr<const DolphinQuery> dolphinQuery, QWidget *parent = nullptr);
41
42 Q_SIGNALS:
43 /** Is emitted whenever settings have changed and a new search might be necessary. */
44 void configurationChanged(const Search::DolphinQuery &dolphinQuery);
45
46 protected:
47 void paintEvent(QPaintEvent *event) override;
48
49 protected:
50 QToolButton *m_removeButton = nullptr;
51 };
52
53 /**
54 * @brief A button-sized UI component for modifying or removing search filters.
55 *
56 * A template widget is taken and this Chip forms a button-like outline around it. The Chip has a close button on the side to remove itself, which communicates
57 * to the user that the effect of the widget inside this Chip no longer applies. The functionality of the widget inside is not affected by the Chip.
58 *
59 * Most logic of this class is in the non-template ChipBase base class.
60 * @see ChipBase above.
61 */
62 template<class Selector>
63 class Chip : public ChipBase
64 {
65 public:
66 Chip(std::shared_ptr<const DolphinQuery> dolphinQuery, QWidget *parent = nullptr)
67 : ChipBase{dolphinQuery, parent}
68 , m_selector{new Selector{dolphinQuery, this}}
69 {
70 // Make the selector flat within the chip.
71 if constexpr (std::is_base_of<QComboBox, Selector>::value) {
72 m_selector->setFrame(false);
73 } else if constexpr (std::is_base_of<QToolButton, Selector>::value) {
74 m_selector->setAutoRaise(true);
75 }
76 setFocusProxy(m_selector);
77 setTabOrder(m_selector, m_removeButton);
78
79 connect(m_selector, &Selector::configurationChanged, this, &ChipBase::configurationChanged);
80
81 // The m_removeButton does not directly remove the Chip. Instead the Selector's removeRestriction() method will emit ChipBase::configurationChanged()
82 // with a DolphinQuery object that effectively removes the effects of the Selector. This in turn will then eventually remove this Chip when the new
83 // state of the Search UI components is propagated through the various UpdatableStateInterface::updateStateToMatch() methods.
84 connect(m_removeButton, &QAbstractButton::clicked, m_selector, &Selector::removeRestriction);
85
86 layout()->addWidget(m_selector);
87 layout()->addWidget(m_removeButton);
88 };
89
90 private:
91 void updateState(const std::shared_ptr<const DolphinQuery> &dolphinQuery) override
92 {
93 m_selector->updateStateToMatch(dolphinQuery);
94 }
95
96 private:
97 Selector *const m_selector;
98 };
99 }
100
101 #endif