From abbba2890fbc10798c7ec05a3c159ccc9b9e9e8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Poizl?= Date: Sat, 26 Jul 2025 17:50:31 +0200 Subject: [PATCH] Add context menu to hide columns in file manager --- src/filemanager.cpp | 65 +++++++++++++++++++++++++++++++++++---------- src/filemanager.h | 7 +++++ src/filemanager.ui | 48 +++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 14 deletions(-) diff --git a/src/filemanager.cpp b/src/filemanager.cpp index 2e19c93..b7143bb 100644 --- a/src/filemanager.cpp +++ b/src/filemanager.cpp @@ -40,7 +40,7 @@ FileManager::~FileManager() settings.setValue("filemanager/leftTabPath", ui->leftTab->dir()); settings.setValue("filemanager/leftTabHeader", ui->leftTab->header()->saveState()); settings.setValue("filemanager/rightTabPath", ui->rightTab->dir()); - settings.setValue("filemanager/rightTabHeader", ui->leftTab->header()->saveState()); + settings.setValue("filemanager/rightTabHeader", ui->rightTab->header()->saveState()); settings.setValue("filemanager/geometry", saveGeometry()); delete ui; } @@ -61,6 +61,7 @@ DirFileSystemModel::DirFileSystemModel(QObject *parent) : QFileSystemModel(paren { _cache = getCacheInstance(); setFilter(QDir::AllEntries | QDir::NoDot); + _fitsKeywords = {"OBJECT", "RA", "DEC"}; } void DirFileSystemModel::setDir(const QString &path) @@ -80,7 +81,7 @@ Qt::ItemFlags DirFileSystemModel::flags(const QModelIndex &index) const int DirFileSystemModel::columnCount(const QModelIndex &parent) const { - return QFileSystemModel::columnCount(parent) + 1; + return QFileSystemModel::columnCount(parent) + _fitsKeywords.size(); } QVariant DirFileSystemModel::data(const QModelIndex &index, int role) const @@ -98,16 +99,23 @@ QVariant DirFileSystemModel::data(const QModelIndex &index, int role) const else { infoData = new ImageInfoData; - if(isFITS(suffix)) - readFITSHeader(path, *infoData); - else if(isXISF(suffix)) - readXISFHeader(path, *infoData); + if(_loadFitsKeywords) + { + if(isFITS(suffix)) + readFITSHeader(path, *infoData); + else if(isXISF(suffix)) + readXISFHeader(path, *infoData); + } _cache->insert(path, infoData); } - for(auto &record : infoData->fitsHeader) - if(record.key == "OBJECT") - return record.value; - + int column = index.column() - QFileSystemModel::columnCount(); + if(column < _fitsKeywords.size()) + { + const QString &key = _fitsKeywords.at(column); + for(auto &record : infoData->fitsHeader) + if(record.key == key) + return record.value; + } return ""; } return QFileSystemModel::data(index, role); @@ -116,7 +124,7 @@ QVariant DirFileSystemModel::data(const QModelIndex &index, int role) const QVariant DirFileSystemModel::headerData(int section, Qt::Orientation orientation, int role) const { if(orientation == Qt::Horizontal && role == Qt::DisplayRole && section >= QFileSystemModel::columnCount()) - return "FITS"; + return _fitsKeywords.at(section - QFileSystemModel::columnCount()); return QFileSystemModel::headerData(section, orientation, role); } @@ -128,13 +136,18 @@ bool DirFileSystemModel::hasChildren(const QModelIndex &parent) const return QFileSystemModel::hasChildren(parent); } +void DirFileSystemModel::loadFitsKeywords(bool enable) +{ + _loadFitsKeywords = enable; +} + DirView::DirView(QWidget *parent) : QTreeView(parent) { _dirFileSystemModel = new DirFileSystemModel(this); -#ifdef Q_OS_LINUX - _dirFileSystemModel->setRootPath("/"); -#elif defined(Q_OS_WIN64) +#ifdef Q_OS_WIN64 _dirFileSystemModel->setRootPath("C:/"); +#else + _dirFileSystemModel->setRootPath("/"); #endif _dirFileSystemModel->setReadOnly(false); setDragEnabled(true); @@ -157,6 +170,9 @@ DirView::DirView(QWidget *parent) : QTreeView(parent) QDesktopServices::openUrl(QUrl::fromLocalFile(info.absoluteFilePath())); } }); + + header()->setContextMenuPolicy(Qt::CustomContextMenu); + connect(header(), &QHeaderView::customContextMenuRequested, this, &DirView::headerContextMenu); } void DirView::setDir(const QString &path) @@ -176,3 +192,24 @@ void DirView::setOpenFilter(const QSet &openFilter) { _openFilter = openFilter; } + +void DirView::headerContextMenu(const QPoint &pos) +{ + QHeaderView *head = header(); + QMenu menu; + int count = head->count(); + for(int i = 0; i < count; i++) + { + QAction *a = menu.addAction(head->model()->headerData(i, Qt::Horizontal).toString()); + a->setCheckable(true); + a->setChecked(!head->isSectionHidden(i)); + a->setData(i); + } + + QAction *a = menu.exec(mapToGlobal(pos)); + if(a) + { + if(a->isChecked())head->showSection(a->data().toInt()); + else head->hideSection(a->data().toInt()); + } +} diff --git a/src/filemanager.h b/src/filemanager.h index 709132b..d5e1ac9 100644 --- a/src/filemanager.h +++ b/src/filemanager.h @@ -25,9 +25,12 @@ private: class DirFileSystemModel : public QFileSystemModel { + Q_OBJECT mutable QCache *_cache = nullptr; static QCache* getCacheInstance(); QModelIndex _dir; + QStringList _fitsKeywords; + bool _loadFitsKeywords = true; public: explicit DirFileSystemModel(QObject *parent = nullptr); void setDir(const QString &path); @@ -37,6 +40,8 @@ public: QVariant data(const QModelIndex &index, int role) const override; QVariant headerData(int section, Qt::Orientation orientation, int role) const override; bool hasChildren(const QModelIndex &parent) const override; +public slots: + void loadFitsKeywords(bool enable); }; class DirView : public QTreeView @@ -49,6 +54,8 @@ public: void setDir(const QString &path); QString dir() const; void setOpenFilter(const QSet &openFilter); +public slots: + void headerContextMenu(const QPoint &pos); signals: void dirChanged(const QString &path); void openFile(const QString &path); diff --git a/src/filemanager.ui b/src/filemanager.ui index 137be0a..8316fe3 100644 --- a/src/filemanager.ui +++ b/src/filemanager.ui @@ -54,7 +54,55 @@ 23 + + + Left Tab + + + + + + + Right Tab + + + + + + + + + true + + + true + + + Load FITS keywords + + + + + true + + + true + + + Load FITS keywords + + + + + Select columns + + + + + Select columns + +