1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
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. *
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. *
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 ***************************************************************************/
21 #include "dolphinview.h"
22 #include <ktoggleaction.h>
23 #include <kactioncollection.h>
25 #include <QApplication>
28 #include <QItemSelection>
34 #include <kcolorscheme.h>
35 #include <kdirlister.h>
36 #include <kfileitemdelegate.h>
38 #include <kiconeffect.h>
39 #include <kio/deletejob.h>
40 #include <kio/netaccess.h>
41 #include <kio/previewjob.h>
43 #include <kmimetyperesolver.h>
44 #include <konqmimedata.h>
45 #include <konq_operations.h>
48 #include "dolphinmodel.h"
49 #include "dolphincolumnview.h"
50 #include "dolphincontroller.h"
51 #include "dolphinsortfilterproxymodel.h"
52 #include "dolphindetailsview.h"
53 #include "dolphiniconsview.h"
54 #include "renamedialog.h"
55 #include "viewproperties.h"
56 #include "dolphinsettings.h"
57 #include "dolphin_generalsettings.h"
59 DolphinView::DolphinView(QWidget
* parent
,
61 KDirLister
* dirLister
,
62 DolphinModel
* dolphinModel
,
63 DolphinSortFilterProxyModel
* proxyModel
) :
67 m_loadingDirectory(false),
68 m_storedCategorizedSorting(false),
69 m_mode(DolphinView::IconsView
),
75 m_fileItemDelegate(0),
77 m_dolphinModel(dolphinModel
),
78 m_dirLister(dirLister
),
79 m_proxyModel(proxyModel
),
82 setFocusPolicy(Qt::StrongFocus
);
83 m_topLayout
= new QVBoxLayout(this);
84 m_topLayout
->setSpacing(0);
85 m_topLayout
->setMargin(0);
87 QClipboard
* clipboard
= QApplication::clipboard();
88 connect(clipboard
, SIGNAL(dataChanged()),
89 this, SLOT(updateCutItems()));
91 connect(m_dirLister
, SIGNAL(completed()),
92 this, SLOT(updateCutItems()));
93 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
94 this, SLOT(generatePreviews(const KFileItemList
&)));
96 m_controller
= new DolphinController(this);
97 m_controller
->setUrl(url
);
99 // Receiver of the DolphinView signal 'urlChanged()' don't need
100 // to care whether the internal controller changed the URL already or whether
101 // the controller just requested an URL change and will be updated later.
102 // In both cases the URL has been changed:
103 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
104 this, SIGNAL(urlChanged(const KUrl
&)));
105 connect(m_controller
, SIGNAL(requestUrlChange(const KUrl
&)),
106 this, SIGNAL(urlChanged(const KUrl
&)));
108 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&)),
109 this, SLOT(openContextMenu(const QPoint
&)));
110 connect(m_controller
, SIGNAL(urlsDropped(const KUrl::List
&, const KUrl
&, const KFileItem
&)),
111 this, SLOT(dropUrls(const KUrl::List
&, const KUrl
&, const KFileItem
&)));
112 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
113 this, SLOT(updateSorting(DolphinView::Sorting
)));
114 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
115 this, SLOT(updateSortOrder(Qt::SortOrder
)));
116 connect(m_controller
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
117 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList
&)));
118 connect(m_controller
, SIGNAL(itemTriggered(const KFileItem
&)),
119 this, SLOT(triggerItem(const KFileItem
&)));
120 connect(m_controller
, SIGNAL(activated()),
121 this, SLOT(activate()));
122 connect(m_controller
, SIGNAL(itemEntered(const KFileItem
&)),
123 this, SLOT(showHoverInformation(const KFileItem
&)));
124 connect(m_controller
, SIGNAL(viewportEntered()),
125 this, SLOT(clearHoverInformation()));
127 applyViewProperties(url
);
128 m_topLayout
->addWidget(itemView());
131 DolphinView::~DolphinView()
133 if (m_previewJob
!= 0) {
134 m_previewJob
->kill();
139 const KUrl
& DolphinView::url() const
141 return m_controller
->url();
144 KUrl
DolphinView::rootUrl() const
146 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
149 void DolphinView::setActive(bool active
)
151 if (active
== m_active
) {
156 m_selectionModel
->clearSelection();
158 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
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());
169 QWidget
* viewport
= itemView()->viewport();
171 palette
.setColor(viewport
->backgroundRole(), color
);
172 viewport
->setPalette(palette
);
180 m_controller
->indicateActivationChange(active
);
183 bool DolphinView::isActive() const
188 void DolphinView::setMode(Mode mode
)
190 if (mode
== m_mode
) {
191 return; // the wished mode is already set
196 if (isColumnViewActive()) {
197 // When changing the mode in the column view, it makes sense
198 // to go back to the root URL of the column view automatically.
199 // Otherwise there it would not be possible to turn off the column view
200 // without focusing the first column.
201 const KUrl root
= rootUrl();
203 m_controller
->setUrl(root
);
208 // It is important to read the view properties _after_ deleting the view,
209 // as e. g. the detail view might adjust the additional information properties
210 // after getting closed:
211 const KUrl viewPropsUrl
= viewPropertiesUrl();
212 ViewProperties
props(viewPropsUrl
);
213 props
.setViewMode(m_mode
);
216 // the file item delegate has been recreated, apply the current
217 // additional information manually
218 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
219 m_fileItemDelegate
->setShowInformation(infoList
);
220 emit
additionalInfoChanged(infoList
);
222 // Not all view modes support categorized sorting. Adjust the sorting model
223 // if changing the view mode results in a change of the categorized sorting
225 m_storedCategorizedSorting
= props
.categorizedSorting();
226 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
227 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
228 m_proxyModel
->setCategorizedModel(categorized
);
229 emit
categorizedSortingChanged();
235 DolphinView::Mode
DolphinView::mode() const
240 void DolphinView::setShowPreview(bool show
)
242 if (m_showPreview
== show
) {
246 const KUrl viewPropsUrl
= viewPropertiesUrl();
247 ViewProperties
props(viewPropsUrl
);
248 props
.setShowPreview(show
);
250 m_showPreview
= show
;
252 emit
showPreviewChanged();
254 loadDirectory(viewPropsUrl
, true);
257 bool DolphinView::showPreview() const
259 return m_showPreview
;
262 void DolphinView::setShowHiddenFiles(bool show
)
264 if (m_dirLister
->showingDotFiles() == show
) {
268 const KUrl viewPropsUrl
= viewPropertiesUrl();
269 ViewProperties
props(viewPropsUrl
);
270 props
.setShowHiddenFiles(show
);
272 m_dirLister
->setShowingDotFiles(show
);
273 emit
showHiddenFilesChanged();
275 loadDirectory(viewPropsUrl
, true);
278 bool DolphinView::showHiddenFiles() const
280 return m_dirLister
->showingDotFiles();
283 void DolphinView::setCategorizedSorting(bool categorized
)
285 if (categorized
== categorizedSorting()) {
289 // setCategorizedSorting(true) may only get invoked
290 // if the view supports categorized sorting
291 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
293 ViewProperties
props(viewPropertiesUrl());
294 props
.setCategorizedSorting(categorized
);
297 m_storedCategorizedSorting
= categorized
;
298 m_proxyModel
->setCategorizedModel(categorized
);
300 emit
categorizedSortingChanged();
303 bool DolphinView::categorizedSorting() const
305 // If all view modes would support categorized sorting, returning
306 // m_proxyModel->isCategorizedModel() would be the way to go. As
307 // currently only the icons view supports caterized sorting, we remember
308 // the stored view properties state in m_storedCategorizedSorting and
309 // return this state. The application takes care to disable the corresponding
310 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
311 // that this setting is not applied to the current view mode.
312 return m_storedCategorizedSorting
;
315 bool DolphinView::supportsCategorizedSorting() const
317 return m_iconsView
!= 0;
320 void DolphinView::selectAll()
322 itemView()->selectAll();
325 void DolphinView::invertSelection()
327 if (isColumnViewActive()) {
328 // QAbstractItemView does not offer a virtual method invertSelection()
329 // as counterpart to QAbstractItemView::selectAll(). This makes it
330 // necessary to delegate the inverting of the selection to the
331 // column view, as only the selection of the active column should
333 m_columnView
->invertSelection();
335 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
336 const QAbstractItemModel
* itemModel
= selectionModel
->model();
338 const QModelIndex topLeft
= itemModel
->index(0, 0);
339 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
340 itemModel
->columnCount() - 1);
342 const QItemSelection
selection(topLeft
, bottomRight
);
343 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
347 bool DolphinView::hasSelection() const
349 return itemView()->selectionModel()->hasSelection();
352 void DolphinView::clearSelection()
354 itemView()->selectionModel()->clear();
357 KFileItemList
DolphinView::selectedItems() const
359 const QAbstractItemView
* view
= itemView();
361 // Our view has a selection, we will map them back to the DolphinModel
362 // and then fill the KFileItemList.
363 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
365 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
366 KFileItemList itemList
;
368 const QModelIndexList indexList
= selection
.indexes();
369 foreach (QModelIndex index
, indexList
) {
370 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
371 if (!item
.isNull()) {
372 itemList
.append(item
);
379 KUrl::List
DolphinView::selectedUrls() const
382 const KFileItemList list
= selectedItems();
383 foreach (KFileItem item
, list
) {
384 urls
.append(item
.url());
389 KFileItem
DolphinView::fileItem(const QModelIndex
& index
) const
391 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
392 return m_dolphinModel
->itemForIndex(dolphinModelIndex
);
395 void DolphinView::setContentsPosition(int x
, int y
)
397 QAbstractItemView
* view
= itemView();
399 // the ColumnView takes care itself for the horizontal scrolling
400 if (!isColumnViewActive()) {
401 view
->horizontalScrollBar()->setValue(x
);
403 view
->verticalScrollBar()->setValue(y
);
405 m_loadingDirectory
= false;
408 QPoint
DolphinView::contentsPosition() const
410 const int x
= itemView()->horizontalScrollBar()->value();
411 const int y
= itemView()->verticalScrollBar()->value();
415 void DolphinView::zoomIn()
417 m_controller
->triggerZoomIn();
420 void DolphinView::zoomOut()
422 m_controller
->triggerZoomOut();
425 bool DolphinView::isZoomInPossible() const
427 return m_controller
->isZoomInPossible();
430 bool DolphinView::isZoomOutPossible() const
432 return m_controller
->isZoomOutPossible();
435 void DolphinView::setSorting(Sorting sorting
)
437 if (sorting
!= this->sorting()) {
438 updateSorting(sorting
);
442 DolphinView::Sorting
DolphinView::sorting() const
444 return m_proxyModel
->sorting();
447 void DolphinView::setSortOrder(Qt::SortOrder order
)
449 if (sortOrder() != order
) {
450 updateSortOrder(order
);
454 Qt::SortOrder
DolphinView::sortOrder() const
456 return m_proxyModel
->sortOrder();
459 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
461 const KUrl viewPropsUrl
= viewPropertiesUrl();
462 ViewProperties
props(viewPropsUrl
);
463 props
.setAdditionalInfo(info
);
464 m_fileItemDelegate
->setShowInformation(info
);
466 emit
additionalInfoChanged(info
);
468 if (itemView() != m_detailsView
) {
469 // the details view requires no reloading of the directory, as it maps
470 // the file item delegate info to its columns internally
471 loadDirectory(viewPropsUrl
, true);
475 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
477 return m_fileItemDelegate
->showInformation();
480 void DolphinView::reload()
483 loadDirectory(url(), true);
486 void DolphinView::refresh()
488 const bool oldActivationState
= m_active
;
492 applyViewProperties(m_controller
->url());
495 setActive(oldActivationState
);
498 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
500 if (m_controller
->url() == url
) {
504 m_controller
->setUrl(url
); // emits urlChanged, which we forward
506 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
507 applyViewProperties(rootUrl
);
508 loadDirectory(rootUrl
);
509 if (itemView() == m_columnView
) {
510 m_columnView
->setRootUrl(rootUrl
);
511 m_columnView
->showColumn(url
);
514 applyViewProperties(url
);
518 emit
startedPathLoading(url
);
521 void DolphinView::setNameFilter(const QString
& nameFilter
)
523 m_proxyModel
->setFilterRegExp(nameFilter
);
525 if (isColumnViewActive()) {
526 // adjusting the directory lister is not enough in the case of the
527 // column view, as each column has its own directory lister internally...
528 m_columnView
->setNameFilter(nameFilter
);
532 void DolphinView::calculateItemCount(int& fileCount
, int& folderCount
)
534 foreach (KFileItem item
, m_dirLister
->items()) {
543 void DolphinView::setUrl(const KUrl
& url
)
545 updateView(url
, KUrl());
548 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
550 QWidget::mouseReleaseEvent(event
);
553 void DolphinView::activate()
558 void DolphinView::triggerItem(const KFileItem
& item
)
560 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
561 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
562 // items are selected by the user, hence don't trigger the
563 // item specified by 'index'
571 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
574 void DolphinView::generatePreviews(const KFileItemList
& items
)
576 if (m_controller
->dolphinView()->showPreview()) {
577 if (m_previewJob
!= 0) {
578 m_previewJob
->kill();
582 m_previewJob
= KIO::filePreview(items
, 128);
583 connect(m_previewJob
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
584 this, SLOT(replaceIcon(const KFileItem
&, const QPixmap
&)));
585 connect(m_previewJob
, SIGNAL(finished(KJob
*)),
586 this, SLOT(slotPreviewJobFinished(KJob
*)));
590 void DolphinView::replaceIcon(const KFileItem
& item
, const QPixmap
& pixmap
)
592 Q_ASSERT(!item
.isNull());
593 if (!m_showPreview
|| (item
.url().directory() != m_dirLister
->url().path())) {
594 // the preview has been deactivated in the meanwhile or the preview
595 // job is still working on items of an older URL, hence
596 // the item is not part of the directory model anymore
600 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
601 if (idx
.isValid() && (idx
.column() == 0)) {
602 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
603 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
604 KIconEffect iconEffect
;
605 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
606 m_dolphinModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
608 m_dolphinModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
613 void DolphinView::emitSelectionChangedSignal()
615 emit
selectionChanged(DolphinView::selectedItems());
618 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
620 if (!url
.isValid()) {
621 const QString
location(url
.pathOrUrl());
622 if (location
.isEmpty()) {
623 emit
errorMessage(i18nc("@info:status", "The location is empty."));
625 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
630 m_cutItemsCache
.clear();
631 m_loadingDirectory
= true;
634 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
636 if (isColumnViewActive()) {
637 // adjusting the directory lister is not enough in the case of the
638 // column view, as each column has its own directory lister internally...
640 m_columnView
->reload();
642 m_columnView
->showColumn(url
);
647 KUrl
DolphinView::viewPropertiesUrl() const
649 if (isColumnViewActive()) {
650 return m_dirLister
->url();
656 void DolphinView::applyViewProperties(const KUrl
& url
)
658 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
659 // The column view is active, hence don't apply the view properties
660 // of sub directories (represented by columns) to the view. The
661 // view always represents the properties of the first column.
665 const ViewProperties
props(url
);
667 const Mode mode
= props
.viewMode();
668 if (m_mode
!= mode
) {
673 if (itemView() == 0) {
676 Q_ASSERT(itemView() != 0);
677 Q_ASSERT(m_fileItemDelegate
!= 0);
679 const bool showHiddenFiles
= props
.showHiddenFiles();
680 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
681 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
682 emit
showHiddenFilesChanged();
685 m_storedCategorizedSorting
= props
.categorizedSorting();
686 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
687 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
688 m_proxyModel
->setCategorizedModel(categorized
);
689 emit
categorizedSortingChanged();
692 const DolphinView::Sorting sorting
= props
.sorting();
693 if (sorting
!= m_proxyModel
->sorting()) {
694 m_proxyModel
->setSorting(sorting
);
695 emit
sortingChanged(sorting
);
698 const Qt::SortOrder sortOrder
= props
.sortOrder();
699 if (sortOrder
!= m_proxyModel
->sortOrder()) {
700 m_proxyModel
->setSortOrder(sortOrder
);
701 emit
sortOrderChanged(sortOrder
);
704 KFileItemDelegate::InformationList info
= props
.additionalInfo();
705 if (info
!= m_fileItemDelegate
->showInformation()) {
706 m_fileItemDelegate
->setShowInformation(info
);
707 emit
additionalInfoChanged(info
);
710 const bool showPreview
= props
.showPreview();
711 if (showPreview
!= m_showPreview
) {
712 m_showPreview
= showPreview
;
713 emit
showPreviewChanged();
717 void DolphinView::changeSelection(const KFileItemList
& selection
)
720 if (selection
.isEmpty()) {
723 const KUrl
& baseUrl
= url();
725 QItemSelection new_selection
;
726 foreach(const KFileItem
& item
, selection
) {
727 url
= item
.url().upUrl();
728 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
729 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
730 new_selection
.select(index
, index
);
733 itemView()->selectionModel()->select(new_selection
,
734 QItemSelectionModel::ClearAndSelect
735 | QItemSelectionModel::Current
);
738 void DolphinView::openContextMenu(const QPoint
& pos
)
742 const QModelIndex index
= itemView()->indexAt(pos
);
743 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
744 item
= fileItem(index
);
747 emit
requestContextMenu(item
, url());
750 void DolphinView::dropUrls(const KUrl::List
& urls
,
751 const KUrl
& destPath
,
752 const KFileItem
& destItem
)
754 const KUrl
& destination
= !destItem
.isNull() && destItem
.isDir() ?
755 destItem
.url() : destPath
;
756 const KUrl sourceDir
= KUrl(urls
.first().directory());
757 if (sourceDir
!= destination
) {
758 dropUrls(urls
, destination
);
762 void DolphinView::dropUrls(const KUrl::List
& urls
,
763 const KUrl
& destination
)
765 emit
urlsDropped(urls
, destination
);
768 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
770 ViewProperties
props(viewPropertiesUrl());
771 props
.setSorting(sorting
);
773 m_proxyModel
->setSorting(sorting
);
775 emit
sortingChanged(sorting
);
778 void DolphinView::updateSortOrder(Qt::SortOrder order
)
780 ViewProperties
props(viewPropertiesUrl());
781 props
.setSortOrder(order
);
783 m_proxyModel
->setSortOrder(order
);
785 emit
sortOrderChanged(order
);
788 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
790 ViewProperties
props(viewPropertiesUrl());
791 props
.setAdditionalInfo(info
);
794 m_fileItemDelegate
->setShowInformation(info
);
796 emit
additionalInfoChanged(info
);
800 void DolphinView::emitContentsMoved()
802 // only emit the contents moved signal if:
803 // - no directory loading is ongoing (this would reset the contents position
805 // - if the Column View is active: the column view does an automatic
806 // positioning during the loading operation, which must be remembered
807 if (!m_loadingDirectory
|| isColumnViewActive()) {
808 const QPoint
pos(contentsPosition());
809 emit
contentsMoved(pos
.x(), pos
.y());
813 void DolphinView::updateCutItems()
815 // restore the icons of all previously selected items to the
817 QList
<CutItem
>::const_iterator it
= m_cutItemsCache
.begin();
818 QList
<CutItem
>::const_iterator end
= m_cutItemsCache
.end();
820 const QModelIndex index
= m_dolphinModel
->indexForUrl((*it
).url
);
821 if (index
.isValid()) {
822 m_dolphinModel
->setData(index
, QIcon((*it
).pixmap
), Qt::DecorationRole
);
826 m_cutItemsCache
.clear();
828 // ... and apply an item effect to all currently cut items
829 applyCutItemEffect();
832 void DolphinView::showHoverInformation(const KFileItem
& item
)
834 if (hasSelection() || !m_active
) {
838 emit
requestItemInfo(item
);
841 void DolphinView::clearHoverInformation()
844 emit
requestItemInfo(KFileItem());
849 void DolphinView::createView()
852 Q_ASSERT(m_iconsView
== 0);
853 Q_ASSERT(m_detailsView
== 0);
854 Q_ASSERT(m_columnView
== 0);
856 QAbstractItemView
* view
= 0;
859 m_iconsView
= new DolphinIconsView(this, m_controller
);
865 m_detailsView
= new DolphinDetailsView(this, m_controller
);
866 view
= m_detailsView
;
870 m_columnView
= new DolphinColumnView(this, m_controller
);
877 m_fileItemDelegate
= new KFileItemDelegate(view
);
878 view
->setItemDelegate(m_fileItemDelegate
);
880 view
->setModel(m_proxyModel
);
881 if (m_selectionModel
!= 0) {
882 view
->setSelectionModel(m_selectionModel
);
884 m_selectionModel
= view
->selectionModel();
887 // reparent the selection model, as it should not be deleted
888 // when deleting the model
889 m_selectionModel
->setParent(this);
891 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
893 new KMimeTypeResolver(view
, m_dolphinModel
);
894 m_topLayout
->insertWidget(1, view
);
896 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
897 this, SLOT(emitSelectionChangedSignal()));
898 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
899 this, SLOT(emitContentsMoved()));
900 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
901 this, SLOT(emitContentsMoved()));
904 void DolphinView::deleteView()
906 QAbstractItemView
* view
= itemView();
908 m_topLayout
->removeWidget(view
);
915 m_fileItemDelegate
= 0;
919 QAbstractItemView
* DolphinView::itemView() const
921 if (m_detailsView
!= 0) {
922 return m_detailsView
;
923 } else if (m_columnView
!= 0) {
930 bool DolphinView::isCutItem(const KFileItem
& item
) const
932 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
933 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
935 const KUrl
& itemUrl
= item
.url();
936 KUrl::List::const_iterator it
= cutUrls
.begin();
937 const KUrl::List::const_iterator end
= cutUrls
.end();
939 if (*it
== itemUrl
) {
948 void DolphinView::applyCutItemEffect()
950 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
951 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
955 KFileItemList
items(m_dirLister
->items());
956 KFileItemList::const_iterator it
= items
.begin();
957 const KFileItemList::const_iterator end
= items
.end();
959 const KFileItem item
= *it
;
960 if (isCutItem(item
)) {
961 const QModelIndex index
= m_dolphinModel
->indexForItem(item
);
962 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
963 if (value
.type() == QVariant::Icon
) {
964 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
965 QPixmap pixmap
= icon
.pixmap(128, 128);
967 // remember current pixmap for the item to be able
968 // to restore it when other items get cut
970 cutItem
.url
= item
.url();
971 cutItem
.pixmap
= pixmap
;
972 m_cutItemsCache
.append(cutItem
);
974 // apply icon effect to the cut item
975 KIconEffect iconEffect
;
976 pixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
977 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
984 KToggleAction
* DolphinView::iconsModeAction(KActionCollection
* actionCollection
)
986 KToggleAction
* iconsView
= actionCollection
->add
<KToggleAction
>("icons");
987 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
988 iconsView
->setShortcut(Qt::CTRL
| Qt::Key_1
);
989 iconsView
->setIcon(KIcon("fileview-icon"));
990 iconsView
->setData(QVariant::fromValue(IconsView
));
994 KToggleAction
* DolphinView::detailsModeAction(KActionCollection
* actionCollection
)
996 KToggleAction
* detailsView
= actionCollection
->add
<KToggleAction
>("details");
997 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
998 detailsView
->setShortcut(Qt::CTRL
| Qt::Key_2
);
999 detailsView
->setIcon(KIcon("fileview-detailed"));
1000 detailsView
->setData(QVariant::fromValue(DetailsView
));
1004 KToggleAction
* DolphinView::columnsModeAction(KActionCollection
* actionCollection
)
1006 KToggleAction
* columnView
= actionCollection
->add
<KToggleAction
>("columns");
1007 columnView
->setText(i18nc("@action:inmenu View Mode", "Columns"));
1008 columnView
->setShortcut(Qt::CTRL
| Qt::Key_3
);
1009 columnView
->setIcon(KIcon("fileview-column"));
1010 columnView
->setData(QVariant::fromValue(ColumnView
));
1014 QString
DolphinView::currentViewModeActionName() const
1017 case DolphinView::IconsView
:
1019 case DolphinView::DetailsView
:
1021 case DolphinView::ColumnView
:
1024 return QString(); // can't happen
1027 void DolphinView::renameSelectedItems()
1029 const KFileItemList items
= selectedItems();
1030 if (items
.count() > 1) {
1031 // More than one item has been selected for renaming. Open
1032 // a rename dialog and rename all items afterwards.
1033 RenameDialog
dialog(this, items
);
1034 if (dialog
.exec() == QDialog::Rejected
) {
1038 const QString newName
= dialog
.newName();
1039 if (newName
.isEmpty()) {
1040 emit
errorMessage(dialog
.errorString());
1042 // TODO: check how this can be integrated into KonqFileUndoManager/KonqOperations
1043 // as one operation instead of n rename operations like it is done now...
1044 Q_ASSERT(newName
.contains('#'));
1046 // iterate through all selected items and rename them...
1047 const int replaceIndex
= newName
.indexOf('#');
1048 Q_ASSERT(replaceIndex
>= 0);
1051 KFileItemList::const_iterator it
= items
.begin();
1052 const KFileItemList::const_iterator end
= items
.end();
1054 const KUrl
& oldUrl
= (*it
).url();
1056 number
.setNum(index
++);
1058 QString
name(newName
);
1059 name
.replace(replaceIndex
, 1, number
);
1061 if (oldUrl
.fileName() != name
) {
1062 KUrl newUrl
= oldUrl
;
1063 newUrl
.setFileName(name
);
1064 KonqOperations::rename(this, oldUrl
, newUrl
);
1065 emit
doingOperation(KonqFileUndoManager::RENAME
);
1071 // Only one item has been selected for renaming. Use the custom
1072 // renaming mechanism from the views.
1073 Q_ASSERT(items
.count() == 1);
1075 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
1076 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
1077 // is a benefit for the user at all -> let's wait for some input first...
1078 RenameDialog
dialog(this, items
);
1079 if (dialog
.exec() == QDialog::Rejected
) {
1083 const QString
& newName
= dialog
.newName();
1084 if (newName
.isEmpty()) {
1085 emit
errorMessage(dialog
.errorString());
1087 const KUrl
& oldUrl
= items
.first().url();
1088 KUrl newUrl
= oldUrl
;
1089 newUrl
.setFileName(newName
);
1090 KonqOperations::rename(this, oldUrl
, newUrl
);
1091 emit
doingOperation(KonqFileUndoManager::RENAME
);
1096 void DolphinView::trashSelectedItems()
1098 emit
doingOperation(KonqFileUndoManager::TRASH
);
1099 KonqOperations::del(this, KonqOperations::TRASH
, selectedUrls());
1102 void DolphinView::deleteSelectedItems()
1104 const KUrl::List list
= selectedUrls();
1105 const bool del
= KonqOperations::askDeleteConfirmation(list
,
1106 KonqOperations::DEL
,
1107 KonqOperations::DEFAULT_CONFIRMATION
,
1111 KIO::Job
* job
= KIO::del(list
);
1112 connect(job
, SIGNAL(result(KJob
*)),
1113 this, SLOT(slotDeleteFileFinished(KJob
*)));
1117 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1119 if (job
->error() == 0) {
1120 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1122 emit
errorMessage(job
->errorString());
1126 void DolphinView::slotPreviewJobFinished(KJob
* job
)
1128 Q_ASSERT(job
== m_previewJob
);
1132 void DolphinView::cutSelectedItems()
1134 QMimeData
* mimeData
= new QMimeData();
1135 const KUrl::List kdeUrls
= selectedUrls();
1136 const KUrl::List mostLocalUrls
;
1137 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, true);
1138 QApplication::clipboard()->setMimeData(mimeData
);
1141 void DolphinView::copySelectedItems()
1143 QMimeData
* mimeData
= new QMimeData();
1144 const KUrl::List kdeUrls
= selectedUrls();
1145 const KUrl::List mostLocalUrls
;
1146 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, false);
1147 QApplication::clipboard()->setMimeData(mimeData
);
1150 void DolphinView::paste()
1152 QClipboard
* clipboard
= QApplication::clipboard();
1153 const QMimeData
* mimeData
= clipboard
->mimeData();
1155 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1157 // per default the pasting is done into the current Url of the view
1158 KUrl
destUrl(url());
1160 // check whether the pasting should be done into a selected directory
1161 const KUrl::List selectedUrls
= this->selectedUrls();
1162 if (selectedUrls
.count() == 1) {
1163 const KFileItem
fileItem(S_IFDIR
,
1165 selectedUrls
.first(),
1167 if (fileItem
.isDir()) {
1168 // only one item is selected which is a directory, hence paste
1169 // into this directory
1170 destUrl
= selectedUrls
.first();
1174 if (KonqMimeData::decodeIsCutSelection(mimeData
)) {
1175 KonqOperations::copy(this, KonqOperations::MOVE
, sourceUrls
, destUrl
);
1176 emit
doingOperation(KonqFileUndoManager::MOVE
);
1179 KonqOperations::copy(this, KonqOperations::COPY
, sourceUrls
, destUrl
);
1180 emit
doingOperation(KonqFileUndoManager::COPY
);
1184 QPair
<bool, QString
> DolphinView::pasteInfo() const
1186 QPair
<bool, QString
> ret
;
1187 QClipboard
* clipboard
= QApplication::clipboard();
1188 const QMimeData
* mimeData
= clipboard
->mimeData();
1190 KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1191 if (!urls
.isEmpty()) {
1193 ret
.second
= i18ncp("@action:inmenu", "Paste One File", "Paste %1 Files", urls
.count());
1196 ret
.second
= i18nc("@action:inmenu", "Paste");
1200 const KUrl::List urls
= selectedUrls();
1201 const uint count
= urls
.count();
1203 // pasting should not be allowed when more than one file
1206 } else if (count
== 1) {
1207 // Only one file is selected. Pasting is only allowed if this
1208 // file is a directory.
1209 // TODO: this doesn't work with remote protocols; instead we need a
1210 // m_activeViewContainer->selectedFileItems() to get the real KFileItems
1211 const KFileItem
fileItem(S_IFDIR
,
1215 ret
.first
= fileItem
.isDir();
1221 KAction
* DolphinView::createRenameAction(KActionCollection
* collection
)
1223 KAction
* rename
= collection
->addAction("rename");
1224 rename
->setText(i18nc("@action:inmenu File", "Rename..."));
1225 rename
->setShortcut(Qt::Key_F2
);
1229 KAction
* DolphinView::createMoveToTrashAction(KActionCollection
* collection
)
1231 KAction
* moveToTrash
= collection
->addAction("move_to_trash");
1232 moveToTrash
->setText(i18nc("@action:inmenu File", "Move to Trash"));
1233 moveToTrash
->setIcon(KIcon("user-trash"));
1234 moveToTrash
->setShortcut(QKeySequence::Delete
);
1238 KAction
* DolphinView::createDeleteAction(KActionCollection
* collection
)
1240 KAction
* deleteAction
= collection
->addAction("delete");
1241 deleteAction
->setIcon(KIcon("edit-delete"));
1242 deleteAction
->setText(i18nc("@action:inmenu File", "Delete"));
1243 deleteAction
->setShortcut(Qt::SHIFT
| Qt::Key_Delete
);
1244 return deleteAction
;
1247 #include "dolphinview.moc"