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