filemanager/model/pathbarmodel.cpp

140 lines
3.4 KiB
C++
Raw Normal View History

2021-03-29 01:51:34 -07:00
/*
* Copyright (C) 2021 CutefishOS Team.
*
* Author: revenmartin <revenmartin@gmail.com>
*
* 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 3 of the License, or
* 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 "pathbarmodel.h"
2021-04-18 03:47:27 -07:00
#include <QDir>
#include <QDebug>
2021-03-29 01:51:34 -07:00
PathBarModel::PathBarModel(QObject *parent)
: QAbstractItemModel(parent)
{
}
PathBarModel::~PathBarModel()
{
}
QString PathBarModel::url() const
{
return m_url;
}
2021-03-29 18:48:59 -07:00
2021-03-29 01:51:34 -07:00
void PathBarModel::setUrl(const QString &url)
{
if (m_url != url) {
beginResetModel();
2021-04-18 03:47:27 -07:00
2021-03-29 01:51:34 -07:00
m_url = url;
qDeleteAll(m_pathList);
m_pathList.clear();
2021-04-18 03:47:27 -07:00
if (m_url.startsWith("trash:/")) {
2021-03-29 01:51:34 -07:00
PathBarItem *item = new PathBarItem;
2021-04-18 03:47:27 -07:00
item->name = m_url;
item->url = m_url;
2021-03-29 01:51:34 -07:00
m_pathList.append(item);
2021-04-18 03:47:27 -07:00
} else {
QDir dir(m_url);
while (true) {
if (dir.isRoot()) {
PathBarItem *item = new PathBarItem;
item->name = "/";
item->url = "/";
m_pathList.append(item);
break;
}
PathBarItem *item = new PathBarItem;
item->name = dir.dirName();
item->url = QUrl::fromLocalFile(dir.absolutePath());
m_pathList.append(item);
dir.cdUp();
}
2021-03-29 01:51:34 -07:00
}
std::reverse(m_pathList.begin(), m_pathList.end());
endResetModel();
emit urlChanged();
}
}
QHash<int, QByteArray> PathBarModel::roleNames() const
{
QHash<int, QByteArray> roleNames;
roleNames[PathBarModel::NameRole] = "name";
roleNames[PathBarModel::UrlRole] = "url";
roleNames[PathBarModel::PathRole] = "path";
return roleNames;
}
int PathBarModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_pathList.size();
}
int PathBarModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 1;
}
QVariant PathBarModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
PathBarItem *item = m_pathList.at(index.row());
switch (role) {
case PathBarModel::NameRole:
return item->name;
case PathBarModel::UrlRole:
return item->url;
case PathBarModel::PathRole:
return item->url.toString(QUrl::PreferLocalFile);
}
return QVariant();
}
QModelIndex PathBarModel::index(int row, int column, const QModelIndex &parent) const
{
if (row < 0 || column != 0 || row >= m_pathList.size()) {
return QModelIndex();
}
if (parent.isValid()) {
return QModelIndex();
}
return createIndex(row, column, m_pathList.at(row));
}
QModelIndex PathBarModel::parent(const QModelIndex &child) const
{
Q_UNUSED(child);
return QModelIndex();
}