Commit 645550df authored by Matthieu Gallien's avatar Matthieu Gallien 🎵

allow to load/save sort preferences for order and roles in all views

parent 1b5e83e1
......@@ -8,6 +8,8 @@
#include "viewslistdata.h"
#include "viewconfigurationdata.h"
#include "elisa_settings.h"
#include <QtTest>
class ViewManagerTests: public QObject
......@@ -37,10 +39,13 @@ private Q_SLOTS:
qRegisterMetaType<QAbstractItemModel*>("QAbstractItemModel*");
qRegisterMetaType<QAbstractProxyModel*>("QAbstractProxyModel*");
qRegisterMetaType<DataTypes::DataType>("DataTypes::DataType");
QStandardPaths::setTestModeEnabled(true);
Elisa::ElisaConfiguration::instance(QStringLiteral("testfoo"));
}
void openAlbumViewTest()
{
Elisa::ElisaConfiguration::self()->setDefaults();
ViewManager viewManager;
ViewsListData viewsData;
viewManager.setViewsData(&viewsData);
......@@ -85,6 +90,7 @@ private Q_SLOTS:
void openAlbumView2Test()
{
Elisa::ElisaConfiguration::self()->setDefaults();
ViewManager viewManager;
ViewsListData viewsData;
viewManager.setViewsData(&viewsData);
......@@ -175,6 +181,7 @@ private Q_SLOTS:
void openAlbumView3Test()
{
Elisa::ElisaConfiguration::self()->setDefaults();
ViewManager viewManager;
ViewsListData viewsData;
viewManager.setViewsData(&viewsData);
......@@ -261,6 +268,7 @@ private Q_SLOTS:
void openArtistViewTest()
{
Elisa::ElisaConfiguration::self()->setDefaults();
ViewManager viewManager;
ViewsListData viewsData;
viewManager.setViewsData(&viewsData);
......@@ -305,6 +313,7 @@ private Q_SLOTS:
void openArtistView2Test()
{
Elisa::ElisaConfiguration::self()->setDefaults();
ViewManager viewManager;
ViewsListData viewsData;
viewManager.setViewsData(&viewsData);
......@@ -395,6 +404,7 @@ private Q_SLOTS:
void openArtistView3Test()
{
Elisa::ElisaConfiguration::self()->setDefaults();
ViewManager viewManager;
ViewsListData viewsData;
viewManager.setViewsData(&viewsData);
......@@ -481,6 +491,7 @@ private Q_SLOTS:
void openGenreViewTest()
{
Elisa::ElisaConfiguration::self()->setDefaults();
ViewManager viewManager;
ViewsListData viewsData;
viewManager.setViewsData(&viewsData);
......@@ -525,6 +536,7 @@ private Q_SLOTS:
void openGenreView2Test()
{
Elisa::ElisaConfiguration::self()->setDefaults();
ViewManager viewManager;
ViewsListData viewsData;
viewManager.setViewsData(&viewsData);
......@@ -615,6 +627,7 @@ private Q_SLOTS:
void openGenreView3Test()
{
Elisa::ElisaConfiguration::self()->setDefaults();
ViewManager viewManager;
ViewsListData viewsData;
viewManager.setViewsData(&viewsData);
......@@ -701,6 +714,7 @@ private Q_SLOTS:
void openArtistFromGenreViewTest()
{
Elisa::ElisaConfiguration::self()->setDefaults();
ViewManager viewManager;
ViewsListData viewsData;
viewManager.setViewsData(&viewsData);
......@@ -769,6 +783,7 @@ private Q_SLOTS:
void openArtistViewAndAlbumFromAnotherArtistTest()
{
Elisa::ElisaConfiguration::self()->setDefaults();
ViewManager viewManager;
ViewsListData viewsData;
viewManager.setViewsData(&viewsData);
......@@ -848,6 +863,7 @@ private Q_SLOTS:
void openNowPlayingViewTest()
{
Elisa::ElisaConfiguration::self()->setDefaults();
ViewManager viewManager;
ViewsListData viewsData;
viewManager.setViewsData(&viewsData);
......
......@@ -36,5 +36,9 @@
<choice name="AllGenres" />
</choices>
</entry>
<entry key="SortRolePreferences" type="StringList">
</entry>
<entry key="SortOrderPreferences" type="StringList">
</entry>
</group>
</kcfg>
......@@ -71,6 +71,7 @@ RowLayout {
sortRoleNames: configurationData.sortRoleNames,
sortOrder: configurationData.sortOrder,
sortOrderNames: configurationData.sortOrderNames,
viewManager: viewManager,
opacity: 1,
}, StackView.Immediate)
}
......@@ -106,6 +107,7 @@ RowLayout {
sortRoleNames: configurationData.sortRoleNames,
sortOrder: configurationData.sortOrder,
sortOrderNames: configurationData.sortOrderNames,
viewManager: viewManager,
opacity: 1,
}, StackView.Immediate)
}
......
......@@ -32,6 +32,7 @@ FocusScope {
property alias sortOrderNames: gridView.sortOrderNames
property alias sortOrder: gridView.sortOrder
property bool modelIsInitialized: false
property alias viewManager: gridView.viewManager
focus: true
......
......@@ -32,6 +32,7 @@ FocusScope {
property bool displaySingleAlbum: false
property alias radioCase: listView.showCreateRadioButton
property bool modelIsInitialized: false
property alias viewManager: listView.viewManager
function openMetaDataView(databaseId, url, entryType) {
metadataLoader.setSource("MediaTrackMetadataView.qml",
......
......@@ -33,6 +33,7 @@ FocusScope {
property var stackView
property url defaultIcon
property int depth: 1
property alias viewManager: navigationBar.viewManager
signal enqueue(var fullData, string name)
signal replaceAndPlay(var fullData, string name)
......
......@@ -41,6 +41,7 @@ FocusScope {
property alias showCreateRadioButton: navigationBar.showCreateRadioButton
property alias navigationBar: navigationBar
property int depth: 1
property alias viewManager: navigationBar.viewManager
signal goBackRequested()
signal showArtist(var name)
......
......@@ -9,6 +9,7 @@ import QtQuick 2.7
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.3
import org.kde.kirigami 2.8 as Kirigami
import org.kde.elisa 1.0
ColumnLayout {
id: navigationBar
......@@ -35,6 +36,8 @@ ColumnLayout {
property alias sortOrder: sortMenu.sortOrder
property alias sortOrderNames: sortMenu.sortOrderNames
property ViewManager viewManager
signal enqueue();
signal replaceAndPlay();
signal createRadio();
......@@ -145,6 +148,10 @@ ColumnLayout {
SortMenu {
id: sortMenu
onSortOrderChanged: if (viewManager) viewManager.sortOrderChanged(sortOrder)
onSortRoleChanged: if (viewManager) viewManager.sortRoleChanged(sortRole)
}
},
FlatButtonWithToolTip {
......
......@@ -14,10 +14,12 @@
#include "models/gridviewproxymodel.h"
#include "models/filebrowsermodel.h"
#include "models/filebrowserproxymodel.h"
#include "elisa_settings.h"
#include <KI18n/KLocalizedString>
#include <QQmlEngine>
#include <QMetaEnum>
class ViewManagerPrivate
{
......@@ -317,8 +319,10 @@ void ViewManager::openViewFromData(const ViewParameters &viewParamaters)
viewParamaters.mMainImage, viewParamaters.mDataType,
newModel, proxyModel, viewParamaters.mFallbackItemIcon,
viewParamaters.mDataFilter,
viewParamaters.mSortRole, viewParamaters.mSortRoles, viewParamaters.mSortRoleNames,
viewParamaters.mSortOrder, viewParamaters.mSortOrderNames,
computePreferredSortRole(viewParamaters.mSortRole),
viewParamaters.mSortRoles, viewParamaters.mSortRoleNames,
computePreferredSortOrder(viewParamaters.mSortOrder),
viewParamaters.mSortOrderNames,
viewParamaters.mViewCanBeRated, viewParamaters.mShowSecondaryTextOnDelegates,
viewParamaters.mIsTreeModel);
......@@ -342,8 +346,10 @@ void ViewManager::openViewFromData(const ViewParameters &viewParamaters)
viewParamaters.mMainTitle, viewParamaters.mSecondaryTitle,
viewParamaters.mMainImage, viewParamaters.mDataType,
newModel, proxyModel, viewParamaters.mDataFilter,
viewParamaters.mSortRole, viewParamaters.mSortRoles, viewParamaters.mSortRoleNames,
viewParamaters.mSortOrder, viewParamaters.mSortOrderNames,
computePreferredSortRole(viewParamaters.mSortRole),
viewParamaters.mSortRoles, viewParamaters.mSortRoleNames,
computePreferredSortOrder(viewParamaters.mSortOrder),
viewParamaters.mSortOrderNames,
viewParamaters.mAlbumCardinality, viewParamaters.mAlbumViewStyle,
viewParamaters.mRadioSpecificStyle, viewParamaters.mIsTreeModel);
......@@ -389,6 +395,76 @@ void ViewManager::applyFilter(ViewParameters &nextViewParameters,
}
}
QString ViewManager::buildViewId() const
{
const auto &entryTypeMetaEnum = QMetaEnum::fromType<ElisaUtils::PlayListEntryType>();
QString viewId;
for (const auto &oneView : d->mViewParametersStack) {
viewId += QString::fromLatin1(entryTypeMetaEnum.valueToKey(oneView.mDataType)) + QStringLiteral("::");
}
return viewId;
}
QStringList::iterator ViewManager::findViewPreference(QStringList &list, const QString &viewId) const
{
auto itViewPreference = list.end();
for(itViewPreference = list.begin(); itViewPreference != list.end(); ++itViewPreference) {
auto parts = itViewPreference->splitRef(QStringLiteral("=="));
if (parts.size() != 2) {
continue;
}
if (parts[0] == viewId) {
break;
}
}
return itViewPreference;
}
Qt::SortOrder ViewManager::computePreferredSortOrder(Qt::SortOrder initialValue) const
{
auto currentSortOrderPreferences = Elisa::ElisaConfiguration::sortOrderPreferences();
auto viewId = buildViewId();
auto itViewPreference = findViewPreference(currentSortOrderPreferences, viewId);
if (itViewPreference != currentSortOrderPreferences.end()) {
auto result = itViewPreference->splitRef(QStringLiteral("=="));
if (result.size() == 2) {
const auto &sortOrderMetaEnum = QMetaEnum::fromType<Qt::SortOrder>();
bool conversionOk;
auto newValue = static_cast<Qt::SortOrder>(sortOrderMetaEnum.keyToValue(result[1].toLatin1().data(), &conversionOk));
if (conversionOk) {
initialValue = newValue;
}
}
}
return initialValue;
}
int ViewManager::computePreferredSortRole(int initialValue) const
{
auto currentSortRolePreferences = Elisa::ElisaConfiguration::sortRolePreferences();
auto viewId = buildViewId();
auto itViewPreference = findViewPreference(currentSortRolePreferences, viewId);
if (itViewPreference != currentSortRolePreferences.end()) {
auto result = itViewPreference->splitRef(QStringLiteral("=="));
if (result.size() == 2) {
const auto &sortRoleMetaEnum = QMetaEnum::fromType<DataTypes::ColumnsRoles>();
bool conversionOk;
auto newValue = static_cast<DataTypes::ColumnsRoles>(sortRoleMetaEnum.keyToValue(result[1].toLatin1().data(), &conversionOk));
if (conversionOk) {
initialValue = newValue;
}
}
}
return initialValue;
}
void ViewManager::goBack()
{
qCDebug(orgKdeElisaViews()) << "ViewManager::goBack" << d->mViewParametersStack.size()
......@@ -424,5 +500,53 @@ void ViewManager::setViewsData(ViewsListData *viewsData)
}
}
void ViewManager::sortOrderChanged(Qt::SortOrder sortOrder)
{
auto currentSortOrderPreferences = Elisa::ElisaConfiguration::sortOrderPreferences();
auto viewId = buildViewId();
auto itViewPreference = findViewPreference(currentSortOrderPreferences, viewId);
const auto &sortOrderMetaEnum = QMetaEnum::fromType<Qt::SortOrder>();
auto enumStringValue = sortOrderMetaEnum.valueToKey(sortOrder);
if (!enumStringValue) {
return;
}
QString newSortOrderPreference = viewId + QStringLiteral("==") + QString::fromLatin1(enumStringValue);
if (itViewPreference != currentSortOrderPreferences.end()) {
(*itViewPreference) = newSortOrderPreference;
} else {
currentSortOrderPreferences.push_back(newSortOrderPreference);
}
Elisa::ElisaConfiguration::setSortOrderPreferences(currentSortOrderPreferences);
Elisa::ElisaConfiguration::self()->save();
}
void ViewManager::sortRoleChanged(int sortRole)
{
auto currentSortRolePreferences = Elisa::ElisaConfiguration::sortRolePreferences();
auto viewId = buildViewId();
auto itViewPreference = findViewPreference(currentSortRolePreferences, viewId);
const auto &sortRoleMetaEnum = QMetaEnum::fromType<DataTypes::ColumnsRoles>();
auto enumStringValue = sortRoleMetaEnum.valueToKey(static_cast<DataTypes::ColumnsRoles>(sortRole));
if (!enumStringValue) {
return;
}
QString newSortRolePreference = viewId + QStringLiteral("==") + QString::fromLatin1(enumStringValue);
if (itViewPreference != currentSortRolePreferences.end()) {
(*itViewPreference) = newSortRolePreference;
} else {
currentSortRolePreferences.push_back(newSortRolePreference);
}
Elisa::ElisaConfiguration::setSortRolePreferences(currentSortRolePreferences);
Elisa::ElisaConfiguration::self()->save();
}
#include "moc_viewmanager.cpp"
......@@ -136,6 +136,10 @@ public Q_SLOTS:
void setViewsData(ViewsListData* viewsData);
void sortOrderChanged(Qt::SortOrder sortOrder);
void sortRoleChanged(int sortRole);
private:
void openViewFromData(const ViewParameters &viewParamaters);
......@@ -143,6 +147,14 @@ private:
void applyFilter(ViewParameters &nextViewParameters,
QString title, const ViewParameters &lastView) const;
QString buildViewId() const;
QStringList::iterator findViewPreference(QStringList &list, const QString &viewId) const;
Qt::SortOrder computePreferredSortOrder(Qt::SortOrder initialValue) const;
int computePreferredSortRole(int initialValue) const;
private:
std::unique_ptr<ViewManagerPrivate> d;
......
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