]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnviewcontainer.cpp
move the DolphinFileItemDelegate creation into ViewExtensionsFactory
[dolphin.git] / src / dolphincolumnviewcontainer.cpp
1 /***************************************************************************
2 * Copyright (C) 2007-2009 by Peter Penz <peter.penz@gmx.at> *
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 "dolphincolumnviewcontainer.h"
21
22 #include "dolphincolumnview.h"
23 #include "dolphincontroller.h"
24 #include "dolphinsortfilterproxymodel.h"
25 #include "settings/dolphinsettings.h"
26
27 #include "dolphin_columnmodesettings.h"
28
29 #include <QPoint>
30 #include <QScrollBar>
31 #include <QTimeLine>
32
33 DolphinColumnViewContainer::DolphinColumnViewContainer(QWidget* parent,
34 DolphinController* controller) :
35 QScrollArea(parent),
36 m_controller(controller),
37 m_active(false),
38 m_index(-1),
39 m_contentX(0),
40 m_columns(),
41 m_emptyViewport(0),
42 m_animation(0)
43 {
44 Q_ASSERT(controller != 0);
45
46 setAcceptDrops(true);
47 setFocusPolicy(Qt::NoFocus);
48 setFrameShape(QFrame::NoFrame);
49 setLayoutDirection(Qt::LeftToRight);
50
51 connect(this, SIGNAL(viewportEntered()),
52 controller, SLOT(emitViewportEntered()));
53 connect(controller, SIGNAL(activationChanged(bool)),
54 this, SLOT(updateColumnsBackground(bool)));
55
56 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
57 this, SLOT(moveContentHorizontally(int)));
58
59 m_animation = new QTimeLine(500, this);
60 connect(m_animation, SIGNAL(frameChanged(int)), horizontalScrollBar(), SLOT(setValue(int)));
61
62 DolphinColumnView* column = new DolphinColumnView(viewport(), this, m_controller->url());
63 m_columns.append(column);
64 setActiveColumnIndex(0);
65
66 m_emptyViewport = new QFrame(viewport());
67 m_emptyViewport->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
68
69 updateColumnsBackground(true);
70 }
71
72 DolphinColumnViewContainer::~DolphinColumnViewContainer()
73 {
74 }
75
76 KUrl DolphinColumnViewContainer::rootUrl() const
77 {
78 return m_columns[0]->url();
79 }
80
81 QAbstractItemView* DolphinColumnViewContainer::activeColumn() const
82 {
83 return m_columns[m_index];
84 }
85
86 bool DolphinColumnViewContainer::showColumn(const KUrl& url)
87 {
88 if (!rootUrl().isParentOf(url)) {
89 removeAllColumns();
90 m_columns[0]->setUrl(url);
91 return false;
92 }
93
94 int columnIndex = 0;
95 foreach (DolphinColumnView* column, m_columns) {
96 if (column->url() == url) {
97 // the column represents already the requested URL, hence activate it
98 requestActivation(column);
99 layoutColumns();
100 return false;
101 } else if (!column->url().isParentOf(url)) {
102 // the column is no parent of the requested URL, hence
103 // just delete all remaining columns
104 if (columnIndex > 0) {
105 QList<DolphinColumnView*>::iterator start = m_columns.begin() + columnIndex;
106 QList<DolphinColumnView*>::iterator end = m_columns.end();
107 for (QList<DolphinColumnView*>::iterator it = start; it != end; ++it) {
108 deleteColumn(*it);
109 }
110 m_columns.erase(start, end);
111
112 const int maxIndex = m_columns.count() - 1;
113 Q_ASSERT(maxIndex >= 0);
114 if (m_index > maxIndex) {
115 m_index = maxIndex;
116 }
117 break;
118 }
119 }
120 ++columnIndex;
121 }
122
123 // Create missing columns. Assuming that the path is "/home/peter/Temp/" and
124 // the target path is "/home/peter/Temp/a/b/c/", then the columns "a", "b" and
125 // "c" will be created.
126 const int lastIndex = m_columns.count() - 1;
127 Q_ASSERT(lastIndex >= 0);
128
129 const KUrl& activeUrl = m_columns[lastIndex]->url();
130 Q_ASSERT(activeUrl.isParentOf(url));
131 Q_ASSERT(activeUrl != url);
132
133 QString path = activeUrl.url(KUrl::AddTrailingSlash);
134 const QString targetPath = url.url(KUrl::AddTrailingSlash);
135
136 columnIndex = lastIndex;
137 int slashIndex = path.count('/');
138 bool hasSubPath = (slashIndex >= 0);
139 while (hasSubPath) {
140 const QString subPath = targetPath.section('/', slashIndex, slashIndex);
141 if (subPath.isEmpty()) {
142 hasSubPath = false;
143 } else {
144 path += subPath + '/';
145 ++slashIndex;
146
147 const KUrl childUrl = KUrl(path);
148 m_columns[columnIndex]->setChildUrl(childUrl);
149 columnIndex++;
150
151 DolphinColumnView* column = new DolphinColumnView(viewport(), this, childUrl);
152 column->setActive(false);
153
154 m_columns.append(column);
155
156 // Before invoking layoutColumns() the column must be set visible temporary.
157 // To prevent a flickering the initial geometry is set to a hidden position.
158 column->setGeometry(QRect(-1, -1, 1, 1));
159 column->show();
160 layoutColumns();
161 updateScrollBar();
162 }
163 }
164
165 // set the last column as active column without modifying the controller
166 // and hence the history
167 m_columns[m_index]->setActive(false);
168 m_index = columnIndex;
169 m_columns[m_index]->setActive(true);
170 assureVisibleActiveColumn();
171
172 return true;
173 }
174
175 void DolphinColumnViewContainer::mousePressEvent(QMouseEvent* event)
176 {
177 m_controller->requestActivation();
178 QScrollArea::mousePressEvent(event);
179 }
180
181 void DolphinColumnViewContainer::resizeEvent(QResizeEvent* event)
182 {
183 QScrollArea::resizeEvent(event);
184 layoutColumns();
185 updateScrollBar();
186 assureVisibleActiveColumn();
187 }
188
189 void DolphinColumnViewContainer::wheelEvent(QWheelEvent* event)
190 {
191 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
192 if ((event->modifiers() & Qt::ControlModifier) == Qt::ControlModifier) {
193 event->ignore();
194 } else {
195 QScrollArea::wheelEvent(event);
196 }
197 }
198
199 void DolphinColumnViewContainer::moveContentHorizontally(int x)
200 {
201 m_contentX = isRightToLeft() ? +x : -x;
202 layoutColumns();
203 }
204
205 void DolphinColumnViewContainer::updateColumnsBackground(bool active)
206 {
207 if (active == m_active) {
208 return;
209 }
210
211 m_active = active;
212
213 // dim the background of the viewport
214 const QPalette::ColorRole role = viewport()->backgroundRole();
215 QColor background = viewport()->palette().color(role);
216 background.setAlpha(0); // make background transparent
217
218 QPalette palette = viewport()->palette();
219 palette.setColor(role, background);
220 viewport()->setPalette(palette);
221
222 foreach (DolphinColumnView* column, m_columns) {
223 column->updateBackground();
224 }
225 }
226
227 void DolphinColumnViewContainer::setActiveColumnIndex(int index)
228 {
229 if (m_index == index) {
230 return;
231 }
232
233 const bool hasActiveColumn = (m_index >= 0);
234 if (hasActiveColumn) {
235 m_columns[m_index]->setActive(false);
236 }
237
238 m_index = index;
239 m_columns[m_index]->setActive(true);
240
241 assureVisibleActiveColumn();
242 }
243
244 void DolphinColumnViewContainer::layoutColumns()
245 {
246 const int gap = 4;
247
248 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
249 const int columnWidth = settings->columnWidth();
250
251 QRect emptyViewportRect;
252 if (isRightToLeft()) {
253 int x = viewport()->width() - columnWidth + m_contentX;
254 foreach (DolphinColumnView* column, m_columns) {
255 column->setGeometry(QRect(x, 0, columnWidth - gap, viewport()->height()));
256 x -= columnWidth;
257 }
258 emptyViewportRect = QRect(0, 0, x + columnWidth - gap, viewport()->height());
259 } else {
260 int x = m_contentX;
261 foreach (DolphinColumnView* column, m_columns) {
262 column->setGeometry(QRect(x, 0, columnWidth - gap, viewport()->height()));
263 x += columnWidth;
264 }
265 emptyViewportRect = QRect(x, 0, viewport()->width() - x - gap, viewport()->height());
266 }
267
268 if (emptyViewportRect.isValid()) {
269 m_emptyViewport->show();
270 m_emptyViewport->setGeometry(emptyViewportRect);
271 } else {
272 m_emptyViewport->hide();
273 }
274 }
275
276 void DolphinColumnViewContainer::updateScrollBar()
277 {
278 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
279 const int contentWidth = m_columns.count() * settings->columnWidth();
280
281 horizontalScrollBar()->setPageStep(contentWidth);
282 horizontalScrollBar()->setRange(0, contentWidth - viewport()->width());
283 }
284
285 void DolphinColumnViewContainer::assureVisibleActiveColumn()
286 {
287 const int viewportWidth = viewport()->width();
288 const int x = activeColumn()->x();
289
290 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
291 const int width = settings->columnWidth();
292
293 if (x + width > viewportWidth) {
294 const int newContentX = m_contentX - x - width + viewportWidth;
295 if (isRightToLeft()) {
296 m_animation->setFrameRange(m_contentX, newContentX);
297 } else {
298 m_animation->setFrameRange(-m_contentX, -newContentX);
299 }
300 if (m_animation->state() != QTimeLine::Running) {
301 m_animation->start();
302 }
303 } else if (x < 0) {
304 const int newContentX = m_contentX - x;
305 if (isRightToLeft()) {
306 m_animation->setFrameRange(m_contentX, newContentX);
307 } else {
308 m_animation->setFrameRange(-m_contentX, -newContentX);
309 }
310 if (m_animation->state() != QTimeLine::Running) {
311 m_animation->start();
312 }
313 }
314 }
315
316 void DolphinColumnViewContainer::requestActivation(DolphinColumnView* column)
317 {
318 m_controller->setItemView(column);
319 if (column->isActive()) {
320 assureVisibleActiveColumn();
321 } else {
322 int index = 0;
323 foreach (DolphinColumnView* currColumn, m_columns) {
324 if (currColumn == column) {
325 setActiveColumnIndex(index);
326 return;
327 }
328 ++index;
329 }
330 }
331 }
332
333 void DolphinColumnViewContainer::removeAllColumns()
334 {
335 QList<DolphinColumnView*>::iterator start = m_columns.begin() + 1;
336 QList<DolphinColumnView*>::iterator end = m_columns.end();
337 for (QList<DolphinColumnView*>::iterator it = start; it != end; ++it) {
338 deleteColumn(*it);
339 }
340 m_columns.erase(start, end);
341 m_index = 0;
342 m_columns[0]->setActive(true);
343 assureVisibleActiveColumn();
344 }
345
346 QPoint DolphinColumnViewContainer::columnPosition(DolphinColumnView* column, const QPoint& point) const
347 {
348 const QPoint topLeft = column->frameGeometry().topLeft();
349 return QPoint(point.x() - topLeft.x(), point.y() - topLeft.y());
350 }
351
352 void DolphinColumnViewContainer::deleteColumn(DolphinColumnView* column)
353 {
354 if (column != 0) {
355 if (m_controller->itemView() == column) {
356 m_controller->setItemView(0);
357 }
358 // deleteWhenNotDragSource(column) does not necessarily delete column,
359 // and we want its preview generator destroyed immediately.
360 column->hide();
361 // Prevent automatic destruction of column when this DolphinColumnViewContainer
362 // is destroyed.
363 column->setParent(0);
364 column->disconnect();
365 emit requestColumnDeletion(column);
366 }
367 }
368
369 #include "dolphincolumnviewcontainer.moc"