File indexing completed on 2021-12-21 13:27:59

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 #include <QTreeWidget>
0023 #include <QAction>
0024 
0025 #include <KConfigGroup>
0026 #include <KSharedConfig>
0027 #include <KToggleAction>
0028 
0029 #include "actioncollection.h"
0030 #include "playlistitem.h"
0031 
0032 ////////////////////////////////////////////////////////////////////////////////
0033 // Playlist::SharedSettings public members
0034 ////////////////////////////////////////////////////////////////////////////////
0035 
0036 Playlist::SharedSettings *Playlist::SharedSettings::instance()
0037 {
0038     static SharedSettings settings;
0039     return &settings;
0040 }
0041 
0042 void Playlist::SharedSettings::setColumnOrder(const Playlist *l)
0043 {
0044     if(!l)
0045         return;
0046 
0047     m_columnOrder.clear();
0048 
0049     for(int i = l->columnOffset(); i < l->columnCount(); ++i)
0050         m_columnOrder.append(l->header()->logicalIndex(i));
0051 
0052     writeConfig();
0053 }
0054 
0055 void Playlist::SharedSettings::toggleColumnVisible(int column)
0056 {
0057     if(column >= m_columnsVisible.size())
0058         m_columnsVisible.resize(column + 1);
0059 
0060     m_columnsVisible[column] = !m_columnsVisible[column];
0061 
0062     writeConfig();
0063 }
0064 
0065 bool Playlist::SharedSettings::isColumnVisible(int column) const
0066 {
0067     if(column >= m_columnsVisible.size()) {
0068         return false;
0069     }
0070 
0071     return m_columnsVisible[column];
0072 }
0073 
0074 void Playlist::SharedSettings::apply(Playlist *l) const
0075 {
0076     if(!l)
0077         return;
0078 
0079     const int offset = l->columnOffset();
0080     auto header = l->header();
0081     int i = 0;
0082 
0083     for(int logicalIndex : m_columnOrder) {
0084         header->moveSection(header->visualIndex(logicalIndex), i + offset);
0085         i++;
0086     }
0087 
0088     for(int j = 0; j < m_columnsVisible.size(); j++) {
0089         if(m_columnsVisible[j] && l->isColumnHidden(j + offset))
0090             l->showColumn(j + offset, false);
0091         else if(!m_columnsVisible[j] && !l->isColumnHidden(j + offset))
0092             l->hideColumn(j + offset, false);
0093     }
0094 
0095     l->updateLeftColumn();
0096     l->slotColumnResizeModeChanged();
0097 }
0098 
0099 ////////////////////////////////////////////////////////////////////////////////
0100 // Playlist::ShareSettings protected members
0101 ////////////////////////////////////////////////////////////////////////////////
0102 
0103 Playlist::SharedSettings::SharedSettings()
0104 {
0105     KConfigGroup config(KSharedConfig::openConfig(), "PlaylistShared");
0106 
0107     bool resizeColumnsManually = config.readEntry("ResizeColumnsManually", false);
0108     ActionCollection::action("resizeColumnsManually")->setChecked(resizeColumnsManually);
0109 
0110     // Preallocate spaces so we don't need to check later.
0111     m_columnsVisible.fill(true, PlaylistItem::lastColumn() + 1);
0112 
0113     // load column order
0114     m_columnOrder = config.readEntry("ColumnOrder", QList<int>()).toVector();
0115 
0116     QVector<int> l = config.readEntry("VisibleColumns", QList<int>()).toVector();
0117 
0118     if(l.isEmpty()) {
0119 
0120         // Provide some default values for column visibility if none were
0121         // read from the configuration file.
0122 
0123         m_columnsVisible[PlaylistItem::BitrateColumn] = false;
0124         m_columnsVisible[PlaylistItem::CommentColumn] = false;
0125         m_columnsVisible[PlaylistItem::FileNameColumn] = false;
0126         m_columnsVisible[PlaylistItem::FullPathColumn] = false;
0127     }
0128     else {
0129         // Convert the int list into a bool list.
0130 
0131         m_columnsVisible.fill(false);
0132         for(int i : qAsConst(l)) {
0133             if(Q_LIKELY(i < m_columnsVisible.size()))
0134                 m_columnsVisible[i] = true;
0135         }
0136     }
0137 }
0138 
0139 ////////////////////////////////////////////////////////////////////////////////
0140 // Playlist::SharedSettings private members
0141 ////////////////////////////////////////////////////////////////////////////////
0142 
0143 void Playlist::SharedSettings::writeConfig()
0144 {
0145     KConfigGroup config(KSharedConfig::openConfig(), "PlaylistShared");
0146     config.writeEntry("ColumnOrder", m_columnOrder.toList());
0147 
0148     QList<int> l;
0149     for(int i = 0; i < m_columnsVisible.size(); i++)
0150         if(m_columnsVisible[i])
0151             l << i;
0152 
0153     config.writeEntry("VisibleColumns", l);
0154     config.writeEntry("ResizeColumnsManually",
0155             ActionCollection::action<KToggleAction>("resizeColumnsManually")->isChecked());
0156 
0157     KSharedConfig::openConfig()->sync();
0158 }
0159 
0160 // vim: set et sw=4 tw=0 sta: