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
|
/***************************************************************************
* Copyright (C) 2013-2015 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 <QListView>
#include <QVBoxLayout>
#include <QSettings>
#include <QAction>
#include <QApplication>
#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"
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);
QVBoxLayout *layout = new QVBoxLayout();
layout->setContentsMargins(0,0,0,0);
layout->addWidget(m_label);
layout->addWidget(m_listView);
setLayout(layout);
m_model = new QFileSystemModel(this);
m_model->setReadOnly(true);
m_model->setNameFilterDisables(false);
m_model->setFilter(QDir::AllDirs | QDir::Files | QDir::NoDot);
m_listView->setModel(m_model);
setContextMenuPolicy(Qt::ActionsContextMenu);
QAction *addToPlaylistAction = new QAction(tr("Add to Playlist"), this);
connect(addToPlaylistAction, SIGNAL(triggered()), SLOT(addToPlayList()));
addAction(addToPlaylistAction);
QAction *selectDirAction = new QAction(tr("Change Directory"), this);
connect(selectDirAction, SIGNAL(triggered()), SLOT(selectDirectory()));
addAction(selectDirAction);
readSettings();
}
FileSystemBrowser::~FileSystemBrowser()
{
QSettings settings (Qmmp::configFile(), QSettings::IniFormat);
settings.beginGroup("Simple");
settings.setValue("fsbrowser_current_dir", m_model->rootPath());
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());
}
settings.endGroup();
m_model->setNameFilters(MetaDataManager::instance()->nameFilters());
}
void FileSystemBrowser::onListViewActivated(const QModelIndex &index)
{
if (!index.isValid())
return;
QString name = m_model->fileName(index);
if(name == "..")
{
setCurrentDirectory(m_model->fileInfo(index).absoluteFilePath());
}
else if(m_model->isDir(index))
{
QFileInfo info = m_model->fileInfo(index);
if(info.isExecutable() && info.isReadable())
setCurrentDirectory(m_model->filePath(index));
}
}
void FileSystemBrowser::addToPlayList()
{
foreach (QModelIndex index, m_listView->selectionModel()->selectedIndexes())
{
if(!index.isValid())
continue;
QString name = m_model->fileName(index);
if(name == "..")
continue;
PlayListManager::instance()->selectedPlayList()->add(m_model->filePath(index));
}
}
void FileSystemBrowser::selectDirectory()
{
QString dir = FileDialog::getExistingDirectory(qApp->activeWindow(),
tr("Select Directory"), m_model->rootPath());
if(!dir.isEmpty())
setCurrentDirectory(dir);
}
void FileSystemBrowser::setCurrentDirectory(const QString &path)
{
if(path.isEmpty())
return;
QModelIndex index = m_model->setRootPath(QDir(path).exists() ? path : QDir::homePath());
if(index.isValid())
{
m_listView->setRootIndex(index);
m_label->setText(QDir(m_model->rootPath()).dirName());
}
else
m_label->clear();
}
|