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(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(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 startDirLister(m_urlNavigator
->url(), true);
613 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
615 QWidget::mouseReleaseEvent(event
);
616 mainWindow()->setActiveView(this);
619 DolphinMainWindow
* DolphinView::mainWindow() const
624 void DolphinView::loadDirectory(const KUrl
& url
)
630 const ViewProperties
props(url
);
632 const Mode mode
= props
.viewMode();
633 bool changeMode
= (m_mode
!= mode
);
634 if (changeMode
&& isColumnViewActive()) {
635 // The column view is active. Only change the
636 // mode if the current URL is no child of the column view.
637 if (m_dirLister
->url().isParentOf(url
)) {
648 const bool showHiddenFiles
= props
.showHiddenFiles();
649 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
650 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
651 emit
showHiddenFilesChanged();
654 const bool categorized
= props
.categorizedSorting();
655 if (categorized
!= categorizedSorting()) {
656 if (supportsCategorizedSorting()) {
657 Q_ASSERT(m_iconsView
!= 0);
659 Q_ASSERT(m_iconsView
->itemCategorizer() == 0);
660 m_iconsView
->setItemCategorizer(new DolphinItemCategorizer());
662 KItemCategorizer
* categorizer
= m_iconsView
->itemCategorizer();
663 m_iconsView
->setItemCategorizer(0);
667 emit
categorizedSortingChanged();
670 const DolphinView::Sorting sorting
= props
.sorting();
671 if (sorting
!= m_proxyModel
->sorting()) {
672 m_proxyModel
->setSorting(sorting
);
673 emit
sortingChanged(sorting
);
676 const Qt::SortOrder sortOrder
= props
.sortOrder();
677 if (sortOrder
!= m_proxyModel
->sortOrder()) {
678 m_proxyModel
->setSortOrder(sortOrder
);
679 emit
sortOrderChanged(sortOrder
);
682 KFileItemDelegate::AdditionalInformation info
= props
.additionalInfo();
683 if (info
!= m_fileItemDelegate
->additionalInformation()) {
684 m_fileItemDelegate
->setAdditionalInformation(info
);
686 emit
additionalInfoChanged(info
);
689 const bool showPreview
= props
.showPreview();
690 if (showPreview
!= m_controller
->showPreview()) {
691 m_controller
->setShowPreview(showPreview
);
692 emit
showPreviewChanged();
696 emit
urlChanged(url
);
698 m_statusBar
->clear();
701 void DolphinView::triggerItem(const QModelIndex
& index
)
703 if (!isValidNameIndex(index
)) {
707 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
708 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
709 // items are selected by the user, hence don't trigger the
710 // item specified by 'index'
714 KFileItem
* item
= m_dirModel
->itemForIndex(m_proxyModel
->mapToSource(index
));
719 // Prefer the local path over the URL. This assures that the
720 // volume space information is correct. Assuming that the URL is media:/sda1,
721 // and the local path is /windows/C: For the URL the space info is related
722 // to the root partition (and hence wrong) and for the local path the space
723 // info is related to the windows partition (-> correct).
724 const QString
localPath(item
->localPath());
726 if (localPath
.isEmpty()) {
734 } else if (item
->isFile()) {
735 // allow to browse through ZIP and tar files
736 KMimeType::Ptr mime
= item
->mimeTypePtr();
737 if (mime
->is("application/zip")) {
738 url
.setProtocol("zip");
740 } else if (mime
->is("application/x-tar") ||
741 mime
->is("application/x-tarz") ||
742 mime
->is("application/x-bzip-compressed-tar") ||
743 mime
->is("application/x-compressed-tar") ||
744 mime
->is("application/x-tzo")) {
745 url
.setProtocol("tar");
755 void DolphinView::updateProgress(int percent
)
757 if (m_showProgress
) {
758 m_statusBar
->setProgress(percent
);
762 void DolphinView::updateItemCount()
764 if (m_showProgress
) {
765 m_statusBar
->setProgressText(QString());
766 m_statusBar
->setProgress(100);
767 m_showProgress
= false;
770 KFileItemList
items(m_dirLister
->items());
771 KFileItemList::const_iterator it
= items
.begin();
772 const KFileItemList::const_iterator end
= items
.end();
778 KFileItem
* item
= *it
;
789 m_blockContentsMovedSignal
= false;
790 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
793 void DolphinView::generatePreviews(const KFileItemList
& items
)
795 if (m_controller
->showPreview()) {
796 KIO::PreviewJob
* job
= KIO::filePreview(items
, 128);
797 connect(job
, SIGNAL(gotPreview(const KFileItem
*, const QPixmap
&)),
798 this, SLOT(showPreview(const KFileItem
*, const QPixmap
&)));
802 void DolphinView::showPreview(const KFileItem
* item
, const QPixmap
& pixmap
)
805 const QModelIndex idx
= m_dirModel
->indexForItem(*item
);
806 if (idx
.isValid() && (idx
.column() == 0)) {
807 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
808 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(*item
)) {
809 KIconEffect iconEffect
;
810 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
811 m_dirModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
813 m_dirModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
818 void DolphinView::restoreContentsPos()
820 KUrl currentUrl
= m_urlNavigator
->url();
821 if (!currentUrl
.isEmpty()) {
822 QAbstractItemView
* view
= itemView();
823 // TODO: view->setCurrentItem(m_urlNavigator->currentFileName());
824 QPoint pos
= m_urlNavigator
->savedPosition();
825 view
->horizontalScrollBar()->setValue(pos
.x());
826 view
->verticalScrollBar()->setValue(pos
.y());
830 void DolphinView::showInfoMessage(const QString
& msg
)
832 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
835 void DolphinView::showErrorMessage(const QString
& msg
)
837 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
840 void DolphinView::emitSelectionChangedSignal()
842 emit
selectionChanged(DolphinView::selectedItems());
845 void DolphinView::closeFilterBar()
848 emit
showFilterBarChanged(false);
851 void DolphinView::startDirLister(const KUrl
& url
, bool reload
)
853 if (!url
.isValid()) {
854 const QString
location(url
.pathOrUrl());
855 if (location
.isEmpty()) {
856 m_statusBar
->setMessage(i18n("The location is empty."), DolphinStatusBar::Error
);
858 m_statusBar
->setMessage(i18n("The location '%1' is invalid.", location
),
859 DolphinStatusBar::Error
);
864 // Only show the directory loading progress if the status bar does
865 // not contain another progress information. This means that
866 // the directory loading progress information has the lowest priority.
867 const QString
progressText(m_statusBar
->progressText());
868 m_showProgress
= progressText
.isEmpty() ||
869 (progressText
== i18n("Loading directory..."));
870 if (m_showProgress
) {
871 m_statusBar
->setProgressText(i18n("Loading directory..."));
872 m_statusBar
->setProgress(0);
875 m_cutItemsCache
.clear();
876 m_blockContentsMovedSignal
= true;
880 bool keepOldDirs
= isColumnViewActive();
885 const KUrl
& dirListerUrl
= m_dirLister
->url();
886 if (dirListerUrl
.isValid()) {
887 const KUrl::List dirs
= m_dirLister
->directories();
890 m_dirLister
->updateDirectory(url
);
894 } else if (m_dirLister
->directories().contains(url
)) {
895 // The dir lister contains the directory already, so
896 // KDirLister::openUrl() may not been invoked twice.
897 m_dirLister
->updateDirectory(url
);
900 const KUrl
& dirListerUrl
= m_dirLister
->url();
901 if ((dirListerUrl
== url
) || !m_dirLister
->url().isParentOf(url
)) {
902 // The current URL is not a child of the dir lister
903 // URL. This may happen when e. g. a bookmark has been selected
904 // and hence the view must be reset.
911 m_dirLister
->openUrl(url
, keepOldDirs
, reload
);
915 QString
DolphinView::defaultStatusBarText() const
917 return KIO::itemsSummaryString(m_fileCount
+ m_folderCount
,
923 QString
DolphinView::selectionStatusBarText() const
926 const KFileItemList list
= selectedItems();
927 if (list
.isEmpty()) {
928 // when an item is triggered, it is temporary selected but selectedItems()
929 // will return an empty list
935 KIO::filesize_t byteSize
= 0;
936 KFileItemList::const_iterator it
= list
.begin();
937 const KFileItemList::const_iterator end
= list
.end();
939 KFileItem
* item
= *it
;
944 byteSize
+= item
->size();
949 if (folderCount
> 0) {
950 text
= i18np("1 Folder selected", "%1 Folders selected", folderCount
);
957 const QString
sizeText(KIO::convertSize(byteSize
));
958 text
+= i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount
, sizeText
);
964 void DolphinView::showFilterBar(bool show
)
966 Q_ASSERT(m_filterBar
!= 0);
974 void DolphinView::updateStatusBar()
976 // As the item count information is less important
977 // in comparison with other messages, it should only
979 // - the status bar is empty or
980 // - shows already the item count information or
981 // - shows only a not very important information
982 // - if any progress is given don't show the item count info at all
983 const QString
msg(m_statusBar
->message());
984 const bool updateStatusBarMsg
= (msg
.isEmpty() ||
985 (msg
== m_statusBar
->defaultText()) ||
986 (m_statusBar
->type() == DolphinStatusBar::Information
)) &&
987 (m_statusBar
->progress() == 100);
989 const QString
text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
990 m_statusBar
->setDefaultText(text
);
992 if (updateStatusBarMsg
) {
993 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
997 void DolphinView::requestActivation()
999 m_mainWindow
->setActiveView(this);
1002 void DolphinView::changeSelection(const KFileItemList
& selection
)
1005 if (selection
.isEmpty()) {
1008 KUrl baseUrl
= url();
1010 QItemSelection new_selection
;
1011 foreach(KFileItem
* item
, selection
) {
1012 url
= item
->url().upUrl();
1013 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
1014 QModelIndex index
= m_proxyModel
->mapFromSource(m_dirModel
->indexForItem(*item
));
1015 new_selection
.select(index
, index
);
1018 itemView()->selectionModel()->select(new_selection
,
1019 QItemSelectionModel::ClearAndSelect
1020 | QItemSelectionModel::Current
);
1023 void DolphinView::changeNameFilter(const QString
& nameFilter
)
1025 // The name filter of KDirLister does a 'hard' filtering, which
1026 // means that only the items are shown where the names match
1027 // exactly the filter. This is non-transparent for the user, which
1028 // just wants to have a 'soft' filtering: does the name contain
1029 // the filter string?
1030 QString
adjustedFilter(nameFilter
);
1031 adjustedFilter
.insert(0, '*');
1032 adjustedFilter
.append('*');
1034 // Use the ProxyModel to filter:
1035 // This code is #ifdefed as setNameFilter behaves
1036 // slightly different than the QSortFilterProxyModel
1037 // as it will not remove directories. I will ask
1038 // our beloved usability experts for input
1041 m_dirLister
->setNameFilter(adjustedFilter
);
1042 m_dirLister
->emitChanges();
1044 m_proxyModel
->setFilterRegExp(nameFilter
);
1048 void DolphinView::openContextMenu(const QPoint
& pos
)
1050 KFileItem
* item
= 0;
1052 const QModelIndex index
= itemView()->indexAt(pos
);
1053 if (isValidNameIndex(index
)) {
1054 item
= fileItem(index
);
1057 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url());
1061 void DolphinView::dropUrls(const KUrl::List
& urls
,
1062 const QModelIndex
& index
,
1065 KFileItem
* directory
= 0;
1066 if (isValidNameIndex(index
)) {
1067 KFileItem
* item
= fileItem(index
);
1068 Q_ASSERT(item
!= 0);
1069 if (item
->isDir()) {
1070 // the URLs are dropped above a directory
1075 if ((directory
== 0) && (source
== itemView())) {
1076 // The dropping is done into the same viewport where
1077 // the dragging has been started. Just ignore this...
1081 const KUrl
& destination
= (directory
== 0) ? url() :
1083 dropUrls(urls
, destination
);
1086 void DolphinView::dropUrls(const KUrl::List
& urls
,
1087 const KUrl
& destination
)
1089 m_mainWindow
->dropUrls(urls
, destination
);
1092 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
1094 ViewProperties
props(url());
1095 props
.setSorting(sorting
);
1097 m_proxyModel
->setSorting(sorting
);
1099 emit
sortingChanged(sorting
);
1102 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1104 ViewProperties
props(url());
1105 props
.setSortOrder(order
);
1107 m_proxyModel
->setSortOrder(order
);
1109 emit
sortOrderChanged(order
);
1112 void DolphinView::emitContentsMoved()
1114 if (!m_blockContentsMovedSignal
) {
1115 emit
contentsMoved(contentsX(), contentsY());
1119 void DolphinView::updateActivationState()
1121 m_urlNavigator
->setActive(isActive());
1123 emit
urlChanged(url());
1124 emit
selectionChanged(selectedItems());
1128 void DolphinView::updateCutItems()
1130 // restore the icons of all previously selected items to the
1131 // original state...
1132 QList
<CutItem
>::const_iterator it
= m_cutItemsCache
.begin();
1133 QList
<CutItem
>::const_iterator end
= m_cutItemsCache
.end();
1135 const QModelIndex index
= m_dirModel
->indexForUrl((*it
).url
);
1136 if (index
.isValid()) {
1137 m_dirModel
->setData(index
, QIcon((*it
).pixmap
), Qt::DecorationRole
);
1141 m_cutItemsCache
.clear();
1143 // ... and apply an item effect to all currently cut items
1144 applyCutItemEffect();
1147 void DolphinView::createView()
1149 // delete current view
1150 QAbstractItemView
* view
= itemView();
1152 m_topLayout
->removeWidget(view
);
1154 if (view
== m_iconsView
) {
1155 KItemCategorizer
* categorizer
= m_iconsView
->itemCategorizer();
1156 m_iconsView
->setItemCategorizer(0);
1159 view
->deleteLater();
1164 m_fileItemDelegate
= 0;
1167 Q_ASSERT(m_iconsView
== 0);
1168 Q_ASSERT(m_detailsView
== 0);
1169 Q_ASSERT(m_columnView
== 0);
1171 // ... and recreate it representing the current mode
1174 m_iconsView
= new DolphinIconsView(this, m_controller
);
1179 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1180 view
= m_detailsView
;
1184 m_columnView
= new DolphinColumnView(this, m_controller
);
1185 view
= m_columnView
;
1189 Q_ASSERT(view
!= 0);
1191 m_fileItemDelegate
= new KFileItemDelegate(view
);
1192 view
->setItemDelegate(m_fileItemDelegate
);
1194 view
->setModel(m_proxyModel
);
1195 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1197 new KMimeTypeResolver(view
, m_dirModel
);
1198 m_topLayout
->insertWidget(1, view
);
1200 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1201 m_controller
, SLOT(indicateSelectionChange()));
1202 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1203 this, SLOT(emitContentsMoved()));
1204 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1205 this, SLOT(emitContentsMoved()));
1208 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags
)
1210 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
1211 const QAbstractItemModel
* itemModel
= selectionModel
->model();
1213 const QModelIndex topLeft
= itemModel
->index(0, 0);
1214 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
1215 itemModel
->columnCount() - 1);
1217 QItemSelection
selection(topLeft
, bottomRight
);
1218 selectionModel
->select(selection
, flags
);
1221 QAbstractItemView
* DolphinView::itemView() const
1223 if (m_detailsView
!= 0) {
1224 return m_detailsView
;
1225 } else if (m_columnView
!= 0) {
1226 return m_columnView
;
1232 bool DolphinView::isValidNameIndex(const QModelIndex
& index
) const
1234 return index
.isValid() && (index
.column() == KDirModel::Name
);
1237 bool DolphinView::isCutItem(const KFileItem
& item
) const
1239 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1240 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1242 const KUrl
& itemUrl
= item
.url();
1243 KUrl::List::const_iterator it
= cutUrls
.begin();
1244 const KUrl::List::const_iterator end
= cutUrls
.end();
1246 if (*it
== itemUrl
) {
1255 void DolphinView::applyCutItemEffect()
1257 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1258 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
1262 KFileItemList
items(m_dirLister
->items());
1263 KFileItemList::const_iterator it
= items
.begin();
1264 const KFileItemList::const_iterator end
= items
.end();
1266 KFileItem
* item
= *it
;
1267 if (isCutItem(*item
)) {
1268 const QModelIndex index
= m_dirModel
->indexForItem(*item
);
1269 const KFileItem
* item
= m_dirModel
->itemForIndex(index
);
1270 const QVariant value
= m_dirModel
->data(index
, Qt::DecorationRole
);
1271 if ((value
.type() == QVariant::Icon
) && (item
!= 0)) {
1272 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
1273 QPixmap pixmap
= icon
.pixmap(128, 128);
1275 // remember current pixmap for the item to be able
1276 // to restore it when other items get cut
1278 cutItem
.url
= item
->url();
1279 cutItem
.pixmap
= pixmap
;
1280 m_cutItemsCache
.append(cutItem
);
1282 // apply icon effect to the cut item
1283 KIconEffect iconEffect
;
1284 pixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
1285 m_dirModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
1292 #include "dolphinview.moc"