]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemlistview.cpp
Merge remote-tracking branch 'origin/KDE/4.11'
[dolphin.git] / src / kitemviews / kstandarditemlistview.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 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 "kstandarditemlistview.h"
21
22 #include <KDebug>
23 #include <KIconLoader>
24 #include "kstandarditemlistwidget.h"
25 #include "kstandarditemlistgroupheader.h"
26
27 KStandardItemListView::KStandardItemListView(QGraphicsWidget* parent) :
28 KItemListView(parent),
29 m_itemLayout(DetailsLayout)
30 {
31 setAcceptDrops(true);
32 setScrollOrientation(Qt::Vertical);
33 setVisibleRoles(QList<QByteArray>() << "text");
34 }
35
36 KStandardItemListView::~KStandardItemListView()
37 {
38 }
39
40 void KStandardItemListView::setItemLayout(ItemLayout layout)
41 {
42 if (m_itemLayout == layout) {
43 return;
44 }
45
46 beginTransaction();
47
48 const ItemLayout previous = m_itemLayout;
49 m_itemLayout = layout;
50
51 setSupportsItemExpanding(itemLayoutSupportsItemExpanding(layout));
52 setScrollOrientation(layout == CompactLayout ? Qt::Horizontal : Qt::Vertical);
53
54 onItemLayoutChanged(layout, previous);
55
56 endTransaction();
57 }
58
59 KStandardItemListView::ItemLayout KStandardItemListView::itemLayout() const
60 {
61 return m_itemLayout;
62 }
63
64 KItemListWidgetCreatorBase* KStandardItemListView::defaultWidgetCreator() const
65 {
66 return new KItemListWidgetCreator<KStandardItemListWidget>();
67 }
68
69 KItemListGroupHeaderCreatorBase* KStandardItemListView::defaultGroupHeaderCreator() const
70 {
71 return new KItemListGroupHeaderCreator<KStandardItemListGroupHeader>();
72 }
73
74 void KStandardItemListView::initializeItemListWidget(KItemListWidget* item)
75 {
76 KStandardItemListWidget* standardItemListWidget = qobject_cast<KStandardItemListWidget*>(item);
77 Q_ASSERT(standardItemListWidget);
78
79 switch (itemLayout()) {
80 case IconsLayout: standardItemListWidget->setLayout(KStandardItemListWidget::IconsLayout); break;
81 case CompactLayout: standardItemListWidget->setLayout(KStandardItemListWidget::CompactLayout); break;
82 case DetailsLayout: standardItemListWidget->setLayout(KStandardItemListWidget::DetailsLayout); break;
83 default: Q_ASSERT(false); break;
84 }
85
86 standardItemListWidget->setSupportsItemExpanding(supportsItemExpanding());
87 }
88
89
90 bool KStandardItemListView::itemSizeHintUpdateRequired(const QSet<QByteArray>& changedRoles) const
91 {
92 // The only thing that can modify the item's size hint is the amount of space
93 // needed to display the text for the visible roles.
94 // Even if the icons have a different size they are always aligned within
95 // the area defined by KItemStyleOption.iconSize and hence result in no
96 // change of the item-size.
97 foreach (const QByteArray& role, visibleRoles()) {
98 if (changedRoles.contains(role)) {
99 return true;
100 }
101 }
102 return false;
103 }
104
105 bool KStandardItemListView::itemLayoutSupportsItemExpanding(ItemLayout layout) const
106 {
107 return layout == DetailsLayout;
108 }
109
110 void KStandardItemListView::onItemLayoutChanged(ItemLayout current, ItemLayout previous)
111 {
112 Q_UNUSED(current);
113 Q_UNUSED(previous);
114 updateLayoutOfVisibleItems();
115 }
116
117 void KStandardItemListView::onScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
118 {
119 Q_UNUSED(current);
120 Q_UNUSED(previous);
121 updateLayoutOfVisibleItems();
122 }
123
124 void KStandardItemListView::onSupportsItemExpandingChanged(bool supportsExpanding)
125 {
126 Q_UNUSED(supportsExpanding);
127 updateLayoutOfVisibleItems();
128 }
129
130
131 void KStandardItemListView::polishEvent()
132 {
133 switch (m_itemLayout) {
134 case IconsLayout: applyDefaultStyleOption(style()->pixelMetric(QStyle::PM_LargeIconSize), 2, 4, 8); break;
135 case CompactLayout: applyDefaultStyleOption(style()->pixelMetric(QStyle::PM_SmallIconSize), 2, 8, 0); break;
136 case DetailsLayout: applyDefaultStyleOption(style()->pixelMetric(QStyle::PM_SmallIconSize), 2, 0, 0); break;
137 default: Q_ASSERT(false); break;
138 }
139
140 QGraphicsWidget::polishEvent();
141 }
142
143 void KStandardItemListView::applyDefaultStyleOption(int iconSize,
144 int padding,
145 int horizontalMargin,
146 int verticalMargin)
147 {
148 KItemListStyleOption option = styleOption();
149
150 bool changed = false;
151 if (option.iconSize < 0) {
152 option.iconSize = iconSize;
153 changed = true;
154 }
155 if (option.padding < 0) {
156 option.padding = padding;
157 changed = true;
158 }
159 if (option.horizontalMargin < 0) {
160 option.horizontalMargin = horizontalMargin;
161 changed = true;
162 }
163 if (option.verticalMargin < 0) {
164 option.verticalMargin = verticalMargin;
165 changed = true;
166 }
167
168 if (changed) {
169 setStyleOption(option);
170 }
171 }
172
173 void KStandardItemListView::updateLayoutOfVisibleItems()
174 {
175 if (model()) {
176 foreach (KItemListWidget* widget, visibleItemListWidgets()) {
177 initializeItemListWidget(widget);
178 }
179 }
180 }
181
182 #include "kstandarditemlistview.moc"