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();
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();
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();
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(); // will call updateAdditionalInfoActions just below
816 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
818 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
819 (m_mode
== DolphinView::IconsView
);
821 QAction
* showSizeInfo
= collection
->action("show_size_info");
822 QAction
* showDateInfo
= collection
->action("show_date_info");
823 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
824 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
825 QAction
* showGroupInfo
= collection
->action("show_group_info");
826 QAction
* showMimeInfo
= collection
->action("show_mime_info");
828 showSizeInfo
->setChecked(false);
829 showDateInfo
->setChecked(false);
830 showPermissionsInfo
->setChecked(false);
831 showOwnerInfo
->setChecked(false);
832 showGroupInfo
->setChecked(false);
833 showMimeInfo
->setChecked(false);
835 showSizeInfo
->setEnabled(enable
);
836 showDateInfo
->setEnabled(enable
);
837 showPermissionsInfo
->setEnabled(enable
);
838 showOwnerInfo
->setEnabled(enable
);
839 showGroupInfo
->setEnabled(enable
);
840 showMimeInfo
->setEnabled(enable
);
842 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
844 case KFileItemDelegate::Size
:
845 showSizeInfo
->setChecked(true);
847 case KFileItemDelegate::ModificationTime
:
848 showDateInfo
->setChecked(true);
850 case KFileItemDelegate::Permissions
:
851 showPermissionsInfo
->setChecked(true);
853 case KFileItemDelegate::Owner
:
854 showOwnerInfo
->setChecked(true);
856 case KFileItemDelegate::OwnerAndGroup
:
857 showGroupInfo
->setChecked(true);
859 case KFileItemDelegate::FriendlyMimeType
:
860 showMimeInfo
->setChecked(true);
868 void DolphinView::toggleAdditionalInfo(QAction
* action
)
870 const KFileItemDelegate::Information info
=
871 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
873 KFileItemDelegate::InformationList list
= additionalInfo();
875 const bool show
= action
->isChecked();
877 const int index
= list
.indexOf(info
);
878 const bool containsInfo
= (index
>= 0);
879 if (show
&& !containsInfo
) {
881 setAdditionalInfo(list
);
882 } else if (!show
&& containsInfo
) {
883 list
.removeAt(index
);
884 setAdditionalInfo(list
);
885 Q_ASSERT(list
.indexOf(info
) < 0);
889 void DolphinView::emitContentsMoved()
891 // only emit the contents moved signal if:
892 // - no directory loading is ongoing (this would reset the contents position
894 // - if the Column View is active: the column view does an automatic
895 // positioning during the loading operation, which must be remembered
896 if (!m_loadingDirectory
|| isColumnViewActive()) {
897 const QPoint
pos(contentsPosition());
898 emit
contentsMoved(pos
.x(), pos
.y());
902 void DolphinView::updateCutItems()
904 // restore the icons of all previously selected items to the
906 QList
<CutItem
>::const_iterator it
= m_cutItemsCache
.begin();
907 QList
<CutItem
>::const_iterator end
= m_cutItemsCache
.end();
909 const QModelIndex index
= m_dolphinModel
->indexForUrl((*it
).url
);
910 if (index
.isValid()) {
911 m_dolphinModel
->setData(index
, QIcon((*it
).pixmap
), Qt::DecorationRole
);
915 m_cutItemsCache
.clear();
917 // ... and apply an item effect to all currently cut items
918 applyCutItemEffect();
921 void DolphinView::showHoverInformation(const KFileItem
& item
)
923 if (hasSelection() || !m_active
) {
927 emit
requestItemInfo(item
);
930 void DolphinView::clearHoverInformation()
933 emit
requestItemInfo(KFileItem());
938 void DolphinView::createView()
941 Q_ASSERT(m_iconsView
== 0);
942 Q_ASSERT(m_detailsView
== 0);
943 Q_ASSERT(m_columnView
== 0);
945 QAbstractItemView
* view
= 0;
948 m_iconsView
= new DolphinIconsView(this, m_controller
);
954 m_detailsView
= new DolphinDetailsView(this, m_controller
);
955 view
= m_detailsView
;
959 m_columnView
= new DolphinColumnView(this, m_controller
);
966 m_fileItemDelegate
= new KFileItemDelegate(view
);
967 view
->setItemDelegate(m_fileItemDelegate
);
969 view
->setModel(m_proxyModel
);
970 if (m_selectionModel
!= 0) {
971 view
->setSelectionModel(m_selectionModel
);
973 m_selectionModel
= view
->selectionModel();
976 // reparent the selection model, as it should not be deleted
977 // when deleting the model
978 m_selectionModel
->setParent(this);
980 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
982 new KMimeTypeResolver(view
, m_dolphinModel
);
983 m_topLayout
->insertWidget(1, view
);
985 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
986 this, SLOT(emitSelectionChangedSignal()));
987 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
988 this, SLOT(emitContentsMoved()));
989 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
990 this, SLOT(emitContentsMoved()));
993 void DolphinView::deleteView()
995 QAbstractItemView
* view
= itemView();
997 m_topLayout
->removeWidget(view
);
1004 m_fileItemDelegate
= 0;
1008 QAbstractItemView
* DolphinView::itemView() const
1010 if (m_detailsView
!= 0) {
1011 return m_detailsView
;
1012 } else if (m_columnView
!= 0) {
1013 return m_columnView
;
1019 bool DolphinView::isCutItem(const KFileItem
& item
) const
1021 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1022 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1024 const KUrl
& itemUrl
= item
.url();
1025 KUrl::List::const_iterator it
= cutUrls
.begin();
1026 const KUrl::List::const_iterator end
= cutUrls
.end();
1028 if (*it
== itemUrl
) {
1037 void DolphinView::applyCutItemEffect()
1039 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1040 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
1044 KFileItemList
items(m_dirLister
->items());
1045 KFileItemList::const_iterator it
= items
.begin();
1046 const KFileItemList::const_iterator end
= items
.end();
1048 const KFileItem item
= *it
;
1049 if (isCutItem(item
)) {
1050 const QModelIndex index
= m_dolphinModel
->indexForItem(item
);
1051 const QVariant value
= m_dolphinModel
->data(index
, Qt::DecorationRole
);
1052 if (value
.type() == QVariant::Icon
) {
1053 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
1054 QPixmap pixmap
= icon
.pixmap(128, 128);
1056 // remember current pixmap for the item to be able
1057 // to restore it when other items get cut
1059 cutItem
.url
= item
.url();
1060 cutItem
.pixmap
= pixmap
;
1061 m_cutItemsCache
.append(cutItem
);
1063 // apply icon effect to the cut item
1064 KIconEffect iconEffect
;
1065 pixmap
= iconEffect
.apply(pixmap
, KIconLoader::Desktop
, KIconLoader::DisabledState
);
1066 m_dolphinModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
1073 KToggleAction
* DolphinView::iconsModeAction(KActionCollection
* actionCollection
)
1075 KToggleAction
* iconsView
= actionCollection
->add
<KToggleAction
>("icons");
1076 iconsView
->setText(i18nc("@action:inmenu View Mode", "Icons"));
1077 iconsView
->setShortcut(Qt::CTRL
| Qt::Key_1
);
1078 iconsView
->setIcon(KIcon("view-list-icons"));
1079 iconsView
->setData(QVariant::fromValue(IconsView
));
1083 KToggleAction
* DolphinView::detailsModeAction(KActionCollection
* actionCollection
)
1085 KToggleAction
* detailsView
= actionCollection
->add
<KToggleAction
>("details");
1086 detailsView
->setText(i18nc("@action:inmenu View Mode", "Details"));
1087 detailsView
->setShortcut(Qt::CTRL
| Qt::Key_2
);
1088 detailsView
->setIcon(KIcon("view-list-details"));
1089 detailsView
->setData(QVariant::fromValue(DetailsView
));
1093 KToggleAction
* DolphinView::columnsModeAction(KActionCollection
* actionCollection
)
1095 KToggleAction
* columnView
= actionCollection
->add
<KToggleAction
>("columns");
1096 columnView
->setText(i18nc("@action:inmenu View Mode", "Columns"));
1097 columnView
->setShortcut(Qt::CTRL
| Qt::Key_3
);
1098 columnView
->setIcon(KIcon("view-file-columns"));
1099 columnView
->setData(QVariant::fromValue(ColumnView
));
1103 QString
DolphinView::currentViewModeActionName() const
1106 case DolphinView::IconsView
:
1108 case DolphinView::DetailsView
:
1110 case DolphinView::ColumnView
:
1113 return QString(); // can't happen
1116 void DolphinView::renameSelectedItems()
1118 const KFileItemList items
= selectedItems();
1119 if (items
.count() > 1) {
1120 // More than one item has been selected for renaming. Open
1121 // a rename dialog and rename all items afterwards.
1122 RenameDialog
dialog(this, items
);
1123 if (dialog
.exec() == QDialog::Rejected
) {
1127 const QString newName
= dialog
.newName();
1128 if (newName
.isEmpty()) {
1129 emit
errorMessage(dialog
.errorString());
1131 // TODO: check how this can be integrated into KonqFileUndoManager/KonqOperations
1132 // as one operation instead of n rename operations like it is done now...
1133 Q_ASSERT(newName
.contains('#'));
1135 // iterate through all selected items and rename them...
1136 const int replaceIndex
= newName
.indexOf('#');
1137 Q_ASSERT(replaceIndex
>= 0);
1140 KFileItemList::const_iterator it
= items
.begin();
1141 const KFileItemList::const_iterator end
= items
.end();
1143 const KUrl
& oldUrl
= (*it
).url();
1145 number
.setNum(index
++);
1147 QString
name(newName
);
1148 name
.replace(replaceIndex
, 1, number
);
1150 if (oldUrl
.fileName() != name
) {
1151 KUrl newUrl
= oldUrl
;
1152 newUrl
.setFileName(name
);
1153 KonqOperations::rename(this, oldUrl
, newUrl
);
1154 emit
doingOperation(KonqFileUndoManager::RENAME
);
1160 // Only one item has been selected for renaming. Use the custom
1161 // renaming mechanism from the views.
1162 Q_ASSERT(items
.count() == 1);
1164 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
1165 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
1166 // is a benefit for the user at all -> let's wait for some input first...
1167 RenameDialog
dialog(this, items
);
1168 if (dialog
.exec() == QDialog::Rejected
) {
1172 const QString
& newName
= dialog
.newName();
1173 if (newName
.isEmpty()) {
1174 emit
errorMessage(dialog
.errorString());
1176 const KUrl
& oldUrl
= items
.first().url();
1177 KUrl newUrl
= oldUrl
;
1178 newUrl
.setFileName(newName
);
1179 KonqOperations::rename(this, oldUrl
, newUrl
);
1180 emit
doingOperation(KonqFileUndoManager::RENAME
);
1185 void DolphinView::trashSelectedItems()
1187 emit
doingOperation(KonqFileUndoManager::TRASH
);
1188 KonqOperations::del(this, KonqOperations::TRASH
, selectedUrls());
1191 void DolphinView::deleteSelectedItems()
1193 const KUrl::List list
= selectedUrls();
1194 const bool del
= KonqOperations::askDeleteConfirmation(list
,
1195 KonqOperations::DEL
,
1196 KonqOperations::DEFAULT_CONFIRMATION
,
1200 KIO::Job
* job
= KIO::del(list
);
1201 connect(job
, SIGNAL(result(KJob
*)),
1202 this, SLOT(slotDeleteFileFinished(KJob
*)));
1206 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1208 if (job
->error() == 0) {
1209 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1211 emit
errorMessage(job
->errorString());
1215 void DolphinView::slotPreviewJobFinished(KJob
* job
)
1217 Q_ASSERT(job
== m_previewJob
);
1221 void DolphinView::cutSelectedItems()
1223 QMimeData
* mimeData
= new QMimeData();
1224 const KUrl::List kdeUrls
= selectedUrls();
1225 const KUrl::List mostLocalUrls
;
1226 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, true);
1227 QApplication::clipboard()->setMimeData(mimeData
);
1230 void DolphinView::copySelectedItems()
1232 QMimeData
* mimeData
= new QMimeData();
1233 const KUrl::List kdeUrls
= selectedUrls();
1234 const KUrl::List mostLocalUrls
;
1235 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, false);
1236 QApplication::clipboard()->setMimeData(mimeData
);
1239 void DolphinView::paste()
1241 QClipboard
* clipboard
= QApplication::clipboard();
1242 const QMimeData
* mimeData
= clipboard
->mimeData();
1244 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1246 // per default the pasting is done into the current Url of the view
1247 KUrl
destUrl(url());
1249 // check whether the pasting should be done into a selected directory
1250 const KUrl::List selectedUrls
= this->selectedUrls();
1251 if (selectedUrls
.count() == 1) {
1252 const KFileItem
fileItem(S_IFDIR
,
1254 selectedUrls
.first(),
1256 if (fileItem
.isDir()) {
1257 // only one item is selected which is a directory, hence paste
1258 // into this directory
1259 destUrl
= selectedUrls
.first();
1263 if (KonqMimeData::decodeIsCutSelection(mimeData
)) {
1264 KonqOperations::copy(this, KonqOperations::MOVE
, sourceUrls
, destUrl
);
1265 emit
doingOperation(KonqFileUndoManager::MOVE
);
1268 KonqOperations::copy(this, KonqOperations::COPY
, sourceUrls
, destUrl
);
1269 emit
doingOperation(KonqFileUndoManager::COPY
);
1273 QPair
<bool, QString
> DolphinView::pasteInfo() const
1275 QPair
<bool, QString
> ret
;
1276 QClipboard
* clipboard
= QApplication::clipboard();
1277 const QMimeData
* mimeData
= clipboard
->mimeData();
1279 KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1280 if (!urls
.isEmpty()) {
1282 ret
.second
= i18ncp("@action:inmenu", "Paste One File", "Paste %1 Files", urls
.count());
1285 ret
.second
= i18nc("@action:inmenu", "Paste");
1289 const KFileItemList items
= selectedItems();
1290 const uint count
= items
.count();
1292 // pasting should not be allowed when more than one file
1295 } else if (count
== 1) {
1296 // Only one file is selected. Pasting is only allowed if this
1297 // file is a directory.
1298 ret
.first
= items
.first().isDir();
1304 KAction
* DolphinView::createRenameAction(KActionCollection
* collection
)
1306 KAction
* rename
= collection
->addAction("rename");
1307 rename
->setText(i18nc("@action:inmenu File", "Rename..."));
1308 rename
->setShortcut(Qt::Key_F2
);
1312 KAction
* DolphinView::createMoveToTrashAction(KActionCollection
* collection
)
1314 KAction
* moveToTrash
= collection
->addAction("move_to_trash");
1315 moveToTrash
->setText(i18nc("@action:inmenu File", "Move to Trash"));
1316 moveToTrash
->setIcon(KIcon("user-trash"));
1317 moveToTrash
->setShortcut(QKeySequence::Delete
);
1321 KAction
* DolphinView::createDeleteAction(KActionCollection
* collection
)
1323 KAction
* deleteAction
= collection
->addAction("delete");
1324 deleteAction
->setIcon(KIcon("edit-delete"));
1325 deleteAction
->setText(i18nc("@action:inmenu File", "Delete"));
1326 deleteAction
->setShortcut(Qt::SHIFT
| Qt::Key_Delete
);
1327 return deleteAction
;
1330 KAction
* DolphinView::createNewDirAction(KActionCollection
* collection
)
1332 // This action doesn't appear in the GUI, it's for the shortcut only.
1333 // KNewMenu takes care of the GUI stuff.
1334 KAction
* newDirAction
= collection
->addAction("create_dir");
1335 newDirAction
->setText(i18n("Create Folder..."));
1336 newDirAction
->setShortcut(Qt::Key_F10
);
1337 return newDirAction
;
1340 KAction
* DolphinView::createSortDescendingAction(KActionCollection
* collection
)
1342 KToggleAction
* sortDescending
= collection
->add
<KToggleAction
>("descending");
1343 sortDescending
->setText(i18nc("@action:inmenu Sort", "Descending"));
1344 return sortDescending
;
1347 KAction
* DolphinView::createShowPreviewAction(KActionCollection
* collection
)
1349 KToggleAction
* showPreview
= collection
->add
<KToggleAction
>("show_preview");
1350 showPreview
->setText(i18nc("@action:intoolbar", "Preview"));
1351 showPreview
->setIcon(KIcon("view-preview"));
1355 KAction
* DolphinView::createShowInGroupsAction(KActionCollection
* collection
)
1357 KToggleAction
* showInGroups
= collection
->add
<KToggleAction
>("show_in_groups");
1358 showInGroups
->setText(i18nc("@action:inmenu View", "Show in Groups"));
1359 return showInGroups
;
1362 KAction
* DolphinView::createShowHiddenFilesAction(KActionCollection
* collection
)
1364 KToggleAction
* showHiddenFiles
= collection
->add
<KToggleAction
>("show_hidden_files");
1365 showHiddenFiles
->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
1366 showHiddenFiles
->setShortcut(Qt::ALT
| Qt::Key_Period
);
1367 return showHiddenFiles
;
1370 QActionGroup
* DolphinView::createAdditionalInformationActionGroup(KActionCollection
* collection
)
1372 QActionGroup
* showInformationGroup
= new QActionGroup(collection
);
1373 showInformationGroup
->setExclusive(false);
1375 KToggleAction
* showSizeInfo
= collection
->add
<KToggleAction
>("show_size_info");
1376 showSizeInfo
->setText(i18nc("@action:inmenu Additional information", "Size"));
1377 showSizeInfo
->setData(KFileItemDelegate::Size
);
1378 showSizeInfo
->setActionGroup(showInformationGroup
);
1380 KToggleAction
* showDateInfo
= collection
->add
<KToggleAction
>("show_date_info");
1381 showDateInfo
->setText(i18nc("@action:inmenu Additional information", "Date"));
1382 showDateInfo
->setData(KFileItemDelegate::ModificationTime
);
1383 showDateInfo
->setActionGroup(showInformationGroup
);
1385 KToggleAction
* showPermissionsInfo
= collection
->add
<KToggleAction
>("show_permissions_info");
1386 showPermissionsInfo
->setText(i18nc("@action:inmenu Additional information", "Permissions"));
1387 showPermissionsInfo
->setData(KFileItemDelegate::Permissions
);
1388 showPermissionsInfo
->setActionGroup(showInformationGroup
);
1390 KToggleAction
* showOwnerInfo
= collection
->add
<KToggleAction
>("show_owner_info");
1391 showOwnerInfo
->setText(i18nc("@action:inmenu Additional information", "Owner"));
1392 showOwnerInfo
->setData(KFileItemDelegate::Owner
);
1393 showOwnerInfo
->setActionGroup(showInformationGroup
);
1395 KToggleAction
* showGroupInfo
= collection
->add
<KToggleAction
>("show_group_info");
1396 showGroupInfo
->setText(i18nc("@action:inmenu Additional information", "Group"));
1397 showGroupInfo
->setData(KFileItemDelegate::OwnerAndGroup
);
1398 showGroupInfo
->setActionGroup(showInformationGroup
);
1400 KToggleAction
* showMimeInfo
= collection
->add
<KToggleAction
>("show_mime_info");
1401 showMimeInfo
->setText(i18nc("@action:inmenu Additional information", "Type"));
1402 showMimeInfo
->setData(KFileItemDelegate::FriendlyMimeType
);
1403 showMimeInfo
->setActionGroup(showInformationGroup
);
1405 return showInformationGroup
;
1408 #include "dolphinview.moc"