Commit 8780c80c authored by Matthieu Gallien's avatar Matthieu Gallien 🎵

simplify the code of FileBrowserModel and its proxy

parent 0b47e234
......@@ -31,26 +31,17 @@ FileBrowserModel::FileBrowserModel(QObject *parent) : KDirModel(parent)
FileBrowserModel::~FileBrowserModel()
= default;
QString FileBrowserModel::url() const
void FileBrowserModel::setUrl(const QUrl &url)
{
return dirLister()->url().toString();
}
void FileBrowserModel::setUrl(const QString &url)
{
QString path = QUrl(url).path();
path = QUrl::fromLocalFile(path).toString();
if (dirLister()->url().path() == QUrl(path).path()) {
dirLister()->updateDirectory(QUrl(path));
if (dirLister()->url() == url) {
dirLister()->updateDirectory(url);
return;
}
beginResetModel();
dirLister()->openUrl(QUrl(path));
dirLister()->openUrl(url);
endResetModel();
emit urlChanged();
}
bool FileBrowserModel::isBusy() const
......@@ -93,8 +84,6 @@ QVariant FileBrowserModel::data(const QModelIndex &index, int role) const
{
auto result = QVariant();
qCDebug(orgKdeElisaModel()) << "FileBrowserModel::data" << index << role;
switch(role)
{
case DataTypes::ColumnsRoles::ResourceRole:
......@@ -148,8 +137,6 @@ QVariant FileBrowserModel::data(const QModelIndex &index, int role) const
result = KDirModel::data(index,role);
}
qCDebug(orgKdeElisaModel()) << "FileBrowserModel::data" << index << role << result;
return result;
}
......@@ -166,7 +153,7 @@ void FileBrowserModel::initialize(MusicListenersManager *manager, DatabaseInterf
Q_UNUSED(artist)
Q_UNUSED(databaseId)
setUrl(pathFilter.toLocalFile());
setUrl(pathFilter);
}
......
......@@ -28,20 +28,16 @@ public:
~FileBrowserModel() override;
QString url() const;
QHash<int, QByteArray> roleNames() const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
void setUrl(const QString &url);
void setUrl(const QUrl &url);
bool isBusy() const;
Q_SIGNALS:
void urlChanged();
void isBusyChanged();
public Q_SLOTS:
......
......@@ -107,23 +107,6 @@ void FileBrowserProxyModel::replaceAndPlayOfPlayList(QModelIndex rootIndex)
ElisaUtils::TriggerPlay);
}
QString FileBrowserProxyModel::parentFolder() const
{
auto fileBrowserModel = dynamic_cast<FileBrowserModel*>(sourceModel());
if (!fileBrowserModel) {
return {};
}
//return to the top folder if parent directory does not exist
QDir dir(fileBrowserModel->dirLister()->url().toLocalFile());
if (dir.cdUp()) {
return dir.path();
} else {
return mTopFolder;
}
}
void FileBrowserProxyModel::disconnectPlayList()
{
if (mPlayList) {
......@@ -140,69 +123,6 @@ void FileBrowserProxyModel::connectPlayList()
}
}
void FileBrowserProxyModel::openParentFolder()
{
auto fileBrowserModel = dynamic_cast<FileBrowserModel*>(sourceModel());
if (!fileBrowserModel) {
return;
}
if (canGoBack()) {
QString parent = parentFolder();
fileBrowserModel->setUrl(parent);
if (parent == mTopFolder) {
Q_EMIT canGoBackChanged();
}
}
}
bool FileBrowserProxyModel::canGoBack() const
{
auto fileBrowserModel = dynamic_cast<FileBrowserModel*>(sourceModel());
if (!fileBrowserModel) {
return false;
}
return fileBrowserModel->dirLister()->url().toLocalFile() != mTopFolder;
}
void FileBrowserProxyModel::openFolder(const QString &folder, bool isDisplayRoot)
{
auto fileBrowserModel = dynamic_cast<FileBrowserModel*>(sourceModel());
if (!fileBrowserModel) {
return;
}
if (folder.isEmpty()) {
return;
}
fileBrowserModel->setUrl(folder);
if (!isDisplayRoot) {
Q_EMIT canGoBackChanged();
}
}
QString FileBrowserProxyModel::url() const
{
auto fileBrowserModel = dynamic_cast<FileBrowserModel*>(sourceModel());
if (!fileBrowserModel) {
return {};
}
return fileBrowserModel->dirLister()->url().toLocalFile();
}
bool FileBrowserProxyModel::sortedAscending() const
{
return sortOrder() ? false : true;
}
void FileBrowserProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
{
KDirSortFilterProxyModel::setSourceModel(sourceModel);
......@@ -212,10 +132,6 @@ void FileBrowserProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
if (!fileBrowserModel) {
return;
}
connect(fileBrowserModel, &FileBrowserModel::urlChanged,this, &FileBrowserProxyModel::urlChanged);
mTopFolder = QDir::homePath();
openFolder(mTopFolder, true);
}
MediaPlayListProxyModel *FileBrowserProxyModel::playList() const
......@@ -228,9 +144,14 @@ int FileBrowserProxyModel::filterRating() const
return mFilterRating;
}
bool FileBrowserProxyModel::sortedAscending() const
{
return sortOrder() ? false : true;
}
void FileBrowserProxyModel::sortModel(Qt::SortOrder order)
{
this->sort(0,order);
sort(0, order);
Q_EMIT sortedAscendingChanged();
}
......
......@@ -32,23 +32,15 @@ class ELISALIB_EXPORT FileBrowserProxyModel : public KDirSortFilterProxyModel
WRITE setFilterText
NOTIFY filterTextChanged)
Q_PROPERTY(bool canGoBack
READ canGoBack
NOTIFY canGoBackChanged)
Q_PROPERTY(QString url
READ url
NOTIFY urlChanged)
Q_PROPERTY(bool sortedAscending
READ sortedAscending
NOTIFY sortedAscendingChanged)
Q_PROPERTY(int filterRating
READ filterRating
WRITE setFilterRating
NOTIFY filterRatingChanged)
Q_PROPERTY(bool sortedAscending
READ sortedAscending
NOTIFY sortedAscendingChanged)
Q_PROPERTY(MediaPlayListProxyModel* playList READ playList WRITE setPlayList NOTIFY playListChanged)
public:
......@@ -59,17 +51,13 @@ public:
QString filterText() const;
QString url() const;
bool canGoBack() const;
int filterRating() const;
bool sortedAscending() const;
void setSourceModel(QAbstractItemModel *sourceModel) override;
MediaPlayListProxyModel* playList() const;
int filterRating() const;
void setSourceModel(QAbstractItemModel *sourceModel) override;
public Q_SLOTS:
......@@ -79,15 +67,11 @@ public Q_SLOTS:
void setFilterText(const QString &filterText);
void openParentFolder();
void openFolder(const QString &folder, bool isDisplayRoot = false);
void sortModel(Qt::SortOrder order);
void setFilterRating(int filterRating);
void setPlayList(MediaPlayListProxyModel* playList);
void setFilterRating(int filterRating);
void sortModel(Qt::SortOrder order);
Q_SIGNALS:
......@@ -95,18 +79,14 @@ Q_SIGNALS:
ElisaUtils::PlayListEnqueueMode enqueueMode,
ElisaUtils::PlayListEnqueueTriggerPlay triggerPlay);
void urlChanged();
void canGoBackChanged();
void filterTextChanged(const QString &filterText);
void filterRatingChanged();
void sortedAscendingChanged();
void playListChanged();
void filterRatingChanged();
protected:
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
......@@ -117,14 +97,10 @@ private:
ElisaUtils::PlayListEnqueueMode enqueueMode,
ElisaUtils::PlayListEnqueueTriggerPlay triggerPlay);
QString parentFolder() const;
void disconnectPlayList();
void connectPlayList();
QString mTopFolder;
QString mFilterText;
QRegularExpression mFilterExpression;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment