Add context menu to hide columns in file manager

This commit is contained in:
2025-07-26 17:50:31 +02:00
parent 9c6847d334
commit abbba2890f
3 changed files with 106 additions and 14 deletions
+51 -14
View File
@@ -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<QString> &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());
}
}
+7
View File
@@ -25,9 +25,12 @@ private:
class DirFileSystemModel : public QFileSystemModel
{
Q_OBJECT
mutable QCache<QString, ImageInfoData> *_cache = nullptr;
static QCache<QString, ImageInfoData>* 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<QString> &openFilter);
public slots:
void headerContextMenu(const QPoint &pos);
signals:
void dirChanged(const QString &path);
void openFile(const QString &path);
+48
View File
@@ -54,7 +54,55 @@
<height>23</height>
</rect>
</property>
<widget class="QMenu" name="menuLeft_Tab">
<property name="title">
<string>Left Tab</string>
</property>
<addaction name="actionLoad_FITS_keywordsLeft"/>
<addaction name="actionSelect_columnsLeft"/>
</widget>
<widget class="QMenu" name="menuRight_Tab">
<property name="title">
<string>Right Tab</string>
</property>
<addaction name="actionLoad_FITS_keywordsRight"/>
<addaction name="actionSelect_columnsRight"/>
</widget>
<addaction name="menuLeft_Tab"/>
<addaction name="menuRight_Tab"/>
</widget>
<action name="actionLoad_FITS_keywordsLeft">
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="text">
<string>Load FITS keywords</string>
</property>
</action>
<action name="actionLoad_FITS_keywordsRight">
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="text">
<string>Load FITS keywords</string>
</property>
</action>
<action name="actionSelect_columnsLeft">
<property name="text">
<string>Select columns</string>
</property>
</action>
<action name="actionSelect_columnsRight">
<property name="text">
<string>Select columns</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>