#include "filesystemwidget.h" #include #include #include #include #include #include FilesystemWidget::FilesystemWidget(QAbstractItemModel *model, QWidget *parent) : QWidget(parent) , m_model(model) { m_listView = new QListView(this); m_listView->setModel(model); QVBoxLayout *layout = new QVBoxLayout(this); layout->addWidget(m_listView); setLayout(layout); connect(m_listView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(fileClicked(QModelIndex))); } void FilesystemWidget::setDir(const QString &dir) { //m_model->setRootPath(dir); //m_treeView->setRootIndex(m_model->index(m_model->rootPath())); } void FilesystemWidget::selectFile(int row) { QModelIndex index = m_model->index(row, 0); m_listView->selectionModel()->clearSelection(); m_listView->selectionModel()->select(index, QItemSelectionModel::SelectCurrent); m_listView->scrollTo(index); } void FilesystemWidget::fileClicked(const QModelIndex &index) { emit fileSelected(index.row()); } Filetree::Filetree(QWidget *parent) : QTreeView(parent) { QSettings settings; m_rootDir = settings.value("filetree/rootDir", QDir::homePath()).toString(); m_fileSystemModel = new QFileSystemModel(this); m_fileSystemModel->setRootPath(m_rootDir); m_fileSystemModel->setNameFilters({"*.fits", "*.fit", "*.xisf", "*.jpg", "*.jpeg", "*.png"}); m_fileSystemModel->setNameFilterDisables(false); setModel(m_fileSystemModel); setRootIndex(m_fileSystemModel->index(m_rootDir)); header()->restoreState(settings.value("filetree/header").toByteArray()); } Filetree::~Filetree() { QSettings settings; settings.setValue("filetree/rootDir", m_rootDir); settings.setValue("filetree/header", header()->saveState()); } void Filetree::contextMenuEvent(QContextMenuEvent *event) { QModelIndex index = indexAt(event->pos()); QFileInfo info = m_fileSystemModel->fileInfo(index); QMenu menu; QAction *open = nullptr; QAction *setRoot = nullptr; QAction *copy = nullptr; QAction *move = nullptr; QAction *indexDir = nullptr; if(info.isFile()) open = menu.addAction(tr("Open")); if(info.isDir()) { open = menu.addAction(tr("Open")); setRoot = menu.addAction(tr("Set as root")); copy = menu.addAction(tr("Copy marked files")); move = menu.addAction(tr("Move marked files")); indexDir = menu.addAction(tr("Index directory")); } menu.addSeparator(); QAction *resetRoot = menu.addAction(tr("Reset root")); QAction *goUp = menu.addAction(tr("Go up")); QAction *a = menu.exec(event->globalPos()); if(a == nullptr) return; if(a == open) { emit fileSelected(m_fileSystemModel->filePath(index)); } else if(a == setRoot && index.isValid()) { setRootIndex(index); m_rootDir = m_fileSystemModel->filePath(index); } else if(a == resetRoot) { setRootIndex(QModelIndex()); m_rootDir = QDir::rootPath(); } else if(a == goUp) { setRootIndex(rootIndex().parent()); m_rootDir = m_fileSystemModel->filePath(rootIndex().parent()); } else if(a == copy) { emit copyFiles(m_fileSystemModel->filePath(index)); } else if(a == move) { emit moveFiles(m_fileSystemModel->filePath(index)); } else if(a == indexDir) { emit indexDirectory(m_fileSystemModel->filePath(index)); } } void Filetree::mouseDoubleClickEvent(QMouseEvent *event) { QModelIndex index = indexAt(event->pos()); QFileInfo info = m_fileSystemModel->fileInfo(index); if(info.isFile()) emit fileSelected(info.filePath()); else QTreeView::mouseDoubleClickEvent(event); }