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 viewport()->setAttribute(Qt::WA_Hover
);
67 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
68 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded
);
69 setSelectionBehavior(SelectItems
);
70 setSelectionMode(QAbstractItemView::ExtendedSelection
);
71 setDragDropMode(QAbstractItemView::DragDrop
);
72 setDropIndicatorShown(false);
73 setSelectionRectVisible(true);
75 setVerticalScrollMode(QListView::ScrollPerPixel
);
76 setHorizontalScrollMode(QListView::ScrollPerPixel
);
78 // apply the column mode settings to the widget
79 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
80 Q_ASSERT(settings
!= 0);
82 if (settings
->useSystemFont()) {
83 m_font
= KGlobalSettings::generalFont();
85 m_font
= QFont(settings
->fontFamily(),
87 settings
->fontWeight(),
88 settings
->italicFont());
91 const int iconSize
= settings
->iconSize();
92 setDecorationSize(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(window());
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
);
119 const DolphinView
* dolphinView
= m_view
->m_controller
->dolphinView();
120 m_proxyModel
->setSorting(dolphinView
->sorting());
121 m_proxyModel
->setSortOrder(dolphinView
->sortOrder());
123 setModel(m_proxyModel
);
124 const bool useSelManager
= KGlobalSettings::singleClick() &&
125 DolphinSettings::instance().generalSettings()->showSelectionToggle();
127 SelectionManager
* selManager
= new SelectionManager(this);
128 connect(selManager
, SIGNAL(selectionChanged()),
129 this, SLOT(requestActivation()));
130 connect(m_view
->m_controller
, SIGNAL(urlChanged(const KUrl
&)),
131 selManager
, SLOT(reset()));
134 new KMimeTypeResolver(this, m_dolphinModel
);
135 m_iconManager
= new IconManager(this, m_proxyModel
);
136 m_iconManager
->setShowPreview(m_view
->m_controller
->dolphinView()->showPreview());
138 m_dirLister
->openUrl(url
, KDirLister::NoFlags
);
140 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
141 this, SLOT(updateFont()));
144 DolphinColumnWidget::~DolphinColumnWidget()
148 delete m_dolphinModel
;
150 m_dirLister
= 0; // deleted by m_dolphinModel
153 void DolphinColumnWidget::setDecorationSize(const QSize
& size
)
156 m_decorationSize
= size
;
158 if (m_iconManager
!= 0) {
159 m_iconManager
->updatePreviews();
163 void DolphinColumnWidget::setActive(bool active
)
165 if (m_active
== active
) {
178 void DolphinColumnWidget::reload()
181 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
184 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting
)
186 m_proxyModel
->setSorting(sorting
);
189 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order
)
191 m_proxyModel
->setSortOrder(order
);
194 void DolphinColumnWidget::setShowHiddenFiles(bool show
)
196 if (show
!= m_dirLister
->showingDotFiles()) {
197 m_dirLister
->setShowingDotFiles(show
);
199 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
203 void DolphinColumnWidget::setShowPreview(bool show
)
205 m_iconManager
->setShowPreview(show
);
208 m_dirLister
->openUrl(m_url
, KDirLister::Reload
);
211 void DolphinColumnWidget::updateBackground()
213 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
214 // cleaning up the cut-indication of DolphinColumnWidget with the code from
215 // DolphinView a common helper-class should be available which can be shared
216 // by all view implementations -> no hardcoded value anymore
217 const QPalette::ColorRole role
= viewport()->backgroundRole();
218 QColor color
= viewport()->palette().color(role
);
219 color
.setAlpha((m_active
&& m_view
->m_active
) ? 255 : 150);
221 QPalette palette
= viewport()->palette();
222 palette
.setColor(role
, color
);
223 viewport()->setPalette(palette
);
228 void DolphinColumnWidget::setNameFilter(const QString
& nameFilter
)
230 m_proxyModel
->setFilterRegExp(nameFilter
);
234 QStyleOptionViewItem
DolphinColumnWidget::viewOptions() const
236 QStyleOptionViewItem viewOptions
= QListView::viewOptions();
237 viewOptions
.font
= m_font
;
238 viewOptions
.decorationSize
= m_decorationSize
;
239 viewOptions
.showDecorationSelected
= true;
243 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions
)
245 DragAndDropHelper::startDrag(this, supportedActions
);
248 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
250 if (event
->mimeData()->hasUrls()) {
251 event
->acceptProposedAction();
255 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
257 QListView::dragLeaveEvent(event
);
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 m_view
->m_controller
->setItemView(this);
272 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
273 if (!item
.isNull() && item
.isDir()) {
274 m_dropRect
= visualRect(index
);
277 setDirtyRegion(m_dropRect
);
279 if (event
->mimeData()->hasUrls()) {
280 // accept url drops, independently from the destination item
281 event
->acceptProposedAction();
285 void DolphinColumnWidget::dropEvent(QDropEvent
* event
)
287 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
288 if (!urls
.isEmpty()) {
289 const QModelIndex index
= indexAt(event
->pos());
290 m_view
->m_controller
->setItemView(this);
291 const KFileItem item
= m_view
->m_controller
->itemForIndex(index
);
292 m_view
->m_controller
->indicateDroppedUrls(urls
,
295 event
->acceptProposedAction();
297 QListView::dropEvent(event
);
300 void DolphinColumnWidget::paintEvent(QPaintEvent
* event
)
302 if (!m_childUrl
.isEmpty()) {
303 // indicate the shown URL of the next column by highlighting the shown folder item
304 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_childUrl
);
305 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
306 if (proxyIndex
.isValid() && !selectionModel()->isSelected(proxyIndex
)) {
307 const QRect itemRect
= visualRect(proxyIndex
);
308 QPainter
painter(viewport());
311 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).foreground().color();
313 painter
.setPen(Qt::NoPen
);
314 painter
.setBrush(color
);
315 painter
.drawRect(itemRect
);
321 QListView::paintEvent(event
);
324 void DolphinColumnWidget::mousePressEvent(QMouseEvent
* event
)
327 QListView::mousePressEvent(event
);
330 void DolphinColumnWidget::keyPressEvent(QKeyEvent
* event
)
332 QListView::keyPressEvent(event
);
334 m_view
->m_controller
->handleKeyPressEvent(event
);
337 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
340 m_view
->requestActivation(this);
341 Q_ASSERT(m_view
->m_controller
->itemView() == this);
342 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
345 QListView::contextMenuEvent(event
);
347 const QModelIndex index
= indexAt(event
->pos());
348 if (index
.isValid() || m_active
) {
349 // Only open a context menu above an item or if the mouse is above
350 // the active column.
351 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
352 Q_ASSERT(m_view
->m_controller
->itemView() == this);
353 m_view
->m_controller
->triggerContextMenuRequest(pos
);
357 void DolphinColumnWidget::wheelEvent(QWheelEvent
* event
)
359 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
360 if (event
->modifiers() & Qt::ControlModifier
) {
364 QListView::wheelEvent(event
);
367 void DolphinColumnWidget::selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
369 QListView::selectionChanged(selected
, deselected
);
371 QItemSelectionModel
* selModel
= m_view
->selectionModel();
372 selModel
->select(selected
, QItemSelectionModel::Select
);
373 selModel
->select(deselected
, QItemSelectionModel::Deselect
);
376 void DolphinColumnWidget::slotEntered(const QModelIndex
& index
)
378 m_view
->m_controller
->setItemView(this);
379 m_view
->m_controller
->emitItemEntered(index
);
382 void DolphinColumnWidget::requestActivation()
384 m_view
->m_controller
->setItemView(this);
385 m_view
->m_controller
->requestActivation();
387 m_view
->requestActivation(this);
388 m_view
->m_controller
->triggerUrlChangeRequest(m_url
);
389 selectionModel()->clear();
393 void DolphinColumnWidget::updateFont()
395 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
396 Q_ASSERT(settings
!= 0);
398 if (settings
->useSystemFont()) {
399 m_font
= KGlobalSettings::generalFont();
403 void DolphinColumnWidget::activate()
405 setFocus(Qt::OtherFocusReason
);
407 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
408 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
409 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
410 if (KGlobalSettings::singleClick()) {
411 connect(this, SIGNAL(clicked(const QModelIndex
&)),
412 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
414 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
415 m_view
->m_controller
, SLOT(triggerItem(const QModelIndex
&)));
418 if (selectionModel() && selectionModel()->currentIndex().isValid())
419 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent
);
424 void DolphinColumnWidget::deactivate()
428 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
429 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
430 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
431 if (KGlobalSettings::singleClick()) {
432 disconnect(this, SIGNAL(clicked(const QModelIndex
&)),
433 this, SLOT(triggerItem(const QModelIndex
&)));
435 disconnect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
436 this, SLOT(triggerItem(const QModelIndex
&)));
439 const QModelIndex current
= selectionModel()->currentIndex();
440 selectionModel()->clear();
441 selectionModel()->setCurrentIndex(current
, QItemSelectionModel::NoUpdate
);
445 #include "dolphincolumnwidget.moc"