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