Commit 81c0dcc2 authored by Kacper Kasper's avatar Kacper Kasper

Fix columns sorting.

Summary:
QTreeWidgetItem in Qt5 requires overloading < operator to implement
custom sorting. Use existing compare methods to do that.

Additionally, natural sorting was implemented for strings.
This allows to sort by filenames like file_<number>.

BUG:389130

Test Plan:
Click on columns to sort them.
Columns are sorted by artist, album and track number.

Reviewers: mpyne

Differential Revision: https://phabricator.kde.org/D10130
parent 516a69f9
......@@ -19,9 +19,11 @@
#include <config-juk.h>
#include <kiconloader.h>
#include <QPixmap>
#include <QCollator>
#include <QFileInfo>
#include <QHeaderView>
#include <QGlobalStatic>
#include <QPixmap>
#include "collectionlist.h"
#include "musicbrainzquery.h"
......@@ -43,6 +45,14 @@ static void startMusicBrainzQuery(const FileHandle &file)
#endif
}
static int naturalCompare(const QString &first, const QString &second)
{
static QCollator collator;
collator.setNumericMode(true);
collator.setCaseSensitivity(Qt::CaseInsensitive);
return collator.compare(first, second);
}
////////////////////////////////////////////////////////////////////////////////
// PlaylistItem public methods
////////////////////////////////////////////////////////////////////////////////
......@@ -292,7 +302,7 @@ PlaylistItem::PlaylistItem(CollectionList *parent) :
QTreeWidgetItem::paintCell(p, colorGroup, column, width, align);
}*/
int PlaylistItem::compare(QTreeWidgetItem *item, int column, bool ascending) const
int PlaylistItem::compare(const QTreeWidgetItem *item, int column, bool ascending) const
{
// reimplemented from QListViewItem
......@@ -301,7 +311,7 @@ int PlaylistItem::compare(QTreeWidgetItem *item, int column, bool ascending) con
if(!item)
return 0;
PlaylistItem *playlistItem = static_cast<PlaylistItem *>(item);
const PlaylistItem *playlistItem = static_cast<const PlaylistItem *>(item);
// The following statments first check to see if you can sort based on the
// specified column. If the values for the two PlaylistItems are the same
......@@ -337,9 +347,9 @@ int PlaylistItem::compare(const PlaylistItem *firstItem, const PlaylistItem *sec
return 0;
if(column < offset) {
QString first = firstItem->text(column).toLower();
QString second = secondItem->text(column).toLower();
return first.localeAwareCompare(second);
QString first = firstItem->text(column);
QString second = secondItem->text(column);
return naturalCompare(first, second);
}
switch(column - offset) {
......@@ -376,11 +386,17 @@ int PlaylistItem::compare(const PlaylistItem *firstItem, const PlaylistItem *sec
return 1;
break;
default:
return QString::localeAwareCompare(firstItem->d->metadata[column - offset],
secondItem->d->metadata[column - offset]);
return naturalCompare(firstItem->d->metadata[column - offset],
secondItem->d->metadata[column - offset]);
}
}
bool PlaylistItem::operator<(const QTreeWidgetItem &other) const
{
bool ascending = playlist()->header()->sortIndicatorOrder() == Qt::AscendingOrder;
return compare(&other, playlist()->sortColumn(), ascending) == -1;
}
bool PlaylistItem::isValid() const
{
return bool(d->fileHandle.tag());
......
......@@ -179,9 +179,11 @@ protected:
//virtual void paintCell(QPainter *p, const QColorGroup &cg, int column, int width, int align);
//virtual void paintFocus(QPainter *, const QColorGroup &, const QRect &) {}
virtual int compare(QTreeWidgetItem *item, int column, bool ascending) const;
virtual int compare(const QTreeWidgetItem *item, int column, bool ascending) const;
int compare(const PlaylistItem *firstItem, const PlaylistItem *secondItem, int column, bool ascending) const;
bool operator<(const QTreeWidgetItem &other) const;
bool isValid() const;
void setTrackId(quint32 id);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment