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 if (settings
->useSystemFont()) {
90 m_font
= KGlobalSettings::generalFont();
92 m_font
= QFont(settings
->fontFamily(),
94 settings
->fontWeight(),
95 settings
->italicFont());
98 const int iconSize
= settings
->iconSize();
99 setDecorationSize(QSize(iconSize
, iconSize
));
101 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
102 setItemDelegate(delegate
);
106 connect(this, SIGNAL(viewportEntered()),
107 m_view
->m_controller
, SLOT(emitViewportEntered()));
108 connect(this, SIGNAL(entered(const QModelIndex
&)),
109 this, SLOT(slotEntered(const QModelIndex
&)));
111 //m_dirLister = new DolphinDirLister(); TODO
112 m_dirLister
= new KDirLister();
113 m_dirLister
->setAutoUpdate(true);
114 m_dirLister
->setMainWindow(window());
115 m_dirLister
->setDelayedMimeTypes(true);
116 const bool showHiddenFiles
= m_view
->m_controller
->dolphinView()->showHiddenFiles();
117 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
119 m_dolphinModel
= new DolphinModel(this);
120 m_dolphinModel
->setDirLister(m_dirLister
);
121 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
123 m_proxyModel
= new DolphinSortFilterProxyModel(this);
124 m_proxyModel
->setSourceModel(m_dolphinModel
);
125 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
126 const DolphinView
* dolphinView
= m_view
->m_controller
->dolphinView();
127 m_proxyModel
->setSorting(dolphinView
->sorting());
128 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
130 setModel(m_proxyModel
);
131 const bool useSelManager
= KGlobalSettings::singleClick() &&
132 DolphinSettings::instance().generalSettings()->showSelectionToggle();
134 SelectionManager
* selManager
= new SelectionManager(this);
135 connect(selManager
, SIGNAL(selectionChanged()),
136 this, SLOT(requestActivation()));
137 connect(m_view
->m_controller
, SIGNAL(urlChanged(const KUrl
&)),
138 selManager
, SLOT(reset()));
141 new KMimeTypeResolver(this, m_dolphinModel
);
142 m_iconManager
= new IconManager(this, m_proxyModel
);
143 m_iconManager
->setShowPreview(m_view
->m_controller
->dolphinView()->showPreview());
145 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
147 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
148 this, SLOT(updateFont()));
151 DolphinColumnWidget::~DolphinColumnWidget()
155 delete m_dolphinModel
;
157 m_dirLister
= 0; // deleted by m_dolphinModel
160 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
163 m_decorationSize
= size
;
167 void DolphinColumnWidget::setActive(bool active
)
169 if (m_active
== active
) {
182 void DolphinColumnWidget::reload()
185 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
188 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting
)
190 m_proxyModel
->setSorting(sorting
);
193 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order
)
195 m_proxyModel
->setSortOrder(order
);
198 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
200 if (show
!= m_dirLister
->showingDotFiles()) {
201 m_dirLister
->setShowingDotFiles(show
);
203 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
207 void DolphinColumnWidget::setShowPreview(bool show
)
209 m_iconManager
->setShowPreview(show
);
212 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
215 void DolphinColumnWidget::updateBackground()
217 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
218 // cleaning up the cut-indication of DolphinColumnWidget with the code from
219 // DolphinView a common helper-class should be available which can be shared
220 // by all view implementations -> no hardcoded value anymore
221 const QPalette::ColorRole role
= viewport()->backgroundRole();
222 QColor color
= viewport()->palette().color(role
);
223 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
225 QPalette palette
= viewport()->palette();
226 palette
.setColor(role
, color
);
227 viewport()->setPalette(palette
);
232 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
234 m_proxyModel
->setFilterRegExp(nameFilter
);
238 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
240 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
241 viewOptions
.font
= m_font
;
242 viewOptions
.decorationSize
= m_decorationSize
;
243 viewOptions
.showDecorationSelected
= true;
247 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
249 DragAndDropHelper::startDrag(this, supportedActions
);
252 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
254 if (event
->mimeData()->hasUrls()) {
255 event
->acceptProposedAction();
261 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
263 QListView::dragLeaveEvent(event
);
265 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
267 setDirtyRegion(m_dropRect
);
270 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
272 QListView::dragMoveEvent(event
);
274 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
275 const QModelIndex index
= indexAt(event
->pos());
276 setDirtyRegion(m_dropRect
);
278 m_dropRect
.setSize(QSize()); // set as invalid
279 if (index
.isValid()) {
280 const KFileItem item
= itemForIndex(index
);
281 if (!item
.isNull() && item
.isDir()) {
282 m_dropRect
= visualRect(index
);
285 setDirtyRegion(m_dropRect
);
287 if (event
->mimeData()->hasUrls()) {
288 // accept url drops, independently from the destination item
289 event
->acceptProposedAction();
293 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
295 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
296 if (!urls
.isEmpty()) {
297 const QModelIndex index
= indexAt(event
->pos());
298 const KFileItem item
= itemForIndex(index
);
299 m_view
->m_controller
->indicateDroppedUrls(urls
,
302 event
->acceptProposedAction();
304 QListView::dropEvent(event
);
308 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
310 if (!m_childUrl
.isEmpty()) {
311 // indicate the shown URL of the next column by highlighting the shown folder item
312 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
313 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
314 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
315 const QRect itemRect
= visualRect(proxyIndex
);
316 QPainter
painter(viewport());
319 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
321 painter
.setPen(Qt::NoPen
);
322 painter
.setBrush(color
);
323 painter
.drawRect(itemRect
);
329 QListView::paintEvent(event
);
331 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
333 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
334 DragAndDropHelper::drawHoverIndication(this, m_dropRect
, brush
);
338 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
341 QListView::mousePressEvent(event
);
344 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
346 QListView::keyPressEvent(event
);
348 const QItemSelectionModel
* selModel
= selectionModel();
349 const QModelIndex currentIndex
= selModel
->currentIndex();
350 const bool trigger
= currentIndex
.isValid()
351 && (event
->key() == Qt::Key_Return
)
352 && (selModel
->selectedIndexes().count() <= 1);
354 triggerItem(currentIndex
);
358 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
361 m_view
->requestActivation(this);
362 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
365 QListView::contextMenuEvent(event
);
367 const QModelIndex index
= indexAt(event
->pos());
368 if (index
.isValid() || m_active
) {
369 // Only open a context menu above an item or if the mouse is above
370 // the active column.
371 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
372 m_view
->m_controller
->triggerContextMenuRequest(pos
);
376 void DolphinColumnWidget::wheelEvent(QWheelEvent
* event
)
378 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
379 if ((event
->modifiers() & Qt::ControlModifier
) == Qt::ControlModifier
) {
383 QListView::wheelEvent(event
);
386 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
388 QListView::selectionChanged(selected
, deselected
);
390 QItemSelectionModel
* selModel
= m_view
->selectionModel();
391 selModel
->select(selected
, QItemSelectionModel::Select
);
392 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
395 void DolphinColumnWidget::triggerItem(const QModelIndex
& index
)
397 const KFileItem item
= itemForIndex(index
);
398 m_view
->m_controller
->triggerItem(item
);
401 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
403 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
404 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
405 m_view
->m_controller
->emitItemEntered(item
);
408 void DolphinColumnWidget::requestActivation()
410 m_view
->m_controller
->requestActivation();
412 m_view
->requestActivation(this);
413 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
414 selectionModel()->clear();
418 void DolphinColumnWidget::updateFont()
420 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
421 Q_ASSERT(settings
!= 0);
423 if (settings
->useSystemFont()) {
424 m_font
= KGlobalSettings::generalFont();
428 void DolphinColumnWidget::activate()
430 setFocus(Qt::OtherFocusReason
);
432 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
433 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
434 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
435 if (KGlobalSettings::singleClick()) {
436 connect(this, SIGNAL(clicked(const QModelIndex
&)),
437 this, SLOT(triggerItem(const QModelIndex
&)));
439 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
440 this, SLOT(triggerItem(const QModelIndex
&)));
443 if (selectionModel() && selectionModel()->currentIndex().isValid())
444 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
449 void DolphinColumnWidget::deactivate()
453 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
454 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
455 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
456 if (KGlobalSettings::singleClick()) {
457 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
458 this, SLOT(triggerItem(const QModelIndex
&)));
460 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
461 this, SLOT(triggerItem(const QModelIndex
&)));
464 const QModelIndex current
= selectionModel()->currentIndex();
465 selectionModel()->clear();
466 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
470 KFileItem
DolphinColumnWidget::itemForIndex(const QModelIndex
& index
) const
472 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
473 return m_dolphinModel
->itemForIndex(dirIndex
);
477 #include "dolphincolumnwidget.moc"