]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnwidget.cpp
3 /***************************************************************************
4 * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
22 #include "dolphincolumnwidget.h"
24 #include "dolphinmodel.h"
25 #include "dolphincolumnview.h"
26 #include "dolphincontroller.h"
27 #include "dolphindirlister.h"
28 #include "dolphinsortfilterproxymodel.h"
29 #include "settings/dolphinsettings.h"
30 #include "dolphinviewautoscroller.h"
31 #include "dolphin_columnmodesettings.h"
32 #include "dolphin_generalsettings.h"
33 #include "draganddrophelper.h"
34 #include "folderexpander.h"
35 #include "selectionmanager.h"
36 #include "tooltips/tooltipmanager.h"
37 #include "versioncontrolobserver.h"
39 #include <kcolorscheme.h>
40 #include <kdirlister.h>
41 #include <kfileitem.h>
42 #include <kfilepreviewgenerator.h>
43 #include <kio/previewjob.h>
44 #include <kiconeffect.h>
46 #include <konqmimedata.h>
48 #include <QApplication>
56 DolphinColumnWidget::DolphinColumnWidget(QWidget* parent,
57 DolphinColumnView* columnView,
62 m_selectionManager(0),
71 m_previewGenerator(0),
75 setMouseTracking(true);
76 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
77 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
78 setSelectionBehavior(SelectItems);
79 setSelectionMode(QAbstractItemView::ExtendedSelection);
80 setDragDropMode(QAbstractItemView::DragDrop);
81 setDropIndicatorShown(false);
82 setSelectionRectVisible(true);
83 setEditTriggers(QAbstractItemView::NoEditTriggers);
85 setVerticalScrollMode(QListView::ScrollPerPixel);
86 setHorizontalScrollMode(QListView::ScrollPerPixel);
88 m_autoScroller = new DolphinViewAutoScroller(this);
90 // apply the column mode settings to the widget
91 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
92 Q_ASSERT(settings != 0);
94 if (settings->useSystemFont()) {
95 m_font = KGlobalSettings::generalFont();
97 m_font = QFont(settings->fontFamily(),
99 settings->fontWeight(),
100 settings->italicFont());
103 KFileItemDelegate* delegate = new KFileItemDelegate(this);
104 delegate->setShowToolTipWhenElided(false);
105 setItemDelegate(delegate);
109 connect(this, SIGNAL(viewportEntered()),
110 m_view->m_controller, SLOT(emitViewportEntered()));
111 connect(this, SIGNAL(entered(const QModelIndex&)),
112 this, SLOT(slotEntered(const QModelIndex&)));
114 const DolphinView* dolphinView = m_view->m_controller->dolphinView();
115 connect(dolphinView, SIGNAL(sortingChanged(DolphinView::Sorting)),
116 this, SLOT(slotSortingChanged(DolphinView::Sorting)));
117 connect(dolphinView, SIGNAL(sortOrderChanged(Qt::SortOrder)),
118 this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
119 connect(dolphinView, SIGNAL(sortFoldersFirstChanged(bool)),
120 this, SLOT(slotSortFoldersFirstChanged(bool)));
121 connect(dolphinView, SIGNAL(showHiddenFilesChanged()),
122 this, SLOT(slotShowHiddenFilesChanged()));
123 connect(dolphinView, SIGNAL(showPreviewChanged()),
124 this, SLOT(slotShowPreviewChanged()));
126 m_dirLister = new DolphinDirLister();
127 m_dirLister->setAutoUpdate(true);
128 m_dirLister->setMainWindow(window());
129 m_dirLister->setDelayedMimeTypes(true);
130 const bool showHiddenFiles = m_view->m_controller->dolphinView()->showHiddenFiles();
131 m_dirLister->setShowingDotFiles(showHiddenFiles);
133 m_dolphinModel = new DolphinModel(this);
134 m_dolphinModel->setDirLister(m_dirLister);
135 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
137 m_proxyModel = new DolphinSortFilterProxyModel(this);
138 m_proxyModel->setSourceModel(m_dolphinModel);
139 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
141 m_proxyModel->setSorting(dolphinView->sorting());
142 m_proxyModel->setSortOrder(dolphinView->sortOrder());
143 m_proxyModel->setSortFoldersFirst(dolphinView->sortFoldersFirst());
145 setModel(m_proxyModel);
147 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
148 m_selectionManager = new SelectionManager(this);
149 connect(m_selectionManager, SIGNAL(selectionChanged()),
150 this, SLOT(requestActivation()));
151 connect(m_view->m_controller, SIGNAL(urlChanged(const KUrl&)),
152 m_selectionManager, SLOT(reset()));
155 //m_previewGenerator = new KFilePreviewGenerator(this);
156 //m_previewGenerator->setPreviewShown(m_view->m_controller->dolphinView()->showPreview());
158 //if (DolphinSettings::instance().generalSettings()->showToolTips()) {
159 // m_toolTipManager = new ToolTipManager(this, m_proxyModel);
162 //m_dirLister->openUrl(url, KDirLister::NoFlags);
164 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
165 this, SLOT(updateFont()));
167 /*FolderExpander* folderExpander = new FolderExpander(this, m_proxyModel);
168 folderExpander->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
169 connect (folderExpander, SIGNAL(enterDir(const QModelIndex&)),
170 m_view->m_controller, SLOT(triggerItem(const QModelIndex&)));
172 new VersionControlObserver(this);*/
174 updateDecorationSize(m_view->m_controller->dolphinView()->showPreview());
177 DolphinColumnWidget::~DolphinColumnWidget()
181 delete m_dolphinModel;
183 m_dirLister = 0; // deleted by m_dolphinModel
186 void DolphinColumnWidget::setActive(bool active)
188 if (active && (m_view->focusProxy() != this)) {
189 m_view->setFocusProxy(this);
192 if (m_active != active) {
203 /*void DolphinColumnWidget::reload()
206 m_dirLister->openUrl(m_url, KDirLister::Reload);
209 /*void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting)
211 m_proxyModel->setSorting(sorting);
214 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order)
216 m_proxyModel->setSortOrder(order);
219 void DolphinColumnWidget::setSortFoldersFirst(bool foldersFirst)
221 m_proxyModel->setSortFoldersFirst(foldersFirst);
224 void DolphinColumnWidget::setShowHiddenFiles(bool show)
226 if (show != m_dirLister->showingDotFiles()) {
227 m_dirLister->setShowingDotFiles(show);
229 m_dirLister->openUrl(m_url, KDirLister::Reload);
233 void DolphinColumnWidget::setShowPreview(bool show)
235 m_previewGenerator->setPreviewShown(show);
238 m_dirLister->openUrl(m_url, KDirLister::Reload);
241 void DolphinColumnWidget::updateBackground()
243 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
244 // cleaning up the cut-indication of DolphinColumnWidget with the code from
245 // DolphinView a common helper-class should be available which can be shared
246 // by all view implementations -> no hardcoded value anymore
247 const QPalette::ColorRole role = viewport()->backgroundRole();
248 QColor color = viewport()->palette().color(role);
249 color.setAlpha((m_active && m_view->m_active) ? 255 : 150);
251 QPalette palette = viewport()->palette();
252 palette.setColor(role, color);
253 viewport()->setPalette(palette);
258 void DolphinColumnWidget::setNameFilter(const QString& nameFilter)
260 m_proxyModel->setFilterRegExp(nameFilter);
263 void DolphinColumnWidget::editItem(const KFileItem& item)
265 const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
266 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
267 if (proxyIndex.isValid()) {
272 KFileItem DolphinColumnWidget::itemAt(const QPoint& pos) const
275 const QModelIndex index = indexAt(pos);
276 if (index.isValid() && (index.column() == DolphinModel::Name)) {
277 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
278 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
283 KFileItemList DolphinColumnWidget::selectedItems() const
285 const QItemSelection selection = m_proxyModel->mapSelectionToSource(selectionModel()->selection());
286 KFileItemList itemList;
288 const QModelIndexList indexList = selection.indexes();
289 foreach (const QModelIndex &index, indexList) {
290 KFileItem item = m_dolphinModel->itemForIndex(index);
291 if (!item.isNull()) {
292 itemList.append(item);
299 QMimeData* DolphinColumnWidget::selectionMimeData() const
301 const QItemSelection selection = m_proxyModel->mapSelectionToSource(selectionModel()->selection());
302 return m_dolphinModel->mimeData(selection.indexes());
305 QStyleOptionViewItem DolphinColumnWidget::viewOptions() const
307 QStyleOptionViewItem viewOptions = QListView::viewOptions();
308 viewOptions.font = m_font;
309 viewOptions.decorationSize = m_decorationSize;
310 viewOptions.showDecorationSelected = true;
314 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions)
316 DragAndDropHelper::instance().startDrag(this, supportedActions, m_view->m_controller);
319 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent* event)
321 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
322 event->acceptProposedAction();
327 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
329 QListView::dragLeaveEvent(event);
330 setDirtyRegion(m_dropRect);
333 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent* event)
335 QListView::dragMoveEvent(event);
337 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
338 const QModelIndex index = indexAt(event->pos());
339 setDirtyRegion(m_dropRect);
341 m_dropRect.setSize(QSize()); // set as invalid
342 if (index.isValid()) {
343 m_view->m_controller->setItemView(this);
344 const KFileItem item = m_view->m_controller->itemForIndex(index);
345 if (!item.isNull() && item.isDir()) {
346 m_dropRect = visualRect(index);
349 setDirtyRegion(m_dropRect);
351 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
352 // accept url drops, independently from the destination item
353 event->acceptProposedAction();
357 void DolphinColumnWidget::dropEvent(QDropEvent* event)
359 const QModelIndex index = indexAt(event->pos());
360 m_view->m_controller->setItemView(this);
361 const KFileItem item = m_view->m_controller->itemForIndex(index);
362 m_view->m_controller->indicateDroppedUrls(item, url(), event);
363 QListView::dropEvent(event);
366 void DolphinColumnWidget::paintEvent(QPaintEvent* event)
368 if (!m_childUrl.isEmpty()) {
369 // indicate the shown URL of the next column by highlighting the shown folder item
370 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
371 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
372 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
373 const QRect itemRect = visualRect(proxyIndex);
374 QPainter painter(viewport());
375 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
377 painter.setPen(Qt::NoPen);
378 painter.setBrush(color);
379 painter.drawRect(itemRect);
383 QListView::paintEvent(event);
386 void DolphinColumnWidget::mousePressEvent(QMouseEvent* event)
389 if (!indexAt(event->pos()).isValid()) {
390 if (QApplication::mouseButtons() & Qt::MidButton) {
391 m_view->m_controller->replaceUrlByClipboard();
393 } else if (event->button() == Qt::LeftButton) {
394 // TODO: see comment in DolphinIconsView::mousePressEvent()
395 setState(QAbstractItemView::DraggingState);
397 QListView::mousePressEvent(event);
400 void DolphinColumnWidget::keyPressEvent(QKeyEvent* event)
402 QListView::keyPressEvent(event);
405 DolphinController* controller = m_view->m_controller;
406 controller->handleKeyPressEvent(event);
407 switch (event->key()) {
408 case Qt::Key_Right: {
409 // Special key handling for the column: A Key_Right should
410 // open a new column for the currently selected folder.
411 const QModelIndex index = currentIndex();
412 const KFileItem item = controller->itemForIndex(index);
413 if (!item.isNull() && item.isDir()) {
414 controller->emitItemTriggered(item);
420 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
421 QItemSelectionModel::Current |
422 QItemSelectionModel::Clear);
429 if (m_toolTipManager != 0) {
430 m_toolTipManager->hideTip();
434 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent* event)
437 m_view->requestActivation(this);
438 Q_ASSERT(m_view->m_controller->itemView() == this);
439 m_view->m_controller->triggerUrlChangeRequest(m_url);
443 QListView::contextMenuEvent(event);
445 const QModelIndex index = indexAt(event->pos());
446 if (!index.isValid()) {
450 if (m_toolTipManager != 0) {
451 m_toolTipManager->hideTip();
454 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
455 Q_ASSERT(m_view->m_controller->itemView() == this);
456 m_view->m_controller->triggerContextMenuRequest(pos);
459 void DolphinColumnWidget::wheelEvent(QWheelEvent* event)
461 if (m_selectionManager != 0) {
462 m_selectionManager->reset();
465 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
466 if (event->modifiers() & Qt::ControlModifier) {
471 const int height = m_decorationSize.height();
472 const int step = (height >= KIconLoader::SizeHuge) ? height / 10 : (KIconLoader::SizeHuge - height) / 2;
473 verticalScrollBar()->setSingleStep(step);
475 QListView::wheelEvent(event);
478 void DolphinColumnWidget::leaveEvent(QEvent* event)
480 QListView::leaveEvent(event);
481 // if the mouse is above an item and moved very fast outside the widget,
482 // no viewportEntered() signal might be emitted although the mouse has been moved
483 // above the viewport
484 m_view->m_controller->emitViewportEntered();
487 void DolphinColumnWidget::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
489 QListView::selectionChanged(selected, deselected);
491 QItemSelectionModel* selModel = m_view->selectionModel();
492 //selModel->select(selected, QItemSelectionModel::Select);
493 //selModel->select(deselected, QItemSelectionModel::Deselect);
496 void DolphinColumnWidget::currentChanged(const QModelIndex& current, const QModelIndex& previous)
498 QListView::currentChanged(current, previous);
499 m_autoScroller->handleCurrentIndexChange(current, previous);
502 void DolphinColumnWidget::slotEntered(const QModelIndex& index)
504 m_view->m_controller->setItemView(this);
505 m_view->m_controller->emitItemEntered(index);
508 void DolphinColumnWidget::requestActivation()
510 m_view->m_controller->setItemView(this);
511 m_view->m_controller->requestActivation();
513 m_view->requestActivation(this);
514 m_view->m_controller->triggerUrlChangeRequest(m_url);
515 selectionModel()->clear();
519 void DolphinColumnWidget::updateFont()
521 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
522 Q_ASSERT(settings != 0);
524 if (settings->useSystemFont()) {
525 m_font = KGlobalSettings::generalFont();
529 void DolphinColumnWidget::slotShowPreviewChanged()
531 kDebug() << "--- slotpreviewchanged";
532 const DolphinView* view = m_view->m_controller->dolphinView();
533 updateDecorationSize(view->showPreview());
536 void DolphinColumnWidget::activate()
538 setFocus(Qt::OtherFocusReason);
540 if (KGlobalSettings::singleClick()) {
541 connect(this, SIGNAL(clicked(const QModelIndex&)),
542 m_view->m_controller, SLOT(triggerItem(const QModelIndex&)));
544 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
545 m_view->m_controller, SLOT(triggerItem(const QModelIndex&)));
548 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
549 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent);
555 void DolphinColumnWidget::deactivate()
558 if (KGlobalSettings::singleClick()) {
559 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
560 m_view->m_controller, SLOT(triggerItem(const QModelIndex&)));
562 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
563 m_view->m_controller, SLOT(triggerItem(const QModelIndex&)));
566 const QModelIndex current = selectionModel()->currentIndex();
567 selectionModel()->clear();
568 selectionModel()->setCurrentIndex(current, QItemSelectionModel::NoUpdate);
572 void DolphinColumnWidget::updateDecorationSize(bool showPreview)
574 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
575 const int iconSize = showPreview ? settings->previewSize() : settings->iconSize();
576 const QSize size(iconSize, iconSize);
579 m_decorationSize = size;
581 if (m_selectionManager != 0) {
582 m_selectionManager->reset();
588 #include "dolphincolumnwidget.moc"