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 "dolphinsortfilterproxymodel.h"
27 #include "dolphinsettings.h"
28 #include "dolphin_columnmodesettings.h"
29 #include "dolphin_generalsettings.h"
30 #include "draganddrophelper.h"
31 #include "selectionmanager.h"
33 #include <kcolorscheme.h>
34 #include <kdirlister.h>
35 #include <kfileitem.h>
36 #include <kio/previewjob.h>
37 #include <kiconeffect.h>
39 #include <kmimetyperesolver.h>
40 #include <konqmimedata.h>
42 #include "iconmanager.h"
44 #include <QApplication>
49 DolphinColumnWidget::DolphinColumnWidget(QWidget
* parent
,
50 DolphinColumnView
* columnView
,
66 setMouseTracking(true);
67 viewport()->setAttribute(Qt::WA_Hover
);
68 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
69 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded
);
70 setSelectionBehavior(SelectItems
);
71 setSelectionMode(QAbstractItemView::ExtendedSelection
);
72 setDragDropMode(QAbstractItemView::DragDrop
);
73 setDropIndicatorShown(false);
74 setSelectionRectVisible(true);
76 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
77 // check avoids a division by zero happening on versions before 4.3.1.
78 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
80 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
81 setVerticalScrollMode(QListView::ScrollPerPixel
);
82 setHorizontalScrollMode(QListView::ScrollPerPixel
);
85 // apply the column mode settings to the widget
86 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
87 Q_ASSERT(settings
!= 0);
89 m_font
= QFont(settings
->fontFamily(), settings
->fontSize());
90 m_font
.setItalic(settings
->italicFont());
91 m_font
.setBold(settings
->boldFont());
93 const int iconSize
= settings
->iconSize();
94 setDecorationSize(QSize(iconSize
, iconSize
));
96 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
97 setItemDelegate(delegate
);
101 connect(this, SIGNAL(viewportEntered()),
102 m_view
->m_controller
, SLOT(emitViewportEntered()));
103 connect(this, SIGNAL(entered(const QModelIndex
&)),
104 this, SLOT(slotEntered(const QModelIndex
&)));
106 //m_dirLister = new DolphinDirLister(); TODO
107 m_dirLister
= new KDirLister();
108 m_dirLister
->setAutoUpdate(true);
109 m_dirLister
->setMainWindow(this);
110 m_dirLister
->setDelayedMimeTypes(true);
111 const bool showHiddenFiles
= m_view
->m_controller
->dolphinView()->showHiddenFiles();
112 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
114 m_dolphinModel
= new DolphinModel(this);
115 m_dolphinModel
->setDirLister(m_dirLister
);
116 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
118 m_proxyModel
= new DolphinSortFilterProxyModel(this);
119 m_proxyModel
->setSourceModel(m_dolphinModel
);
120 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
122 setModel(m_proxyModel
);
123 const bool useSelManager
= KGlobalSettings::singleClick() &&
124 DolphinSettings::instance().generalSettings()->showSelectionToggle();
126 SelectionManager
* selManager
= new SelectionManager(this);
127 connect(selManager
, SIGNAL(selectionChanged()),
128 this, SLOT(requestActivation()));
129 connect(m_view
->m_controller
, SIGNAL(urlChanged(const KUrl
&)),
130 selManager
, SLOT(reset()));
132 new KMimeTypeResolver(this, m_dolphinModel
);
133 m_iconManager
= new IconManager(this, m_proxyModel
);
134 m_iconManager
->setShowPreview(m_view
->m_controller
->dolphinView()->showPreview());
136 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
139 DolphinColumnWidget::~DolphinColumnWidget()
143 delete m_dolphinModel
;
145 m_dirLister
= 0; // deleted by m_dolphinModel
148 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
151 m_decorationSize
= size
;
155 void DolphinColumnWidget::setActive(bool active
)
157 if (m_active
== active
) {
170 void DolphinColumnWidget::reload()
173 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
176 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
178 if (show
!= m_dirLister
->showingDotFiles()) {
179 m_dirLister
->setShowingDotFiles(show
);
181 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
185 void DolphinColumnWidget::setShowPreview(bool show
)
187 m_iconManager
->setShowPreview(show
);
190 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
193 void DolphinColumnWidget::updateBackground()
195 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
196 // cleaning up the cut-indication of DolphinColumnWidget with the code from
197 // DolphinView a common helper-class should be available which can be shared
198 // by all view implementations -> no hardcoded value anymore
199 const QPalette::ColorRole role
= viewport()->backgroundRole();
200 QColor color
= viewport()->palette().color(role
);
201 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
203 QPalette palette
= viewport()->palette();
204 palette
.setColor(role
, color
);
205 viewport()->setPalette(palette
);
210 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
212 m_proxyModel
->setFilterRegExp(nameFilter
);
216 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
218 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
219 viewOptions
.font
= m_font
;
220 viewOptions
.decorationSize
= m_decorationSize
;
221 viewOptions
.showDecorationSelected
= true;
225 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
227 DragAndDropHelper::startDrag(this, supportedActions
);
230 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
232 if (event
->mimeData()->hasUrls()) {
233 event
->acceptProposedAction();
239 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
241 QListView::dragLeaveEvent(event
);
243 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
245 setDirtyRegion(m_dropRect
);
248 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
250 QListView::dragMoveEvent(event
);
252 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
253 const QModelIndex index
= indexAt(event
->pos());
254 setDirtyRegion(m_dropRect
);
256 m_dropRect
.setSize(QSize()); // set as invalid
257 if (index
.isValid()) {
258 const KFileItem item
= itemForIndex(index
);
259 if (!item
.isNull() && item
.isDir()) {
260 m_dropRect
= visualRect(index
);
263 setDirtyRegion(m_dropRect
);
265 if (event
->mimeData()->hasUrls()) {
266 // accept url drops, independently from the destination item
267 event
->acceptProposedAction();
271 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
273 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
274 if (!urls
.isEmpty()) {
275 const QModelIndex index
= indexAt(event
->pos());
276 const KFileItem item
= itemForIndex(index
);
277 m_view
->m_controller
->indicateDroppedUrls(urls
,
280 event
->acceptProposedAction();
282 QListView::dropEvent(event
);
286 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
288 if (!m_childUrl
.isEmpty()) {
289 // indicate the shown URL of the next column by highlighting the shown folder item
290 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
291 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
292 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
293 const QRect itemRect
= visualRect(proxyIndex
);
294 QPainter
painter(viewport());
297 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
299 painter
.setPen(Qt::NoPen
);
300 painter
.setBrush(color
);
301 painter
.drawRect(itemRect
);
307 QListView::paintEvent(event
);
309 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
311 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
312 DragAndDropHelper::drawHoverIndication(this, m_dropRect
, brush
);
316 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
319 QListView::mousePressEvent(event
);
322 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
324 QListView::keyPressEvent(event
);
326 const QItemSelectionModel
* selModel
= selectionModel();
327 const QModelIndex currentIndex
= selModel
->currentIndex();
328 const bool trigger
= currentIndex
.isValid()
329 && (event
->key() == Qt::Key_Return
)
330 && (selModel
->selectedIndexes().count() <= 1);
332 triggerItem(currentIndex
);
336 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
339 m_view
->requestActivation(this);
340 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
343 QListView::contextMenuEvent(event
);
345 const QModelIndex index
= indexAt(event
->pos());
346 if (index
.isValid() || m_active
) {
347 // Only open a context menu above an item or if the mouse is above
348 // the active column.
349 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
350 m_view
->m_controller
->triggerContextMenuRequest(pos
);
354 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
356 QListView::selectionChanged(selected
, deselected
);
358 QItemSelectionModel
* selModel
= m_view
->selectionModel();
359 selModel
->select(selected
, QItemSelectionModel::Select
);
360 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
363 void DolphinColumnWidget::triggerItem(const QModelIndex
& index
)
365 const KFileItem item
= itemForIndex(index
);
366 m_view
->m_controller
->triggerItem(item
);
369 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
371 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
372 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
373 m_view
->m_controller
->emitItemEntered(item
);
376 void DolphinColumnWidget::requestActivation()
378 m_view
->m_controller
->requestActivation();
380 m_view
->requestActivation(this);
381 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
382 selectionModel()->clear();
386 void DolphinColumnWidget::activate()
388 setFocus(Qt::OtherFocusReason
);
390 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
391 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
392 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
393 if (KGlobalSettings::singleClick()) {
394 connect(this, SIGNAL(clicked(const QModelIndex
&)),
395 this, SLOT(triggerItem(const QModelIndex
&)));
397 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
398 this, SLOT(triggerItem(const QModelIndex
&)));
401 if (selectionModel() && selectionModel()->currentIndex().isValid())
402 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
407 void DolphinColumnWidget::deactivate()
411 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
412 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
413 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
414 if (KGlobalSettings::singleClick()) {
415 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
416 this, SLOT(triggerItem(const QModelIndex
&)));
418 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
419 this, SLOT(triggerItem(const QModelIndex
&)));
422 const QModelIndex current
= selectionModel()->currentIndex();
423 selectionModel()->clear();
424 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
428 KFileItem
DolphinColumnWidget::itemForIndex(const QModelIndex
& index
) const
430 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
431 return m_dolphinModel
->itemForIndex(dirIndex
);
435 #include "dolphincolumnwidget.moc"