File indexing completed on 2023-05-30 11:30:50

0001 /**
0002  * Copyright (C) 2002-2004 Scott Wheeler <wheeler@kde.org>
0003  *           (portions copied from playlist.cpp)
0004  * Copyright (C) 2018 Michael Pyne <mpyne@kde.org>
0005  *
0006  * This program is free software; you can redistribute it and/or modify it under
0007  * the terms of the GNU General Public License as published by the Free Software
0008  * Foundation; either version 2 of the License, or (at your option) any later
0009  * version.
0010  *
0011  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0013  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License along with
0016  * this program.  If not, see <http://www.gnu.org/licenses/>.
0017  */
0018 
0019 #include "playlistsharedsettings.h"
0020 
0021 #include <QHeaderView>
0022 
0023 #include <KConfigGroup>
0024 #include <KSharedConfig>
0025 #include <KToggleAction>
0026 
0027 #include "actioncollection.h"
0028 #include "playlistitem.h"
0029 
0030 ////////////////////////////////////////////////////////////////////////////////
0031 // Playlist::SharedSettings public members
0032 ////////////////////////////////////////////////////////////////////////////////
0033 
0034 Playlist::SharedSettings *Playlist::SharedSettings::instance()
0035 {
0036     static SharedSettings settings;
0037     return &settings;
0038 }
0039 
0040 void Playlist::SharedSettings::setColumnOrder(const Playlist *l)
0041 {
0042     if(!l)
0043         return;
0044 
0045     m_columnOrder.clear();
0046 
0047     for(int i = l->columnOffset(); i < l->columnCount(); ++i)
0048         m_columnOrder.append(l->header()->logicalIndex(i));
0049 
0050     writeConfig();
0051 }
0052 
0053 void Playlist::SharedSettings::toggleColumnVisible(int column)
0054 {
0055     if(column >= m_columnsVisible.size())
0056         m_columnsVisible.resize(column + 1);
0057 
0058     m_columnsVisible[column] = !m_columnsVisible[column];
0059 
0060     writeConfig();
0061 }
0062 
0063 bool Playlist::SharedSettings::isColumnVisible(int column) const
0064 {
0065     if(column >= m_columnsVisible.size()) {
0066         return false;
0067     }
0068 
0069     return m_columnsVisible[column];
0070 }
0071 
0072 void Playlist::SharedSettings::apply(Playlist *l) const
0073 {
0074     if(!l)
0075         return;
0076 
0077     const int offset = l->columnOffset();
0078     auto header = l->header();
0079     int i = 0;
0080 
0081     for(int logicalIndex : m_columnOrder) {
0082         header->moveSection(header->visualIndex(logicalIndex), i + offset);
0083         i++;
0084     }
0085 
0086     for(int j = 0; j < m_columnsVisible.size(); j++) {
0087         if(m_columnsVisible[j] && l->isColumnHidden(j + offset))
0088             l->showColumn(j + offset, false);
0089         else if(!m_columnsVisible[j] && !l->isColumnHidden(j + offset))
0090             l->hideColumn(j + offset, false);
0091     }
0092 
0093     l->updateLeftColumn();
0094     l->slotColumnResizeModeChanged();
0095 }
0096 
0097 ////////////////////////////////////////////////////////////////////////////////
0098 // Playlist::ShareSettings protected members
0099 ////////////////////////////////////////////////////////////////////////////////
0100 
0101 Playlist::SharedSettings::SharedSettings()
0102 {
0103     KConfigGroup config(KSharedConfig::openConfig(), "PlaylistShared");
0104 
0105     bool resizeColumnsManually = config.readEntry("ResizeColumnsManually", false);
0106     ActionCollection::action("resizeColumnsManually")->setChecked(resizeColumnsManually);
0107 
0108     // Preallocate spaces so we don't need to check later.
0109     m_columnsVisible.fill(true, PlaylistItem::lastColumn() + 1);
0110 
0111     // load column order
0112     m_columnOrder = config.readEntry("ColumnOrder", QList<int>()).toVector();
0113 
0114     QVector<int> l = config.readEntry("VisibleColumns", QList<int>()).toVector();
0115 
0116     if(l.isEmpty()) {
0117 
0118         // Provide some default values for column visibility if none were
0119         // read from the configuration file.
0120 
0121         m_columnsVisible[PlaylistItem::BitrateColumn] = false;
0122         m_columnsVisible[PlaylistItem::CommentColumn] = false;
0123         m_columnsVisible[PlaylistItem::FileNameColumn] = false;
0124         m_columnsVisible[PlaylistItem::FullPathColumn] = false;
0125     }
0126     else {
0127         // Convert the int list into a bool list.
0128 
0129         m_columnsVisible.fill(false);
0130         for(int i : qAsConst(l)) {
0131             if(Q_LIKELY(i < m_columnsVisible.size()))
0132                 m_columnsVisible[i] = true;
0133         }
0134     }
0135 }
0136 
0137 ////////////////////////////////////////////////////////////////////////////////
0138 // Playlist::SharedSettings private members
0139 ////////////////////////////////////////////////////////////////////////////////
0140 
0141 void Playlist::SharedSettings::writeConfig()
0142 {
0143     KConfigGroup config(KSharedConfig::openConfig(), "PlaylistShared");
0144     config.writeEntry("ColumnOrder", m_columnOrder.toList());
0145 
0146     QList<int> l;
0147     for(int i = 0; i < m_columnsVisible.size(); i++)
0148         if(m_columnsVisible[i])
0149             l << i;
0150 
0151     config.writeEntry("VisibleColumns", l);
0152     config.writeEntry("ResizeColumnsManually",
0153             ActionCollection::action<KToggleAction>("resizeColumnsManually")->isChecked());
0154 
0155     KSharedConfig::openConfig()->sync();
0156 }
0157 
0158 // vim: set et sw=4 tw=0 sta: