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 <QItemSelectionModel>
27 #include <QMouseEvent>
28 #include <QVBoxLayout>
32 #include <kdirmodel.h>
33 #include <kfileitemdelegate.h>
34 #include <kfileplacesmodel.h>
36 #include <kiconeffect.h>
37 #include <kio/netaccess.h>
38 #include <kio/renamedialog.h>
39 #include <kio/previewjob.h>
40 #include <kmimetyperesolver.h>
41 #include <konqmimedata.h>
42 #include <konq_operations.h>
45 #include "dolphincolumnview.h"
46 #include "dolphincontroller.h"
47 #include "dolphinstatusbar.h"
48 #include "dolphinmainwindow.h"
49 #include "dolphindirlister.h"
50 #include "dolphinsortfilterproxymodel.h"
51 #include "dolphindetailsview.h"
52 #include "dolphiniconsview.h"
53 #include "dolphincontextmenu.h"
54 #include "filterbar.h"
55 #include "renamedialog.h"
56 #include "kurlnavigator.h"
57 #include "viewproperties.h"
58 #include "dolphinsettings.h"
59 #include "dolphin_generalsettings.h"
61 DolphinView::DolphinView(DolphinMainWindow
* mainWindow
,
65 bool showHiddenFiles
) :
67 m_showProgress(false),
68 m_blockContentsMovedSignal(false),
73 m_mainWindow(mainWindow
),
80 m_fileItemDelegate(0),
88 setFocusPolicy(Qt::StrongFocus
);
89 m_topLayout
= new QVBoxLayout(this);
90 m_topLayout
->setSpacing(0);
91 m_topLayout
->setMargin(0);
93 connect(m_mainWindow
, SIGNAL(activeViewChanged()),
94 this, SLOT(updateActivationState()));
96 QClipboard
* clipboard
= QApplication::clipboard();
97 connect(clipboard
, SIGNAL(dataChanged()),
98 this, SLOT(updateCutItems()));
100 m_urlNavigator
= new KUrlNavigator(DolphinSettings::instance().placesModel(), url
, this);
101 m_urlNavigator
->setUrlEditable(DolphinSettings::instance().generalSettings()->editableUrl());
102 m_urlNavigator
->setHomeUrl(DolphinSettings::instance().generalSettings()->homeUrl());
103 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
104 this, SLOT(loadDirectory(const KUrl
&)));
105 connect(m_urlNavigator
, SIGNAL(urlsDropped(const KUrl::List
&, const KUrl
&)),
106 this, SLOT(dropUrls(const KUrl::List
&, const KUrl
&)));
107 connect(m_urlNavigator
, SIGNAL(activated()),
108 this, SLOT(requestActivation()));
109 connect(this, SIGNAL(contentsMoved(int, int)),
110 m_urlNavigator
, SLOT(savePosition(int, int)));
112 m_statusBar
= new DolphinStatusBar(this);
114 m_dirLister
= new DolphinDirLister();
115 m_dirLister
->setAutoUpdate(true);
116 m_dirLister
->setMainWindow(this);
117 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
118 m_dirLister
->setDelayedMimeTypes(true);
120 connect(m_dirLister
, SIGNAL(clear()),
121 this, SLOT(updateStatusBar()));
122 connect(m_dirLister
, SIGNAL(percent(int)),
123 this, SLOT(updateProgress(int)));
124 connect(m_dirLister
, SIGNAL(deleteItem(KFileItem
*)),
125 this, SLOT(updateStatusBar()));
126 connect(m_dirLister
, SIGNAL(completed()),
127 this, SLOT(updateItemCount()));
128 connect(m_dirLister
, SIGNAL(completed()),
129 this, SLOT(updateCutItems()));
130 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
131 this, SLOT(generatePreviews(const KFileItemList
&)));
132 connect(m_dirLister
, SIGNAL(infoMessage(const QString
&)),
133 this, SLOT(showInfoMessage(const QString
&)));
134 connect(m_dirLister
, SIGNAL(errorMessage(const QString
&)),
135 this, SLOT(showErrorMessage(const QString
&)));
137 m_dirModel
= new KDirModel();
138 m_dirModel
->setDirLister(m_dirLister
);
139 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
141 m_proxyModel
= new DolphinSortFilterProxyModel(this);
142 m_proxyModel
->setSourceModel(m_dirModel
);
144 m_controller
= new DolphinController(this);
145 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&)),
146 this, SLOT(openContextMenu(const QPoint
&)));
147 connect(m_controller
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&, QWidget
*)),
148 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&, QWidget
*)));
149 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
150 this, SLOT(updateSorting(DolphinView::Sorting
)));
151 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
152 this, SLOT(updateSortOrder(Qt::SortOrder
)));
153 connect(m_controller
, SIGNAL(itemTriggered(const QModelIndex
&)),
154 this, SLOT(triggerItem(const QModelIndex
&)));
155 connect(m_controller
, SIGNAL(selectionChanged()),
156 this, SLOT(emitSelectionChangedSignal()));
157 connect(m_controller
, SIGNAL(activated()),
158 this, SLOT(requestActivation()));
162 m_iconSize
= K3Icon::SizeMedium
;
164 m_filterBar
= new FilterBar(this);
166 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
167 this, SLOT(changeNameFilter(const QString
&)));
168 connect(m_filterBar
, SIGNAL(closeRequest()),
169 this, SLOT(closeFilterBar()));
171 m_topLayout
->addWidget(m_urlNavigator
);
172 m_topLayout
->addWidget(itemView());
173 m_topLayout
->addWidget(m_filterBar
);
174 m_topLayout
->addWidget(m_statusBar
);
177 DolphinView::~DolphinView()
183 void DolphinView::setUrl(const KUrl
& url
)
185 m_urlNavigator
->setUrl(url
);
186 m_controller
->setUrl(url
);
189 const KUrl
& DolphinView::url() const
191 return m_urlNavigator
->url();
194 bool DolphinView::isActive() const
196 return m_mainWindow
->activeView() == this;
199 void DolphinView::setMode(Mode mode
)
201 if (mode
== m_mode
) {
202 return; // the wished mode is already set
207 ViewProperties
props(m_urlNavigator
->url());
208 props
.setViewMode(m_mode
);
211 startDirLister(m_urlNavigator
->url());
216 DolphinView::Mode
DolphinView::mode() const
221 void DolphinView::setShowPreview(bool show
)
223 ViewProperties
props(m_urlNavigator
->url());
224 props
.setShowPreview(show
);
226 m_controller
->setShowPreview(show
);
228 emit
showPreviewChanged();
232 bool DolphinView::showPreview() const
234 return m_controller
->showPreview();
237 void DolphinView::setShowHiddenFiles(bool show
)
239 if (m_dirLister
->showingDotFiles() == show
) {
243 ViewProperties
props(m_urlNavigator
->url());
244 props
.setShowHiddenFiles(show
);
247 m_dirLister
->setShowingDotFiles(show
);
248 emit
showHiddenFilesChanged();
253 bool DolphinView::showHiddenFiles() const
255 return m_dirLister
->showingDotFiles();
258 void DolphinView::renameSelectedItems()
260 DolphinView
* view
= mainWindow()->activeView();
261 const KUrl::List urls
= selectedUrls();
262 if (urls
.count() > 1) {
263 // More than one item has been selected for renaming. Open
264 // a rename dialog and rename all items afterwards.
265 RenameDialog
dialog(urls
);
266 if (dialog
.exec() == QDialog::Rejected
) {
270 const QString
& newName
= dialog
.newName();
271 if (newName
.isEmpty()) {
272 view
->statusBar()->setMessage(dialog
.errorString(),
273 DolphinStatusBar::Error
);
276 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
277 // as one operation instead of n rename operations like it is done now...
278 Q_ASSERT(newName
.contains('#'));
280 // iterate through all selected items and rename them...
281 const int replaceIndex
= newName
.indexOf('#');
282 Q_ASSERT(replaceIndex
>= 0);
285 KUrl::List::const_iterator it
= urls
.begin();
286 KUrl::List::const_iterator end
= urls
.end();
288 const KUrl
& oldUrl
= *it
;
290 number
.setNum(index
++);
292 QString
name(newName
);
293 name
.replace(replaceIndex
, 1, number
);
295 if (oldUrl
.fileName() != name
) {
296 KUrl newUrl
= oldUrl
;
297 newUrl
.setFileName(name
);
298 m_mainWindow
->rename(oldUrl
, newUrl
);
305 // Only one item has been selected for renaming. Use the custom
306 // renaming mechanism from the views.
307 Q_ASSERT(urls
.count() == 1);
309 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
310 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
311 // is a benefit for the user at all -> let's wait for some input first...
312 RenameDialog
dialog(urls
);
313 if (dialog
.exec() == QDialog::Rejected
) {
317 const QString
& newName
= dialog
.newName();
318 if (newName
.isEmpty()) {
319 view
->statusBar()->setMessage(dialog
.errorString(),
320 DolphinStatusBar::Error
);
323 const KUrl
& oldUrl
= urls
.first();
324 KUrl newUrl
= oldUrl
;
325 newUrl
.setFileName(newName
);
326 m_mainWindow
->rename(oldUrl
, newUrl
);
331 void DolphinView::selectAll()
333 selectAll(QItemSelectionModel::Select
);
336 void DolphinView::invertSelection()
338 selectAll(QItemSelectionModel::Toggle
);
341 DolphinStatusBar
* DolphinView::statusBar() const
346 int DolphinView::contentsX() const
348 return itemView()->horizontalScrollBar()->value();
351 int DolphinView::contentsY() const
353 return itemView()->verticalScrollBar()->value();
356 void DolphinView::emitRequestItemInfo(const KUrl
& url
)
358 emit
requestItemInfo(url
);
361 bool DolphinView::isFilterBarVisible() const
363 return m_filterBar
->isVisible();
366 bool DolphinView::isUrlEditable() const
368 return m_urlNavigator
->isUrlEditable();
371 void DolphinView::zoomIn()
373 m_controller
->triggerZoomIn();
376 void DolphinView::zoomOut()
378 m_controller
->triggerZoomOut();
381 bool DolphinView::isZoomInPossible() const
383 return m_controller
->isZoomInPossible();
386 bool DolphinView::isZoomOutPossible() const
388 return m_controller
->isZoomOutPossible();
391 void DolphinView::setSorting(Sorting sorting
)
393 if (sorting
!= this->sorting()) {
394 updateSorting(sorting
);
398 DolphinView::Sorting
DolphinView::sorting() const
400 return m_proxyModel
->sorting();
403 void DolphinView::setSortOrder(Qt::SortOrder order
)
405 if (sortOrder() != order
) {
406 updateSortOrder(order
);
410 Qt::SortOrder
DolphinView::sortOrder() const
412 return m_proxyModel
->sortOrder();
415 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info
)
417 ViewProperties
props(m_urlNavigator
->url());
418 props
.setAdditionalInfo(info
);
420 m_fileItemDelegate
->setAdditionalInformation(info
);
422 emit
additionalInfoChanged(info
);
426 KFileItemDelegate::AdditionalInformation
DolphinView::additionalInfo() const
428 return m_fileItemDelegate
->additionalInformation();
431 void DolphinView::goBack()
433 m_urlNavigator
->goBack();
436 void DolphinView::goForward()
438 m_urlNavigator
->goForward();
441 void DolphinView::goUp()
443 m_urlNavigator
->goUp();
446 void DolphinView::goHome()
448 m_urlNavigator
->goHome();
451 void DolphinView::setUrlEditable(bool editable
)
453 m_urlNavigator
->setUrlEditable(editable
);
456 bool DolphinView::hasSelection() const
458 return itemView()->selectionModel()->hasSelection();
461 void DolphinView::clearSelection()
463 itemView()->selectionModel()->clear();
466 KFileItemList
DolphinView::selectedItems() const
468 const QAbstractItemView
* view
= itemView();
470 // Our view has a selection, we will map them back to the DirModel
471 // and then fill the KFileItemList.
472 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
474 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
475 KFileItemList itemList
;
477 const QModelIndexList indexList
= selection
.indexes();
478 QModelIndexList::const_iterator end
= indexList
.end();
479 for (QModelIndexList::const_iterator it
= indexList
.begin(); it
!= end
; ++it
) {
480 Q_ASSERT((*it
).isValid());
482 KFileItem
* item
= m_dirModel
->itemForIndex(*it
);
484 itemList
.append(item
);
491 KUrl::List
DolphinView::selectedUrls() const
495 const KFileItemList list
= selectedItems();
496 KFileItemList::const_iterator it
= list
.begin();
497 const KFileItemList::const_iterator end
= list
.end();
499 KFileItem
* item
= *it
;
500 urls
.append(item
->url());
507 KFileItem
* DolphinView::fileItem(const QModelIndex index
) const
509 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
510 return m_dirModel
->itemForIndex(dirModelIndex
);
513 void DolphinView::rename(const KUrl
& source
, const QString
& newName
)
517 if (newName
.isEmpty() || (source
.fileName() == newName
)) {
521 KUrl
dest(source
.upUrl());
522 dest
.addPath(newName
);
524 const bool destExists
= KIO::NetAccess::exists(dest
,
526 mainWindow()->activeView());
528 // the destination already exists, hence ask the user
530 KIO::RenameDialog
renameDialog(this,
531 i18n("File Already Exists"),
535 switch (renameDialog
.exec()) {
536 case KIO::R_OVERWRITE
:
537 // the destination should be overwritten
538 ok
= KIO::NetAccess::file_move(source
, dest
, -1, true);
541 case KIO::R_RENAME
: {
542 // a new name for the destination has been used
543 KUrl
newDest(renameDialog
.newDestUrl());
544 ok
= KIO::NetAccess::file_move(source
, newDest
);
549 // the renaming operation has been canceled
555 // no destination exists, hence just move the file to
557 ok
= KIO::NetAccess::file_move(source
, dest
);
560 const QString destFileName
= dest
.fileName();
562 m_statusBar
->setMessage(i18n("Renamed file '%1' to '%2'.",source
.fileName(), destFileName
),
563 DolphinStatusBar::OperationCompleted
);
565 KonqOperations::rename(this, source
, destFileName
);
568 m_statusBar
->setMessage(i18n("Renaming of file '%1' to '%2' failed.",source
.fileName(), destFileName
),
569 DolphinStatusBar::Error
);
574 void DolphinView::reload()
576 startDirLister(m_urlNavigator
->url(), true);
579 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
581 QWidget::mouseReleaseEvent(event
);
582 mainWindow()->setActiveView(this);
585 DolphinMainWindow
* DolphinView::mainWindow() const
590 void DolphinView::loadDirectory(const KUrl
& url
)
596 const ViewProperties
props(url
);
598 const Mode mode
= props
.viewMode();
599 bool changeMode
= (m_mode
!= mode
);
600 if (changeMode
&& isColumnViewActive()) {
601 // The column view is active. Only change the
602 // mode if the current URL is no child of the column view.
603 if (m_dirLister
->url().isParentOf(url
)) {
614 const bool showHiddenFiles
= props
.showHiddenFiles();
615 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
616 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
617 emit
showHiddenFilesChanged();
620 const DolphinView::Sorting sorting
= props
.sorting();
621 if (sorting
!= m_proxyModel
->sorting()) {
622 m_proxyModel
->setSorting(sorting
);
623 emit
sortingChanged(sorting
);
626 const Qt::SortOrder sortOrder
= props
.sortOrder();
627 if (sortOrder
!= m_proxyModel
->sortOrder()) {
628 m_proxyModel
->setSortOrder(sortOrder
);
629 emit
sortOrderChanged(sortOrder
);
632 KFileItemDelegate::AdditionalInformation info
= props
.additionalInfo();
633 if (info
!= m_fileItemDelegate
->additionalInformation()) {
634 m_fileItemDelegate
->setAdditionalInformation(info
);
636 emit
additionalInfoChanged(info
);
639 const bool showPreview
= props
.showPreview();
640 if (showPreview
!= m_controller
->showPreview()) {
641 m_controller
->setShowPreview(showPreview
);
642 emit
showPreviewChanged();
646 emit
urlChanged(url
);
648 m_statusBar
->clear();
651 void DolphinView::triggerItem(const QModelIndex
& index
)
653 if (!isValidNameIndex(index
)) {
657 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
658 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
659 // items are selected by the user, hence don't trigger the
660 // item specified by 'index'
664 KFileItem
* item
= m_dirModel
->itemForIndex(m_proxyModel
->mapToSource(index
));
669 // Prefer the local path over the URL. This assures that the
670 // volume space information is correct. Assuming that the URL is media:/sda1,
671 // and the local path is /windows/C: For the URL the space info is related
672 // to the root partition (and hence wrong) and for the local path the space
673 // info is related to the windows partition (-> correct).
674 const QString
localPath(item
->localPath());
676 if (localPath
.isEmpty()) {
686 else if (item
->isFile()) {
687 // allow to browse through ZIP and tar files
688 KMimeType::Ptr mime
= item
->mimeTypePtr();
689 if (mime
->is("application/zip")) {
690 url
.setProtocol("zip");
693 else if (mime
->is("application/x-tar") ||
694 mime
->is("application/x-tarz") ||
695 mime
->is("application/x-bzip-compressed-tar") ||
696 mime
->is("application/x-compressed-tar") ||
697 mime
->is("application/x-tzo")) {
698 url
.setProtocol("tar");
710 void DolphinView::updateProgress(int percent
)
712 if (m_showProgress
) {
713 m_statusBar
->setProgress(percent
);
717 void DolphinView::updateItemCount()
719 if (m_showProgress
) {
720 m_statusBar
->setProgressText(QString());
721 m_statusBar
->setProgress(100);
722 m_showProgress
= false;
725 KFileItemList
items(m_dirLister
->items());
726 KFileItemList::const_iterator it
= items
.begin();
727 const KFileItemList::const_iterator end
= items
.end();
733 KFileItem
* item
= *it
;
745 m_blockContentsMovedSignal
= false;
746 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
749 void DolphinView::generatePreviews(const KFileItemList
& items
)
751 if (m_controller
->showPreview()) {
752 KIO::PreviewJob
* job
= KIO::filePreview(items
, 128);
753 connect(job
, SIGNAL(gotPreview(const KFileItem
*, const QPixmap
&)),
754 this, SLOT(showPreview(const KFileItem
*, const QPixmap
&)));
758 void DolphinView::showPreview(const KFileItem
* item
, const QPixmap
& pixmap
)
761 const QModelIndex idx
= m_dirModel
->indexForItem(*item
);
762 if (idx
.isValid() && (idx
.column() == 0)) {
763 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
764 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(*item
)) {
765 KIconEffect iconEffect
;
766 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
767 m_dirModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
770 m_dirModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
775 void DolphinView::restoreContentsPos()
777 KUrl currentUrl
= m_urlNavigator
->url();
778 if (!currentUrl
.isEmpty()) {
779 QAbstractItemView
* view
= itemView();
780 // TODO: view->setCurrentItem(m_urlNavigator->currentFileName());
781 QPoint pos
= m_urlNavigator
->savedPosition();
782 view
->horizontalScrollBar()->setValue(pos
.x());
783 view
->verticalScrollBar()->setValue(pos
.y());
787 void DolphinView::showInfoMessage(const QString
& msg
)
789 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
792 void DolphinView::showErrorMessage(const QString
& msg
)
794 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
797 void DolphinView::emitSelectionChangedSignal()
799 emit
selectionChanged(DolphinView::selectedItems());
802 void DolphinView::closeFilterBar()
805 emit
showFilterBarChanged(false);
808 void DolphinView::startDirLister(const KUrl
& url
, bool reload
)
810 if (!url
.isValid()) {
811 const QString
location(url
.pathOrUrl());
812 if (location
.isEmpty()) {
813 m_statusBar
->setMessage(i18n("The location is empty."), DolphinStatusBar::Error
);
816 m_statusBar
->setMessage(i18n("The location '%1' is invalid.",location
),
817 DolphinStatusBar::Error
);
822 // Only show the directory loading progress if the status bar does
823 // not contain another progress information. This means that
824 // the directory loading progress information has the lowest priority.
825 const QString
progressText(m_statusBar
->progressText());
826 m_showProgress
= progressText
.isEmpty() ||
827 (progressText
== i18n("Loading directory..."));
828 if (m_showProgress
) {
829 m_statusBar
->setProgressText(i18n("Loading directory..."));
830 m_statusBar
->setProgress(0);
833 m_cutItemsCache
.clear();
834 m_blockContentsMovedSignal
= true;
838 bool keepOldDirs
= isColumnViewActive();
843 const KUrl
& dirListerUrl
= m_dirLister
->url();
844 if (dirListerUrl
.isValid()) {
845 const KUrl::List dirs
= m_dirLister
->directories();
847 foreach (url
, dirs
) {
848 m_dirLister
->updateDirectory(url
);
853 else if (m_dirLister
->directories().contains(url
)) {
854 // The dir lister contains the directory already, so
855 // KDirLister::openUrl() may not been invoked twice.
856 m_dirLister
->updateDirectory(url
);
860 const KUrl
& dirListerUrl
= m_dirLister
->url();
861 if ((dirListerUrl
== url
) || !m_dirLister
->url().isParentOf(url
)) {
862 // The current URL is not a child of the dir lister
863 // URL. This may happen when e. g. a bookmark has been selected
864 // and hence the view must be reset.
871 m_dirLister
->openUrl(url
, keepOldDirs
, reload
);
875 QString
DolphinView::defaultStatusBarText() const
877 return KIO::itemsSummaryString(m_fileCount
+ m_folderCount
,
883 QString
DolphinView::selectionStatusBarText() const
886 const KFileItemList list
= selectedItems();
887 if (list
.isEmpty()) {
888 // when an item is triggered, it is temporary selected but selectedItems()
889 // will return an empty list
895 KIO::filesize_t byteSize
= 0;
896 KFileItemList::const_iterator it
= list
.begin();
897 const KFileItemList::const_iterator end
= list
.end();
899 KFileItem
* item
= *it
;
905 byteSize
+= item
->size();
910 if (folderCount
> 0) {
911 text
= i18np("1 Folder selected", "%1 Folders selected", folderCount
);
918 const QString
sizeText(KIO::convertSize(byteSize
));
919 text
+= i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount
, sizeText
);
925 void DolphinView::showFilterBar(bool show
)
927 Q_ASSERT(m_filterBar
!= 0);
936 void DolphinView::updateStatusBar()
938 // As the item count information is less important
939 // in comparison with other messages, it should only
941 // - the status bar is empty or
942 // - shows already the item count information or
943 // - shows only a not very important information
944 // - if any progress is given don't show the item count info at all
945 const QString
msg(m_statusBar
->message());
946 const bool updateStatusBarMsg
= (msg
.isEmpty() ||
947 (msg
== m_statusBar
->defaultText()) ||
948 (m_statusBar
->type() == DolphinStatusBar::Information
)) &&
949 (m_statusBar
->progress() == 100);
951 const QString
text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
952 m_statusBar
->setDefaultText(text
);
954 if (updateStatusBarMsg
) {
955 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
959 void DolphinView::requestActivation()
961 m_mainWindow
->setActiveView(this);
964 void DolphinView::changeSelection(const KFileItemList
& selection
)
967 if (selection
.isEmpty()) {
970 KUrl baseUrl
= url();
972 QItemSelection new_selection
;
973 foreach (KFileItem
* item
, selection
) {
974 url
= item
->url().upUrl();
975 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
976 QModelIndex index
= m_proxyModel
->mapFromSource(m_dirModel
->indexForItem(*item
));
977 new_selection
.select(index
, index
);
980 itemView()->selectionModel()->select(new_selection
,
981 QItemSelectionModel::ClearAndSelect
982 | QItemSelectionModel::Current
);
985 void DolphinView::changeNameFilter(const QString
& nameFilter
)
987 // The name filter of KDirLister does a 'hard' filtering, which
988 // means that only the items are shown where the names match
989 // exactly the filter. This is non-transparent for the user, which
990 // just wants to have a 'soft' filtering: does the name contain
991 // the filter string?
992 QString
adjustedFilter(nameFilter
);
993 adjustedFilter
.insert(0, '*');
994 adjustedFilter
.append('*');
996 // Use the ProxyModel to filter:
997 // This code is #ifdefed as setNameFilter behaves
998 // slightly different than the QSortFilterProxyModel
999 // as it will not remove directories. I will ask
1000 // our beloved usability experts for input
1003 m_dirLister
->setNameFilter(adjustedFilter
);
1004 m_dirLister
->emitChanges();
1006 m_proxyModel
->setFilterRegExp( nameFilter
);
1010 void DolphinView::openContextMenu(const QPoint
& pos
)
1012 KFileItem
* item
= 0;
1014 const QModelIndex index
= itemView()->indexAt(pos
);
1015 if (isValidNameIndex(index
)) {
1016 item
= fileItem(index
);
1019 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url());
1023 void DolphinView::dropUrls(const KUrl::List
& urls
,
1024 const QModelIndex
& index
,
1027 KFileItem
* directory
= 0;
1028 if (isValidNameIndex(index
)) {
1029 KFileItem
* item
= fileItem(index
);
1030 Q_ASSERT(item
!= 0);
1031 if (item
->isDir()) {
1032 // the URLs are dropped above a directory
1037 if ((directory
== 0) && (source
== itemView())) {
1038 // The dropping is done into the same viewport where
1039 // the dragging has been started. Just ignore this...
1043 const KUrl
& destination
= (directory
== 0) ? url() :
1045 dropUrls(urls
, destination
);
1048 void DolphinView::dropUrls(const KUrl::List
& urls
,
1049 const KUrl
& destination
)
1051 m_mainWindow
->dropUrls(urls
, destination
);
1054 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
1056 ViewProperties
props(url());
1057 props
.setSorting(sorting
);
1059 m_proxyModel
->setSorting(sorting
);
1061 emit
sortingChanged(sorting
);
1064 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1066 ViewProperties
props(url());
1067 props
.setSortOrder(order
);
1069 m_proxyModel
->setSortOrder(order
);
1071 emit
sortOrderChanged(order
);
1074 void DolphinView::emitContentsMoved()
1076 if (!m_blockContentsMovedSignal
) {
1077 emit
contentsMoved(contentsX(), contentsY());
1081 void DolphinView::updateActivationState()
1083 m_urlNavigator
->setActive(isActive());
1085 emit
urlChanged(url());
1086 emit
selectionChanged(selectedItems());
1090 void DolphinView::updateCutItems()
1092 // restore the icons of all previously selected items to the
1093 // original state...
1094 QList
<CutItem
>::const_iterator it
= m_cutItemsCache
.begin();
1095 QList
<CutItem
>::const_iterator end
= m_cutItemsCache
.end();
1097 const QModelIndex index
= m_dirModel
->indexForUrl((*it
).url
);
1098 if (index
.isValid()) {
1099 m_dirModel
->setData(index
, QIcon((*it
).pixmap
), Qt::DecorationRole
);
1103 m_cutItemsCache
.clear();
1105 // ... and apply an item effect to all currently cut items
1106 applyCutItemEffect();
1109 void DolphinView::createView()
1111 // delete current view
1112 QAbstractItemView
* view
= itemView();
1114 m_topLayout
->removeWidget(view
);
1116 view
->deleteLater();
1121 m_fileItemDelegate
= 0;
1124 Q_ASSERT(m_iconsView
== 0);
1125 Q_ASSERT(m_detailsView
== 0);
1126 Q_ASSERT(m_columnView
== 0);
1128 // ... and recreate it representing the current mode
1131 m_iconsView
= new DolphinIconsView(this, m_controller
);
1136 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1137 view
= m_detailsView
;
1141 m_columnView
= new DolphinColumnView(this, m_controller
);
1142 view
= m_columnView
;
1146 Q_ASSERT(view
!= 0);
1148 m_fileItemDelegate
= new KFileItemDelegate(view
);
1149 view
->setItemDelegate(m_fileItemDelegate
);
1151 view
->setModel(m_proxyModel
);
1152 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1154 new KMimeTypeResolver(view
, m_dirModel
);
1155 m_topLayout
->insertWidget(1, view
);
1157 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1158 m_controller
, SLOT(indicateSelectionChange()));
1159 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1160 this, SLOT(emitContentsMoved()));
1161 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1162 this, SLOT(emitContentsMoved()));
1165 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags
)
1167 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
1168 const QAbstractItemModel
* itemModel
= selectionModel
->model();
1170 const QModelIndex topLeft
= itemModel
->index(0, 0);
1171 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
1172 itemModel
->columnCount() - 1);
1174 QItemSelection
selection(topLeft
, bottomRight
);
1175 selectionModel
->select(selection
, flags
);
1178 QAbstractItemView
* DolphinView::itemView() const
1180 if (m_detailsView
!= 0) {
1181 return m_detailsView
;
1183 else if (m_columnView
!= 0) {
1184 return m_columnView
;
1190 bool DolphinView::isValidNameIndex(const QModelIndex
& index
) const
1192 return index
.isValid() && (index
.column() == KDirModel::Name
);
1195 bool DolphinView::isCutItem(const KFileItem
& item
) const
1197 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1198 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1200 const KUrl
& itemUrl
= item
.url();
1201 KUrl::List::const_iterator it
= cutUrls
.begin();
1202 const KUrl::List::const_iterator end
= cutUrls
.end();
1204 if (*it
== itemUrl
) {
1213 void DolphinView::applyCutItemEffect()
1215 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1216 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
1220 KFileItemList
items(m_dirLister
->items());
1221 KFileItemList::const_iterator it
= items
.begin();
1222 const KFileItemList::const_iterator end
= items
.end();
1224 KFileItem
* item
= *it
;
1225 if (isCutItem(*item
)) {
1226 const QModelIndex index
= m_dirModel
->indexForItem(*item
);
1227 const KFileItem
* item
= m_dirModel
->itemForIndex(index
);
1228 const QVariant value
= m_dirModel
->data(index
, Qt::DecorationRole
);
1229 if ((value
.type() == QVariant::Icon
) && (item
!= 0)) {
1230 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
1231 QPixmap pixmap
= icon
.pixmap(128, 128);
1233 // remember current pixmap for the item to be able
1234 // to restore it when other items get cut
1236 cutItem
.url
= item
->url();
1237 cutItem
.pixmap
= pixmap
;
1238 m_cutItemsCache
.append(cutItem
);
1240 // apply icon effect to the cut item
1241 KIconEffect iconEffect
;
1242 pixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
1243 m_dirModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
1250 #include "dolphinview.moc"