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 "draganddrophelper.h"
31 #include <kcolorscheme.h>
32 #include <kdirlister.h>
33 #include <kfileitem.h>
34 #include <kio/previewjob.h>
35 #include <kiconeffect.h>
37 #include <kmimetyperesolver.h>
38 #include <konqmimedata.h>
40 #include "iconmanager.h"
42 #include <QApplication>
47 DolphinColumnWidget::DolphinColumnWidget(QWidget
* parent
,
48 DolphinColumnView
* columnView
,
64 setMouseTracking(true);
65 viewport()->setAttribute(Qt::WA_Hover
);
66 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
67 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
68 setSelectionBehavior(SelectItems
);
69 setSelectionMode(QAbstractItemView::ExtendedSelection
);
70 setDragDropMode(QAbstractItemView::DragDrop
);
71 setDropIndicatorShown(false);
72 setSelectionRectVisible(true);
74 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
75 // check avoids a division by zero happening on versions before 4.3.1.
76 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
78 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
79 setVerticalScrollMode(QListView::ScrollPerPixel
);
80 setHorizontalScrollMode(QListView::ScrollPerPixel
);
83 // apply the column mode settings to the widget
84 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
85 Q_ASSERT(settings
!= 0);
87 m_font
= QFont(settings
->fontFamily(), settings
->fontSize());
88 m_font
.setItalic(settings
->italicFont());
89 m_font
.setBold(settings
->boldFont());
91 const int iconSize
= settings
->iconSize();
92 m_decorationSize
= QSize(iconSize
, iconSize
);
94 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
95 setItemDelegate(delegate
);
99 connect(this, SIGNAL(viewportEntered()),
100 m_view
->m_controller
, SLOT(emitViewportEntered()));
101 connect(this, SIGNAL(entered(const QModelIndex
&)),
102 this, SLOT(slotEntered(const QModelIndex
&)));
104 //m_dirLister = new DolphinDirLister(); TODO
105 m_dirLister
= new KDirLister();
106 m_dirLister
->setAutoUpdate(true);
107 m_dirLister
->setMainWindow(this);
108 m_dirLister
->setDelayedMimeTypes(true);
109 const bool showHiddenFiles
= m_view
->m_controller
->dolphinView()->showHiddenFiles();
110 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
112 m_dolphinModel
= new DolphinModel(this);
113 m_dolphinModel
->setDirLister(m_dirLister
);
114 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
116 m_proxyModel
= new DolphinSortFilterProxyModel(this);
117 m_proxyModel
->setSourceModel(m_dolphinModel
);
118 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
120 setModel(m_proxyModel
);
121 new KMimeTypeResolver(this, m_dolphinModel
);
122 m_iconManager
= new IconManager(this, m_proxyModel
);
123 m_iconManager
->setShowPreview(m_view
->m_controller
->dolphinView()->showPreview());
125 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
128 DolphinColumnWidget::~DolphinColumnWidget()
132 delete m_dolphinModel
;
134 m_dirLister
= 0; // deleted by m_dolphinModel
137 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
139 m_decorationSize
= size
;
143 void DolphinColumnWidget::setActive(bool active
)
145 if (m_active
== active
) {
158 void DolphinColumnWidget::reload()
161 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
164 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
166 if (show
!= m_dirLister
->showingDotFiles()) {
167 m_dirLister
->setShowingDotFiles(show
);
169 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
173 void DolphinColumnWidget::setShowPreview(bool show
)
175 m_iconManager
->setShowPreview(show
);
178 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
181 void DolphinColumnWidget::updateBackground()
183 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
184 // cleaning up the cut-indication of DolphinColumnWidget with the code from
185 // DolphinView a common helper-class should be available which can be shared
186 // by all view implementations -> no hardcoded value anymore
187 const QPalette::ColorRole role
= viewport()->backgroundRole();
188 QColor color
= viewport()->palette().color(role
);
189 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
191 QPalette palette
= viewport()->palette();
192 palette
.setColor(role
, color
);
193 viewport()->setPalette(palette
);
198 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
200 m_proxyModel
->setFilterRegExp(nameFilter
);
204 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
206 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
207 viewOptions
.font
= m_font
;
208 viewOptions
.decorationSize
= m_decorationSize
;
209 viewOptions
.showDecorationSelected
= true;
213 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
215 DragAndDropHelper::startDrag(this, supportedActions
);
218 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
220 if (event
->mimeData()->hasUrls()) {
221 event
->acceptProposedAction();
227 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
229 QListView::dragLeaveEvent(event
);
231 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
233 setDirtyRegion(m_dropRect
);
236 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
238 QListView::dragMoveEvent(event
);
240 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
241 const QModelIndex index
= indexAt(event
->pos());
242 setDirtyRegion(m_dropRect
);
244 m_dropRect
.setSize(QSize()); // set as invalid
245 if (index
.isValid()) {
246 const KFileItem item
= itemForIndex(index
);
247 if (!item
.isNull() && item
.isDir()) {
248 m_dropRect
= visualRect(index
);
251 setDirtyRegion(m_dropRect
);
253 if (event
->mimeData()->hasUrls()) {
254 // accept url drops, independently from the destination item
255 event
->acceptProposedAction();
259 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
261 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
262 if (!urls
.isEmpty()) {
263 const QModelIndex index
= indexAt(event
->pos());
264 const KFileItem item
= itemForIndex(index
);
265 m_view
->m_controller
->indicateDroppedUrls(urls
,
268 event
->acceptProposedAction();
270 QListView::dropEvent(event
);
274 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
276 if (!m_childUrl
.isEmpty()) {
277 // indicate the shown URL of the next column by highlighting the shown folder item
278 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
279 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
280 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
281 const QRect itemRect
= visualRect(proxyIndex
);
282 QPainter
painter(viewport());
285 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
287 painter
.setPen(Qt::NoPen
);
288 painter
.setBrush(color
);
289 painter
.drawRect(itemRect
);
295 QListView::paintEvent(event
);
297 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
299 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
300 DragAndDropHelper::drawHoverIndication(this, m_dropRect
, brush
);
304 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
306 m_view
->m_controller
->requestActivation();
308 m_view
->requestActivation(this);
309 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
312 QListView::mousePressEvent(event
);
315 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
317 QListView::keyPressEvent(event
);
319 const QItemSelectionModel
* selModel
= selectionModel();
320 const QModelIndex currentIndex
= selModel
->currentIndex();
321 const bool trigger
= currentIndex
.isValid()
322 && (event
->key() == Qt::Key_Return
)
323 && (selModel
->selectedIndexes().count() <= 1);
325 triggerItem(currentIndex
);
329 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
332 m_view
->requestActivation(this);
333 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
336 QListView::contextMenuEvent(event
);
338 const QModelIndex index
= indexAt(event
->pos());
339 if (index
.isValid() || m_active
) {
340 // Only open a context menu above an item or if the mouse is above
341 // the active column.
342 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
343 m_view
->m_controller
->triggerContextMenuRequest(pos
);
347 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
349 QListView::selectionChanged(selected
, deselected
);
351 QItemSelectionModel
* selModel
= m_view
->selectionModel();
352 selModel
->select(selected
, QItemSelectionModel::Select
);
353 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
356 void DolphinColumnWidget::triggerItem(const QModelIndex
& index
)
358 const KFileItem item
= itemForIndex(index
);
359 m_view
->m_controller
->triggerItem(item
);
362 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
364 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
365 const KFileItem item
= m_dolphinModel
->itemForIndex(dirIndex
);
366 m_view
->m_controller
->emitItemEntered(item
);
369 void DolphinColumnWidget::activate()
371 setFocus(Qt::OtherFocusReason
);
373 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
374 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
375 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
376 if (KGlobalSettings::singleClick()) {
377 connect(this, SIGNAL(clicked(const QModelIndex
&)),
378 this, SLOT(triggerItem(const QModelIndex
&)));
380 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
381 this, SLOT(triggerItem(const QModelIndex
&)));
384 if (selectionModel() && selectionModel()->currentIndex().isValid())
385 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
390 void DolphinColumnWidget::deactivate()
394 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
395 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
396 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
397 if (KGlobalSettings::singleClick()) {
398 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
399 this, SLOT(triggerItem(const QModelIndex
&)));
401 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
402 this, SLOT(triggerItem(const QModelIndex
&)));
405 const QModelIndex current
= selectionModel()->currentIndex();
406 selectionModel()->clear();
407 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
411 KFileItem
DolphinColumnWidget::itemForIndex(const QModelIndex
& index
) const
413 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
414 return m_dolphinModel
->itemForIndex(dirIndex
);
418 #include "dolphincolumnwidget.moc"