7 #include "toolbarlayoutdelegate.h"
9 #include "loggingcategory.h"
10 #include "toolbarlayout.h"
14 , m_component(component)
19 void ToolBarDelegateIncubator::setStateCallback(std::function<
void(
QQuickItem *)> callback)
21 m_stateCallback = callback;
24 void ToolBarDelegateIncubator::setCompletedCallback(std::function<
void(ToolBarDelegateIncubator *)> callback)
26 m_completedCallback = callback;
29 void ToolBarDelegateIncubator::create()
31 m_component->create(*
this, m_context);
34 bool ToolBarDelegateIncubator::isFinished()
39 void ToolBarDelegateIncubator::setInitialState(
QObject *
object)
41 auto item = qobject_cast<QQuickItem *>(
object);
43 m_stateCallback(item);
50 qCWarning(KirigamiLog) <<
"Could not create delegate for ToolBarLayout";
51 const auto e = errors();
52 for (
const auto &error : e) {
53 qCWarning(KirigamiLog) <<
error;
59 m_completedCallback(
this);
64 ToolBarLayoutDelegate::ToolBarLayoutDelegate(
ToolBarLayout *parent)
70 ToolBarLayoutDelegate::~ToolBarLayoutDelegate()
72 if (m_fullIncubator) {
73 m_fullIncubator->clear();
74 delete m_fullIncubator;
76 if (m_iconIncubator) {
77 m_iconIncubator->clear();
78 delete m_iconIncubator;
85 m_icon->disconnect(
this);
90 QObject *ToolBarLayoutDelegate::action()
const
95 void ToolBarLayoutDelegate::setAction(
QObject *action)
97 if (action == m_action) {
102 QObject::disconnect(m_action, SIGNAL(visibleChanged()),
this, SLOT(actionVisibleChanged()));
103 QObject::disconnect(m_action, SIGNAL(displayHintChanged()),
this, SLOT(displayHintChanged()));
108 if (m_action->property(
"visible").isValid()) {
109 QObject::connect(m_action, SIGNAL(visibleChanged()),
this, SLOT(actionVisibleChanged()));
110 m_actionVisible = m_action->property(
"visible").toBool();
113 if (m_action->property(
"displayHint").isValid()) {
114 QObject::connect(m_action, SIGNAL(displayHintChanged()),
this, SLOT(displayHintChanged()));
122 m_fullIncubator =
new ToolBarDelegateIncubator(fullComponent, qmlContext(fullComponent));
123 m_fullIncubator->setStateCallback(callback);
124 m_fullIncubator->setCompletedCallback([
this](ToolBarDelegateIncubator *incubator) {
125 if (incubator->isError()) {
126 qCWarning(KirigamiLog) <<
"Could not create delegate for ToolBarLayout";
127 const auto errors = incubator->errors();
128 for (const auto &error : errors) {
129 qCWarning(KirigamiLog) << error;
134 m_full = qobject_cast<QQuickItem *>(incubator->object());
135 m_full->setVisible(
false);
137 m_parent->relayout();
140 m_parent->relayout();
148 m_parent->relayout();
152 m_iconIncubator =
new ToolBarDelegateIncubator(iconComponent, qmlContext(iconComponent));
153 m_iconIncubator->setStateCallback(callback);
154 m_iconIncubator->setCompletedCallback([
this](ToolBarDelegateIncubator *incubator) {
155 if (incubator->isError()) {
156 qCWarning(KirigamiLog) <<
"Could not create delegate for ToolBarLayout";
157 const auto errors = incubator->errors();
158 for (const auto &error : errors) {
159 qCWarning(KirigamiLog) << error;
164 m_icon = qobject_cast<QQuickItem *>(incubator->object());
165 m_icon->setVisible(
false);
167 m_parent->relayout();
170 m_parent->relayout();
178 m_parent->relayout();
183 m_fullIncubator->create();
184 m_iconIncubator->create();
187 bool ToolBarLayoutDelegate::isReady()
const
192 bool ToolBarLayoutDelegate::isActionVisible()
const
194 return m_actionVisible;
197 bool ToolBarLayoutDelegate::isHidden()
const
199 return DisplayHint::isDisplayHintSet(m_displayHint, DisplayHint::AlwaysHide);
202 bool ToolBarLayoutDelegate::isIconOnly()
const
204 return DisplayHint::isDisplayHintSet(m_displayHint, DisplayHint::IconOnly);
207 bool ToolBarLayoutDelegate::isKeepVisible()
const
209 return DisplayHint::isDisplayHintSet(m_displayHint, DisplayHint::KeepVisible);
212 bool ToolBarLayoutDelegate::isVisible()
const
214 return m_iconVisible || m_fullVisible;
217 void ToolBarLayoutDelegate::hide()
219 m_iconVisible =
false;
220 m_fullVisible =
false;
221 ensureItemVisibility();
224 void ToolBarLayoutDelegate::showFull()
226 m_iconVisible =
false;
227 m_fullVisible =
true;
230 void ToolBarLayoutDelegate::showIcon()
232 m_iconVisible =
true;
233 m_fullVisible =
false;
236 void ToolBarLayoutDelegate::show()
238 ensureItemVisibility();
241 void ToolBarLayoutDelegate::setPosition(qreal x, qreal y)
249 void ToolBarLayoutDelegate::setHeight(qreal height)
251 m_full->setHeight(height);
252 m_icon->setHeight(height);
255 qreal ToolBarLayoutDelegate::width()
const
258 return m_icon->width();
260 return m_full->width();
263 qreal ToolBarLayoutDelegate::height()
const
266 return m_icon->height();
268 return m_full->height();
271 qreal ToolBarLayoutDelegate::implicitWidth()
const
274 return m_icon->implicitWidth();
276 return m_full->implicitWidth();
279 qreal ToolBarLayoutDelegate::implicitHeight()
const
282 return m_icon->implicitHeight();
284 return m_full->implicitHeight();
287 qreal ToolBarLayoutDelegate::maxHeight()
const
289 return std::max(m_full->height(), m_icon->height());
292 qreal ToolBarLayoutDelegate::iconWidth()
const
294 return m_icon->width();
297 qreal ToolBarLayoutDelegate::fullWidth()
const
299 return m_full->width();
302 void ToolBarLayoutDelegate::actionVisibleChanged()
304 m_actionVisible = m_action->property(
"visible").toBool();
305 m_parent->relayout();
308 void ToolBarLayoutDelegate::displayHintChanged()
311 m_parent->relayout();
314 void ToolBarLayoutDelegate::cleanupIncubators()
316 if (m_fullIncubator && m_fullIncubator->isFinished()) {
317 delete m_fullIncubator;
318 m_fullIncubator =
nullptr;
321 if (m_iconIncubator && m_iconIncubator->isFinished()) {
322 delete m_iconIncubator;
323 m_iconIncubator =
nullptr;