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