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