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>
33 #include <kcolorscheme.h>
34 #include <kdirlister.h>
35 #include <kfileitemdelegate.h>
37 #include <kiconeffect.h>
38 #include <kio/deletejob.h>
39 #include <kio/netaccess.h>
40 #include <kio/previewjob.h>
43 #include <kmimetyperesolver.h>
44 #include <konqmimedata.h>
45 #include <konq_operations.h>
48 #include "dolphindropcontroller.h"
49 #include "dolphinmodel.h"
50 #include "dolphincolumnview.h"
51 #include "dolphincontroller.h"
52 #include "dolphinsortfilterproxymodel.h"
53 #include "dolphindetailsview.h"
54 #include "dolphiniconsview.h"
55 #include "renamedialog.h"
56 #include "viewproperties.h"
57 #include "dolphinsettings.h"
58 #include "dolphin_generalsettings.h"
60 DolphinView::DolphinView(QWidget
* parent
,
62 KDirLister
* dirLister
,
63 DolphinModel
* dolphinModel
,
64 DolphinSortFilterProxyModel
* proxyModel
) :
68 m_loadingDirectory(false),
69 m_storedCategorizedSorting(false),
70 m_mode(DolphinView::IconsView
),
76 m_fileItemDelegate(0),
78 m_dolphinModel(dolphinModel
),
79 m_dirLister(dirLister
),
80 m_proxyModel(proxyModel
),
83 setFocusPolicy(Qt::StrongFocus
);
84 m_topLayout
= new QVBoxLayout(this);
85 m_topLayout
->setSpacing(0);
86 m_topLayout
->setMargin(0);
88 QClipboard
* clipboard
= QApplication::clipboard();
89 connect(clipboard
, SIGNAL(dataChanged()),
90 this, SLOT(updateCutItems()));
92 connect(m_dirLister
, SIGNAL(completed()),
93 this, SLOT(updateCutItems()));
94 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
95 this, SLOT(generatePreviews(const KFileItemList
&)));
97 m_controller
= new DolphinController(this);
98 m_controller
->setUrl(url
);
100 // Receiver of the DolphinView signal 'urlChanged()' don't need
101 // to care whether the internal controller changed the URL already or whether
102 // the controller just requested an URL change and will be updated later.
103 // In both cases the URL has been changed:
104 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
105 this, SIGNAL(urlChanged(const KUrl
&)));
106 connect(m_controller
, SIGNAL(requestUrlChange(const KUrl
&)),
107 this, SIGNAL(urlChanged(const KUrl
&)));
109 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&)),
110 this, SLOT(openContextMenu(const QPoint
&)));
111 connect(m_controller
, SIGNAL(urlsDropped(const KUrl::List
&, const KUrl
&, const KFileItem
&)),
112 this, SLOT(dropUrls(const KUrl::List
&, const KUrl
&, const KFileItem
&)));
113 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
114 this, SLOT(updateSorting(DolphinView::Sorting
)));
115 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
116 this, SLOT(updateSortOrder(Qt::SortOrder
)));
117 connect(m_controller
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
118 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList
&)));
119 connect(m_controller
, SIGNAL(itemTriggered(const KFileItem
&)),
120 this, SLOT(triggerItem(const KFileItem
&)));
121 connect(m_controller
, SIGNAL(activated()),
122 this, SLOT(activate()));
123 connect(m_controller
, SIGNAL(itemEntered(const KFileItem
&)),
124 this, SLOT(showHoverInformation(const KFileItem
&)));
125 connect(m_controller
, SIGNAL(viewportEntered()),
126 this, SLOT(clearHoverInformation()));
128 applyViewProperties(url
);
129 m_topLayout
->addWidget(itemView());
132 DolphinView::~DolphinView()
134 if (m_previewJob
!= 0) {
135 m_previewJob
->kill();
140 const KUrl
& DolphinView::url() const
142 return m_controller
->url();
145 KUrl
DolphinView::rootUrl() const
147 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
150 void DolphinView::setActive(bool active
)
152 if (active
== m_active
) {
157 m_selectionModel
->clearSelection();
159 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
161 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
162 // bypasses the problem when having a split view and changing the active view to
163 // update the some URL dependent states. A nicer approach should be no big deal...
164 emit
urlChanged(url());
165 emit
selectionChanged(selectedItems());
170 QWidget
* viewport
= itemView()->viewport();
172 palette
.setColor(viewport
->backgroundRole(), color
);
173 viewport
->setPalette(palette
);
181 m_controller
->indicateActivationChange(active
);
184 bool DolphinView::isActive() const
189 void DolphinView::setMode(Mode mode
)
191 if (mode
== m_mode
) {
192 return; // the wished mode is already set
197 if (isColumnViewActive()) {
198 // When changing the mode in the column view, it makes sense
199 // to go back to the root URL of the column view automatically.
200 // Otherwise there it would not be possible to turn off the column view
201 // without focusing the first column.
202 const KUrl root
= rootUrl();
204 m_controller
->setUrl(root
);
209 // It is important to read the view properties _after_ deleting the view,
210 // as e. g. the detail view might adjust the additional information properties
211 // after getting closed:
212 const KUrl viewPropsUrl
= viewPropertiesUrl();
213 ViewProperties
props(viewPropsUrl
);
214 props
.setViewMode(m_mode
);
217 // the file item delegate has been recreated, apply the current
218 // additional information manually
219 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
220 m_fileItemDelegate
->setShowInformation(infoList
);
221 emit
additionalInfoChanged(infoList
);
223 // Not all view modes support categorized sorting. Adjust the sorting model
224 // if changing the view mode results in a change of the categorized sorting
226 m_storedCategorizedSorting
= props
.categorizedSorting();
227 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
228 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
229 m_proxyModel
->setCategorizedModel(categorized
);
230 emit
categorizedSortingChanged();
236 DolphinView::Mode
DolphinView::mode() const
241 void DolphinView::setShowPreview(bool show
)
243 if (m_showPreview
== show
) {
247 const KUrl viewPropsUrl
= viewPropertiesUrl();
248 ViewProperties
props(viewPropsUrl
);
249 props
.setShowPreview(show
);
251 m_showPreview
= show
;
253 emit
showPreviewChanged();
255 loadDirectory(viewPropsUrl
, true);
258 bool DolphinView::showPreview() const
260 return m_showPreview
;
263 void DolphinView::setShowHiddenFiles(bool show
)
265 if (m_dirLister
->showingDotFiles() == show
) {
269 const KUrl viewPropsUrl
= viewPropertiesUrl();
270 ViewProperties
props(viewPropsUrl
);
271 props
.setShowHiddenFiles(show
);
273 m_dirLister
->setShowingDotFiles(show
);
274 emit
showHiddenFilesChanged();
276 loadDirectory(viewPropsUrl
, true);
279 bool DolphinView::showHiddenFiles() const
281 return m_dirLister
->showingDotFiles();
284 void DolphinView::setCategorizedSorting(bool categorized
)
286 if (categorized
== categorizedSorting()) {
290 // setCategorizedSorting(true) may only get invoked
291 // if the view supports categorized sorting
292 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
294 ViewProperties
props(viewPropertiesUrl());
295 props
.setCategorizedSorting(categorized
);
298 m_storedCategorizedSorting
= categorized
;
299 m_proxyModel
->setCategorizedModel(categorized
);
301 emit
categorizedSortingChanged();
304 bool DolphinView::categorizedSorting() const
306 // If all view modes would support categorized sorting, returning
307 // m_proxyModel->isCategorizedModel() would be the way to go. As
308 // currently only the icons view supports caterized sorting, we remember
309 // the stored view properties state in m_storedCategorizedSorting and
310 // return this state. The application takes care to disable the corresponding
311 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
312 // that this setting is not applied to the current view mode.
313 return m_storedCategorizedSorting
;
316 bool DolphinView::supportsCategorizedSorting() const
318 return m_iconsView
!= 0;
321 void DolphinView::selectAll()
323 QAbstractItemView
* view
= itemView();
324 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
325 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
326 // selection instead of selecting all items. This is bypassed for KDE 4.0
327 // by invoking clearSelection() first.
328 view
->clearSelection();
332 void DolphinView::invertSelection()
334 if (isColumnViewActive()) {
335 // QAbstractItemView does not offer a virtual method invertSelection()
336 // as counterpart to QAbstractItemView::selectAll(). This makes it
337 // necessary to delegate the inverting of the selection to the
338 // column view, as only the selection of the active column should
340 m_columnView
->invertSelection();
342 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
343 const QAbstractItemModel
* itemModel
= selectionModel
->model();
345 const QModelIndex topLeft
= itemModel
->index(0, 0);
346 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
347 itemModel
->columnCount() - 1);
349 const QItemSelection
selection(topLeft
, bottomRight
);
350 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
354 bool DolphinView::hasSelection() const
356 return itemView()->selectionModel()->hasSelection();
359 void DolphinView::clearSelection()
361 itemView()->selectionModel()->clear();
364 KFileItemList
DolphinView::selectedItems() const
366 const QAbstractItemView
* view
= itemView();
368 // Our view has a selection, we will map them back to the DolphinModel
369 // and then fill the KFileItemList.
370 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
372 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
373 KFileItemList itemList
;
375 const QModelIndexList indexList
= selection
.indexes();
376 foreach (QModelIndex index
, indexList
) {
377 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
378 if (!item
.isNull()) {
379 itemList
.append(item
);
386 KUrl::List
DolphinView::selectedUrls() const
389 const KFileItemList list
= selectedItems();
390 foreach (KFileItem item
, list
) {
391 urls
.append(item
.url());
396 KFileItem
DolphinView::fileItem(const QModelIndex
& index
) const
398 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
399 return m_dolphinModel
->itemForIndex(dolphinModelIndex
);
402 void DolphinView::setContentsPosition(int x
, int y
)
404 QAbstractItemView
* view
= itemView();
406 // the ColumnView takes care itself for the horizontal scrolling
407 if (!isColumnViewActive()) {
408 view
->horizontalScrollBar()->setValue(x
);
410 view
->verticalScrollBar()->setValue(y
);
412 m_loadingDirectory
= false;
415 QPoint
DolphinView::contentsPosition() const
417 const int x
= itemView()->horizontalScrollBar()->value();
418 const int y
= itemView()->verticalScrollBar()->value();
422 void DolphinView::zoomIn()
424 m_controller
->triggerZoomIn();
427 void DolphinView::zoomOut()
429 m_controller
->triggerZoomOut();
432 bool DolphinView::isZoomInPossible() const
434 return m_controller
->isZoomInPossible();
437 bool DolphinView::isZoomOutPossible() const
439 return m_controller
->isZoomOutPossible();
442 void DolphinView::setSorting(Sorting sorting
)
444 if (sorting
!= this->sorting()) {
445 updateSorting(sorting
);
449 DolphinView::Sorting
DolphinView::sorting() const
451 return m_proxyModel
->sorting();
454 void DolphinView::setSortOrder(Qt::SortOrder order
)
456 if (sortOrder() != order
) {
457 updateSortOrder(order
);
461 Qt::SortOrder
DolphinView::sortOrder() const
463 return m_proxyModel
->sortOrder();
466 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
468 const KUrl viewPropsUrl
= viewPropertiesUrl();
469 ViewProperties
props(viewPropsUrl
);
470 props
.setAdditionalInfo(info
);
471 m_fileItemDelegate
->setShowInformation(info
);
473 emit
additionalInfoChanged(info
);
475 if (itemView() != m_detailsView
) {
476 // the details view requires no reloading of the directory, as it maps
477 // the file item delegate info to its columns internally
478 loadDirectory(viewPropsUrl
, true);
482 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
484 return m_fileItemDelegate
->showInformation();
487 void DolphinView::reload()
490 loadDirectory(url(), true);
493 void DolphinView::refresh()
495 const bool oldActivationState
= m_active
;
499 applyViewProperties(m_controller
->url());
502 setActive(oldActivationState
);
505 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
507 if (m_controller
->url() == url
) {
511 m_controller
->setUrl(url
); // emits urlChanged, which we forward
513 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
514 applyViewProperties(rootUrl
);
515 loadDirectory(rootUrl
);
516 if (itemView() == m_columnView
) {
517 m_columnView
->setRootUrl(rootUrl
);
518 m_columnView
->showColumn(url
);
521 applyViewProperties(url
);
525 emit
startedPathLoading(url
);
528 void DolphinView::setNameFilter(const QString
& nameFilter
)
530 m_proxyModel
->setFilterRegExp(nameFilter
);
532 if (isColumnViewActive()) {
533 // adjusting the directory lister is not enough in the case of the
534 // column view, as each column has its own directory lister internally...
535 m_columnView
->setNameFilter(nameFilter
);
539 void DolphinView::calculateItemCount(int& fileCount
, int& folderCount
)
541 foreach (KFileItem item
, m_dirLister
->items()) {
550 void DolphinView::setUrl(const KUrl
& url
)
552 updateView(url
, KUrl());
555 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
557 QWidget::mouseReleaseEvent(event
);
560 void DolphinView::activate()
565 void DolphinView::triggerItem(const KFileItem
& item
)
567 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
568 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
569 // items are selected by the user, hence don't trigger the
570 // item specified by 'index'
578 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
581 void DolphinView::generatePreviews(const KFileItemList
& items
)
583 if (m_controller
->dolphinView()->showPreview()) {
584 if (m_previewJob
!= 0) {
585 m_previewJob
->kill();
589 m_previewJob
= KIO::filePreview(items
, 128);
590 connect(m_previewJob
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
591 this, SLOT(replaceIcon(const KFileItem
&, const QPixmap
&)));
592 connect(m_previewJob
, SIGNAL(finished(KJob
*)),
593 this, SLOT(slotPreviewJobFinished(KJob
*)));
597 void DolphinView::replaceIcon(const KFileItem
& item
, const QPixmap
& pixmap
)
599 Q_ASSERT(!item
.isNull());
600 if (!m_showPreview
|| (item
.url().directory() != m_dirLister
->url().path())) {
601 // the preview has been deactivated in the meanwhile or the preview
602 // job is still working on items of an older URL, hence
603 // the item is not part of the directory model anymore
607 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
608 if (idx
.isValid() && (idx
.column() == 0)) {
609 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
610 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
611 KIconEffect iconEffect
;
612 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
613 m_dolphinModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
615 m_dolphinModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
620 void DolphinView::emitSelectionChangedSignal()
622 emit
selectionChanged(DolphinView::selectedItems());
625 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
627 if (!url
.isValid()) {
628 const QString
location(url
.pathOrUrl());
629 if (location
.isEmpty()) {
630 emit
errorMessage(i18nc("@info:status", "The location is empty."));
632 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
637 m_cutItemsCache
.clear();
638 m_loadingDirectory
= true;
641 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
643 if (isColumnViewActive()) {
644 // adjusting the directory lister is not enough in the case of the
645 // column view, as each column has its own directory lister internally...
647 m_columnView
->reload();
649 m_columnView
->showColumn(url
);
654 KUrl
DolphinView::viewPropertiesUrl() const
656 if (isColumnViewActive()) {
657 return m_dirLister
->url();
663 void DolphinView::applyViewProperties(const KUrl
& url
)
665 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
666 // The column view is active, hence don't apply the view properties
667 // of sub directories (represented by columns) to the view. The
668 // view always represents the properties of the first column.
672 const ViewProperties
props(url
);
674 const Mode mode
= props
.viewMode();
675 if (m_mode
!= mode
) {
680 if (itemView() == 0) {
683 Q_ASSERT(itemView() != 0);
684 Q_ASSERT(m_fileItemDelegate
!= 0);
686 const bool showHiddenFiles
= props
.showHiddenFiles();
687 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
688 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
689 emit
showHiddenFilesChanged();
692 m_storedCategorizedSorting
= props
.categorizedSorting();
693 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
694 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
695 m_proxyModel
->setCategorizedModel(categorized
);
696 emit
categorizedSortingChanged();
699 const DolphinView::Sorting sorting
= props
.sorting();
700 if (sorting
!= m_proxyModel
->sorting()) {
701 m_proxyModel
->setSorting(sorting
);
702 emit
sortingChanged(sorting
);
705 const Qt::SortOrder sortOrder
= props
.sortOrder();
706 if (sortOrder
!= m_proxyModel
->sortOrder()) {
707 m_proxyModel
->setSortOrder(sortOrder
);
708 emit
sortOrderChanged(sortOrder
);
711 KFileItemDelegate::InformationList info
= props
.additionalInfo();
712 if (info
!= m_fileItemDelegate
->showInformation()) {
713 m_fileItemDelegate
->setShowInformation(info
);
714 emit
additionalInfoChanged(info
);
717 const bool showPreview
= props
.showPreview();
718 if (showPreview
!= m_showPreview
) {
719 m_showPreview
= showPreview
;
720 emit
showPreviewChanged();
724 void DolphinView::changeSelection(const KFileItemList
& selection
)
727 if (selection
.isEmpty()) {
730 const KUrl
& baseUrl
= url();
732 QItemSelection new_selection
;
733 foreach(const KFileItem
& item
, selection
) {
734 url
= item
.url().upUrl();
735 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
736 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
737 new_selection
.select(index
, index
);
740 itemView()->selectionModel()->select(new_selection
,
741 QItemSelectionModel::ClearAndSelect
742 | QItemSelectionModel::Current
);
745 void DolphinView::openContextMenu(const QPoint
& pos
)
749 const QModelIndex index
= itemView()->indexAt(pos
);
750 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
751 item
= fileItem(index
);
754 emit
requestContextMenu(item
, url());
757 void DolphinView::dropUrls(const KUrl::List
& urls
,
758 const KUrl
& destPath
,
759 const KFileItem
& destItem
)
761 Q_ASSERT(!urls
.isEmpty());
762 const KUrl
& destination
= !destItem
.isNull() && destItem
.isDir() ?
763 destItem
.url() : destPath
;
764 const KUrl sourceDir
= KUrl(urls
.first().directory());
765 if (sourceDir
!= destination
) {
766 dropUrls(urls
, destination
);
770 void DolphinView::dropUrls(const KUrl::List
& urls
,
771 const KUrl
& destination
)
773 DolphinDropController
dropController(this);
774 // forward doingOperation signal up to the mainwindow
775 connect(&dropController
, SIGNAL(doingOperation(KonqFileUndoManager::CommandType
)),
776 this, SIGNAL(doingOperation(KonqFileUndoManager::CommandType
)));
777 dropController
.dropUrls(urls
, destination
);
780 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
782 ViewProperties
props(viewPropertiesUrl());
783 props
.setSorting(sorting
);
785 m_proxyModel
->setSorting(sorting
);
787 emit
sortingChanged(sorting
);
790 void DolphinView::updateSortOrder(Qt::SortOrder order
)
792 ViewProperties
props(viewPropertiesUrl());
793 props
.setSortOrder(order
);
795 m_proxyModel
->setSortOrder(order
);
797 emit
sortOrderChanged(order
);
800 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
802 ViewProperties
props(viewPropertiesUrl());
803 props
.setAdditionalInfo(info
);
806 m_fileItemDelegate
->setShowInformation(info
);
808 emit
additionalInfoChanged(info
);
812 void DolphinView::emitContentsMoved()
814 // only emit the contents moved signal if:
815 // - no directory loading is ongoing (this would reset the contents position
817 // - if the Column View is active: the column view does an automatic
818 // positioning during the loading operation, which must be remembered
819 if (!m_loadingDirectory
|| isColumnViewActive()) {
820 const QPoint
pos(contentsPosition());
821 emit
contentsMoved(pos
.x(), pos
.y());
825 void DolphinView::updateCutItems()
827 // restore the icons of all previously selected items to the
829 QList
<CutItem
>::const_iterator it
= m_cutItemsCache
.begin();
830 QList
<CutItem
>::const_iterator end
= m_cutItemsCache
.end();
832 const QModelIndex index
= m_dolphinModel
->indexForUrl((*it
).url
);
833 if (index
.isValid()) {
834 m_dolphinModel
->setData(index
, QIcon((*it
).pixmap
), Qt::DecorationRole
);
838 m_cutItemsCache
.clear();
840 // ... and apply an item effect to all currently cut items
841 applyCutItemEffect();
844 void DolphinView::showHoverInformation(const KFileItem
& item
)
846 if (hasSelection() || !m_active
) {
850 emit
requestItemInfo(item
);
853 void DolphinView::clearHoverInformation()
856 emit
requestItemInfo(KFileItem());
861 void DolphinView::createView()
864 Q_ASSERT(m_iconsView
== 0);
865 Q_ASSERT(m_detailsView
== 0);
866 Q_ASSERT(m_columnView
== 0);
868 QAbstractItemView
* view
= 0;
871 m_iconsView
= new DolphinIconsView(this, m_controller
);
877 m_detailsView
= new DolphinDetailsView(this, m_controller
);
878 view
= m_detailsView
;
882 m_columnView
= new DolphinColumnView(this, m_controller
);
889 m_fileItemDelegate
= new KFileItemDelegate(view
);
890 view
->setItemDelegate(m_fileItemDelegate
);
892 view
->setModel(m_proxyModel
);
893 if (m_selectionModel
!= 0) {
894 view
->setSelectionModel(m_selectionModel
);
896 m_selectionModel
= view
->selectionModel();
899 // reparent the selection model, as it should not be deleted
900 // when deleting the model
901 m_selectionModel
->setParent(this);
903 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
905 new KMimeTypeResolver(view
, m_dolphinModel
);
906 m_topLayout
->insertWidget(1, view
);
908 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
909 this, SLOT(emitSelectionChangedSignal()));
910 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
911 this, SLOT(emitContentsMoved()));
912 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
913 this, SLOT(emitContentsMoved()));
916 void DolphinView::deleteView()
918 QAbstractItemView
* view
= itemView();
920 m_topLayout
->removeWidget(view
);
927 m_fileItemDelegate
= 0;
931 QAbstractItemView
* DolphinView::itemView() const
933 if (m_detailsView
!= 0) {
934 return m_detailsView
;
935 } else if (m_columnView
!= 0) {
942 bool DolphinView::isCutItem(const KFileItem
& item
) const
944 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
945 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
947 const KUrl
& itemUrl
= item
.url();
948 KUrl::List::const_iterator it
= cutUrls
.begin();
949 const KUrl::List::const_iterator end
= cutUrls
.end();
951 if (*it
== itemUrl
) {
960 void DolphinView::applyCutItemEffect()
962 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
963 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
967 KFileItemList
items(m_dirLister
->items());
968 KFileItemList::const_iterator it
= items
.begin();
969 const KFileItemList::const_iterator end
= items
.end();
971 const KFileItem item
= *it
;
972 if (isCutItem(item
)) {
973 const QModelIndex index
= m_dolphinModel
->indexForItem(item
);
974 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
975 if (value
.type() == QVariant::Icon
) {
976 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
977 QPixmap pixmap
= icon
.pixmap(128, 128);
979 // remember current pixmap for the item to be able
980 // to restore it when other items get cut
982 cutItem
.url
= item
.url();
983 cutItem
.pixmap
= pixmap
;
984 m_cutItemsCache
.append(cutItem
);
986 // apply icon effect to the cut item
987 KIconEffect iconEffect
;
988 pixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
989 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
996 KToggleAction
* DolphinView::iconsModeAction(KActionCollection
* actionCollection
)
998 KToggleAction
* iconsView
= actionCollection
->add
<KToggleAction
>("icons");
999 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
1000 iconsView
->setShortcut(Qt::CTRL
| Qt::Key_1
);
1001 iconsView
->setIcon(KIcon("view-list-icons"));
1002 iconsView
->setData(QVariant::fromValue(IconsView
));
1006 KToggleAction
* DolphinView::detailsModeAction(KActionCollection
* actionCollection
)
1008 KToggleAction
* detailsView
= actionCollection
->add
<KToggleAction
>("details");
1009 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
1010 detailsView
->setShortcut(Qt::CTRL
| Qt::Key_2
);
1011 detailsView
->setIcon(KIcon("view-list-details"));
1012 detailsView
->setData(QVariant::fromValue(DetailsView
));
1016 KToggleAction
* DolphinView::columnsModeAction(KActionCollection
* actionCollection
)
1018 KToggleAction
* columnView
= actionCollection
->add
<KToggleAction
>("columns");
1019 columnView
->setText(i18nc("@action:inmenu View Mode", "Columns"));
1020 columnView
->setShortcut(Qt::CTRL
| Qt::Key_3
);
1021 columnView
->setIcon(KIcon("view-file-columns"));
1022 columnView
->setData(QVariant::fromValue(ColumnView
));
1026 QString
DolphinView::currentViewModeActionName() const
1029 case DolphinView::IconsView
:
1031 case DolphinView::DetailsView
:
1033 case DolphinView::ColumnView
:
1036 return QString(); // can't happen
1039 void DolphinView::renameSelectedItems()
1041 const KFileItemList items
= selectedItems();
1042 if (items
.count() > 1) {
1043 // More than one item has been selected for renaming. Open
1044 // a rename dialog and rename all items afterwards.
1045 RenameDialog
dialog(this, items
);
1046 if (dialog
.exec() == QDialog::Rejected
) {
1050 const QString newName
= dialog
.newName();
1051 if (newName
.isEmpty()) {
1052 emit
errorMessage(dialog
.errorString());
1054 // TODO: check how this can be integrated into KonqFileUndoManager/KonqOperations
1055 // as one operation instead of n rename operations like it is done now...
1056 Q_ASSERT(newName
.contains('#'));
1058 // iterate through all selected items and rename them...
1059 const int replaceIndex
= newName
.indexOf('#');
1060 Q_ASSERT(replaceIndex
>= 0);
1063 KFileItemList::const_iterator it
= items
.begin();
1064 const KFileItemList::const_iterator end
= items
.end();
1066 const KUrl
& oldUrl
= (*it
).url();
1068 number
.setNum(index
++);
1070 QString
name(newName
);
1071 name
.replace(replaceIndex
, 1, number
);
1073 if (oldUrl
.fileName() != name
) {
1074 KUrl newUrl
= oldUrl
;
1075 newUrl
.setFileName(name
);
1076 KonqOperations::rename(this, oldUrl
, newUrl
);
1077 emit
doingOperation(KonqFileUndoManager::RENAME
);
1083 // Only one item has been selected for renaming. Use the custom
1084 // renaming mechanism from the views.
1085 Q_ASSERT(items
.count() == 1);
1087 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
1088 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
1089 // is a benefit for the user at all -> let's wait for some input first...
1090 RenameDialog
dialog(this, items
);
1091 if (dialog
.exec() == QDialog::Rejected
) {
1095 const QString
& newName
= dialog
.newName();
1096 if (newName
.isEmpty()) {
1097 emit
errorMessage(dialog
.errorString());
1099 const KUrl
& oldUrl
= items
.first().url();
1100 KUrl newUrl
= oldUrl
;
1101 newUrl
.setFileName(newName
);
1102 KonqOperations::rename(this, oldUrl
, newUrl
);
1103 emit
doingOperation(KonqFileUndoManager::RENAME
);
1108 void DolphinView::trashSelectedItems()
1110 emit
doingOperation(KonqFileUndoManager::TRASH
);
1111 KonqOperations::del(this, KonqOperations::TRASH
, selectedUrls());
1114 void DolphinView::deleteSelectedItems()
1116 const KUrl::List list
= selectedUrls();
1117 const bool del
= KonqOperations::askDeleteConfirmation(list
,
1118 KonqOperations::DEL
,
1119 KonqOperations::DEFAULT_CONFIRMATION
,
1123 KIO::Job
* job
= KIO::del(list
);
1124 connect(job
, SIGNAL(result(KJob
*)),
1125 this, SLOT(slotDeleteFileFinished(KJob
*)));
1129 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1131 if (job
->error() == 0) {
1132 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1134 emit
errorMessage(job
->errorString());
1138 void DolphinView::slotPreviewJobFinished(KJob
* job
)
1140 Q_ASSERT(job
== m_previewJob
);
1144 void DolphinView::cutSelectedItems()
1146 QMimeData
* mimeData
= new QMimeData();
1147 const KUrl::List kdeUrls
= selectedUrls();
1148 const KUrl::List mostLocalUrls
;
1149 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, true);
1150 QApplication::clipboard()->setMimeData(mimeData
);
1153 void DolphinView::copySelectedItems()
1155 QMimeData
* mimeData
= new QMimeData();
1156 const KUrl::List kdeUrls
= selectedUrls();
1157 const KUrl::List mostLocalUrls
;
1158 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, false);
1159 QApplication::clipboard()->setMimeData(mimeData
);
1162 void DolphinView::paste()
1164 QClipboard
* clipboard
= QApplication::clipboard();
1165 const QMimeData
* mimeData
= clipboard
->mimeData();
1167 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1169 // per default the pasting is done into the current Url of the view
1170 KUrl
destUrl(url());
1172 // check whether the pasting should be done into a selected directory
1173 const KUrl::List selectedUrls
= this->selectedUrls();
1174 if (selectedUrls
.count() == 1) {
1175 const KFileItem
fileItem(S_IFDIR
,
1177 selectedUrls
.first(),
1179 if (fileItem
.isDir()) {
1180 // only one item is selected which is a directory, hence paste
1181 // into this directory
1182 destUrl
= selectedUrls
.first();
1186 if (KonqMimeData::decodeIsCutSelection(mimeData
)) {
1187 KonqOperations::copy(this, KonqOperations::MOVE
, sourceUrls
, destUrl
);
1188 emit
doingOperation(KonqFileUndoManager::MOVE
);
1191 KonqOperations::copy(this, KonqOperations::COPY
, sourceUrls
, destUrl
);
1192 emit
doingOperation(KonqFileUndoManager::COPY
);
1196 QPair
<bool, QString
> DolphinView::pasteInfo() const
1198 QPair
<bool, QString
> ret
;
1199 QClipboard
* clipboard
= QApplication::clipboard();
1200 const QMimeData
* mimeData
= clipboard
->mimeData();
1202 KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1203 if (!urls
.isEmpty()) {
1205 ret
.second
= i18ncp("@action:inmenu", "Paste One File", "Paste %1 Files", urls
.count());
1208 ret
.second
= i18nc("@action:inmenu", "Paste");
1212 const KFileItemList items
= selectedItems();
1213 const uint count
= items
.count();
1215 // pasting should not be allowed when more than one file
1218 } else if (count
== 1) {
1219 // Only one file is selected. Pasting is only allowed if this
1220 // file is a directory.
1221 ret
.first
= items
.first().isDir();
1227 KAction
* DolphinView::createRenameAction(KActionCollection
* collection
)
1229 KAction
* rename
= collection
->addAction("rename");
1230 rename
->setText(i18nc("@action:inmenu File", "Rename..."));
1231 rename
->setShortcut(Qt::Key_F2
);
1235 KAction
* DolphinView::createMoveToTrashAction(KActionCollection
* collection
)
1237 KAction
* moveToTrash
= collection
->addAction("move_to_trash");
1238 moveToTrash
->setText(i18nc("@action:inmenu File", "Move to Trash"));
1239 moveToTrash
->setIcon(KIcon("user-trash"));
1240 moveToTrash
->setShortcut(QKeySequence::Delete
);
1244 KAction
* DolphinView::createDeleteAction(KActionCollection
* collection
)
1246 KAction
* deleteAction
= collection
->addAction("delete");
1247 deleteAction
->setIcon(KIcon("edit-delete"));
1248 deleteAction
->setText(i18nc("@action:inmenu File", "Delete"));
1249 deleteAction
->setShortcut(Qt::SHIFT
| Qt::Key_Delete
);
1250 return deleteAction
;
1253 #include "dolphinview.moc"