]>
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 "settings/dolphinsettings.h"
27 #include "dolphin_columnmodesettings.h"
29 #include <kfilepreviewgenerator.h>
35 DolphinColumnViewContainer::DolphinColumnViewContainer(QWidget
* parent
, DolphinController
* controller
) :
37 m_controller(controller
),
45 Q_ASSERT(controller
!= 0);
48 setFocusPolicy(Qt::NoFocus
);
49 setFrameShape(QFrame::NoFrame
);
50 setLayoutDirection(Qt::LeftToRight
);
52 connect(this, SIGNAL(viewportEntered()),
53 controller
, SLOT(emitViewportEntered()));
54 connect(controller
, SIGNAL(activationChanged(bool)),
55 this, SLOT(updateColumnsBackground(bool)));
57 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
58 this, SLOT(moveContentHorizontally(int)));
60 m_animation
= new QTimeLine(500, this);
61 connect(m_animation
, SIGNAL(frameChanged(int)), horizontalScrollBar(), SLOT(setValue(int)));
63 DolphinColumnView
* column
= new DolphinColumnView(viewport(), this, m_controller
->url());
64 m_columns
.append(column
);
65 setActiveColumnIndex(0);
67 m_emptyViewport
= new QFrame(viewport());
68 m_emptyViewport
->setFrameStyle(QFrame::StyledPanel
| QFrame::Sunken
);
70 updateColumnsBackground(true);
73 DolphinColumnViewContainer::~DolphinColumnViewContainer()
77 KUrl
DolphinColumnViewContainer::rootUrl() const
79 return m_columns
[0]->url();
82 QAbstractItemView
* DolphinColumnViewContainer::activeColumn() const
84 return m_columns
[m_index
];
87 bool DolphinColumnViewContainer::showColumn(const KUrl
& url
)
89 if (!rootUrl().isParentOf(url
)) {
91 m_columns
[0]->setUrl(url
);
96 foreach (DolphinColumnView
* column
, m_columns
) {
97 if (column
->url() == url
) {
98 // the column represents already the requested URL, hence activate it
99 requestActivation(column
);
102 } else if (!column
->url().isParentOf(url
)) {
103 // the column is no parent of the requested URL, hence
104 // just delete all remaining columns
105 if (columnIndex
> 0) {
106 QList
<DolphinColumnView
*>::iterator start
= m_columns
.begin() + columnIndex
;
107 QList
<DolphinColumnView
*>::iterator end
= m_columns
.end();
108 for (QList
<DolphinColumnView
*>::iterator it
= start
; it
!= end
; ++it
) {
111 m_columns
.erase(start
, end
);
113 const int maxIndex
= m_columns
.count() - 1;
114 Q_ASSERT(maxIndex
>= 0);
115 if (m_index
> maxIndex
) {
124 // Create missing columns. Assuming that the path is "/home/peter/Temp/" and
125 // the target path is "/home/peter/Temp/a/b/c/", then the columns "a", "b" and
126 // "c" will be created.
127 const int lastIndex
= m_columns
.count() - 1;
128 Q_ASSERT(lastIndex
>= 0);
130 const KUrl
& activeUrl
= m_columns
[lastIndex
]->url();
131 Q_ASSERT(activeUrl
.isParentOf(url
));
132 Q_ASSERT(activeUrl
!= url
);
134 QString path
= activeUrl
.url(KUrl::AddTrailingSlash
);
135 const QString targetPath
= url
.url(KUrl::AddTrailingSlash
);
137 columnIndex
= lastIndex
;
138 int slashIndex
= path
.count('/');
139 bool hasSubPath
= (slashIndex
>= 0);
141 const QString subPath
= targetPath
.section('/', slashIndex
, slashIndex
);
142 if (subPath
.isEmpty()) {
145 path
+= subPath
+ '/';
148 const KUrl childUrl
= KUrl(path
);
149 m_columns
[columnIndex
]->setChildUrl(childUrl
);
152 DolphinColumnView
* column
= new DolphinColumnView(viewport(), this, childUrl
);
153 column
->setActive(false);
155 m_columns
.append(column
);
157 // Before invoking layoutColumns() the column must be set visible temporary.
158 // To prevent a flickering the initial geometry is set to a hidden position.
159 column
->setGeometry(QRect(-1, -1, 1, 1));
166 // set the last column as active column without modifying the controller
167 // and hence the history
168 m_columns
[m_index
]->setActive(false);
169 m_index
= columnIndex
;
170 m_columns
[m_index
]->setActive(true);
171 assureVisibleActiveColumn();
176 void DolphinColumnViewContainer::mousePressEvent(QMouseEvent
* event
)
178 m_controller
->requestActivation();
179 QScrollArea::mousePressEvent(event
);
182 void DolphinColumnViewContainer::resizeEvent(QResizeEvent
* event
)
184 QScrollArea::resizeEvent(event
);
187 assureVisibleActiveColumn();
190 void DolphinColumnViewContainer::wheelEvent(QWheelEvent
* event
)
192 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
193 if ((event
->modifiers() & Qt::ControlModifier
) == Qt::ControlModifier
) {
196 QScrollArea::wheelEvent(event
);
200 void DolphinColumnViewContainer::moveContentHorizontally(int x
)
202 m_contentX
= isRightToLeft() ? +x
: -x
;
206 void DolphinColumnViewContainer::updateColumnsBackground(bool active
)
208 if (active
== m_active
) {
214 // dim the background of the viewport
215 const QPalette::ColorRole role
= viewport()->backgroundRole();
216 QColor background
= viewport()->palette().color(role
);
217 background
.setAlpha(0); // make background transparent
219 QPalette palette
= viewport()->palette();
220 palette
.setColor(role
, background
);
221 viewport()->setPalette(palette
);
223 foreach (DolphinColumnView
* column
, m_columns
) {
224 column
->updateBackground();
228 void DolphinColumnViewContainer::setActiveColumnIndex(int index
)
230 if (m_index
== index
) {
234 const bool hasActiveColumn
= (m_index
>= 0);
235 if (hasActiveColumn
) {
236 m_columns
[m_index
]->setActive(false);
240 m_columns
[m_index
]->setActive(true);
242 assureVisibleActiveColumn();
245 void DolphinColumnViewContainer::layoutColumns()
249 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
250 const int columnWidth
= settings
->columnWidth();
252 QRect emptyViewportRect
;
253 if (isRightToLeft()) {
254 int x
= viewport()->width() - columnWidth
+ m_contentX
;
255 foreach (DolphinColumnView
* column
, m_columns
) {
256 column
->setGeometry(QRect(x
, 0, columnWidth
- gap
, viewport()->height()));
259 emptyViewportRect
= QRect(0, 0, x
+ columnWidth
- gap
, viewport()->height());
262 foreach (DolphinColumnView
* column
, m_columns
) {
263 column
->setGeometry(QRect(x
, 0, columnWidth
- gap
, viewport()->height()));
266 emptyViewportRect
= QRect(x
, 0, viewport()->width() - x
- gap
, viewport()->height());
269 if (emptyViewportRect
.isValid()) {
270 m_emptyViewport
->show();
271 m_emptyViewport
->setGeometry(emptyViewportRect
);
273 m_emptyViewport
->hide();
277 void DolphinColumnViewContainer::updateScrollBar()
279 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
280 const int contentWidth
= m_columns
.count() * settings
->columnWidth();
282 horizontalScrollBar()->setPageStep(contentWidth
);
283 horizontalScrollBar()->setRange(0, contentWidth
- viewport()->width());
286 void DolphinColumnViewContainer::assureVisibleActiveColumn()
288 const int viewportWidth
= viewport()->width();
289 const int x
= activeColumn()->x();
291 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
292 const int width
= settings
->columnWidth();
294 if (x
+ width
> viewportWidth
) {
295 const int newContentX
= m_contentX
- x
- width
+ viewportWidth
;
296 if (isRightToLeft()) {
297 m_animation
->setFrameRange(m_contentX
, newContentX
);
299 m_animation
->setFrameRange(-m_contentX
, -newContentX
);
301 if (m_animation
->state() != QTimeLine::Running
) {
302 m_animation
->start();
305 const int newContentX
= m_contentX
- x
;
306 if (isRightToLeft()) {
307 m_animation
->setFrameRange(m_contentX
, newContentX
);
309 m_animation
->setFrameRange(-m_contentX
, -newContentX
);
311 if (m_animation
->state() != QTimeLine::Running
) {
312 m_animation
->start();
317 void DolphinColumnViewContainer::requestActivation(DolphinColumnView
* column
)
319 m_controller
->setItemView(column
);
320 if (column
->isActive()) {
321 assureVisibleActiveColumn();
324 foreach (DolphinColumnView
* currColumn
, m_columns
) {
325 if (currColumn
== column
) {
326 setActiveColumnIndex(index
);
334 void DolphinColumnViewContainer::removeAllColumns()
336 QList
<DolphinColumnView
*>::iterator start
= m_columns
.begin() + 1;
337 QList
<DolphinColumnView
*>::iterator end
= m_columns
.end();
338 for (QList
<DolphinColumnView
*>::iterator it
= start
; it
!= end
; ++it
) {
341 m_columns
.erase(start
, end
);
343 m_columns
[0]->setActive(true);
344 assureVisibleActiveColumn();
347 QPoint
DolphinColumnViewContainer::columnPosition(DolphinColumnView
* column
, const QPoint
& point
) const
349 const QPoint topLeft
= column
->frameGeometry().topLeft();
350 return QPoint(point
.x() - topLeft
.x(), point
.y() - topLeft
.y());
353 void DolphinColumnViewContainer::deleteColumn(DolphinColumnView
* column
)
356 if (m_controller
->itemView() == column
) {
357 m_controller
->setItemView(0);
359 // deleteWhenNotDragSource(column) does not necessarily delete column,
360 // and we want its preview generator destroyed immediately.
361 column
->m_previewGenerator
->deleteLater();
362 column
->m_previewGenerator
= 0;
364 // Prevent automatic destruction of column when this DolphinColumnViewContainer
366 column
->setParent(0);
367 column
->disconnect();
368 emit
requestColumnDeletion(column
);
372 #include "dolphincolumnviewcontainer.moc"