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(topLevelWidget());
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 SelectionManager
* selManager
= new SelectionManager(this);
130 connect(selManager
, SIGNAL(selectionChanged()),
131 this, SLOT(requestActivation()));
132 connect(m_view
->m_controller
, SIGNAL(urlChanged(const KUrl
&)),
133 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
);
142 DolphinColumnWidget::~DolphinColumnWidget()
146 delete m_dolphinModel
;
148 m_dirLister
= 0; // deleted by m_dolphinModel
151 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
154 m_decorationSize
= size
;
158 void DolphinColumnWidget::setActive(bool active
)
160 if (m_active
== active
) {
173 void DolphinColumnWidget::reload()
176 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
179 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting
)
181 m_proxyModel
->setSorting(sorting
);
184 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order
)
186 m_proxyModel
->setSortOrder(order
);
189 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
191 if (show
!= m_dirLister
->showingDotFiles()) {
192 m_dirLister
->setShowingDotFiles(show
);
194 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
198 void DolphinColumnWidget::setShowPreview(bool show
)
200 m_iconManager
->setShowPreview(show
);
203 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
206 void DolphinColumnWidget::updateBackground()
208 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
209 // cleaning up the cut-indication of DolphinColumnWidget with the code from
210 // DolphinView a common helper-class should be available which can be shared
211 // by all view implementations -> no hardcoded value anymore
212 const QPalette::ColorRole role
= viewport()->backgroundRole();
213 QColor color
= viewport()->palette().color(role
);
214 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
216 QPalette palette
= viewport()->palette();
217 palette
.setColor(role
, color
);
218 viewport()->setPalette(palette
);
223 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
225 m_proxyModel
->setFilterRegExp(nameFilter
);
229 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
231 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
232 viewOptions
.font
= m_font
;
233 viewOptions
.decorationSize
= m_decorationSize
;
234 viewOptions
.showDecorationSelected
= true;
238 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
240 DragAndDropHelper::startDrag(this, supportedActions
);
243 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
245 if (event
->mimeData()->hasUrls()) {
246 event
->acceptProposedAction();
252 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
254 QListView::dragLeaveEvent(event
);
256 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
258 setDirtyRegion(m_dropRect
);
261 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
263 QListView::dragMoveEvent(event
);
265 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
266 const QModelIndex index
= indexAt(event
->pos());
267 setDirtyRegion(m_dropRect
);
269 m_dropRect
.setSize(QSize()); // set as invalid
270 if (index
.isValid()) {
271 const KFileItem item
= itemForIndex(index
);
272 if (!item
.isNull() && item
.isDir()) {
273 m_dropRect
= visualRect(index
);
276 setDirtyRegion(m_dropRect
);
278 if (event
->mimeData()->hasUrls()) {
279 // accept url drops, independently from the destination item
280 event
->acceptProposedAction();
284 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
286 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
287 if (!urls
.isEmpty()) {
288 const QModelIndex index
= indexAt(event
->pos());
289 const KFileItem item
= itemForIndex(index
);
290 m_view
->m_controller
->indicateDroppedUrls(urls
,
293 event
->acceptProposedAction();
295 QListView::dropEvent(event
);
299 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
301 if (!m_childUrl
.isEmpty()) {
302 // indicate the shown URL of the next column by highlighting the shown folder item
303 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
304 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
305 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
306 const QRect itemRect
= visualRect(proxyIndex
);
307 QPainter
painter(viewport());
310 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
312 painter
.setPen(Qt::NoPen
);
313 painter
.setBrush(color
);
314 painter
.drawRect(itemRect
);
320 QListView::paintEvent(event
);
322 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
324 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
325 DragAndDropHelper::drawHoverIndication(this, m_dropRect
, brush
);
329 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
332 QListView::mousePressEvent(event
);
335 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
337 QListView::keyPressEvent(event
);
339 const QItemSelectionModel
* selModel
= selectionModel();
340 const QModelIndex currentIndex
= selModel
->currentIndex();
341 const bool trigger
= currentIndex
.isValid()
342 && (event
->key() == Qt::Key_Return
)
343 && (selModel
->selectedIndexes().count() <= 1);
345 triggerItem(currentIndex
);
349 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
352 m_view
->requestActivation(this);
353 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
356 QListView::contextMenuEvent(event
);
358 const QModelIndex index
= indexAt(event
->pos());
359 if (index
.isValid() || m_active
) {
360 // Only open a context menu above an item or if the mouse is above
361 // the active column.
362 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
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
) == 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::triggerItem(const QModelIndex
& index
)
388 const KFileItem item
= itemForIndex(index
);
389 m_view
->m_controller
->triggerItem(item
);
392 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
394 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
395 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
396 m_view
->m_controller
->emitItemEntered(item
);
399 void DolphinColumnWidget::requestActivation()
401 m_view
->m_controller
->requestActivation();
403 m_view
->requestActivation(this);
404 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
405 selectionModel()->clear();
409 void DolphinColumnWidget::activate()
411 setFocus(Qt::OtherFocusReason
);
413 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
414 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
415 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
416 if (KGlobalSettings::singleClick()) {
417 connect(this, SIGNAL(clicked(const QModelIndex
&)),
418 this, SLOT(triggerItem(const QModelIndex
&)));
420 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
421 this, SLOT(triggerItem(const QModelIndex
&)));
424 if (selectionModel() && selectionModel()->currentIndex().isValid())
425 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
430 void DolphinColumnWidget::deactivate()
434 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
435 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
436 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
437 if (KGlobalSettings::singleClick()) {
438 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
439 this, SLOT(triggerItem(const QModelIndex
&)));
441 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
442 this, SLOT(triggerItem(const QModelIndex
&)));
445 const QModelIndex current
= selectionModel()->currentIndex();
446 selectionModel()->clear();
447 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
451 KFileItem
DolphinColumnWidget::itemForIndex(const QModelIndex
& index
) const
453 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
454 return m_dolphinModel
->itemForIndex(dirIndex
);
458 #include "dolphincolumnwidget.moc"