File indexing completed on 2025-02-23 04:34:20
0001 /** 0002 * \file configurabletreeview.h 0003 * QTreeView with configurable visibility, order and sort column. 0004 * 0005 * \b Project: Kid3 0006 * \author Urs Fleisch 0007 * \date 3 Jan 2014 0008 * 0009 * Copyright (C) 2014-2024 Urs Fleisch 0010 * 0011 * This file is part of Kid3. 0012 * 0013 * Kid3 is free software; you can redistribute it and/or modify 0014 * it under the terms of the GNU General Public License as published by 0015 * the Free Software Foundation; either version 2 of the License, or 0016 * (at your option) any later version. 0017 * 0018 * Kid3 is distributed in the hope that it will be useful, 0019 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0021 * GNU General Public License for more details. 0022 * 0023 * You should have received a copy of the GNU General Public License 0024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0025 */ 0026 0027 #pragma once 0028 0029 #include <QTreeView> 0030 #include <QKeySequence> 0031 0032 class QActionGroup; 0033 0034 /** 0035 * QTreeView with configurable visibility, order and sort column. 0036 */ 0037 class ConfigurableTreeView : public QTreeView { 0038 Q_OBJECT 0039 public: 0040 /** 0041 * Constructor. 0042 * @param parent parent widget 0043 */ 0044 explicit ConfigurableTreeView(QWidget* parent = nullptr); 0045 0046 /** 0047 * Destructor. 0048 */ 0049 ~ConfigurableTreeView() override = default; 0050 0051 /** 0052 * Set visible columns. 0053 * @param columns logical indexes of visible columns 0054 */ 0055 void setVisibleColumns(const QList<int>& columns); 0056 0057 /** 0058 * Get visible columns. 0059 * @return logical indexes of visible columns. 0060 */ 0061 QList<int> getVisibleColumns() const; 0062 0063 /** 0064 * Set if custom column widths are enabled. 0065 * @param enable true to enable custom column widths, false for automatic 0066 * column widths 0067 */ 0068 void setCustomColumnWidthsEnabled(bool enable); 0069 0070 /** 0071 * Check if custom column widths are enabled. 0072 * @return true if custom column widths are enabled. 0073 */ 0074 bool areCustomColumnWidthsEnabled() const; 0075 0076 /** 0077 * Initialize custom column widths from contents if not yet valid. 0078 * @param minimumWidth minimum width for column, -1 if not used 0079 * @return size of first section, -1 when initialization not necessary. 0080 */ 0081 int initializeColumnWidthsFromContents(int minimumWidth); 0082 0083 /** 0084 * Set column widths. 0085 * @param columnWidths column widths 0086 */ 0087 void setColumnWidths(const QList<int>& columnWidths); 0088 0089 /** 0090 * Get column widths. 0091 * @return column widths. 0092 */ 0093 QList<int> getColumnWidths() const; 0094 0095 /** 0096 * Set the maximum number of columns shown. 0097 * This can be used to restrict the number of columns, e.g. to omit columns 0098 * with tag data. 0099 * @param maxNumColumns maximum number of columns 0100 */ 0101 void setMaxNumColumns(int maxNumColumns) { m_maxNumColumns = maxNumColumns; } 0102 0103 /** 0104 * Get sort column and order. 0105 * This method returns the values which can be set with sortByColumn(). 0106 * 0107 * @param column the logical index of the sort column is returned here 0108 * @param order the sort order is returned here 0109 */ 0110 void getSortByColumn(int& column, Qt::SortOrder& order) const; 0111 0112 /** 0113 * Temporarily disconnect the model to improve performance. 0114 * The old model state is preserved and will be restored by reconnectModel(). 0115 */ 0116 void disconnectModel(); 0117 0118 /** 0119 * Reconnect to the model. 0120 * The state before the call to disconnectModel() is restored. 0121 */ 0122 void reconnectModel(); 0123 0124 /** 0125 * Set keyboard shortcuts for the open parent and open current actions. 0126 * @param map map of action names to key sequences 0127 */ 0128 void setShortcuts(const QMap<QString, QKeySequence>& map); 0129 0130 signals: 0131 /** 0132 * Emitted when the parent shall be activated. 0133 * This is emitted when Command + Up is pressed to mimic the shortcut of the 0134 * macOS Finder. 0135 * @param index root index of item view 0136 */ 0137 void parentActivated(const QModelIndex& index); 0138 0139 protected: 0140 /** 0141 * Reimplemented to go to parent item with Left key and 0142 * make Return/Enter send activated() also on the Mac. 0143 * @param event key event 0144 */ 0145 void keyPressEvent(QKeyEvent* event) override; 0146 0147 private slots: 0148 /** 0149 * Show context menu for header. 0150 * @param pos context menu position 0151 */ 0152 void showHeaderContextMenu(const QPoint& pos); 0153 0154 /** 0155 * Toggle visibility of column. 0156 * @param visible true to set column visible 0157 */ 0158 void toggleColumnVisibility(bool visible); 0159 0160 private: 0161 /** 0162 * Set column widths to custom column widths set with setColumnWidths(). 0163 * @return true if custom column widths settings could be applied. 0164 */ 0165 bool resizeColumnWidths(); 0166 0167 quint32 m_columnVisibility; 0168 0169 /** State stored by disconnectModel() and restored by reconnectModel() */ 0170 QAbstractItemModel* m_oldModel; 0171 QItemSelectionModel* m_oldSelectionModel; 0172 QPersistentModelIndex m_oldRootIndex; 0173 QList<int> m_columnWidths; 0174 QActionGroup* m_columnActionGroup; 0175 QAction* m_autoColumnAction; 0176 QAction* m_customColumnAction; 0177 QKeySequence m_openParentKey; 0178 QKeySequence m_openCurrentKey; 0179 int m_maxNumColumns; 0180 };