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>
42 #include <kmimetyperesolver.h>
43 #include <konqmimedata.h>
44 #include <konq_operations.h>
47 #include "dolphinmodel.h"
48 #include "dolphincolumnview.h"
49 #include "dolphincontroller.h"
50 #include "dolphinsortfilterproxymodel.h"
51 #include "dolphindetailsview.h"
52 #include "dolphiniconsview.h"
53 #include "renamedialog.h"
54 #include "viewproperties.h"
55 #include "dolphinsettings.h"
56 #include "dolphin_generalsettings.h"
58 DolphinView::DolphinView(QWidget
* parent
,
60 KDirLister
* dirLister
,
61 DolphinModel
* dolphinModel
,
62 DolphinSortFilterProxyModel
* proxyModel
) :
66 m_loadingDirectory(false),
67 m_storedCategorizedSorting(false),
68 m_mode(DolphinView::IconsView
),
74 m_fileItemDelegate(0),
76 m_dolphinModel(dolphinModel
),
77 m_dirLister(dirLister
),
78 m_proxyModel(proxyModel
)
80 setFocusPolicy(Qt::StrongFocus
);
81 m_topLayout
= new QVBoxLayout(this);
82 m_topLayout
->setSpacing(0);
83 m_topLayout
->setMargin(0);
85 QClipboard
* clipboard
= QApplication::clipboard();
86 connect(clipboard
, SIGNAL(dataChanged()),
87 this, SLOT(updateCutItems()));
89 connect(m_dirLister
, SIGNAL(completed()),
90 this, SLOT(updateCutItems()));
91 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
92 this, SLOT(generatePreviews(const KFileItemList
&)));
94 m_controller
= new DolphinController(this);
95 m_controller
->setUrl(url
);
97 // Receiver of the DolphinView signal 'urlChanged()' don't need
98 // to care whether the internal controller changed the URL already or whether
99 // the controller just requested an URL change and will be updated later.
100 // In both cases the URL has been changed:
101 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
102 this, SIGNAL(urlChanged(const KUrl
&)));
103 connect(m_controller
, SIGNAL(requestUrlChange(const KUrl
&)),
104 this, SIGNAL(urlChanged(const KUrl
&)));
106 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&)),
107 this, SLOT(openContextMenu(const QPoint
&)));
108 connect(m_controller
, SIGNAL(urlsDropped(const KUrl::List
&, const KUrl
&, const KFileItem
&)),
109 this, SLOT(dropUrls(const KUrl::List
&, const KUrl
&, const KFileItem
&)));
110 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
111 this, SLOT(updateSorting(DolphinView::Sorting
)));
112 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
113 this, SLOT(updateSortOrder(Qt::SortOrder
)));
114 connect(m_controller
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
115 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList
&)));
116 connect(m_controller
, SIGNAL(itemTriggered(const KFileItem
&)),
117 this, SLOT(triggerItem(const KFileItem
&)));
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()));
125 applyViewProperties(url
);
126 m_topLayout
->addWidget(itemView());
129 DolphinView::~DolphinView()
133 const KUrl
& DolphinView::url() const
135 return m_controller
->url();
138 KUrl
DolphinView::rootUrl() const
140 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
143 void DolphinView::setActive(bool active
)
145 if (active
== m_active
) {
151 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
153 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
154 // bypasses the problem when having a split view and changing the active view to
155 // update the some URL dependent states. A nicer approach should be no big deal...
156 emit
urlChanged(url());
157 emit
selectionChanged(selectedItems());
162 QWidget
* viewport
= itemView()->viewport();
164 palette
.setColor(viewport
->backgroundRole(), color
);
165 viewport
->setPalette(palette
);
173 m_controller
->indicateActivationChange(active
);
176 bool DolphinView::isActive() const
181 void DolphinView::setMode(Mode mode
)
183 if (mode
== m_mode
) {
184 return; // the wished mode is already set
189 if (isColumnViewActive()) {
190 // When changing the mode in the column view, it makes sense
191 // to go back to the root URL of the column view automatically.
192 // Otherwise there it would not be possible to turn off the column view
193 // without focusing the first column.
194 const KUrl root
= rootUrl();
196 m_controller
->setUrl(root
);
201 // It is important to read the view properties _after_ deleting the view,
202 // as e. g. the detail view might adjust the additional information properties
203 // after getting closed:
204 const KUrl viewPropsUrl
= viewPropertiesUrl();
205 ViewProperties
props(viewPropsUrl
);
206 props
.setViewMode(m_mode
);
209 // the file item delegate has been recreated, apply the current
210 // additional information manually
211 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
212 m_fileItemDelegate
->setShowInformation(infoList
);
213 emit
additionalInfoChanged(infoList
);
215 // Not all view modes support categorized sorting. Adjust the sorting model
216 // if changing the view mode results in a change of the categorized sorting
218 m_storedCategorizedSorting
= props
.categorizedSorting();
219 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
220 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
221 m_proxyModel
->setCategorizedModel(categorized
);
222 emit
categorizedSortingChanged();
228 DolphinView::Mode
DolphinView::mode() const
233 void DolphinView::setShowPreview(bool show
)
235 if (m_showPreview
== show
) {
239 const KUrl viewPropsUrl
= viewPropertiesUrl();
240 ViewProperties
props(viewPropsUrl
);
241 props
.setShowPreview(show
);
243 m_showPreview
= show
;
245 emit
showPreviewChanged();
247 loadDirectory(viewPropsUrl
, true);
250 bool DolphinView::showPreview() const
252 return m_showPreview
;
255 void DolphinView::setShowHiddenFiles(bool show
)
257 if (m_dirLister
->showingDotFiles() == show
) {
261 const KUrl viewPropsUrl
= viewPropertiesUrl();
262 ViewProperties
props(viewPropsUrl
);
263 props
.setShowHiddenFiles(show
);
265 m_dirLister
->setShowingDotFiles(show
);
266 emit
showHiddenFilesChanged();
268 loadDirectory(viewPropsUrl
, true);
271 bool DolphinView::showHiddenFiles() const
273 return m_dirLister
->showingDotFiles();
276 void DolphinView::setCategorizedSorting(bool categorized
)
278 if (categorized
== categorizedSorting()) {
282 // setCategorizedSorting(true) may only get invoked
283 // if the view supports categorized sorting
284 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
286 ViewProperties
props(viewPropertiesUrl());
287 props
.setCategorizedSorting(categorized
);
290 m_storedCategorizedSorting
= categorized
;
291 m_proxyModel
->setCategorizedModel(categorized
);
293 emit
categorizedSortingChanged();
296 bool DolphinView::categorizedSorting() const
298 // If all view modes would support categorized sorting, returning
299 // m_proxyModel->isCategorizedModel() would be the way to go. As
300 // currently only the icons view supports caterized sorting, we remember
301 // the stored view properties state in m_storedCategorizedSorting and
302 // return this state. The application takes care to disable the corresponding
303 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
304 // that this setting is not applied to the current view mode.
305 return m_storedCategorizedSorting
;
308 bool DolphinView::supportsCategorizedSorting() const
310 return m_iconsView
!= 0;
313 void DolphinView::selectAll()
315 itemView()->selectAll();
318 void DolphinView::invertSelection()
320 if (isColumnViewActive()) {
321 // QAbstractItemView does not offer a virtual method invertSelection()
322 // as counterpart to QAbstractItemView::selectAll(). This makes it
323 // necessary to delegate the inverting of the selection to the
324 // column view, as only the selection of the active column should
326 m_columnView
->invertSelection();
328 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
329 const QAbstractItemModel
* itemModel
= selectionModel
->model();
331 const QModelIndex topLeft
= itemModel
->index(0, 0);
332 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
333 itemModel
->columnCount() - 1);
335 const QItemSelection
selection(topLeft
, bottomRight
);
336 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
340 bool DolphinView::hasSelection() const
342 return itemView()->selectionModel()->hasSelection();
345 void DolphinView::clearSelection()
347 itemView()->selectionModel()->clear();
350 KFileItemList
DolphinView::selectedItems() const
352 const QAbstractItemView
* view
= itemView();
354 // Our view has a selection, we will map them back to the DolphinModel
355 // and then fill the KFileItemList.
356 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
358 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
359 KFileItemList itemList
;
361 const QModelIndexList indexList
= selection
.indexes();
362 foreach (QModelIndex index
, indexList
) {
363 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
364 if (!item
.isNull()) {
365 itemList
.append(item
);
372 KUrl::List
DolphinView::selectedUrls() const
375 const KFileItemList list
= selectedItems();
376 foreach (KFileItem item
, list
) {
377 urls
.append(item
.url());
382 KFileItem
DolphinView::fileItem(const QModelIndex
& index
) const
384 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
385 return m_dolphinModel
->itemForIndex(dolphinModelIndex
);
388 void DolphinView::setContentsPosition(int x
, int y
)
390 QAbstractItemView
* view
= itemView();
392 // the ColumnView takes care itself for the horizontal scrolling
393 if (!isColumnViewActive()) {
394 view
->horizontalScrollBar()->setValue(x
);
396 view
->verticalScrollBar()->setValue(y
);
398 m_loadingDirectory
= false;
401 QPoint
DolphinView::contentsPosition() const
403 const int x
= itemView()->horizontalScrollBar()->value();
404 const int y
= itemView()->verticalScrollBar()->value();
408 void DolphinView::zoomIn()
410 m_controller
->triggerZoomIn();
413 void DolphinView::zoomOut()
415 m_controller
->triggerZoomOut();
418 bool DolphinView::isZoomInPossible() const
420 return m_controller
->isZoomInPossible();
423 bool DolphinView::isZoomOutPossible() const
425 return m_controller
->isZoomOutPossible();
428 void DolphinView::setSorting(Sorting sorting
)
430 if (sorting
!= this->sorting()) {
431 updateSorting(sorting
);
435 DolphinView::Sorting
DolphinView::sorting() const
437 return m_proxyModel
->sorting();
440 void DolphinView::setSortOrder(Qt::SortOrder order
)
442 if (sortOrder() != order
) {
443 updateSortOrder(order
);
447 Qt::SortOrder
DolphinView::sortOrder() const
449 return m_proxyModel
->sortOrder();
452 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
454 const KUrl viewPropsUrl
= viewPropertiesUrl();
455 ViewProperties
props(viewPropsUrl
);
456 props
.setAdditionalInfo(info
);
457 m_fileItemDelegate
->setShowInformation(info
);
459 emit
additionalInfoChanged(info
);
461 if (itemView() != m_detailsView
) {
462 // the details view requires no reloading of the directory, as it maps
463 // the file item delegate info to its columns internally
464 loadDirectory(viewPropsUrl
, true);
468 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
470 return m_fileItemDelegate
->showInformation();
473 void DolphinView::reload()
476 loadDirectory(url(), true);
479 void DolphinView::refresh()
481 const bool oldActivationState
= m_active
;
485 applyViewProperties(m_controller
->url());
488 setActive(oldActivationState
);
491 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
493 if (m_controller
->url() == url
) {
497 m_controller
->setUrl(url
); // emits urlChanged, which we forward
499 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
500 applyViewProperties(rootUrl
);
501 loadDirectory(rootUrl
);
502 if (itemView() == m_columnView
) {
503 m_columnView
->setRootUrl(rootUrl
);
504 m_columnView
->showColumn(url
);
507 applyViewProperties(url
);
511 emit
startedPathLoading(url
);
514 void DolphinView::setNameFilter(const QString
& nameFilter
)
516 m_proxyModel
->setFilterRegExp(nameFilter
);
518 if (isColumnViewActive()) {
519 // adjusting the directory lister is not enough in the case of the
520 // column view, as each column has its own directory lister internally...
521 m_columnView
->setNameFilter(nameFilter
);
525 void DolphinView::calculateItemCount(int& fileCount
, int& folderCount
)
527 foreach (KFileItem item
, m_dirLister
->items()) {
536 void DolphinView::setUrl(const KUrl
& url
)
538 updateView(url
, KUrl());
541 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
543 QWidget::mouseReleaseEvent(event
);
546 void DolphinView::activate()
551 void DolphinView::triggerItem(const KFileItem
& item
)
553 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
554 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
555 // items are selected by the user, hence don't trigger the
556 // item specified by 'index'
564 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
567 void DolphinView::generatePreviews(const KFileItemList
& items
)
569 if (m_controller
->dolphinView()->showPreview()) {
570 KIO::PreviewJob
* job
= KIO::filePreview(items
, 128);
571 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
572 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
576 void DolphinView::showPreview(const KFileItem
& item
, const QPixmap
& pixmap
)
578 Q_ASSERT(!item
.isNull());
579 if (item
.url().directory() != m_dirLister
->url().path()) {
580 // the preview job is still working on items of an older URL, hence
581 // the item is not part of the directory model anymore
585 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
586 if (idx
.isValid() && (idx
.column() == 0)) {
587 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
588 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
589 KIconEffect iconEffect
;
590 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
591 m_dolphinModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
593 m_dolphinModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
598 void DolphinView::emitSelectionChangedSignal()
600 emit
selectionChanged(DolphinView::selectedItems());
603 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
605 if (!url
.isValid()) {
606 const QString
location(url
.pathOrUrl());
607 if (location
.isEmpty()) {
608 emit
errorMessage(i18nc("@info:status", "The location is empty."));
610 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
615 m_cutItemsCache
.clear();
616 m_loadingDirectory
= true;
619 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
621 if (isColumnViewActive()) {
622 // adjusting the directory lister is not enough in the case of the
623 // column view, as each column has its own directory lister internally...
625 m_columnView
->reload();
627 m_columnView
->showColumn(url
);
632 KUrl
DolphinView::viewPropertiesUrl() const
634 if (isColumnViewActive()) {
635 return m_dirLister
->url();
641 void DolphinView::applyViewProperties(const KUrl
& url
)
643 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
644 // The column view is active, hence don't apply the view properties
645 // of sub directories (represented by columns) to the view. The
646 // view always represents the properties of the first column.
650 const ViewProperties
props(url
);
652 const Mode mode
= props
.viewMode();
653 if (m_mode
!= mode
) {
658 if (itemView() == 0) {
661 Q_ASSERT(itemView() != 0);
662 Q_ASSERT(m_fileItemDelegate
!= 0);
664 const bool showHiddenFiles
= props
.showHiddenFiles();
665 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
666 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
667 emit
showHiddenFilesChanged();
670 m_storedCategorizedSorting
= props
.categorizedSorting();
671 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
672 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
673 m_proxyModel
->setCategorizedModel(categorized
);
674 emit
categorizedSortingChanged();
677 const DolphinView::Sorting sorting
= props
.sorting();
678 if (sorting
!= m_proxyModel
->sorting()) {
679 m_proxyModel
->setSorting(sorting
);
680 emit
sortingChanged(sorting
);
683 const Qt::SortOrder sortOrder
= props
.sortOrder();
684 if (sortOrder
!= m_proxyModel
->sortOrder()) {
685 m_proxyModel
->setSortOrder(sortOrder
);
686 emit
sortOrderChanged(sortOrder
);
689 KFileItemDelegate::InformationList info
= props
.additionalInfo();
690 if (info
!= m_fileItemDelegate
->showInformation()) {
691 m_fileItemDelegate
->setShowInformation(info
);
692 emit
additionalInfoChanged(info
);
695 const bool showPreview
= props
.showPreview();
696 if (showPreview
!= m_showPreview
) {
697 m_showPreview
= showPreview
;
698 emit
showPreviewChanged();
702 void DolphinView::changeSelection(const KFileItemList
& selection
)
705 if (selection
.isEmpty()) {
708 const KUrl
& baseUrl
= url();
710 QItemSelection new_selection
;
711 foreach(const KFileItem
& item
, selection
) {
712 url
= item
.url().upUrl();
713 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
714 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
715 new_selection
.select(index
, index
);
718 itemView()->selectionModel()->select(new_selection
,
719 QItemSelectionModel::ClearAndSelect
720 | QItemSelectionModel::Current
);
723 void DolphinView::openContextMenu(const QPoint
& pos
)
727 const QModelIndex index
= itemView()->indexAt(pos
);
728 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
729 item
= fileItem(index
);
732 emit
requestContextMenu(item
, url());
735 void DolphinView::dropUrls(const KUrl::List
& urls
,
736 const KUrl
& destPath
,
737 const KFileItem
& destItem
)
739 const KUrl
& destination
= !destItem
.isNull() && destItem
.isDir() ?
740 destItem
.url() : destPath
;
741 const KUrl sourceDir
= KUrl(urls
.first().directory());
742 if (sourceDir
!= destination
) {
743 dropUrls(urls
, destination
);
747 void DolphinView::dropUrls(const KUrl::List
& urls
,
748 const KUrl
& destination
)
750 emit
urlsDropped(urls
, destination
);
753 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
755 ViewProperties
props(viewPropertiesUrl());
756 props
.setSorting(sorting
);
758 m_proxyModel
->setSorting(sorting
);
760 emit
sortingChanged(sorting
);
763 void DolphinView::updateSortOrder(Qt::SortOrder order
)
765 ViewProperties
props(viewPropertiesUrl());
766 props
.setSortOrder(order
);
768 m_proxyModel
->setSortOrder(order
);
770 emit
sortOrderChanged(order
);
773 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
775 ViewProperties
props(viewPropertiesUrl());
776 props
.setAdditionalInfo(info
);
779 m_fileItemDelegate
->setShowInformation(info
);
781 emit
additionalInfoChanged(info
);
785 void DolphinView::emitContentsMoved()
787 // only emit the contents moved signal if:
788 // - no directory loading is ongoing (this would reset the contents position
790 // - if the Column View is active: the column view does an automatic
791 // positioning during the loading operation, which must be remembered
792 if (!m_loadingDirectory
|| isColumnViewActive()) {
793 const QPoint
pos(contentsPosition());
794 emit
contentsMoved(pos
.x(), pos
.y());
798 void DolphinView::updateCutItems()
800 // restore the icons of all previously selected items to the
802 QList
<CutItem
>::const_iterator it
= m_cutItemsCache
.begin();
803 QList
<CutItem
>::const_iterator end
= m_cutItemsCache
.end();
805 const QModelIndex index
= m_dolphinModel
->indexForUrl((*it
).url
);
806 if (index
.isValid()) {
807 m_dolphinModel
->setData(index
, QIcon((*it
).pixmap
), Qt::DecorationRole
);
811 m_cutItemsCache
.clear();
813 // ... and apply an item effect to all currently cut items
814 applyCutItemEffect();
817 void DolphinView::showHoverInformation(const KFileItem
& item
)
819 if (hasSelection()) {
823 emit
requestItemInfo(item
);
826 void DolphinView::clearHoverInformation()
828 emit
requestItemInfo(KFileItem());
832 void DolphinView::createView()
835 Q_ASSERT(m_iconsView
== 0);
836 Q_ASSERT(m_detailsView
== 0);
837 Q_ASSERT(m_columnView
== 0);
839 QAbstractItemView
* view
= 0;
842 m_iconsView
= new DolphinIconsView(this, m_controller
);
848 m_detailsView
= new DolphinDetailsView(this, m_controller
);
849 view
= m_detailsView
;
853 m_columnView
= new DolphinColumnView(this, m_controller
);
860 m_fileItemDelegate
= new KFileItemDelegate(view
);
861 view
->setItemDelegate(m_fileItemDelegate
);
863 view
->setModel(m_proxyModel
);
864 if (m_selectionModel
!= 0) {
865 view
->setSelectionModel(m_selectionModel
);
867 m_selectionModel
= view
->selectionModel();
870 m_selectionModel
->setParent(this); //Reparent the selection model. We do not want it to be deleted when we delete the model
872 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
874 new KMimeTypeResolver(view
, m_dolphinModel
);
875 m_topLayout
->insertWidget(1, view
);
877 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
878 this, SLOT(emitSelectionChangedSignal()));
879 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
880 this, SLOT(emitContentsMoved()));
881 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
882 this, SLOT(emitContentsMoved()));
885 void DolphinView::deleteView()
887 QAbstractItemView
* view
= itemView();
889 m_topLayout
->removeWidget(view
);
896 m_fileItemDelegate
= 0;
900 QAbstractItemView
* DolphinView::itemView() const
902 if (m_detailsView
!= 0) {
903 return m_detailsView
;
904 } else if (m_columnView
!= 0) {
911 bool DolphinView::isCutItem(const KFileItem
& item
) const
913 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
914 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
916 const KUrl
& itemUrl
= item
.url();
917 KUrl::List::const_iterator it
= cutUrls
.begin();
918 const KUrl::List::const_iterator end
= cutUrls
.end();
920 if (*it
== itemUrl
) {
929 void DolphinView::applyCutItemEffect()
931 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
932 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
936 KFileItemList
items(m_dirLister
->items());
937 KFileItemList::const_iterator it
= items
.begin();
938 const KFileItemList::const_iterator end
= items
.end();
940 const KFileItem item
= *it
;
941 if (isCutItem(item
)) {
942 const QModelIndex index
= m_dolphinModel
->indexForItem(item
);
943 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
944 if (value
.type() == QVariant::Icon
) {
945 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
946 QPixmap pixmap
= icon
.pixmap(128, 128);
948 // remember current pixmap for the item to be able
949 // to restore it when other items get cut
951 cutItem
.url
= item
.url();
952 cutItem
.pixmap
= pixmap
;
953 m_cutItemsCache
.append(cutItem
);
955 // apply icon effect to the cut item
956 KIconEffect iconEffect
;
957 pixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
958 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
965 KToggleAction
* DolphinView::iconsModeAction(KActionCollection
* actionCollection
)
967 KToggleAction
* iconsView
= actionCollection
->add
<KToggleAction
>("icons");
968 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
969 iconsView
->setShortcut(Qt::CTRL
| Qt::Key_1
);
970 iconsView
->setIcon(KIcon("fileview-icon"));
971 iconsView
->setData(QVariant::fromValue(IconsView
));
975 KToggleAction
* DolphinView::detailsModeAction(KActionCollection
* actionCollection
)
977 KToggleAction
* detailsView
= actionCollection
->add
<KToggleAction
>("details");
978 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
979 detailsView
->setShortcut(Qt::CTRL
| Qt::Key_2
);
980 detailsView
->setIcon(KIcon("fileview-detailed"));
981 detailsView
->setData(QVariant::fromValue(DetailsView
));
985 KToggleAction
* DolphinView::columnsModeAction(KActionCollection
* actionCollection
)
987 KToggleAction
* columnView
= actionCollection
->add
<KToggleAction
>("columns");
988 columnView
->setText(i18nc("@action:inmenu View Mode", "Columns"));
989 columnView
->setShortcut(Qt::CTRL
| Qt::Key_3
);
990 columnView
->setIcon(KIcon("fileview-column"));
991 columnView
->setData(QVariant::fromValue(ColumnView
));
995 QString
DolphinView::currentViewModeActionName() const
998 case DolphinView::IconsView
:
1000 case DolphinView::DetailsView
:
1002 case DolphinView::ColumnView
:
1005 return QString(); // can't happen
1008 void DolphinView::renameSelectedItems()
1010 const KFileItemList items
= selectedItems();
1011 if (items
.count() > 1) {
1012 // More than one item has been selected for renaming. Open
1013 // a rename dialog and rename all items afterwards.
1014 RenameDialog
dialog(this, items
);
1015 if (dialog
.exec() == QDialog::Rejected
) {
1019 const QString newName
= dialog
.newName();
1020 if (newName
.isEmpty()) {
1021 emit
errorMessage(dialog
.errorString());
1023 // TODO: check how this can be integrated into KonqFileUndoManager/KonqOperations
1024 // as one operation instead of n rename operations like it is done now...
1025 Q_ASSERT(newName
.contains('#'));
1027 // iterate through all selected items and rename them...
1028 const int replaceIndex
= newName
.indexOf('#');
1029 Q_ASSERT(replaceIndex
>= 0);
1032 KFileItemList::const_iterator it
= items
.begin();
1033 const KFileItemList::const_iterator end
= items
.end();
1035 const KUrl
& oldUrl
= (*it
).url();
1037 number
.setNum(index
++);
1039 QString
name(newName
);
1040 name
.replace(replaceIndex
, 1, number
);
1042 if (oldUrl
.fileName() != name
) {
1043 KUrl newUrl
= oldUrl
;
1044 newUrl
.setFileName(name
);
1045 KonqOperations::rename(this, oldUrl
, newUrl
);
1046 emit
doingOperation(KonqFileUndoManager::RENAME
);
1052 // Only one item has been selected for renaming. Use the custom
1053 // renaming mechanism from the views.
1054 Q_ASSERT(items
.count() == 1);
1056 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
1057 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
1058 // is a benefit for the user at all -> let's wait for some input first...
1059 RenameDialog
dialog(this, items
);
1060 if (dialog
.exec() == QDialog::Rejected
) {
1064 const QString
& newName
= dialog
.newName();
1065 if (newName
.isEmpty()) {
1066 emit
errorMessage(dialog
.errorString());
1068 const KUrl
& oldUrl
= items
.first().url();
1069 KUrl newUrl
= oldUrl
;
1070 newUrl
.setFileName(newName
);
1071 KonqOperations::rename(this, oldUrl
, newUrl
);
1072 emit
doingOperation(KonqFileUndoManager::RENAME
);
1077 void DolphinView::trashSelectedItems()
1079 emit
doingOperation(KonqFileUndoManager::TRASH
);
1080 KonqOperations::del(this, KonqOperations::TRASH
, selectedUrls());
1083 void DolphinView::deleteSelectedItems()
1085 const KUrl::List list
= selectedUrls();
1086 const bool del
= KonqOperations::askDeleteConfirmation(list
,
1087 KonqOperations::DEL
,
1088 KonqOperations::DEFAULT_CONFIRMATION
,
1092 KIO::Job
* job
= KIO::del(list
);
1093 connect(job
, SIGNAL(result(KJob
*)),
1094 this, SLOT(slotDeleteFileFinished(KJob
*)));
1098 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1100 if (job
->error() == 0) {
1101 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1103 emit
errorMessage(job
->errorString());
1107 void DolphinView::cutSelectedItems()
1109 QMimeData
* mimeData
= new QMimeData();
1110 const KUrl::List kdeUrls
= selectedUrls();
1111 const KUrl::List mostLocalUrls
;
1112 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, true);
1113 QApplication::clipboard()->setMimeData(mimeData
);
1116 void DolphinView::copySelectedItems()
1118 QMimeData
* mimeData
= new QMimeData();
1119 const KUrl::List kdeUrls
= selectedUrls();
1120 const KUrl::List mostLocalUrls
;
1121 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, false);
1122 QApplication::clipboard()->setMimeData(mimeData
);
1125 void DolphinView::paste()
1127 QClipboard
* clipboard
= QApplication::clipboard();
1128 const QMimeData
* mimeData
= clipboard
->mimeData();
1130 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1132 // per default the pasting is done into the current Url of the view
1133 KUrl
destUrl(url());
1135 // check whether the pasting should be done into a selected directory
1136 const KUrl::List selectedUrls
= this->selectedUrls();
1137 if (selectedUrls
.count() == 1) {
1138 const KFileItem
fileItem(S_IFDIR
,
1140 selectedUrls
.first(),
1142 if (fileItem
.isDir()) {
1143 // only one item is selected which is a directory, hence paste
1144 // into this directory
1145 destUrl
= selectedUrls
.first();
1149 if (KonqMimeData::decodeIsCutSelection(mimeData
)) {
1150 KonqOperations::copy(this, KonqOperations::MOVE
, sourceUrls
, destUrl
);
1151 emit
doingOperation(KonqFileUndoManager::MOVE
);
1154 KonqOperations::copy(this, KonqOperations::COPY
, sourceUrls
, destUrl
);
1155 emit
doingOperation(KonqFileUndoManager::COPY
);
1159 QPair
<bool, QString
> DolphinView::pasteInfo() const
1161 QPair
<bool, QString
> ret
;
1162 QClipboard
* clipboard
= QApplication::clipboard();
1163 const QMimeData
* mimeData
= clipboard
->mimeData();
1165 KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1166 if (!urls
.isEmpty()) {
1168 ret
.second
= i18ncp("@action:inmenu", "Paste One File", "Paste %1 Files", urls
.count());
1171 ret
.second
= i18nc("@action:inmenu", "Paste");
1175 const KUrl::List urls
= selectedUrls();
1176 const uint count
= urls
.count();
1178 // pasting should not be allowed when more than one file
1181 } else if (count
== 1) {
1182 // Only one file is selected. Pasting is only allowed if this
1183 // file is a directory.
1184 // TODO: this doesn't work with remote protocols; instead we need a
1185 // m_activeViewContainer->selectedFileItems() to get the real KFileItems
1186 const KFileItem
fileItem(S_IFDIR
,
1190 ret
.first
= fileItem
.isDir();
1196 KAction
* DolphinView::createRenameAction(KActionCollection
* collection
)
1198 KAction
* rename
= collection
->addAction("rename");
1199 rename
->setText(i18nc("@action:inmenu File", "Rename..."));
1200 rename
->setShortcut(Qt::Key_F2
);
1204 KAction
* DolphinView::createMoveToTrashAction(KActionCollection
* collection
)
1206 KAction
* moveToTrash
= collection
->addAction("move_to_trash");
1207 moveToTrash
->setText(i18nc("@action:inmenu File", "Move to Trash"));
1208 moveToTrash
->setIcon(KIcon("user-trash"));
1209 moveToTrash
->setShortcut(QKeySequence::Delete
);
1213 KAction
* DolphinView::createDeleteAction(KActionCollection
* collection
)
1215 KAction
* deleteAction
= collection
->addAction("delete");
1216 deleteAction
->setIcon(KIcon("edit-delete"));
1217 deleteAction
->setText(i18nc("@action:inmenu File", "Delete"));
1218 deleteAction
->setShortcut(Qt::SHIFT
| Qt::Key_Delete
);
1219 return deleteAction
;
1222 #include "dolphinview.moc"