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 "urlnavigator.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 UrlNavigator(new KFilePlacesModel(this), url
, this);
101 m_urlNavigator
->setUrlEditable(DolphinSettings::instance().generalSettings()->editableUrl());
102 m_urlNavigator
->setHomeUrl(DolphinSettings::instance().generalSettings()->homeUrl());
103 m_urlNavigator
->setShowHiddenFiles(showHiddenFiles
);
104 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
105 this, SLOT(loadDirectory(const KUrl
&)));
106 connect(m_urlNavigator
, SIGNAL(urlsDropped(const KUrl::List
&, const KUrl
&)),
107 this, SLOT(dropUrls(const KUrl::List
&, const KUrl
&)));
108 connect(m_urlNavigator
, SIGNAL(activated()),
109 this, SLOT(requestActivation()));
110 connect(this, SIGNAL(contentsMoved(int, int)),
111 m_urlNavigator
, SLOT(storeContentsPosition(int, int)));
113 m_statusBar
= new DolphinStatusBar(this);
115 m_dirLister
= new DolphinDirLister();
116 m_dirLister
->setAutoUpdate(true);
117 m_dirLister
->setMainWindow(this);
118 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
119 m_dirLister
->setDelayedMimeTypes(true);
121 connect(m_dirLister
, SIGNAL(clear()),
122 this, SLOT(updateStatusBar()));
123 connect(m_dirLister
, SIGNAL(percent(int)),
124 this, SLOT(updateProgress(int)));
125 connect(m_dirLister
, SIGNAL(deleteItem(KFileItem
*)),
126 this, SLOT(updateStatusBar()));
127 connect(m_dirLister
, SIGNAL(completed()),
128 this, SLOT(updateItemCount()));
129 connect(m_dirLister
, SIGNAL(completed()),
130 this, SLOT(updateCutItems()));
131 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
132 this, SLOT(generatePreviews(const KFileItemList
&)));
133 connect(m_dirLister
, SIGNAL(infoMessage(const QString
&)),
134 this, SLOT(showInfoMessage(const QString
&)));
135 connect(m_dirLister
, SIGNAL(errorMessage(const QString
&)),
136 this, SLOT(showErrorMessage(const QString
&)));
138 m_dirModel
= new KDirModel();
139 m_dirModel
->setDirLister(m_dirLister
);
140 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
142 m_proxyModel
= new DolphinSortFilterProxyModel(this);
143 m_proxyModel
->setSourceModel(m_dirModel
);
145 m_controller
= new DolphinController(this);
146 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&)),
147 this, SLOT(openContextMenu(const QPoint
&)));
148 connect(m_controller
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&, QWidget
*)),
149 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&, QWidget
*)));
150 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
151 this, SLOT(updateSorting(DolphinView::Sorting
)));
152 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
153 this, SLOT(updateSortOrder(Qt::SortOrder
)));
154 connect(m_controller
, SIGNAL(itemTriggered(const QModelIndex
&)),
155 this, SLOT(triggerItem(const QModelIndex
&)));
156 connect(m_controller
, SIGNAL(selectionChanged()),
157 this, SLOT(emitSelectionChangedSignal()));
158 connect(m_controller
, SIGNAL(activated()),
159 this, SLOT(requestActivation()));
163 m_iconSize
= K3Icon::SizeMedium
;
165 m_filterBar
= new FilterBar(this);
167 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
168 this, SLOT(changeNameFilter(const QString
&)));
169 connect(m_filterBar
, SIGNAL(closeRequest()),
170 this, SLOT(closeFilterBar()));
172 m_topLayout
->addWidget(m_urlNavigator
);
173 m_topLayout
->addWidget(itemView());
174 m_topLayout
->addWidget(m_filterBar
);
175 m_topLayout
->addWidget(m_statusBar
);
178 DolphinView::~DolphinView()
184 void DolphinView::setUrl(const KUrl
& url
)
186 m_urlNavigator
->setUrl(url
);
187 m_controller
->setUrl(url
);
190 const KUrl
& DolphinView::url() const
192 return m_urlNavigator
->url();
195 bool DolphinView::isActive() const
197 return m_mainWindow
->activeView() == this;
200 void DolphinView::setMode(Mode mode
)
202 if (mode
== m_mode
) {
203 return; // the wished mode is already set
208 ViewProperties
props(m_urlNavigator
->url());
209 props
.setViewMode(m_mode
);
212 startDirLister(m_urlNavigator
->url());
217 DolphinView::Mode
DolphinView::mode() const
222 void DolphinView::setShowPreview(bool show
)
224 ViewProperties
props(m_urlNavigator
->url());
225 props
.setShowPreview(show
);
227 m_controller
->setShowPreview(show
);
229 emit
showPreviewChanged();
233 bool DolphinView::showPreview() const
235 return m_controller
->showPreview();
238 void DolphinView::setShowHiddenFiles(bool show
)
240 if (m_dirLister
->showingDotFiles() == show
) {
244 ViewProperties
props(m_urlNavigator
->url());
245 props
.setShowHiddenFiles(show
);
248 m_dirLister
->setShowingDotFiles(show
);
249 m_urlNavigator
->setShowHiddenFiles(show
);
251 emit
showHiddenFilesChanged();
256 bool DolphinView::showHiddenFiles() const
258 return m_dirLister
->showingDotFiles();
261 void DolphinView::renameSelectedItems()
263 DolphinView
* view
= mainWindow()->activeView();
264 const KUrl::List urls
= selectedUrls();
265 if (urls
.count() > 1) {
266 // More than one item has been selected for renaming. Open
267 // a rename dialog and rename all items afterwards.
268 RenameDialog
dialog(urls
);
269 if (dialog
.exec() == QDialog::Rejected
) {
273 const QString
& newName
= dialog
.newName();
274 if (newName
.isEmpty()) {
275 view
->statusBar()->setMessage(dialog
.errorString(),
276 DolphinStatusBar::Error
);
279 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
280 // as one operation instead of n rename operations like it is done now...
281 Q_ASSERT(newName
.contains('#'));
283 // iterate through all selected items and rename them...
284 const int replaceIndex
= newName
.indexOf('#');
285 Q_ASSERT(replaceIndex
>= 0);
288 KUrl::List::const_iterator it
= urls
.begin();
289 KUrl::List::const_iterator end
= urls
.end();
291 const KUrl
& oldUrl
= *it
;
293 number
.setNum(index
++);
295 QString
name(newName
);
296 name
.replace(replaceIndex
, 1, number
);
298 if (oldUrl
.fileName() != name
) {
299 KUrl newUrl
= oldUrl
;
300 newUrl
.setFileName(name
);
301 m_mainWindow
->rename(oldUrl
, newUrl
);
308 // Only one item has been selected for renaming. Use the custom
309 // renaming mechanism from the views.
310 Q_ASSERT(urls
.count() == 1);
312 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
313 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
314 // is a benefit for the user at all -> let's wait for some input first...
315 RenameDialog
dialog(urls
);
316 if (dialog
.exec() == QDialog::Rejected
) {
320 const QString
& newName
= dialog
.newName();
321 if (newName
.isEmpty()) {
322 view
->statusBar()->setMessage(dialog
.errorString(),
323 DolphinStatusBar::Error
);
326 const KUrl
& oldUrl
= urls
.first();
327 KUrl newUrl
= oldUrl
;
328 newUrl
.setFileName(newName
);
329 m_mainWindow
->rename(oldUrl
, newUrl
);
334 void DolphinView::selectAll()
336 selectAll(QItemSelectionModel::Select
);
339 void DolphinView::invertSelection()
341 selectAll(QItemSelectionModel::Toggle
);
344 DolphinStatusBar
* DolphinView::statusBar() const
349 int DolphinView::contentsX() const
351 return itemView()->horizontalScrollBar()->value();
354 int DolphinView::contentsY() const
356 return itemView()->verticalScrollBar()->value();
359 void DolphinView::emitRequestItemInfo(const KUrl
& url
)
361 emit
requestItemInfo(url
);
364 bool DolphinView::isFilterBarVisible() const
366 return m_filterBar
->isVisible();
369 bool DolphinView::isUrlEditable() const
371 return m_urlNavigator
->isUrlEditable();
374 void DolphinView::zoomIn()
376 m_controller
->triggerZoomIn();
379 void DolphinView::zoomOut()
381 m_controller
->triggerZoomOut();
384 bool DolphinView::isZoomInPossible() const
386 return m_controller
->isZoomInPossible();
389 bool DolphinView::isZoomOutPossible() const
391 return m_controller
->isZoomOutPossible();
394 void DolphinView::setSorting(Sorting sorting
)
396 if (sorting
!= this->sorting()) {
397 updateSorting(sorting
);
401 DolphinView::Sorting
DolphinView::sorting() const
403 return m_proxyModel
->sorting();
406 void DolphinView::setSortOrder(Qt::SortOrder order
)
408 if (sortOrder() != order
) {
409 updateSortOrder(order
);
413 Qt::SortOrder
DolphinView::sortOrder() const
415 return m_proxyModel
->sortOrder();
418 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info
)
420 ViewProperties
props(m_urlNavigator
->url());
421 props
.setAdditionalInfo(info
);
423 m_fileItemDelegate
->setAdditionalInformation(info
);
425 emit
additionalInfoChanged(info
);
429 KFileItemDelegate::AdditionalInformation
DolphinView::additionalInfo() const
431 return m_fileItemDelegate
->additionalInformation();
434 void DolphinView::goBack()
436 m_urlNavigator
->goBack();
439 void DolphinView::goForward()
441 m_urlNavigator
->goForward();
444 void DolphinView::goUp()
446 m_urlNavigator
->goUp();
449 void DolphinView::goHome()
451 m_urlNavigator
->goHome();
454 void DolphinView::setUrlEditable(bool editable
)
456 m_urlNavigator
->setUrlEditable(editable
);
459 bool DolphinView::hasSelection() const
461 return itemView()->selectionModel()->hasSelection();
464 void DolphinView::clearSelection()
466 itemView()->selectionModel()->clear();
469 KFileItemList
DolphinView::selectedItems() const
471 const QAbstractItemView
* view
= itemView();
473 // Our view has a selection, we will map them back to the DirModel
474 // and then fill the KFileItemList.
475 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
477 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
478 KFileItemList itemList
;
480 const QModelIndexList indexList
= selection
.indexes();
481 QModelIndexList::const_iterator end
= indexList
.end();
482 for (QModelIndexList::const_iterator it
= indexList
.begin(); it
!= end
; ++it
) {
483 Q_ASSERT((*it
).isValid());
485 KFileItem
* item
= m_dirModel
->itemForIndex(*it
);
487 itemList
.append(item
);
494 KUrl::List
DolphinView::selectedUrls() const
498 const KFileItemList list
= selectedItems();
499 KFileItemList::const_iterator it
= list
.begin();
500 const KFileItemList::const_iterator end
= list
.end();
502 KFileItem
* item
= *it
;
503 urls
.append(item
->url());
510 KFileItem
* DolphinView::fileItem(const QModelIndex index
) const
512 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
513 return m_dirModel
->itemForIndex(dirModelIndex
);
516 void DolphinView::rename(const KUrl
& source
, const QString
& newName
)
520 if (newName
.isEmpty() || (source
.fileName() == newName
)) {
524 KUrl
dest(source
.upUrl());
525 dest
.addPath(newName
);
527 const bool destExists
= KIO::NetAccess::exists(dest
,
529 mainWindow()->activeView());
531 // the destination already exists, hence ask the user
533 KIO::RenameDialog
renameDialog(this,
534 i18n("File Already Exists"),
538 switch (renameDialog
.exec()) {
539 case KIO::R_OVERWRITE
:
540 // the destination should be overwritten
541 ok
= KIO::NetAccess::file_move(source
, dest
, -1, true);
544 case KIO::R_RENAME
: {
545 // a new name for the destination has been used
546 KUrl
newDest(renameDialog
.newDestUrl());
547 ok
= KIO::NetAccess::file_move(source
, newDest
);
552 // the renaming operation has been canceled
558 // no destination exists, hence just move the file to
560 ok
= KIO::NetAccess::file_move(source
, dest
);
563 const QString destFileName
= dest
.fileName();
565 m_statusBar
->setMessage(i18n("Renamed file '%1' to '%2'.",source
.fileName(), destFileName
),
566 DolphinStatusBar::OperationCompleted
);
568 KonqOperations::rename(this, source
, destFileName
);
571 m_statusBar
->setMessage(i18n("Renaming of file '%1' to '%2' failed.",source
.fileName(), destFileName
),
572 DolphinStatusBar::Error
);
577 void DolphinView::reload()
579 startDirLister(m_urlNavigator
->url(), true);
582 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
584 QWidget::mouseReleaseEvent(event
);
585 mainWindow()->setActiveView(this);
588 DolphinMainWindow
* DolphinView::mainWindow() const
593 void DolphinView::loadDirectory(const KUrl
& url
)
599 const ViewProperties
props(url
);
601 const Mode mode
= props
.viewMode();
602 bool changeMode
= (m_mode
!= mode
);
603 if (changeMode
&& isColumnViewActive()) {
604 // The column view is active. Only change the
605 // mode if the current URL is no child of the column view.
606 if (m_dirLister
->url().isParentOf(url
)) {
617 const bool showHiddenFiles
= props
.showHiddenFiles();
618 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
619 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
620 emit
showHiddenFilesChanged();
623 const DolphinView::Sorting sorting
= props
.sorting();
624 if (sorting
!= m_proxyModel
->sorting()) {
625 m_proxyModel
->setSorting(sorting
);
626 emit
sortingChanged(sorting
);
629 const Qt::SortOrder sortOrder
= props
.sortOrder();
630 if (sortOrder
!= m_proxyModel
->sortOrder()) {
631 m_proxyModel
->setSortOrder(sortOrder
);
632 emit
sortOrderChanged(sortOrder
);
635 KFileItemDelegate::AdditionalInformation info
= props
.additionalInfo();
636 if (info
!= m_fileItemDelegate
->additionalInformation()) {
637 m_fileItemDelegate
->setAdditionalInformation(info
);
639 emit
additionalInfoChanged(info
);
642 const bool showPreview
= props
.showPreview();
643 if (showPreview
!= m_controller
->showPreview()) {
644 m_controller
->setShowPreview(showPreview
);
645 emit
showPreviewChanged();
649 emit
urlChanged(url
);
651 m_statusBar
->clear();
654 void DolphinView::triggerItem(const QModelIndex
& index
)
656 if (!isValidNameIndex(index
)) {
660 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
661 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
662 // items are selected by the user, hence don't trigger the
663 // item specified by 'index'
667 KFileItem
* item
= m_dirModel
->itemForIndex(m_proxyModel
->mapToSource(index
));
672 // Prefer the local path over the URL. This assures that the
673 // volume space information is correct. Assuming that the URL is media:/sda1,
674 // and the local path is /windows/C: For the URL the space info is related
675 // to the root partition (and hence wrong) and for the local path the space
676 // info is related to the windows partition (-> correct).
677 const QString
localPath(item
->localPath());
679 if (localPath
.isEmpty()) {
689 else if (item
->isFile()) {
690 // allow to browse through ZIP and tar files
691 KMimeType::Ptr mime
= item
->mimeTypePtr();
692 if (mime
->is("application/zip")) {
693 url
.setProtocol("zip");
696 else if (mime
->is("application/x-tar") ||
697 mime
->is("application/x-tarz") ||
698 mime
->is("application/x-bzip-compressed-tar") ||
699 mime
->is("application/x-compressed-tar") ||
700 mime
->is("application/x-tzo")) {
701 url
.setProtocol("tar");
713 void DolphinView::updateProgress(int percent
)
715 if (m_showProgress
) {
716 m_statusBar
->setProgress(percent
);
720 void DolphinView::updateItemCount()
722 if (m_showProgress
) {
723 m_statusBar
->setProgressText(QString());
724 m_statusBar
->setProgress(100);
725 m_showProgress
= false;
728 KFileItemList
items(m_dirLister
->items());
729 KFileItemList::const_iterator it
= items
.begin();
730 const KFileItemList::const_iterator end
= items
.end();
736 KFileItem
* item
= *it
;
748 m_blockContentsMovedSignal
= false;
749 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
752 void DolphinView::generatePreviews(const KFileItemList
& items
)
754 if (m_controller
->showPreview()) {
755 KIO::PreviewJob
* job
= KIO::filePreview(items
, 128);
756 connect(job
, SIGNAL(gotPreview(const KFileItem
*, const QPixmap
&)),
757 this, SLOT(showPreview(const KFileItem
*, const QPixmap
&)));
761 void DolphinView::showPreview(const KFileItem
* item
, const QPixmap
& pixmap
)
764 const QModelIndex idx
= m_dirModel
->indexForItem(*item
);
765 if (idx
.isValid() && (idx
.column() == 0)) {
766 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
767 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(*item
)) {
768 KIconEffect iconEffect
;
769 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
770 m_dirModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
773 m_dirModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
778 void DolphinView::restoreContentsPos()
780 KUrl currentUrl
= m_urlNavigator
->url();
781 if (!currentUrl
.isEmpty()) {
782 QAbstractItemView
* view
= itemView();
783 // TODO: view->setCurrentItem(m_urlNavigator->currentFileName());
784 QPoint pos
= m_urlNavigator
->savedPosition();
785 view
->horizontalScrollBar()->setValue(pos
.x());
786 view
->verticalScrollBar()->setValue(pos
.y());
790 void DolphinView::showInfoMessage(const QString
& msg
)
792 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
795 void DolphinView::showErrorMessage(const QString
& msg
)
797 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
800 void DolphinView::emitSelectionChangedSignal()
802 emit
selectionChanged(DolphinView::selectedItems());
805 void DolphinView::closeFilterBar()
808 emit
showFilterBarChanged(false);
811 void DolphinView::startDirLister(const KUrl
& url
, bool reload
)
813 if (!url
.isValid()) {
814 const QString
location(url
.pathOrUrl());
815 if (location
.isEmpty()) {
816 m_statusBar
->setMessage(i18n("The location is empty."), DolphinStatusBar::Error
);
819 m_statusBar
->setMessage(i18n("The location '%1' is invalid.",location
),
820 DolphinStatusBar::Error
);
825 // Only show the directory loading progress if the status bar does
826 // not contain another progress information. This means that
827 // the directory loading progress information has the lowest priority.
828 const QString
progressText(m_statusBar
->progressText());
829 m_showProgress
= progressText
.isEmpty() ||
830 (progressText
== i18n("Loading directory..."));
831 if (m_showProgress
) {
832 m_statusBar
->setProgressText(i18n("Loading directory..."));
833 m_statusBar
->setProgress(0);
836 m_cutItemsCache
.clear();
837 m_blockContentsMovedSignal
= true;
841 bool keepOldDirs
= isColumnViewActive();
846 const KUrl
& dirListerUrl
= m_dirLister
->url();
847 if (dirListerUrl
.isValid()) {
848 const KUrl::List dirs
= m_dirLister
->directories();
850 foreach (url
, dirs
) {
851 m_dirLister
->updateDirectory(url
);
856 else if (m_dirLister
->directories().contains(url
)) {
857 // The dir lister contains the directory already, so
858 // KDirLister::openUrl() may not been invoked twice.
859 m_dirLister
->updateDirectory(url
);
863 const KUrl
& dirListerUrl
= m_dirLister
->url();
864 if ((dirListerUrl
== url
) || !m_dirLister
->url().isParentOf(url
)) {
865 // The current URL is not a child of the dir lister
866 // URL. This may happen when e. g. a bookmark has been selected
867 // and hence the view must be reset.
874 m_dirLister
->openUrl(url
, keepOldDirs
, reload
);
878 QString
DolphinView::defaultStatusBarText() const
880 return KIO::itemsSummaryString(m_fileCount
+ m_folderCount
,
886 QString
DolphinView::selectionStatusBarText() const
889 const KFileItemList list
= selectedItems();
890 if (list
.isEmpty()) {
891 // when an item is triggered, it is temporary selected but selectedItems()
892 // will return an empty list
898 KIO::filesize_t byteSize
= 0;
899 KFileItemList::const_iterator it
= list
.begin();
900 const KFileItemList::const_iterator end
= list
.end();
902 KFileItem
* item
= *it
;
908 byteSize
+= item
->size();
913 if (folderCount
> 0) {
914 text
= i18np("1 Folder selected", "%1 Folders selected", folderCount
);
921 const QString
sizeText(KIO::convertSize(byteSize
));
922 text
+= i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount
, sizeText
);
928 void DolphinView::showFilterBar(bool show
)
930 Q_ASSERT(m_filterBar
!= 0);
939 void DolphinView::updateStatusBar()
941 // As the item count information is less important
942 // in comparison with other messages, it should only
944 // - the status bar is empty or
945 // - shows already the item count information or
946 // - shows only a not very important information
947 // - if any progress is given don't show the item count info at all
948 const QString
msg(m_statusBar
->message());
949 const bool updateStatusBarMsg
= (msg
.isEmpty() ||
950 (msg
== m_statusBar
->defaultText()) ||
951 (m_statusBar
->type() == DolphinStatusBar::Information
)) &&
952 (m_statusBar
->progress() == 100);
954 const QString
text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
955 m_statusBar
->setDefaultText(text
);
957 if (updateStatusBarMsg
) {
958 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
962 void DolphinView::requestActivation()
964 m_mainWindow
->setActiveView(this);
967 void DolphinView::changeSelection(const KFileItemList
& selection
)
970 if (selection
.isEmpty()) {
973 KUrl baseUrl
= url();
975 QItemSelection new_selection
;
976 foreach (KFileItem
* item
, selection
) {
977 url
= item
->url().upUrl();
978 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
979 QModelIndex index
= m_proxyModel
->mapFromSource(m_dirModel
->indexForItem(*item
));
980 new_selection
.select(index
, index
);
983 itemView()->selectionModel()->select(new_selection
,
984 QItemSelectionModel::ClearAndSelect
985 | QItemSelectionModel::Current
);
988 void DolphinView::changeNameFilter(const QString
& nameFilter
)
990 // The name filter of KDirLister does a 'hard' filtering, which
991 // means that only the items are shown where the names match
992 // exactly the filter. This is non-transparent for the user, which
993 // just wants to have a 'soft' filtering: does the name contain
994 // the filter string?
995 QString
adjustedFilter(nameFilter
);
996 adjustedFilter
.insert(0, '*');
997 adjustedFilter
.append('*');
999 // Use the ProxyModel to filter:
1000 // This code is #ifdefed as setNameFilter behaves
1001 // slightly different than the QSortFilterProxyModel
1002 // as it will not remove directories. I will ask
1003 // our beloved usability experts for input
1006 m_dirLister
->setNameFilter(adjustedFilter
);
1007 m_dirLister
->emitChanges();
1009 m_proxyModel
->setFilterRegExp( nameFilter
);
1013 void DolphinView::openContextMenu(const QPoint
& pos
)
1015 KFileItem
* item
= 0;
1017 const QModelIndex index
= itemView()->indexAt(pos
);
1018 if (isValidNameIndex(index
)) {
1019 item
= fileItem(index
);
1022 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url());
1026 void DolphinView::dropUrls(const KUrl::List
& urls
,
1027 const QModelIndex
& index
,
1030 KFileItem
* directory
= 0;
1031 if (isValidNameIndex(index
)) {
1032 KFileItem
* item
= fileItem(index
);
1033 Q_ASSERT(item
!= 0);
1034 if (item
->isDir()) {
1035 // the URLs are dropped above a directory
1040 if ((directory
== 0) && (source
== itemView())) {
1041 // The dropping is done into the same viewport where
1042 // the dragging has been started. Just ignore this...
1046 const KUrl
& destination
= (directory
== 0) ? url() :
1048 dropUrls(urls
, destination
);
1051 void DolphinView::dropUrls(const KUrl::List
& urls
,
1052 const KUrl
& destination
)
1054 m_mainWindow
->dropUrls(urls
, destination
);
1057 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
1059 ViewProperties
props(url());
1060 props
.setSorting(sorting
);
1062 m_proxyModel
->setSorting(sorting
);
1064 emit
sortingChanged(sorting
);
1067 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1069 ViewProperties
props(url());
1070 props
.setSortOrder(order
);
1072 m_proxyModel
->setSortOrder(order
);
1074 emit
sortOrderChanged(order
);
1077 void DolphinView::emitContentsMoved()
1079 if (!m_blockContentsMovedSignal
) {
1080 emit
contentsMoved(contentsX(), contentsY());
1084 void DolphinView::updateActivationState()
1086 m_urlNavigator
->setActive(isActive());
1088 emit
urlChanged(url());
1089 emit
selectionChanged(selectedItems());
1093 void DolphinView::updateCutItems()
1095 // restore the icons of all previously selected items to the
1096 // original state...
1097 QList
<CutItem
>::const_iterator it
= m_cutItemsCache
.begin();
1098 QList
<CutItem
>::const_iterator end
= m_cutItemsCache
.end();
1100 const QModelIndex index
= m_dirModel
->indexForUrl((*it
).url
);
1101 if (index
.isValid()) {
1102 m_dirModel
->setData(index
, QIcon((*it
).pixmap
), Qt::DecorationRole
);
1106 m_cutItemsCache
.clear();
1108 // ... and apply an item effect to all currently cut items
1109 applyCutItemEffect();
1112 void DolphinView::createView()
1114 // delete current view
1115 QAbstractItemView
* view
= itemView();
1117 m_topLayout
->removeWidget(view
);
1119 view
->deleteLater();
1124 m_fileItemDelegate
= 0;
1127 Q_ASSERT(m_iconsView
== 0);
1128 Q_ASSERT(m_detailsView
== 0);
1129 Q_ASSERT(m_columnView
== 0);
1131 // ... and recreate it representing the current mode
1134 m_iconsView
= new DolphinIconsView(this, m_controller
);
1139 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1140 view
= m_detailsView
;
1144 m_columnView
= new DolphinColumnView(this, m_controller
);
1145 view
= m_columnView
;
1149 Q_ASSERT(view
!= 0);
1151 m_fileItemDelegate
= new KFileItemDelegate(view
);
1152 view
->setItemDelegate(m_fileItemDelegate
);
1154 view
->setModel(m_proxyModel
);
1155 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1157 new KMimeTypeResolver(view
, m_dirModel
);
1158 m_topLayout
->insertWidget(1, view
);
1160 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1161 m_controller
, SLOT(indicateSelectionChange()));
1162 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1163 this, SLOT(emitContentsMoved()));
1164 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1165 this, SLOT(emitContentsMoved()));
1168 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags
)
1170 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
1171 const QAbstractItemModel
* itemModel
= selectionModel
->model();
1173 const QModelIndex topLeft
= itemModel
->index(0, 0);
1174 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
1175 itemModel
->columnCount() - 1);
1177 QItemSelection
selection(topLeft
, bottomRight
);
1178 selectionModel
->select(selection
, flags
);
1181 QAbstractItemView
* DolphinView::itemView() const
1183 if (m_detailsView
!= 0) {
1184 return m_detailsView
;
1186 else if (m_columnView
!= 0) {
1187 return m_columnView
;
1193 bool DolphinView::isValidNameIndex(const QModelIndex
& index
) const
1195 return index
.isValid() && (index
.column() == KDirModel::Name
);
1198 bool DolphinView::isCutItem(const KFileItem
& item
) const
1200 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1201 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1203 const KUrl
& itemUrl
= item
.url();
1204 KUrl::List::const_iterator it
= cutUrls
.begin();
1205 const KUrl::List::const_iterator end
= cutUrls
.end();
1207 if (*it
== itemUrl
) {
1216 void DolphinView::applyCutItemEffect()
1218 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1219 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
1223 KFileItemList
items(m_dirLister
->items());
1224 KFileItemList::const_iterator it
= items
.begin();
1225 const KFileItemList::const_iterator end
= items
.end();
1227 KFileItem
* item
= *it
;
1228 if (isCutItem(*item
)) {
1229 const QModelIndex index
= m_dirModel
->indexForItem(*item
);
1230 const KFileItem
* item
= m_dirModel
->itemForIndex(index
);
1231 const QVariant value
= m_dirModel
->data(index
, Qt::DecorationRole
);
1232 if ((value
.type() == QVariant::Icon
) && (item
!= 0)) {
1233 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
1234 QPixmap pixmap
= icon
.pixmap(128, 128);
1236 // remember current pixmap for the item to be able
1237 // to restore it when other items get cut
1239 cutItem
.url
= item
->url();
1240 cutItem
.pixmap
= pixmap
;
1241 m_cutItemsCache
.append(cutItem
);
1243 // apply icon effect to the cut item
1244 KIconEffect iconEffect
;
1245 pixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
1246 m_dirModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
1253 #include "dolphinview.moc"