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"
23 #include <QApplication>
26 #include <QItemSelection>
31 #include <kactioncollection.h>
32 #include <kcolorscheme.h>
33 #include <kdirlister.h>
34 #include <kfileitemdelegate.h>
36 #include <kiconeffect.h>
37 #include <kio/deletejob.h>
38 #include <kio/netaccess.h>
39 #include <kio/previewjob.h>
42 #include <kmimetyperesolver.h>
43 #include <konqmimedata.h>
44 #include <konq_operations.h>
45 #include <ktoggleaction.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 const KUrl viewPropsUrl
= viewPropertiesUrl();
210 ViewProperties
props(viewPropsUrl
);
211 props
.setViewMode(m_mode
);
214 // the file item delegate has been recreated, apply the current
215 // additional information manually
216 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
217 m_fileItemDelegate
->setShowInformation(infoList
);
218 emit
additionalInfoChanged(infoList
);
220 // Not all view modes support categorized sorting. Adjust the sorting model
221 // if changing the view mode results in a change of the categorized sorting
223 m_storedCategorizedSorting
= props
.categorizedSorting();
224 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
225 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
226 m_proxyModel
->setCategorizedModel(categorized
);
227 emit
categorizedSortingChanged();
233 DolphinView::Mode
DolphinView::mode() const
238 void DolphinView::setShowPreview(bool show
)
240 if (m_showPreview
== show
) {
244 const KUrl viewPropsUrl
= viewPropertiesUrl();
245 ViewProperties
props(viewPropsUrl
);
246 props
.setShowPreview(show
);
248 m_showPreview
= show
;
250 emit
showPreviewChanged();
252 loadDirectory(viewPropsUrl
, true);
255 bool DolphinView::showPreview() const
257 return m_showPreview
;
260 void DolphinView::setShowHiddenFiles(bool show
)
262 if (m_dirLister
->showingDotFiles() == show
) {
266 const KUrl viewPropsUrl
= viewPropertiesUrl();
267 ViewProperties
props(viewPropsUrl
);
268 props
.setShowHiddenFiles(show
);
270 m_dirLister
->setShowingDotFiles(show
);
271 emit
showHiddenFilesChanged();
273 loadDirectory(viewPropsUrl
, true);
276 bool DolphinView::showHiddenFiles() const
278 return m_dirLister
->showingDotFiles();
281 void DolphinView::setCategorizedSorting(bool categorized
)
283 if (categorized
== categorizedSorting()) {
287 // setCategorizedSorting(true) may only get invoked
288 // if the view supports categorized sorting
289 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
291 ViewProperties
props(viewPropertiesUrl());
292 props
.setCategorizedSorting(categorized
);
295 m_storedCategorizedSorting
= categorized
;
296 m_proxyModel
->setCategorizedModel(categorized
);
298 emit
categorizedSortingChanged();
301 bool DolphinView::categorizedSorting() const
303 // If all view modes would support categorized sorting, returning
304 // m_proxyModel->isCategorizedModel() would be the way to go. As
305 // currently only the icons view supports caterized sorting, we remember
306 // the stored view properties state in m_storedCategorizedSorting and
307 // return this state. The application takes care to disable the corresponding
308 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
309 // that this setting is not applied to the current view mode.
310 return m_storedCategorizedSorting
;
313 bool DolphinView::supportsCategorizedSorting() const
315 return m_iconsView
!= 0;
318 void DolphinView::selectAll()
320 QAbstractItemView
* view
= itemView();
321 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
322 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
323 // selection instead of selecting all items. This is bypassed for KDE 4.0
324 // by invoking clearSelection() first.
325 view
->clearSelection();
329 void DolphinView::invertSelection()
331 if (isColumnViewActive()) {
332 // QAbstractItemView does not offer a virtual method invertSelection()
333 // as counterpart to QAbstractItemView::selectAll(). This makes it
334 // necessary to delegate the inverting of the selection to the
335 // column view, as only the selection of the active column should
337 m_columnView
->invertSelection();
339 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
340 const QAbstractItemModel
* itemModel
= selectionModel
->model();
342 const QModelIndex topLeft
= itemModel
->index(0, 0);
343 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
344 itemModel
->columnCount() - 1);
346 const QItemSelection
selection(topLeft
, bottomRight
);
347 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
351 bool DolphinView::hasSelection() const
353 return itemView()->selectionModel()->hasSelection();
356 void DolphinView::clearSelection()
358 itemView()->selectionModel()->clear();
361 KFileItemList
DolphinView::selectedItems() const
363 const QAbstractItemView
* view
= itemView();
365 // Our view has a selection, we will map them back to the DolphinModel
366 // and then fill the KFileItemList.
367 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
369 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
370 KFileItemList itemList
;
372 const QModelIndexList indexList
= selection
.indexes();
373 foreach (QModelIndex index
, indexList
) {
374 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
375 if (!item
.isNull()) {
376 itemList
.append(item
);
383 KUrl::List
DolphinView::selectedUrls() const
386 const KFileItemList list
= selectedItems();
387 foreach (KFileItem item
, list
) {
388 urls
.append(item
.url());
393 KFileItem
DolphinView::fileItem(const QModelIndex
& index
) const
395 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
396 return m_dolphinModel
->itemForIndex(dolphinModelIndex
);
399 void DolphinView::setContentsPosition(int x
, int y
)
401 QAbstractItemView
* view
= itemView();
403 // the ColumnView takes care itself for the horizontal scrolling
404 if (!isColumnViewActive()) {
405 view
->horizontalScrollBar()->setValue(x
);
407 view
->verticalScrollBar()->setValue(y
);
409 m_loadingDirectory
= false;
412 QPoint
DolphinView::contentsPosition() const
414 const int x
= itemView()->horizontalScrollBar()->value();
415 const int y
= itemView()->verticalScrollBar()->value();
419 void DolphinView::zoomIn()
421 m_controller
->triggerZoomIn();
424 void DolphinView::zoomOut()
426 m_controller
->triggerZoomOut();
429 bool DolphinView::isZoomInPossible() const
431 return m_controller
->isZoomInPossible();
434 bool DolphinView::isZoomOutPossible() const
436 return m_controller
->isZoomOutPossible();
439 void DolphinView::setSorting(Sorting sorting
)
441 if (sorting
!= this->sorting()) {
442 updateSorting(sorting
);
446 DolphinView::Sorting
DolphinView::sorting() const
448 return m_proxyModel
->sorting();
451 void DolphinView::setSortOrder(Qt::SortOrder order
)
453 if (sortOrder() != order
) {
454 updateSortOrder(order
);
458 Qt::SortOrder
DolphinView::sortOrder() const
460 return m_proxyModel
->sortOrder();
463 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
465 const KUrl viewPropsUrl
= viewPropertiesUrl();
466 ViewProperties
props(viewPropsUrl
);
467 props
.setAdditionalInfo(info
);
468 m_fileItemDelegate
->setShowInformation(info
);
470 emit
additionalInfoChanged(info
);
472 if (itemView() != m_detailsView
) {
473 // the details view requires no reloading of the directory, as it maps
474 // the file item delegate info to its columns internally
475 loadDirectory(viewPropsUrl
, true);
479 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
481 return m_fileItemDelegate
->showInformation();
484 void DolphinView::reload()
487 loadDirectory(url(), true);
490 void DolphinView::refresh()
492 const bool oldActivationState
= m_active
;
496 applyViewProperties(m_controller
->url());
499 setActive(oldActivationState
);
502 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
504 if (m_controller
->url() == url
) {
508 m_controller
->setUrl(url
); // emits urlChanged, which we forward
510 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
511 applyViewProperties(rootUrl
);
512 loadDirectory(rootUrl
);
513 if (itemView() == m_columnView
) {
514 m_columnView
->setRootUrl(rootUrl
);
515 m_columnView
->showColumn(url
);
518 applyViewProperties(url
);
522 emit
startedPathLoading(url
);
525 void DolphinView::setNameFilter(const QString
& nameFilter
)
527 m_proxyModel
->setFilterRegExp(nameFilter
);
529 if (isColumnViewActive()) {
530 // adjusting the directory lister is not enough in the case of the
531 // column view, as each column has its own directory lister internally...
532 m_columnView
->setNameFilter(nameFilter
);
536 void DolphinView::calculateItemCount(int& fileCount
, int& folderCount
)
538 foreach (KFileItem item
, m_dirLister
->items()) {
547 void DolphinView::setUrl(const KUrl
& url
)
549 updateView(url
, KUrl());
552 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
554 QWidget::mouseReleaseEvent(event
);
557 void DolphinView::activate()
562 void DolphinView::triggerItem(const KFileItem
& item
)
564 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
565 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
566 // items are selected by the user, hence don't trigger the
567 // item specified by 'index'
575 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
578 void DolphinView::generatePreviews(const KFileItemList
& items
)
580 if (m_controller
->dolphinView()->showPreview()) {
581 if (m_previewJob
!= 0) {
582 m_previewJob
->kill();
586 m_previewJob
= KIO::filePreview(items
, 128);
587 connect(m_previewJob
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
588 this, SLOT(replaceIcon(const KFileItem
&, const QPixmap
&)));
589 connect(m_previewJob
, SIGNAL(finished(KJob
*)),
590 this, SLOT(slotPreviewJobFinished(KJob
*)));
594 void DolphinView::replaceIcon(const KFileItem
& item
, const QPixmap
& pixmap
)
596 Q_ASSERT(!item
.isNull());
597 if (!m_showPreview
|| (item
.url().directory() != m_dirLister
->url().path())) {
598 // the preview has been deactivated in the meanwhile or the preview
599 // job is still working on items of an older URL, hence
600 // the item is not part of the directory model anymore
604 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
605 if (idx
.isValid() && (idx
.column() == 0)) {
606 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
607 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
608 KIconEffect iconEffect
;
609 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
610 m_dolphinModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
612 m_dolphinModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
617 void DolphinView::emitSelectionChangedSignal()
619 emit
selectionChanged(DolphinView::selectedItems());
622 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
624 if (!url
.isValid()) {
625 const QString
location(url
.pathOrUrl());
626 if (location
.isEmpty()) {
627 emit
errorMessage(i18nc("@info:status", "The location is empty."));
629 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
634 m_cutItemsCache
.clear();
635 m_loadingDirectory
= true;
638 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
640 if (isColumnViewActive()) {
641 // adjusting the directory lister is not enough in the case of the
642 // column view, as each column has its own directory lister internally...
644 m_columnView
->reload();
646 m_columnView
->showColumn(url
);
651 KUrl
DolphinView::viewPropertiesUrl() const
653 if (isColumnViewActive()) {
654 return m_dirLister
->url();
660 void DolphinView::applyViewProperties(const KUrl
& url
)
662 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
663 // The column view is active, hence don't apply the view properties
664 // of sub directories (represented by columns) to the view. The
665 // view always represents the properties of the first column.
669 const ViewProperties
props(url
);
671 const Mode mode
= props
.viewMode();
672 if (m_mode
!= mode
) {
677 if (itemView() == 0) {
680 Q_ASSERT(itemView() != 0);
681 Q_ASSERT(m_fileItemDelegate
!= 0);
683 const bool showHiddenFiles
= props
.showHiddenFiles();
684 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
685 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
686 emit
showHiddenFilesChanged();
689 m_storedCategorizedSorting
= props
.categorizedSorting();
690 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
691 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
692 m_proxyModel
->setCategorizedModel(categorized
);
693 emit
categorizedSortingChanged();
696 const DolphinView::Sorting sorting
= props
.sorting();
697 if (sorting
!= m_proxyModel
->sorting()) {
698 m_proxyModel
->setSorting(sorting
);
699 emit
sortingChanged(sorting
);
702 const Qt::SortOrder sortOrder
= props
.sortOrder();
703 if (sortOrder
!= m_proxyModel
->sortOrder()) {
704 m_proxyModel
->setSortOrder(sortOrder
);
705 emit
sortOrderChanged(sortOrder
);
708 KFileItemDelegate::InformationList info
= props
.additionalInfo();
709 if (info
!= m_fileItemDelegate
->showInformation()) {
710 m_fileItemDelegate
->setShowInformation(info
);
711 emit
additionalInfoChanged(info
);
714 const bool showPreview
= props
.showPreview();
715 if (showPreview
!= m_showPreview
) {
716 m_showPreview
= showPreview
;
717 emit
showPreviewChanged();
721 void DolphinView::changeSelection(const KFileItemList
& selection
)
724 if (selection
.isEmpty()) {
727 const KUrl
& baseUrl
= url();
729 QItemSelection new_selection
;
730 foreach(const KFileItem
& item
, selection
) {
731 url
= item
.url().upUrl();
732 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
733 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
734 new_selection
.select(index
, index
);
737 itemView()->selectionModel()->select(new_selection
,
738 QItemSelectionModel::ClearAndSelect
739 | QItemSelectionModel::Current
);
742 void DolphinView::openContextMenu(const QPoint
& pos
)
746 const QModelIndex index
= itemView()->indexAt(pos
);
747 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
748 item
= fileItem(index
);
751 emit
requestContextMenu(item
, url());
754 void DolphinView::dropUrls(const KUrl::List
& urls
,
755 const KUrl
& destPath
,
756 const KFileItem
& destItem
)
758 Q_ASSERT(!urls
.isEmpty());
759 const KUrl
& destination
= !destItem
.isNull() && destItem
.isDir() ?
760 destItem
.url() : destPath
;
761 const KUrl sourceDir
= KUrl(urls
.first().directory());
762 if (sourceDir
!= destination
) {
763 dropUrls(urls
, destination
);
767 void DolphinView::dropUrls(const KUrl::List
& urls
,
768 const KUrl
& destination
)
770 DolphinDropController
dropController(this);
771 // forward doingOperation signal up to the mainwindow
772 connect(&dropController
, SIGNAL(doingOperation(KonqFileUndoManager::CommandType
)),
773 this, SIGNAL(doingOperation(KonqFileUndoManager::CommandType
)));
774 dropController
.dropUrls(urls
, destination
);
777 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
779 ViewProperties
props(viewPropertiesUrl());
780 props
.setSorting(sorting
);
782 m_proxyModel
->setSorting(sorting
);
784 emit
sortingChanged(sorting
);
787 void DolphinView::updateSortOrder(Qt::SortOrder order
)
789 ViewProperties
props(viewPropertiesUrl());
790 props
.setSortOrder(order
);
792 m_proxyModel
->setSortOrder(order
);
794 emit
sortOrderChanged(order
);
797 void DolphinView::toggleSortOrder()
799 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
800 Qt::DescendingOrder
:
805 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
807 ViewProperties
props(viewPropertiesUrl());
808 props
.setAdditionalInfo(info
);
811 m_fileItemDelegate
->setShowInformation(info
);
813 emit
additionalInfoChanged(info
);
817 void DolphinView::emitContentsMoved()
819 // only emit the contents moved signal if:
820 // - no directory loading is ongoing (this would reset the contents position
822 // - if the Column View is active: the column view does an automatic
823 // positioning during the loading operation, which must be remembered
824 if (!m_loadingDirectory
|| isColumnViewActive()) {
825 const QPoint
pos(contentsPosition());
826 emit
contentsMoved(pos
.x(), pos
.y());
830 void DolphinView::updateCutItems()
832 // restore the icons of all previously selected items to the
834 QList
<CutItem
>::const_iterator it
= m_cutItemsCache
.begin();
835 QList
<CutItem
>::const_iterator end
= m_cutItemsCache
.end();
837 const QModelIndex index
= m_dolphinModel
->indexForUrl((*it
).url
);
838 if (index
.isValid()) {
839 m_dolphinModel
->setData(index
, QIcon((*it
).pixmap
), Qt::DecorationRole
);
843 m_cutItemsCache
.clear();
845 // ... and apply an item effect to all currently cut items
846 applyCutItemEffect();
849 void DolphinView::showHoverInformation(const KFileItem
& item
)
851 if (hasSelection() || !m_active
) {
855 emit
requestItemInfo(item
);
858 void DolphinView::clearHoverInformation()
861 emit
requestItemInfo(KFileItem());
866 void DolphinView::createView()
869 Q_ASSERT(m_iconsView
== 0);
870 Q_ASSERT(m_detailsView
== 0);
871 Q_ASSERT(m_columnView
== 0);
873 QAbstractItemView
* view
= 0;
876 m_iconsView
= new DolphinIconsView(this, m_controller
);
882 m_detailsView
= new DolphinDetailsView(this, m_controller
);
883 view
= m_detailsView
;
887 m_columnView
= new DolphinColumnView(this, m_controller
);
894 m_fileItemDelegate
= new KFileItemDelegate(view
);
895 view
->setItemDelegate(m_fileItemDelegate
);
897 view
->setModel(m_proxyModel
);
898 if (m_selectionModel
!= 0) {
899 view
->setSelectionModel(m_selectionModel
);
901 m_selectionModel
= view
->selectionModel();
904 // reparent the selection model, as it should not be deleted
905 // when deleting the model
906 m_selectionModel
->setParent(this);
908 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
910 new KMimeTypeResolver(view
, m_dolphinModel
);
911 m_topLayout
->insertWidget(1, view
);
913 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
914 this, SLOT(emitSelectionChangedSignal()));
915 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
916 this, SLOT(emitContentsMoved()));
917 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
918 this, SLOT(emitContentsMoved()));
921 void DolphinView::deleteView()
923 QAbstractItemView
* view
= itemView();
925 m_topLayout
->removeWidget(view
);
932 m_fileItemDelegate
= 0;
936 QAbstractItemView
* DolphinView::itemView() const
938 if (m_detailsView
!= 0) {
939 return m_detailsView
;
940 } else if (m_columnView
!= 0) {
947 bool DolphinView::isCutItem(const KFileItem
& item
) const
949 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
950 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
952 const KUrl
& itemUrl
= item
.url();
953 KUrl::List::const_iterator it
= cutUrls
.begin();
954 const KUrl::List::const_iterator end
= cutUrls
.end();
956 if (*it
== itemUrl
) {
965 void DolphinView::applyCutItemEffect()
967 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
968 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
972 KFileItemList
items(m_dirLister
->items());
973 KFileItemList::const_iterator it
= items
.begin();
974 const KFileItemList::const_iterator end
= items
.end();
976 const KFileItem item
= *it
;
977 if (isCutItem(item
)) {
978 const QModelIndex index
= m_dolphinModel
->indexForItem(item
);
979 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
980 if (value
.type() == QVariant::Icon
) {
981 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
982 QPixmap pixmap
= icon
.pixmap(128, 128);
984 // remember current pixmap for the item to be able
985 // to restore it when other items get cut
987 cutItem
.url
= item
.url();
988 cutItem
.pixmap
= pixmap
;
989 m_cutItemsCache
.append(cutItem
);
991 // apply icon effect to the cut item
992 KIconEffect iconEffect
;
993 pixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
994 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
1001 KToggleAction
* DolphinView::iconsModeAction(KActionCollection
* actionCollection
)
1003 KToggleAction
* iconsView
= actionCollection
->add
<KToggleAction
>("icons");
1004 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
1005 iconsView
->setShortcut(Qt::CTRL
| Qt::Key_1
);
1006 iconsView
->setIcon(KIcon("view-list-icons"));
1007 iconsView
->setData(QVariant::fromValue(IconsView
));
1011 KToggleAction
* DolphinView::detailsModeAction(KActionCollection
* actionCollection
)
1013 KToggleAction
* detailsView
= actionCollection
->add
<KToggleAction
>("details");
1014 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
1015 detailsView
->setShortcut(Qt::CTRL
| Qt::Key_2
);
1016 detailsView
->setIcon(KIcon("view-list-details"));
1017 detailsView
->setData(QVariant::fromValue(DetailsView
));
1021 KToggleAction
* DolphinView::columnsModeAction(KActionCollection
* actionCollection
)
1023 KToggleAction
* columnView
= actionCollection
->add
<KToggleAction
>("columns");
1024 columnView
->setText(i18nc("@action:inmenu View Mode", "Columns"));
1025 columnView
->setShortcut(Qt::CTRL
| Qt::Key_3
);
1026 columnView
->setIcon(KIcon("view-file-columns"));
1027 columnView
->setData(QVariant::fromValue(ColumnView
));
1031 QString
DolphinView::currentViewModeActionName() const
1034 case DolphinView::IconsView
:
1036 case DolphinView::DetailsView
:
1038 case DolphinView::ColumnView
:
1041 return QString(); // can't happen
1044 void DolphinView::renameSelectedItems()
1046 const KFileItemList items
= selectedItems();
1047 if (items
.count() > 1) {
1048 // More than one item has been selected for renaming. Open
1049 // a rename dialog and rename all items afterwards.
1050 RenameDialog
dialog(this, items
);
1051 if (dialog
.exec() == QDialog::Rejected
) {
1055 const QString newName
= dialog
.newName();
1056 if (newName
.isEmpty()) {
1057 emit
errorMessage(dialog
.errorString());
1059 // TODO: check how this can be integrated into KonqFileUndoManager/KonqOperations
1060 // as one operation instead of n rename operations like it is done now...
1061 Q_ASSERT(newName
.contains('#'));
1063 // iterate through all selected items and rename them...
1064 const int replaceIndex
= newName
.indexOf('#');
1065 Q_ASSERT(replaceIndex
>= 0);
1068 KFileItemList::const_iterator it
= items
.begin();
1069 const KFileItemList::const_iterator end
= items
.end();
1071 const KUrl
& oldUrl
= (*it
).url();
1073 number
.setNum(index
++);
1075 QString
name(newName
);
1076 name
.replace(replaceIndex
, 1, number
);
1078 if (oldUrl
.fileName() != name
) {
1079 KUrl newUrl
= oldUrl
;
1080 newUrl
.setFileName(name
);
1081 KonqOperations::rename(this, oldUrl
, newUrl
);
1082 emit
doingOperation(KonqFileUndoManager::RENAME
);
1088 // Only one item has been selected for renaming. Use the custom
1089 // renaming mechanism from the views.
1090 Q_ASSERT(items
.count() == 1);
1092 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
1093 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
1094 // is a benefit for the user at all -> let's wait for some input first...
1095 RenameDialog
dialog(this, items
);
1096 if (dialog
.exec() == QDialog::Rejected
) {
1100 const QString
& newName
= dialog
.newName();
1101 if (newName
.isEmpty()) {
1102 emit
errorMessage(dialog
.errorString());
1104 const KUrl
& oldUrl
= items
.first().url();
1105 KUrl newUrl
= oldUrl
;
1106 newUrl
.setFileName(newName
);
1107 KonqOperations::rename(this, oldUrl
, newUrl
);
1108 emit
doingOperation(KonqFileUndoManager::RENAME
);
1113 void DolphinView::trashSelectedItems()
1115 emit
doingOperation(KonqFileUndoManager::TRASH
);
1116 KonqOperations::del(this, KonqOperations::TRASH
, selectedUrls());
1119 void DolphinView::deleteSelectedItems()
1121 const KUrl::List list
= selectedUrls();
1122 const bool del
= KonqOperations::askDeleteConfirmation(list
,
1123 KonqOperations::DEL
,
1124 KonqOperations::DEFAULT_CONFIRMATION
,
1128 KIO::Job
* job
= KIO::del(list
);
1129 connect(job
, SIGNAL(result(KJob
*)),
1130 this, SLOT(slotDeleteFileFinished(KJob
*)));
1134 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1136 if (job
->error() == 0) {
1137 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1139 emit
errorMessage(job
->errorString());
1143 void DolphinView::slotPreviewJobFinished(KJob
* job
)
1145 Q_ASSERT(job
== m_previewJob
);
1149 void DolphinView::cutSelectedItems()
1151 QMimeData
* mimeData
= new QMimeData();
1152 const KUrl::List kdeUrls
= selectedUrls();
1153 const KUrl::List mostLocalUrls
;
1154 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, true);
1155 QApplication::clipboard()->setMimeData(mimeData
);
1158 void DolphinView::copySelectedItems()
1160 QMimeData
* mimeData
= new QMimeData();
1161 const KUrl::List kdeUrls
= selectedUrls();
1162 const KUrl::List mostLocalUrls
;
1163 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, false);
1164 QApplication::clipboard()->setMimeData(mimeData
);
1167 void DolphinView::paste()
1169 QClipboard
* clipboard
= QApplication::clipboard();
1170 const QMimeData
* mimeData
= clipboard
->mimeData();
1172 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1174 // per default the pasting is done into the current Url of the view
1175 KUrl
destUrl(url());
1177 // check whether the pasting should be done into a selected directory
1178 const KUrl::List selectedUrls
= this->selectedUrls();
1179 if (selectedUrls
.count() == 1) {
1180 const KFileItem
fileItem(S_IFDIR
,
1182 selectedUrls
.first(),
1184 if (fileItem
.isDir()) {
1185 // only one item is selected which is a directory, hence paste
1186 // into this directory
1187 destUrl
= selectedUrls
.first();
1191 if (KonqMimeData::decodeIsCutSelection(mimeData
)) {
1192 KonqOperations::copy(this, KonqOperations::MOVE
, sourceUrls
, destUrl
);
1193 emit
doingOperation(KonqFileUndoManager::MOVE
);
1196 KonqOperations::copy(this, KonqOperations::COPY
, sourceUrls
, destUrl
);
1197 emit
doingOperation(KonqFileUndoManager::COPY
);
1201 QPair
<bool, QString
> DolphinView::pasteInfo() const
1203 QPair
<bool, QString
> ret
;
1204 QClipboard
* clipboard
= QApplication::clipboard();
1205 const QMimeData
* mimeData
= clipboard
->mimeData();
1207 KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1208 if (!urls
.isEmpty()) {
1210 ret
.second
= i18ncp("@action:inmenu", "Paste One File", "Paste %1 Files", urls
.count());
1213 ret
.second
= i18nc("@action:inmenu", "Paste");
1217 const KFileItemList items
= selectedItems();
1218 const uint count
= items
.count();
1220 // pasting should not be allowed when more than one file
1223 } else if (count
== 1) {
1224 // Only one file is selected. Pasting is only allowed if this
1225 // file is a directory.
1226 ret
.first
= items
.first().isDir();
1232 KAction
* DolphinView::createRenameAction(KActionCollection
* collection
)
1234 KAction
* rename
= collection
->addAction("rename");
1235 rename
->setText(i18nc("@action:inmenu File", "Rename..."));
1236 rename
->setShortcut(Qt::Key_F2
);
1240 KAction
* DolphinView::createMoveToTrashAction(KActionCollection
* collection
)
1242 KAction
* moveToTrash
= collection
->addAction("move_to_trash");
1243 moveToTrash
->setText(i18nc("@action:inmenu File", "Move to Trash"));
1244 moveToTrash
->setIcon(KIcon("user-trash"));
1245 moveToTrash
->setShortcut(QKeySequence::Delete
);
1249 KAction
* DolphinView::createDeleteAction(KActionCollection
* collection
)
1251 KAction
* deleteAction
= collection
->addAction("delete");
1252 deleteAction
->setIcon(KIcon("edit-delete"));
1253 deleteAction
->setText(i18nc("@action:inmenu File", "Delete"));
1254 deleteAction
->setShortcut(Qt::SHIFT
| Qt::Key_Delete
);
1255 return deleteAction
;
1258 KAction
* DolphinView::createNewDirAction(KActionCollection
* collection
)
1260 // This action doesn't appear in the GUI, it's for the shortcut only.
1261 // KNewMenu takes care of the GUI stuff.
1262 KAction
* newDirAction
= collection
->addAction("create_dir");
1263 newDirAction
->setText(i18n("Create Folder..."));
1264 newDirAction
->setShortcut(Qt::Key_F10
);
1265 return newDirAction
;
1268 KAction
* DolphinView::createSortDescendingAction(KActionCollection
* collection
)
1270 KToggleAction
* sortDescending
= collection
->add
<KToggleAction
>("descending");
1271 sortDescending
->setText(i18nc("@action:inmenu Sort", "Descending"));
1272 return sortDescending
;
1275 #include "dolphinview.moc"