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
,
65 KActionCollection
* actionCollection
) :
69 m_loadingDirectory(false),
70 m_storedCategorizedSorting(false),
71 m_mode(DolphinView::IconsView
),
77 m_fileItemDelegate(0),
79 m_dolphinModel(dolphinModel
),
80 m_dirLister(dirLister
),
81 m_proxyModel(proxyModel
),
84 setFocusPolicy(Qt::StrongFocus
);
85 m_topLayout
= new QVBoxLayout(this);
86 m_topLayout
->setSpacing(0);
87 m_topLayout
->setMargin(0);
89 QClipboard
* clipboard
= QApplication::clipboard();
90 connect(clipboard
, SIGNAL(dataChanged()),
91 this, SLOT(updateCutItems()));
93 connect(m_dirLister
, SIGNAL(completed()),
94 this, SLOT(updateCutItems()));
95 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
96 this, SLOT(generatePreviews(const KFileItemList
&)));
98 m_controller
= new DolphinController(this);
99 m_controller
->setUrl(url
);
101 // Receiver of the DolphinView signal 'urlChanged()' don't need
102 // to care whether the internal controller changed the URL already or whether
103 // the controller just requested an URL change and will be updated later.
104 // In both cases the URL has been changed:
105 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
106 this, SIGNAL(urlChanged(const KUrl
&)));
107 connect(m_controller
, SIGNAL(requestUrlChange(const KUrl
&)),
108 this, SIGNAL(urlChanged(const KUrl
&)));
110 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&)),
111 this, SLOT(openContextMenu(const QPoint
&)));
112 connect(m_controller
, SIGNAL(urlsDropped(const KUrl::List
&, const KUrl
&, const KFileItem
&)),
113 this, SLOT(dropUrls(const KUrl::List
&, const KUrl
&, const KFileItem
&)));
114 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
115 this, SLOT(updateSorting(DolphinView::Sorting
)));
116 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
117 this, SLOT(updateSortOrder(Qt::SortOrder
)));
118 connect(m_controller
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
119 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList
&)));
120 connect(m_controller
, SIGNAL(itemTriggered(const KFileItem
&)),
121 this, SLOT(triggerItem(const KFileItem
&)));
122 connect(m_controller
, SIGNAL(activated()),
123 this, SLOT(activate()));
124 connect(m_controller
, SIGNAL(itemEntered(const KFileItem
&)),
125 this, SLOT(showHoverInformation(const KFileItem
&)));
126 connect(m_controller
, SIGNAL(viewportEntered()),
127 this, SLOT(clearHoverInformation()));
129 applyViewProperties(url
);
130 m_topLayout
->addWidget(itemView());
132 Q_ASSERT(actionCollection
!= 0);
133 if (actionCollection
->action("create_dir") == 0) {
134 // This action doesn't appear in the GUI, it's for the shortcut only.
135 // KNewMenu takes care of the GUI stuff.
136 KAction
* newDirAction
= actionCollection
->addAction("create_dir");
137 newDirAction
->setText(i18n("Create Folder..."));
138 connect(newDirAction
, SIGNAL(triggered()), SLOT(createDir()));
139 newDirAction
->setShortcut(Qt::Key_F10
);
143 DolphinView::~DolphinView()
145 if (m_previewJob
!= 0) {
146 m_previewJob
->kill();
151 const KUrl
& DolphinView::url() const
153 return m_controller
->url();
156 KUrl
DolphinView::rootUrl() const
158 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
161 void DolphinView::setActive(bool active
)
163 if (active
== m_active
) {
168 m_selectionModel
->clearSelection();
170 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
172 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
173 // bypasses the problem when having a split view and changing the active view to
174 // update the some URL dependent states. A nicer approach should be no big deal...
175 emit
urlChanged(url());
176 emit
selectionChanged(selectedItems());
181 QWidget
* viewport
= itemView()->viewport();
183 palette
.setColor(viewport
->backgroundRole(), color
);
184 viewport
->setPalette(palette
);
192 m_controller
->indicateActivationChange(active
);
195 bool DolphinView::isActive() const
200 void DolphinView::setMode(Mode mode
)
202 if (mode
== m_mode
) {
203 return; // the wished mode is already set
208 if (isColumnViewActive()) {
209 // When changing the mode in the column view, it makes sense
210 // to go back to the root URL of the column view automatically.
211 // Otherwise there it would not be possible to turn off the column view
212 // without focusing the first column.
213 const KUrl root
= rootUrl();
215 m_controller
->setUrl(root
);
220 const KUrl viewPropsUrl
= viewPropertiesUrl();
221 ViewProperties
props(viewPropsUrl
);
222 props
.setViewMode(m_mode
);
225 // the file item delegate has been recreated, apply the current
226 // additional information manually
227 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
228 m_fileItemDelegate
->setShowInformation(infoList
);
229 emit
additionalInfoChanged(infoList
);
231 // Not all view modes support categorized sorting. Adjust the sorting model
232 // if changing the view mode results in a change of the categorized sorting
234 m_storedCategorizedSorting
= props
.categorizedSorting();
235 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
236 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
237 m_proxyModel
->setCategorizedModel(categorized
);
238 emit
categorizedSortingChanged();
244 DolphinView::Mode
DolphinView::mode() const
249 void DolphinView::setShowPreview(bool show
)
251 if (m_showPreview
== show
) {
255 const KUrl viewPropsUrl
= viewPropertiesUrl();
256 ViewProperties
props(viewPropsUrl
);
257 props
.setShowPreview(show
);
259 m_showPreview
= show
;
261 emit
showPreviewChanged();
263 loadDirectory(viewPropsUrl
, true);
266 bool DolphinView::showPreview() const
268 return m_showPreview
;
271 void DolphinView::setShowHiddenFiles(bool show
)
273 if (m_dirLister
->showingDotFiles() == show
) {
277 const KUrl viewPropsUrl
= viewPropertiesUrl();
278 ViewProperties
props(viewPropsUrl
);
279 props
.setShowHiddenFiles(show
);
281 m_dirLister
->setShowingDotFiles(show
);
282 emit
showHiddenFilesChanged();
284 loadDirectory(viewPropsUrl
, true);
287 bool DolphinView::showHiddenFiles() const
289 return m_dirLister
->showingDotFiles();
292 void DolphinView::setCategorizedSorting(bool categorized
)
294 if (categorized
== categorizedSorting()) {
298 // setCategorizedSorting(true) may only get invoked
299 // if the view supports categorized sorting
300 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
302 ViewProperties
props(viewPropertiesUrl());
303 props
.setCategorizedSorting(categorized
);
306 m_storedCategorizedSorting
= categorized
;
307 m_proxyModel
->setCategorizedModel(categorized
);
309 emit
categorizedSortingChanged();
312 bool DolphinView::categorizedSorting() const
314 // If all view modes would support categorized sorting, returning
315 // m_proxyModel->isCategorizedModel() would be the way to go. As
316 // currently only the icons view supports caterized sorting, we remember
317 // the stored view properties state in m_storedCategorizedSorting and
318 // return this state. The application takes care to disable the corresponding
319 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
320 // that this setting is not applied to the current view mode.
321 return m_storedCategorizedSorting
;
324 bool DolphinView::supportsCategorizedSorting() const
326 return m_iconsView
!= 0;
329 void DolphinView::selectAll()
331 QAbstractItemView
* view
= itemView();
332 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
333 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
334 // selection instead of selecting all items. This is bypassed for KDE 4.0
335 // by invoking clearSelection() first.
336 view
->clearSelection();
340 void DolphinView::invertSelection()
342 if (isColumnViewActive()) {
343 // QAbstractItemView does not offer a virtual method invertSelection()
344 // as counterpart to QAbstractItemView::selectAll(). This makes it
345 // necessary to delegate the inverting of the selection to the
346 // column view, as only the selection of the active column should
348 m_columnView
->invertSelection();
350 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
351 const QAbstractItemModel
* itemModel
= selectionModel
->model();
353 const QModelIndex topLeft
= itemModel
->index(0, 0);
354 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
355 itemModel
->columnCount() - 1);
357 const QItemSelection
selection(topLeft
, bottomRight
);
358 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
362 bool DolphinView::hasSelection() const
364 return itemView()->selectionModel()->hasSelection();
367 void DolphinView::clearSelection()
369 itemView()->selectionModel()->clear();
372 KFileItemList
DolphinView::selectedItems() const
374 const QAbstractItemView
* view
= itemView();
376 // Our view has a selection, we will map them back to the DolphinModel
377 // and then fill the KFileItemList.
378 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
380 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
381 KFileItemList itemList
;
383 const QModelIndexList indexList
= selection
.indexes();
384 foreach (QModelIndex index
, indexList
) {
385 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
386 if (!item
.isNull()) {
387 itemList
.append(item
);
394 KUrl::List
DolphinView::selectedUrls() const
397 const KFileItemList list
= selectedItems();
398 foreach (KFileItem item
, list
) {
399 urls
.append(item
.url());
404 KFileItem
DolphinView::fileItem(const QModelIndex
& index
) const
406 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
407 return m_dolphinModel
->itemForIndex(dolphinModelIndex
);
410 void DolphinView::setContentsPosition(int x
, int y
)
412 QAbstractItemView
* view
= itemView();
414 // the ColumnView takes care itself for the horizontal scrolling
415 if (!isColumnViewActive()) {
416 view
->horizontalScrollBar()->setValue(x
);
418 view
->verticalScrollBar()->setValue(y
);
420 m_loadingDirectory
= false;
423 QPoint
DolphinView::contentsPosition() const
425 const int x
= itemView()->horizontalScrollBar()->value();
426 const int y
= itemView()->verticalScrollBar()->value();
430 void DolphinView::zoomIn()
432 m_controller
->triggerZoomIn();
435 void DolphinView::zoomOut()
437 m_controller
->triggerZoomOut();
440 bool DolphinView::isZoomInPossible() const
442 return m_controller
->isZoomInPossible();
445 bool DolphinView::isZoomOutPossible() const
447 return m_controller
->isZoomOutPossible();
450 void DolphinView::setSorting(Sorting sorting
)
452 if (sorting
!= this->sorting()) {
453 updateSorting(sorting
);
457 DolphinView::Sorting
DolphinView::sorting() const
459 return m_proxyModel
->sorting();
462 void DolphinView::setSortOrder(Qt::SortOrder order
)
464 if (sortOrder() != order
) {
465 updateSortOrder(order
);
469 Qt::SortOrder
DolphinView::sortOrder() const
471 return m_proxyModel
->sortOrder();
474 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
476 const KUrl viewPropsUrl
= viewPropertiesUrl();
477 ViewProperties
props(viewPropsUrl
);
478 props
.setAdditionalInfo(info
);
479 m_fileItemDelegate
->setShowInformation(info
);
481 emit
additionalInfoChanged(info
);
483 if (itemView() != m_detailsView
) {
484 // the details view requires no reloading of the directory, as it maps
485 // the file item delegate info to its columns internally
486 loadDirectory(viewPropsUrl
, true);
490 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
492 return m_fileItemDelegate
->showInformation();
495 void DolphinView::reload()
498 loadDirectory(url(), true);
501 void DolphinView::refresh()
503 const bool oldActivationState
= m_active
;
507 applyViewProperties(m_controller
->url());
510 setActive(oldActivationState
);
513 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
515 if (m_controller
->url() == url
) {
519 m_controller
->setUrl(url
); // emits urlChanged, which we forward
521 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
522 applyViewProperties(rootUrl
);
523 loadDirectory(rootUrl
);
524 if (itemView() == m_columnView
) {
525 m_columnView
->setRootUrl(rootUrl
);
526 m_columnView
->showColumn(url
);
529 applyViewProperties(url
);
533 emit
startedPathLoading(url
);
536 void DolphinView::setNameFilter(const QString
& nameFilter
)
538 m_proxyModel
->setFilterRegExp(nameFilter
);
540 if (isColumnViewActive()) {
541 // adjusting the directory lister is not enough in the case of the
542 // column view, as each column has its own directory lister internally...
543 m_columnView
->setNameFilter(nameFilter
);
547 void DolphinView::calculateItemCount(int& fileCount
, int& folderCount
)
549 foreach (KFileItem item
, m_dirLister
->items()) {
558 void DolphinView::setUrl(const KUrl
& url
)
560 updateView(url
, KUrl());
563 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
565 QWidget::mouseReleaseEvent(event
);
568 void DolphinView::activate()
573 void DolphinView::triggerItem(const KFileItem
& item
)
575 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
576 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
577 // items are selected by the user, hence don't trigger the
578 // item specified by 'index'
586 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
589 void DolphinView::generatePreviews(const KFileItemList
& items
)
591 if (m_controller
->dolphinView()->showPreview()) {
592 if (m_previewJob
!= 0) {
593 m_previewJob
->kill();
597 m_previewJob
= KIO::filePreview(items
, 128);
598 connect(m_previewJob
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
599 this, SLOT(replaceIcon(const KFileItem
&, const QPixmap
&)));
600 connect(m_previewJob
, SIGNAL(finished(KJob
*)),
601 this, SLOT(slotPreviewJobFinished(KJob
*)));
605 void DolphinView::replaceIcon(const KFileItem
& item
, const QPixmap
& pixmap
)
607 Q_ASSERT(!item
.isNull());
608 if (!m_showPreview
|| (item
.url().directory() != m_dirLister
->url().path())) {
609 // the preview has been deactivated in the meanwhile or the preview
610 // job is still working on items of an older URL, hence
611 // the item is not part of the directory model anymore
615 const QModelIndex idx
= m_dolphinModel
->indexForItem(item
);
616 if (idx
.isValid() && (idx
.column() == 0)) {
617 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
618 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
619 KIconEffect iconEffect
;
620 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
621 m_dolphinModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
623 m_dolphinModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
628 void DolphinView::emitSelectionChangedSignal()
630 emit
selectionChanged(DolphinView::selectedItems());
633 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
635 if (!url
.isValid()) {
636 const QString
location(url
.pathOrUrl());
637 if (location
.isEmpty()) {
638 emit
errorMessage(i18nc("@info:status", "The location is empty."));
640 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
645 m_cutItemsCache
.clear();
646 m_loadingDirectory
= true;
649 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
651 if (isColumnViewActive()) {
652 // adjusting the directory lister is not enough in the case of the
653 // column view, as each column has its own directory lister internally...
655 m_columnView
->reload();
657 m_columnView
->showColumn(url
);
662 KUrl
DolphinView::viewPropertiesUrl() const
664 if (isColumnViewActive()) {
665 return m_dirLister
->url();
671 void DolphinView::applyViewProperties(const KUrl
& url
)
673 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
674 // The column view is active, hence don't apply the view properties
675 // of sub directories (represented by columns) to the view. The
676 // view always represents the properties of the first column.
680 const ViewProperties
props(url
);
682 const Mode mode
= props
.viewMode();
683 if (m_mode
!= mode
) {
688 if (itemView() == 0) {
691 Q_ASSERT(itemView() != 0);
692 Q_ASSERT(m_fileItemDelegate
!= 0);
694 const bool showHiddenFiles
= props
.showHiddenFiles();
695 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
696 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
697 emit
showHiddenFilesChanged();
700 m_storedCategorizedSorting
= props
.categorizedSorting();
701 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
702 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
703 m_proxyModel
->setCategorizedModel(categorized
);
704 emit
categorizedSortingChanged();
707 const DolphinView::Sorting sorting
= props
.sorting();
708 if (sorting
!= m_proxyModel
->sorting()) {
709 m_proxyModel
->setSorting(sorting
);
710 emit
sortingChanged(sorting
);
713 const Qt::SortOrder sortOrder
= props
.sortOrder();
714 if (sortOrder
!= m_proxyModel
->sortOrder()) {
715 m_proxyModel
->setSortOrder(sortOrder
);
716 emit
sortOrderChanged(sortOrder
);
719 KFileItemDelegate::InformationList info
= props
.additionalInfo();
720 if (info
!= m_fileItemDelegate
->showInformation()) {
721 m_fileItemDelegate
->setShowInformation(info
);
722 emit
additionalInfoChanged(info
);
725 const bool showPreview
= props
.showPreview();
726 if (showPreview
!= m_showPreview
) {
727 m_showPreview
= showPreview
;
728 emit
showPreviewChanged();
732 void DolphinView::changeSelection(const KFileItemList
& selection
)
735 if (selection
.isEmpty()) {
738 const KUrl
& baseUrl
= url();
740 QItemSelection new_selection
;
741 foreach(const KFileItem
& item
, selection
) {
742 url
= item
.url().upUrl();
743 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
744 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
745 new_selection
.select(index
, index
);
748 itemView()->selectionModel()->select(new_selection
,
749 QItemSelectionModel::ClearAndSelect
750 | QItemSelectionModel::Current
);
753 void DolphinView::openContextMenu(const QPoint
& pos
)
757 const QModelIndex index
= itemView()->indexAt(pos
);
758 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
759 item
= fileItem(index
);
762 emit
requestContextMenu(item
, url());
765 void DolphinView::dropUrls(const KUrl::List
& urls
,
766 const KUrl
& destPath
,
767 const KFileItem
& destItem
)
769 Q_ASSERT(!urls
.isEmpty());
770 const KUrl
& destination
= !destItem
.isNull() && destItem
.isDir() ?
771 destItem
.url() : destPath
;
772 const KUrl sourceDir
= KUrl(urls
.first().directory());
773 if (sourceDir
!= destination
) {
774 dropUrls(urls
, destination
);
778 void DolphinView::dropUrls(const KUrl::List
& urls
,
779 const KUrl
& destination
)
781 DolphinDropController
dropController(this);
782 // forward doingOperation signal up to the mainwindow
783 connect(&dropController
, SIGNAL(doingOperation(KonqFileUndoManager::CommandType
)),
784 this, SIGNAL(doingOperation(KonqFileUndoManager::CommandType
)));
785 dropController
.dropUrls(urls
, destination
);
788 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
790 ViewProperties
props(viewPropertiesUrl());
791 props
.setSorting(sorting
);
793 m_proxyModel
->setSorting(sorting
);
795 emit
sortingChanged(sorting
);
798 void DolphinView::updateSortOrder(Qt::SortOrder order
)
800 ViewProperties
props(viewPropertiesUrl());
801 props
.setSortOrder(order
);
803 m_proxyModel
->setSortOrder(order
);
805 emit
sortOrderChanged(order
);
808 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
810 ViewProperties
props(viewPropertiesUrl());
811 props
.setAdditionalInfo(info
);
814 m_fileItemDelegate
->setShowInformation(info
);
816 emit
additionalInfoChanged(info
);
820 void DolphinView::emitContentsMoved()
822 // only emit the contents moved signal if:
823 // - no directory loading is ongoing (this would reset the contents position
825 // - if the Column View is active: the column view does an automatic
826 // positioning during the loading operation, which must be remembered
827 if (!m_loadingDirectory
|| isColumnViewActive()) {
828 const QPoint
pos(contentsPosition());
829 emit
contentsMoved(pos
.x(), pos
.y());
833 void DolphinView::updateCutItems()
835 // restore the icons of all previously selected items to the
837 QList
<CutItem
>::const_iterator it
= m_cutItemsCache
.begin();
838 QList
<CutItem
>::const_iterator end
= m_cutItemsCache
.end();
840 const QModelIndex index
= m_dolphinModel
->indexForUrl((*it
).url
);
841 if (index
.isValid()) {
842 m_dolphinModel
->setData(index
, QIcon((*it
).pixmap
), Qt::DecorationRole
);
846 m_cutItemsCache
.clear();
848 // ... and apply an item effect to all currently cut items
849 applyCutItemEffect();
852 void DolphinView::showHoverInformation(const KFileItem
& item
)
854 if (hasSelection() || !m_active
) {
858 emit
requestItemInfo(item
);
861 void DolphinView::clearHoverInformation()
864 emit
requestItemInfo(KFileItem());
869 void DolphinView::createView()
872 Q_ASSERT(m_iconsView
== 0);
873 Q_ASSERT(m_detailsView
== 0);
874 Q_ASSERT(m_columnView
== 0);
876 QAbstractItemView
* view
= 0;
879 m_iconsView
= new DolphinIconsView(this, m_controller
);
885 m_detailsView
= new DolphinDetailsView(this, m_controller
);
886 view
= m_detailsView
;
890 m_columnView
= new DolphinColumnView(this, m_controller
);
897 m_fileItemDelegate
= new KFileItemDelegate(view
);
898 view
->setItemDelegate(m_fileItemDelegate
);
900 view
->setModel(m_proxyModel
);
901 if (m_selectionModel
!= 0) {
902 view
->setSelectionModel(m_selectionModel
);
904 m_selectionModel
= view
->selectionModel();
907 // reparent the selection model, as it should not be deleted
908 // when deleting the model
909 m_selectionModel
->setParent(this);
911 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
913 new KMimeTypeResolver(view
, m_dolphinModel
);
914 m_topLayout
->insertWidget(1, view
);
916 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
917 this, SLOT(emitSelectionChangedSignal()));
918 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
919 this, SLOT(emitContentsMoved()));
920 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
921 this, SLOT(emitContentsMoved()));
924 void DolphinView::deleteView()
926 QAbstractItemView
* view
= itemView();
928 m_topLayout
->removeWidget(view
);
935 m_fileItemDelegate
= 0;
939 QAbstractItemView
* DolphinView::itemView() const
941 if (m_detailsView
!= 0) {
942 return m_detailsView
;
943 } else if (m_columnView
!= 0) {
950 bool DolphinView::isCutItem(const KFileItem
& item
) const
952 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
953 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
955 const KUrl
& itemUrl
= item
.url();
956 KUrl::List::const_iterator it
= cutUrls
.begin();
957 const KUrl::List::const_iterator end
= cutUrls
.end();
959 if (*it
== itemUrl
) {
968 void DolphinView::applyCutItemEffect()
970 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
971 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
975 KFileItemList
items(m_dirLister
->items());
976 KFileItemList::const_iterator it
= items
.begin();
977 const KFileItemList::const_iterator end
= items
.end();
979 const KFileItem item
= *it
;
980 if (isCutItem(item
)) {
981 const QModelIndex index
= m_dolphinModel
->indexForItem(item
);
982 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
983 if (value
.type() == QVariant::Icon
) {
984 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
985 QPixmap pixmap
= icon
.pixmap(128, 128);
987 // remember current pixmap for the item to be able
988 // to restore it when other items get cut
990 cutItem
.url
= item
.url();
991 cutItem
.pixmap
= pixmap
;
992 m_cutItemsCache
.append(cutItem
);
994 // apply icon effect to the cut item
995 KIconEffect iconEffect
;
996 pixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
997 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
1004 KToggleAction
* DolphinView::iconsModeAction(KActionCollection
* actionCollection
)
1006 KToggleAction
* iconsView
= actionCollection
->add
<KToggleAction
>("icons");
1007 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
1008 iconsView
->setShortcut(Qt::CTRL
| Qt::Key_1
);
1009 iconsView
->setIcon(KIcon("view-list-icons"));
1010 iconsView
->setData(QVariant::fromValue(IconsView
));
1014 KToggleAction
* DolphinView::detailsModeAction(KActionCollection
* actionCollection
)
1016 KToggleAction
* detailsView
= actionCollection
->add
<KToggleAction
>("details");
1017 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
1018 detailsView
->setShortcut(Qt::CTRL
| Qt::Key_2
);
1019 detailsView
->setIcon(KIcon("view-list-details"));
1020 detailsView
->setData(QVariant::fromValue(DetailsView
));
1024 KToggleAction
* DolphinView::columnsModeAction(KActionCollection
* actionCollection
)
1026 KToggleAction
* columnView
= actionCollection
->add
<KToggleAction
>("columns");
1027 columnView
->setText(i18nc("@action:inmenu View Mode", "Columns"));
1028 columnView
->setShortcut(Qt::CTRL
| Qt::Key_3
);
1029 columnView
->setIcon(KIcon("view-file-columns"));
1030 columnView
->setData(QVariant::fromValue(ColumnView
));
1034 QString
DolphinView::currentViewModeActionName() const
1037 case DolphinView::IconsView
:
1039 case DolphinView::DetailsView
:
1041 case DolphinView::ColumnView
:
1044 return QString(); // can't happen
1047 void DolphinView::renameSelectedItems()
1049 const KFileItemList items
= selectedItems();
1050 if (items
.count() > 1) {
1051 // More than one item has been selected for renaming. Open
1052 // a rename dialog and rename all items afterwards.
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 // TODO: check how this can be integrated into KonqFileUndoManager/KonqOperations
1063 // as one operation instead of n rename operations like it is done now...
1064 Q_ASSERT(newName
.contains('#'));
1066 // iterate through all selected items and rename them...
1067 const int replaceIndex
= newName
.indexOf('#');
1068 Q_ASSERT(replaceIndex
>= 0);
1071 KFileItemList::const_iterator it
= items
.begin();
1072 const KFileItemList::const_iterator end
= items
.end();
1074 const KUrl
& oldUrl
= (*it
).url();
1076 number
.setNum(index
++);
1078 QString
name(newName
);
1079 name
.replace(replaceIndex
, 1, number
);
1081 if (oldUrl
.fileName() != name
) {
1082 KUrl newUrl
= oldUrl
;
1083 newUrl
.setFileName(name
);
1084 KonqOperations::rename(this, oldUrl
, newUrl
);
1085 emit
doingOperation(KonqFileUndoManager::RENAME
);
1091 // Only one item has been selected for renaming. Use the custom
1092 // renaming mechanism from the views.
1093 Q_ASSERT(items
.count() == 1);
1095 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
1096 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
1097 // is a benefit for the user at all -> let's wait for some input first...
1098 RenameDialog
dialog(this, items
);
1099 if (dialog
.exec() == QDialog::Rejected
) {
1103 const QString
& newName
= dialog
.newName();
1104 if (newName
.isEmpty()) {
1105 emit
errorMessage(dialog
.errorString());
1107 const KUrl
& oldUrl
= items
.first().url();
1108 KUrl newUrl
= oldUrl
;
1109 newUrl
.setFileName(newName
);
1110 KonqOperations::rename(this, oldUrl
, newUrl
);
1111 emit
doingOperation(KonqFileUndoManager::RENAME
);
1116 void DolphinView::trashSelectedItems()
1118 emit
doingOperation(KonqFileUndoManager::TRASH
);
1119 KonqOperations::del(this, KonqOperations::TRASH
, selectedUrls());
1122 void DolphinView::deleteSelectedItems()
1124 const KUrl::List list
= selectedUrls();
1125 const bool del
= KonqOperations::askDeleteConfirmation(list
,
1126 KonqOperations::DEL
,
1127 KonqOperations::DEFAULT_CONFIRMATION
,
1131 KIO::Job
* job
= KIO::del(list
);
1132 connect(job
, SIGNAL(result(KJob
*)),
1133 this, SLOT(slotDeleteFileFinished(KJob
*)));
1137 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1139 if (job
->error() == 0) {
1140 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1142 emit
errorMessage(job
->errorString());
1146 void DolphinView::slotPreviewJobFinished(KJob
* job
)
1148 Q_ASSERT(job
== m_previewJob
);
1152 void DolphinView::createDir()
1154 KonqOperations::newDir(this, url());
1157 void DolphinView::cutSelectedItems()
1159 QMimeData
* mimeData
= new QMimeData();
1160 const KUrl::List kdeUrls
= selectedUrls();
1161 const KUrl::List mostLocalUrls
;
1162 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, true);
1163 QApplication::clipboard()->setMimeData(mimeData
);
1166 void DolphinView::copySelectedItems()
1168 QMimeData
* mimeData
= new QMimeData();
1169 const KUrl::List kdeUrls
= selectedUrls();
1170 const KUrl::List mostLocalUrls
;
1171 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, false);
1172 QApplication::clipboard()->setMimeData(mimeData
);
1175 void DolphinView::paste()
1177 QClipboard
* clipboard
= QApplication::clipboard();
1178 const QMimeData
* mimeData
= clipboard
->mimeData();
1180 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1182 // per default the pasting is done into the current Url of the view
1183 KUrl
destUrl(url());
1185 // check whether the pasting should be done into a selected directory
1186 const KUrl::List selectedUrls
= this->selectedUrls();
1187 if (selectedUrls
.count() == 1) {
1188 const KFileItem
fileItem(S_IFDIR
,
1190 selectedUrls
.first(),
1192 if (fileItem
.isDir()) {
1193 // only one item is selected which is a directory, hence paste
1194 // into this directory
1195 destUrl
= selectedUrls
.first();
1199 if (KonqMimeData::decodeIsCutSelection(mimeData
)) {
1200 KonqOperations::copy(this, KonqOperations::MOVE
, sourceUrls
, destUrl
);
1201 emit
doingOperation(KonqFileUndoManager::MOVE
);
1204 KonqOperations::copy(this, KonqOperations::COPY
, sourceUrls
, destUrl
);
1205 emit
doingOperation(KonqFileUndoManager::COPY
);
1209 QPair
<bool, QString
> DolphinView::pasteInfo() const
1211 QPair
<bool, QString
> ret
;
1212 QClipboard
* clipboard
= QApplication::clipboard();
1213 const QMimeData
* mimeData
= clipboard
->mimeData();
1215 KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1216 if (!urls
.isEmpty()) {
1218 ret
.second
= i18ncp("@action:inmenu", "Paste One File", "Paste %1 Files", urls
.count());
1221 ret
.second
= i18nc("@action:inmenu", "Paste");
1225 const KFileItemList items
= selectedItems();
1226 const uint count
= items
.count();
1228 // pasting should not be allowed when more than one file
1231 } else if (count
== 1) {
1232 // Only one file is selected. Pasting is only allowed if this
1233 // file is a directory.
1234 ret
.first
= items
.first().isDir();
1240 KAction
* DolphinView::createRenameAction(KActionCollection
* collection
)
1242 KAction
* rename
= collection
->addAction("rename");
1243 rename
->setText(i18nc("@action:inmenu File", "Rename..."));
1244 rename
->setShortcut(Qt::Key_F2
);
1248 KAction
* DolphinView::createMoveToTrashAction(KActionCollection
* collection
)
1250 KAction
* moveToTrash
= collection
->addAction("move_to_trash");
1251 moveToTrash
->setText(i18nc("@action:inmenu File", "Move to Trash"));
1252 moveToTrash
->setIcon(KIcon("user-trash"));
1253 moveToTrash
->setShortcut(QKeySequence::Delete
);
1257 KAction
* DolphinView::createDeleteAction(KActionCollection
* collection
)
1259 KAction
* deleteAction
= collection
->addAction("delete");
1260 deleteAction
->setIcon(KIcon("edit-delete"));
1261 deleteAction
->setText(i18nc("@action:inmenu File", "Delete"));
1262 deleteAction
->setShortcut(Qt::SHIFT
| Qt::Key_Delete
);
1263 return deleteAction
;
1266 #include "dolphinview.moc"