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