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