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
,
65 setMouseTracking(true);
66 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
67 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded
);
68 setSelectionBehavior(SelectItems
);
69 setSelectionMode(QAbstractItemView::ExtendedSelection
);
70 setDragDropMode(QAbstractItemView::DragDrop
);
71 setDropIndicatorShown(false);
72 setSelectionRectVisible(true);
74 setVerticalScrollMode(QListView::ScrollPerPixel
);
75 setHorizontalScrollMode(QListView::ScrollPerPixel
);
77 // apply the column mode settings to the widget
78 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
79 Q_ASSERT(settings
!= 0);
81 if (settings
->useSystemFont()) {
82 m_font
= KGlobalSettings::generalFont();
84 m_font
= QFont(settings
->fontFamily(),
86 settings
->fontWeight(),
87 settings
->italicFont());
90 const int iconSize
= settings
->iconSize();
91 setDecorationSize(QSize(iconSize
, iconSize
));
93 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
94 setItemDelegate(delegate
);
98 connect(this, SIGNAL(viewportEntered()),
99 m_view
->m_controller
, SLOT(emitViewportEntered()));
100 connect(this, SIGNAL(entered(const QModelIndex
&)),
101 this, SLOT(slotEntered(const QModelIndex
&)));
103 //m_dirLister = new DolphinDirLister(); TODO
104 m_dirLister
= new KDirLister();
105 m_dirLister
->setAutoUpdate(true);
106 m_dirLister
->setMainWindow(window());
107 m_dirLister
->setDelayedMimeTypes(true);
108 const bool showHiddenFiles
= m_view
->m_controller
->dolphinView()->showHiddenFiles();
109 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
111 m_dolphinModel
= new DolphinModel(this);
112 m_dolphinModel
->setDirLister(m_dirLister
);
113 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
115 m_proxyModel
= new DolphinSortFilterProxyModel(this);
116 m_proxyModel
->setSourceModel(m_dolphinModel
);
117 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
118 const DolphinView
* dolphinView
= m_view
->m_controller
->dolphinView();
119 m_proxyModel
->setSorting(dolphinView
->sorting());
120 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
122 setModel(m_proxyModel
);
123 const bool useSelManager
= KGlobalSettings::singleClick() &&
124 DolphinSettings::instance().generalSettings()->showSelectionToggle();
126 SelectionManager
* selManager
= new SelectionManager(this);
127 connect(selManager
, SIGNAL(selectionChanged()),
128 this, SLOT(requestActivation()));
129 connect(m_view
->m_controller
, SIGNAL(urlChanged(const KUrl
&)),
130 selManager
, SLOT(reset()));
133 new KMimeTypeResolver(this, m_dolphinModel
);
134 m_iconManager
= new IconManager(this, m_proxyModel
);
135 m_iconManager
->setShowPreview(m_view
->m_controller
->dolphinView()->showPreview());
137 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
139 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
140 this, SLOT(updateFont()));
143 DolphinColumnWidget::~DolphinColumnWidget()
147 delete m_dolphinModel
;
149 m_dirLister
= 0; // deleted by m_dolphinModel
152 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
155 m_decorationSize
= size
;
157 if (m_iconManager
!= 0) {
158 m_iconManager
->updatePreviews();
162 void DolphinColumnWidget::setActive(bool active
)
164 if (m_active
== active
) {
177 void DolphinColumnWidget::reload()
180 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
183 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting
)
185 m_proxyModel
->setSorting(sorting
);
188 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order
)
190 m_proxyModel
->setSortOrder(order
);
193 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
195 if (show
!= m_dirLister
->showingDotFiles()) {
196 m_dirLister
->setShowingDotFiles(show
);
198 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
202 void DolphinColumnWidget::setShowPreview(bool show
)
204 m_iconManager
->setShowPreview(show
);
207 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
210 void DolphinColumnWidget::updateBackground()
212 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
213 // cleaning up the cut-indication of DolphinColumnWidget with the code from
214 // DolphinView a common helper-class should be available which can be shared
215 // by all view implementations -> no hardcoded value anymore
216 const QPalette::ColorRole role
= viewport()->backgroundRole();
217 QColor color
= viewport()->palette().color(role
);
218 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
220 QPalette palette
= viewport()->palette();
221 palette
.setColor(role
, color
);
222 viewport()->setPalette(palette
);
227 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
229 m_proxyModel
->setFilterRegExp(nameFilter
);
232 void DolphinColumnWidget::editItem(const KFileItem
& item
)
234 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(item
);
235 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
236 if (proxyIndex
.isValid()) {
241 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
243 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
244 viewOptions
.font
= m_font
;
245 viewOptions
.decorationSize
= m_decorationSize
;
246 viewOptions
.showDecorationSelected
= true;
250 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
252 DragAndDropHelper::startDrag(this, supportedActions
);
255 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
257 if (event
->mimeData()->hasUrls()) {
258 event
->acceptProposedAction();
262 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
264 QListView::dragLeaveEvent(event
);
265 setDirtyRegion(m_dropRect
);
268 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
270 QListView::dragMoveEvent(event
);
272 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
273 const QModelIndex index
= indexAt(event
->pos());
274 setDirtyRegion(m_dropRect
);
276 m_dropRect
.setSize(QSize()); // set as invalid
277 if (index
.isValid()) {
278 m_view
->m_controller
->setItemView(this);
279 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
280 if (!item
.isNull() && item
.isDir()) {
281 m_dropRect
= visualRect(index
);
284 setDirtyRegion(m_dropRect
);
286 if (event
->mimeData()->hasUrls()) {
287 // accept url drops, independently from the destination item
288 event
->acceptProposedAction();
292 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
294 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
295 if (!urls
.isEmpty()) {
296 const QModelIndex index
= indexAt(event
->pos());
297 m_view
->m_controller
->setItemView(this);
298 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
299 m_view
->m_controller
->indicateDroppedUrls(urls
,
302 event
->acceptProposedAction();
304 QListView::dropEvent(event
);
307 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
309 if (!m_childUrl
.isEmpty()) {
310 // indicate the shown URL of the next column by highlighting the shown folder item
311 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
312 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
313 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
314 const QRect itemRect
= visualRect(proxyIndex
);
315 QPainter
painter(viewport());
316 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
318 painter
.setPen(Qt::NoPen
);
319 painter
.setBrush(color
);
320 painter
.drawRect(itemRect
);
324 QListView::paintEvent(event
);
327 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
330 QListView::mousePressEvent(event
);
333 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
335 QListView::keyPressEvent(event
);
337 m_view
->m_controller
->handleKeyPressEvent(event
);
340 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
343 m_view
->requestActivation(this);
344 Q_ASSERT(m_view
->m_controller
->itemView() == this);
345 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
348 QListView::contextMenuEvent(event
);
350 const QModelIndex index
= indexAt(event
->pos());
351 if (index
.isValid() || m_active
) {
352 // Only open a context menu above an item or if the mouse is above
353 // the active column.
354 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
355 Q_ASSERT(m_view
->m_controller
->itemView() == this);
356 m_view
->m_controller
->triggerContextMenuRequest(pos
);
360 void DolphinColumnWidget::wheelEvent(QWheelEvent
* event
)
362 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
363 if (event
->modifiers() & Qt::ControlModifier
) {
367 QListView::wheelEvent(event
);
370 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
372 QListView::selectionChanged(selected
, deselected
);
374 QItemSelectionModel
* selModel
= m_view
->selectionModel();
375 selModel
->select(selected
, QItemSelectionModel::Select
);
376 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
379 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
381 m_view
->m_controller
->setItemView(this);
382 m_view
->m_controller
->emitItemEntered(index
);
385 void DolphinColumnWidget::requestActivation()
387 m_view
->m_controller
->setItemView(this);
388 m_view
->m_controller
->requestActivation();
390 m_view
->requestActivation(this);
391 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
392 selectionModel()->clear();
396 void DolphinColumnWidget::updateFont()
398 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
399 Q_ASSERT(settings
!= 0);
401 if (settings
->useSystemFont()) {
402 m_font
= KGlobalSettings::generalFont();
406 void DolphinColumnWidget::activate()
408 setFocus(Qt::OtherFocusReason
);
410 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
411 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
412 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
413 if (KGlobalSettings::singleClick()) {
414 connect(this, SIGNAL(clicked(const QModelIndex
&)),
415 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
417 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
418 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
421 if (selectionModel() && selectionModel()->currentIndex().isValid())
422 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
427 void DolphinColumnWidget::deactivate()
431 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
432 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
433 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
434 if (KGlobalSettings::singleClick()) {
435 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
436 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
438 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
439 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
442 const QModelIndex current
= selectionModel()->currentIndex();
443 selectionModel()->clear();
444 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
448 #include "dolphincolumnwidget.moc"