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),
75 m_dolphinModel(dolphinModel
),
76 m_dirLister(dirLister
),
77 m_proxyModel(proxyModel
)
79 setFocusPolicy(Qt::StrongFocus
);
80 m_topLayout
= new QVBoxLayout(this);
81 m_topLayout
->setSpacing(0);
82 m_topLayout
->setMargin(0);
84 QClipboard
* clipboard
= QApplication::clipboard();
85 connect(clipboard
, SIGNAL(dataChanged()),
86 this, SLOT(updateCutItems()));
88 connect(m_dirLister
, SIGNAL(completed()),
89 this, SLOT(updateCutItems()));
90 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
91 this, SLOT(generatePreviews(const KFileItemList
&)));
93 m_controller
= new DolphinController(this);
94 m_controller
->setUrl(url
);
96 // Receiver of the DolphinView signal 'urlChanged()' don't need
97 // to care whether the internal controller changed the URL already or whether
98 // the controller just requested an URL change and will be updated later.
99 // In both cases the URL has been changed:
100 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
101 this, SIGNAL(urlChanged(const KUrl
&)));
102 connect(m_controller
, SIGNAL(requestUrlChange(const KUrl
&)),
103 this, SIGNAL(urlChanged(const KUrl
&)));
105 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&)),
106 this, SLOT(openContextMenu(const QPoint
&)));
107 connect(m_controller
, SIGNAL(urlsDropped(const KUrl::List
&, const KUrl
&, const KFileItem
&)),
108 this, SLOT(dropUrls(const KUrl::List
&, const KUrl
&, const KFileItem
&)));
109 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
110 this, SLOT(updateSorting(DolphinView::Sorting
)));
111 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
112 this, SLOT(updateSortOrder(Qt::SortOrder
)));
113 connect(m_controller
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
114 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList
&)));
115 connect(m_controller
, SIGNAL(itemTriggered(const KFileItem
&)),
116 this, SLOT(triggerItem(const KFileItem
&)));
117 connect(m_controller
, SIGNAL(activated()),
118 this, SLOT(activate()));
119 connect(m_controller
, SIGNAL(itemEntered(const KFileItem
&)),
120 this, SLOT(showHoverInformation(const KFileItem
&)));
121 connect(m_controller
, SIGNAL(viewportEntered()),
122 this, SLOT(clearHoverInformation()));
124 applyViewProperties(url
);
125 m_topLayout
->addWidget(itemView());
128 DolphinView::~DolphinView()
132 const KUrl
& DolphinView::url() const
134 return m_controller
->url();
137 KUrl
DolphinView::rootUrl() const
139 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
142 void DolphinView::setActive(bool active
)
144 if (active
== m_active
) {
150 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
152 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
153 // bypasses the problem when having a split view and changing the active view to
154 // update the some URL dependent states. A nicer approach should be no big deal...
155 emit
urlChanged(url());
156 emit
selectionChanged(selectedItems());
161 QWidget
* viewport
= itemView()->viewport();
163 palette
.setColor(viewport
->backgroundRole(), color
);
164 viewport
->setPalette(palette
);
172 m_controller
->indicateActivationChange(active
);
175 bool DolphinView::isActive() const
180 void DolphinView::setMode(Mode mode
)
182 if (mode
== m_mode
) {
183 return; // the wished mode is already set
188 if (isColumnViewActive()) {
189 // When changing the mode in the column view, it makes sense
190 // to go back to the root URL of the column view automatically.
191 // Otherwise there it would not be possible to turn off the column view
192 // without focusing the first column.
193 const KUrl root
= rootUrl();
195 m_controller
->setUrl(root
);
200 // It is important to read the view properties _after_ deleting the view,
201 // as e. g. the detail view might adjust the additional information properties
202 // after getting closed:
203 const KUrl viewPropsUrl
= viewPropertiesUrl();
204 ViewProperties
props(viewPropsUrl
);
205 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();
225 loadDirectory(viewPropsUrl
);
230 DolphinView::Mode
DolphinView::mode() const
235 void DolphinView::setShowPreview(bool show
)
237 if (m_showPreview
== show
) {
241 const KUrl viewPropsUrl
= viewPropertiesUrl();
242 ViewProperties
props(viewPropsUrl
);
243 props
.setShowPreview(show
);
245 m_showPreview
= show
;
247 emit
showPreviewChanged();
249 loadDirectory(viewPropsUrl
, true);
252 bool DolphinView::showPreview() const
254 return m_showPreview
;
257 void DolphinView::setShowHiddenFiles(bool show
)
259 if (m_dirLister
->showingDotFiles() == show
) {
263 const KUrl viewPropsUrl
= viewPropertiesUrl();
264 ViewProperties
props(viewPropsUrl
);
265 props
.setShowHiddenFiles(show
);
267 m_dirLister
->setShowingDotFiles(show
);
268 emit
showHiddenFilesChanged();
270 loadDirectory(viewPropsUrl
, true);
273 bool DolphinView::showHiddenFiles() const
275 return m_dirLister
->showingDotFiles();
278 void DolphinView::setCategorizedSorting(bool categorized
)
280 if (categorized
== categorizedSorting()) {
284 // setCategorizedSorting(true) may only get invoked
285 // if the view supports categorized sorting
286 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
288 ViewProperties
props(viewPropertiesUrl());
289 props
.setCategorizedSorting(categorized
);
292 m_storedCategorizedSorting
= categorized
;
293 m_proxyModel
->setCategorizedModel(categorized
);
295 emit
categorizedSortingChanged();
298 bool DolphinView::categorizedSorting() const
300 // If all view modes would support categorized sorting, returning
301 // m_proxyModel->isCategorizedModel() would be the way to go. As
302 // currently only the icons view supports caterized sorting, we remember
303 // the stored view properties state in m_storedCategorizedSorting and
304 // return this state. The application takes care to disable the corresponding
305 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
306 // that this setting is not applied to the current view mode.
307 return m_storedCategorizedSorting
;
310 bool DolphinView::supportsCategorizedSorting() const
312 return m_iconsView
!= 0;
315 void DolphinView::selectAll()
317 itemView()->selectAll();
320 void DolphinView::invertSelection()
322 if (isColumnViewActive()) {
323 // QAbstractItemView does not offer a virtual method invertSelection()
324 // as counterpart to QAbstractItemView::selectAll(). This makes it
325 // necessary to delegate the inverting of the selection to the
326 // column view, as only the selection of the active column should
328 m_columnView
->invertSelection();
330 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
331 const QAbstractItemModel
* itemModel
= selectionModel
->model();
333 const QModelIndex topLeft
= itemModel
->index(0, 0);
334 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
335 itemModel
->columnCount() - 1);
337 const QItemSelection
selection(topLeft
, bottomRight
);
338 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
342 bool DolphinView::hasSelection() const
344 return itemView()->selectionModel()->hasSelection();
347 void DolphinView::clearSelection()
349 itemView()->selectionModel()->clear();
352 KFileItemList
DolphinView::selectedItems() const
354 const QAbstractItemView
* view
= itemView();
356 // Our view has a selection, we will map them back to the DolphinModel
357 // and then fill the KFileItemList.
358 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
360 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
361 KFileItemList itemList
;
363 const QModelIndexList indexList
= selection
.indexes();
364 foreach (QModelIndex index
, indexList
) {
365 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
366 if (!item
.isNull()) {
367 itemList
.append(item
);
374 KUrl::List
DolphinView::selectedUrls() const
377 const KFileItemList list
= selectedItems();
378 foreach (KFileItem item
, list
) {
379 urls
.append(item
.url());
384 KFileItem
DolphinView::fileItem(const QModelIndex
& index
) const
386 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
387 return m_dolphinModel
->itemForIndex(dolphinModelIndex
);
390 void DolphinView::setContentsPosition(int x
, int y
)
392 QAbstractItemView
* view
= itemView();
394 // the ColumnView takes care itself for the horizontal scrolling
395 if (!isColumnViewActive()) {
396 view
->horizontalScrollBar()->setValue(x
);
398 view
->verticalScrollBar()->setValue(y
);
400 m_loadingDirectory
= false;
403 QPoint
DolphinView::contentsPosition() const
405 const int x
= itemView()->horizontalScrollBar()->value();
406 const int y
= itemView()->verticalScrollBar()->value();
410 void DolphinView::zoomIn()
412 m_controller
->triggerZoomIn();
415 void DolphinView::zoomOut()
417 m_controller
->triggerZoomOut();
420 bool DolphinView::isZoomInPossible() const
422 return m_controller
->isZoomInPossible();
425 bool DolphinView::isZoomOutPossible() const
427 return m_controller
->isZoomOutPossible();
430 void DolphinView::setSorting(Sorting sorting
)
432 if (sorting
!= this->sorting()) {
433 updateSorting(sorting
);
437 DolphinView::Sorting
DolphinView::sorting() const
439 return m_proxyModel
->sorting();
442 void DolphinView::setSortOrder(Qt::SortOrder order
)
444 if (sortOrder() != order
) {
445 updateSortOrder(order
);
449 Qt::SortOrder
DolphinView::sortOrder() const
451 return m_proxyModel
->sortOrder();
454 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
456 const KUrl viewPropsUrl
= viewPropertiesUrl();
457 ViewProperties
props(viewPropsUrl
);
458 props
.setAdditionalInfo(info
);
459 m_fileItemDelegate
->setShowInformation(info
);
461 emit
additionalInfoChanged(info
);
463 if (itemView() != m_detailsView
) {
464 // the details view requires no reloading of the directory, as it maps
465 // the file item delegate info to its columns internally
466 loadDirectory(viewPropsUrl
, true);
470 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
472 return m_fileItemDelegate
->showInformation();
475 void DolphinView::reload()
478 loadDirectory(url(), true);
481 void DolphinView::refresh()
483 const bool oldActivationState
= m_active
;
487 applyViewProperties(m_controller
->url());
490 setActive(oldActivationState
);
493 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
495 if (m_controller
->url() == url
) {
499 m_controller
->setUrl(url
); // emits urlChanged, which we forward
501 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
502 applyViewProperties(rootUrl
);
503 loadDirectory(rootUrl
);
504 if (itemView() == m_columnView
) {
505 m_columnView
->setRootUrl(rootUrl
);
506 m_columnView
->showColumn(url
);
509 applyViewProperties(url
);
513 emit
startedPathLoading(url
);
516 void DolphinView::setNameFilter(const QString
& nameFilter
)
518 m_proxyModel
->setFilterRegExp(nameFilter
);
520 if (isColumnViewActive()) {
521 // adjusting the directory lister is not enough in the case of the
522 // column view, as each column has its own directory lister internally...
523 m_columnView
->setNameFilter(nameFilter
);
527 void DolphinView::calculateItemCount(int& fileCount
, int& folderCount
)
529 foreach (KFileItem item
, m_dirLister
->items()) {
538 void DolphinView::setUrl(const KUrl
& url
)
540 updateView(url
, KUrl());
543 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
545 QWidget::mouseReleaseEvent(event
);
548 void DolphinView::activate()
553 void DolphinView::triggerItem(const KFileItem
& item
)
555 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
556 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
557 // items are selected by the user, hence don't trigger the
558 // item specified by 'index'
566 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
569 void DolphinView::generatePreviews(const KFileItemList
& items
)
571 if (m_controller
->dolphinView()->showPreview()) {
572 KIO::PreviewJob
* job
= KIO::filePreview(items
, 128);
573 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
574 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
578 void DolphinView::showPreview(const KFileItem
& item
, const QPixmap
& pixmap
)
580 Q_ASSERT(!item
.isNull());
581 if (item
.url().directory() != m_dirLister
->url().path()) {
582 // the preview job is still working on items of an older URL, hence
583 // the item is not part of the directory model anymore
587 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
588 if (idx
.isValid() && (idx
.column() == 0)) {
589 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
590 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
591 KIconEffect iconEffect
;
592 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
593 m_dolphinModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
595 m_dolphinModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
600 void DolphinView::emitSelectionChangedSignal()
602 emit
selectionChanged(DolphinView::selectedItems());
605 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
607 if (!url
.isValid()) {
608 const QString
location(url
.pathOrUrl());
609 if (location
.isEmpty()) {
610 emit
errorMessage(i18nc("@info:status", "The location is empty."));
612 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
617 m_cutItemsCache
.clear();
618 m_loadingDirectory
= true;
621 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
623 if (isColumnViewActive()) {
624 // adjusting the directory lister is not enough in the case of the
625 // column view, as each column has its own directory lister internally...
627 m_columnView
->reload();
629 m_columnView
->showColumn(url
);
634 KUrl
DolphinView::viewPropertiesUrl() const
636 if (isColumnViewActive()) {
637 return m_dirLister
->url();
643 void DolphinView::applyViewProperties(const KUrl
& url
)
645 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
646 // The column view is active, hence don't apply the view properties
647 // of sub directories (represented by columns) to the view. The
648 // view always represents the properties of the first column.
652 const ViewProperties
props(url
);
654 const Mode mode
= props
.viewMode();
655 if (m_mode
!= mode
) {
660 if (itemView() == 0) {
663 Q_ASSERT(itemView() != 0);
664 Q_ASSERT(m_fileItemDelegate
!= 0);
666 const bool showHiddenFiles
= props
.showHiddenFiles();
667 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
668 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
669 emit
showHiddenFilesChanged();
672 m_storedCategorizedSorting
= props
.categorizedSorting();
673 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
674 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
675 m_proxyModel
->setCategorizedModel(categorized
);
676 emit
categorizedSortingChanged();
679 const DolphinView::Sorting sorting
= props
.sorting();
680 if (sorting
!= m_proxyModel
->sorting()) {
681 m_proxyModel
->setSorting(sorting
);
682 emit
sortingChanged(sorting
);
685 const Qt::SortOrder sortOrder
= props
.sortOrder();
686 if (sortOrder
!= m_proxyModel
->sortOrder()) {
687 m_proxyModel
->setSortOrder(sortOrder
);
688 emit
sortOrderChanged(sortOrder
);
691 KFileItemDelegate::InformationList info
= props
.additionalInfo();
692 if (info
!= m_fileItemDelegate
->showInformation()) {
693 m_fileItemDelegate
->setShowInformation(info
);
694 emit
additionalInfoChanged(info
);
697 const bool showPreview
= props
.showPreview();
698 if (showPreview
!= m_showPreview
) {
699 m_showPreview
= showPreview
;
700 emit
showPreviewChanged();
704 void DolphinView::changeSelection(const KFileItemList
& selection
)
707 if (selection
.isEmpty()) {
710 const KUrl
& baseUrl
= url();
712 QItemSelection new_selection
;
713 foreach(const KFileItem
& item
, selection
) {
714 url
= item
.url().upUrl();
715 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
716 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
717 new_selection
.select(index
, index
);
720 itemView()->selectionModel()->select(new_selection
,
721 QItemSelectionModel::ClearAndSelect
722 | QItemSelectionModel::Current
);
725 void DolphinView::openContextMenu(const QPoint
& pos
)
729 const QModelIndex index
= itemView()->indexAt(pos
);
730 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
731 item
= fileItem(index
);
734 emit
requestContextMenu(item
, url());
737 void DolphinView::dropUrls(const KUrl::List
& urls
,
738 const KUrl
& destPath
,
739 const KFileItem
& destItem
)
741 const KUrl
& destination
= !destItem
.isNull() && destItem
.isDir() ?
742 destItem
.url() : destPath
;
743 const KUrl sourceDir
= KUrl(urls
.first().directory());
744 if (sourceDir
!= destination
) {
745 dropUrls(urls
, destination
);
749 void DolphinView::dropUrls(const KUrl::List
& urls
,
750 const KUrl
& destination
)
752 emit
urlsDropped(urls
, destination
);
755 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
757 ViewProperties
props(viewPropertiesUrl());
758 props
.setSorting(sorting
);
760 m_proxyModel
->setSorting(sorting
);
762 emit
sortingChanged(sorting
);
765 void DolphinView::updateSortOrder(Qt::SortOrder order
)
767 ViewProperties
props(viewPropertiesUrl());
768 props
.setSortOrder(order
);
770 m_proxyModel
->setSortOrder(order
);
772 emit
sortOrderChanged(order
);
775 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
777 ViewProperties
props(viewPropertiesUrl());
778 props
.setAdditionalInfo(info
);
781 m_fileItemDelegate
->setShowInformation(info
);
783 emit
additionalInfoChanged(info
);
787 void DolphinView::emitContentsMoved()
789 // only emit the contents moved signal if:
790 // - no directory loading is ongoing (this would reset the contents position
792 // - if the Column View is active: the column view does an automatic
793 // positioning during the loading operation, which must be remembered
794 if (!m_loadingDirectory
|| isColumnViewActive()) {
795 const QPoint
pos(contentsPosition());
796 emit
contentsMoved(pos
.x(), pos
.y());
800 void DolphinView::updateCutItems()
802 // restore the icons of all previously selected items to the
804 QList
<CutItem
>::const_iterator it
= m_cutItemsCache
.begin();
805 QList
<CutItem
>::const_iterator end
= m_cutItemsCache
.end();
807 const QModelIndex index
= m_dolphinModel
->indexForUrl((*it
).url
);
808 if (index
.isValid()) {
809 m_dolphinModel
->setData(index
, QIcon((*it
).pixmap
), Qt::DecorationRole
);
813 m_cutItemsCache
.clear();
815 // ... and apply an item effect to all currently cut items
816 applyCutItemEffect();
819 void DolphinView::showHoverInformation(const KFileItem
& item
)
821 if (hasSelection()) {
825 emit
requestItemInfo(item
);
828 void DolphinView::clearHoverInformation()
830 emit
requestItemInfo(KFileItem());
834 void DolphinView::createView()
837 Q_ASSERT(m_iconsView
== 0);
838 Q_ASSERT(m_detailsView
== 0);
839 Q_ASSERT(m_columnView
== 0);
841 QAbstractItemView
* view
= 0;
844 m_iconsView
= new DolphinIconsView(this, m_controller
);
850 m_detailsView
= new DolphinDetailsView(this, m_controller
);
851 view
= m_detailsView
;
855 m_columnView
= new DolphinColumnView(this, m_controller
);
862 m_fileItemDelegate
= new KFileItemDelegate(view
);
863 view
->setItemDelegate(m_fileItemDelegate
);
865 view
->setModel(m_proxyModel
);
866 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
868 new KMimeTypeResolver(view
, m_dolphinModel
);
869 m_topLayout
->insertWidget(1, view
);
871 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
872 this, SLOT(emitSelectionChangedSignal()));
873 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
874 this, SLOT(emitContentsMoved()));
875 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
876 this, SLOT(emitContentsMoved()));
879 void DolphinView::deleteView()
881 QAbstractItemView
* view
= itemView();
883 m_topLayout
->removeWidget(view
);
890 m_fileItemDelegate
= 0;
894 QAbstractItemView
* DolphinView::itemView() const
896 if (m_detailsView
!= 0) {
897 return m_detailsView
;
898 } else if (m_columnView
!= 0) {
905 bool DolphinView::isCutItem(const KFileItem
& item
) const
907 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
908 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
910 const KUrl
& itemUrl
= item
.url();
911 KUrl::List::const_iterator it
= cutUrls
.begin();
912 const KUrl::List::const_iterator end
= cutUrls
.end();
914 if (*it
== itemUrl
) {
923 void DolphinView::applyCutItemEffect()
925 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
926 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
930 KFileItemList
items(m_dirLister
->items());
931 KFileItemList::const_iterator it
= items
.begin();
932 const KFileItemList::const_iterator end
= items
.end();
934 const KFileItem item
= *it
;
935 if (isCutItem(item
)) {
936 const QModelIndex index
= m_dolphinModel
->indexForItem(item
);
937 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
938 if (value
.type() == QVariant::Icon
) {
939 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
940 QPixmap pixmap
= icon
.pixmap(128, 128);
942 // remember current pixmap for the item to be able
943 // to restore it when other items get cut
945 cutItem
.url
= item
.url();
946 cutItem
.pixmap
= pixmap
;
947 m_cutItemsCache
.append(cutItem
);
949 // apply icon effect to the cut item
950 KIconEffect iconEffect
;
951 pixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
952 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
959 KToggleAction
* DolphinView::iconsModeAction(KActionCollection
* actionCollection
)
961 KToggleAction
* iconsView
= actionCollection
->add
<KToggleAction
>("icons");
962 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
963 iconsView
->setShortcut(Qt::CTRL
| Qt::Key_1
);
964 iconsView
->setIcon(KIcon("fileview-icon"));
965 iconsView
->setData(QVariant::fromValue(IconsView
));
969 KToggleAction
* DolphinView::detailsModeAction(KActionCollection
* actionCollection
)
971 KToggleAction
* detailsView
= actionCollection
->add
<KToggleAction
>("details");
972 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
973 detailsView
->setShortcut(Qt::CTRL
| Qt::Key_2
);
974 detailsView
->setIcon(KIcon("fileview-detailed"));
975 detailsView
->setData(QVariant::fromValue(DetailsView
));
979 KToggleAction
* DolphinView::columnsModeAction(KActionCollection
* actionCollection
)
981 KToggleAction
* columnView
= actionCollection
->add
<KToggleAction
>("columns");
982 columnView
->setText(i18nc("@action:inmenu View Mode", "Columns"));
983 columnView
->setShortcut(Qt::CTRL
| Qt::Key_3
);
984 columnView
->setIcon(KIcon("fileview-column"));
985 columnView
->setData(QVariant::fromValue(ColumnView
));
989 QString
DolphinView::currentViewModeActionName() const
992 case DolphinView::IconsView
:
994 case DolphinView::DetailsView
:
996 case DolphinView::ColumnView
:
999 return QString(); // can't happen
1002 void DolphinView::renameSelectedItems()
1004 const KFileItemList items
= selectedItems();
1005 if (items
.count() > 1) {
1006 // More than one item has been selected for renaming. Open
1007 // a rename dialog and rename all items afterwards.
1008 RenameDialog
dialog(this, items
);
1009 if (dialog
.exec() == QDialog::Rejected
) {
1013 const QString newName
= dialog
.newName();
1014 if (newName
.isEmpty()) {
1015 emit
errorMessage(dialog
.errorString());
1017 // TODO: check how this can be integrated into KonqFileUndoManager/KonqOperations
1018 // as one operation instead of n rename operations like it is done now...
1019 Q_ASSERT(newName
.contains('#'));
1021 // iterate through all selected items and rename them...
1022 const int replaceIndex
= newName
.indexOf('#');
1023 Q_ASSERT(replaceIndex
>= 0);
1026 KFileItemList::const_iterator it
= items
.begin();
1027 const KFileItemList::const_iterator end
= items
.end();
1029 const KUrl
& oldUrl
= (*it
).url();
1031 number
.setNum(index
++);
1033 QString
name(newName
);
1034 name
.replace(replaceIndex
, 1, number
);
1036 if (oldUrl
.fileName() != name
) {
1037 KUrl newUrl
= oldUrl
;
1038 newUrl
.setFileName(name
);
1039 KonqOperations::rename(this, oldUrl
, newUrl
);
1040 emit
doingOperation(KonqFileUndoManager::RENAME
);
1046 // Only one item has been selected for renaming. Use the custom
1047 // renaming mechanism from the views.
1048 Q_ASSERT(items
.count() == 1);
1050 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
1051 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
1052 // is a benefit for the user at all -> let's wait for some input first...
1053 RenameDialog
dialog(this, items
);
1054 if (dialog
.exec() == QDialog::Rejected
) {
1058 const QString
& newName
= dialog
.newName();
1059 if (newName
.isEmpty()) {
1060 emit
errorMessage(dialog
.errorString());
1062 const KUrl
& oldUrl
= items
.first().url();
1063 KUrl newUrl
= oldUrl
;
1064 newUrl
.setFileName(newName
);
1065 KonqOperations::rename(this, oldUrl
, newUrl
);
1066 emit
doingOperation(KonqFileUndoManager::RENAME
);
1071 void DolphinView::trashSelectedItems()
1073 emit
doingOperation(KonqFileUndoManager::TRASH
);
1074 KonqOperations::del(this, KonqOperations::TRASH
, selectedUrls());
1077 void DolphinView::deleteSelectedItems()
1079 const KUrl::List list
= selectedUrls();
1080 const bool del
= KonqOperations::askDeleteConfirmation(list
,
1081 KonqOperations::DEL
,
1082 KonqOperations::DEFAULT_CONFIRMATION
,
1086 KIO::Job
* job
= KIO::del(list
);
1087 connect(job
, SIGNAL(result(KJob
*)),
1088 this, SLOT(slotDeleteFileFinished(KJob
*)));
1092 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1094 if (job
->error() == 0) {
1095 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1097 emit
errorMessage(job
->errorString());
1101 void DolphinView::cutSelectedItems()
1103 QMimeData
* mimeData
= new QMimeData();
1104 const KUrl::List kdeUrls
= selectedUrls();
1105 const KUrl::List mostLocalUrls
;
1106 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, true);
1107 QApplication::clipboard()->setMimeData(mimeData
);
1110 void DolphinView::copySelectedItems()
1112 QMimeData
* mimeData
= new QMimeData();
1113 const KUrl::List kdeUrls
= selectedUrls();
1114 const KUrl::List mostLocalUrls
;
1115 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, false);
1116 QApplication::clipboard()->setMimeData(mimeData
);
1119 void DolphinView::paste()
1121 QClipboard
* clipboard
= QApplication::clipboard();
1122 const QMimeData
* mimeData
= clipboard
->mimeData();
1124 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1126 // per default the pasting is done into the current Url of the view
1127 KUrl
destUrl(url());
1129 // check whether the pasting should be done into a selected directory
1130 const KUrl::List selectedUrls
= this->selectedUrls();
1131 if (selectedUrls
.count() == 1) {
1132 const KFileItem
fileItem(S_IFDIR
,
1134 selectedUrls
.first(),
1136 if (fileItem
.isDir()) {
1137 // only one item is selected which is a directory, hence paste
1138 // into this directory
1139 destUrl
= selectedUrls
.first();
1143 if (KonqMimeData::decodeIsCutSelection(mimeData
)) {
1144 KonqOperations::copy(this, KonqOperations::MOVE
, sourceUrls
, destUrl
);
1145 emit
doingOperation(KonqFileUndoManager::MOVE
);
1148 KonqOperations::copy(this, KonqOperations::COPY
, sourceUrls
, destUrl
);
1149 emit
doingOperation(KonqFileUndoManager::COPY
);
1153 QPair
<bool, QString
> DolphinView::pasteInfo() const
1155 QPair
<bool, QString
> ret
;
1156 QClipboard
* clipboard
= QApplication::clipboard();
1157 const QMimeData
* mimeData
= clipboard
->mimeData();
1159 KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1160 if (!urls
.isEmpty()) {
1162 ret
.second
= i18ncp("@action:inmenu", "Paste One File", "Paste %1 Files", urls
.count());
1165 ret
.second
= i18nc("@action:inmenu", "Paste");
1169 const KUrl::List urls
= selectedUrls();
1170 const uint count
= urls
.count();
1172 // pasting should not be allowed when more than one file
1175 } else if (count
== 1) {
1176 // Only one file is selected. Pasting is only allowed if this
1177 // file is a directory.
1178 // TODO: this doesn't work with remote protocols; instead we need a
1179 // m_activeViewContainer->selectedFileItems() to get the real KFileItems
1180 const KFileItem
fileItem(S_IFDIR
,
1184 ret
.first
= fileItem
.isDir();
1190 KAction
* DolphinView::createRenameAction(KActionCollection
* collection
)
1192 KAction
* rename
= collection
->addAction("rename");
1193 rename
->setText(i18nc("@action:inmenu File", "Rename..."));
1194 rename
->setShortcut(Qt::Key_F2
);
1198 KAction
* DolphinView::createMoveToTrashAction(KActionCollection
* collection
)
1200 KAction
* moveToTrash
= collection
->addAction("move_to_trash");
1201 moveToTrash
->setText(i18nc("@action:inmenu File", "Move to Trash"));
1202 moveToTrash
->setIcon(KIcon("user-trash"));
1203 moveToTrash
->setShortcut(QKeySequence::Delete
);
1207 KAction
* DolphinView::createDeleteAction(KActionCollection
* collection
)
1209 KAction
* deleteAction
= collection
->addAction("delete");
1210 deleteAction
->setIcon(KIcon("edit-delete"));
1211 deleteAction
->setText(i18nc("@action:inmenu File", "Delete"));
1212 deleteAction
->setShortcut(Qt::SHIFT
| Qt::Key_Delete
);
1213 return deleteAction
;
1216 #include "dolphinview.moc"