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