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"
32 #include "tooltipmanager.h"
34 #include <kcolorscheme.h>
35 #include <kdirlister.h>
36 #include <kfileitem.h>
37 #include <kio/previewjob.h>
38 #include <kiconeffect.h>
40 #include <kmimetyperesolver.h>
41 #include <konqmimedata.h>
43 #include "iconmanager.h"
45 #include <QApplication>
50 DolphinColumnWidget::DolphinColumnWidget(QWidget
* parent
,
51 DolphinColumnView
* columnView
,
56 m_selectionManager(0),
67 setMouseTracking(true);
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);
75 setEditTriggers(QAbstractItemView::NoEditTriggers
);
77 setVerticalScrollMode(QListView::ScrollPerPixel
);
78 setHorizontalScrollMode(QListView::ScrollPerPixel
);
80 // apply the column mode settings to the widget
81 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
82 Q_ASSERT(settings
!= 0);
84 if (settings
->useSystemFont()) {
85 m_font
= KGlobalSettings::generalFont();
87 m_font
= QFont(settings
->fontFamily(),
89 settings
->fontWeight(),
90 settings
->italicFont());
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(window());
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
);
121 const DolphinView
* dolphinView
= m_view
->m_controller
->dolphinView();
122 m_proxyModel
->setSorting(dolphinView
->sorting());
123 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
125 setModel(m_proxyModel
);
126 const bool useSelManager
= KGlobalSettings::singleClick() &&
127 DolphinSettings::instance().generalSettings()->showSelectionToggle();
129 m_selectionManager
= new SelectionManager(this);
130 connect(m_selectionManager
, SIGNAL(selectionChanged()),
131 this, SLOT(requestActivation()));
132 connect(m_view
->m_controller
, SIGNAL(urlChanged(const KUrl
&)),
133 m_selectionManager
, SLOT(reset()));
136 new KMimeTypeResolver(this, m_dolphinModel
);
137 m_iconManager
= new IconManager(this, m_proxyModel
);
138 m_iconManager
->setShowPreview(m_view
->m_controller
->dolphinView()->showPreview());
140 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
141 new ToolTipManager(this, m_proxyModel
);
144 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
146 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
147 this, SLOT(updateFont()));
150 DolphinColumnWidget::~DolphinColumnWidget()
154 delete m_dolphinModel
;
156 m_dirLister
= 0; // deleted by m_dolphinModel
159 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
162 m_decorationSize
= size
;
164 if (m_iconManager
!= 0) {
165 m_iconManager
->updatePreviews();
167 if (m_selectionManager
!= 0) {
168 m_selectionManager
->reset();
172 void DolphinColumnWidget::setActive(bool active
)
174 if (m_active
== active
) {
187 void DolphinColumnWidget::reload()
190 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
193 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting
)
195 m_proxyModel
->setSorting(sorting
);
198 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order
)
200 m_proxyModel
->setSortOrder(order
);
203 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
205 if (show
!= m_dirLister
->showingDotFiles()) {
206 m_dirLister
->setShowingDotFiles(show
);
208 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
212 void DolphinColumnWidget::setShowPreview(bool show
)
214 m_iconManager
->setShowPreview(show
);
217 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
220 void DolphinColumnWidget::updateBackground()
222 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
223 // cleaning up the cut-indication of DolphinColumnWidget with the code from
224 // DolphinView a common helper-class should be available which can be shared
225 // by all view implementations -> no hardcoded value anymore
226 const QPalette::ColorRole role
= viewport()->backgroundRole();
227 QColor color
= viewport()->palette().color(role
);
228 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
230 QPalette palette
= viewport()->palette();
231 palette
.setColor(role
, color
);
232 viewport()->setPalette(palette
);
237 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
239 m_proxyModel
->setFilterRegExp(nameFilter
);
242 void DolphinColumnWidget::editItem(const KFileItem
& item
)
244 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
245 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
246 if (proxyIndex
.isValid()) {
251 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
253 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
254 viewOptions
.font
= m_font
;
255 viewOptions
.decorationSize
= m_decorationSize
;
256 viewOptions
.showDecorationSelected
= true;
260 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
262 DragAndDropHelper::startDrag(this, supportedActions
);
265 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
267 if (event
->mimeData()->hasUrls()) {
268 event
->acceptProposedAction();
272 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
274 QListView::dragLeaveEvent(event
);
275 setDirtyRegion(m_dropRect
);
278 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
280 QListView::dragMoveEvent(event
);
282 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
283 const QModelIndex index
= indexAt(event
->pos());
284 setDirtyRegion(m_dropRect
);
286 m_dropRect
.setSize(QSize()); // set as invalid
287 if (index
.isValid()) {
288 m_view
->m_controller
->setItemView(this);
289 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
290 if (!item
.isNull() && item
.isDir()) {
291 m_dropRect
= visualRect(index
);
294 setDirtyRegion(m_dropRect
);
296 if (event
->mimeData()->hasUrls()) {
297 // accept url drops, independently from the destination item
298 event
->acceptProposedAction();
302 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
304 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
305 if (!urls
.isEmpty()) {
306 const QModelIndex index
= indexAt(event
->pos());
307 m_view
->m_controller
->setItemView(this);
308 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
309 m_view
->m_controller
->indicateDroppedUrls(urls
,
312 event
->acceptProposedAction();
314 QListView::dropEvent(event
);
317 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
319 if (!m_childUrl
.isEmpty()) {
320 // indicate the shown URL of the next column by highlighting the shown folder item
321 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
322 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
323 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
324 const QRect itemRect
= visualRect(proxyIndex
);
325 QPainter
painter(viewport());
326 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
328 painter
.setPen(Qt::NoPen
);
329 painter
.setBrush(color
);
330 painter
.drawRect(itemRect
);
334 QListView::paintEvent(event
);
337 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
340 if (indexAt(event
->pos()).isValid() && (event
->button() == Qt::LeftButton
)) {
341 // TODO: see comment in DolphinIconsView::mousePressEvent()
342 setState(QAbstractItemView::DraggingState
);
344 QListView::mousePressEvent(event
);
347 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
349 QListView::keyPressEvent(event
);
351 m_view
->m_controller
->handleKeyPressEvent(event
);
354 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
357 m_view
->requestActivation(this);
358 Q_ASSERT(m_view
->m_controller
->itemView() == this);
359 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
362 QListView::contextMenuEvent(event
);
364 const QModelIndex index
= indexAt(event
->pos());
365 if (index
.isValid() || m_active
) {
366 // Only open a context menu above an item or if the mouse is above
367 // the active column.
368 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
369 Q_ASSERT(m_view
->m_controller
->itemView() == this);
370 m_view
->m_controller
->triggerContextMenuRequest(pos
);
374 void DolphinColumnWidget::wheelEvent(QWheelEvent
* event
)
376 if (m_selectionManager
!= 0) {
377 m_selectionManager
->reset();
380 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
381 if (event
->modifiers() & Qt::ControlModifier
) {
386 QListView::wheelEvent(event
);
389 void DolphinColumnWidget::leaveEvent(QEvent
* event
)
391 QListView::leaveEvent(event
);
392 // if the mouse is above an item and moved very fast outside the widget,
393 // no viewportEntered() signal might be emitted although the mouse has been moved
394 // above the viewport
395 m_view
->m_controller
->emitViewportEntered();
398 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
400 QListView::selectionChanged(selected
, deselected
);
402 QItemSelectionModel
* selModel
= m_view
->selectionModel();
403 selModel
->select(selected
, QItemSelectionModel::Select
);
404 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
407 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
409 m_view
->m_controller
->setItemView(this);
410 m_view
->m_controller
->emitItemEntered(index
);
413 void DolphinColumnWidget::requestActivation()
415 m_view
->m_controller
->setItemView(this);
416 m_view
->m_controller
->requestActivation();
418 m_view
->requestActivation(this);
419 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
420 selectionModel()->clear();
424 void DolphinColumnWidget::updateFont()
426 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
427 Q_ASSERT(settings
!= 0);
429 if (settings
->useSystemFont()) {
430 m_font
= KGlobalSettings::generalFont();
434 void DolphinColumnWidget::activate()
436 setFocus(Qt::OtherFocusReason
);
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 connect(this, SIGNAL(clicked(const QModelIndex
&)),
443 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
445 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
446 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
449 if (selectionModel() && selectionModel()->currentIndex().isValid())
450 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
455 void DolphinColumnWidget::deactivate()
459 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
460 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
461 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
462 if (KGlobalSettings::singleClick()) {
463 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
464 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
466 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
467 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
470 const QModelIndex current
= selectionModel()->currentIndex();
471 selectionModel()->clear();
472 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
476 #include "dolphincolumnwidget.moc"