Compare commits
3 Commits
ab1ebd61b6
...
8c222c6f6b
Author | SHA1 | Date |
---|---|---|
|
8c222c6f6b | |
|
579b4f3f55 | |
|
a9745ec117 |
|
@ -7,11 +7,12 @@ Label {
|
|||
|
||||
required property Item dragParent
|
||||
required property string holder
|
||||
required property int nights
|
||||
required property int reservationId
|
||||
required property string reservationStatus
|
||||
|
||||
Drag.active: dragHandler.active
|
||||
Drag.hotSpot.x: 0
|
||||
Drag.hotSpot.y: 0
|
||||
Drag.hotSpot.y: height / 2
|
||||
elide: Text.ElideRight
|
||||
padding: 2
|
||||
text: holder
|
||||
|
|
|
@ -99,29 +99,16 @@ Control {
|
|||
id: verticalScroll
|
||||
|
||||
}
|
||||
delegate: DropArea {
|
||||
property real contentWidth: ListView.view.contentWidth
|
||||
required property string name
|
||||
delegate: TimelineView {
|
||||
required property TimelineModel timeline
|
||||
property real viewportWidth: ListView.view.width
|
||||
property real viewportX: ListView.view.contentX
|
||||
|
||||
height: control.rowHeight
|
||||
width: contentWidth
|
||||
|
||||
onPositionChanged: function (drag) {
|
||||
drag.source.y = y;
|
||||
drag.source.x = (Math.floor(drag.x / control.dayWidth) + .5) * control.dayWidth;
|
||||
}
|
||||
|
||||
TimelineView {
|
||||
dayWidth: control.dayWidth
|
||||
fromDate: control.fromDate
|
||||
height: parent.height
|
||||
model: parent.timeline
|
||||
height: control.rowHeight
|
||||
model: timeline
|
||||
toDate: control.toDate
|
||||
viewportWidth: parent.viewportWidth
|
||||
viewportX: parent.viewportX
|
||||
viewportWidth: ListView.view.width
|
||||
viewportX: ListView.view.contentX
|
||||
|
||||
delegate: Reservation {
|
||||
dragParent: timelineList.contentItem
|
||||
|
@ -134,7 +121,6 @@ Control {
|
|||
palette: control.palette
|
||||
}
|
||||
}
|
||||
}
|
||||
header: Rectangle {
|
||||
border.color: Fusion.outline(control.palette)
|
||||
color: control.palette.base
|
||||
|
@ -201,5 +187,51 @@ Control {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
DropArea {
|
||||
property int prevDay
|
||||
property int prevLodging
|
||||
|
||||
function addDays(date, days) {
|
||||
var newDate = new Date(date);
|
||||
newDate.setDate(date.getDate() + days);
|
||||
return newDate;
|
||||
}
|
||||
|
||||
function findDayAndLodging(drag: DragEvent): list<int> {
|
||||
const pos = timelineList.contentItem.mapFromItem(this, drag.x, drag.y);
|
||||
const day = Math.floor(pos.x / control.dayWidth);
|
||||
const lodging = Math.floor(pos.y / control.rowHeight);
|
||||
return [day, lodging];
|
||||
}
|
||||
|
||||
function isSpotAvailable(day: int, lodging: int, reservation: Reservation): bool {
|
||||
const view = timelineList.itemAtIndex(lodging);
|
||||
if (!view) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return view.timeline.areDatesAvailable(addDays(control.fromDate, day), addDays(control.fromDate, day + reservation.nights), reservation.reservationId);
|
||||
}
|
||||
|
||||
anchors.fill: timelineList
|
||||
anchors.leftMargin: -30 * control.dayWidth
|
||||
|
||||
onEntered: function (drag) {
|
||||
[prevDay, prevLodging] = findDayAndLodging(drag);
|
||||
}
|
||||
onPositionChanged: function (drag) {
|
||||
let [day, lodging] = findDayAndLodging(drag);
|
||||
if ((day != prevDay || lodging != prevLodging) && isSpotAvailable(day, lodging, drag.source)) {
|
||||
prevDay = day;
|
||||
prevLodging = lodging;
|
||||
} else {
|
||||
day = prevDay;
|
||||
lodging = prevLodging;
|
||||
}
|
||||
drag.source.y = lodging * control.rowHeight;
|
||||
drag.source.x = (day + .5) * control.dayWidth;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,31 @@ void TimelineModel::update(const QList<Item *> &items)
|
|||
m_items.assign(items.begin(), items.end());
|
||||
}
|
||||
|
||||
bool TimelineModel::areDatesAvailable(QDate arrival, QDate departure, int excludedId) const
|
||||
{
|
||||
auto [begin, end] = indexesOf(arrival, departure);
|
||||
if (begin == end) {
|
||||
return true;
|
||||
}
|
||||
const Item *first = m_items.at(begin);
|
||||
if (first->id == excludedId) {
|
||||
begin++;
|
||||
if (begin == end) {
|
||||
return true;
|
||||
}
|
||||
first = m_items.at(begin);
|
||||
}
|
||||
const Item *last = m_items.at(end - 1);
|
||||
if (last->id == excludedId) {
|
||||
--end;
|
||||
if (begin == end) {
|
||||
return true;
|
||||
}
|
||||
last = m_items.at(end - 1);
|
||||
}
|
||||
return arrival >= last->departure() || departure <= first->arrival;
|
||||
}
|
||||
|
||||
std::pair<qsizetype, qsizetype> TimelineModel::indexesOf(QDate from, QDate to) const
|
||||
{
|
||||
qsizetype begin = searchIndex(from);
|
||||
|
@ -25,13 +50,16 @@ std::pair<qsizetype, qsizetype> TimelineModel::indexesOf(QDate from, QDate to) c
|
|||
|
||||
while (begin > 0) {
|
||||
--begin;
|
||||
const Item *item = m_items.at(begin);
|
||||
if (m_items.at(begin)->departure() < from) {
|
||||
++begin;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (begin == 0 && m_items.at(begin)->arrival >= to) {
|
||||
return std::make_pair(begin, begin);
|
||||
}
|
||||
|
||||
for (qsizetype end = begin + 1; end < m_items.count(); end++) {
|
||||
if (m_items.at(end)->arrival >= to) {
|
||||
return std::make_pair(begin, end);
|
||||
|
|
|
@ -31,6 +31,7 @@ public:
|
|||
|
||||
void update(const QList<Item *> &items);
|
||||
|
||||
Q_INVOKABLE bool areDatesAvailable(QDate arrival, QDate departure, int excludeId) const;
|
||||
std::pair<qsizetype, qsizetype> indexesOf(QDate from, QDate to) const;
|
||||
const Item *at(qsizetype index) const;
|
||||
|
||||
|
|
|
@ -174,6 +174,8 @@ TimelineView::Item *TimelineView::createItem(qint64 day, const TimelineModel::It
|
|||
if (m_reusableItems.isEmpty()) {
|
||||
QVariantMap initialProperties{
|
||||
{"holder", modelItem.holder},
|
||||
{"nights", modelItem.nights},
|
||||
{"reservationId", modelItem.id},
|
||||
{"reservationStatus", modelItem.status},
|
||||
};
|
||||
item = qobject_cast<QQuickItem *>(
|
||||
|
@ -186,6 +188,8 @@ TimelineView::Item *TimelineView::createItem(qint64 day, const TimelineModel::It
|
|||
} else {
|
||||
item = m_reusableItems.takeLast();
|
||||
item->setProperty("holder", modelItem.holder);
|
||||
item->setProperty("nights", modelItem.nights);
|
||||
item->setProperty("reservationId", modelItem.id);
|
||||
item->setProperty("reservationStatus", modelItem.status);
|
||||
}
|
||||
auto *viewItem = new TimelineView::Item(day, modelItem.nights, *item, *this);
|
||||
|
|
Loading…
Reference in New Issue