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