]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnwidget.cpp
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 "dolphincolumnwidget.h"
22 #include "dolphinmodel.h"
23 #include "dolphincolumnview.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>
35 #include <kfileitem.h>
36 #include <kio/previewjob.h>
37 #include <kiconeffect.h>
38 #include <konqmimedata.h>
40 #include <QAbstractProxyModel>
41 #include <QApplication>
48 DolphinColumnWidget::DolphinColumnWidget(QWidget
* parent
,
49 DolphinColumnView
* columnView
,
64 setMouseTracking(true);
65 viewport()->setAttribute(Qt::WA_Hover
);
66 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
67 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
68 setSelectionBehavior(SelectItems
);
69 setSelectionMode(QAbstractItemView::ExtendedSelection
);
70 setDragDropMode(QAbstractItemView::DragDrop
);
71 setDropIndicatorShown(false);
72 setFocusPolicy(Qt::NoFocus
);
74 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
75 // check avoids a division by zero happening on versions before 4.3.1.
76 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
78 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
79 setVerticalScrollMode(QListView::ScrollPerPixel
);
80 setHorizontalScrollMode(QListView::ScrollPerPixel
);
83 // apply the column mode settings to the widget
84 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
85 Q_ASSERT(settings
!= 0);
87 m_viewOptions
= QListView::viewOptions();
89 QFont
font(settings
->fontFamily(), settings
->fontSize());
90 font
.setItalic(settings
->italicFont());
91 font
.setBold(settings
->boldFont());
92 m_viewOptions
.font
= font
;
94 const int iconSize
= settings
->iconSize();
95 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
97 m_viewOptions
.showDecorationSelected
= true;
99 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
100 setItemDelegate(delegate
);
104 connect(this, SIGNAL(viewportEntered()),
105 m_view
->m_controller
, SLOT(emitViewportEntered()));
106 connect(this, SIGNAL(entered(const QModelIndex
&)),
107 this, SLOT(slotEntered(const QModelIndex
&)));
109 //m_dirLister = new DolphinDirLister(); TODO
110 m_dirLister
= new KDirLister();
111 m_dirLister
->setAutoUpdate(true);
112 m_dirLister
->setMainWindow(this);
113 m_dirLister
->setDelayedMimeTypes(true);
114 m_dirLister
->setShowingDotFiles(m_view
->m_controller
->showHiddenFiles());
115 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
116 this, SLOT(generatePreviews(const KFileItemList
&)));
118 m_dolphinModel
= new DolphinModel(this);
119 m_dolphinModel
->setDirLister(m_dirLister
);
120 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
122 m_proxyModel
= new DolphinSortFilterProxyModel(this);
123 m_proxyModel
->setSourceModel(m_dolphinModel
);
125 setModel(m_proxyModel
);
127 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
130 DolphinColumnWidget::~DolphinColumnWidget()
136 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
138 m_viewOptions
.decorationSize
= size
;
142 void DolphinColumnWidget::setActive(bool active
)
144 if (m_active
== active
) {
157 void DolphinColumnWidget::reload()
160 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
163 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
165 if (show
!= m_dirLister
->showingDotFiles()) {
166 m_dirLister
->setShowingDotFiles(show
);
168 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
172 void DolphinColumnWidget::setShowPreview(bool show
)
174 if (show
!= m_showPreview
) {
176 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
180 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
182 if (event
->mimeData()->hasUrls()) {
183 event
->acceptProposedAction();
189 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
191 QListView::dragLeaveEvent(event
);
193 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
195 setDirtyRegion(m_dropRect
);
198 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
200 QListView::dragMoveEvent(event
);
202 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
203 const QModelIndex index
= indexAt(event
->pos());
204 setDirtyRegion(m_dropRect
);
205 m_dropRect
= visualRect(index
);
206 setDirtyRegion(m_dropRect
);
209 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
211 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
212 if (!urls
.isEmpty()) {
213 event
->acceptProposedAction();
214 m_view
->m_controller
->indicateDroppedUrls(urls
,
216 indexAt(event
->pos()),
219 QListView::dropEvent(event
);
223 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
225 if (!m_childUrl
.isEmpty()) {
226 // indicate the shown URL of the next column by highlighting the shown folder item
227 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
228 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
229 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
230 const QRect itemRect
= visualRect(proxyIndex
);
231 QPainter
painter(viewport());
234 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
236 painter
.setPen(Qt::NoPen
);
237 painter
.setBrush(color
);
238 painter
.drawRect(itemRect
);
244 QListView::paintEvent(event
);
246 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
248 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
249 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
253 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
256 m_view
->requestActivation(this);
259 QListView::mousePressEvent(event
);
262 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
264 QListView::keyPressEvent(event
);
266 const QItemSelectionModel
* selModel
= selectionModel();
267 const QModelIndex currentIndex
= selModel
->currentIndex();
268 const bool trigger
= currentIndex
.isValid()
269 && (event
->key() == Qt::Key_Return
)
270 && (selModel
->selectedIndexes().count() <= 1);
272 triggerItem(currentIndex
);
276 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
279 m_view
->requestActivation(this);
282 QListView::contextMenuEvent(event
);
284 const QModelIndex index
= indexAt(event
->pos());
285 if (index
.isValid() || m_active
) {
286 // Only open a context menu above an item or if the mouse is above
287 // the active column.
288 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
289 m_view
->m_controller
->triggerContextMenuRequest(pos
);
293 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
295 QListView::selectionChanged(selected
, deselected
);
297 QItemSelectionModel
* selModel
= m_view
->selectionModel();
298 selModel
->select(selected
, QItemSelectionModel::Select
);
299 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
301 void DolphinColumnWidget::triggerItem(const QModelIndex
& index
)
303 const KFileItem item
= m_dolphinModel
->itemForIndex(m_proxyModel
->mapToSource(index
));
304 m_view
->m_controller
->triggerItem(item
);
306 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
307 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
308 // items are selected by the user, hence don't trigger the
309 // item specified by 'index'
313 // TODO: check ZIP support (see DolphinViewContainer::triggerItem)
314 KFileItem item = m_dolphinModel->itemForIndex(m_proxyModel->mapToSource(index));
317 const KUrl url = item.mostLocalUrl(isLocal);
318 m_view->showColumn(url);
319 m_view->m_controller->setUrl(url);
320 } else if (item.isFile()) {
325 void DolphinColumnWidget::generatePreviews(const KFileItemList
& items
)
327 // TODO: same implementation as in DolphinView; create helper class
328 // for generatePreviews(), showPreview() and isCutItem()
330 if (m_view
->m_controller
->showPreview()) {
331 KIO::PreviewJob
* job
= KIO::filePreview(items
, 128);
332 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
333 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
337 void DolphinColumnWidget::showPreview(const KFileItem
& item
, const QPixmap
& pixmap
)
339 // TODO: same implementation as in DolphinView; create helper class
340 // for generatePreviews(), showPreview() and isCutItem()
342 Q_ASSERT(!item
.isNull());
343 if (item
.url().directory() != m_dirLister
->url().path()) {
344 // the preview job is still working on items of an older URL, hence
345 // the item is not part of the directory model anymore
349 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
350 if (idx
.isValid() && (idx
.column() == 0)) {
351 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
352 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
353 KIconEffect iconEffect
;
354 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
355 m_dolphinModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
357 m_dolphinModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
362 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
364 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
365 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
366 m_view
->m_controller
->emitItemEntered(item
);
369 void DolphinColumnWidget::activate()
371 if (m_view
->hasFocus()) {
372 setFocus(Qt::OtherFocusReason
);
374 m_view
->setFocusProxy(this);
376 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
377 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
378 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
379 if (KGlobalSettings::singleClick()) {
380 connect(this, SIGNAL(clicked(const QModelIndex
&)),
381 this, SLOT(triggerItem(const QModelIndex
&)));
383 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
384 this, SLOT(triggerItem(const QModelIndex
&)));
387 const QColor bgColor
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
388 QPalette palette
= viewport()->palette();
389 palette
.setColor(viewport()->backgroundRole(), bgColor
);
390 viewport()->setPalette(palette
);
392 if (!m_childUrl
.isEmpty()) {
393 // assure that the current index is set on the index that represents
395 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
396 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
397 selectionModel()->setCurrentIndex(proxyIndex
, QItemSelectionModel::Current
);
403 void DolphinColumnWidget::deactivate()
405 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
406 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
407 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
408 if (KGlobalSettings::singleClick()) {
409 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
410 this, SLOT(triggerItem(const QModelIndex
&)));
412 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
413 this, SLOT(triggerItem(const QModelIndex
&)));
416 const QPalette palette
= m_view
->viewport()->palette();
417 viewport()->setPalette(palette
);
419 selectionModel()->clear();
423 bool DolphinColumnWidget::isCutItem(const KFileItem
& item
) const
425 // TODO: same implementation as in DolphinView; create helper class
426 // for generatePreviews(), showPreview() and isCutItem()
428 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
429 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
431 const KUrl
& itemUrl
= item
.url();
432 KUrl::List::const_iterator it
= cutUrls
.begin();
433 const KUrl::List::const_iterator end
= cutUrls
.end();
435 if (*it
== itemUrl
) {
444 #include "dolphincolumnwidget.moc"