2022-11-21 19:29:50 +08:00
|
|
|
#include "fileview.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QApplication>
|
2022-11-29 15:06:34 +08:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QScrollBar>
|
2022-11-22 19:55:52 +08:00
|
|
|
#include "generatetools.h"
|
|
|
|
#include "fileviewdelegate.h"
|
2022-11-21 19:29:50 +08:00
|
|
|
|
|
|
|
const int TAG_INFO = 100;
|
|
|
|
const int ICON_MODE_ICON_WIDTH = 64;
|
|
|
|
const int ICON_MODE_ICON_HEIGHT = 64;
|
|
|
|
const int LIST_MODE_ICON_WIDTH = 32;
|
|
|
|
const int LIST_MODE_ICON_HEIGHT = 32;
|
2022-11-22 19:55:52 +08:00
|
|
|
const int ICON_MODE_ITEM_WIDTH = 96;
|
|
|
|
const int ICON_MODE_ITEM_HEIGHT = 104;
|
|
|
|
const int LIST_MODE_ITEM_HEIGHT = 56;
|
2022-11-29 15:06:34 +08:00
|
|
|
const int ICON_MODE_SPACING = 16;
|
|
|
|
const int LIST_MODE_SPACING = 4;
|
2022-11-22 19:55:52 +08:00
|
|
|
const QString CONFIG_PATH = getenv("HOME") + QString("/.connectivitycache/") + QString("setting.ini");
|
|
|
|
const QString CONFIG_KEY = "LastFilePath";
|
|
|
|
const QString DEFAULT_DOWNLOAD_PATH = getenv("HOME") + QString("/下载");
|
2022-11-21 19:29:50 +08:00
|
|
|
|
|
|
|
FileView::FileView(QWidget *parent) : QListView(parent)
|
|
|
|
{
|
2022-12-05 11:26:56 +08:00
|
|
|
setContentsMargins(0, 0, 0, 0);
|
2022-11-23 20:55:36 +08:00
|
|
|
setStyleSheet("QListView{background:transparent;}");
|
|
|
|
// 自适应布局
|
2022-11-21 19:29:50 +08:00
|
|
|
setResizeMode(QListView::Adjust);
|
2022-11-23 20:55:36 +08:00
|
|
|
// 列表中的图标不可拖动
|
2022-11-21 19:29:50 +08:00
|
|
|
setMovement(QListView::Static);
|
2022-11-23 20:55:36 +08:00
|
|
|
// 去边框
|
2022-11-21 19:29:50 +08:00
|
|
|
setFrameShape(QListView::NoFrame);
|
2022-11-23 20:55:36 +08:00
|
|
|
// 不可编辑
|
2022-11-21 19:29:50 +08:00
|
|
|
setEditTriggers(QAbstractItemView::NoEditTriggers);
|
2022-11-23 20:55:36 +08:00
|
|
|
// 设置选中模式为选中行
|
2022-11-21 19:29:50 +08:00
|
|
|
setSelectionBehavior(QAbstractItemView::SelectRows);
|
2022-11-24 14:29:21 +08:00
|
|
|
// 设置滑动条
|
2022-11-21 19:29:50 +08:00
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
|
2022-11-24 14:29:21 +08:00
|
|
|
// 设置项大小相同
|
2022-11-21 19:29:50 +08:00
|
|
|
setUniformItemSizes(true);
|
2022-11-24 14:29:21 +08:00
|
|
|
// 设置右键菜单
|
2022-11-21 19:29:50 +08:00
|
|
|
setContextMenuPolicy(Qt::CustomContextMenu);
|
2022-11-24 14:29:21 +08:00
|
|
|
//设置接收鼠标拖进事件
|
|
|
|
setAcceptDrops(true);
|
|
|
|
setDragDropMode(QListView::DragDrop);
|
2022-11-21 19:29:50 +08:00
|
|
|
// 注册文件信息类型
|
|
|
|
FileInfo::registerMetaType();
|
|
|
|
|
|
|
|
m_listModel = new QStandardItemModel(this);
|
|
|
|
setModel(m_listModel);
|
2022-11-22 19:55:52 +08:00
|
|
|
FileViewDelegate *delegate = new FileViewDelegate(this);
|
|
|
|
setItemDelegate(delegate);
|
2022-11-21 19:29:50 +08:00
|
|
|
|
|
|
|
connect(this, &FileView::doubleClicked, this, &FileView::slotDoubleClicked);
|
2022-11-22 19:55:52 +08:00
|
|
|
connect(this, &FileView::clicked, this, &FileView::slotClicked);
|
|
|
|
connect(this, &FileView::customContextMenuRequested, this, &FileView::slotContextMenu);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileView::setViewMode(QListView::ViewMode mode)
|
|
|
|
{
|
|
|
|
m_viewModel = mode;
|
2022-11-29 15:06:34 +08:00
|
|
|
setIconSize(getIconSize());
|
2022-11-22 19:55:52 +08:00
|
|
|
if (m_viewModel == QListView::IconMode) {
|
2022-11-29 15:06:34 +08:00
|
|
|
setSpacing(ICON_MODE_SPACING);
|
2022-11-22 19:55:52 +08:00
|
|
|
} else {
|
2022-11-29 15:06:34 +08:00
|
|
|
setSpacing(LIST_MODE_SPACING);
|
2022-11-22 19:55:52 +08:00
|
|
|
}
|
|
|
|
updateViewMode();
|
2022-12-01 11:21:10 +08:00
|
|
|
QListView::setViewMode(m_viewModel);
|
2022-12-07 14:06:43 +08:00
|
|
|
update();
|
2022-12-01 11:21:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QListView::ViewMode FileView::getViewMode() const
|
|
|
|
{
|
|
|
|
return m_viewModel;
|
2022-11-22 19:55:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileView::setModelFlag(PublicAttributes::Model model)
|
|
|
|
{
|
|
|
|
m_model = model;
|
2022-11-21 19:29:50 +08:00
|
|
|
}
|
|
|
|
|
2022-11-29 15:06:34 +08:00
|
|
|
QMap<QString, QStandardItem *> FileView::setViewInfo(const QMap<QString, FileInfo> &map)
|
2022-11-21 19:29:50 +08:00
|
|
|
{
|
2022-11-29 15:06:34 +08:00
|
|
|
setAdjustHeight(map.size());
|
2022-11-22 19:55:52 +08:00
|
|
|
QMap<QString, FileInfo> dirMap; // 临时存储目录项
|
2022-11-21 19:29:50 +08:00
|
|
|
QMap<QString, FileInfo> fileMap; // 临时存储非目录项
|
2022-11-29 15:06:34 +08:00
|
|
|
for (FileInfo info : map) {
|
|
|
|
if (info.type() == FileInfo::FileType::Dir) {
|
|
|
|
dirMap.insert(info.name(), info);
|
2022-11-21 19:29:50 +08:00
|
|
|
} else {
|
2022-11-22 19:55:52 +08:00
|
|
|
// 不是文件夹
|
2022-11-29 15:06:34 +08:00
|
|
|
fileMap.insert(info.name(), info);
|
2022-11-21 19:29:50 +08:00
|
|
|
}
|
|
|
|
}
|
2022-11-22 19:55:52 +08:00
|
|
|
setInfo(dirMap);
|
|
|
|
setInfo(fileMap);
|
2022-11-29 15:06:34 +08:00
|
|
|
|
|
|
|
return m_itemMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMap<QString, QStandardItem *> FileView::setViewInfo(const QList<FileInfo> &list)
|
|
|
|
{
|
|
|
|
setAdjustHeight(list.size());
|
|
|
|
QList<FileInfo> dirList; // 临时存储目录项
|
|
|
|
QList<FileInfo> fileList; // 临时存储非目录项
|
|
|
|
for (FileInfo info : list) {
|
|
|
|
if (info.type() == FileInfo::FileType::Dir) {
|
|
|
|
dirList.append(info);
|
|
|
|
} else {
|
|
|
|
// 不是文件夹
|
|
|
|
fileList.append(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setInfo(dirList);
|
|
|
|
setInfo(fileList);
|
|
|
|
|
|
|
|
return m_itemMap;
|
2022-11-22 19:55:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileView::setDownloadBtn(bool isShow)
|
|
|
|
{
|
2022-11-29 15:06:34 +08:00
|
|
|
if (!m_isShowDownloadBtn) {
|
|
|
|
Q_EMIT sigSelectionModels(isShow);
|
|
|
|
return;
|
|
|
|
}
|
2022-11-22 19:55:52 +08:00
|
|
|
if (isShow) {
|
|
|
|
if (m_downloadBtn != nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_downloadBtn = new LevitationButton(this);
|
|
|
|
connect(m_downloadBtn, &LevitationButton::clicked, this, &FileView::slotDownloadBtnClicked);
|
|
|
|
m_downloadBtn->setIcon(":/filemanageview/download.svg");
|
|
|
|
m_downloadBtn->setText(tr("Download"));
|
|
|
|
m_downloadBtn->move(
|
2022-12-14 11:00:59 +08:00
|
|
|
QPoint(this->width() / 2 - m_downloadBtn->width() / 2, this->height() - 30 - m_downloadBtn->height()));
|
|
|
|
m_downloadBtn->changeFontSize(m_fontSize);
|
2022-11-22 19:55:52 +08:00
|
|
|
m_downloadBtn->show();
|
|
|
|
} else {
|
|
|
|
if (m_downloadBtn != nullptr) {
|
|
|
|
m_downloadBtn->deleteLater();
|
|
|
|
m_downloadBtn = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 20:55:36 +08:00
|
|
|
void FileView::setTheme(PublicAttributes::Theme theme)
|
|
|
|
{
|
|
|
|
m_theme = theme;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
PublicAttributes::Theme FileView::getTheme() const
|
|
|
|
{
|
|
|
|
return m_theme;
|
|
|
|
}
|
|
|
|
|
2022-12-14 11:00:59 +08:00
|
|
|
void FileView::changeFontSize(double fontSize)
|
|
|
|
{
|
|
|
|
m_fontSize = fontSize;
|
|
|
|
if (m_downloadBtn != nullptr) {
|
|
|
|
m_downloadBtn->changeFontSize(m_fontSize);
|
|
|
|
}
|
|
|
|
QFont font;
|
|
|
|
font.setPointSizeF(fontSize);
|
|
|
|
setFont(font);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2022-11-29 19:03:55 +08:00
|
|
|
void FileView::setItemMode(FileView::ItemSelectMode mode, bool isClear)
|
2022-11-23 20:55:36 +08:00
|
|
|
{
|
|
|
|
m_itemMode = mode;
|
2022-11-29 19:03:55 +08:00
|
|
|
if (isClear) {
|
|
|
|
clearSelection();
|
2022-12-14 16:46:21 +08:00
|
|
|
setDownloadBtn(false);
|
2022-11-29 19:03:55 +08:00
|
|
|
}
|
2022-11-23 20:55:36 +08:00
|
|
|
if (m_itemMode == ItemSelectMode::Default) {
|
|
|
|
QListView::setSelectionMode(QAbstractItemView::ExtendedSelection);
|
|
|
|
} else {
|
|
|
|
QListView::setSelectionMode(QAbstractItemView::MultiSelection);
|
|
|
|
}
|
2022-12-07 14:06:43 +08:00
|
|
|
update();
|
2022-11-23 20:55:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FileView::ItemSelectMode FileView::getItemMode() const
|
|
|
|
{
|
|
|
|
return m_itemMode;
|
|
|
|
}
|
|
|
|
|
2022-11-25 11:33:22 +08:00
|
|
|
void FileView::setAllowDrag(bool isAllowDrag)
|
|
|
|
{
|
|
|
|
m_isAllowDrag = isAllowDrag;
|
|
|
|
}
|
|
|
|
|
2022-11-29 15:06:34 +08:00
|
|
|
void FileView::setNoDownloadBtn()
|
|
|
|
{
|
|
|
|
m_isShowDownloadBtn = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileView::setAdaptiveHeight()
|
|
|
|
{
|
|
|
|
m_isAdaptiveHeight = true;
|
|
|
|
verticalScrollBar()->setDisabled(true);
|
|
|
|
horizontalScrollBar()->setDisabled(true);
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
2022-11-29 19:03:55 +08:00
|
|
|
// 设置右键菜单
|
|
|
|
setContextMenuPolicy(Qt::NoContextMenu);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileView::getSelectInfoList(QList<FileInfo> &list)
|
|
|
|
{
|
|
|
|
for (QModelIndex index : selectionModel()->selectedIndexes()) {
|
|
|
|
FileInfo fileInfo = index.data(Qt::DisplayRole + TAG_INFO).value<FileInfo>();
|
|
|
|
list.append(fileInfo);
|
|
|
|
}
|
2022-11-29 15:06:34 +08:00
|
|
|
}
|
|
|
|
|
2022-11-22 19:55:52 +08:00
|
|
|
void FileView::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
2022-11-23 20:55:36 +08:00
|
|
|
if (!indexAt(event->pos()).isValid()) {
|
|
|
|
if (QApplication::keyboardModifiers() != Qt::ControlModifier
|
|
|
|
|| event->source() != Qt::MouseEventSynthesizedByQt) {
|
|
|
|
clearSelection();
|
|
|
|
setDownloadBtn(false);
|
|
|
|
}
|
2022-11-22 19:55:52 +08:00
|
|
|
}
|
|
|
|
QListView::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileView::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
QModelIndexList list = selectionModel()->selectedIndexes();
|
|
|
|
if (list.size() >= 2) {
|
2022-11-23 20:55:36 +08:00
|
|
|
if (list.size() == m_listModel->rowCount()) {
|
|
|
|
Q_EMIT sigItemSelectAll(true);
|
|
|
|
} else {
|
|
|
|
Q_EMIT sigItemSelectAll(false);
|
|
|
|
}
|
|
|
|
setItemMode(FileView::ItemSelectMode::Edit);
|
2022-11-22 19:55:52 +08:00
|
|
|
setDownloadBtn(true);
|
|
|
|
}
|
|
|
|
QListView::mouseReleaseEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileView::keyPressEvent(QKeyEvent *event)
|
|
|
|
{
|
|
|
|
if ((event->modifiers() == Qt::ControlModifier) && (event->key() == Qt::Key_A)) {
|
2022-11-23 20:55:36 +08:00
|
|
|
Q_EMIT sigItemSelectAll(true);
|
|
|
|
setItemMode(FileView::ItemSelectMode::Edit);
|
2022-11-22 19:55:52 +08:00
|
|
|
setDownloadBtn(true);
|
2022-11-29 19:03:55 +08:00
|
|
|
Q_EMIT sigCtrlAClicked();
|
2022-11-22 19:55:52 +08:00
|
|
|
}
|
|
|
|
QListView::keyPressEvent(event);
|
|
|
|
}
|
|
|
|
|
2022-11-24 14:29:21 +08:00
|
|
|
void FileView::dragEnterEvent(QDragEnterEvent *event)
|
|
|
|
{
|
2022-11-25 11:33:22 +08:00
|
|
|
if (m_isAllowDrag && m_itemMode == ItemSelectMode::Default && event->mimeData()->hasUrls()) {
|
2022-11-24 14:29:21 +08:00
|
|
|
event->accept();
|
|
|
|
} else {
|
|
|
|
event->ignore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileView::dragMoveEvent(QDragMoveEvent *event)
|
|
|
|
{
|
|
|
|
auto index = indexAt(event->pos());
|
|
|
|
if (index.isValid()) {
|
|
|
|
QHoverEvent he(QHoverEvent::HoverMove, event->posF(), event->posF());
|
|
|
|
viewportEvent(&he);
|
|
|
|
} else {
|
|
|
|
QHoverEvent he(QHoverEvent::HoverLeave, event->posF(), event->posF());
|
|
|
|
viewportEvent(&he);
|
|
|
|
}
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileView::dropEvent(QDropEvent *event)
|
|
|
|
{
|
|
|
|
auto urls = event->mimeData()->urls();
|
|
|
|
qInfo() << "drag urls" << urls;
|
|
|
|
if (urls.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList localpath;
|
|
|
|
for (auto &url : urls) {
|
|
|
|
localpath << url.toLocalFile();
|
|
|
|
}
|
|
|
|
QString dirName = "";
|
|
|
|
QModelIndex index = indexAt(event->pos());
|
|
|
|
if (index.isValid()) {
|
|
|
|
FileInfo fileInfo = index.data(Qt::DisplayRole + TAG_INFO).value<FileInfo>();
|
2022-11-24 15:26:14 +08:00
|
|
|
if (fileInfo.type() == FileInfo::FileType::Dir) {
|
2022-11-24 14:29:21 +08:00
|
|
|
dirName = fileInfo.name();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Q_EMIT sigUploadFileInfo(dirName, localpath);
|
|
|
|
}
|
|
|
|
|
2022-11-29 15:06:34 +08:00
|
|
|
void FileView::resizeEvent(QResizeEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
setAdjustHeight(m_listModel->rowCount());
|
|
|
|
Q_EMIT sigSizeChanged(size());
|
|
|
|
|
|
|
|
return QListView::resizeEvent(event);
|
|
|
|
}
|
|
|
|
|
2022-11-22 19:55:52 +08:00
|
|
|
void FileView::setInfo(const QMap<QString, FileInfo> &map)
|
|
|
|
{
|
2022-11-29 15:06:34 +08:00
|
|
|
for (FileInfo info : map) {
|
2022-11-21 19:29:50 +08:00
|
|
|
QStandardItem *item = new QStandardItem;
|
2022-11-29 15:06:34 +08:00
|
|
|
if (info.type() == FileInfo::FileType::Txt) {
|
2022-11-21 19:29:50 +08:00
|
|
|
item->setIcon(QIcon::fromTheme("text"));
|
2022-11-29 15:06:34 +08:00
|
|
|
} else if (info.type() == FileInfo::FileType::Video) {
|
|
|
|
item->setIcon(getVideoIcon(info.getThumbnailPath()));
|
|
|
|
} else if (info.type() == FileInfo::FileType::Image) {
|
|
|
|
item->setIcon(getPictureIcon(info.getThumbnailPath()));
|
|
|
|
} else if (info.type() == FileInfo::FileType::Audio) {
|
2022-11-21 19:29:50 +08:00
|
|
|
item->setIcon(QIcon::fromTheme("audio-x-generic"));
|
2022-11-29 15:06:34 +08:00
|
|
|
} else if (info.type() == FileInfo::FileType::Dir) {
|
2022-11-22 19:55:52 +08:00
|
|
|
item->setIcon(QIcon::fromTheme("folder"));
|
2022-11-21 19:29:50 +08:00
|
|
|
} else {
|
2022-11-29 15:06:34 +08:00
|
|
|
item->setIcon(QIcon::fromTheme(info.iconName(info.getContentType())));
|
2022-11-21 19:29:50 +08:00
|
|
|
}
|
|
|
|
// 设置文件名
|
2022-11-29 15:06:34 +08:00
|
|
|
item->setData(info.name(), Qt::DisplayRole);
|
2022-11-21 19:29:50 +08:00
|
|
|
// 标记文件信息
|
2022-11-29 15:06:34 +08:00
|
|
|
item->setData(info, Qt::DisplayRole + TAG_INFO);
|
|
|
|
item->setToolTip(info.name());
|
|
|
|
m_itemMap.insert(info.getThumbnailPath(), item);
|
|
|
|
m_listModel->appendRow(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileView::setInfo(const QList<FileInfo> &list)
|
|
|
|
{
|
|
|
|
for (FileInfo info : list) {
|
|
|
|
QStandardItem *item = new QStandardItem;
|
|
|
|
if (info.type() == FileInfo::FileType::Txt) {
|
|
|
|
item->setIcon(QIcon::fromTheme("text"));
|
|
|
|
} else if (info.type() == FileInfo::FileType::Video) {
|
|
|
|
item->setIcon(getVideoIcon(info.getThumbnailPath()));
|
|
|
|
} else if (info.type() == FileInfo::FileType::Image) {
|
|
|
|
item->setIcon(getPictureIcon(info.getThumbnailPath()));
|
|
|
|
} else if (info.type() == FileInfo::FileType::Audio) {
|
|
|
|
item->setIcon(QIcon::fromTheme("audio-x-generic"));
|
|
|
|
} else if (info.type() == FileInfo::FileType::Dir) {
|
|
|
|
item->setIcon(QIcon::fromTheme("folder"));
|
|
|
|
} else {
|
|
|
|
item->setIcon(QIcon::fromTheme(info.iconName(info.getContentType())));
|
|
|
|
}
|
|
|
|
// 设置文件名
|
|
|
|
item->setData(info.name(), Qt::DisplayRole);
|
|
|
|
// 标记文件信息
|
|
|
|
item->setData(info, Qt::DisplayRole + TAG_INFO);
|
|
|
|
item->setToolTip(info.name());
|
|
|
|
m_itemMap.insert(info.getThumbnailPath(), item);
|
2022-11-21 19:29:50 +08:00
|
|
|
m_listModel->appendRow(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 19:55:52 +08:00
|
|
|
void FileView::openFile(const QModelIndex &index)
|
2022-11-21 19:29:50 +08:00
|
|
|
{
|
2022-11-22 19:55:52 +08:00
|
|
|
FileInfo fileInfo = index.data(Qt::DisplayRole + TAG_INFO).value<FileInfo>();
|
|
|
|
Q_EMIT sigOpenFile(fileInfo.type(), fileInfo.getPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileView::sendDownloadInfo()
|
|
|
|
{
|
|
|
|
QSettings setting(CONFIG_PATH, QSettings::IniFormat);
|
|
|
|
QString lastPath = setting.value(CONFIG_KEY).toString();
|
|
|
|
if (lastPath.isEmpty()) {
|
|
|
|
lastPath = DEFAULT_DOWNLOAD_PATH;
|
|
|
|
}
|
|
|
|
QFileDialog fileDialog;
|
|
|
|
QString filePath = fileDialog.getExistingDirectory(this, tr("Choose folder"), lastPath); // Choose folder选择文件夹
|
|
|
|
if (!filePath.isEmpty()) {
|
|
|
|
setting.setValue(CONFIG_KEY, filePath);
|
|
|
|
} else {
|
|
|
|
filePath = DEFAULT_DOWNLOAD_PATH;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<FileInfo> fileList;
|
|
|
|
for (QModelIndex index : selectionModel()->selectedIndexes()) {
|
2022-11-21 19:29:50 +08:00
|
|
|
FileInfo fileInfo = index.data(Qt::DisplayRole + TAG_INFO).value<FileInfo>();
|
2022-11-22 19:55:52 +08:00
|
|
|
fileList.append(fileInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_EMIT sigDownFileInfo(filePath, fileList);
|
|
|
|
}
|
|
|
|
|
2022-11-29 15:06:34 +08:00
|
|
|
void FileView::setAdjustHeight(int count)
|
|
|
|
{
|
|
|
|
if (m_isAdaptiveHeight) {
|
|
|
|
if (m_viewModel == QListView::IconMode) {
|
|
|
|
int column = (size().width() - ICON_MODE_SPACING) / (ICON_MODE_SPACING + ICON_MODE_ITEM_WIDTH);
|
2022-11-30 14:30:33 +08:00
|
|
|
if (column == 0) {
|
|
|
|
column = 1;
|
|
|
|
}
|
2022-11-29 15:06:34 +08:00
|
|
|
int row = count / column;
|
2022-11-30 14:30:33 +08:00
|
|
|
if (row == 0 || (count % column) != 0) {
|
|
|
|
row = row + 1;
|
|
|
|
}
|
2022-12-05 11:26:56 +08:00
|
|
|
setFixedHeight((row * (ICON_MODE_ITEM_HEIGHT + ICON_MODE_SPACING)) + ICON_MODE_SPACING);
|
2022-11-29 15:06:34 +08:00
|
|
|
} else {
|
2022-12-05 11:26:56 +08:00
|
|
|
setFixedHeight(((LIST_MODE_ITEM_HEIGHT + LIST_MODE_SPACING + LIST_MODE_SPACING) * count)
|
|
|
|
+ LIST_MODE_SPACING);
|
2022-11-29 15:06:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 19:55:52 +08:00
|
|
|
void FileView::updateViewMode()
|
|
|
|
{
|
2022-11-23 20:55:36 +08:00
|
|
|
if (m_listModel->rowCount() == 0) {
|
2022-11-22 19:55:52 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-11-23 20:55:36 +08:00
|
|
|
for (int i = 0; i < m_listModel->rowCount(); i++) {
|
2022-11-22 19:55:52 +08:00
|
|
|
QStandardItem *item = m_listModel->item(i, 0);
|
|
|
|
QModelIndex index = m_listModel->index(i, 0, QModelIndex());
|
|
|
|
if (m_viewModel == QListView::IconMode) {
|
|
|
|
item->setSizeHint(QSize(ICON_MODE_ITEM_WIDTH, ICON_MODE_ITEM_HEIGHT));
|
|
|
|
} else {
|
|
|
|
item->setSizeHint(QSize(width(), LIST_MODE_ITEM_HEIGHT));
|
|
|
|
FileInfo fileInfo = index.data(Qt::DisplayRole + TAG_INFO).value<FileInfo>();
|
2022-11-24 15:26:14 +08:00
|
|
|
if (fileInfo.type() != FileInfo::FileType::Dir) {
|
2022-11-22 19:55:52 +08:00
|
|
|
QString sizeStr = GenerateTools::intSizeToString(fileInfo.size());
|
|
|
|
QVariant metaSize;
|
|
|
|
metaSize.setValue(sizeStr);
|
|
|
|
m_listModel->setData(index, metaSize, Qt::UserRole);
|
|
|
|
}
|
|
|
|
}
|
2022-11-21 19:29:50 +08:00
|
|
|
}
|
2022-11-29 19:03:55 +08:00
|
|
|
setAdjustHeight(m_listModel->rowCount());
|
|
|
|
Q_EMIT sigSizeChanged(size());
|
2022-11-21 19:29:50 +08:00
|
|
|
}
|
|
|
|
|
2022-11-29 15:06:34 +08:00
|
|
|
QSize FileView::getIconSize()
|
|
|
|
{
|
|
|
|
if (m_viewModel == QListView::IconMode) {
|
|
|
|
return QSize(ICON_MODE_ICON_WIDTH, ICON_MODE_ICON_HEIGHT);
|
|
|
|
} else {
|
|
|
|
return QSize(LIST_MODE_ICON_WIDTH, LIST_MODE_ICON_HEIGHT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QIcon FileView::getPictureIcon(QString path)
|
|
|
|
{
|
|
|
|
if (path.isEmpty()) {
|
|
|
|
return QIcon::fromTheme("image-x-generic");
|
|
|
|
} else {
|
2022-12-07 15:23:26 +08:00
|
|
|
QFileInfo info(path);
|
|
|
|
if (!info.exists()) {
|
2022-11-29 15:06:34 +08:00
|
|
|
return QIcon::fromTheme("image-x-generic");
|
|
|
|
}
|
2022-12-07 15:23:26 +08:00
|
|
|
QPixmap thumbnail = QPixmap(path);
|
|
|
|
thumbnail = thumbnail.scaled(getIconSize(), Qt::KeepAspectRatio);
|
|
|
|
return QIcon(thumbnail);
|
2022-11-29 15:06:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QIcon FileView::getVideoIcon(QString path)
|
|
|
|
{
|
|
|
|
if (path.isEmpty()) {
|
|
|
|
return QIcon::fromTheme("video");
|
|
|
|
} else {
|
2022-12-07 15:23:26 +08:00
|
|
|
QFileInfo info(path);
|
|
|
|
if (!info.exists()) {
|
|
|
|
return QIcon(":/filemanageview/videodefault.svg");
|
|
|
|
}
|
2022-12-05 11:26:56 +08:00
|
|
|
QPixmap thumbnail = QPixmap(path);
|
|
|
|
thumbnail = thumbnail.scaled(getIconSize(), Qt::KeepAspectRatio);
|
2022-12-07 15:23:26 +08:00
|
|
|
QPixmap playPixmap(":/filemanageview/play.svg");
|
|
|
|
if (m_viewModel == QListView::IconMode) {
|
|
|
|
playPixmap = playPixmap.scaled(QSize(36, 36), Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
2022-11-29 15:06:34 +08:00
|
|
|
} else {
|
2022-12-07 15:23:26 +08:00
|
|
|
playPixmap = playPixmap.scaled(QSize(24, 24), Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
2022-11-29 15:06:34 +08:00
|
|
|
}
|
2022-12-07 15:23:26 +08:00
|
|
|
QPainter imagepainter(&thumbnail); //新建画板
|
|
|
|
imagepainter.setCompositionMode(QPainter::CompositionMode_SourceOver); //设置重叠效果
|
|
|
|
imagepainter.drawPixmap((thumbnail.width() - playPixmap.width()) / 2,
|
|
|
|
(thumbnail.height() - playPixmap.height()) / 2, playPixmap);
|
|
|
|
imagepainter.end();
|
|
|
|
return QIcon(thumbnail);
|
2022-11-29 15:06:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 19:55:52 +08:00
|
|
|
void FileView::slotDoubleClicked(const QModelIndex &index)
|
|
|
|
{
|
|
|
|
openFile(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileView::slotClicked(const QModelIndex &index)
|
|
|
|
{
|
2022-12-01 16:32:36 +08:00
|
|
|
if (m_model == PublicAttributes::Model::Table && m_itemMode == ItemSelectMode::Default) {
|
2022-11-22 19:55:52 +08:00
|
|
|
openFile(index);
|
|
|
|
} else {
|
|
|
|
setDownloadBtn(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileView::slotDownloadBtnClicked()
|
|
|
|
{
|
|
|
|
sendDownloadInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileView::slotContextMenu(const QPoint &pos)
|
|
|
|
{
|
|
|
|
setDownloadBtn(false);
|
|
|
|
QModelIndexList list = selectionModel()->selectedIndexes();
|
|
|
|
if (!list.empty()) {
|
|
|
|
QMenu *menu = new QMenu(this);
|
|
|
|
QAction *openAction = new QAction(QIcon::fromTheme("folder-open-symbolic"), tr("Open"), this);
|
|
|
|
connect(openAction, &QAction::triggered, this, &FileView::slotOpenTriggered);
|
|
|
|
QAction *downloadAction = new QAction(QIcon(":/filemanageview/download.svg"), tr("Download"), this);
|
|
|
|
connect(downloadAction, &QAction::triggered, this, &FileView::slotDownloadTriggered);
|
|
|
|
if (list.size() == 1) {
|
|
|
|
menu->addAction(openAction);
|
|
|
|
}
|
|
|
|
menu->addAction(downloadAction);
|
|
|
|
menu->exec(QCursor::pos()); // 在当前鼠标位置显示
|
2022-11-23 20:55:36 +08:00
|
|
|
clearSelection();
|
2022-11-22 19:55:52 +08:00
|
|
|
|
|
|
|
delete menu;
|
|
|
|
delete openAction;
|
|
|
|
delete downloadAction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileView::slotOpenTriggered()
|
|
|
|
{
|
|
|
|
QModelIndex index = currentIndex();
|
|
|
|
openFile(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileView::slotDownloadTriggered()
|
|
|
|
{
|
|
|
|
sendDownloadInfo();
|
|
|
|
}
|