File indexing completed on 2024-05-19 05:21:55

0001 /*
0002  * Copyright (C) 2007 by Mathias Soeken <msoeken@tzi.de>
0003  * Copyright (C) 2019  Alexander Potashev <aspotashev@gmail.com>
0004  *
0005  *   This program is free software; you can redistribute it and/or modify
0006  *   it under the terms of the GNU General Public License as published by
0007  *   the Free Software Foundation; either version 2 of the License, or
0008  *   (at your option) any later version.
0009  *
0010  *   This program is distributed in the hope that it will be useful,
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  *   GNU General Public License for more details.
0014  *
0015  *   You should have received a copy of the GNU General Public License along
0016  *   with this program; if not, write to the
0017  *      Free Software Foundation, Inc.
0018  *      51 Franklin Street, Fifth Floor
0019  *      Boston, MA  02110-1301  USA.
0020  *
0021  */
0022 
0023 #include "treeviewheadercontextmenu.h"
0024 
0025 #include <QAction>
0026 #include <QHeaderView>
0027 #include <QTreeView>
0028 
0029 #include <KLocalizedString>
0030 
0031 #include "ktt_debug.h"
0032 
0033 TreeViewHeaderContextMenu::TreeViewHeaderContextMenu(QObject *parent, QTreeView *widget, QVector<int> &&excludedColumns)
0034     : QObject(parent)
0035     , m_widget(widget)
0036     , m_contextMenu(nullptr)
0037     , m_excludedColumns(excludedColumns)
0038 {
0039     m_widget->header()->setContextMenuPolicy(Qt::CustomContextMenu);
0040     connect(m_widget->header(),
0041             &QHeaderView::customContextMenuRequested,
0042             this,
0043             &TreeViewHeaderContextMenu::slotCustomContextMenuRequested);
0044 
0045     m_contextMenu = new QMenu(m_widget);
0046     m_contextMenu->addSection(i18nc("@title:menu", "Columns"));
0047     connect(m_contextMenu, &QMenu::triggered, this, &TreeViewHeaderContextMenu::slotTriggered);
0048     connect(m_contextMenu, &QMenu::aboutToShow, this, &TreeViewHeaderContextMenu::slotAboutToShow);
0049     updateActions();
0050 }
0051 
0052 TreeViewHeaderContextMenu::~TreeViewHeaderContextMenu()
0053 {
0054     qDeleteAll(m_actions);
0055 }
0056 
0057 void TreeViewHeaderContextMenu::slotCustomContextMenuRequested(const QPoint &pos)
0058 {
0059     qCDebug(KTT_LOG) << "Entering function";
0060     if (m_widget && m_contextMenu) {
0061         m_contextMenu->exec(m_widget->mapToGlobal(pos));
0062     }
0063 }
0064 
0065 void TreeViewHeaderContextMenu::updateActions()
0066 {
0067     qCDebug(KTT_LOG) << "Entering function";
0068     if (m_widget) {
0069         for (QAction *action : m_actions) {
0070             m_contextMenu->removeAction(action);
0071         }
0072 
0073         m_actionColumnMapping.clear();
0074         qDeleteAll(m_actions);
0075         m_actions.clear();
0076 
0077         for (int c = 0; c < m_widget->model()->columnCount(); ++c) {
0078             if (m_excludedColumns.contains(c)) {
0079                 continue;
0080             }
0081 
0082             auto *action = new QAction(this);
0083             updateAction(action, c);
0084             m_actions.append(action);
0085 
0086             m_contextMenu->addAction(action);
0087             m_actionColumnMapping[action] = c;
0088         }
0089     }
0090 }
0091 
0092 void TreeViewHeaderContextMenu::slotTriggered(QAction *action)
0093 {
0094     qCDebug(KTT_LOG) << "Entering function";
0095     if (m_widget && action) {
0096         int column = m_actionColumnMapping[action];
0097         bool hidden = m_widget->isColumnHidden(column);
0098         m_widget->setColumnHidden(column, !hidden);
0099         updateAction(action, column);
0100         Q_EMIT columnToggled(column);
0101     }
0102 }
0103 
0104 void TreeViewHeaderContextMenu::slotAboutToShow()
0105 {
0106     qCDebug(KTT_LOG) << "Entering function";
0107     for (QAction *action : m_actions) {
0108         updateAction(action, m_actionColumnMapping[action]);
0109     }
0110 }
0111 
0112 void TreeViewHeaderContextMenu::updateAction(QAction *action, int column)
0113 {
0114     action->setCheckable(true);
0115     action->setChecked(!m_widget->isColumnHidden(column));
0116     action->setText(m_widget->model()->headerData(column, Qt::Horizontal).toString());
0117 }
0118 
0119 #include "moc_treeviewheadercontextmenu.cpp"