Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
PIM
Itinerary
Commits
48d612e2
Commit
48d612e2
authored
May 10, 2018
by
Volker Krause
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Start to refactor the timeline model to support nested elements
parent
d7eb4095
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
20 deletions
+38
-20
src/app/timelinemodel.cpp
src/app/timelinemodel.cpp
+23
-19
src/app/timelinemodel.h
src/app/timelinemodel.h
+15
-1
No files found.
src/app/timelinemodel.cpp
View file @
48d612e2
...
...
@@ -111,10 +111,12 @@ void TimelineModel::setReservationManager(ReservationManager* mgr)
{
beginResetModel
();
m_resMgr
=
mgr
;
m_reservationIds
=
mgr
->
reservations
();
m_reservationIds
.
push_back
({});
// today marker
std
::
sort
(
m_reservationIds
.
begin
(),
m_reservationIds
.
end
(),
[
this
](
const
QString
&
lhs
,
const
QString
&
rhs
)
{
return
isBeforeReservation
(
m_resMgr
->
reservation
(
lhs
),
m_resMgr
->
reservation
(
rhs
));
for
(
const
auto
&
resId
:
mgr
->
reservations
())
{
m_elements
.
push_back
(
Element
{
resId
,
SelfContained
});
}
m_elements
.
push_back
(
Element
{});
// today marker
std
::
sort
(
m_elements
.
begin
(),
m_elements
.
end
(),
[
this
](
const
Element
&
lhs
,
const
Element
&
rhs
)
{
return
isBeforeReservation
(
m_resMgr
->
reservation
(
lhs
.
id
),
m_resMgr
->
reservation
(
rhs
.
id
));
});
connect
(
mgr
,
&
ReservationManager
::
reservationAdded
,
this
,
&
TimelineModel
::
reservationAdded
);
connect
(
mgr
,
&
ReservationManager
::
reservationUpdated
,
this
,
&
TimelineModel
::
reservationUpdated
);
...
...
@@ -127,7 +129,7 @@ int TimelineModel::rowCount(const QModelIndex& parent) const
{
if
(
parent
.
isValid
()
||
!
m_resMgr
)
return
0
;
return
m_
reservationId
s
.
size
();
return
m_
element
s
.
size
();
}
QVariant
TimelineModel
::
data
(
const
QModelIndex
&
index
,
int
role
)
const
...
...
@@ -135,7 +137,7 @@ QVariant TimelineModel::data(const QModelIndex& index, int role) const
if
(
!
index
.
isValid
()
||
!
m_resMgr
)
return
{};
const
auto
res
=
m_resMgr
->
reservation
(
m_
reservationId
s
.
at
(
index
.
row
()));
const
auto
res
=
m_resMgr
->
reservation
(
m_
element
s
.
at
(
index
.
row
())
.
id
);
switch
(
role
)
{
case
PassRole
:
return
QVariant
::
fromValue
(
m_passMgr
->
pass
(
passId
(
res
)));
...
...
@@ -168,7 +170,7 @@ QVariant TimelineModel::data(const QModelIndex& index, int role) const
return
{};
case
TodayEmptyRole
:
if
(
res
.
isNull
())
{
return
index
.
row
()
==
(
m_reservationId
s
.
size
()
-
1
)
||
relevantDate
(
m_resMgr
->
reservation
(
m_
reservationId
s
.
at
(
index
.
row
()
+
1
)))
>
QDate
::
currentDate
();
return
index
.
row
()
==
(
int
)(
m_element
s
.
size
()
-
1
)
||
relevantDate
(
m_resMgr
->
reservation
(
m_
element
s
.
at
(
index
.
row
()
+
1
)
.
id
))
>
QDate
::
currentDate
();
}
return
{};
case
IsTodayRole
:
...
...
@@ -192,38 +194,40 @@ QHash<int, QByteArray> TimelineModel::roleNames() const
int
TimelineModel
::
todayRow
()
const
{
return
m_reservationIds
.
indexOf
({});
const
auto
it
=
std
::
find_if
(
m_elements
.
begin
(),
m_elements
.
end
(),
[](
const
Element
&
e
)
{
return
e
.
id
.
isEmpty
();
});
return
std
::
distance
(
m_elements
.
begin
(),
it
);
}
void
TimelineModel
::
reservationAdded
(
const
QString
&
resId
)
{
auto
it
=
std
::
lower_bound
(
m_
reservationIds
.
begin
(),
m_reservationId
s
.
end
(),
resId
,
[
this
](
const
QString
&
lhs
,
const
QString
&
rhs
)
{
return
isBeforeReservation
(
m_resMgr
->
reservation
(
lhs
),
m_resMgr
->
reservation
(
rhs
));
auto
it
=
std
::
lower_bound
(
m_
elements
.
begin
(),
m_element
s
.
end
(),
resId
,
[
this
](
const
Element
&
lhs
,
const
QString
&
rhs
)
{
return
isBeforeReservation
(
m_resMgr
->
reservation
(
lhs
.
id
),
m_resMgr
->
reservation
(
rhs
));
});
auto
index
=
std
::
distance
(
m_
reservationId
s
.
begin
(),
it
);
auto
index
=
std
::
distance
(
m_
element
s
.
begin
(),
it
);
beginInsertRows
({},
index
,
index
);
m_
reservationIds
.
insert
(
it
,
resId
);
m_
elements
.
insert
(
it
,
Element
{
resId
,
SelfContained
}
);
endInsertRows
();
emit
todayRowChanged
();
}
void
TimelineModel
::
reservationUpdated
(
const
QString
&
resId
)
{
auto
it
=
std
::
lower_bound
(
m_
reservationIds
.
begin
(),
m_reservationId
s
.
end
(),
resId
,
[
this
](
const
QString
&
lhs
,
const
QString
&
rhs
)
{
return
isBeforeReservation
(
m_resMgr
->
reservation
(
lhs
),
m_resMgr
->
reservation
(
rhs
));
auto
it
=
std
::
lower_bound
(
m_
elements
.
begin
(),
m_element
s
.
end
(),
resId
,
[
this
](
const
Element
&
lhs
,
const
QString
&
rhs
)
{
return
isBeforeReservation
(
m_resMgr
->
reservation
(
lhs
.
id
),
m_resMgr
->
reservation
(
rhs
));
});
auto
row
=
std
::
distance
(
m_
reservationId
s
.
begin
(),
it
);
auto
row
=
std
::
distance
(
m_
element
s
.
begin
(),
it
);
emit
dataChanged
(
index
(
row
,
0
),
index
(
row
,
0
));
}
void
TimelineModel
::
reservationRemoved
(
const
QString
&
resId
)
{
const
auto
i
ndex
=
m_reservationIds
.
indexOf
(
resId
);
if
(
i
ndex
<
0
)
{
const
auto
i
t
=
std
::
find_if
(
m_elements
.
begin
(),
m_elements
.
end
(),
[
resId
](
const
Element
&
e
)
{
return
e
.
id
==
resId
;
}
);
if
(
i
t
==
m_elements
.
end
()
)
{
return
;
}
beginRemoveRows
({},
index
,
index
);
m_reservationIds
.
remove
(
index
);
const
auto
row
=
std
::
distance
(
m_elements
.
begin
(),
it
);
beginRemoveRows
({},
row
,
row
);
m_elements
.
erase
(
it
);
endRemoveRows
();
emit
todayRowChanged
();
}
src/app/timelinemodel.h
View file @
48d612e2
...
...
@@ -19,6 +19,7 @@
#define TIMELINEMODEL_H
#include <QAbstractListModel>
#include <QDateTime>
class
PkPassManager
;
class
ReservationManager
;
...
...
@@ -49,6 +50,14 @@ public:
};
Q_ENUM
(
ElementType
)
// indicates whether an element is self-contained or the beginning/end of a longer timespan/range
enum
RangeType
{
SelfContained
,
RangeBegin
,
RangeEnd
};
Q_ENUM
(
RangeType
)
explicit
TimelineModel
(
QObject
*
parent
=
nullptr
);
~
TimelineModel
();
...
...
@@ -71,7 +80,12 @@ private:
PkPassManager
*
m_passMgr
=
nullptr
;
ReservationManager
*
m_resMgr
=
nullptr
;
QVector
<
QString
>
m_reservationIds
;
struct
Element
{
QString
id
;
// reservation id
RangeType
rangeType
;
};
std
::
vector
<
Element
>
m_elements
;
};
#endif // TIMELINEMODEL_H
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment