Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit f3171c17 authored by Michael Pyne's avatar Michael Pyne

Extract Playlist::SharedSettings into a separate file.

This helps declutter playlist.cpp and even makes it seem a bit easier to
read through, I think.

I also tried to fix a couple of outstanding bugs along the way, for
instance reordering columns should work now, although there's still a
lot of improvements that need to be made here.
parent 0fb1217a
......@@ -107,6 +107,7 @@ set(juk_SRCS ${tunepimp_SRCS}
playlistinterface.cpp
playlistitem.cpp
playlistsearch.cpp
playlistsharedsettings.cpp
playlistsplitter.cpp
scrobbler.cpp
scrobbleconfigdlg.cpp
......
......@@ -61,6 +61,7 @@
#include "playlistitem.h"
#include "playlistcollection.h"
#include "playlistsearch.h"
#include "playlistsharedsettings.h"
#include "mediafiles.h"
#include "collectionlist.h"
#include "filerenamer.h"
......@@ -94,184 +95,19 @@ static bool manualResize()
}
////////////////////////////////////////////////////////////////////////////////
// Playlist::SharedSettings definition
// static members
////////////////////////////////////////////////////////////////////////////////
bool Playlist::m_visibleChanged = false;
bool Playlist::m_shuttingDown = false;
/**
* Shared settings between the playlists.
*/
class Playlist::SharedSettings
{
public:
static SharedSettings *instance();
/**
* Sets the default column order to that of Playlist @param p.
*/
void setColumnOrder(const Playlist *l);
void toggleColumnVisible(int column);
void setInlineCompletionMode(KCompletion::CompletionMode mode);
/**
* Apply the settings.
*/
void apply(Playlist *l) const;
void sync() { writeConfig(); }
protected:
SharedSettings();
~SharedSettings() {}
private:
void writeConfig();
static SharedSettings *m_instance;
QList<int> m_columnOrder;
QVector<bool> m_columnsVisible;
KCompletion::CompletionMode m_inlineCompletion;
};
Playlist::SharedSettings *Playlist::SharedSettings::m_instance = 0;
////////////////////////////////////////////////////////////////////////////////
// Playlist::SharedSettings public members
////////////////////////////////////////////////////////////////////////////////
Playlist::SharedSettings *Playlist::SharedSettings::instance()
{
static SharedSettings settings;
return &settings;
}
void Playlist::SharedSettings::setColumnOrder(const Playlist *l)
{
if(!l)
return;
m_columnOrder.clear();
for(int i = l->columnOffset(); i < l->columnCount(); ++i)
m_columnOrder.append(l->header()->visualIndex(i)); // FIXME MISMATCH with apply
writeConfig();
}
void Playlist::SharedSettings::toggleColumnVisible(int column)
{
if(column >= m_columnsVisible.size())
m_columnsVisible.fill(true, column + 1);
m_columnsVisible[column] = !m_columnsVisible[column];
writeConfig();
}
void Playlist::SharedSettings::setInlineCompletionMode(KCompletion::CompletionMode mode)
{
m_inlineCompletion = mode;
writeConfig();
}
void Playlist::SharedSettings::apply(Playlist *l) const
{
if(!l)
return;
int offset = l->columnOffset();
int i = 0;
//bool oldState = l->header()->blockSignals(true);
foreach(int column, m_columnOrder)
// FIXME this is broken
l->header()->moveSection(i++ + offset, column + offset); // FIXME mismatch with setColumnOrder
//l->header()->blockSignals(oldState);
for(int i = 0; i < m_columnsVisible.size(); i++) {
if(m_columnsVisible[i] && l->isColumnHidden(i + offset))
l->showColumn(i + offset, false);
else if(!m_columnsVisible[i] && !l->isColumnHidden(i + offset))
l->hideColumn(i + offset, false);
}
l->updateLeftColumn();
// FIXME rename
//l->renameLineEdit()->setCompletionMode(m_inlineCompletion);
l->slotColumnResizeModeChanged();
}
////////////////////////////////////////////////////////////////////////////////
// Playlist::ShareSettings protected members
////////////////////////////////////////////////////////////////////////////////
Playlist::SharedSettings::SharedSettings()
{
KConfigGroup config(KSharedConfig::openConfig(), "PlaylistShared");
bool resizeColumnsManually = config.readEntry("ResizeColumnsManually", false);
action("resizeColumnsManually")->setChecked(resizeColumnsManually);
// Preallocate spaces so we don't need to check later.
m_columnsVisible.fill(true, PlaylistItem::lastColumn() + 1);
// save column order
m_columnOrder = config.readEntry("ColumnOrder", QList<int>());
QList<int> l = config.readEntry("VisibleColumns", QList<int>());
if(l.isEmpty()) {
// Provide some default values for column visibility if none were
// read from the configuration file.
m_columnsVisible[PlaylistItem::BitrateColumn] = false;
m_columnsVisible[PlaylistItem::CommentColumn] = false;
m_columnsVisible[PlaylistItem::FileNameColumn] = false;
m_columnsVisible[PlaylistItem::FullPathColumn] = false;
}
else {
// Convert the int list into a bool list.
m_columnsVisible.fill(false);
for(int i = 0; i < l.size() && i < m_columnsVisible.size(); ++i)
m_columnsVisible[i] = bool(l[i]);
}
m_inlineCompletion = KCompletion::CompletionMode(
config.readEntry("InlineCompletionMode", int(KCompletion::CompletionAuto)));
}
////////////////////////////////////////////////////////////////////////////////
// Playlist::SharedSettings private members
////////////////////////////////////////////////////////////////////////////////
void Playlist::SharedSettings::writeConfig()
{
KConfigGroup config(KSharedConfig::openConfig(), "PlaylistShared");
config.writeEntry("ColumnOrder", m_columnOrder);
QList<int> l;
for(int i = 0; i < m_columnsVisible.size(); i++)
l.append(int(m_columnsVisible[i]));
config.writeEntry("VisibleColumns", l);
config.writeEntry("InlineCompletionMode", int(m_inlineCompletion));
config.writeEntry("ResizeColumnsManually", manualResize());
KSharedConfig::openConfig()->sync();
}
bool Playlist::m_visibleChanged = false;
bool Playlist::m_shuttingDown = false;
PlaylistItemList Playlist::m_history;
QVector<PlaylistItem *> Playlist::m_backMenuItems;
int Playlist::m_leftColumn = 0;
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////
PlaylistItemList Playlist::m_history;
QVector<PlaylistItem *> Playlist::m_backMenuItems;
int Playlist::m_leftColumn = 0;
Playlist::Playlist(PlaylistCollection *collection, const QString &name,
const QString &iconName) :
QTreeWidget(collection->playlistStack()),
......
......@@ -213,7 +213,7 @@ public:
/**
* Shows column \a c. If \a updateSearch is true then a signal that the
* visible columns have changed will be emitted and things like the search
* will be udated.
* will be updated.
*/
void showColumn(int c, bool updateSearch = true);
......
/**
* Copyright (C) 2002-2004 Scott Wheeler <wheeler@kde.org>
* (portions copied from playlist.cpp)
* Copyright (C) 2018 Michael Pyne <mpyne@kde.org>
*
* 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.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "playlistsharedsettings.h"
#include <QHeaderView>
#include <QTreeWidget>
#include <QAction>
#include <KConfigGroup>
#include <KSharedConfig>
#include <KToggleAction>
#include "actioncollection.h"
#include "playlistitem.h"
////////////////////////////////////////////////////////////////////////////////
// Playlist::SharedSettings public members
////////////////////////////////////////////////////////////////////////////////
Playlist::SharedSettings *Playlist::SharedSettings::instance()
{
static SharedSettings settings;
return &settings;
}
void Playlist::SharedSettings::setColumnOrder(const Playlist *l)
{
if(!l)
return;
m_columnOrder.clear();
for(int i = l->columnOffset(); i < l->columnCount(); ++i)
m_columnOrder.append(l->header()->logicalIndex(i));
writeConfig();
}
void Playlist::SharedSettings::toggleColumnVisible(int column)
{
if(column >= m_columnsVisible.size())
m_columnsVisible.resize(column + 1);
m_columnsVisible[column] = !m_columnsVisible[column];
writeConfig();
}
void Playlist::SharedSettings::setInlineCompletionMode(KCompletion::CompletionMode mode)
{
m_inlineCompletion = mode;
writeConfig();
}
void Playlist::SharedSettings::apply(Playlist *l) const
{
if(!l)
return;
const int offset = l->columnOffset();
auto header = l->header();
int i = 0;
for(int logicalIndex : m_columnOrder) {
header->moveSection(header->visualIndex(logicalIndex), i + offset);
i++;
}
for(int j = 0; j < m_columnsVisible.size(); j++) {
if(m_columnsVisible[j] && l->isColumnHidden(j + offset))
l->showColumn(j + offset, false);
else if(!m_columnsVisible[j] && !l->isColumnHidden(j + offset))
l->hideColumn(j + offset, false);
}
l->updateLeftColumn();
// FIXME rename -- needs a delegate which requires porting to QTreeView
//l->renameLineEdit()->setCompletionMode(m_inlineCompletion);
l->slotColumnResizeModeChanged();
}
////////////////////////////////////////////////////////////////////////////////
// Playlist::ShareSettings protected members
////////////////////////////////////////////////////////////////////////////////
Playlist::SharedSettings::SharedSettings()
{
KConfigGroup config(KSharedConfig::openConfig(), "PlaylistShared");
bool resizeColumnsManually = config.readEntry("ResizeColumnsManually", false);
ActionCollection::action("resizeColumnsManually")->setChecked(resizeColumnsManually);
// Preallocate spaces so we don't need to check later.
m_columnsVisible.fill(true, PlaylistItem::lastColumn() + 1);
// load column order
m_columnOrder = config.readEntry("ColumnOrder", QList<int>()).toVector();
QVector<int> l = config.readEntry("VisibleColumns", QList<int>()).toVector();
if(l.isEmpty()) {
// Provide some default values for column visibility if none were
// read from the configuration file.
m_columnsVisible[PlaylistItem::BitrateColumn] = false;
m_columnsVisible[PlaylistItem::CommentColumn] = false;
m_columnsVisible[PlaylistItem::FileNameColumn] = false;
m_columnsVisible[PlaylistItem::FullPathColumn] = false;
}
else {
// Convert the int list into a bool list.
m_columnsVisible.fill(false);
for(int i : l) {
if(Q_LIKELY(i < m_columnsVisible.size()))
m_columnsVisible[i] = true;
}
}
m_inlineCompletion = KCompletion::CompletionMode(
config.readEntry("InlineCompletionMode", int(KCompletion::CompletionAuto)));
}
////////////////////////////////////////////////////////////////////////////////
// Playlist::SharedSettings private members
////////////////////////////////////////////////////////////////////////////////
void Playlist::SharedSettings::writeConfig()
{
KConfigGroup config(KSharedConfig::openConfig(), "PlaylistShared");
config.writeEntry("ColumnOrder", m_columnOrder.toList());
QList<int> l;
for(int i = 0; i < m_columnsVisible.size(); i++)
if(m_columnsVisible[i])
l << i;
config.writeEntry("VisibleColumns", l);
config.writeEntry("InlineCompletionMode", int(m_inlineCompletion));
config.writeEntry("ResizeColumnsManually",
ActionCollection::action<KToggleAction>("resizeColumnsManually")->isChecked());
KSharedConfig::openConfig()->sync();
}
// vim: set et sw=4 tw=0 sta:
/**
* Copyright (C) 2002-2004 Scott Wheeler <wheeler@kde.org>
* Copyright (C) 2007 Michael Pyne <mpyne@kde.org>
*
* 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.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef JUK_PLAYLISTSHAREDSETTINGS_H
#define JUK_PLAYLISTSHAREDSETTINGS_H
#include <KCompletion>
#include <QVector>
#include "playlist.h"
/**
* Shared settings between the playlists.
*/
class Playlist::SharedSettings
{
public:
static SharedSettings *instance();
/**
* Sets the default column order to that of Playlist @param p.
*/
void setColumnOrder(const Playlist *l);
void toggleColumnVisible(int column);
void setInlineCompletionMode(KCompletion::CompletionMode mode);
/**
* Apply the settings.
*/
void apply(Playlist *l) const;
void sync() { writeConfig(); }
protected:
SharedSettings();
~SharedSettings() {}
private:
void writeConfig();
QVector<int> m_columnOrder;
QVector<bool> m_columnsVisible;
KCompletion::CompletionMode m_inlineCompletion;
};
#endif
// vim: set et sw=4 tw=0 sta:
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