]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistheader.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "kitemlistheader_p.h"
22 #include "kitemmodelbase.h"
24 #include <QFontMetricsF>
26 #include <QStyleOptionHeader>
28 KItemListHeader::KItemListHeader(QGraphicsWidget
* parent
) :
29 QGraphicsWidget(parent
),
32 m_visibleRolesWidths()
34 QStyleOptionHeader opt
;
35 const QSize headerSize
= style()->sizeFromContents(QStyle::CT_HeaderSection
, &opt
, QSize());
36 resize(0, headerSize
.height());
39 KItemListHeader::~KItemListHeader()
43 void KItemListHeader::setModel(KItemModelBase
* model
)
45 if (m_model
== 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
)));
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
)));
66 KItemModelBase
* KItemListHeader::model() const
71 void KItemListHeader::setVisibleRoles(const QList
<QByteArray
>& roles
)
73 m_visibleRoles
= roles
;
77 QList
<QByteArray
> KItemListHeader::visibleRoles() const
79 return m_visibleRoles
;
82 void KItemListHeader::setVisibleRolesWidths(const QHash
<QByteArray
, qreal
> rolesWidths
)
84 m_visibleRolesWidths
= rolesWidths
;
88 QHash
<QByteArray
, qreal
> KItemListHeader::visibleRolesWidths() const
90 return m_visibleRolesWidths
;
93 void KItemListHeader::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
101 opt
.rect
= rect().toRect();
102 opt
.state
|= QStyle::State_Horizontal
;
103 style()->drawControl(QStyle::CE_HeaderEmptyArea
, &opt
, painter
);
110 // TODO: This is a rough draft only
111 QFontMetricsF
fontMetrics(font());
112 QTextOption
textOption(Qt::AlignLeft
| Qt::AlignVCenter
);
114 painter
->setFont(font());
115 painter
->setPen(palette().text().color());
117 const qreal margin
= style()->pixelMetric(QStyle::PM_HeaderMargin
);
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
);
125 x
+= m_visibleRolesWidths
.value(role
) + margin
;
129 void KItemListHeader::slotSortRoleChanged(const QByteArray
& current
, const QByteArray
& previous
)
135 void KItemListHeader::slotSortOrderChanged(Qt::SortOrder current
, Qt::SortOrder previous
)
141 #include "kitemlistheader_p.moc"