aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Ui/qsui/filesystembrowser.cpp
blob: 2b2fc0a782a84d798236dedab03c15066bf8f67c (plain) (blame)
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
65
66
67
68
69
70
71
72
73
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/***************************************************************************
 *   Copyright (C) 2013-2017 by Ilya Kotov                                 *
 *   forkotov02@hotmail.ru                                                 *
 *                                                                         *
 *   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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include <QFileSystemModel>
#include <QSortFilterProxyModel>
#include <QListView>
#include <QVBoxLayout>
#include <QSettings>
#include <QAction>
#include <QApplication>
#include <QLineEdit>
#include <qmmp/metadatamanager.h>
#include <qmmpui/playlistmanager.h>
#include <qmmpui/filedialog.h>
#include <qmmpui/playlistmanager.h>
#include <qmmp/qmmp.h>
#include "elidinglabel.h"
#include "filesystembrowser.h"

class FileSystemFilterProxyModel : public QSortFilterProxyModel
{
public:
    FileSystemFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent) {}

protected:
    virtual bool filterAcceptsRow(
            int source_row, const QModelIndex &source_parent) const{
        QFileSystemModel *sm = qobject_cast<QFileSystemModel*>(sourceModel());
        if (source_parent == sm->index(sm->rootPath())) {
            return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
        }
        return true;
    }
};

FileSystemBrowser::FileSystemBrowser(QWidget *parent) :
    QWidget(parent)
{
    m_update = false;

    m_listView = new QListView(this);
    m_listView->setFrameStyle(QFrame::NoFrame);
    m_listView->setDragEnabled(true);
    m_listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
    connect(m_listView, SIGNAL(activated(QModelIndex)), SLOT(onListViewActivated(QModelIndex)));

    m_label = new Utils::ElidingLabel(this);
    m_label->setContentsMargins(5,5,5,0);
    m_label->setMargin(0);

    m_filterLineEdit = new QLineEdit(this);
    m_filterLineEdit->setContentsMargins(5,5,5,0);
    m_filterLineEdit->setVisible(false);

    QVBoxLayout *layout = new QVBoxLayout();
    layout->setContentsMargins(0,0,0,0);
    layout->addWidget(m_label);
    layout->addWidget(m_filterLineEdit);
    layout->addWidget(m_listView);
    setLayout(layout);

    m_fileSystemModel = new QFileSystemModel(this);
    m_fileSystemModel->setReadOnly(true);
    m_fileSystemModel->setNameFilterDisables(false);
#if QT_VERSION >= 0x040700
    m_fileSystemModel->setFilter(QDir::AllDirs | QDir::Files | QDir::NoDot);
#else
    m_fileSystemModel->setFilter(QDir::AllDirs | QDir::Files);
#endif

    m_proxyModel = new  FileSystemFilterProxyModel(this);
    m_proxyModel->setDynamicSortFilter(true);
    m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
    m_proxyModel->setSourceModel(m_fileSystemModel);
    m_listView->setModel(m_proxyModel);

    setContextMenuPolicy(Qt::ActionsContextMenu);
    QAction *addToPlaylistAction = new QAction(tr("Add to Playlist"), this);
    addAction(addToPlaylistAction);
    QAction *selectDirAction = new QAction(tr("Change Directory"), this);
    addAction(selectDirAction);
    addAction(m_showFilterAction = new QAction(tr("Quick Search"), this));
    m_showFilterAction->setCheckable(true);

    connect(selectDirAction, SIGNAL(triggered()), SLOT(selectDirectory()));
    connect(addToPlaylistAction, SIGNAL(triggered()), SLOT(addToPlayList()));
    connect(m_showFilterAction, SIGNAL(toggled(bool)), m_filterLineEdit, SLOT(setVisible(bool)));
    connect(m_showFilterAction, SIGNAL(triggered()), m_filterLineEdit, SLOT(clear()));
    connect(m_filterLineEdit, SIGNAL(textChanged(QString)), SLOT(onFilterLineEditTextChanged(QString)));

    readSettings();
}

FileSystemBrowser::~FileSystemBrowser()
{
    QSettings settings (Qmmp::configFile(), QSettings::IniFormat);
    settings.beginGroup("Simple");
    settings.setValue("fsbrowser_current_dir", m_fileSystemModel->rootDirectory().canonicalPath());
    settings.setValue("fsbrowser_quick_search", m_showFilterAction->isChecked());
    settings.endGroup();
}

void FileSystemBrowser::readSettings()
{
    QSettings settings (Qmmp::configFile(), QSettings::IniFormat);
    settings.beginGroup("Simple");
    if(!m_update)
    {
        m_update = true;
        setCurrentDirectory(settings.value("fsbrowser_current_dir", QDir::homePath()).toString());
        m_showFilterAction->setChecked(settings.value("fsbrowser_quick_search", false).toBool());
    }
    settings.endGroup();
    m_fileSystemModel->setNameFilters(MetaDataManager::instance()->nameFilters());
}

void FileSystemBrowser::onListViewActivated(const QModelIndex &index)
{
    if (!index.isValid())
        return;

    QModelIndex sourceIndex = m_proxyModel->mapToSource(index);

    QString name = m_fileSystemModel->fileName(sourceIndex);

    if(name == "..")
    {
        setCurrentDirectory(m_fileSystemModel->fileInfo(sourceIndex).absoluteFilePath());
    }
    else if(m_fileSystemModel->isDir(sourceIndex))
    {
        QFileInfo info = m_fileSystemModel->fileInfo(sourceIndex);
        if(info.isExecutable() && info.isReadable())
            setCurrentDirectory(m_fileSystemModel->filePath(sourceIndex));
    }
}

void FileSystemBrowser::addToPlayList()
{
    foreach (QModelIndex index, m_listView->selectionModel()->selectedIndexes())
    {
        if(!index.isValid())
            continue;

        QModelIndex sourceIndex = m_proxyModel->mapToSource(index);
        QString name = m_fileSystemModel->fileName(sourceIndex);
        if(name == "..")
            continue;
        PlayListManager::instance()->selectedPlayList()->add(m_fileSystemModel->filePath(sourceIndex));
    }
}

void FileSystemBrowser::selectDirectory()
{
    QString dir = FileDialog::getExistingDirectory(qApp->activeWindow(),
                                                   tr("Select Directory"), m_fileSystemModel->rootDirectory().canonicalPath());
    if(!dir.isEmpty())
        setCurrentDirectory(dir);
}

void FileSystemBrowser::onFilterLineEditTextChanged(const QString &str)
{
    m_proxyModel->setFilterFixedString(str);
}

void FileSystemBrowser::setCurrentDirectory(const QString &path)
{
    if(path.isEmpty())
        return;

    m_filterLineEdit->clear();

    QModelIndex index = m_fileSystemModel->setRootPath(QDir(path).exists() ? path : QDir::homePath());
    if(index.isValid())
    {
        m_listView->setRootIndex(m_proxyModel->mapFromSource(index));
        m_label->setText(QDir(m_fileSystemModel->rootPath()).dirName());
    }
    else
        m_label->clear();
}