File indexing completed on 2024-05-12 04:58:27
0001 /* ============================================================ 0002 * Falkon - Qt web browser 0003 * Copyright (C) 2010-2014 David Rosca <nowrep@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 3 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 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 #include "headerview.h" 0019 0020 #include <QMenu> 0021 #include <QContextMenuEvent> 0022 0023 HeaderView::HeaderView(QAbstractItemView* parent) 0024 : QHeaderView(Qt::Horizontal, parent) 0025 , m_parent(parent) 0026 , m_resizeOnShow(false) 0027 { 0028 setSectionsMovable(true); 0029 setStretchLastSection(true); 0030 setDefaultAlignment(Qt::AlignLeft); 0031 setMinimumSectionSize(60); 0032 } 0033 0034 void HeaderView::setDefaultSectionSizes(const QList<double> &sizes) 0035 { 0036 m_sectionSizes = sizes; 0037 } 0038 0039 QList<double> HeaderView::defaultSectionSizes() const 0040 { 0041 return m_sectionSizes; 0042 } 0043 0044 bool HeaderView::restoreState(const QByteArray &state) 0045 { 0046 m_resizeOnShow = !QHeaderView::restoreState(state); 0047 0048 return !m_resizeOnShow; 0049 } 0050 0051 void HeaderView::showEvent(QShowEvent* event) 0052 { 0053 if (m_resizeOnShow) { 0054 for (int i = 0; i < m_sectionSizes.count(); ++i) { 0055 int size = m_parent->width() * m_sectionSizes.at(i); 0056 resizeSection(i, size); 0057 } 0058 } 0059 0060 QHeaderView::showEvent(event); 0061 } 0062 0063 void HeaderView::contextMenuEvent(QContextMenuEvent* event) 0064 { 0065 if (!m_menu) { 0066 m_menu = new QMenu(this); 0067 0068 for (int i = 0; i < count(); ++i) { 0069 auto* act = new QAction(model()->headerData(i, Qt::Horizontal).toString(), m_menu); 0070 act->setCheckable(true); 0071 act->setData(i); 0072 0073 connect(act, &QAction::triggered, this, &HeaderView::toggleSectionVisibility); 0074 m_menu->addAction(act); 0075 } 0076 } 0077 0078 for (int i = 0; i < m_menu->actions().count(); ++i) { 0079 QAction* act = m_menu->actions().at(i); 0080 act->setEnabled(i > 0); 0081 act->setChecked(!isSectionHidden(i)); 0082 } 0083 0084 m_menu->popup(event->globalPos()); 0085 } 0086 0087 void HeaderView::toggleSectionVisibility() 0088 { 0089 if (auto* act = qobject_cast<QAction*>(sender())) { 0090 int index = act->data().toInt(); 0091 0092 setSectionHidden(index, !isSectionHidden(index)); 0093 } 0094 }