From e404727c4518fc1860cda33ab93524611673eae9 Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Tue, 14 Jan 2025 21:46:02 +0100 Subject: [PATCH] =?UTF-8?q?Show=20the=20reservation=E2=80=99s=20holder=20n?= =?UTF-8?q?ame=20in=20timelineview?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TimelineView is not a general control that can be used with any model, thus i do not need the complexity of Qt’s QQmlDelegateModel incubation task to initialize required properties. I _would_ have used it if it were available in C++, but since it is not, what i mean is that i do not need to _reimplement_ all of it for my case: i already know what required properties there are. --- src/ReservationsPage.qml | 12 +++++++++++- src/timelineview.cpp | 30 ++++++++++++++++++++---------- src/timelineview.h | 2 +- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/ReservationsPage.qml b/src/ReservationsPage.qml index 771c471..619015a 100644 --- a/src/ReservationsPage.qml +++ b/src/ReservationsPage.qml @@ -80,9 +80,19 @@ Page { viewportX: ListView.view.contentX delegate: Rectangle { + id: booking + + required property string holder + border.color: "black" border.width: 1 - color: "blue" + color: "lightblue" + + Label { + anchors.fill: parent + elide: Text.ElideRight + text: booking.holder + } } } header: Pane { diff --git a/src/timelineview.cpp b/src/timelineview.cpp index 9df9052..9b5f41d 100644 --- a/src/timelineview.cpp +++ b/src/timelineview.cpp @@ -167,16 +167,26 @@ void TimelineView::componentComplete() populate(); } -TimelineView::Item *TimelineView::createItem(qint64 day, qint64 len) +TimelineView::Item *TimelineView::createItem(qint64 day, const TimelineModel::Item &modelItem) { - QQuickItem *item = m_reusableItems.isEmpty() ? qobject_cast(m_delegate->create( - m_delegate->creationContext())) - : m_reusableItems.takeLast(); - if (!item) { - qmlWarning(m_delegate) << TimelineView::tr("Delegate must be of Item type"); - return nullptr; + QQuickItem *item; + + if (m_reusableItems.isEmpty()) { + QVariantMap initialProperties{ + {"holder", modelItem.holder}, + }; + item = qobject_cast( + m_delegate->createWithInitialProperties(initialProperties, + m_delegate->creationContext())); + if (!item) { + qmlWarning(m_delegate) << TimelineView::tr("Delegate must be of Item type"); + return nullptr; + } + } else { + item = m_reusableItems.takeLast(); + item->setProperty("holder", modelItem.holder); } - auto *viewItem = new TimelineView::Item(day, len, *item, *this); + auto *viewItem = new TimelineView::Item(day, modelItem.nights, *item, *this); return viewItem; } @@ -235,7 +245,7 @@ void TimelineView::populate() if (!m_items.isEmpty() && m_items.first()->day <= day) { continue; } - TimelineView::Item *viewItem = createItem(day, item->nights); + TimelineView::Item *viewItem = createItem(day, *item); if (!viewItem) { break; } @@ -263,7 +273,7 @@ void TimelineView::populate() if (!m_items.isEmpty() && m_items.last()->day >= day) { continue; } - TimelineView::Item *viewItem = createItem(day, item->nights); + TimelineView::Item *viewItem = createItem(day, *item); if (!viewItem) { break; } diff --git a/src/timelineview.h b/src/timelineview.h index c462cd5..c11da37 100644 --- a/src/timelineview.h +++ b/src/timelineview.h @@ -61,7 +61,7 @@ private: struct Item; - Item *createItem(qint64 day, qint64 len); + Item *createItem(qint64 day, const TimelineModel::Item &modelItem); void releaseItem(Item *item); void drainItems();