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