1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> *
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 "dolphincolumnview.h"
22 #include "dolphinmodel.h"
23 #include "dolphincolumnwidget.h"
24 #include "dolphincontroller.h"
25 #include "dolphindirlister.h"
26 #include "dolphinmodel.h"
27 #include "dolphinsortfilterproxymodel.h"
28 #include "dolphinsettings.h"
30 #include "dolphin_columnmodesettings.h"
32 #include <kcolorutils.h>
33 #include <kcolorscheme.h>
34 #include <kdirlister.h>
36 #include <QAbstractProxyModel>
37 #include <QApplication>
43 DolphinColumnView::DolphinColumnView(QWidget
* parent
, DolphinController
* controller
) :
44 QAbstractItemView(parent
),
45 m_controller(controller
),
46 m_restoreActiveColumnFocus(false),
54 Q_ASSERT(controller
!= 0);
57 setDragDropMode(QAbstractItemView::DragDrop
);
58 setDropIndicatorShown(false);
59 setSelectionMode(ExtendedSelection
);
61 connect(this, SIGNAL(entered(const QModelIndex
&)),
62 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
63 connect(this, SIGNAL(viewportEntered()),
64 controller
, SLOT(emitViewportEntered()));
65 connect(controller
, SIGNAL(zoomIn()),
66 this, SLOT(zoomIn()));
67 connect(controller
, SIGNAL(zoomOut()),
68 this, SLOT(zoomOut()));
69 connect(controller
, SIGNAL(urlChanged(const KUrl
&)),
70 this, SLOT(showColumn(const KUrl
&)));
71 connect(controller
, SIGNAL(showHiddenFilesChanged(bool)),
72 this, SLOT(slotShowHiddenFilesChanged(bool)));
73 connect(controller
, SIGNAL(showPreviewChanged(bool)),
74 this, SLOT(slotShowPreviewChanged(bool)));
76 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
77 this, SLOT(moveContentHorizontally(int)));
79 m_animation
= new QTimeLine(500, this);
80 connect(m_animation
, SIGNAL(frameChanged(int)), horizontalScrollBar(), SLOT(setValue(int)));
82 DolphinColumnWidget
* column
= new DolphinColumnWidget(viewport(), this, m_controller
->url());
83 m_columns
.append(column
);
84 setActiveColumnIndex(0);
86 updateDecorationSize();
88 // dim the background of the viewport
89 QColor bgColor
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
90 const QColor fgColor
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
91 bgColor
= KColorUtils::mix(bgColor
, fgColor
, 0.04);
93 QPalette palette
= viewport()->palette();
94 palette
.setColor(viewport()->backgroundRole(), bgColor
);
95 viewport()->setPalette(palette
);
98 DolphinColumnView::~DolphinColumnView()
102 QModelIndex
DolphinColumnView::indexAt(const QPoint
& point
) const
104 foreach (DolphinColumnWidget
* column
, m_columns
) {
105 const QPoint topLeft
= column
->frameGeometry().topLeft();
106 const QPoint
adjustedPoint(point
.x() - topLeft
.x(), point
.y() - topLeft
.y());
107 const QModelIndex index
= column
->indexAt(adjustedPoint
);
108 if (index
.isValid()) {
113 return QModelIndex();
116 void DolphinColumnView::scrollTo(const QModelIndex
& index
, ScrollHint hint
)
118 activeColumn()->scrollTo(index
, hint
);
121 QRect
DolphinColumnView::visualRect(const QModelIndex
& index
) const
123 return activeColumn()->visualRect(index
);
126 void DolphinColumnView::setModel(QAbstractItemModel
* model
)
128 m_proxyModel
= static_cast<QAbstractProxyModel
*>(model
);
129 m_dolphinModel
= static_cast<DolphinModel
*>(m_proxyModel
->sourceModel());
130 QAbstractItemView::setModel(model
);
133 void DolphinColumnView::invertSelection()
135 QItemSelectionModel
* selectionModel
= activeColumn()->selectionModel();
136 const QAbstractItemModel
* itemModel
= selectionModel
->model();
138 const QModelIndex topLeft
= itemModel
->index(0, 0);
139 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
140 itemModel
->columnCount() - 1);
142 const QItemSelection
selection(topLeft
, bottomRight
);
143 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
146 void DolphinColumnView::reload()
148 foreach (DolphinColumnWidget
* column
, m_columns
) {
153 void DolphinColumnView::showColumn(const KUrl
& url
)
155 const KUrl
& rootUrl
= m_columns
[0]->url();
156 if (!rootUrl
.isParentOf(url
)) {
157 // the URL is no child URL of the column view, hence clear all columns
158 // and reset the root column
159 QList
<DolphinColumnWidget
*>::iterator start
= m_columns
.begin() + 1;
160 QList
<DolphinColumnWidget
*>::iterator end
= m_columns
.end();
161 for (QList
<DolphinColumnWidget
*>::iterator it
= start
; it
!= end
; ++it
) {
162 (*it
)->deleteLater();
164 m_columns
.erase(start
, end
);
166 m_columns
[0]->setActive(true);
167 m_columns
[0]->setUrl(url
);
168 assureVisibleActiveColumn();
172 KDirLister
* dirLister
= m_dolphinModel
->dirLister();
173 const KUrl dirListerUrl
= dirLister
->url();
174 if (dirListerUrl
!= rootUrl
) {
175 // It is possible that root URL of the directory lister is adjusted
176 // after creating the column widget (e. g. when restoring the history
177 // having a different root URL than the controller indicates).
178 m_columns
[0]->setUrl(dirListerUrl
);
182 foreach (DolphinColumnWidget
* column
, m_columns
) {
183 if (column
->url() == url
) {
184 // the column represents already the requested URL, hence activate it
185 requestActivation(column
);
187 } else if (!column
->url().isParentOf(url
)) {
188 // the column is no parent of the requested URL, hence
189 // just delete all remaining columns
190 if (columnIndex
> 0) {
191 QList
<DolphinColumnWidget
*>::iterator start
= m_columns
.begin() + columnIndex
;
192 QList
<DolphinColumnWidget
*>::iterator end
= m_columns
.end();
193 for (QList
<DolphinColumnWidget
*>::iterator it
= start
; it
!= end
; ++it
) {
194 (*it
)->deleteLater();
196 m_columns
.erase(start
, end
);
198 const int maxIndex
= m_columns
.count() - 1;
199 Q_ASSERT(maxIndex
>= 0);
200 if (m_index
> maxIndex
) {
209 // Create missing columns. Assuming that the path is "/home/peter/Temp/" and
210 // the target path is "/home/peter/Temp/a/b/c/", then the columns "a", "b" and
211 // "c" will be created.
212 const int lastIndex
= m_columns
.count() - 1;
213 Q_ASSERT(lastIndex
>= 0);
215 const KUrl
& activeUrl
= m_columns
[lastIndex
]->url();
216 Q_ASSERT(activeUrl
.isParentOf(url
));
217 Q_ASSERT(activeUrl
!= url
);
219 QString path
= activeUrl
.url(KUrl::AddTrailingSlash
);
220 const QString targetPath
= url
.url(KUrl::AddTrailingSlash
);
222 columnIndex
= lastIndex
;
223 int slashIndex
= path
.count('/');
224 bool hasSubPath
= (slashIndex
>= 0);
226 const QString subPath
= targetPath
.section('/', slashIndex
, slashIndex
);
227 if (subPath
.isEmpty()) {
230 path
+= subPath
+ '/';
233 const KUrl childUrl
= KUrl(path
);
234 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(KUrl(path
));
235 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
237 m_columns
[columnIndex
]->setChildUrl(childUrl
);
240 DolphinColumnWidget
* column
= new DolphinColumnWidget(viewport(), this, childUrl
);
241 column
->setActive(false);
243 m_columns
.append(column
);
245 // Before invoking layoutColumns() the column must be set visible temporary.
246 // To prevent a flickering the initial geometry is set to a hidden position.
247 column
->setGeometry(QRect(-1, -1, 1, 1));
252 // the layout is finished, now let the column be invisible until it
253 // gets a valid root index due to expandToActiveUrl()
258 // set the last column as active column without modifying the controller
259 // and hence the history
260 activeColumn()->setActive(false);
261 m_index
= columnIndex
;
262 activeColumn()->setActive(true);
263 assureVisibleActiveColumn();
266 void DolphinColumnView::selectAll()
268 activeColumn()->selectAll();
271 bool DolphinColumnView::isIndexHidden(const QModelIndex
& index
) const
274 return false;//activeColumn()->isIndexHidden(index);
277 QModelIndex
DolphinColumnView::moveCursor(CursorAction cursorAction
, Qt::KeyboardModifiers modifiers
)
279 // Parts of this code have been taken from QColumnView::moveCursor().
280 // Copyright (C) 1992-2007 Trolltech ASA.
284 return QModelIndex();
287 const QModelIndex current
= currentIndex();
288 if (isRightToLeft()) {
289 if (cursorAction
== MoveLeft
) {
290 cursorAction
= MoveRight
;
291 } else if (cursorAction
== MoveRight
) {
292 cursorAction
= MoveLeft
;
296 switch (cursorAction
) {
299 setActiveColumnIndex(m_index
- 1);
304 if (m_index
< m_columns
.count() - 1) {
305 setActiveColumnIndex(m_index
+ 1);
313 return QModelIndex();
316 void DolphinColumnView::setSelection(const QRect
& rect
, QItemSelectionModel::SelectionFlags flags
)
320 //activeColumn()->setSelection(rect, flags);
323 QRegion
DolphinColumnView::visualRegionForSelection(const QItemSelection
& selection
) const
326 return QRegion(); //activeColumn()->visualRegionForSelection(selection);
329 int DolphinColumnView::horizontalOffset() const
334 int DolphinColumnView::verticalOffset() const
339 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
341 m_controller
->triggerActivation();
342 QAbstractItemView::mousePressEvent(event
);
345 void DolphinColumnView::resizeEvent(QResizeEvent
* event
)
347 QAbstractItemView::resizeEvent(event
);
352 void DolphinColumnView::zoomIn()
354 if (isZoomInPossible()) {
355 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
356 switch (settings
->iconSize()) {
357 case KIconLoader::SizeSmall
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
358 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeLarge
); break;
359 default: Q_ASSERT(false); break;
361 updateDecorationSize();
365 void DolphinColumnView::zoomOut()
367 if (isZoomOutPossible()) {
368 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
369 switch (settings
->iconSize()) {
370 case KIconLoader::SizeLarge
: settings
->setIconSize(KIconLoader::SizeMedium
); break;
371 case KIconLoader::SizeMedium
: settings
->setIconSize(KIconLoader::SizeSmall
); break;
372 default: Q_ASSERT(false); break;
374 updateDecorationSize();
378 void DolphinColumnView::moveContentHorizontally(int x
)
380 m_contentX
= isRightToLeft() ? +x
: -x
;
384 void DolphinColumnView::updateDecorationSize()
386 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
387 const int iconSize
= settings
->iconSize();
389 foreach (QObject
* object
, viewport()->children()) {
390 if (object
->inherits("QListView")) {
391 DolphinColumnWidget
* widget
= static_cast<DolphinColumnWidget
*>(object
);
392 widget
->setDecorationSize(QSize(iconSize
, iconSize
));
396 m_controller
->setZoomInPossible(isZoomInPossible());
397 m_controller
->setZoomOutPossible(isZoomOutPossible());
402 void DolphinColumnView::slotShowHiddenFilesChanged(bool show
)
404 foreach (DolphinColumnWidget
* column
, m_columns
) {
405 column
->setShowHiddenFiles(show
);
409 void DolphinColumnView::slotShowPreviewChanged(bool show
)
411 foreach (DolphinColumnWidget
* column
, m_columns
) {
412 column
->setShowPreview(show
);
416 bool DolphinColumnView::isZoomInPossible() const
418 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
419 return settings
->iconSize() < KIconLoader::SizeLarge
;
422 bool DolphinColumnView::isZoomOutPossible() const
424 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
425 return settings
->iconSize() > KIconLoader::SizeSmall
;
428 void DolphinColumnView::setActiveColumnIndex(int index
)
430 if (m_index
== index
) {
434 const bool hasActiveColumn
= (m_index
>= 0);
435 if (hasActiveColumn
) {
436 m_columns
[m_index
]->setActive(false);
440 m_columns
[m_index
]->setActive(true);
442 m_controller
->setUrl(m_columns
[m_index
]->url());
444 assureVisibleActiveColumn();
447 void DolphinColumnView::layoutColumns()
449 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
450 const int columnWidth
= settings
->columnWidth();
451 if (isRightToLeft()) {
452 int x
= viewport()->width() - columnWidth
+ m_contentX
;
453 foreach (DolphinColumnWidget
* column
, m_columns
) {
454 column
->setGeometry(QRect(x
, 0, columnWidth
, viewport()->height()));
459 foreach (DolphinColumnWidget
* column
, m_columns
) {
460 column
->setGeometry(QRect(x
, 0, columnWidth
, viewport()->height()));
466 void DolphinColumnView::updateScrollBar()
468 int contentWidth
= 0;
469 foreach (DolphinColumnWidget
* column
, m_columns
) {
470 contentWidth
+= column
->width();
473 horizontalScrollBar()->setPageStep(contentWidth
);
474 horizontalScrollBar()->setRange(0, contentWidth
- viewport()->width());
477 void DolphinColumnView::assureVisibleActiveColumn()
479 const int viewportWidth
= viewport()->width();
480 const int x
= activeColumn()->x();
481 const int width
= activeColumn()->width();
482 if (x
+ width
> viewportWidth
) {
483 const int newContentX
= m_contentX
- x
- width
+ viewportWidth
;
484 if (isRightToLeft()) {
485 m_animation
->setFrameRange(m_contentX
, newContentX
);
487 m_animation
->setFrameRange(-m_contentX
, -newContentX
);
489 m_animation
->start();
491 const int newContentX
= m_contentX
- x
;
492 if (isRightToLeft()) {
493 m_animation
->setFrameRange(m_contentX
, newContentX
);
495 m_animation
->setFrameRange(-m_contentX
, -newContentX
);
497 m_animation
->start();
501 void DolphinColumnView::requestActivation(DolphinColumnWidget
* column
)
503 if (column
->isActive()) {
504 assureVisibleActiveColumn();
507 foreach (DolphinColumnWidget
* currColumn
, m_columns
) {
508 if (currColumn
== column
) {
509 setActiveColumnIndex(index
);
517 #include "dolphincolumnview.moc"