Commit 468a5169 authored by Michael Pyne's avatar Michael Pyne

Port sort function for list of playlists to KF5.

Now that sorting is enabled, it turned out that the sort really was
broken for everybody (sorry!). The old Qt 3 and 4 "sort by subclassing
the list item" feature wasn't actually ported into the Qt 5
compatibilility list view. So although we were defining the compare
function, nothing was actually using it.

Things seemed to work fine on my local system but I suspect that's
because I had playlists from KDE 4 times that I copied over which would
have already sorted the special playlists to the beginning.

Since the old method doesn't work I've just added a hidden sort key
column for now. The "proper solution" is probably to use a
QSortFilterProxyModel but that will be some time away.

CHANGELOG:Restore proper sorting of the list of playlists.
BUG:402398
FIXED-IN:18.12.1
parent dc3124e4
......@@ -76,14 +76,16 @@ PlaylistBox::PlaylistBox(PlayerManager *player, QWidget *parent, QStackedWidget
setRootIsDecorated(false);
setContextMenuPolicy(Qt::CustomContextMenu);
setDropIndicatorShown(true);
setColumnCount(2); // Use fake column for sorting
setColumnHidden(1, true);
setSortingEnabled(true);
sortByColumn(1, Qt::AscendingOrder);
header()->blockSignals(true);
header()->hide();
header()->blockSignals(false);
sortByColumn(0);
viewport()->setAcceptDrops(true);
setDropIndicatorShown(true);
setSelectionMode(QAbstractItemView::ExtendedSelection);
......@@ -653,7 +655,7 @@ PlaylistBox::Item *PlaylistBox::Item::m_collectionItem = 0;
PlaylistBox::Item::Item(PlaylistBox *listBox, const QString &icon, const QString &text, Playlist *l)
: QObject(listBox), QTreeWidgetItem(listBox, QStringList(text)),
PlaylistObserver(l),
m_playlist(l), m_text(text), m_iconName(icon), m_sortedFirst(false)
m_playlist(l), m_iconName(icon), m_sortedFirst(false)
{
init();
}
......@@ -661,7 +663,7 @@ PlaylistBox::Item::Item(PlaylistBox *listBox, const QString &icon, const QString
PlaylistBox::Item::Item(Item *parent, const QString &icon, const QString &text, Playlist *l)
: QObject(parent->listView()), QTreeWidgetItem(parent, QStringList(text)),
PlaylistObserver(l),
m_playlist(l), m_text(text), m_iconName(icon), m_sortedFirst(false)
m_playlist(l), m_iconName(icon), m_sortedFirst(false)
{
init();
}
......@@ -671,37 +673,13 @@ PlaylistBox::Item::~Item()
}
int PlaylistBox::Item::compare(QTreeWidgetItem *i, int col, bool) const
{
Item *otherItem = static_cast<Item *>(i);
PlaylistBox *playlistBox = static_cast<PlaylistBox *>(treeWidget());
if(m_playlist == playlistBox->upcomingPlaylist() && otherItem->m_playlist != CollectionList::instance())
return -1;
if(otherItem->m_playlist == playlistBox->upcomingPlaylist() && m_playlist != CollectionList::instance())
return 1;
if(m_sortedFirst && !otherItem->m_sortedFirst)
return -1;
else if(otherItem->m_sortedFirst && !m_sortedFirst)
return 1;
return text(col).toLower().localeAwareCompare(i->text(col).toLower());
}
// FIXME paintcell
// FIXME paintcell
/*void PlaylistBox::Item::paintCell(QPainter *painter, const QColorGroup &colorGroup, int column, int width, int align)
{
PlaylistBox *playlistBox = static_cast<PlaylistBox *>(listView());
playlistBox->viewMode()->paintCell(this, painter, colorGroup, column, width, align);
}*/
void PlaylistBox::Item::setText(int column, const QString &text)
{
m_text = text;
QTreeWidgetItem::setText(column, text);
}
void PlaylistBox::Item::setup()
{
listView()->viewMode()->setupItem(this);
......@@ -713,15 +691,13 @@ void PlaylistBox::Item::setup()
void PlaylistBox::Item::slotSetName(const QString &name)
{
if(listView()) {
setText(0, name);
setSelected(true);
setText(0, name); // Display name
setText(1, sortTextFor(name));
setSelected(true);
treeWidget()->sortItems(0, Qt::AscendingOrder);
treeWidget()->scrollToItem(treeWidget()->currentItem());
//FIXME viewmode
//listView()->viewMode()->queueRefresh();
}
treeWidget()->scrollToItem(this);
//FIXME viewmode
//listView()->viewMode()->queueRefresh();
}
void PlaylistBox::Item::playingItemHasChanged()
......@@ -746,8 +722,9 @@ void PlaylistBox::Item::init()
list->setupItem(this);
const QString itemText(text());
setIcon(0, QIcon::fromTheme(m_iconName));
list->addNameToDict(m_text);
list->addNameToDict(itemText);
if(m_playlist) {
connect(m_playlist, SIGNAL(signalNameChanged(QString)),
......@@ -764,6 +741,22 @@ void PlaylistBox::Item::init()
if(m_playlist == list->historyPlaylist() || m_playlist == list->upcomingPlaylist())
m_sortedFirst = true;
setText(1, sortTextFor(itemText));
}
QString PlaylistBox::Item::sortTextFor(const QString &name) const
{
// Collection List goes before everything, then
// playlists that 'sort first', then remainder of
// playlists.
const auto prefix
= (playlist() == CollectionList::instance())
? QStringLiteral("0")
: m_sortedFirst
? QStringLiteral("1")
: QStringLiteral("2");
return prefix + name;
}
// vim: set et sw=4 tw=0 sta:
......@@ -156,21 +156,19 @@ public:
virtual ~Item();
protected:
using QTreeWidgetItem::text;
Item(PlaylistBox *listBox, const QString &icon, const QString &text, Playlist *l = 0);
Item(Item *parent, const QString &icon, const QString &text, Playlist *l = 0);
Playlist *playlist() const { return m_playlist; }
PlaylistBox *listView() const { return static_cast<PlaylistBox *>(QTreeWidgetItem::treeWidget()); }
QString iconName() const { return m_iconName; }
QString text() const { return m_text; }
QString text() const { return QTreeWidgetItem::text(0); }
void setSortedFirst(bool first = true) { m_sortedFirst = first; }
virtual int compare(QTreeWidgetItem *i, int col, bool) const;
/*virtual void paintCell(QPainter *p, const QColorGroup &colorGroup, int column, int width, int align);
virtual void paintFocus(QPainter *, const QColorGroup &, const QRect &) {}*/
virtual void setText(int column, const QString &text);
virtual QString text(int column) const { return QTreeWidgetItem::text(column); }
virtual void setup();
......@@ -193,9 +191,9 @@ protected slots:
private:
// setup() was already taken.
void init();
QString sortTextFor(const QString &name) const;
Playlist *m_playlist;
QString m_text;
QString m_iconName;
bool m_sortedFirst;
static Item *m_collectionItem;
......
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