]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemlistview.cpp
Prepare view-engine for non-KFileItem usecase
[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
26 KStandardItemListView::KStandardItemListView(QGraphicsWidget* parent) :
27 KItemListView(parent),
28 m_itemLayout(DetailsLayout)
29 {
30 setAcceptDrops(true);
31 setScrollOrientation(Qt::Vertical);
32 setVisibleRoles(QList<QByteArray>() << "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 switch (layout) {
51 case IconsLayout:
52 setScrollOrientation(Qt::Vertical);
53 setSupportsItemExpanding(false);
54 break;
55 case DetailsLayout:
56 setScrollOrientation(Qt::Vertical);
57 setSupportsItemExpanding(true);
58 break;
59 case CompactLayout:
60 setScrollOrientation(Qt::Horizontal);
61 setSupportsItemExpanding(false);
62 break;
63 default:
64 Q_ASSERT(false);
65 break;
66 }
67
68 onItemLayoutChanged(layout, previous);
69
70 endTransaction();
71 }
72
73 KStandardItemListView::ItemLayout KStandardItemListView::itemLayout() const
74 {
75 return m_itemLayout;
76 }
77
78 KItemListWidgetCreatorBase* KStandardItemListView::defaultWidgetCreator() const
79 {
80 return new KItemListWidgetCreator<KStandardItemListWidget>();
81 }
82
83 KItemListGroupHeaderCreatorBase* KStandardItemListView::defaultGroupHeaderCreator() const
84 {
85 return 0; // TODO: new KItemListGroupHeaderCreator<KStandardItemListGroupHeader>()
86 }
87
88 void KStandardItemListView::initializeItemListWidget(KItemListWidget* item)
89 {
90 KStandardItemListWidget* standardItemListWidget = qobject_cast<KStandardItemListWidget*>(item);
91 Q_ASSERT(standardItemListWidget);
92
93 switch (itemLayout()) {
94 case IconsLayout: standardItemListWidget->setLayout(KStandardItemListWidget::IconsLayout); break;
95 case CompactLayout: standardItemListWidget->setLayout(KStandardItemListWidget::CompactLayout); break;
96 case DetailsLayout: standardItemListWidget->setLayout(KStandardItemListWidget::DetailsLayout); break;
97 default: Q_ASSERT(false); break;
98 }
99
100 standardItemListWidget->setSupportsItemExpanding(supportsItemExpanding());
101 }
102
103
104 bool KStandardItemListView::itemSizeHintUpdateRequired(const QSet<QByteArray>& changedRoles) const
105 {
106 // Even if the icons have a different size they are always aligned within
107 // the area defined by KItemStyleOption.iconSize and hence result in no
108 // change of the item-size.
109 const bool containsIconName = changedRoles.contains("iconName");
110 const bool containsIconPixmap = changedRoles.contains("iconPixmap");
111 const int count = changedRoles.count();
112
113 const bool iconChanged = (containsIconName && containsIconPixmap && count == 2) ||
114 (containsIconName && count == 1) ||
115 (containsIconPixmap && count == 1);
116 return !iconChanged;
117 }
118
119 void KStandardItemListView::onItemLayoutChanged(ItemLayout current, ItemLayout previous)
120 {
121 Q_UNUSED(current);
122 Q_UNUSED(previous);
123 updateLayoutOfVisibleItems();
124 }
125
126 void KStandardItemListView::onScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
127 {
128 Q_UNUSED(current);
129 Q_UNUSED(previous);
130 updateLayoutOfVisibleItems();
131 }
132
133 void KStandardItemListView::onSupportsItemExpandingChanged(bool supportsExpanding)
134 {
135 Q_UNUSED(supportsExpanding);
136 updateLayoutOfVisibleItems();
137 }
138
139
140 void KStandardItemListView::polishEvent()
141 {
142 switch (m_itemLayout) {
143 case IconsLayout: applyDefaultStyleOption(KIconLoader::SizeMedium, 2, 4, 8); break;
144 case CompactLayout: applyDefaultStyleOption(KIconLoader::SizeSmall, 2, 8, 0); break;
145 case DetailsLayout: applyDefaultStyleOption(KIconLoader::SizeSmall, 2, 0, 0); break;
146 default: Q_ASSERT(false); break;
147 }
148
149 QGraphicsWidget::polishEvent();
150 }
151
152 void KStandardItemListView::applyDefaultStyleOption(int iconSize,
153 int padding,
154 int horizontalMargin,
155 int verticalMargin)
156 {
157 KItemListStyleOption option = styleOption();
158
159 bool changed = false;
160 if (option.iconSize < 0) {
161 option.iconSize = iconSize;
162 changed = true;
163 }
164 if (option.padding < 0) {
165 option.padding = padding;
166 changed = true;
167 }
168 if (option.horizontalMargin < 0) {
169 option.horizontalMargin = horizontalMargin;
170 changed = true;
171 }
172 if (option.verticalMargin < 0) {
173 option.verticalMargin = verticalMargin;
174 changed = true;
175 }
176
177 if (changed) {
178 setStyleOption(option);
179 }
180 }
181
182 void KStandardItemListView::updateLayoutOfVisibleItems()
183 {
184 if (model()) {
185 foreach (KItemListWidget* widget, visibleItemListWidgets()) {
186 initializeItemListWidget(widget);
187 }
188 }
189 }
190
191 #include "kstandarditemlistview.moc"