]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnviewcontainer.cpp
1 /***************************************************************************
2 * Copyright (C) 2007-2009 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 "dolphincolumnviewcontainer.h"
22 #include "dolphincolumnview.h"
23 #include "dolphincontroller.h"
24 #include "dolphinsortfilterproxymodel.h"
25 #include "draganddrophelper.h"
26 #include "settings/dolphinsettings.h"
28 #include "dolphin_columnmodesettings.h"
35 DolphinColumnViewContainer::DolphinColumnViewContainer(QWidget
* parent
,
36 DolphinController
* controller
) :
38 m_controller(controller
),
48 Q_ASSERT(controller
!= 0);
51 setFocusPolicy(Qt::NoFocus
);
52 setFrameShape(QFrame::NoFrame
);
53 setLayoutDirection(Qt::LeftToRight
);
55 connect(controller
, SIGNAL(activationChanged(bool)),
56 this, SLOT(updateColumnsBackground(bool)));
58 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
59 this, SLOT(moveContentHorizontally(int)));
61 m_animation
= new QTimeLine(500, this);
62 connect(m_animation
, SIGNAL(frameChanged(int)), horizontalScrollBar(), SLOT(setValue(int)));
64 m_activeUrlTimer
= new QTimer(this);
65 m_activeUrlTimer
->setSingleShot(true);
66 m_activeUrlTimer
->setInterval(200);
67 connect(m_activeUrlTimer
, SIGNAL(timeout()),
68 this, SLOT(updateActiveUrl()));
70 DolphinColumnView
* column
= new DolphinColumnView(viewport(), this, m_controller
->url());
71 m_columns
.append(column
);
72 setActiveColumnIndex(0);
74 m_emptyViewport
= new QFrame(viewport());
75 m_emptyViewport
->setFrameStyle(QFrame::StyledPanel
| QFrame::Sunken
);
77 updateColumnsBackground(true);
81 DolphinColumnViewContainer::~DolphinColumnViewContainer()
87 KUrl
DolphinColumnViewContainer::rootUrl() const
89 return m_columns
[0]->url();
92 QAbstractItemView
* DolphinColumnViewContainer::activeColumn() const
94 return m_columns
[m_index
];
97 void DolphinColumnViewContainer::showColumn(const KUrl
& url
)
99 if (!rootUrl().isParentOf(url
)) {
101 m_columns
[0]->setUrl(url
);
106 foreach (DolphinColumnView
* column
, m_columns
) {
107 if (column
->url() == url
) {
108 // the column represents already the requested URL, hence activate it
109 requestActivation(column
);
112 } else if (!column
->url().isParentOf(url
)) {
113 // the column is no parent of the requested URL, hence
114 // just delete all remaining columns
115 if (columnIndex
> 0) {
116 QList
<DolphinColumnView
*>::iterator start
= m_columns
.begin() + columnIndex
;
117 QList
<DolphinColumnView
*>::iterator end
= m_columns
.end();
118 for (QList
<DolphinColumnView
*>::iterator it
= start
; it
!= end
; ++it
) {
121 m_columns
.erase(start
, end
);
123 const int maxIndex
= m_columns
.count() - 1;
124 Q_ASSERT(maxIndex
>= 0);
125 if (m_index
> maxIndex
) {
134 // Create missing columns. Assuming that the path is "/home/peter/Temp/" and
135 // the target path is "/home/peter/Temp/a/b/c/", then the columns "a", "b" and
136 // "c" will be created.
137 const int lastIndex
= m_columns
.count() - 1;
138 Q_ASSERT(lastIndex
>= 0);
140 const KUrl
& activeUrl
= m_columns
[lastIndex
]->url();
141 Q_ASSERT(activeUrl
.isParentOf(url
));
142 Q_ASSERT(activeUrl
!= url
);
144 QString path
= activeUrl
.url(KUrl::AddTrailingSlash
);
145 const QString targetPath
= url
.url(KUrl::AddTrailingSlash
);
147 columnIndex
= lastIndex
;
148 int slashIndex
= path
.count('/');
149 bool hasSubPath
= (slashIndex
>= 0);
151 const QString subPath
= targetPath
.section('/', slashIndex
, slashIndex
);
152 if (subPath
.isEmpty()) {
155 path
+= subPath
+ '/';
158 const KUrl childUrl
= KUrl(path
);
159 m_columns
[columnIndex
]->setChildUrl(childUrl
);
162 DolphinColumnView
* column
= new DolphinColumnView(viewport(), this, childUrl
);
163 column
->setActive(false);
165 m_columns
.append(column
);
167 // Before invoking layoutColumns() the column must be set visible temporary.
168 // To prevent a flickering the initial geometry is set to a hidden position.
169 column
->setGeometry(QRect(-1, -1, 1, 1));
176 // set the last column as active column without modifying the controller
177 // and hence the history
178 m_columns
[m_index
]->setActive(false);
179 m_index
= columnIndex
;
180 m_columns
[m_index
]->setActive(true);
181 assureVisibleActiveColumn();
184 void DolphinColumnViewContainer::mousePressEvent(QMouseEvent
* event
)
186 m_controller
->requestActivation();
187 QScrollArea::mousePressEvent(event
);
190 void DolphinColumnViewContainer::keyPressEvent(QKeyEvent
* event
)
192 if (event
->key() == Qt::Key_Left
) {
193 setActiveColumnIndex(m_index
- 1);
195 QScrollArea::keyPressEvent(event
);
199 void DolphinColumnViewContainer::resizeEvent(QResizeEvent
* event
)
201 QScrollArea::resizeEvent(event
);
204 assureVisibleActiveColumn();
207 void DolphinColumnViewContainer::wheelEvent(QWheelEvent
* event
)
209 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
210 if ((event
->modifiers() & Qt::ControlModifier
) == Qt::ControlModifier
) {
213 QScrollArea::wheelEvent(event
);
217 void DolphinColumnViewContainer::moveContentHorizontally(int x
)
219 m_contentX
= isRightToLeft() ? +x
: -x
;
223 void DolphinColumnViewContainer::updateColumnsBackground(bool active
)
225 if (active
== m_active
) {
231 // dim the background of the viewport
232 const QPalette::ColorRole role
= viewport()->backgroundRole();
233 QColor background
= viewport()->palette().color(role
);
234 background
.setAlpha(0); // make background transparent
236 QPalette palette
= viewport()->palette();
237 palette
.setColor(role
, background
);
238 viewport()->setPalette(palette
);
240 foreach (DolphinColumnView
* column
, m_columns
) {
241 column
->updateBackground();
245 void DolphinColumnViewContainer::updateActiveUrl()
247 const KUrl activeUrl
= m_columns
[m_index
]->url();
248 m_controller
->setUrl(activeUrl
);
251 void DolphinColumnViewContainer::setActiveColumnIndex(int index
)
253 if ((m_index
== index
) || (index
< 0) || (index
>= m_columns
.count())) {
257 const bool hasActiveColumn
= (m_index
>= 0);
258 if (hasActiveColumn
) {
259 m_columns
[m_index
]->setActive(false);
263 m_columns
[m_index
]->setActive(true);
265 assureVisibleActiveColumn();
266 m_activeUrlTimer
->start(); // calls slot updateActiveUrl()
269 void DolphinColumnViewContainer::layoutColumns()
273 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
274 const int columnWidth
= settings
->columnWidth();
276 QRect emptyViewportRect
;
277 if (isRightToLeft()) {
278 int x
= viewport()->width() - columnWidth
+ m_contentX
;
279 foreach (DolphinColumnView
* column
, m_columns
) {
280 column
->setGeometry(QRect(x
, 0, columnWidth
- gap
, viewport()->height()));
283 emptyViewportRect
= QRect(0, 0, x
+ columnWidth
- gap
, viewport()->height());
286 foreach (DolphinColumnView
* column
, m_columns
) {
287 column
->setGeometry(QRect(x
, 0, columnWidth
- gap
, viewport()->height()));
290 emptyViewportRect
= QRect(x
, 0, viewport()->width() - x
- gap
, viewport()->height());
293 if (emptyViewportRect
.isValid()) {
294 m_emptyViewport
->show();
295 m_emptyViewport
->setGeometry(emptyViewportRect
);
297 m_emptyViewport
->hide();
301 void DolphinColumnViewContainer::updateScrollBar()
303 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
304 const int contentWidth
= m_columns
.count() * settings
->columnWidth();
306 horizontalScrollBar()->setPageStep(contentWidth
);
307 horizontalScrollBar()->setRange(0, contentWidth
- viewport()->width());
310 void DolphinColumnViewContainer::assureVisibleActiveColumn()
312 const int viewportWidth
= viewport()->width();
313 const int x
= activeColumn()->x();
315 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
316 const int width
= settings
->columnWidth();
318 if (x
+ width
> viewportWidth
) {
319 const int newContentX
= m_contentX
- x
- width
+ viewportWidth
;
320 if (isRightToLeft()) {
321 m_animation
->setFrameRange(m_contentX
, newContentX
);
323 m_animation
->setFrameRange(-m_contentX
, -newContentX
);
325 if (m_animation
->state() != QTimeLine::Running
) {
326 m_animation
->start();
329 const int newContentX
= m_contentX
- x
;
330 if (isRightToLeft()) {
331 m_animation
->setFrameRange(m_contentX
, newContentX
);
333 m_animation
->setFrameRange(-m_contentX
, -newContentX
);
335 if (m_animation
->state() != QTimeLine::Running
) {
336 m_animation
->start();
341 void DolphinColumnViewContainer::requestActivation(DolphinColumnView
* column
)
343 m_controller
->setItemView(column
);
344 if (column
->isActive()) {
345 assureVisibleActiveColumn();
348 foreach (DolphinColumnView
* currColumn
, m_columns
) {
349 if (currColumn
== column
) {
350 setActiveColumnIndex(index
);
358 void DolphinColumnViewContainer::removeAllColumns()
360 QList
<DolphinColumnView
*>::iterator start
= m_columns
.begin() + 1;
361 QList
<DolphinColumnView
*>::iterator end
= m_columns
.end();
362 for (QList
<DolphinColumnView
*>::iterator it
= start
; it
!= end
; ++it
) {
365 m_columns
.erase(start
, end
);
367 m_columns
[0]->setActive(true);
368 assureVisibleActiveColumn();
371 QPoint
DolphinColumnViewContainer::columnPosition(DolphinColumnView
* column
, const QPoint
& point
) const
373 const QPoint topLeft
= column
->frameGeometry().topLeft();
374 return QPoint(point
.x() - topLeft
.x(), point
.y() - topLeft
.y());
377 void DolphinColumnViewContainer::deleteColumn(DolphinColumnView
* column
)
383 if (m_controller
->itemView() == column
) {
384 m_controller
->setItemView(0);
386 // deleteWhenNotDragSource(column) does not necessarily delete column,
387 // and we want its preview generator destroyed immediately.
389 // Prevent automatic destruction of column when this DolphinColumnViewContainer
391 column
->setParent(0);
392 column
->disconnect();
394 if (DragAndDropHelper::instance().isDragSource(column
)) {
395 // The column is a drag source (the feature "Open folders
396 // during drag operations" is used). Deleting the view
397 // during an ongoing drag operation is not allowed, so
398 // this will postponed.
399 if (m_dragSource
!= 0) {
400 // the old stored view is obviously not the drag source anymore
401 m_dragSource
->deleteLater();
405 column
->setParent(0);
406 column
->disconnect();
408 m_dragSource
= column
;
410 column
->deleteLater();
414 #include "dolphincolumnviewcontainer.moc"