]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
only resolve the MIME-types asynchronously, if no preview is generated
[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 const QAbstractItemView* view = itemView();
299
300 // Our view has a selection, we will map them back to the DolphinModel
301 // and then fill the KFileItemList.
302 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
303
304 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
305 KFileItemList itemList;
306
307 const QModelIndexList indexList = selection.indexes();
308 foreach (const QModelIndex &index, indexList) {
309 KFileItem item = m_dolphinModel->itemForIndex(index);
310 if (!item.isNull()) {
311 itemList.append(item);
312 }
313 }
314
315 return itemList;
316 }
317
318 KUrl::List DolphinView::selectedUrls() const
319 {
320 KUrl::List urls;
321 const KFileItemList list = selectedItems();
322 foreach (const KFileItem &item, list) {
323 urls.append(item.url());
324 }
325 return urls;
326 }
327
328 KFileItem DolphinView::fileItem(const QModelIndex& index) const
329 {
330 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
331 return m_dolphinModel->itemForIndex(dolphinModelIndex);
332 }
333
334 void DolphinView::setContentsPosition(int x, int y)
335 {
336 QAbstractItemView* view = itemView();
337
338 // the ColumnView takes care itself for the horizontal scrolling
339 if (!isColumnViewActive()) {
340 view->horizontalScrollBar()->setValue(x);
341 }
342 view->verticalScrollBar()->setValue(y);
343
344 m_loadingDirectory = false;
345 }
346
347 QPoint DolphinView::contentsPosition() const
348 {
349 const int x = itemView()->horizontalScrollBar()->value();
350 const int y = itemView()->verticalScrollBar()->value();
351 return QPoint(x, y);
352 }
353
354 void DolphinView::zoomIn()
355 {
356 m_controller->triggerZoomIn();
357 m_iconManager->updatePreviews();
358 }
359
360 void DolphinView::zoomOut()
361 {
362 m_controller->triggerZoomOut();
363 m_iconManager->updatePreviews();
364 }
365
366 bool DolphinView::isZoomInPossible() const
367 {
368 return m_controller->isZoomInPossible();
369 }
370
371 bool DolphinView::isZoomOutPossible() const
372 {
373 return m_controller->isZoomOutPossible();
374 }
375
376 void DolphinView::setSorting(Sorting sorting)
377 {
378 if (sorting != this->sorting()) {
379 updateSorting(sorting);
380 }
381 }
382
383 DolphinView::Sorting DolphinView::sorting() const
384 {
385 return m_proxyModel->sorting();
386 }
387
388 void DolphinView::setSortOrder(Qt::SortOrder order)
389 {
390 if (sortOrder() != order) {
391 updateSortOrder(order);
392 }
393 }
394
395 Qt::SortOrder DolphinView::sortOrder() const
396 {
397 return m_proxyModel->sortOrder();
398 }
399
400 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
401 {
402 const KUrl viewPropsUrl = viewPropertiesUrl();
403 ViewProperties props(viewPropsUrl);
404 props.setAdditionalInfo(info);
405 m_fileItemDelegate->setShowInformation(info);
406
407 emit additionalInfoChanged();
408
409 if (itemView() != m_detailsView) {
410 // the details view requires no reloading of the directory, as it maps
411 // the file item delegate info to its columns internally
412 loadDirectory(viewPropsUrl);
413 }
414 }
415
416 KFileItemDelegate::InformationList DolphinView::additionalInfo() const
417 {
418 return m_fileItemDelegate->showInformation();
419 }
420
421 void DolphinView::reload()
422 {
423 setUrl(url());
424 loadDirectory(url(), true);
425 }
426
427 void DolphinView::refresh()
428 {
429 const bool oldActivationState = m_active;
430 m_active = true;
431
432 createView();
433 applyViewProperties(m_controller->url());
434 reload();
435
436 setActive(oldActivationState);
437 }
438
439 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
440 {
441 if (m_controller->url() == url) {
442 return;
443 }
444
445 m_controller->setUrl(url); // emits urlChanged, which we forward
446
447 if (!rootUrl.isEmpty() && rootUrl.isParentOf(url)) {
448 applyViewProperties(rootUrl);
449 loadDirectory(rootUrl);
450 if (itemView() == m_columnView) {
451 m_columnView->setRootUrl(rootUrl);
452 m_columnView->showColumn(url);
453 }
454 } else {
455 applyViewProperties(url);
456 loadDirectory(url);
457 }
458
459 emit startedPathLoading(url);
460 }
461
462 void DolphinView::setNameFilter(const QString& nameFilter)
463 {
464 m_proxyModel->setFilterRegExp(nameFilter);
465
466 if (isColumnViewActive()) {
467 // adjusting the directory lister is not enough in the case of the
468 // column view, as each column has its own directory lister internally...
469 m_columnView->setNameFilter(nameFilter);
470 }
471 }
472
473 void DolphinView::calculateItemCount(int& fileCount, int& folderCount)
474 {
475 foreach (const KFileItem &item, m_dirLister->items()) {
476 if (item.isDir()) {
477 ++folderCount;
478 } else {
479 ++fileCount;
480 }
481 }
482 }
483
484 void DolphinView::setUrl(const KUrl& url)
485 {
486 // remember current item candidate (see restoreCurrentItem())
487 m_currentItemUrl = url;
488 updateView(url, KUrl());
489 }
490
491 void DolphinView::changeSelection(const KFileItemList& selection)
492 {
493 clearSelection();
494 if (selection.isEmpty()) {
495 return;
496 }
497 const KUrl& baseUrl = url();
498 KUrl url;
499 QItemSelection new_selection;
500 foreach(const KFileItem& item, selection) {
501 url = item.url().upUrl();
502 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
503 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
504 new_selection.select(index, index);
505 }
506 }
507 itemView()->selectionModel()->select(new_selection,
508 QItemSelectionModel::ClearAndSelect
509 | QItemSelectionModel::Current);
510 }
511
512 void DolphinView::renameSelectedItems()
513 {
514 const KFileItemList items = selectedItems();
515 if (items.count() > 1) {
516 // More than one item has been selected for renaming. Open
517 // a rename dialog and rename all items afterwards.
518 RenameDialog dialog(this, items);
519 if (dialog.exec() == QDialog::Rejected) {
520 return;
521 }
522
523 const QString newName = dialog.newName();
524 if (newName.isEmpty()) {
525 emit errorMessage(dialog.errorString());
526 } else {
527 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
528 // as one operation instead of n rename operations like it is done now...
529 Q_ASSERT(newName.contains('#'));
530
531 // iterate through all selected items and rename them...
532 int index = 1;
533 foreach (const KFileItem &item, items) {
534 const KUrl& oldUrl = item.url();
535 QString number;
536 number.setNum(index++);
537
538 QString name = newName;
539 name.replace('#', number);
540
541 if (oldUrl.fileName() != name) {
542 KUrl newUrl = oldUrl;
543 newUrl.setFileName(name);
544 KonqOperations::rename(this, oldUrl, newUrl);
545 emit doingOperation(KIO::FileUndoManager::Rename);
546 }
547 }
548 }
549 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
550 Q_ASSERT(items.count() == 1);
551
552 if (isColumnViewActive()) {
553 m_columnView->editItem(items.first());
554 } else {
555 const QModelIndex dirIndex = m_dolphinModel->indexForItem(items.first());
556 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
557 itemView()->edit(proxyIndex);
558 }
559 } else {
560 Q_ASSERT(items.count() == 1);
561
562 RenameDialog dialog(this, items);
563 if (dialog.exec() == QDialog::Rejected) {
564 return;
565 }
566
567 const QString& newName = dialog.newName();
568 if (newName.isEmpty()) {
569 emit errorMessage(dialog.errorString());
570 } else {
571 const KUrl& oldUrl = items.first().url();
572 KUrl newUrl = oldUrl;
573 newUrl.setFileName(newName);
574 KonqOperations::rename(this, oldUrl, newUrl);
575 emit doingOperation(KIO::FileUndoManager::Rename);
576 }
577 }
578 }
579
580 void DolphinView::trashSelectedItems()
581 {
582 emit doingOperation(KIO::FileUndoManager::Trash);
583 KonqOperations::del(this, KonqOperations::TRASH, selectedUrls());
584 }
585
586 void DolphinView::deleteSelectedItems()
587 {
588 const KUrl::List list = selectedUrls();
589 const bool del = KonqOperations::askDeleteConfirmation(list,
590 KonqOperations::DEL,
591 KonqOperations::DEFAULT_CONFIRMATION,
592 this);
593
594 if (del) {
595 KIO::Job* job = KIO::del(list);
596 connect(job, SIGNAL(result(KJob*)),
597 this, SLOT(slotDeleteFileFinished(KJob*)));
598 }
599 }
600
601 void DolphinView::cutSelectedItems()
602 {
603 QMimeData* mimeData = new QMimeData();
604 const KUrl::List kdeUrls = selectedUrls();
605 const KUrl::List mostLocalUrls;
606 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, true);
607 QApplication::clipboard()->setMimeData(mimeData);
608 }
609
610 void DolphinView::copySelectedItems()
611 {
612 QMimeData* mimeData = new QMimeData();
613 const KUrl::List kdeUrls = selectedUrls();
614 const KUrl::List mostLocalUrls;
615 KonqMimeData::populateMimeData(mimeData, kdeUrls, mostLocalUrls, false);
616 QApplication::clipboard()->setMimeData(mimeData);
617 }
618
619 void DolphinView::paste()
620 {
621 pasteToUrl(url());
622 }
623
624 void DolphinView::pasteIntoFolder()
625 {
626 const KFileItemList items = selectedItems();
627 if ((items.count() == 1) && items.first().isDir()) {
628 pasteToUrl(items.first().url());
629 }
630 }
631
632 void DolphinView::setShowPreview(bool show)
633 {
634 if (m_showPreview == show) {
635 return;
636 }
637
638 const KUrl viewPropsUrl = viewPropertiesUrl();
639 ViewProperties props(viewPropsUrl);
640 props.setShowPreview(show);
641
642 m_showPreview = show;
643 m_iconManager->setShowPreview(show);
644 emit showPreviewChanged();
645
646 loadDirectory(viewPropsUrl);
647 }
648
649 void DolphinView::setShowHiddenFiles(bool show)
650 {
651 if (m_dirLister->showingDotFiles() == show) {
652 return;
653 }
654
655 const KUrl viewPropsUrl = viewPropertiesUrl();
656 ViewProperties props(viewPropsUrl);
657 props.setShowHiddenFiles(show);
658
659 m_dirLister->setShowingDotFiles(show);
660 emit showHiddenFilesChanged();
661
662 loadDirectory(viewPropsUrl);
663 }
664
665 void DolphinView::setCategorizedSorting(bool categorized)
666 {
667 if (categorized == categorizedSorting()) {
668 return;
669 }
670
671 // setCategorizedSorting(true) may only get invoked
672 // if the view supports categorized sorting
673 Q_ASSERT(!categorized || supportsCategorizedSorting());
674
675 ViewProperties props(viewPropertiesUrl());
676 props.setCategorizedSorting(categorized);
677 props.save();
678
679 m_storedCategorizedSorting = categorized;
680 m_proxyModel->setCategorizedModel(categorized);
681
682 emit categorizedSortingChanged();
683 }
684
685 void DolphinView::toggleSortOrder()
686 {
687 const Qt::SortOrder order = (sortOrder() == Qt::AscendingOrder) ?
688 Qt::DescendingOrder :
689 Qt::AscendingOrder;
690 setSortOrder(order);
691 }
692
693 void DolphinView::toggleAdditionalInfo(QAction* action)
694 {
695 const KFileItemDelegate::Information info =
696 static_cast<KFileItemDelegate::Information>(action->data().toInt());
697
698 KFileItemDelegate::InformationList list = additionalInfo();
699
700 const bool show = action->isChecked();
701
702 const int index = list.indexOf(info);
703 const bool containsInfo = (index >= 0);
704 if (show && !containsInfo) {
705 list.append(info);
706 setAdditionalInfo(list);
707 } else if (!show && containsInfo) {
708 list.removeAt(index);
709 setAdditionalInfo(list);
710 Q_ASSERT(list.indexOf(info) < 0);
711 }
712 }
713
714
715 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
716 {
717 QWidget::mouseReleaseEvent(event);
718 setActive(true);
719 }
720
721 void DolphinView::wheelEvent(QWheelEvent* event)
722 {
723 if (event->modifiers() & Qt::ControlModifier) {
724 const int delta = event->delta();
725 if ((delta > 0) && isZoomInPossible()) {
726 zoomIn();
727 } else if ((delta < 0) && isZoomOutPossible()) {
728 zoomOut();
729 }
730 event->accept();
731 }
732 }
733
734 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
735 {
736 if ((watched == itemView()) && (event->type() == QEvent::FocusIn)) {
737 m_controller->requestActivation();
738 }
739
740 return QWidget::eventFilter(watched, event);
741 }
742
743 void DolphinView::activate()
744 {
745 setActive(true);
746 }
747
748 void DolphinView::triggerItem(const KFileItem& item)
749 {
750 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
751 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
752 // items are selected by the user, hence don't trigger the
753 // item specified by 'index'
754 return;
755 }
756
757 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
758 if (item.isNull() || m_isContextMenuOpen) {
759 return;
760 }
761
762 if (m_toolTipManager != 0) {
763 m_toolTipManager->hideTip();
764 }
765 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
766 }
767
768 void DolphinView::emitSelectionChangedSignal()
769 {
770 emit selectionChanged(DolphinView::selectedItems());
771 }
772
773 void DolphinView::openContextMenu(const QPoint& pos)
774 {
775 KFileItem item;
776
777 const QModelIndex index = itemView()->indexAt(pos);
778 if (index.isValid() && (index.column() == DolphinModel::Name)) {
779 item = fileItem(index);
780 }
781
782 if (m_toolTipManager != 0) {
783 m_toolTipManager->hideTip();
784 }
785
786 m_isContextMenuOpen = true; // TODO: workaround for Qt-issue 207192
787 emit requestContextMenu(item, url());
788 m_isContextMenuOpen = false;
789 }
790
791 void DolphinView::dropUrls(const KUrl::List& urls,
792 const KUrl& destPath,
793 const KFileItem& destItem)
794 {
795 Q_ASSERT(!urls.isEmpty());
796 const KUrl& destination = !destItem.isNull() && destItem.isDir() ?
797 destItem.url() : destPath;
798 const KUrl sourceDir = KUrl(urls.first().directory());
799 if (sourceDir != destination) {
800 dropUrls(urls, destination);
801 }
802 }
803
804 void DolphinView::dropUrls(const KUrl::List& urls,
805 const KUrl& destination)
806 {
807 DolphinDropController dropController(this);
808 // forward doingOperation signal up to the mainwindow
809 connect(&dropController, SIGNAL(doingOperation(KIO::FileUndoManager::CommandType)),
810 this, SIGNAL(doingOperation(KIO::FileUndoManager::CommandType)));
811 dropController.dropUrls(urls, destination);
812 }
813
814 void DolphinView::updateSorting(DolphinView::Sorting sorting)
815 {
816 ViewProperties props(viewPropertiesUrl());
817 props.setSorting(sorting);
818
819 m_proxyModel->setSorting(sorting);
820
821 emit sortingChanged(sorting);
822 }
823
824 void DolphinView::updateSortOrder(Qt::SortOrder order)
825 {
826 ViewProperties props(viewPropertiesUrl());
827 props.setSortOrder(order);
828
829 m_proxyModel->setSortOrder(order);
830
831 emit sortOrderChanged(order);
832 }
833
834 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
835 {
836 ViewProperties props(viewPropertiesUrl());
837 props.setAdditionalInfo(info);
838 props.save();
839
840 m_fileItemDelegate->setShowInformation(info);
841
842 emit additionalInfoChanged();
843 }
844
845 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
846 {
847 const bool enable = (m_mode == DolphinView::DetailsView) ||
848 (m_mode == DolphinView::IconsView);
849
850 QAction* showSizeInfo = collection->action("show_size_info");
851 QAction* showDateInfo = collection->action("show_date_info");
852 QAction* showPermissionsInfo = collection->action("show_permissions_info");
853 QAction* showOwnerInfo = collection->action("show_owner_info");
854 QAction* showGroupInfo = collection->action("show_group_info");
855 QAction* showMimeInfo = collection->action("show_mime_info");
856
857 showSizeInfo->setChecked(false);
858 showDateInfo->setChecked(false);
859 showPermissionsInfo->setChecked(false);
860 showOwnerInfo->setChecked(false);
861 showGroupInfo->setChecked(false);
862 showMimeInfo->setChecked(false);
863
864 showSizeInfo->setEnabled(enable);
865 showDateInfo->setEnabled(enable);
866 showPermissionsInfo->setEnabled(enable);
867 showOwnerInfo->setEnabled(enable);
868 showGroupInfo->setEnabled(enable);
869 showMimeInfo->setEnabled(enable);
870
871 foreach (KFileItemDelegate::Information info, m_fileItemDelegate->showInformation()) {
872 switch (info) {
873 case KFileItemDelegate::Size:
874 showSizeInfo->setChecked(true);
875 break;
876 case KFileItemDelegate::ModificationTime:
877 showDateInfo->setChecked(true);
878 break;
879 case KFileItemDelegate::Permissions:
880 showPermissionsInfo->setChecked(true);
881 break;
882 case KFileItemDelegate::Owner:
883 showOwnerInfo->setChecked(true);
884 break;
885 case KFileItemDelegate::OwnerAndGroup:
886 showGroupInfo->setChecked(true);
887 break;
888 case KFileItemDelegate::FriendlyMimeType:
889 showMimeInfo->setChecked(true);
890 break;
891 default:
892 break;
893 }
894 }
895 }
896
897 QPair<bool, QString> DolphinView::pasteInfo() const
898 {
899 QPair<bool, QString> ret;
900 QClipboard* clipboard = QApplication::clipboard();
901 const QMimeData* mimeData = clipboard->mimeData();
902
903 KUrl::List urls = KUrl::List::fromMimeData(mimeData);
904 if (!urls.isEmpty()) {
905 // disable the paste action if no writing is supported
906 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url());
907 ret.first = KonqFileItemCapabilities(KFileItemList() << item).supportsWriting();
908
909 if (urls.count() == 1) {
910 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, urls.first(), true);
911 ret.second = item.isDir() ? i18nc("@action:inmenu", "Paste One Folder") :
912 i18nc("@action:inmenu", "Paste One File");
913
914 } else {
915 ret.second = i18ncp("@action:inmenu", "Paste One Item", "Paste %1 Items", urls.count());
916 }
917 } else {
918 ret.first = false;
919 ret.second = i18nc("@action:inmenu", "Paste");
920 }
921
922 return ret;
923 }
924
925 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
926 {
927 m_tabsForFiles = tabsForFiles;
928 }
929
930 bool DolphinView::isTabsForFilesEnabled() const
931 {
932 return m_tabsForFiles;
933 }
934
935 void DolphinView::emitContentsMoved()
936 {
937 // only emit the contents moved signal if:
938 // - no directory loading is ongoing (this would reset the contents position
939 // always to (0, 0))
940 // - if the Column View is active: the column view does an automatic
941 // positioning during the loading operation, which must be remembered
942 if (!m_loadingDirectory || isColumnViewActive()) {
943 const QPoint pos(contentsPosition());
944 emit contentsMoved(pos.x(), pos.y());
945 }
946 }
947
948 void DolphinView::showHoverInformation(const KFileItem& item)
949 {
950 emit requestItemInfo(item);
951 }
952
953 void DolphinView::clearHoverInformation()
954 {
955 emit requestItemInfo(KFileItem());
956 }
957
958 void DolphinView::slotDeleteFileFinished(KJob* job)
959 {
960 if (job->error() == 0) {
961 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
962 } else {
963 emit errorMessage(job->errorString());
964 }
965 }
966
967
968 void DolphinView::restoreCurrentItem()
969 {
970 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_currentItemUrl);
971 if (dirIndex.isValid()) {
972 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
973 QAbstractItemView* view = itemView();
974 const bool clearSelection = !hasSelection();
975 view->setCurrentIndex(proxyIndex);
976 if (clearSelection) {
977 view->clearSelection();
978 }
979 }
980 }
981
982 void DolphinView::loadDirectory(const KUrl& url, bool reload)
983 {
984 if (!url.isValid()) {
985 const QString location(url.pathOrUrl());
986 if (location.isEmpty()) {
987 emit errorMessage(i18nc("@info:status", "The location is empty."));
988 } else {
989 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
990 }
991 return;
992 }
993
994 m_loadingDirectory = true;
995
996 m_dirLister->stop();
997 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
998
999 if (isColumnViewActive()) {
1000 // adjusting the directory lister is not enough in the case of the
1001 // column view, as each column has its own directory lister internally...
1002 if (reload) {
1003 m_columnView->reload();
1004 } else {
1005 m_columnView->showColumn(url);
1006 }
1007 }
1008 }
1009
1010 KUrl DolphinView::viewPropertiesUrl() const
1011 {
1012 if (isColumnViewActive()) {
1013 return m_columnView->rootUrl();
1014 }
1015
1016 return url();
1017 }
1018
1019 void DolphinView::applyViewProperties(const KUrl& url)
1020 {
1021 if (isColumnViewActive() && rootUrl().isParentOf(url)) {
1022 // The column view is active, hence don't apply the view properties
1023 // of sub directories (represented by columns) to the view. The
1024 // view always represents the properties of the first column.
1025 return;
1026 }
1027
1028 const ViewProperties props(url);
1029
1030 const Mode mode = props.viewMode();
1031 if (m_mode != mode) {
1032 m_mode = mode;
1033 createView();
1034 emit modeChanged();
1035 }
1036 if (itemView() == 0) {
1037 createView();
1038 }
1039 Q_ASSERT(itemView() != 0);
1040 Q_ASSERT(m_fileItemDelegate != 0);
1041
1042 const bool showHiddenFiles = props.showHiddenFiles();
1043 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
1044 m_dirLister->setShowingDotFiles(showHiddenFiles);
1045 emit showHiddenFilesChanged();
1046 }
1047
1048 m_storedCategorizedSorting = props.categorizedSorting();
1049 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1050 if (categorized != m_proxyModel->isCategorizedModel()) {
1051 m_proxyModel->setCategorizedModel(categorized);
1052 emit categorizedSortingChanged();
1053 }
1054
1055 const DolphinView::Sorting sorting = props.sorting();
1056 if (sorting != m_proxyModel->sorting()) {
1057 m_proxyModel->setSorting(sorting);
1058 emit sortingChanged(sorting);
1059 }
1060
1061 const Qt::SortOrder sortOrder = props.sortOrder();
1062 if (sortOrder != m_proxyModel->sortOrder()) {
1063 m_proxyModel->setSortOrder(sortOrder);
1064 emit sortOrderChanged(sortOrder);
1065 }
1066
1067 KFileItemDelegate::InformationList info = props.additionalInfo();
1068 if (info != m_fileItemDelegate->showInformation()) {
1069 m_fileItemDelegate->setShowInformation(info);
1070 emit additionalInfoChanged();
1071 }
1072
1073 const bool showPreview = props.showPreview();
1074 if (showPreview != m_showPreview) {
1075 m_showPreview = showPreview;
1076 m_iconManager->setShowPreview(showPreview);
1077 emit showPreviewChanged();
1078 }
1079 }
1080
1081 void DolphinView::createView()
1082 {
1083 deleteView();
1084 Q_ASSERT(m_iconsView == 0);
1085 Q_ASSERT(m_detailsView == 0);
1086 Q_ASSERT(m_columnView == 0);
1087
1088 QAbstractItemView* view = 0;
1089 switch (m_mode) {
1090 case IconsView: {
1091 m_iconsView = new DolphinIconsView(this, m_controller);
1092 view = m_iconsView;
1093 break;
1094 }
1095
1096 case DetailsView:
1097 m_detailsView = new DolphinDetailsView(this, m_controller);
1098 view = m_detailsView;
1099 break;
1100
1101 case ColumnView:
1102 m_columnView = new DolphinColumnView(this, m_controller);
1103 view = m_columnView;
1104 break;
1105 }
1106
1107 Q_ASSERT(view != 0);
1108 view->installEventFilter(this);
1109
1110 m_controller->setItemView(view);
1111
1112 m_fileItemDelegate = new KFileItemDelegate(view);
1113 view->setItemDelegate(m_fileItemDelegate);
1114
1115 view->setModel(m_proxyModel);
1116 if (m_selectionModel != 0) {
1117 view->setSelectionModel(m_selectionModel);
1118 } else {
1119 m_selectionModel = view->selectionModel();
1120 }
1121
1122 // reparent the selection model, as it should not be deleted
1123 // when deleting the model
1124 m_selectionModel->setParent(this);
1125
1126 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
1127
1128 m_iconManager = new IconManager(view, m_proxyModel);
1129 m_iconManager->setShowPreview(m_showPreview);
1130
1131 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1132 m_toolTipManager = new ToolTipManager(view, m_proxyModel);
1133 }
1134
1135 m_topLayout->insertWidget(1, view);
1136
1137 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
1138 this, SLOT(emitSelectionChangedSignal()));
1139 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1140 this, SLOT(emitContentsMoved()));
1141 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1142 this, SLOT(emitContentsMoved()));
1143 }
1144
1145 void DolphinView::deleteView()
1146 {
1147 QAbstractItemView* view = itemView();
1148 if (view != 0) {
1149 m_topLayout->removeWidget(view);
1150 view->close();
1151 view->deleteLater();
1152 view = 0;
1153 m_iconsView = 0;
1154 m_detailsView = 0;
1155 m_columnView = 0;
1156 m_fileItemDelegate = 0;
1157 m_iconManager = 0;
1158 m_toolTipManager = 0;
1159 }
1160 }
1161
1162 QAbstractItemView* DolphinView::itemView() const
1163 {
1164 if (m_detailsView != 0) {
1165 return m_detailsView;
1166 } else if (m_columnView != 0) {
1167 return m_columnView;
1168 }
1169
1170 return m_iconsView;
1171 }
1172
1173 bool DolphinView::isCutItem(const KFileItem& item) const
1174 {
1175 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1176 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
1177
1178 const KUrl& itemUrl = item.url();
1179 KUrl::List::const_iterator it = cutUrls.begin();
1180 const KUrl::List::const_iterator end = cutUrls.end();
1181 while (it != end) {
1182 if (*it == itemUrl) {
1183 return true;
1184 }
1185 ++it;
1186 }
1187
1188 return false;
1189 }
1190
1191 void DolphinView::pasteToUrl(const KUrl& url)
1192 {
1193 QClipboard* clipboard = QApplication::clipboard();
1194 const QMimeData* mimeData = clipboard->mimeData();
1195
1196 const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
1197 if (KonqMimeData::decodeIsCutSelection(mimeData)) {
1198 KonqOperations::copy(this, KonqOperations::MOVE, sourceUrls, url);
1199 emit doingOperation(KIO::FileUndoManager::Move);
1200 clipboard->clear();
1201 } else {
1202 KonqOperations::copy(this, KonqOperations::COPY, sourceUrls, url);
1203 emit doingOperation(KIO::FileUndoManager::Copy);
1204 }
1205 }
1206
1207 void DolphinView::slotRequestUrlChange(const KUrl& url)
1208 {
1209 emit requestUrlChange(url);
1210 m_controller->setUrl(url);
1211 }
1212
1213 void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
1214 {
1215 if (oldUrl == m_controller->url()) {
1216 m_controller->setUrl(newUrl);
1217 }
1218 }
1219
1220 #include "dolphinview.moc"