Debian Codegraph @

src/Gui/QSint/actionpanel/actiongroup.cpp

raw
1 // SPDX-License-Identifier: LGPL-3.0-only
2 /***************************************************************************
3 * *
4 * Copyright: https://code.google.com/p/qsint/ *
5 * License: LGPL *
6 * *
7 ***************************************************************************/
8 #include "actiongroup.h"
9 #include "taskheader_p.h"
10 #include "taskgroup_p.h"
11
12 #include <QPainter>
13 #include <QVBoxLayout>
14 #include <QTimer>
15
16 namespace QSint
17 {
18
19 ActionGroup::ActionGroup(QWidget *parent)
20 : QWidget(parent),
21 myHeader(new TaskHeader(QPixmap(), "", false, this))
22 {
23 myHeader->setVisible(false);
24 init(false);
25 }
26
27 ActionGroup::ActionGroup(const QString &title, bool expandable, QWidget *parent)
28 : QWidget(parent),
29 myHeader(new TaskHeader(QPixmap(), title, expandable, this))
30 {
31 init(true);
32 }
33
34 ActionGroup::ActionGroup(const QPixmap &icon, const QString &title, bool expandable, QWidget *parent)
35 : QWidget(parent),
36 myHeader(new TaskHeader(icon, title, expandable, this))
37 {
38 init(true);
39 }
40
41 ActionGroup::~ActionGroup() = default;
42
43 void ActionGroup::init(bool hasHeader)
44 {
45 m_foldStep = 0;
46 myScheme = ActionPanelScheme::defaultScheme();
47 setBackgroundRole(QPalette::Button);
48 setAutoFillBackground(true);
49
50 auto *layout = new QVBoxLayout(this);
51 layout->setContentsMargins(0, 0, 0, 0);
52 layout->setSpacing(0);
53
54 layout->addWidget(myHeader);
55
56 auto *separator = new QFrame(this);
57 separator->setFrameShape(QFrame::HLine);
58 separator->setFrameShadow(QFrame::Raised);
59 separator->setFixedHeight(separatorHeight);
60 separator->setContentsMargins(8, 0, 8, 0);
61 separator->setProperty("class", "separator");
62 layout->addWidget(separator);
63
64 myGroup = new TaskGroup(this, hasHeader);
65 layout->addWidget(myGroup);
66
67 myDummy = new QWidget(this);
68 layout->addWidget(myDummy);
69 myDummy->hide();
70
71 connect(myHeader, &TaskHeader::activated, this, &ActionGroup::showHide);
72 }
73
74 void ActionGroup::setScheme(ActionPanelScheme *scheme)
75 {
76 myScheme = scheme;
77 myHeader->setScheme(scheme);
78 myGroup->setScheme(scheme);
79 update();
80 }
81
82 QBoxLayout* ActionGroup::groupLayout()
83 {
84 return myGroup->groupLayout();
85 }
86
87 ActionLabel* ActionGroup::addAction(QAction *action, bool addToLayout, bool addStretch)
88 {
89 if (!action) return nullptr;
90
91 auto *label = new ActionLabel(action, this);
92 myGroup->addActionLabel(label, addToLayout, addStretch);
93
94 return label;
95 }
96
97 ActionLabel* ActionGroup::addActionLabel(ActionLabel *label, bool addToLayout, bool addStretch)
98 {
99 if (!label) return nullptr;
100
101 myGroup->addActionLabel(label, addToLayout, addStretch);
102 return label;
103 }
104
105 bool ActionGroup::addWidget(QWidget *widget, bool addToLayout, bool addStretch)
106 {
107 return myGroup->addWidget(widget, addToLayout, addStretch);
108 }
109
110 void ActionGroup::showHide()
111 {
112 if (m_foldStep || !myHeader->expandable()) return;
113
114 if (myGroup->isVisible())
115 {
116 m_foldPixmap = myGroup->transparentRender();
117 m_tempHeight = m_fullHeight = myGroup->height();
118 m_foldDelta = m_fullHeight / myScheme->groupFoldSteps;
119 m_foldStep = myScheme->groupFoldSteps;
120 m_foldDirection = -1;
121
122 myGroup->hide();
123 myDummy->setFixedSize(myGroup->size());
124
125 QTimer::singleShot(myScheme->groupFoldDelay, this, &ActionGroup::processHide);
126 }
127 else
128 {
129 m_foldStep = myScheme->groupFoldSteps;
130 m_foldDirection = 1;
131 m_tempHeight = 0;
132
133 QTimer::singleShot(myScheme->groupFoldDelay, this, &ActionGroup::processShow);
134 }
135 myDummy->show();
136 }
137
138 void ActionGroup::processHide()
139 {
140 if (--m_foldStep == 0)
141 {
142 myDummy->hide();
143 myHeader->setFold(false);
144 setFixedHeight(myHeader->height() + separatorHeight);
145 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
146 return;
147 }
148
149 m_tempHeight -= m_foldDelta;
150 myDummy->setFixedHeight(m_tempHeight);
151 setFixedHeight(myDummy->height() + myHeader->height() + separatorHeight);
152
153 QTimer::singleShot(myScheme->groupFoldDelay, this, &ActionGroup::processHide);
154 }
155
156 void ActionGroup::processShow()
157 {
158 if (--m_foldStep == 0)
159 {
160 myDummy->hide();
161 m_foldPixmap = QPixmap();
162 myGroup->show();
163 myHeader->setFold(true);
164 setFixedHeight(m_fullHeight + myHeader->height() + separatorHeight);
165 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
166 setMaximumHeight(QWIDGETSIZE_MAX);
167 return;
168 }
169
170 m_tempHeight += m_foldDelta;
171 myDummy->setFixedHeight(m_tempHeight);
172 setFixedHeight(myDummy->height() + myHeader->height() + separatorHeight);
173
174 QTimer::singleShot(myScheme->groupFoldDelay, this, &ActionGroup::processShow);
175 }
176
177 void ActionGroup::paintEvent(QPaintEvent *event)
178 {
179 Q_UNUSED(event);
180 QPainter p(this);
181
182 if (!myDummy->isVisible()) {
183 return;
184 }
185 if (myScheme->groupFoldThaw)
186 {
187 double opacity = (m_foldDirection < 0)
188 ? static_cast<double>(m_foldStep) / myScheme->groupFoldSteps
189 : static_cast<double>(myScheme->groupFoldSteps - m_foldStep) / myScheme->groupFoldSteps;
190 p.setOpacity(opacity);
191 }
192
193 switch (myScheme->groupFoldEffect)
194 {
195 case ActionPanelScheme::ShrunkFolding:
196 p.drawPixmap(myDummy->pos(), m_foldPixmap.scaled(myDummy->size()));
197 break;
198 case ActionPanelScheme::SlideFolding:
199 p.drawPixmap(myDummy->pos(), m_foldPixmap,
200 QRect(0, m_foldPixmap.height() - myDummy->height(),
201 m_foldPixmap.width(), myDummy->width()));
202 break;
203 default:
204 p.drawPixmap(myDummy->pos(), m_foldPixmap);
205 }
206 }
207
208 bool ActionGroup::isExpandable() const
209 {
210 return myHeader->expandable();
211 }
212
213 void ActionGroup::setExpandable(bool expandable)
214 {
215 myHeader->setExpandable(expandable);
216 }
217
218 bool ActionGroup::hasHeader() const
219 {
220 return myHeader->isVisible();
221 }
222
223 void ActionGroup::setHeader(bool enable)
224 {
225 myHeader->setVisible(enable);
226 }
227
228 QString ActionGroup::headerText() const
229 {
230 return myHeader->myTitle->text();
231 }
232
233 void ActionGroup::setHeaderText(const QString &headerText)
234 {
235 myHeader->myTitle->setText(headerText);
236 }
237
238 void ActionGroup::setHeaderIcon(const QPixmap &icon)
239 {
240 myHeader->myTitle->setIcon(icon);
241 }
242
243 QSize ActionGroup::minimumSizeHint() const
244 {
245 return {200, 65};
246 }
247
248 } // namespace