1 /**
2 * Copyright (C) 2002-2004 Scott Wheeler <wheeler@kde.org>
3 * (portions copied from playlist.cpp)
4 * Copyright (C) 2018 Michael Pyne <mpyne@kde.org>
5 *
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free Software
8 * Foundation; either version 2 of the License, or (at your option) any later
9 * version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "playlistsharedsettings.h"
20
21 #include <QHeaderView>
22 #include <QTreeWidget>
23 #include <QAction>
24
25 #include <KConfigGroup>
26 #include <KSharedConfig>
27 #include <KToggleAction>
28
29 #include "actioncollection.h"
30 #include "playlistitem.h"
31
32 ////////////////////////////////////////////////////////////////////////////////
33 // Playlist::SharedSettings public members
34 ////////////////////////////////////////////////////////////////////////////////
35
instance()36 Playlist::SharedSettings *Playlist::SharedSettings::instance()
37 {
38 static SharedSettings settings;
39 return &settings;
40 }
41
setColumnOrder(const Playlist * l)42 void Playlist::SharedSettings::setColumnOrder(const Playlist *l)
43 {
44 if(!l)
45 return;
46
47 m_columnOrder.clear();
48
49 for(int i = l->columnOffset(); i < l->columnCount(); ++i)
50 m_columnOrder.append(l->header()->logicalIndex(i));
51
52 writeConfig();
53 }
54
toggleColumnVisible(int column)55 void Playlist::SharedSettings::toggleColumnVisible(int column)
56 {
57 if(column >= m_columnsVisible.size())
58 m_columnsVisible.resize(column + 1);
59
60 m_columnsVisible[column] = !m_columnsVisible[column];
61
62 writeConfig();
63 }
64
isColumnVisible(int column) const65 bool Playlist::SharedSettings::isColumnVisible(int column) const
66 {
67 if(column >= m_columnsVisible.size()) {
68 return false;
69 }
70
71 return m_columnsVisible[column];
72 }
73
apply(Playlist * l) const74 void Playlist::SharedSettings::apply(Playlist *l) const
75 {
76 if(!l)
77 return;
78
79 const int offset = l->columnOffset();
80 auto header = l->header();
81 int i = 0;
82
83 for(int logicalIndex : m_columnOrder) {
84 header->moveSection(header->visualIndex(logicalIndex), i + offset);
85 i++;
86 }
87
88 for(int j = 0; j < m_columnsVisible.size(); j++) {
89 if(m_columnsVisible[j] && l->isColumnHidden(j + offset))
90 l->showColumn(j + offset, false);
91 else if(!m_columnsVisible[j] && !l->isColumnHidden(j + offset))
92 l->hideColumn(j + offset, false);
93 }
94
95 l->updateLeftColumn();
96 l->slotColumnResizeModeChanged();
97 }
98
99 ////////////////////////////////////////////////////////////////////////////////
100 // Playlist::ShareSettings protected members
101 ////////////////////////////////////////////////////////////////////////////////
102
SharedSettings()103 Playlist::SharedSettings::SharedSettings()
104 {
105 KConfigGroup config(KSharedConfig::openConfig(), "PlaylistShared");
106
107 bool resizeColumnsManually = config.readEntry("ResizeColumnsManually", false);
108 ActionCollection::action("resizeColumnsManually")->setChecked(resizeColumnsManually);
109
110 // Preallocate spaces so we don't need to check later.
111 m_columnsVisible.fill(true, PlaylistItem::lastColumn() + 1);
112
113 // load column order
114 m_columnOrder = config.readEntry("ColumnOrder", QList<int>()).toVector();
115
116 QVector<int> l = config.readEntry("VisibleColumns", QList<int>()).toVector();
117
118 if(l.isEmpty()) {
119
120 // Provide some default values for column visibility if none were
121 // read from the configuration file.
122
123 m_columnsVisible[PlaylistItem::BitrateColumn] = false;
124 m_columnsVisible[PlaylistItem::CommentColumn] = false;
125 m_columnsVisible[PlaylistItem::FileNameColumn] = false;
126 m_columnsVisible[PlaylistItem::FullPathColumn] = false;
127 }
128 else {
129 // Convert the int list into a bool list.
130
131 m_columnsVisible.fill(false);
132 for(int i : qAsConst(l)) {
133 if(Q_LIKELY(i < m_columnsVisible.size()))
134 m_columnsVisible[i] = true;
135 }
136 }
137 }
138
139 ////////////////////////////////////////////////////////////////////////////////
140 // Playlist::SharedSettings private members
141 ////////////////////////////////////////////////////////////////////////////////
142
writeConfig()143 void Playlist::SharedSettings::writeConfig()
144 {
145 KConfigGroup config(KSharedConfig::openConfig(), "PlaylistShared");
146 config.writeEntry("ColumnOrder", m_columnOrder.toList());
147
148 QList<int> l;
149 for(int i = 0; i < m_columnsVisible.size(); i++)
150 if(m_columnsVisible[i])
151 l << i;
152
153 config.writeEntry("VisibleColumns", l);
154 config.writeEntry("ResizeColumnsManually",
155 ActionCollection::action<KToggleAction>("resizeColumnsManually")->isChecked());
156
157 KSharedConfig::openConfig()->sync();
158 }
159
160 // vim: set et sw=4 tw=0 sta:
161