Falkon Develop
Cross-platform Qt-based web browser
headerview.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "headerview.h"
19
20#include <QMenu>
21#include <QContextMenuEvent>
22
23HeaderView::HeaderView(QAbstractItemView* parent)
24 : QHeaderView(Qt::Horizontal, parent)
25 , m_parent(parent)
26 , m_resizeOnShow(false)
27{
28 setSectionsMovable(true);
29 setStretchLastSection(true);
30 setDefaultAlignment(Qt::AlignLeft);
31 setMinimumSectionSize(60);
32}
33
34void HeaderView::setDefaultSectionSizes(const QList<double> &sizes)
35{
36 m_sectionSizes = sizes;
37}
38
40{
41 return m_sectionSizes;
42}
43
44bool HeaderView::restoreState(const QByteArray &state)
45{
46 m_resizeOnShow = !QHeaderView::restoreState(state);
47
48 return !m_resizeOnShow;
49}
50
51void HeaderView::showEvent(QShowEvent* event)
52{
53 if (m_resizeOnShow) {
54 for (int i = 0; i < m_sectionSizes.count(); ++i) {
55 int size = m_parent->width() * m_sectionSizes.at(i);
56 resizeSection(i, size);
57 }
58 }
59
60 QHeaderView::showEvent(event);
61}
62
63void HeaderView::contextMenuEvent(QContextMenuEvent* event)
64{
65 if (!m_menu) {
66 m_menu = new QMenu(this);
67
68 for (int i = 0; i < count(); ++i) {
69 auto* act = new QAction(model()->headerData(i, Qt::Horizontal).toString(), m_menu);
70 act->setCheckable(true);
71 act->setData(i);
72
73 connect(act, &QAction::triggered, this, &HeaderView::toggleSectionVisibility);
74 m_menu->addAction(act);
75 }
76 }
77
78 for (int i = 0; i < m_menu->actions().count(); ++i) {
79 QAction* act = m_menu->actions().at(i);
80 act->setEnabled(i > 0);
81 act->setChecked(!isSectionHidden(i));
82 }
83
84 m_menu->popup(event->globalPos());
85}
86
87void HeaderView::toggleSectionVisibility()
88{
89 if (auto* act = qobject_cast<QAction*>(sender())) {
90 int index = act->data().toInt();
91
92 setSectionHidden(index, !isSectionHidden(index));
93 }
94}
void setDefaultSectionSizes(const QList< double > &sizes)
Definition: headerview.cpp:34
QList< double > defaultSectionSizes() const
Definition: headerview.cpp:39
HeaderView(QAbstractItemView *parent)
Definition: headerview.cpp:23
bool restoreState(const QByteArray &state)
Definition: headerview.cpp:44
i
Definition: i18n.py:23
State state