playlistsharedsettings.cpp 5.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/**
 * 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();
}

65 66 67 68 69 70 71 72 73
bool Playlist::SharedSettings::isColumnVisible(int column) const
{
    if(column >= m_columnsVisible.size()) {
        return false;
    }

    return m_columnsVisible[column];
}

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
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: