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(entered(const QModelIndex
&)),
105 m_view
->m_controller
, SLOT(emitItemEntered(const QModelIndex
&)));
106 connect(this, SIGNAL(viewportEntered()),
107 m_view
->m_controller
, SLOT(emitViewportEntered()));
108 connect(this, SIGNAL(viewportEntered()),
109 m_view
->m_controller
, SLOT(emitViewportEntered()));
111 connect(this, SIGNAL(entered(const QModelIndex
&)),
112 this, SLOT(slotEntered(const QModelIndex
&)));
114 //m_dirLister = new DolphinDirLister(); TODO
115 m_dirLister
= new KDirLister();
116 m_dirLister
->setAutoUpdate(true);
117 m_dirLister
->setMainWindow(this);
118 m_dirLister
->setDelayedMimeTypes(true);
119 m_dirLister
->setShowingDotFiles(m_view
->m_controller
->showHiddenFiles());
120 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
121 this, SLOT(generatePreviews(const KFileItemList
&)));
123 m_dolphinModel
= new DolphinModel(this);
124 m_dolphinModel
->setDirLister(m_dirLister
);
125 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
127 m_proxyModel
= new DolphinSortFilterProxyModel(this);
128 m_proxyModel
->setSourceModel(m_dolphinModel
);
130 setModel(m_proxyModel
);
132 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
135 DolphinColumnWidget::~DolphinColumnWidget()
141 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
143 m_viewOptions
.decorationSize
= size
;
147 void DolphinColumnWidget::setActive(bool active
)
149 if (m_active
== active
) {
162 void DolphinColumnWidget::reload()
165 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
168 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
170 if (show
!= m_dirLister
->showingDotFiles()) {
171 m_dirLister
->setShowingDotFiles(show
);
173 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
177 void DolphinColumnWidget::setShowPreview(bool show
)
179 if (show
!= m_showPreview
) {
181 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
185 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
187 if (event
->mimeData()->hasUrls()) {
188 event
->acceptProposedAction();
194 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
196 QListView::dragLeaveEvent(event
);
198 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
200 setDirtyRegion(m_dropRect
);
203 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
205 QListView::dragMoveEvent(event
);
207 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
208 const QModelIndex index
= indexAt(event
->pos());
209 setDirtyRegion(m_dropRect
);
210 m_dropRect
= visualRect(index
);
211 setDirtyRegion(m_dropRect
);
214 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
216 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
217 if (!urls
.isEmpty()) {
218 event
->acceptProposedAction();
219 m_view
->m_controller
->indicateDroppedUrls(urls
,
221 indexAt(event
->pos()),
224 QListView::dropEvent(event
);
228 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
230 if (!m_childUrl
.isEmpty()) {
231 // indicate the shown URL of the next column by highlighting the shown folder item
232 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
233 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
234 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
235 const QRect itemRect
= visualRect(proxyIndex
);
236 QPainter
painter(viewport());
239 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
241 painter
.setPen(Qt::NoPen
);
242 painter
.setBrush(color
);
243 painter
.drawRect(itemRect
);
249 QListView::paintEvent(event
);
251 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
253 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
254 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
258 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
261 m_view
->requestActivation(this);
264 QListView::mousePressEvent(event
);
267 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
269 QListView::keyPressEvent(event
);
271 const QItemSelectionModel
* selModel
= selectionModel();
272 const QModelIndex currentIndex
= selModel
->currentIndex();
273 const bool trigger
= currentIndex
.isValid()
274 && (event
->key() == Qt::Key_Return
)
275 && (selModel
->selectedIndexes().count() <= 1);
277 triggerItem(currentIndex
);
281 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
284 m_view
->requestActivation(this);
287 QListView::contextMenuEvent(event
);
289 const QModelIndex index
= indexAt(event
->pos());
290 if (index
.isValid() || m_active
) {
291 // Only open a context menu above an item or if the mouse is above
292 // the active column.
293 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
294 m_view
->m_controller
->triggerContextMenuRequest(pos
);
298 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
300 QListView::selectionChanged(selected
, deselected
);
302 QItemSelectionModel
* selModel
= m_view
->selectionModel();
303 selModel
->select(selected
, QItemSelectionModel::Select
);
304 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
306 void DolphinColumnWidget::triggerItem(const QModelIndex
& index
)
308 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
309 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
310 // items are selected by the user, hence don't trigger the
311 // item specified by 'index'
315 // TODO: check ZIP support (see DolphinViewContainer::triggerItem)
316 KFileItem item
= m_dolphinModel
->itemForIndex(m_proxyModel
->mapToSource(index
));
319 const KUrl url
= item
.mostLocalUrl(isLocal
);
320 m_view
->showColumn(url
);
321 m_view
->m_controller
->setUrl(url
);
322 } else if (item
.isFile()) {
327 void DolphinColumnWidget::generatePreviews(const KFileItemList
& items
)
329 // TODO: same implementation as in DolphinView; create helper class
330 // for generatePreviews(), showPreview() and isCutItem()
332 if (m_view
->m_controller
->showPreview()) {
333 KIO::PreviewJob
* job
= KIO::filePreview(items
, 128);
334 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
335 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
339 void DolphinColumnWidget::showPreview(const KFileItem
& item
, const QPixmap
& pixmap
)
341 // TODO: same implementation as in DolphinView; create helper class
342 // for generatePreviews(), showPreview() and isCutItem()
344 Q_ASSERT(!item
.isNull());
345 if (item
.url().directory() != m_dirLister
->url().path()) {
346 // the preview job is still working on items of an older URL, hence
347 // the item is not part of the directory model anymore
351 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
352 if (idx
.isValid() && (idx
.column() == 0)) {
353 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
354 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
355 KIconEffect iconEffect
;
356 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
357 m_dolphinModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
359 m_dolphinModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
364 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
366 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
367 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
368 m_view
->m_controller
->emitItemEntered(item
);
371 void DolphinColumnWidget::activate()
373 if (m_view
->hasFocus()) {
374 setFocus(Qt::OtherFocusReason
);
376 m_view
->setFocusProxy(this);
378 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
379 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
380 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
381 if (KGlobalSettings::singleClick()) {
382 connect(this, SIGNAL(clicked(const QModelIndex
&)),
383 this, SLOT(triggerItem(const QModelIndex
&)));
385 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
386 this, SLOT(triggerItem(const QModelIndex
&)));
389 const QColor bgColor
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
390 QPalette palette
= viewport()->palette();
391 palette
.setColor(viewport()->backgroundRole(), bgColor
);
392 viewport()->setPalette(palette
);
394 if (!m_childUrl
.isEmpty()) {
395 // assure that the current index is set on the index that represents
397 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
398 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
399 selectionModel()->setCurrentIndex(proxyIndex
, QItemSelectionModel::Current
);
405 void DolphinColumnWidget::deactivate()
407 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
408 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
409 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
410 if (KGlobalSettings::singleClick()) {
411 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
412 this, SLOT(triggerItem(const QModelIndex
&)));
414 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
415 this, SLOT(triggerItem(const QModelIndex
&)));
418 const QPalette palette
= m_view
->viewport()->palette();
419 viewport()->setPalette(palette
);
421 selectionModel()->clear();
425 bool DolphinColumnWidget::isCutItem(const KFileItem
& item
) const
427 // TODO: same implementation as in DolphinView; create helper class
428 // for generatePreviews(), showPreview() and isCutItem()
430 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
431 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
433 const KUrl
& itemUrl
= item
.url();
434 KUrl::List::const_iterator it
= cutUrls
.begin();
435 const KUrl::List::const_iterator end
= cutUrls
.end();
437 if (*it
== itemUrl
) {
446 #include "dolphincolumnwidget.moc"