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