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