Files
ANSLIB/MediaClient/MediaClientForMobile/FileListModel.cpp

174 lines
4.1 KiB
C++
Raw Normal View History

2026-03-28 11:39:04 +11:00
/***************************************************************************************
*
* IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
*
* By downloading, copying, installing or using the software you agree to this license.
* If you do not agree to this license, do not download, install,
* copy or use the software.
*
* Copyright (C) 2014-2024, Happytimesoft Corporation, all rights reserved.
*
* Redistribution and use in binary forms, with or without modification, are permitted.
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*
****************************************************************************************/
#include "FileListModel.h"
#include "utils.h"
#include <QApplication>
#include <QBrush>
#include <QDir>
#include <QPalette>
FileListModel::FileListModel(int fileFormat, QObject *parent)
: QAbstractListModel(parent)
, m_bCheckable(false)
, m_fileFormat(fileFormat)
{
}
int FileListModel::rowCount(const QModelIndex & /* parent */) const
{
return m_fileCount;
}
QVariant FileListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= m_fileList.size() || index.row() < 0)
return QVariant();
if (role == Qt::DisplayRole)
{
return m_fileList.at(index.row());
}
else if (role == Qt::DecorationRole && m_fileFormat == FILE_FORMAT_PIC)
{
QString file = m_basePath + "/" + m_fileList.at(index.row());
QPixmap pixmap(file);
pixmap = pixmap.scaled(60, 40);
return pixmap;
}
else if (role == Qt::CheckStateRole && m_bCheckable)
{
return m_boolList.at(index.row()) ? Qt::Checked : Qt::Unchecked;
}
else if (role == Qt::SizeHintRole && m_fileFormat == FILE_FORMAT_VIDEO)
{
return QSize(100, 40);
}
return QVariant();
}
Qt::ItemFlags FileListModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
if (index.row() >= m_fileList.size() || index.row() < 0)
return Qt::NoItemFlags;
if (m_bCheckable)
{
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
}
else
{
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
}
bool FileListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role != Qt::CheckStateRole)
return false;
if (index.row() >= m_fileList.size() || index.row() < 0)
return false;
m_boolList.replace(index.row(), value.toBool());
emit dataChanged(index, index);
return true;
}
bool FileListModel::canFetchMore(const QModelIndex & /* index */) const
{
if (m_fileCount < m_fileList.size())
return true;
else
return false;
}
void FileListModel::fetchMore(const QModelIndex & /* index */)
{
int remainder = m_fileList.size() - m_fileCount;
int itemsToFetch = qMin(100, remainder);
beginInsertRows(QModelIndex(), m_fileCount, m_fileCount+itemsToFetch-1);
m_fileCount += itemsToFetch;
endInsertRows();
}
void FileListModel::setNameFilters(const QStringList &filters)
{
m_nameFilters = filters;
}
bool FileListModel::removeRows(int pos, int count, const QModelIndex &parent)
{
if (pos >= m_fileList.size() || pos < 0)
return false;
beginRemoveRows(parent, pos, pos);
m_fileList.removeAt(pos);
m_boolList.removeAt(pos);
endRemoveRows();
return true;
}
void FileListModel::setDirPath(const QString &path)
{
QDir dir(path);
m_basePath = path;
dir.setNameFilters(m_nameFilters);
beginResetModel();
m_fileList = dir.entryList();
m_fileCount = 0;
for (int i = 0; i < m_fileList.size(); i++)
{
m_boolList.push_back(false);
}
endResetModel();
}
void FileListModel::setCheckable(bool checkable)
{
m_bCheckable = checkable;
if (m_fileCount > 0)
{
emit dataChanged(index(0), index(m_fileCount-1));
}
}