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 setVerticalScrollMode(QListView::ScrollPerPixel
);
77 setHorizontalScrollMode(QListView::ScrollPerPixel
);
79 // apply the column mode settings to the widget
80 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
81 Q_ASSERT(settings
!= 0);
83 if (settings
->useSystemFont()) {
84 m_font
= KGlobalSettings::generalFont();
86 m_font
= QFont(settings
->fontFamily(),
88 settings
->fontWeight(),
89 settings
->italicFont());
92 const int iconSize
= settings
->iconSize();
93 setDecorationSize(QSize(iconSize
, iconSize
));
95 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
96 setItemDelegate(delegate
);
100 connect(this, SIGNAL(viewportEntered()),
101 m_view
->m_controller
, SLOT(emitViewportEntered()));
102 connect(this, SIGNAL(entered(const QModelIndex
&)),
103 this, SLOT(slotEntered(const QModelIndex
&)));
105 //m_dirLister = new DolphinDirLister(); TODO
106 m_dirLister
= new KDirLister();
107 m_dirLister
->setAutoUpdate(true);
108 m_dirLister
->setMainWindow(window());
109 m_dirLister
->setDelayedMimeTypes(true);
110 const bool showHiddenFiles
= m_view
->m_controller
->dolphinView()->showHiddenFiles();
111 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
113 m_dolphinModel
= new DolphinModel(this);
114 m_dolphinModel
->setDirLister(m_dirLister
);
115 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
117 m_proxyModel
= new DolphinSortFilterProxyModel(this);
118 m_proxyModel
->setSourceModel(m_dolphinModel
);
119 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
120 const DolphinView
* dolphinView
= m_view
->m_controller
->dolphinView();
121 m_proxyModel
->setSorting(dolphinView
->sorting());
122 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
124 setModel(m_proxyModel
);
125 const bool useSelManager
= KGlobalSettings::singleClick() &&
126 DolphinSettings::instance().generalSettings()->showSelectionToggle();
128 SelectionManager
* selManager
= new SelectionManager(this);
129 connect(selManager
, SIGNAL(selectionChanged()),
130 this, SLOT(requestActivation()));
131 connect(m_view
->m_controller
, SIGNAL(urlChanged(const KUrl
&)),
132 selManager
, SLOT(reset()));
135 new KMimeTypeResolver(this, m_dolphinModel
);
136 m_iconManager
= new IconManager(this, m_proxyModel
);
137 m_iconManager
->setShowPreview(m_view
->m_controller
->dolphinView()->showPreview());
139 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
141 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
142 this, SLOT(updateFont()));
145 DolphinColumnWidget::~DolphinColumnWidget()
149 delete m_dolphinModel
;
151 m_dirLister
= 0; // deleted by m_dolphinModel
154 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
157 m_decorationSize
= size
;
159 if (m_iconManager
!= 0) {
160 m_iconManager
->updatePreviews();
164 void DolphinColumnWidget::setActive(bool active
)
166 if (m_active
== active
) {
179 void DolphinColumnWidget::reload()
182 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
185 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting
)
187 m_proxyModel
->setSorting(sorting
);
190 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order
)
192 m_proxyModel
->setSortOrder(order
);
195 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
197 if (show
!= m_dirLister
->showingDotFiles()) {
198 m_dirLister
->setShowingDotFiles(show
);
200 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
204 void DolphinColumnWidget::setShowPreview(bool show
)
206 m_iconManager
->setShowPreview(show
);
209 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
212 void DolphinColumnWidget::updateBackground()
214 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
215 // cleaning up the cut-indication of DolphinColumnWidget with the code from
216 // DolphinView a common helper-class should be available which can be shared
217 // by all view implementations -> no hardcoded value anymore
218 const QPalette::ColorRole role
= viewport()->backgroundRole();
219 QColor color
= viewport()->palette().color(role
);
220 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
222 QPalette palette
= viewport()->palette();
223 palette
.setColor(role
, color
);
224 viewport()->setPalette(palette
);
229 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
231 m_proxyModel
->setFilterRegExp(nameFilter
);
235 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
237 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
238 viewOptions
.font
= m_font
;
239 viewOptions
.decorationSize
= m_decorationSize
;
240 viewOptions
.showDecorationSelected
= true;
244 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
246 DragAndDropHelper::startDrag(this, supportedActions
);
249 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
251 if (event
->mimeData()->hasUrls()) {
252 event
->acceptProposedAction();
258 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
260 QListView::dragLeaveEvent(event
);
262 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
264 setDirtyRegion(m_dropRect
);
267 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
269 QListView::dragMoveEvent(event
);
271 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
272 const QModelIndex index
= indexAt(event
->pos());
273 setDirtyRegion(m_dropRect
);
275 m_dropRect
.setSize(QSize()); // set as invalid
276 if (index
.isValid()) {
277 m_view
->m_controller
->setItemView(this);
278 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
279 if (!item
.isNull() && item
.isDir()) {
280 m_dropRect
= visualRect(index
);
283 setDirtyRegion(m_dropRect
);
285 if (event
->mimeData()->hasUrls()) {
286 // accept url drops, independently from the destination item
287 event
->acceptProposedAction();
291 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
293 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
294 if (!urls
.isEmpty()) {
295 const QModelIndex index
= indexAt(event
->pos());
296 m_view
->m_controller
->setItemView(this);
297 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
298 m_view
->m_controller
->indicateDroppedUrls(urls
,
301 event
->acceptProposedAction();
303 QListView::dropEvent(event
);
307 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
309 if (!m_childUrl
.isEmpty()) {
310 // indicate the shown URL of the next column by highlighting the shown folder item
311 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
312 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
313 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
314 const QRect itemRect
= visualRect(proxyIndex
);
315 QPainter
painter(viewport());
318 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
320 painter
.setPen(Qt::NoPen
);
321 painter
.setBrush(color
);
322 painter
.drawRect(itemRect
);
328 QListView::paintEvent(event
);
330 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
332 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
333 DragAndDropHelper::drawHoverIndication(this, m_dropRect
, brush
);
337 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
340 QListView::mousePressEvent(event
);
343 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
345 QListView::keyPressEvent(event
);
347 m_view
->m_controller
->handleKeyPressEvent(event
);
350 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
353 m_view
->requestActivation(this);
354 Q_ASSERT(m_view
->m_controller
->itemView() == this);
355 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
358 QListView::contextMenuEvent(event
);
360 const QModelIndex index
= indexAt(event
->pos());
361 if (index
.isValid() || m_active
) {
362 // Only open a context menu above an item or if the mouse is above
363 // the active column.
364 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
365 Q_ASSERT(m_view
->m_controller
->itemView() == this);
366 m_view
->m_controller
->triggerContextMenuRequest(pos
);
370 void DolphinColumnWidget::wheelEvent(QWheelEvent
* event
)
372 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
373 if (event
->modifiers() & Qt::ControlModifier
) {
377 QListView::wheelEvent(event
);
380 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
382 QListView::selectionChanged(selected
, deselected
);
384 QItemSelectionModel
* selModel
= m_view
->selectionModel();
385 selModel
->select(selected
, QItemSelectionModel::Select
);
386 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
389 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
391 m_view
->m_controller
->setItemView(this);
392 m_view
->m_controller
->emitItemEntered(index
);
395 void DolphinColumnWidget::requestActivation()
397 m_view
->m_controller
->setItemView(this);
398 m_view
->m_controller
->requestActivation();
400 m_view
->requestActivation(this);
401 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
402 selectionModel()->clear();
406 void DolphinColumnWidget::updateFont()
408 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
409 Q_ASSERT(settings
!= 0);
411 if (settings
->useSystemFont()) {
412 m_font
= KGlobalSettings::generalFont();
416 void DolphinColumnWidget::activate()
418 setFocus(Qt::OtherFocusReason
);
420 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
421 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
422 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
423 if (KGlobalSettings::singleClick()) {
424 connect(this, SIGNAL(clicked(const QModelIndex
&)),
425 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
427 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
428 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
431 if (selectionModel() && selectionModel()->currentIndex().isValid())
432 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
437 void DolphinColumnWidget::deactivate()
441 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
442 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
443 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
444 if (KGlobalSettings::singleClick()) {
445 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
446 this, SLOT(triggerItem(const QModelIndex
&)));
448 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
449 this, SLOT(triggerItem(const QModelIndex
&)));
452 const QModelIndex current
= selectionModel()->currentIndex();
453 selectionModel()->clear();
454 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
458 #include "dolphincolumnwidget.moc"