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 "dolphinitemcategorizer.h"
55 #include "filterbar.h"
56 #include "renamedialog.h"
57 #include "kurlnavigator.h"
58 #include "viewproperties.h"
59 #include "dolphinsettings.h"
60 #include "dolphin_generalsettings.h"
62 DolphinView::DolphinView(DolphinMainWindow
* mainWindow
,
66 bool showHiddenFiles
) :
68 m_showProgress(false),
69 m_blockContentsMovedSignal(false),
74 m_mainWindow(mainWindow
),
81 m_fileItemDelegate(0),
89 setFocusPolicy(Qt::StrongFocus
);
90 m_topLayout
= new QVBoxLayout(this);
91 m_topLayout
->setSpacing(0);
92 m_topLayout
->setMargin(0);
94 connect(m_mainWindow
, SIGNAL(activeViewChanged()),
95 this, SLOT(updateActivationState()));
97 QClipboard
* clipboard
= QApplication::clipboard();
98 connect(clipboard
, SIGNAL(dataChanged()),
99 this, SLOT(updateCutItems()));
101 m_urlNavigator
= new KUrlNavigator(DolphinSettings::instance().placesModel(), url
, this);
102 m_urlNavigator
->setUrlEditable(DolphinSettings::instance().generalSettings()->editableUrl());
103 m_urlNavigator
->setHomeUrl(DolphinSettings::instance().generalSettings()->homeUrl());
104 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
105 this, SLOT(changeDirectory(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(savePosition(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 emit
showHiddenFilesChanged();
254 bool DolphinView::showHiddenFiles() const
256 return m_dirLister
->showingDotFiles();
259 void DolphinView::setCategorizedSorting(bool categorized
)
261 if (!supportsCategorizedSorting() || (categorized
== categorizedSorting())) {
265 Q_ASSERT(m_iconsView
!= 0);
267 Q_ASSERT(m_iconsView
->itemCategorizer() == 0);
268 m_iconsView
->setItemCategorizer(new DolphinItemCategorizer());
270 KItemCategorizer
* categorizer
= m_iconsView
->itemCategorizer();
271 m_iconsView
->setItemCategorizer(0);
275 ViewProperties
props(m_urlNavigator
->url());
276 props
.setCategorizedSorting(categorized
);
279 emit
categorizedSortingChanged();
282 bool DolphinView::categorizedSorting() const
284 if (!supportsCategorizedSorting()) {
288 Q_ASSERT(m_iconsView
!= 0);
289 return m_iconsView
->itemCategorizer() != 0;
292 bool DolphinView::supportsCategorizedSorting() const
294 return m_iconsView
!= 0;
297 void DolphinView::renameSelectedItems()
299 DolphinView
* view
= mainWindow()->activeView();
300 const KUrl::List urls
= selectedUrls();
301 if (urls
.count() > 1) {
302 // More than one item has been selected for renaming. Open
303 // a rename dialog and rename all items afterwards.
304 RenameDialog
dialog(urls
);
305 if (dialog
.exec() == QDialog::Rejected
) {
309 const QString
& newName
= dialog
.newName();
310 if (newName
.isEmpty()) {
311 view
->statusBar()->setMessage(dialog
.errorString(),
312 DolphinStatusBar::Error
);
314 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
315 // as one operation instead of n rename operations like it is done now...
316 Q_ASSERT(newName
.contains('#'));
318 // iterate through all selected items and rename them...
319 const int replaceIndex
= newName
.indexOf('#');
320 Q_ASSERT(replaceIndex
>= 0);
323 KUrl::List::const_iterator it
= urls
.begin();
324 KUrl::List::const_iterator end
= urls
.end();
326 const KUrl
& oldUrl
= *it
;
328 number
.setNum(index
++);
330 QString
name(newName
);
331 name
.replace(replaceIndex
, 1, number
);
333 if (oldUrl
.fileName() != name
) {
334 KUrl newUrl
= oldUrl
;
335 newUrl
.setFileName(name
);
336 m_mainWindow
->rename(oldUrl
, newUrl
);
342 // Only one item has been selected for renaming. Use the custom
343 // renaming mechanism from the views.
344 Q_ASSERT(urls
.count() == 1);
346 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
347 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
348 // is a benefit for the user at all -> let's wait for some input first...
349 RenameDialog
dialog(urls
);
350 if (dialog
.exec() == QDialog::Rejected
) {
354 const QString
& newName
= dialog
.newName();
355 if (newName
.isEmpty()) {
356 view
->statusBar()->setMessage(dialog
.errorString(),
357 DolphinStatusBar::Error
);
359 const KUrl
& oldUrl
= urls
.first();
360 KUrl newUrl
= oldUrl
;
361 newUrl
.setFileName(newName
);
362 m_mainWindow
->rename(oldUrl
, newUrl
);
367 void DolphinView::selectAll()
369 selectAll(QItemSelectionModel::Select
);
372 void DolphinView::invertSelection()
374 selectAll(QItemSelectionModel::Toggle
);
377 DolphinStatusBar
* DolphinView::statusBar() const
382 int DolphinView::contentsX() const
384 return itemView()->horizontalScrollBar()->value();
387 int DolphinView::contentsY() const
389 return itemView()->verticalScrollBar()->value();
392 void DolphinView::emitRequestItemInfo(const KUrl
& url
)
394 emit
requestItemInfo(url
);
397 bool DolphinView::isFilterBarVisible() const
399 return m_filterBar
->isVisible();
402 bool DolphinView::isUrlEditable() const
404 return m_urlNavigator
->isUrlEditable();
407 void DolphinView::zoomIn()
409 m_controller
->triggerZoomIn();
412 void DolphinView::zoomOut()
414 m_controller
->triggerZoomOut();
417 bool DolphinView::isZoomInPossible() const
419 return m_controller
->isZoomInPossible();
422 bool DolphinView::isZoomOutPossible() const
424 return m_controller
->isZoomOutPossible();
427 void DolphinView::setSorting(Sorting sorting
)
429 if (sorting
!= this->sorting()) {
430 updateSorting(sorting
);
434 DolphinView::Sorting
DolphinView::sorting() const
436 return m_proxyModel
->sorting();
439 void DolphinView::setSortOrder(Qt::SortOrder order
)
441 if (sortOrder() != order
) {
442 updateSortOrder(order
);
446 Qt::SortOrder
DolphinView::sortOrder() const
448 return m_proxyModel
->sortOrder();
451 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info
)
453 ViewProperties
props(m_urlNavigator
->url());
454 props
.setAdditionalInfo(info
);
456 m_fileItemDelegate
->setAdditionalInformation(info
);
458 emit
additionalInfoChanged(info
);
462 KFileItemDelegate::AdditionalInformation
DolphinView::additionalInfo() const
464 return m_fileItemDelegate
->additionalInformation();
467 void DolphinView::goBack()
469 m_urlNavigator
->goBack();
472 void DolphinView::goForward()
474 m_urlNavigator
->goForward();
477 void DolphinView::goUp()
479 m_urlNavigator
->goUp();
482 void DolphinView::goHome()
484 m_urlNavigator
->goHome();
487 void DolphinView::setUrlEditable(bool editable
)
489 m_urlNavigator
->setUrlEditable(editable
);
492 bool DolphinView::hasSelection() const
494 return itemView()->selectionModel()->hasSelection();
497 void DolphinView::clearSelection()
499 itemView()->selectionModel()->clear();
502 KFileItemList
DolphinView::selectedItems() const
504 const QAbstractItemView
* view
= itemView();
506 // Our view has a selection, we will map them back to the DirModel
507 // and then fill the KFileItemList.
508 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
510 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
511 KFileItemList itemList
;
513 const QModelIndexList indexList
= selection
.indexes();
514 QModelIndexList::const_iterator end
= indexList
.end();
515 for (QModelIndexList::const_iterator it
= indexList
.begin(); it
!= end
; ++it
) {
516 Q_ASSERT((*it
).isValid());
518 KFileItem
* item
= m_dirModel
->itemForIndex(*it
);
520 itemList
.append(item
);
527 KUrl::List
DolphinView::selectedUrls() const
531 const KFileItemList list
= selectedItems();
532 KFileItemList::const_iterator it
= list
.begin();
533 const KFileItemList::const_iterator end
= list
.end();
535 KFileItem
* item
= *it
;
536 urls
.append(item
->url());
543 KFileItem
* DolphinView::fileItem(const QModelIndex index
) const
545 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
546 return m_dirModel
->itemForIndex(dirModelIndex
);
549 void DolphinView::rename(const KUrl
& source
, const QString
& newName
)
553 if (newName
.isEmpty() || (source
.fileName() == newName
)) {
557 KUrl
dest(source
.upUrl());
558 dest
.addPath(newName
);
560 const bool destExists
= KIO::NetAccess::exists(dest
,
562 mainWindow()->activeView());
564 // the destination already exists, hence ask the user
566 KIO::RenameDialog
renameDialog(this,
567 i18n("File Already Exists"),
571 switch (renameDialog
.exec()) {
572 case KIO::R_OVERWRITE
:
573 // the destination should be overwritten
574 ok
= KIO::NetAccess::file_move(source
, dest
, -1, true);
577 case KIO::R_RENAME
: {
578 // a new name for the destination has been used
579 KUrl
newDest(renameDialog
.newDestUrl());
580 ok
= KIO::NetAccess::file_move(source
, newDest
);
585 // the renaming operation has been canceled
590 // no destination exists, hence just move the file to
592 ok
= KIO::NetAccess::file_move(source
, dest
);
595 const QString destFileName
= dest
.fileName();
597 m_statusBar
->setMessage(i18n("Renamed file '%1' to '%2'.", source
.fileName(), destFileName
),
598 DolphinStatusBar::OperationCompleted
);
600 KonqOperations::rename(this, source
, destFileName
);
602 m_statusBar
->setMessage(i18n("Renaming of file '%1' to '%2' failed.", source
.fileName(), destFileName
),
603 DolphinStatusBar::Error
);
608 void DolphinView::reload()
610 const KUrl
& url
= m_urlNavigator
->url();
611 changeDirectory(url
);
612 startDirLister(url
, true);
615 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
617 QWidget::mouseReleaseEvent(event
);
618 mainWindow()->setActiveView(this);
621 DolphinMainWindow
* DolphinView::mainWindow() const
626 void DolphinView::changeDirectory(const KUrl
& url
)
632 const ViewProperties
props(url
);
634 const Mode mode
= props
.viewMode();
635 bool changeMode
= (m_mode
!= mode
);
636 if (changeMode
&& isColumnViewActive()) {
637 // The column view is active. Only change the
638 // mode if the current URL is no child of the column view.
639 if (m_dirLister
->url().isParentOf(url
)) {
650 const bool showHiddenFiles
= props
.showHiddenFiles();
651 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
652 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
653 emit
showHiddenFilesChanged();
656 const bool categorized
= props
.categorizedSorting();
657 if (categorized
!= categorizedSorting()) {
658 if (supportsCategorizedSorting()) {
659 Q_ASSERT(m_iconsView
!= 0);
661 Q_ASSERT(m_iconsView
->itemCategorizer() == 0);
662 m_iconsView
->setItemCategorizer(new DolphinItemCategorizer());
664 KItemCategorizer
* categorizer
= m_iconsView
->itemCategorizer();
665 m_iconsView
->setItemCategorizer(0);
669 emit
categorizedSortingChanged();
672 const DolphinView::Sorting sorting
= props
.sorting();
673 if (sorting
!= m_proxyModel
->sorting()) {
674 m_proxyModel
->setSorting(sorting
);
675 emit
sortingChanged(sorting
);
678 const Qt::SortOrder sortOrder
= props
.sortOrder();
679 if (sortOrder
!= m_proxyModel
->sortOrder()) {
680 m_proxyModel
->setSortOrder(sortOrder
);
681 emit
sortOrderChanged(sortOrder
);
684 KFileItemDelegate::AdditionalInformation info
= props
.additionalInfo();
685 if (info
!= m_fileItemDelegate
->additionalInformation()) {
686 m_fileItemDelegate
->setAdditionalInformation(info
);
688 emit
additionalInfoChanged(info
);
691 const bool showPreview
= props
.showPreview();
692 if (showPreview
!= m_controller
->showPreview()) {
693 m_controller
->setShowPreview(showPreview
);
694 emit
showPreviewChanged();
698 emit
urlChanged(url
);
700 m_statusBar
->clear();
703 void DolphinView::triggerItem(const QModelIndex
& index
)
705 if (!isValidNameIndex(index
)) {
709 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
710 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
711 // items are selected by the user, hence don't trigger the
712 // item specified by 'index'
716 KFileItem
* item
= m_dirModel
->itemForIndex(m_proxyModel
->mapToSource(index
));
721 // Prefer the local path over the URL. This assures that the
722 // volume space information is correct. Assuming that the URL is media:/sda1,
723 // and the local path is /windows/C: For the URL the space info is related
724 // to the root partition (and hence wrong) and for the local path the space
725 // info is related to the windows partition (-> correct).
726 const QString
localPath(item
->localPath());
728 if (localPath
.isEmpty()) {
736 } else if (item
->isFile()) {
737 // allow to browse through ZIP and tar files
738 KMimeType::Ptr mime
= item
->mimeTypePtr();
739 if (mime
->is("application/zip")) {
740 url
.setProtocol("zip");
742 } else if (mime
->is("application/x-tar") ||
743 mime
->is("application/x-tarz") ||
744 mime
->is("application/x-bzip-compressed-tar") ||
745 mime
->is("application/x-compressed-tar") ||
746 mime
->is("application/x-tzo")) {
747 url
.setProtocol("tar");
757 void DolphinView::updateProgress(int percent
)
759 if (m_showProgress
) {
760 m_statusBar
->setProgress(percent
);
764 void DolphinView::updateItemCount()
766 if (m_showProgress
) {
767 m_statusBar
->setProgressText(QString());
768 m_statusBar
->setProgress(100);
769 m_showProgress
= false;
772 KFileItemList
items(m_dirLister
->items());
773 KFileItemList::const_iterator it
= items
.begin();
774 const KFileItemList::const_iterator end
= items
.end();
780 KFileItem
* item
= *it
;
791 m_blockContentsMovedSignal
= false;
792 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
795 void DolphinView::generatePreviews(const KFileItemList
& items
)
797 if (m_controller
->showPreview()) {
798 KIO::PreviewJob
* job
= KIO::filePreview(items
, 128);
799 connect(job
, SIGNAL(gotPreview(const KFileItem
*, const QPixmap
&)),
800 this, SLOT(showPreview(const KFileItem
*, const QPixmap
&)));
804 void DolphinView::showPreview(const KFileItem
* item
, const QPixmap
& pixmap
)
807 const QModelIndex idx
= m_dirModel
->indexForItem(*item
);
808 if (idx
.isValid() && (idx
.column() == 0)) {
809 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
810 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(*item
)) {
811 KIconEffect iconEffect
;
812 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
813 m_dirModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
815 m_dirModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
820 void DolphinView::restoreContentsPos()
822 KUrl currentUrl
= m_urlNavigator
->url();
823 if (!currentUrl
.isEmpty()) {
824 QAbstractItemView
* view
= itemView();
825 // TODO: view->setCurrentItem(m_urlNavigator->currentFileName());
826 QPoint pos
= m_urlNavigator
->savedPosition();
827 view
->horizontalScrollBar()->setValue(pos
.x());
828 view
->verticalScrollBar()->setValue(pos
.y());
832 void DolphinView::showInfoMessage(const QString
& msg
)
834 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
837 void DolphinView::showErrorMessage(const QString
& msg
)
839 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
842 void DolphinView::emitSelectionChangedSignal()
844 emit
selectionChanged(DolphinView::selectedItems());
847 void DolphinView::closeFilterBar()
850 emit
showFilterBarChanged(false);
853 void DolphinView::startDirLister(const KUrl
& url
, bool reload
)
855 if (!url
.isValid()) {
856 const QString
location(url
.pathOrUrl());
857 if (location
.isEmpty()) {
858 m_statusBar
->setMessage(i18n("The location is empty."), DolphinStatusBar::Error
);
860 m_statusBar
->setMessage(i18n("The location '%1' is invalid.", location
),
861 DolphinStatusBar::Error
);
866 // Only show the directory loading progress if the status bar does
867 // not contain another progress information. This means that
868 // the directory loading progress information has the lowest priority.
869 const QString
progressText(m_statusBar
->progressText());
870 m_showProgress
= progressText
.isEmpty() ||
871 (progressText
== i18n("Loading folder..."));
872 if (m_showProgress
) {
873 m_statusBar
->setProgressText(i18n("Loading folder..."));
874 m_statusBar
->setProgress(0);
877 m_cutItemsCache
.clear();
878 m_blockContentsMovedSignal
= true;
882 bool keepOldDirs
= isColumnViewActive();
887 const KUrl
& dirListerUrl
= m_dirLister
->url();
888 if (dirListerUrl
.isValid()) {
889 const KUrl::List dirs
= m_dirLister
->directories();
892 m_dirLister
->updateDirectory(url
);
896 } else if (m_dirLister
->directories().contains(url
)) {
897 // The dir lister contains the directory already, so
898 // KDirLister::openUrl() may not been invoked twice.
899 m_dirLister
->updateDirectory(url
);
902 const KUrl
& dirListerUrl
= m_dirLister
->url();
903 if ((dirListerUrl
== url
) || !m_dirLister
->url().isParentOf(url
)) {
904 // The current URL is not a child of the dir lister
905 // URL. This may happen when e. g. a bookmark has been selected
906 // and hence the view must be reset.
913 m_dirLister
->openUrl(url
, keepOldDirs
, reload
);
917 QString
DolphinView::defaultStatusBarText() const
919 return KIO::itemsSummaryString(m_fileCount
+ m_folderCount
,
925 QString
DolphinView::selectionStatusBarText() const
928 const KFileItemList list
= selectedItems();
929 if (list
.isEmpty()) {
930 // when an item is triggered, it is temporary selected but selectedItems()
931 // will return an empty list
937 KIO::filesize_t byteSize
= 0;
938 KFileItemList::const_iterator it
= list
.begin();
939 const KFileItemList::const_iterator end
= list
.end();
941 KFileItem
* item
= *it
;
946 byteSize
+= item
->size();
951 if (folderCount
> 0) {
952 text
= i18np("1 Folder selected", "%1 Folders selected", folderCount
);
959 const QString
sizeText(KIO::convertSize(byteSize
));
960 text
+= i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount
, sizeText
);
966 void DolphinView::showFilterBar(bool show
)
968 Q_ASSERT(m_filterBar
!= 0);
976 void DolphinView::updateStatusBar()
978 // As the item count information is less important
979 // in comparison with other messages, it should only
981 // - the status bar is empty or
982 // - shows already the item count information or
983 // - shows only a not very important information
984 // - if any progress is given don't show the item count info at all
985 const QString
msg(m_statusBar
->message());
986 const bool updateStatusBarMsg
= (msg
.isEmpty() ||
987 (msg
== m_statusBar
->defaultText()) ||
988 (m_statusBar
->type() == DolphinStatusBar::Information
)) &&
989 (m_statusBar
->progress() == 100);
991 const QString
text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
992 m_statusBar
->setDefaultText(text
);
994 if (updateStatusBarMsg
) {
995 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
999 void DolphinView::requestActivation()
1001 m_mainWindow
->setActiveView(this);
1004 void DolphinView::changeSelection(const KFileItemList
& selection
)
1007 if (selection
.isEmpty()) {
1010 KUrl baseUrl
= url();
1012 QItemSelection new_selection
;
1013 foreach(KFileItem
* item
, selection
) {
1014 url
= item
->url().upUrl();
1015 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
1016 QModelIndex index
= m_proxyModel
->mapFromSource(m_dirModel
->indexForItem(*item
));
1017 new_selection
.select(index
, index
);
1020 itemView()->selectionModel()->select(new_selection
,
1021 QItemSelectionModel::ClearAndSelect
1022 | QItemSelectionModel::Current
);
1025 void DolphinView::changeNameFilter(const QString
& nameFilter
)
1027 // The name filter of KDirLister does a 'hard' filtering, which
1028 // means that only the items are shown where the names match
1029 // exactly the filter. This is non-transparent for the user, which
1030 // just wants to have a 'soft' filtering: does the name contain
1031 // the filter string?
1032 QString
adjustedFilter(nameFilter
);
1033 adjustedFilter
.insert(0, '*');
1034 adjustedFilter
.append('*');
1036 // Use the ProxyModel to filter:
1037 // This code is #ifdefed as setNameFilter behaves
1038 // slightly different than the QSortFilterProxyModel
1039 // as it will not remove directories. I will ask
1040 // our beloved usability experts for input
1043 m_dirLister
->setNameFilter(adjustedFilter
);
1044 m_dirLister
->emitChanges();
1046 m_proxyModel
->setFilterRegExp(nameFilter
);
1050 void DolphinView::openContextMenu(const QPoint
& pos
)
1052 KFileItem
* item
= 0;
1054 const QModelIndex index
= itemView()->indexAt(pos
);
1055 if (isValidNameIndex(index
)) {
1056 item
= fileItem(index
);
1059 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url());
1063 void DolphinView::dropUrls(const KUrl::List
& urls
,
1064 const QModelIndex
& index
,
1067 KFileItem
* directory
= 0;
1068 if (isValidNameIndex(index
)) {
1069 KFileItem
* item
= fileItem(index
);
1070 Q_ASSERT(item
!= 0);
1071 if (item
->isDir()) {
1072 // the URLs are dropped above a directory
1077 if ((directory
== 0) && (source
== itemView())) {
1078 // The dropping is done into the same viewport where
1079 // the dragging has been started. Just ignore this...
1083 const KUrl
& destination
= (directory
== 0) ? url() :
1085 dropUrls(urls
, destination
);
1088 void DolphinView::dropUrls(const KUrl::List
& urls
,
1089 const KUrl
& destination
)
1091 m_mainWindow
->dropUrls(urls
, destination
);
1094 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
1096 ViewProperties
props(url());
1097 props
.setSorting(sorting
);
1099 m_proxyModel
->setSorting(sorting
);
1101 emit
sortingChanged(sorting
);
1104 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1106 ViewProperties
props(url());
1107 props
.setSortOrder(order
);
1109 m_proxyModel
->setSortOrder(order
);
1111 emit
sortOrderChanged(order
);
1114 void DolphinView::emitContentsMoved()
1116 if (!m_blockContentsMovedSignal
) {
1117 emit
contentsMoved(contentsX(), contentsY());
1121 void DolphinView::updateActivationState()
1123 m_urlNavigator
->setActive(isActive());
1125 emit
urlChanged(url());
1126 emit
selectionChanged(selectedItems());
1130 void DolphinView::updateCutItems()
1132 // restore the icons of all previously selected items to the
1133 // original state...
1134 QList
<CutItem
>::const_iterator it
= m_cutItemsCache
.begin();
1135 QList
<CutItem
>::const_iterator end
= m_cutItemsCache
.end();
1137 const QModelIndex index
= m_dirModel
->indexForUrl((*it
).url
);
1138 if (index
.isValid()) {
1139 m_dirModel
->setData(index
, QIcon((*it
).pixmap
), Qt::DecorationRole
);
1143 m_cutItemsCache
.clear();
1145 // ... and apply an item effect to all currently cut items
1146 applyCutItemEffect();
1149 void DolphinView::createView()
1151 // delete current view
1152 QAbstractItemView
* view
= itemView();
1154 m_topLayout
->removeWidget(view
);
1156 if (view
== m_iconsView
) {
1157 KItemCategorizer
* categorizer
= m_iconsView
->itemCategorizer();
1158 m_iconsView
->setItemCategorizer(0);
1161 view
->deleteLater();
1166 m_fileItemDelegate
= 0;
1169 Q_ASSERT(m_iconsView
== 0);
1170 Q_ASSERT(m_detailsView
== 0);
1171 Q_ASSERT(m_columnView
== 0);
1173 // ... and recreate it representing the current mode
1176 m_iconsView
= new DolphinIconsView(this, m_controller
);
1181 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1182 view
= m_detailsView
;
1186 m_columnView
= new DolphinColumnView(this, m_controller
);
1187 view
= m_columnView
;
1191 Q_ASSERT(view
!= 0);
1193 m_fileItemDelegate
= new KFileItemDelegate(view
);
1194 view
->setItemDelegate(m_fileItemDelegate
);
1196 view
->setModel(m_proxyModel
);
1197 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1199 new KMimeTypeResolver(view
, m_dirModel
);
1200 m_topLayout
->insertWidget(1, view
);
1202 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1203 m_controller
, SLOT(indicateSelectionChange()));
1204 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1205 this, SLOT(emitContentsMoved()));
1206 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1207 this, SLOT(emitContentsMoved()));
1210 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags
)
1212 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
1213 const QAbstractItemModel
* itemModel
= selectionModel
->model();
1215 const QModelIndex topLeft
= itemModel
->index(0, 0);
1216 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
1217 itemModel
->columnCount() - 1);
1219 QItemSelection
selection(topLeft
, bottomRight
);
1220 selectionModel
->select(selection
, flags
);
1223 QAbstractItemView
* DolphinView::itemView() const
1225 if (m_detailsView
!= 0) {
1226 return m_detailsView
;
1227 } else if (m_columnView
!= 0) {
1228 return m_columnView
;
1234 bool DolphinView::isValidNameIndex(const QModelIndex
& index
) const
1236 return index
.isValid() && (index
.column() == KDirModel::Name
);
1239 bool DolphinView::isCutItem(const KFileItem
& item
) const
1241 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1242 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1244 const KUrl
& itemUrl
= item
.url();
1245 KUrl::List::const_iterator it
= cutUrls
.begin();
1246 const KUrl::List::const_iterator end
= cutUrls
.end();
1248 if (*it
== itemUrl
) {
1257 void DolphinView::applyCutItemEffect()
1259 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1260 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
1264 KFileItemList
items(m_dirLister
->items());
1265 KFileItemList::const_iterator it
= items
.begin();
1266 const KFileItemList::const_iterator end
= items
.end();
1268 KFileItem
* item
= *it
;
1269 if (isCutItem(*item
)) {
1270 const QModelIndex index
= m_dirModel
->indexForItem(*item
);
1271 const KFileItem
* item
= m_dirModel
->itemForIndex(index
);
1272 const QVariant value
= m_dirModel
->data(index
, Qt::DecorationRole
);
1273 if ((value
.type() == QVariant::Icon
) && (item
!= 0)) {
1274 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
1275 QPixmap pixmap
= icon
.pixmap(128, 128);
1277 // remember current pixmap for the item to be able
1278 // to restore it when other items get cut
1280 cutItem
.url
= item
->url();
1281 cutItem
.pixmap
= pixmap
;
1282 m_cutItemsCache
.append(cutItem
);
1284 // apply icon effect to the cut item
1285 KIconEffect iconEffect
;
1286 pixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
1287 m_dirModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
1294 #include "dolphinview.moc"