]>
cloud.milkyroute.net Git - dolphin.git/blob - src/views/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 "dolphin_columnmodesettings.h"
24 #include "dolphincolumnview.h"
25 #include "dolphinviewcontroller.h"
26 #include "dolphinsortfilterproxymodel.h"
27 #include "draganddrophelper.h"
28 #include "settings/dolphinsettings.h"
29 #include "viewmodecontroller.h"
36 DolphinColumnViewContainer::DolphinColumnViewContainer(QWidget
* parent
,
37 DolphinViewController
* dolphinViewController
,
38 const ViewModeController
* viewModeController
) :
40 m_dolphinViewController(dolphinViewController
),
41 m_viewModeController(viewModeController
),
51 Q_ASSERT(dolphinViewController
!= 0);
52 Q_ASSERT(viewModeController
!= 0);
55 setFocusPolicy(Qt::NoFocus
);
56 setFrameShape(QFrame::NoFrame
);
57 setLayoutDirection(Qt::LeftToRight
);
59 connect(viewModeController
, SIGNAL(activationChanged(bool)),
60 this, SLOT(updateColumnsBackground(bool)));
62 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
63 this, SLOT(moveContentHorizontally(int)));
65 m_animation
= new QTimeLine(500, this);
66 connect(m_animation
, SIGNAL(frameChanged(int)), horizontalScrollBar(), SLOT(setValue(int)));
68 m_activeUrlTimer
= new QTimer(this);
69 m_activeUrlTimer
->setSingleShot(true);
70 m_activeUrlTimer
->setInterval(200);
71 connect(m_activeUrlTimer
, SIGNAL(timeout()),
72 this, SLOT(updateActiveUrl()));
74 DolphinColumnView
* column
= new DolphinColumnView(viewport(), this, viewModeController
->url());
75 m_columns
.append(column
);
76 requestActivation(column
);
78 m_emptyViewport
= new QFrame(viewport());
79 m_emptyViewport
->setFrameStyle(QFrame::StyledPanel
| QFrame::Sunken
);
81 updateColumnsBackground(true);
85 DolphinColumnViewContainer::~DolphinColumnViewContainer()
91 KUrl
DolphinColumnViewContainer::rootUrl() const
93 return m_columns
[0]->url();
96 QAbstractItemView
* DolphinColumnViewContainer::activeColumn() const
98 return m_columns
[m_index
];
101 void DolphinColumnViewContainer::showColumn(const KUrl
& url
)
103 if (!rootUrl().isParentOf(url
)) {
105 m_columns
[0]->setUrl(url
);
110 foreach (DolphinColumnView
* column
, m_columns
) {
111 if (column
->url() == url
) {
112 // the column represents already the requested URL, hence activate it
113 requestActivation(column
);
116 } else if (!column
->url().isParentOf(url
)) {
117 // the column is no parent of the requested URL, hence
118 // just delete all remaining columns
119 if (columnIndex
> 0) {
120 QList
<DolphinColumnView
*>::iterator start
= m_columns
.begin() + columnIndex
;
121 QList
<DolphinColumnView
*>::iterator end
= m_columns
.end();
122 for (QList
<DolphinColumnView
*>::iterator it
= start
; it
!= end
; ++it
) {
125 m_columns
.erase(start
, end
);
127 const int maxIndex
= m_columns
.count() - 1;
128 Q_ASSERT(maxIndex
>= 0);
129 if (m_index
> maxIndex
) {
138 // Create missing columns. Assuming that the path is "/home/peter/Temp/" and
139 // the target path is "/home/peter/Temp/a/b/c/", then the columns "a", "b" and
140 // "c" will be created.
141 const int lastIndex
= m_columns
.count() - 1;
142 Q_ASSERT(lastIndex
>= 0);
144 const KUrl
& activeUrl
= m_columns
[lastIndex
]->url();
145 Q_ASSERT(activeUrl
.isParentOf(url
));
146 Q_ASSERT(activeUrl
!= url
);
148 QString path
= activeUrl
.url(KUrl::AddTrailingSlash
);
149 const QString targetPath
= url
.url(KUrl::AddTrailingSlash
);
151 columnIndex
= lastIndex
;
152 int slashIndex
= path
.count('/');
153 bool hasSubPath
= (slashIndex
>= 0);
155 const QString subPath
= targetPath
.section('/', slashIndex
, slashIndex
);
156 if (subPath
.isEmpty()) {
159 path
+= subPath
+ '/';
162 const KUrl childUrl
= KUrl(path
);
163 m_columns
[columnIndex
]->setChildUrl(childUrl
);
166 DolphinColumnView
* column
= new DolphinColumnView(viewport(), this, childUrl
);
167 m_columns
.append(column
);
169 // Before invoking layoutColumns() the column must be set visible temporary.
170 // To prevent a flickering the initial geometry is set to a hidden position.
171 column
->setGeometry(QRect(-1, -1, 1, 1));
178 requestActivation(m_columns
[columnIndex
]);
181 void DolphinColumnViewContainer::mousePressEvent(QMouseEvent
* event
)
183 m_dolphinViewController
->requestActivation();
184 QScrollArea::mousePressEvent(event
);
187 void DolphinColumnViewContainer::keyPressEvent(QKeyEvent
* event
)
189 if (event
->key() == Qt::Key_Left
) {
191 requestActivation(m_columns
[m_index
- 1]);
194 QScrollArea::keyPressEvent(event
);
198 void DolphinColumnViewContainer::resizeEvent(QResizeEvent
* event
)
200 QScrollArea::resizeEvent(event
);
203 assureVisibleActiveColumn();
206 void DolphinColumnViewContainer::wheelEvent(QWheelEvent
* event
)
208 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
209 if ((event
->modifiers() & Qt::ControlModifier
) == Qt::ControlModifier
) {
212 QScrollArea::wheelEvent(event
);
216 void DolphinColumnViewContainer::moveContentHorizontally(int x
)
218 m_contentX
= isRightToLeft() ? +x
: -x
;
222 void DolphinColumnViewContainer::updateColumnsBackground(bool active
)
224 if (active
== m_active
) {
230 // dim the background of the viewport
231 const QPalette::ColorRole role
= viewport()->backgroundRole();
232 QColor background
= viewport()->palette().color(role
);
233 background
.setAlpha(0); // make background transparent
235 QPalette palette
= viewport()->palette();
236 palette
.setColor(role
, background
);
237 viewport()->setPalette(palette
);
239 foreach (DolphinColumnView
* column
, m_columns
) {
240 column
->updateBackground();
244 void DolphinColumnViewContainer::updateActiveUrl()
246 const KUrl activeUrl
= m_columns
[m_index
]->url();
247 m_dolphinViewController
->requestUrlChange(activeUrl
);
250 void DolphinColumnViewContainer::layoutColumns()
254 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
255 const int columnWidth
= settings
->columnWidth();
257 QRect emptyViewportRect
;
258 if (isRightToLeft()) {
259 int x
= viewport()->width() - columnWidth
+ m_contentX
;
260 foreach (DolphinColumnView
* column
, m_columns
) {
261 column
->setGeometry(QRect(x
, 0, columnWidth
- gap
, viewport()->height()));
264 emptyViewportRect
= QRect(0, 0, x
+ columnWidth
- gap
, viewport()->height());
267 foreach (DolphinColumnView
* column
, m_columns
) {
268 column
->setGeometry(QRect(x
, 0, columnWidth
- gap
, viewport()->height()));
271 emptyViewportRect
= QRect(x
, 0, viewport()->width() - x
- gap
, viewport()->height());
274 if (emptyViewportRect
.isValid()) {
275 m_emptyViewport
->show();
276 m_emptyViewport
->setGeometry(emptyViewportRect
);
278 m_emptyViewport
->hide();
282 void DolphinColumnViewContainer::updateScrollBar()
284 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
285 const int contentWidth
= m_columns
.count() * settings
->columnWidth();
287 horizontalScrollBar()->setPageStep(contentWidth
);
288 horizontalScrollBar()->setRange(0, contentWidth
- viewport()->width());
291 void DolphinColumnViewContainer::assureVisibleActiveColumn()
293 const int viewportWidth
= viewport()->width();
294 const int x
= activeColumn()->x();
296 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
297 const int width
= settings
->columnWidth();
299 if (x
+ width
> viewportWidth
) {
300 const int newContentX
= m_contentX
- x
- width
+ viewportWidth
;
301 if (isRightToLeft()) {
302 m_animation
->setFrameRange(m_contentX
, newContentX
);
304 m_animation
->setFrameRange(-m_contentX
, -newContentX
);
306 if (m_animation
->state() != QTimeLine::Running
) {
307 m_animation
->start();
310 const int newContentX
= m_contentX
- x
;
311 if (isRightToLeft()) {
312 m_animation
->setFrameRange(m_contentX
, newContentX
);
314 m_animation
->setFrameRange(-m_contentX
, -newContentX
);
316 if (m_animation
->state() != QTimeLine::Running
) {
317 m_animation
->start();
322 void DolphinColumnViewContainer::requestActivation(DolphinColumnView
* column
)
324 if (m_dolphinViewController
->itemView() != column
) {
325 m_dolphinViewController
->setItemView(column
);
327 if (focusProxy() != column
) {
328 setFocusProxy(column
);
331 if (column
->isActive()) {
332 assureVisibleActiveColumn();
334 // Deactivate the currently active column
336 m_columns
[m_index
]->setActive(false);
339 // Get the index of the column that should get activated
341 foreach (DolphinColumnView
* currColumn
, m_columns
) {
342 if (currColumn
== column
) {
348 Q_ASSERT(index
!= m_index
);
349 Q_ASSERT(index
< m_columns
.count());
351 // Activate the requested column
353 m_columns
[m_index
]->setActive(true);
355 assureVisibleActiveColumn();
356 m_activeUrlTimer
->start(); // calls slot updateActiveUrl()
360 void DolphinColumnViewContainer::removeAllColumns()
362 QList
<DolphinColumnView
*>::iterator start
= m_columns
.begin() + 1;
363 QList
<DolphinColumnView
*>::iterator end
= m_columns
.end();
364 for (QList
<DolphinColumnView
*>::iterator it
= start
; it
!= end
; ++it
) {
367 m_columns
.erase(start
, end
);
369 m_columns
[0]->setActive(true);
370 assureVisibleActiveColumn();
373 QPoint
DolphinColumnViewContainer::columnPosition(DolphinColumnView
* column
, const QPoint
& point
) const
375 const QPoint topLeft
= column
->frameGeometry().topLeft();
376 return QPoint(point
.x() - topLeft
.x(), point
.y() - topLeft
.y());
379 void DolphinColumnViewContainer::deleteColumn(DolphinColumnView
* column
)
385 if (m_dolphinViewController
->itemView() == column
) {
386 m_dolphinViewController
->setItemView(0);
388 // deleteWhenNotDragSource(column) does not necessarily delete column,
389 // and we want its preview generator destroyed immediately.
391 // Prevent automatic destruction of column when this DolphinColumnViewContainer
393 column
->setParent(0);
394 column
->disconnect();
396 if (DragAndDropHelper::instance().isDragSource(column
)) {
397 // The column is a drag source (the feature "Open folders
398 // during drag operations" is used). Deleting the view
399 // during an ongoing drag operation is not allowed, so
400 // this will postponed.
401 if (m_dragSource
!= 0) {
402 // the old stored view is obviously not the drag source anymore
403 m_dragSource
->deleteLater();
407 column
->setParent(0);
408 column
->disconnect();
410 m_dragSource
= column
;
412 column
->deleteLater();
416 #include "dolphincolumnviewcontainer.moc"