From 903de8188d84293cccaeb7a5a499d8dbcdea378e Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Tue, 1 Sep 2015 20:43:49 +0200 Subject: [PATCH] GUI: implement resetting columns See #1268. --- ChangeLog | 6 ++ src/mkvtoolnix-gui/chapter_editor/tab.cpp | 4 +- src/mkvtoolnix-gui/jobs/tool.cpp | 2 +- src/mkvtoolnix-gui/merge/attachments.cpp | 2 +- src/mkvtoolnix-gui/merge/input.cpp | 4 +- .../util/header_view_manager.cpp | 79 +++++++++++++------ src/mkvtoolnix-gui/util/header_view_manager.h | 6 +- src/mkvtoolnix-gui/util/model.cpp | 8 -- src/mkvtoolnix-gui/util/model.h | 2 - 9 files changed, 70 insertions(+), 43 deletions(-) diff --git a/ChangeLog b/ChangeLog index ae70b8752..dbb4f2740 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2015-09-01 Moritz Bunkus + + * MKVToolNix GUI: new feature: the state of all columns in all + list/tree views can be reset (both the shown/hidden state as well + as their order) from the column's context menu. See #1268. + 2015-08-30 Moritz Bunkus * MKVToolNix GUI: new feature: the column headers of all list/tree diff --git a/src/mkvtoolnix-gui/chapter_editor/tab.cpp b/src/mkvtoolnix-gui/chapter_editor/tab.cpp index 111cdd188..f2bbf433e 100644 --- a/src/mkvtoolnix-gui/chapter_editor/tab.cpp +++ b/src/mkvtoolnix-gui/chapter_editor/tab.cpp @@ -91,8 +91,8 @@ Tab::setupUi() { Util::fixScrollAreaBackground(ui->scrollArea); Util::preventScrollingWithoutFocus(this); - Util::HeaderViewManager::create(*ui->elements->header(), "ChapterEditor::Elements"); - Util::HeaderViewManager::create(*ui->tvChNames->header(), "ChapterEditor::ChapterNames"); + Util::HeaderViewManager::create(*ui->elements, "ChapterEditor::Elements"); + Util::HeaderViewManager::create(*ui->tvChNames, "ChapterEditor::ChapterNames"); auto mw = MainWindow::get(); connect(ui->elements, &Util::BasicTreeView::customContextMenuRequested, this, &Tab::showChapterContextMenu); diff --git a/src/mkvtoolnix-gui/jobs/tool.cpp b/src/mkvtoolnix-gui/jobs/tool.cpp index de35457f1..9517c7fbe 100644 --- a/src/mkvtoolnix-gui/jobs/tool.cpp +++ b/src/mkvtoolnix-gui/jobs/tool.cpp @@ -70,7 +70,7 @@ Tool::setupUi() { ui->jobs->setModel(m_model); Util::preventScrollingWithoutFocus(this); - Util::HeaderViewManager::create(*ui->jobs->header(), "Jobs::Jobs"); + Util::HeaderViewManager::create(*ui->jobs, "Jobs::Jobs"); m_jobsMenu->addAction(m_viewOutputAction); m_jobsMenu->addAction(m_openFolderAction); diff --git a/src/mkvtoolnix-gui/merge/attachments.cpp b/src/mkvtoolnix-gui/merge/attachments.cpp index 430440244..e7f90bdf8 100644 --- a/src/mkvtoolnix-gui/merge/attachments.cpp +++ b/src/mkvtoolnix-gui/merge/attachments.cpp @@ -52,7 +52,7 @@ Tab::setupAttachmentsControls() { onAttachmentSelectionChanged(); - Util::HeaderViewManager::create(*ui->attachments->header(), "Merge::Attachments"); + Util::HeaderViewManager::create(*ui->attachments, "Merge::Attachments"); } void diff --git a/src/mkvtoolnix-gui/merge/input.cpp b/src/mkvtoolnix-gui/merge/input.cpp index fd9bfc98c..5ac83d36d 100644 --- a/src/mkvtoolnix-gui/merge/input.cpp +++ b/src/mkvtoolnix-gui/merge/input.cpp @@ -207,8 +207,8 @@ Tab::setupInputControls() { enableMoveFilesButtons(); onTrackSelectionChanged(); - Util::HeaderViewManager::create(*ui->files->header(), "Merge::Files"); - Util::HeaderViewManager::create(*ui->tracks->header(), "Merge::Tracks"); + Util::HeaderViewManager::create(*ui->files, "Merge::Files"); + Util::HeaderViewManager::create(*ui->tracks, "Merge::Tracks"); } void diff --git a/src/mkvtoolnix-gui/util/header_view_manager.cpp b/src/mkvtoolnix-gui/util/header_view_manager.cpp index 2462a2ab0..644710589 100644 --- a/src/mkvtoolnix-gui/util/header_view_manager.cpp +++ b/src/mkvtoolnix-gui/util/header_view_manager.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "common/qt.h" #include "mkvtoolnix-gui/main_window/main_window.h" @@ -19,7 +20,7 @@ namespace mtx { namespace gui { namespace Util { class HeaderViewManagerPrivate { friend class HeaderViewManager; - QHeaderView *headerView{}; + QTreeView *treeView{}; QString name; bool restoringState{}; }; @@ -34,17 +35,19 @@ HeaderViewManager::~HeaderViewManager() { } void -HeaderViewManager::manage(QHeaderView &headerView, +HeaderViewManager::manage(QTreeView &treeView, QString const &name) { Q_D(HeaderViewManager); - d->name = name; - d->headerView = &headerView; + d->name = name; + d->treeView = &treeView; - d->headerView->setContextMenuPolicy(Qt::CustomContextMenu); + auto headerView = d->treeView->header(); - connect(d->headerView, &QHeaderView::customContextMenuRequested, this, &HeaderViewManager::showContextMenu); - connect(d->headerView, &QHeaderView::sectionMoved, this, &HeaderViewManager::saveState); + headerView->setContextMenuPolicy(Qt::CustomContextMenu); + + connect(headerView, &QHeaderView::customContextMenuRequested, this, &HeaderViewManager::showContextMenu); + connect(headerView, &QHeaderView::sectionMoved, this, &HeaderViewManager::saveState); QTimer::singleShot(0, this, SLOT(restoreState())); } @@ -57,10 +60,11 @@ HeaderViewManager::saveState() { return; QStringList hiddenFlags, visualIndexes; + auto headerView = d->treeView->header(); - for (auto logicalIndex = 0, columnCount = d->headerView->count(); logicalIndex < columnCount; ++logicalIndex) { - hiddenFlags << Q("%1").arg(d->headerView->isSectionHidden(logicalIndex)); - visualIndexes << Q("%1").arg(d->headerView->visualIndex(logicalIndex)); + for (auto logicalIndex = 0, columnCount = headerView->count(); logicalIndex < columnCount; ++logicalIndex) { + hiddenFlags << Q("%1").arg(headerView->isSectionHidden(logicalIndex)); + visualIndexes << Q("%1").arg(headerView->visualIndex(logicalIndex)); } auto reg = Settings::registry(); @@ -99,21 +103,23 @@ void HeaderViewManager::restoreHidden(QStringList hiddenFlags) { Q_D(HeaderViewManager); - auto const columnCount = d->headerView->count(); + auto headerView = d->treeView->header(); + auto const columnCount = headerView->count(); while (hiddenFlags.count() < columnCount) hiddenFlags << Q("0"); for (auto logicalIndex = 0; logicalIndex < columnCount; ++logicalIndex) - d->headerView->setSectionHidden(logicalIndex, !!hiddenFlags[logicalIndex].toInt()); + headerView->setSectionHidden(logicalIndex, !!hiddenFlags[logicalIndex].toInt()); } void HeaderViewManager::restoreVisualIndexes(QStringList visualIndexes) { Q_D(HeaderViewManager); + auto headerView = d->treeView->header(); auto visualToLogical = QHash{}; - auto const columnCount = d->headerView->count(); + auto const columnCount = headerView->count(); while (visualIndexes.count() < columnCount) visualIndexes << Q("%1").arg(visualIndexes.count()); @@ -131,18 +137,18 @@ HeaderViewManager::restoreVisualIndexes(QStringList visualIndexes) { if ((0 > logicalIndex) || (columnCount <= logicalIndex)) continue; - auto currentVisualIndex = d->headerView->visualIndex(logicalIndex); + auto currentVisualIndex = headerView->visualIndex(logicalIndex); if (currentVisualIndex != visualIndex) - d->headerView->moveSection(currentVisualIndex, visualIndex); + headerView->moveSection(currentVisualIndex, visualIndex); } } HeaderViewManager * -HeaderViewManager::create(QHeaderView &headerView, +HeaderViewManager::create(QTreeView &treeView, QString const &name) { - auto manager = new HeaderViewManager{&headerView}; - manager->manage(headerView, name); + auto manager = new HeaderViewManager{&treeView}; + manager->manage(treeView, name); return manager; } @@ -151,27 +157,52 @@ void HeaderViewManager::toggleColumn(int column) { Q_D(HeaderViewManager); - d->headerView->setSectionHidden(column, !d->headerView->isSectionHidden(column)); + auto headerView = d->treeView->header(); + headerView->setSectionHidden(column, !headerView->isSectionHidden(column)); saveState(); } +void +HeaderViewManager::resetColumns() { + Q_D(HeaderViewManager); + + d->restoringState = true; + + restoreVisualIndexes({}); + restoreHidden({}); + + d->restoringState = false; + + saveState(); + + resizeViewColumnsToContents(d->treeView); +} + void HeaderViewManager::showContextMenu(QPoint const &pos) { Q_D(HeaderViewManager); - auto menu = new QMenu{d->headerView}; + auto headerView = d->treeView->header(); + auto menu = new QMenu{headerView}; + auto action = new QAction{menu}; - for (int column = 1, columnCount = d->headerView->count(); column < columnCount; ++column) { + action->setText(QY("Reset all columns")); + menu->addAction(action); + menu->addSeparator(); + + connect(action, &QAction::triggered, this, &HeaderViewManager::resetColumns); + + for (int column = 1, columnCount = headerView->count(); column < columnCount; ++column) { auto action = new QAction{menu}; - auto text = d->headerView->model()->headerData(column, Qt::Horizontal, Util::HiddenDescriptionRole).toString(); + auto text = headerView->model()->headerData(column, Qt::Horizontal, Util::HiddenDescriptionRole).toString(); if (text.isEmpty()) - text = d->headerView->model()->headerData(column, Qt::Horizontal, Qt::DisplayRole).toString(); + text = headerView->model()->headerData(column, Qt::Horizontal, Qt::DisplayRole).toString(); action->setText(text); action->setCheckable(true); - action->setChecked(!d->headerView->isSectionHidden(column)); + action->setChecked(!headerView->isSectionHidden(column)); menu->addAction(action); diff --git a/src/mkvtoolnix-gui/util/header_view_manager.h b/src/mkvtoolnix-gui/util/header_view_manager.h index b03ea7b82..4b881500f 100644 --- a/src/mkvtoolnix-gui/util/header_view_manager.h +++ b/src/mkvtoolnix-gui/util/header_view_manager.h @@ -5,8 +5,8 @@ #include -class QHeaderView; class QString; +class QTreeView; namespace mtx { namespace gui { namespace Util { @@ -18,7 +18,7 @@ public: explicit HeaderViewManager(QObject *parent = nullptr); virtual ~HeaderViewManager(); - void manage(QHeaderView &headerView, QString const &name); + void manage(QTreeView &treeView, QString const &name); public slots: void saveState(); @@ -29,7 +29,7 @@ public slots: void resetColumns(); public: - static HeaderViewManager *create(QHeaderView &headerView, QString const &name); + static HeaderViewManager *create(QTreeView &treeView, QString const &name); protected: void restoreHidden(QStringList hiddenFlags); diff --git a/src/mkvtoolnix-gui/util/model.cpp b/src/mkvtoolnix-gui/util/model.cpp index 7d248c2d2..6bfa82cf7 100644 --- a/src/mkvtoolnix-gui/util/model.cpp +++ b/src/mkvtoolnix-gui/util/model.cpp @@ -3,20 +3,12 @@ #include #include #include -#include #include #include "mkvtoolnix-gui/util/model.h" namespace mtx { namespace gui { namespace Util { -void -resizeViewColumnsToContents(QTableView *view) { - auto columnCount = view->model()->columnCount(QModelIndex{}); - for (auto column = 0; columnCount > column; ++column) - view->resizeColumnToContents(column); -} - void resizeViewColumnsToContents(QTreeView *view) { auto columnCount = view->model()->columnCount(QModelIndex{}); diff --git a/src/mkvtoolnix-gui/util/model.h b/src/mkvtoolnix-gui/util/model.h index b0f804831..126cdd5e7 100644 --- a/src/mkvtoolnix-gui/util/model.h +++ b/src/mkvtoolnix-gui/util/model.h @@ -6,7 +6,6 @@ class QAbstractItemView; class QItemSelection; class QItemSelectionModel; -class QTableView; class QTreeView; namespace mtx { namespace gui { namespace Util { @@ -23,7 +22,6 @@ enum MtxGuiRoles { HiddenDescriptionRole, }; -void resizeViewColumnsToContents(QTableView *view); void resizeViewColumnsToContents(QTreeView *view); int numSelectedRows(QItemSelection &selection); QModelIndex selectedRowIdx(QItemSelection const &selection);