-
Notifications
You must be signed in to change notification settings - Fork 817
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a sortedactivitylistmodel that automatically handles sorting of a…
…ctivities Signed-off-by: Claudio Cambra <[email protected]>
- Loading branch information
1 parent
b91dad9
commit 6136b34
Showing
12 changed files
with
221 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright (C) by Claudio Cambra <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* for more details. | ||
*/ | ||
|
||
#include "activitylistmodel.h" | ||
|
||
#include "sortedactivitylistmodel.h" | ||
|
||
namespace OCC { | ||
|
||
SortedActivityListModel::SortedActivityListModel(QObject *parent) | ||
: QSortFilterProxyModel(parent) | ||
{ | ||
} | ||
|
||
void SortedActivityListModel::sortModel() | ||
{ | ||
sort(0); | ||
} | ||
|
||
ActivityListModel* SortedActivityListModel::activityListModel() const | ||
{ | ||
return static_cast<ActivityListModel*>(sourceModel()); | ||
} | ||
|
||
void SortedActivityListModel::setActivityListModel(ActivityListModel* activityListModel) | ||
{ | ||
const auto currentSetModel = sourceModel(); | ||
|
||
if(currentSetModel) { | ||
disconnect(currentSetModel, &ActivityListModel::rowsInserted, this, &SortedActivityListModel::sortModel); | ||
disconnect(currentSetModel, &ActivityListModel::rowsMoved, this, &SortedActivityListModel::sortModel); | ||
disconnect(currentSetModel, &ActivityListModel::rowsRemoved, this, &SortedActivityListModel::sortModel); | ||
disconnect(currentSetModel, &ActivityListModel::dataChanged, this, &SortedActivityListModel::sortModel); | ||
disconnect(currentSetModel, &ActivityListModel::modelReset, this, &SortedActivityListModel::sortModel); | ||
} | ||
|
||
// Re-sort model when any changes take place | ||
connect(activityListModel, &ActivityListModel::rowsInserted, this, &SortedActivityListModel::sortModel); | ||
connect(activityListModel, &ActivityListModel::rowsMoved, this, &SortedActivityListModel::sortModel); | ||
connect(activityListModel, &ActivityListModel::rowsRemoved, this, &SortedActivityListModel::sortModel); | ||
connect(activityListModel, &ActivityListModel::dataChanged, this, &SortedActivityListModel::sortModel); | ||
connect(activityListModel, &ActivityListModel::modelReset, this, &SortedActivityListModel::sortModel); | ||
|
||
setSourceModel(activityListModel); | ||
Q_EMIT activityListModelChanged(); | ||
} | ||
|
||
bool SortedActivityListModel::lessThan(const QModelIndex &sourceLeft, const QModelIndex &sourceRight) const | ||
{ | ||
if (!sourceLeft.isValid() || !sourceRight.isValid()) { | ||
return false; | ||
} | ||
|
||
const auto leftActivity = sourceLeft.data(ActivityListModel::ActivityRole).value<Activity>(); | ||
const auto rightActivity = sourceRight.data(ActivityListModel::ActivityRole).value<Activity>(); | ||
|
||
// First compare by general activity type | ||
const auto leftType = leftActivity._type; | ||
|
||
if(leftType == Activity::DummyFetchingActivityType) { | ||
// The fetching activities dummy activity always goes at the top | ||
return true; | ||
} else if(leftType == Activity::DummyMoreActivitiesAvailableType) { | ||
// Likewise the dummy "more activities available" activity always goes at the bottom | ||
return false; | ||
} | ||
|
||
const auto rightType = rightActivity._type; | ||
|
||
if(leftType != rightType) { | ||
return leftType < rightType; | ||
} | ||
|
||
const auto leftSyncFileItemStatus = leftActivity._syncFileItemStatus; | ||
const auto rightSyncFileItemStatus = rightActivity._syncFileItemStatus; | ||
|
||
// Then compare by status | ||
if(leftSyncFileItemStatus != rightSyncFileItemStatus) { | ||
// We want to shove erors towards the top. | ||
return (leftSyncFileItemStatus != SyncFileItem::NoStatus && | ||
leftSyncFileItemStatus != SyncFileItem::Success) || | ||
leftSyncFileItemStatus == SyncFileItem::FatalError || | ||
leftSyncFileItemStatus < rightSyncFileItemStatus; | ||
} | ||
|
||
const auto leftSyncResultStatus = leftActivity._syncResultStatus; | ||
const auto rightSyncResultStatus = rightActivity._syncResultStatus; | ||
|
||
if(leftSyncResultStatus != rightSyncResultStatus) { | ||
// We only ever use SyncResult::Error in activities | ||
return (leftSyncResultStatus != SyncResult::Undefined && | ||
leftSyncResultStatus != SyncResult::Success) || | ||
leftSyncResultStatus == SyncResult::Error; | ||
} | ||
|
||
// Finally sort by time, latest first | ||
const auto leftDateTime = leftActivity._dateTime; | ||
const auto rightDateTime = rightActivity._dateTime; | ||
|
||
return leftDateTime > rightDateTime; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (C) by Claudio Cambra <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* for more details. | ||
*/ | ||
|
||
#ifndef SORTEDACTIVITYMODEL_H | ||
#define SORTEDACTIVITYMODEL_H | ||
|
||
#include <QSortFilterProxyModel> | ||
|
||
namespace OCC { | ||
|
||
class ActivityListModel; | ||
|
||
class SortedActivityListModel : public QSortFilterProxyModel | ||
{ | ||
Q_OBJECT | ||
Q_PROPERTY(ActivityListModel* activityListModel READ activityListModel WRITE setActivityListModel NOTIFY activityListModelChanged) | ||
|
||
public: | ||
explicit SortedActivityListModel(QObject *parent = nullptr); | ||
|
||
ActivityListModel *activityListModel() const; | ||
|
||
signals: | ||
void activityListModelChanged(); | ||
|
||
public slots: | ||
void setActivityListModel(ActivityListModel *activityListModel); | ||
|
||
protected: | ||
bool lessThan(const QModelIndex &sourceLeft, const QModelIndex &sourceRight) const override; | ||
|
||
private slots: | ||
void sortModel(); | ||
}; | ||
|
||
} | ||
|
||
#endif // SORTEDACTIVITYMODEL_H |
Oops, something went wrong.