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
;
161 void DolphinColumnWidget::setActive(bool active
)
163 if (m_active
== active
) {
176 void DolphinColumnWidget::reload()
179 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
182 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting
)
184 m_proxyModel
->setSorting(sorting
);
187 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order
)
189 m_proxyModel
->setSortOrder(order
);
192 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
194 if (show
!= m_dirLister
->showingDotFiles()) {
195 m_dirLister
->setShowingDotFiles(show
);
197 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
201 void DolphinColumnWidget::setShowPreview(bool show
)
203 m_iconManager
->setShowPreview(show
);
206 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
209 void DolphinColumnWidget::updateBackground()
211 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
212 // cleaning up the cut-indication of DolphinColumnWidget with the code from
213 // DolphinView a common helper-class should be available which can be shared
214 // by all view implementations -> no hardcoded value anymore
215 const QPalette::ColorRole role
= viewport()->backgroundRole();
216 QColor color
= viewport()->palette().color(role
);
217 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
219 QPalette palette
= viewport()->palette();
220 palette
.setColor(role
, color
);
221 viewport()->setPalette(palette
);
226 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
228 m_proxyModel
->setFilterRegExp(nameFilter
);
232 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
234 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
235 viewOptions
.font
= m_font
;
236 viewOptions
.decorationSize
= m_decorationSize
;
237 viewOptions
.showDecorationSelected
= true;
241 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
243 DragAndDropHelper::startDrag(this, supportedActions
);
246 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
248 if (event
->mimeData()->hasUrls()) {
249 event
->acceptProposedAction();
255 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
257 QListView::dragLeaveEvent(event
);
259 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
261 setDirtyRegion(m_dropRect
);
264 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
266 QListView::dragMoveEvent(event
);
268 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
269 const QModelIndex index
= indexAt(event
->pos());
270 setDirtyRegion(m_dropRect
);
272 m_dropRect
.setSize(QSize()); // set as invalid
273 if (index
.isValid()) {
274 m_view
->m_controller
->setItemView(this);
275 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
276 if (!item
.isNull() && item
.isDir()) {
277 m_dropRect
= visualRect(index
);
280 setDirtyRegion(m_dropRect
);
282 if (event
->mimeData()->hasUrls()) {
283 // accept url drops, independently from the destination item
284 event
->acceptProposedAction();
288 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
290 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
291 if (!urls
.isEmpty()) {
292 const QModelIndex index
= indexAt(event
->pos());
293 m_view
->m_controller
->setItemView(this);
294 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
295 m_view
->m_controller
->indicateDroppedUrls(urls
,
298 event
->acceptProposedAction();
300 QListView::dropEvent(event
);
304 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
306 if (!m_childUrl
.isEmpty()) {
307 // indicate the shown URL of the next column by highlighting the shown folder item
308 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
309 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
310 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
311 const QRect itemRect
= visualRect(proxyIndex
);
312 QPainter
painter(viewport());
315 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
317 painter
.setPen(Qt::NoPen
);
318 painter
.setBrush(color
);
319 painter
.drawRect(itemRect
);
325 QListView::paintEvent(event
);
327 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
329 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
330 DragAndDropHelper::drawHoverIndication(this, m_dropRect
, brush
);
334 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
337 QListView::mousePressEvent(event
);
340 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
342 QListView::keyPressEvent(event
);
344 m_view
->m_controller
->handleKeyPressEvent(event
);
347 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
350 m_view
->requestActivation(this);
351 Q_ASSERT(m_view
->m_controller
->itemView() == this);
352 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
355 QListView::contextMenuEvent(event
);
357 const QModelIndex index
= indexAt(event
->pos());
358 if (index
.isValid() || m_active
) {
359 // Only open a context menu above an item or if the mouse is above
360 // the active column.
361 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
362 Q_ASSERT(m_view
->m_controller
->itemView() == this);
363 m_view
->m_controller
->triggerContextMenuRequest(pos
);
367 void DolphinColumnWidget::wheelEvent(QWheelEvent
* event
)
369 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
370 if (event
->modifiers() & Qt::ControlModifier
) {
374 QListView::wheelEvent(event
);
377 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
379 QListView::selectionChanged(selected
, deselected
);
381 QItemSelectionModel
* selModel
= m_view
->selectionModel();
382 selModel
->select(selected
, QItemSelectionModel::Select
);
383 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
386 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
388 m_view
->m_controller
->setItemView(this);
389 m_view
->m_controller
->emitItemEntered(index
);
392 void DolphinColumnWidget::requestActivation()
394 m_view
->m_controller
->requestActivation();
396 m_view
->requestActivation(this);
397 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
398 selectionModel()->clear();
400 Q_ASSERT(m_view
->m_controller
->itemView() == this);
403 void DolphinColumnWidget::updateFont()
405 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
406 Q_ASSERT(settings
!= 0);
408 if (settings
->useSystemFont()) {
409 m_font
= KGlobalSettings::generalFont();
413 void DolphinColumnWidget::activate()
415 setFocus(Qt::OtherFocusReason
);
417 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
418 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
419 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
420 if (KGlobalSettings::singleClick()) {
421 connect(this, SIGNAL(clicked(const QModelIndex
&)),
422 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
424 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
425 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
428 if (selectionModel() && selectionModel()->currentIndex().isValid())
429 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
434 void DolphinColumnWidget::deactivate()
438 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
439 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
440 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
441 if (KGlobalSettings::singleClick()) {
442 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
443 this, SLOT(triggerItem(const QModelIndex
&)));
445 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
446 this, SLOT(triggerItem(const QModelIndex
&)));
449 const QModelIndex current
= selectionModel()->currentIndex();
450 selectionModel()->clear();
451 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
455 #include "dolphincolumnwidget.moc"