Skip to content

Commit

Permalink
[BUGFIX] Fix several issues with Booked Controllers dialog (#138)
Browse files Browse the repository at this point in the history
This fixes a non-working from-to time window filter and some timezone
conversion problems.

As a drive-by change it introduces proper sorting on date/time columns.
  • Loading branch information
jonaseberle committed Apr 23, 2022
1 parent af53be4 commit e1619d8
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,6 +3,7 @@
/*.pro.user

# build
/.qtc_clangd
/.cache
/.qmake*
/Makefile
Expand Down
8 changes: 3 additions & 5 deletions src/BookedAtcDialog.cpp
Expand Up @@ -150,13 +150,11 @@ void BookedAtcDialog::on_dateTimeFilter_dateTimeChanged(QDateTime dateTime) {
_editFilterTimer.start(1000);
}

void BookedAtcDialog::on_editFilter_textChanged(QString searchStr) {
Q_UNUSED(searchStr);
void BookedAtcDialog::on_editFilter_textChanged(QString) {
_editFilterTimer.start(1000);
}

void BookedAtcDialog::on_spinHours_valueChanged(int val) {
Q_UNUSED(val);
void BookedAtcDialog::on_spinHours_valueChanged(int) {
_editFilterTimer.start(1000);
}

Expand Down Expand Up @@ -188,7 +186,7 @@ void BookedAtcDialog::performSearch() {
_bookedAtcSortModel->setFilterRegExp(regex);

//Date, Time, TimeSpan
QDateTime from = dateTimeFilter->dateTime();
QDateTime from = dateTimeFilter->dateTime().toUTC();
QDateTime to = from.addSecs(spinHours->value() * 3600);
_bookedAtcSortModel->setDateTimeRange(from, to);

Expand Down
17 changes: 10 additions & 7 deletions src/BookedAtcDialogModel.cpp
Expand Up @@ -51,21 +51,24 @@ QVariant BookedAtcDialogModel::data(const QModelIndex &index, int role) const {
case 6: return c->bookingInfoStr;
case 7: return c->link;
}
} else if(role == Qt::EditRole) { // we are faking "EditRole" to access raw data
} else if(role == Qt::EditRole) { // we are faking "EditRole" to access raw data and for sorting
BookedController* c = _controllers[index.row()];
switch(index.column()) {
case 4: return c->starts(); break;
case 5: return c->ends(); break;
case 6: return c->bookingInfoStr; break;
case 7: return c->link; break;
case 0: return c->label;
case 1: return c->facilityString();
case 2: return c->realName();
case 3: return c->starts();
case 4: return c->starts();
case 5: return c->ends();
case 6: return c->bookingInfoStr;
case 7: return c->link;
}
}

return QVariant();
}

int BookedAtcDialogModel::rowCount(const QModelIndex &parent) const {
Q_UNUSED(parent);
int BookedAtcDialogModel::rowCount(const QModelIndex&) const {
return _controllers.count();
}

Expand Down
53 changes: 30 additions & 23 deletions src/BookedAtcSortFilter.cpp
Expand Up @@ -4,33 +4,40 @@

#include "BookedAtcSortFilter.h"

#include "BookedAtcDialog.h"

bool BookedAtcSortFilter::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const {
if (!this->_from.isNull() && !this->_to.isNull()) {
bool BookedAtcSortFilter::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
{
if (this->_from.isValid() && this->_to.isValid()) {
if (sourceModel()->index(source_row, 0, source_parent).isValid()) {
QDateTime starts =
sourceModel()->index(source_row, 5, source_parent)
.data(Qt::EditRole).toDateTime();
QDateTime ends =
sourceModel()->index(source_row, 6, source_parent)
.data(Qt::EditRole).toDateTime();
if (_to == _from && _from < ends) return
(QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent));
if (_from < starts && _to > starts) return
(QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent));
if (_from < ends && _to > starts) return
(QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent));
return (false);
QDateTime starts = sourceModel()->index(source_row, 4, source_parent).data(Qt::EditRole).toDateTime().toUTC();
QDateTime ends = sourceModel()->index(source_row, 5, source_parent).data(Qt::EditRole).toDateTime().toUTC();
if (
(_to == _from && _from <= ends) // _to == _from means for: ever
|| (_from <= starts && _to >= starts)
|| (_from <= ends && _to >= starts)
) {
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
}
return false;
}
} else
return(QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent));
return (false);
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
return false;
}

void BookedAtcSortFilter::setDateTimeRange(QDateTime& dtfrom, QDateTime& dtto) {
this->_from = dtfrom;
this->_to = dtto;
this->invalidateFilter();
bool BookedAtcSortFilter::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
auto leftData = sourceModel()->data(left, Qt::EditRole);
auto rightData = sourceModel()->data(right, Qt::EditRole);

if (leftData.userType() == QMetaType::QDateTime) {
return leftData.toDateTime() < rightData.toDateTime();
}
return QString::localeAwareCompare(leftData.toString(), rightData.toString()) < 0;
}

void BookedAtcSortFilter::setDateTimeRange(QDateTime& from, QDateTime& to)
{
this->_from = from;
this->_to = to;
this->invalidateFilter();
}
2 changes: 1 addition & 1 deletion src/BookedAtcSortFilter.h
Expand Up @@ -15,7 +15,7 @@ class BookedAtcSortFilter : public QSortFilterProxyModel {

protected:
bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const;

bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
private:
QDateTime _from, _to;
};
Expand Down
16 changes: 8 additions & 8 deletions src/BookedController.cpp
Expand Up @@ -224,16 +224,16 @@ QString BookedController::mapLabel() const {

QDateTime BookedController::starts() const {
return QDateTime(
QDate::fromString(date, QString("yyyyMMdd")),
QTime::fromString(timeFrom, QString("HHmm")),
Qt::UTC
);
QDate::fromString(date, QString("yyyyMMdd")),
QTime::fromString(timeFrom, QString("HHmm")),
Qt::UTC
);
}

QDateTime BookedController::ends() const {
return QDateTime(
QDate::fromString(date, QString("yyyyMMdd")),
QTime::fromString(timeTo, QString("HHmm")),
Qt::UTC
);
QDate::fromString(date, QString("yyyyMMdd")),
QTime::fromString(timeTo, QString("HHmm")),
Qt::UTC
);
}

0 comments on commit e1619d8

Please sign in to comment.