]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistheader.cpp
Show the role-description in the header of the details view
[dolphin.git] / src / kitemviews / kitemlistheader.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "kitemlistheader_p.h"
21
22 #include "kitemmodelbase.h"
23
24 #include <QFontMetricsF>
25 #include <QPainter>
26 #include <QStyleOptionHeader>
27
28 KItemListHeader::KItemListHeader(QGraphicsWidget* parent) :
29 QGraphicsWidget(parent),
30 m_model(0),
31 m_visibleRoles(),
32 m_visibleRolesWidths()
33 {
34 QStyleOptionHeader opt;
35 const QSize headerSize = style()->sizeFromContents(QStyle::CT_HeaderSection, &opt, QSize());
36 resize(0, headerSize.height());
37 }
38
39 KItemListHeader::~KItemListHeader()
40 {
41 }
42
43 void KItemListHeader::setModel(KItemModelBase* model)
44 {
45 if (m_model == model) {
46 return;
47 }
48
49 if (m_model) {
50 disconnect(m_model, SIGNAL(sortRoleChanged(QByteArray,QByteArray)),
51 this, SLOT(slotSortRoleChanged(QByteArray,QByteArray)));
52 disconnect(m_model, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)),
53 this, SLOT(slotSortOrderChanged(Qt::SortOrder,Qt::SortOrder)));
54 }
55
56 m_model = model;
57
58 if (m_model) {
59 connect(m_model, SIGNAL(sortRoleChanged(QByteArray,QByteArray)),
60 this, SLOT(slotSortRoleChanged(QByteArray,QByteArray)));
61 connect(m_model, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)),
62 this, SLOT(slotSortOrderChanged(Qt::SortOrder,Qt::SortOrder)));
63 }
64 }
65
66 KItemModelBase* KItemListHeader::model() const
67 {
68 return m_model;
69 }
70
71 void KItemListHeader::setVisibleRoles(const QList<QByteArray>& roles)
72 {
73 m_visibleRoles = roles;
74 update();
75 }
76
77 QList<QByteArray> KItemListHeader::visibleRoles() const
78 {
79 return m_visibleRoles;
80 }
81
82 void KItemListHeader::setVisibleRolesWidths(const QHash<QByteArray, qreal> rolesWidths)
83 {
84 m_visibleRolesWidths = rolesWidths;
85 update();
86 }
87
88 QHash<QByteArray, qreal> KItemListHeader::visibleRolesWidths() const
89 {
90 return m_visibleRolesWidths;
91 }
92
93 void KItemListHeader::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
94 {
95 Q_UNUSED(option);
96 Q_UNUSED(widget);
97
98 // Draw background
99 QStyleOption opt;
100 opt.init(widget);
101 opt.rect = rect().toRect();
102 opt.state |= QStyle::State_Horizontal;
103 style()->drawControl(QStyle::CE_HeaderEmptyArea, &opt, painter);
104
105 if (!m_model) {
106 return;
107 }
108
109 // Draw roles
110 // TODO: This is a rough draft only
111 QFontMetricsF fontMetrics(font());
112 QTextOption textOption(Qt::AlignLeft | Qt::AlignVCenter);
113
114 painter->setFont(font());
115 painter->setPen(palette().text().color());
116
117 const qreal margin = style()->pixelMetric(QStyle::PM_HeaderMargin);
118 qreal x = margin;
119 foreach (const QByteArray& role, m_visibleRoles) {
120 const QString roleDescription = m_model->roleDescription(role);
121 const qreal textWidth = fontMetrics.width(roleDescription);
122 QRectF rect(x, 0, textWidth, size().height());
123 painter->drawText(rect, roleDescription, textOption);
124
125 x += m_visibleRolesWidths.value(role) + margin;
126 }
127 }
128
129 void KItemListHeader::slotSortRoleChanged(const QByteArray& current, const QByteArray& previous)
130 {
131 Q_UNUSED(current);
132 Q_UNUSED(previous);
133 }
134
135 void KItemListHeader::slotSortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous)
136 {
137 Q_UNUSED(current);
138 Q_UNUSED(previous);
139 }
140
141 #include "kitemlistheader_p.moc"