]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Provide a 'int selectedItemsCount() const' interface for performance reasons, so...
[dolphin.git] / src / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphinview.h"
22
23 #include <QApplication>
24 #include <QClipboard>
25 #include <QKeyEvent>
26 #include <QItemSelection>
27 #include <QBoxLayout>
28 #include <QTimer>
29 #include <QScrollBar>
30
31 #include <kactioncollection.h>
32 #include <kcolorscheme.h>
33 #include <kdirlister.h>
34 #include <kfileitemdelegate.h>
35 #include <kiconeffect.h>
36 #include <klocale.h>
37 #include <kio/deletejob.h>
38 #include <kio/netaccess.h>
39 #include <kio/previewjob.h>
40 #include <kjob.h>
41 #include <kmenu.h>
42 #include <kmessagebox.h>
43 #include <kmimetyperesolver.h>
44 #include <konq_fileitemcapabilities.h>
45 #include <konq_operations.h>
46 #include <konqmimedata.h>
47 #include <ktoggleaction.h>
48 #include <kurl.h>
49
50 #include "dolphindropcontroller.h"
51 #include "dolphinmodel.h"
52 #include "dolphincolumnview.h"
53 #include "dolphincontroller.h"
54 #include "dolphinsortfilterproxymodel.h"
55 #include "dolphindetailsview.h"
56 #include "dolphiniconsview.h"
57 #include "dolphinsettings.h"
58 #include "dolphin_generalsettings.h"
59 #include "iconmanager.h"
60 #include "renamedialog.h"
61 #include "tooltipmanager.h"
62 #include "viewproperties.h"
63
64 DolphinView::DolphinView(QWidget* parent,
65 const KUrl& url,
66 KDirLister* dirLister,
67 DolphinModel* dolphinModel,
68 DolphinSortFilterProxyModel* proxyModel) :
69 QWidget(parent),
70 m_active(true),
71 m_showPreview(false),
72 m_loadingDirectory(false),
73 m_storedCategorizedSorting(false),
74 m_tabsForFiles(false),
75 m_isContextMenuOpen(false),
76 m_mode(DolphinView::IconsView),
77 m_topLayout(0),
78 m_controller(0),
79 m_iconsView(0),
80 m_detailsView(0),
81 m_columnView(0),
82 m_fileItemDelegate(0),
83 m_selectionModel(0),
84 m_dolphinModel(dolphinModel),
85 m_dirLister(dirLister),
86 m_proxyModel(proxyModel),
87 m_iconManager(0),
88 m_toolTipManager(0),
89 m_rootUrl(),
90 m_currentItemUrl()
91 {
92 m_topLayout = new QVBoxLayout(this);
93 m_topLayout->setSpacing(0);
94 m_topLayout->setMargin(0);
95
96 m_controller = new DolphinController(this);
97 m_controller->setUrl(url);
98
99 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
100 this, SIGNAL(urlChanged(const KUrl&)));
101 connect(m_controller, SIGNAL(requestUrlChange(const KUrl&)),
102 this, SLOT(slotRequestUrlChange(const KUrl&)));
103
104 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
105 this, SLOT(openContextMenu(const QPoint&)));
106 connect(m_controller, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&, const KFileItem&)),
107 this, SLOT(dropUrls(const KUrl::List&, const KUrl&, const KFileItem&)));
108 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
109 this, SLOT(updateSorting(DolphinView::Sorting)));
110 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
111 this, SLOT(updateSortOrder(Qt::SortOrder)));
112 connect(m_controller, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
113 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList&)));
114 connect(m_controller, SIGNAL(itemTriggered(const KFileItem&)),
115 this, SLOT(triggerItem(const KFileItem&)));
116 connect(m_controller, SIGNAL(tabRequested(const KUrl&)),
117 this, SIGNAL(tabRequested(const KUrl&)));
118 connect(m_controller, SIGNAL(activated()),
119 this, SLOT(activate()));
120 connect(m_controller, SIGNAL(itemEntered(const KFileItem&)),
121 this, SLOT(showHoverInformation(const KFileItem&)));
122 connect(m_controller, SIGNAL(viewportEntered()),
123 this, SLOT(clearHoverInformation()));
124
125 connect(m_dirLister, SIGNAL(redirection(KUrl, KUrl)),
126 this, SLOT(slotRedirection(KUrl, KUrl)));
127 connect(m_dirLister, SIGNAL(completed()),
128 this, SLOT(restoreCurrentItem()));
129
130 applyViewProperties(url);
131 m_topLayout->addWidget(itemView());
132 }
133
134 DolphinView::~DolphinView()
135 {
136 }
137
138 const KUrl& DolphinView::url() const
139 {
140 return m_controller->url();
141 }
142
143 KUrl DolphinView::rootUrl() const
144 {
145 return isColumnViewActive() ? m_columnView->rootUrl() : url();
146 }
147
148 void DolphinView::setActive(bool active)
149 {
150 if (active == m_active) {
151 return;
152 }
153
154 m_active = active;
155
156 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
157 if (active) {
158 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
159 // bypasses the problem when having a split view and changing the active view to
160 // update the some URL dependent states. A nicer approach should be no big deal...
161 emit urlChanged(url());
162 emit selectionChanged(selectedItems());
163 } else {
164 color.setAlpha(150);
165 }
166
167 QWidget* viewport = itemView()->viewport();
168 QPalette palette;
169 palette.setColor(viewport->backgroundRole(), color);
170 viewport->setPalette(palette);
171
172 update();
173
174 if (active) {
175 itemView()->setFocus();
176 emit activated();
177 }
178
179 m_controller->indicateActivationChange(active);
180 }
181
182 bool DolphinView::isActive() const
183 {
184 return m_active;
185 }
186
187 void DolphinView::setMode(Mode mode)
188 {
189 if (mode == m_mode) {
190 return; // the wished mode is already set
191 }
192
193 m_mode = mode;
194
195 deleteView();
196
197 const KUrl viewPropsUrl = viewPropertiesUrl();
198 ViewProperties props(viewPropsUrl);
199 props.setViewMode(m_mode);
200 createView();
201
202 // the file item delegate has been recreated, apply the current
203 // additional information manually
204 const KFileItemDelegate::InformationList infoList = props.additionalInfo();
205 m_fileItemDelegate->setShowInformation(infoList);
206 emit additionalInfoChanged();
207
208 // Not all view modes support categorized sorting. Adjust the sorting model
209 // if changing the view mode results in a change of the categorized sorting
210 // capabilities.
211 m_storedCategorizedSorting = props.categorizedSorting();
212 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
213 if (categorized != m_proxyModel->isCategorizedModel()) {
214 m_proxyModel->setCategorizedModel(categorized);
215 emit categorizedSortingChanged();
216 }
217
218 emit modeChanged();
219 }
220
221 DolphinView::Mode DolphinView::mode() const
222 {
223 return m_mode;
224 }
225
226 bool DolphinView::showPreview() const
227 {
228 return m_showPreview;
229 }
230
231 bool DolphinView::showHiddenFiles() const
232 {
233 return m_dirLister->showingDotFiles();
234 }
235
236 bool DolphinView::categorizedSorting() const
237 {
238 // If all view modes would support categorized sorting, returning
239 // m_proxyModel->isCategorizedModel() would be the way to go. As
240 // currently only the icons view supports caterized sorting, we remember
241 // the stored view properties state in m_storedCategorizedSorting and
242 // return this state. The application takes care to disable the corresponding
243 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
244 // that this setting is not applied to the current view mode.
245 return m_storedCategorizedSorting;
246 }
247
248 bool DolphinView::supportsCategorizedSorting() const
249 {
250 return m_iconsView != 0;
251 }
252
253 void DolphinView::selectAll()
254 {
255 QAbstractItemView* view = itemView();
256 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
257 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
258 // selection instead of selecting all items. This is bypassed for KDE 4.0
259 // by invoking clearSelection() first.
260 view->clearSelection();
261 view->selectAll();
262 }
263
264 void DolphinView::invertSelection()
265 {
266 if (isColumnViewActive()) {
267 // QAbstractItemView does not offer a virtual method invertSelection()
268 // as counterpart to QAbstractItemView::selectAll(). This makes it
269 // necessary to delegate the inverting of the selection to the
270 // column view, as only the selection of the active column should
271 // get inverted.
272 m_columnView->invertSelection();
273 } else {
274 QItemSelectionModel* selectionModel = itemView()->selectionModel();
275 const QAbstractItemModel* itemModel = selectionModel->model();
276
277 const QModelIndex topLeft = itemModel->index(0, 0);
278 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
279 itemModel->columnCount() - 1);
280
281 const QItemSelection selection(topLeft, bottomRight);
282 selectionModel->select(selection, QItemSelectionModel::Toggle);
283 }
284 }
285
286 bool DolphinView::hasSelection() const
287 {
288 return itemView()->selectionModel()->hasSelection();
289 }
290
291 void DolphinView::clearSelection()
292 {
293 itemView()->selectionModel()->clear();
294 }
295
296 KFileItemList DolphinView::selectedItems() const
297 {
298 if (isColumnViewActive()) {
299 return m_columnView->selectedItems();
300 }
301
302 const QAbstractItemView* view = itemView();
303
304 // Our view has a selection, we will map them back to the DolphinModel
305 // and then fill the KFileItemList.
306 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
307
308 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
309 KFileItemList itemList;
310
311 const QModelIndexList indexList = selection.indexes();
312 foreach (const QModelIndex &index, indexList) {
313 KFileItem item = m_dolphinModel->itemForIndex(index);
314 if (!item.isNull()) {
315 itemList.append(item);
316 }
317 }
318
319 return itemList;
320 }
321
322 KUrl::List DolphinView::selectedUrls() const
323 {
324 KUrl::List urls;
325 const KFileItemList list = selectedItems();
326 foreach (const KFileItem &item, list) {
327 urls.append(item.url());
328 }
329 return urls;
330 }
331
332 int DolphinView::selectedItemsCount() const
333 {
334 if (isColumnViewActive()) {
335 // TODO: get rid of this special case by adjusting the dir lister
336 // to the current column
337 return m_columnView->selectedItems().count();
338 }
339
340 return itemView()->selectionModel()->selection().count();
341 }
342
343 void DolphinView::setContentsPosition(int x, int y)
344 {
345 QAbstractItemView* view = itemView();
346
347 // the ColumnView takes care itself for the horizontal scrolling
348 if (!isColumnViewActive()) {
349 view->horizontalScrollBar()->setValue(x);
350 }
351 view->verticalScrollBar()->setValue(y);
352
353 m_loadingDirectory = false;
354 }
355
356 QPoint DolphinView::contentsPosition() const
357 {
358 const int x = itemView()->horizontalScrollBar()->value();
359 const int y = itemView()->verticalScrollBar()->value();
360 return QPoint(x, y);
361 }
362
363 void DolphinView::zoomIn()
364 {
365 m_controller->triggerZoomIn();
366 m_iconManager->updatePreviews();
367 }
368
369 void DolphinView::zoomOut()
370 {
371 m_controller->triggerZoomOut();
372 m_iconManager->updatePreviews();
373 }
374
375 bool DolphinView::isZoomInPossible() const
376 {
377 return m_controller->isZoomInPossible();
378 }
379
380 bool DolphinView::isZoomOutPossible() const
381 {
382 return m_controller->isZoomOutPossible();
383 }
384
385 void DolphinView::setSorting(Sorting sorting)
386 {
387 if (sorting != this->sorting()) {
388 updateSorting(sorting);
389 }
390 }
391
392 DolphinView::Sorting DolphinView::sorting() const
393 {
394 return m_proxyModel->sorting();
395 }
396
397 void DolphinView::setSortOrder(Qt::SortOrder order)
398 {
399 if (sortOrder() != order) {
400 updateSortOrder(order);
401 }
402 }
403
404 Qt::SortOrder DolphinView::sortOrder() const
405 {
406 return m_proxyModel->sortOrder();
407 }
408
409 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
410 {
411 const KUrl viewPropsUrl = viewPropertiesUrl();
412 ViewProperties props(viewPropsUrl);
413 props.setAdditionalInfo(info);
414 m_fileItemDelegate->setShowInformation(info);
415
416 emit additionalInfoChanged();
417
418 if (itemView() != m_detailsView) {
419 // the details view requires no reloading of the directory, as it maps
420 // the file item delegate info to its columns internally
421 loadDirectory(viewPropsUrl);
422 }
423 }
424
425 KFileItemDelegate::InformationList DolphinView::additionalInfo() const
426 {
427 return m_fileItemDelegate->showInformation();
428 }
429
430 void DolphinView::reload()
431 {
432 setUrl(url());
433 loadDirectory(url(), true);
434 }
435
436 void DolphinView::refresh()
437 {
438 const bool oldActivationState = m_active;
439 m_active = true;
440
441 createView();
442 applyViewProperties(m_controller->url());
443 reload();
444
445 setActive(oldActivationState);
446 }
447
448 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
449 {
450 if (m_controller->url() == url) {
451 return;
452 }
453
454 m_iconManager->cancelPreviews();
455 m_controller->setUrl(url); // emits urlChanged, which we forward
456
457 if (!rootUrl.isEmpty() && rootUrl.isParentOf(url)) {
458 applyViewProperties(rootUrl);
459 loadDirectory(rootUrl);
460 if (itemView() == m_columnView) {
461 m_columnView->setRootUrl(rootUrl);
462 m_columnView->showColumn(url);
463 }
464 } else {
465 applyViewProperties(url);
466 loadDirectory(url);
467 }
468
469 emit startedPathLoading(url);
470 }
471
472 void DolphinView::setNameFilter(const QString& nameFilter)
473 {
474 m_proxyModel->setFilterRegExp(nameFilter);
475
476 if (isColumnViewActive()) {
477 // adjusting the directory lister is not enough in the case of the
478 // column view, as each column has its own directory lister internally...
479 m_columnView->setNameFilter(nameFilter);
480 }
481 }
482
483 void DolphinView::calculateItemCount(int& fileCount, int& folderCount)
484 {
485 foreach (const KFileItem &item, m_dirLister->items()) {
486 if (item.isDir()) {
487 ++folderCount;
488 } else {
489 ++fileCount;
490 }
491 }
492 }
493
494 void DolphinView::setUrl(const KUrl& url)
495 {
496 // remember current item candidate (see restoreCurrentItem())
497 m_currentItemUrl = url;
498 updateView(url, KUrl());
499 }
500
501 void DolphinView::changeSelection(const KFileItemList& selection)
502 {
503 clearSelection();
504 if (selection.isEmpty()) {
505 return;
506 }
507 const KUrl& baseUrl = url();
508 KUrl url;
509 QItemSelection new_selection;
510 foreach(const KFileItem& item, selection) {
511 url = item.url().upUrl();
512 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
513 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
514 new_selection.select(index, index);
515 }
516 }
517 itemView()->selectionModel()->select(new_selection,
518 QItemSelectionModel::ClearAndSelect
519 | QItemSelectionModel::Current);
520 }
521
522 void DolphinView::renameSelectedItems()
523 {
524 const KFileItemList items = selectedItems();
525 if (items.count() > 1) {
526 // More than one item has been selected for renaming. Open
527 // a rename dialog and rename all items afterwards.
528 RenameDialog dialog(this, items);
529 if (dialog.exec() == QDialog::Rejected) {
530 return;
531 }
532
533 const QString newName = dialog.newName();
534 if (newName.isEmpty()) {
535 emit errorMessage(dialog.errorString());
536 } else {
537 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
538 // as one operation instead of n rename operations like it is done now...
539 Q_ASSERT(newName.contains('#'));
540
541 // iterate through all selected items and rename them...
542 int index = 1;
543 foreach (const KFileItem &item, items) {
544 const KUrl& oldUrl = item.url();
545 QString number;
546 number.setNum(index++);
547
548 QString name = newName;
549 name.replace('#', number);
550
551 if (oldUrl.fileName() != name) {
552 KUrl newUrl = oldUrl;
553 newUrl.setFileName(name);
554 KonqOperations::rename(this, oldUrl, newUrl);
555 emit doingOperation(KIO::FileUndoManager::Rename);
556 }
557 }
558 }
559 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
560 Q_ASSERT(items.count() == 1);
561
562 if (isColumnViewActive()) {
563 m_columnView->editItem(items.first());
564 } else {
565 const QModelIndex dirIndex = m_dolphinModel->indexForItem(items.first());
566 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
567 itemView()->edit(proxyIndex);
568 }
569 } else {
570 Q_ASSERT(items.count() == 1);
571
572 RenameDialog dialog(this, items);
573 if (dialog.exec() == QDialog::Rejected) {
574 return;
575 }
576
577 const QString& newName = dialog.newName();
578 if (newName.isEmpty()) {
579 emit errorMessage(dialog.errorString());
580 } else {
581 const KUrl& oldUrl = items.first().url();
582 KUrl newUrl = oldUrl;
583 newUrl.setFileName(newName);
584 KonqOperations::rename(this, oldUrl, newUrl);
585 emit doingOperation(KIO::FileUndoManager::Rename);
586 }
587 }
588 }
589
590 void DolphinView::trashSelectedItems()
591 {
592 emit doingOperation(KIO::FileUndoManager::Trash);
593 KonqOperations::del(this, KonqOperations::TRASH, selectedUrls());
594 }
595
596 void DolphinView::deleteSelectedItems()
597 {
598 const KUrl::List list = selectedUrls();
599 const bool del = KonqOperations::askDeleteConfirmation(list,
600 KonqOperations::DEL,
601 KonqOperations::DEFAULT_CONFIRMATION,
602 this);
603
604 if (del) {
605 KIO::Job* job = KIO::del(list);
606 connect(job, SIGNAL(result(KJob*)),
607 this, SLOT(slotDeleteFileFinished(KJob*)));
608 }
609 }
610
611 void DolphinView::cutSelectedItems()
612 {
613 QMimeData* mimeData = new QMimeData();
614 const KUrl::List kdeUrls = selectedUrls();
615 const KUrl::List mostLocalUrls;
616 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, true);
617 QApplication::clipboard()->setMimeData(mimeData);
618 }
619
620 void DolphinView::copySelectedItems()
621 {
622 QMimeData* mimeData = new QMimeData();
623 const KUrl::List kdeUrls = selectedUrls();
624 const KUrl::List mostLocalUrls;
625 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, false);
626 QApplication::clipboard()->setMimeData(mimeData);
627 }
628
629 void DolphinView::paste()
630 {
631 pasteToUrl(url());
632 }
633
634 void DolphinView::pasteIntoFolder()
635 {
636 const KFileItemList items = selectedItems();
637 if ((items.count() == 1) && items.first().isDir()) {
638 pasteToUrl(items.first().url());
639 }
640 }
641
642 void DolphinView::setShowPreview(bool show)
643 {
644 if (m_showPreview == show) {
645 return;
646 }
647
648 const KUrl viewPropsUrl = viewPropertiesUrl();
649 ViewProperties props(viewPropsUrl);
650 props.setShowPreview(show);
651
652 m_showPreview = show;
653 m_iconManager->setShowPreview(show);
654 emit showPreviewChanged();
655
656 loadDirectory(viewPropsUrl);
657 }
658
659 void DolphinView::setShowHiddenFiles(bool show)
660 {
661 if (m_dirLister->showingDotFiles() == show) {
662 return;
663 }
664
665 const KUrl viewPropsUrl = viewPropertiesUrl();
666 ViewProperties props(viewPropsUrl);
667 props.setShowHiddenFiles(show);
668
669 m_dirLister->setShowingDotFiles(show);
670 emit showHiddenFilesChanged();
671
672 loadDirectory(viewPropsUrl);
673 }
674
675 void DolphinView::setCategorizedSorting(bool categorized)
676 {
677 if (categorized == categorizedSorting()) {
678 return;
679 }
680
681 // setCategorizedSorting(true) may only get invoked
682 // if the view supports categorized sorting
683 Q_ASSERT(!categorized || supportsCategorizedSorting());
684
685 ViewProperties props(viewPropertiesUrl());
686 props.setCategorizedSorting(categorized);
687 props.save();
688
689 m_storedCategorizedSorting = categorized;
690 m_proxyModel->setCategorizedModel(categorized);
691
692 emit categorizedSortingChanged();
693 }
694
695 void DolphinView::toggleSortOrder()
696 {
697 const Qt::SortOrder order = (sortOrder() == Qt::AscendingOrder) ?
698 Qt::DescendingOrder :
699 Qt::AscendingOrder;
700 setSortOrder(order);
701 }
702
703 void DolphinView::toggleAdditionalInfo(QAction* action)
704 {
705 const KFileItemDelegate::Information info =
706 static_cast<KFileItemDelegate::Information>(action->data().toInt());
707
708 KFileItemDelegate::InformationList list = additionalInfo();
709
710 const bool show = action->isChecked();
711
712 const int index = list.indexOf(info);
713 const bool containsInfo = (index >= 0);
714 if (show && !containsInfo) {
715 list.append(info);
716 setAdditionalInfo(list);
717 } else if (!show && containsInfo) {
718 list.removeAt(index);
719 setAdditionalInfo(list);
720 Q_ASSERT(list.indexOf(info) < 0);
721 }
722 }
723
724
725 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
726 {
727 QWidget::mouseReleaseEvent(event);
728 setActive(true);
729 }
730
731 void DolphinView::wheelEvent(QWheelEvent* event)
732 {
733 if (event->modifiers() & Qt::ControlModifier) {
734 const int delta = event->delta();
735 if ((delta > 0) && isZoomInPossible()) {
736 zoomIn();
737 } else if ((delta < 0) && isZoomOutPossible()) {
738 zoomOut();
739 }
740 event->accept();
741 }
742 }
743
744 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
745 {
746 if ((watched == itemView()) && (event->type() == QEvent::FocusIn)) {
747 m_controller->requestActivation();
748 }
749
750 return QWidget::eventFilter(watched, event);
751 }
752
753 void DolphinView::activate()
754 {
755 setActive(true);
756 }
757
758 void DolphinView::triggerItem(const KFileItem& item)
759 {
760 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
761 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
762 // items are selected by the user, hence don't trigger the
763 // item specified by 'index'
764 return;
765 }
766
767 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
768 if (item.isNull() || m_isContextMenuOpen) {
769 return;
770 }
771
772 if (m_toolTipManager != 0) {
773 m_toolTipManager->hideTip();
774 }
775 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
776 }
777
778 void DolphinView::emitSelectionChangedSignal()
779 {
780 emit selectionChanged(DolphinView::selectedItems());
781 }
782
783 void DolphinView::openContextMenu(const QPoint& pos)
784 {
785 KFileItem item;
786 if (isColumnViewActive()) {
787 item = m_columnView->itemAt(pos);
788 } else {
789 const QModelIndex index = itemView()->indexAt(pos);
790 if (index.isValid() && (index.column() == DolphinModel::Name)) {
791 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
792 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
793 }
794 }
795
796 if (m_toolTipManager != 0) {
797 m_toolTipManager->hideTip();
798 }
799
800 m_isContextMenuOpen = true; // TODO: workaround for Qt-issue 207192
801 emit requestContextMenu(item, url());
802 m_isContextMenuOpen = false;
803 }
804
805 void DolphinView::dropUrls(const KUrl::List& urls,
806 const KUrl& destPath,
807 const KFileItem& destItem)
808 {
809 Q_ASSERT(!urls.isEmpty());
810 const KUrl destination = !destItem.isNull() && destItem.isDir() ?
811 destItem.url() : destPath;
812 const KUrl sourceDir = KUrl(urls.first().directory());
813 if (sourceDir != destination) {
814 DolphinDropController dropController(this);
815 // forward doingOperation signal up to the mainwindow
816 connect(&dropController, SIGNAL(doingOperation(KIO::FileUndoManager::CommandType)),
817 this, SIGNAL(doingOperation(KIO::FileUndoManager::CommandType)));
818 dropController.dropUrls(urls, destination);
819 }
820 }
821
822 void DolphinView::updateSorting(DolphinView::Sorting sorting)
823 {
824 ViewProperties props(viewPropertiesUrl());
825 props.setSorting(sorting);
826
827 m_proxyModel->setSorting(sorting);
828
829 emit sortingChanged(sorting);
830 }
831
832 void DolphinView::updateSortOrder(Qt::SortOrder order)
833 {
834 ViewProperties props(viewPropertiesUrl());
835 props.setSortOrder(order);
836
837 m_proxyModel->setSortOrder(order);
838
839 emit sortOrderChanged(order);
840 }
841
842 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
843 {
844 ViewProperties props(viewPropertiesUrl());
845 props.setAdditionalInfo(info);
846 props.save();
847
848 m_fileItemDelegate->setShowInformation(info);
849
850 emit additionalInfoChanged();
851 }
852
853 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
854 {
855 const bool enable = (m_mode == DolphinView::DetailsView) ||
856 (m_mode == DolphinView::IconsView);
857
858 QAction* showSizeInfo = collection->action("show_size_info");
859 QAction* showDateInfo = collection->action("show_date_info");
860 QAction* showPermissionsInfo = collection->action("show_permissions_info");
861 QAction* showOwnerInfo = collection->action("show_owner_info");
862 QAction* showGroupInfo = collection->action("show_group_info");
863 QAction* showMimeInfo = collection->action("show_mime_info");
864
865 showSizeInfo->setChecked(false);
866 showDateInfo->setChecked(false);
867 showPermissionsInfo->setChecked(false);
868 showOwnerInfo->setChecked(false);
869 showGroupInfo->setChecked(false);
870 showMimeInfo->setChecked(false);
871
872 showSizeInfo->setEnabled(enable);
873 showDateInfo->setEnabled(enable);
874 showPermissionsInfo->setEnabled(enable);
875 showOwnerInfo->setEnabled(enable);
876 showGroupInfo->setEnabled(enable);
877 showMimeInfo->setEnabled(enable);
878
879 foreach (KFileItemDelegate::Information info, m_fileItemDelegate->showInformation()) {
880 switch (info) {
881 case KFileItemDelegate::Size:
882 showSizeInfo->setChecked(true);
883 break;
884 case KFileItemDelegate::ModificationTime:
885 showDateInfo->setChecked(true);
886 break;
887 case KFileItemDelegate::Permissions:
888 showPermissionsInfo->setChecked(true);
889 break;
890 case KFileItemDelegate::Owner:
891 showOwnerInfo->setChecked(true);
892 break;
893 case KFileItemDelegate::OwnerAndGroup:
894 showGroupInfo->setChecked(true);
895 break;
896 case KFileItemDelegate::FriendlyMimeType:
897 showMimeInfo->setChecked(true);
898 break;
899 default:
900 break;
901 }
902 }
903 }
904
905 QPair<bool, QString> DolphinView::pasteInfo() const
906 {
907 QPair<bool, QString> ret;
908 QClipboard* clipboard = QApplication::clipboard();
909 const QMimeData* mimeData = clipboard->mimeData();
910
911 KUrl::List urls = KUrl::List::fromMimeData(mimeData);
912 if (!urls.isEmpty()) {
913 // disable the paste action if no writing is supported
914 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url());
915 ret.first = KonqFileItemCapabilities(KFileItemList() << item).supportsWriting();
916
917 if (urls.count() == 1) {
918 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, urls.first(), true);
919 ret.second = item.isDir() ? i18nc("@action:inmenu", "Paste One Folder") :
920 i18nc("@action:inmenu", "Paste One File");
921
922 } else {
923 ret.second = i18ncp("@action:inmenu", "Paste One Item", "Paste %1 Items", urls.count());
924 }
925 } else {
926 ret.first = false;
927 ret.second = i18nc("@action:inmenu", "Paste");
928 }
929
930 return ret;
931 }
932
933 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
934 {
935 m_tabsForFiles = tabsForFiles;
936 }
937
938 bool DolphinView::isTabsForFilesEnabled() const
939 {
940 return m_tabsForFiles;
941 }
942
943 void DolphinView::emitContentsMoved()
944 {
945 // only emit the contents moved signal if:
946 // - no directory loading is ongoing (this would reset the contents position
947 // always to (0, 0))
948 // - if the Column View is active: the column view does an automatic
949 // positioning during the loading operation, which must be remembered
950 if (!m_loadingDirectory || isColumnViewActive()) {
951 const QPoint pos(contentsPosition());
952 emit contentsMoved(pos.x(), pos.y());
953 }
954 }
955
956 void DolphinView::showHoverInformation(const KFileItem& item)
957 {
958 emit requestItemInfo(item);
959 }
960
961 void DolphinView::clearHoverInformation()
962 {
963 emit requestItemInfo(KFileItem());
964 }
965
966 void DolphinView::slotDeleteFileFinished(KJob* job)
967 {
968 if (job->error() == 0) {
969 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
970 } else {
971 emit errorMessage(job->errorString());
972 }
973 }
974
975
976 void DolphinView::restoreCurrentItem()
977 {
978 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_currentItemUrl);
979 if (dirIndex.isValid()) {
980 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
981 QAbstractItemView* view = itemView();
982 const bool clearSelection = !hasSelection();
983 view->setCurrentIndex(proxyIndex);
984 if (clearSelection) {
985 view->clearSelection();
986 }
987 }
988 }
989
990 void DolphinView::loadDirectory(const KUrl& url, bool reload)
991 {
992 if (!url.isValid()) {
993 const QString location(url.pathOrUrl());
994 if (location.isEmpty()) {
995 emit errorMessage(i18nc("@info:status", "The location is empty."));
996 } else {
997 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
998 }
999 return;
1000 }
1001
1002 m_loadingDirectory = true;
1003
1004 m_dirLister->stop();
1005 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
1006
1007 if (isColumnViewActive()) {
1008 // adjusting the directory lister is not enough in the case of the
1009 // column view, as each column has its own directory lister internally...
1010 if (reload) {
1011 m_columnView->reload();
1012 } else {
1013 m_columnView->showColumn(url);
1014 }
1015 }
1016 }
1017
1018 KUrl DolphinView::viewPropertiesUrl() const
1019 {
1020 if (isColumnViewActive()) {
1021 return m_columnView->rootUrl();
1022 }
1023
1024 return url();
1025 }
1026
1027 void DolphinView::applyViewProperties(const KUrl& url)
1028 {
1029 if (isColumnViewActive() && rootUrl().isParentOf(url)) {
1030 // The column view is active, hence don't apply the view properties
1031 // of sub directories (represented by columns) to the view. The
1032 // view always represents the properties of the first column.
1033 return;
1034 }
1035
1036 const ViewProperties props(url);
1037
1038 const Mode mode = props.viewMode();
1039 if (m_mode != mode) {
1040 m_mode = mode;
1041 createView();
1042 emit modeChanged();
1043 }
1044 if (itemView() == 0) {
1045 createView();
1046 }
1047 Q_ASSERT(itemView() != 0);
1048 Q_ASSERT(m_fileItemDelegate != 0);
1049
1050 const bool showHiddenFiles = props.showHiddenFiles();
1051 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
1052 m_dirLister->setShowingDotFiles(showHiddenFiles);
1053 emit showHiddenFilesChanged();
1054 }
1055
1056 m_storedCategorizedSorting = props.categorizedSorting();
1057 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1058 if (categorized != m_proxyModel->isCategorizedModel()) {
1059 m_proxyModel->setCategorizedModel(categorized);
1060 emit categorizedSortingChanged();
1061 }
1062
1063 const DolphinView::Sorting sorting = props.sorting();
1064 if (sorting != m_proxyModel->sorting()) {
1065 m_proxyModel->setSorting(sorting);
1066 emit sortingChanged(sorting);
1067 }
1068
1069 const Qt::SortOrder sortOrder = props.sortOrder();
1070 if (sortOrder != m_proxyModel->sortOrder()) {
1071 m_proxyModel->setSortOrder(sortOrder);
1072 emit sortOrderChanged(sortOrder);
1073 }
1074
1075 KFileItemDelegate::InformationList info = props.additionalInfo();
1076 if (info != m_fileItemDelegate->showInformation()) {
1077 m_fileItemDelegate->setShowInformation(info);
1078 emit additionalInfoChanged();
1079 }
1080
1081 const bool showPreview = props.showPreview();
1082 if (showPreview != m_showPreview) {
1083 m_showPreview = showPreview;
1084 m_iconManager->setShowPreview(showPreview);
1085 emit showPreviewChanged();
1086 }
1087 }
1088
1089 void DolphinView::createView()
1090 {
1091 deleteView();
1092 Q_ASSERT(m_iconsView == 0);
1093 Q_ASSERT(m_detailsView == 0);
1094 Q_ASSERT(m_columnView == 0);
1095
1096 QAbstractItemView* view = 0;
1097 switch (m_mode) {
1098 case IconsView: {
1099 m_iconsView = new DolphinIconsView(this, m_controller);
1100 view = m_iconsView;
1101 break;
1102 }
1103
1104 case DetailsView:
1105 m_detailsView = new DolphinDetailsView(this, m_controller);
1106 view = m_detailsView;
1107 break;
1108
1109 case ColumnView:
1110 m_columnView = new DolphinColumnView(this, m_controller);
1111 view = m_columnView;
1112 break;
1113 }
1114
1115 Q_ASSERT(view != 0);
1116 view->installEventFilter(this);
1117
1118 m_controller->setItemView(view);
1119
1120 m_fileItemDelegate = new KFileItemDelegate(view);
1121 view->setItemDelegate(m_fileItemDelegate);
1122
1123 view->setModel(m_proxyModel);
1124 if (m_selectionModel != 0) {
1125 view->setSelectionModel(m_selectionModel);
1126 } else {
1127 m_selectionModel = view->selectionModel();
1128 }
1129
1130 // reparent the selection model, as it should not be deleted
1131 // when deleting the model
1132 m_selectionModel->setParent(this);
1133
1134 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
1135
1136 m_iconManager = new IconManager(view, m_proxyModel);
1137 m_iconManager->setShowPreview(m_showPreview);
1138
1139 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1140 m_toolTipManager = new ToolTipManager(view, m_proxyModel);
1141 }
1142
1143 m_topLayout->insertWidget(1, view);
1144
1145 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
1146 this, SLOT(emitSelectionChangedSignal()));
1147 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1148 this, SLOT(emitContentsMoved()));
1149 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1150 this, SLOT(emitContentsMoved()));
1151 }
1152
1153 void DolphinView::deleteView()
1154 {
1155 QAbstractItemView* view = itemView();
1156 if (view != 0) {
1157 // It's important to set the keyboard focus to the parent
1158 // before deleting the view: Otherwise when having a split
1159 // view the other view will get the focus and will request
1160 // an activation (see DolphinView::eventFilter()).
1161 setFocus();
1162
1163 m_topLayout->removeWidget(view);
1164 view->close();
1165 view->deleteLater();
1166 view = 0;
1167 m_iconsView = 0;
1168 m_detailsView = 0;
1169 m_columnView = 0;
1170 m_fileItemDelegate = 0;
1171 m_iconManager = 0;
1172 m_toolTipManager = 0;
1173 }
1174 }
1175
1176 QAbstractItemView* DolphinView::itemView() const
1177 {
1178 if (m_detailsView != 0) {
1179 return m_detailsView;
1180 } else if (m_columnView != 0) {
1181 return m_columnView;
1182 }
1183
1184 return m_iconsView;
1185 }
1186
1187 bool DolphinView::isCutItem(const KFileItem& item) const
1188 {
1189 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1190 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
1191
1192 const KUrl& itemUrl = item.url();
1193 KUrl::List::const_iterator it = cutUrls.begin();
1194 const KUrl::List::const_iterator end = cutUrls.end();
1195 while (it != end) {
1196 if (*it == itemUrl) {
1197 return true;
1198 }
1199 ++it;
1200 }
1201
1202 return false;
1203 }
1204
1205 void DolphinView::pasteToUrl(const KUrl& url)
1206 {
1207 QClipboard* clipboard = QApplication::clipboard();
1208 const QMimeData* mimeData = clipboard->mimeData();
1209
1210 const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
1211 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
1212 KonqOperations::copy(this, KonqOperations::MOVE, sourceUrls, url);
1213 emit doingOperation(KIO::FileUndoManager::Move);
1214 clipboard->clear();
1215 } else {
1216 KonqOperations::copy(this, KonqOperations::COPY, sourceUrls, url);
1217 emit doingOperation(KIO::FileUndoManager::Copy);
1218 }
1219 }
1220
1221 void DolphinView::slotRequestUrlChange(const KUrl& url)
1222 {
1223 emit requestUrlChange(url);
1224 m_controller->setUrl(url);
1225 }
1226
1227 void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
1228 {
1229 if (oldUrl == m_controller->url()) {
1230 m_controller->setUrl(newUrl);
1231 }
1232 }
1233
1234 #include "dolphinview.moc"