Add Filetree dock

This commit is contained in:
2022-05-09 15:58:23 +02:00
parent 8c248b7cfc
commit c971a919ec
4 changed files with 88 additions and 0 deletions
+63
View File
@@ -1,6 +1,10 @@
#include "filesystemwidget.h"
#include <QSettings>
#include <QVBoxLayout>
#include <QContextMenuEvent>
#include <QMenu>
#include <QSettings>
#include <QHeaderView>
FilesystemWidget::FilesystemWidget(QAbstractItemModel *model, QWidget *parent) : QWidget(parent)
, m_model(model)
@@ -34,3 +38,62 @@ 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;
if(info.isFile())
open = menu.addAction(tr("Open"));
if(info.isDir())
setRoot = menu.addAction("Set as root");
QAction *resetRoot = menu.addAction("Reset root");
QAction *goUp = menu.addAction("Go up");
QAction *a = menu.exec(event->globalPos());
if(a == open)
{
emit fileSelected(m_fileSystemModel->filePath(index));
}
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());
}
}