1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "dolphinview.h"
23 #include <QApplication>
26 #include <QItemSelectionModel>
27 #include <QMouseEvent>
28 #include <QVBoxLayout>
32 #include <kdirmodel.h>
33 #include <kfileitemdelegate.h>
34 #include <kfileplacesmodel.h>
36 #include <kiconeffect.h>
37 #include <kio/netaccess.h>
38 #include <kio/renamedialog.h>
39 #include <kio/previewjob.h>
40 #include <kmimetyperesolver.h>
41 #include <konqmimedata.h>
42 #include <konq_operations.h>
45 #include "dolphincolumnview.h"
46 #include "dolphincontroller.h"
47 #include "dolphinstatusbar.h"
48 #include "dolphinmainwindow.h"
49 #include "dolphindirlister.h"
50 #include "dolphinsortfilterproxymodel.h"
51 #include "dolphindetailsview.h"
52 #include "dolphiniconsview.h"
53 #include "dolphincontextmenu.h"
54 #include "filterbar.h"
55 #include "renamedialog.h"
56 #include "kurlnavigator.h"
57 #include "viewproperties.h"
58 #include "dolphinsettings.h"
59 #include "dolphin_generalsettings.h"
61 DolphinView::DolphinView(DolphinMainWindow
* mainWindow
,
65 bool showHiddenFiles
) :
67 m_showProgress(false),
68 m_blockContentsMovedSignal(false),
73 m_mainWindow(mainWindow
),
80 m_fileItemDelegate(0),
88 setFocusPolicy(Qt::StrongFocus
);
89 m_topLayout
= new QVBoxLayout(this);
90 m_topLayout
->setSpacing(0);
91 m_topLayout
->setMargin(0);
93 connect(m_mainWindow
, SIGNAL(activeViewChanged()),
94 this, SLOT(updateActivationState()));
96 QClipboard
* clipboard
= QApplication::clipboard();
97 connect(clipboard
, SIGNAL(dataChanged()),
98 this, SLOT(updateCutItems()));
100 m_urlNavigator
= new KUrlNavigator(DolphinSettings::instance().placesModel(), url
, this);
101 m_urlNavigator
->setUrlEditable(DolphinSettings::instance().generalSettings()->editableUrl());
102 m_urlNavigator
->setHomeUrl(DolphinSettings::instance().generalSettings()->homeUrl());
103 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
104 this, SLOT(loadDirectory(const KUrl
&)));
105 connect(m_urlNavigator
, SIGNAL(urlsDropped(const KUrl::List
&, const KUrl
&)),
106 this, SLOT(dropUrls(const KUrl::List
&, const KUrl
&)));
107 connect(m_urlNavigator
, SIGNAL(activated()),
108 this, SLOT(requestActivation()));
109 connect(this, SIGNAL(contentsMoved(int, int)),
110 m_urlNavigator
, SLOT(savePosition(int, int)));
112 m_statusBar
= new DolphinStatusBar(this);
114 m_dirLister
= new DolphinDirLister();
115 m_dirLister
->setAutoUpdate(true);
116 m_dirLister
->setMainWindow(this);
117 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
118 m_dirLister
->setDelayedMimeTypes(true);
120 connect(m_dirLister
, SIGNAL(clear()),
121 this, SLOT(updateStatusBar()));
122 connect(m_dirLister
, SIGNAL(percent(int)),
123 this, SLOT(updateProgress(int)));
124 connect(m_dirLister
, SIGNAL(deleteItem(KFileItem
*)),
125 this, SLOT(updateStatusBar()));
126 connect(m_dirLister
, SIGNAL(completed()),
127 this, SLOT(updateItemCount()));
128 connect(m_dirLister
, SIGNAL(completed()),
129 this, SLOT(updateCutItems()));
130 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
131 this, SLOT(generatePreviews(const KFileItemList
&)));
132 connect(m_dirLister
, SIGNAL(infoMessage(const QString
&)),
133 this, SLOT(showInfoMessage(const QString
&)));
134 connect(m_dirLister
, SIGNAL(errorMessage(const QString
&)),
135 this, SLOT(showErrorMessage(const QString
&)));
137 m_dirModel
= new KDirModel();
138 m_dirModel
->setDirLister(m_dirLister
);
139 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
141 m_proxyModel
= new DolphinSortFilterProxyModel(this);
142 m_proxyModel
->setSourceModel(m_dirModel
);
144 m_controller
= new DolphinController(this);
145 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&)),
146 this, SLOT(openContextMenu(const QPoint
&)));
147 connect(m_controller
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&, QWidget
*)),
148 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&, QWidget
*)));
149 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
150 this, SLOT(updateSorting(DolphinView::Sorting
)));
151 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
152 this, SLOT(updateSortOrder(Qt::SortOrder
)));
153 connect(m_controller
, SIGNAL(itemTriggered(const QModelIndex
&)),
154 this, SLOT(triggerItem(const QModelIndex
&)));
155 connect(m_controller
, SIGNAL(selectionChanged()),
156 this, SLOT(emitSelectionChangedSignal()));
157 connect(m_controller
, SIGNAL(activated()),
158 this, SLOT(requestActivation()));
162 m_iconSize
= K3Icon::SizeMedium
;
164 m_filterBar
= new FilterBar(this);
166 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
167 this, SLOT(changeNameFilter(const QString
&)));
168 connect(m_filterBar
, SIGNAL(closeRequest()),
169 this, SLOT(closeFilterBar()));
171 m_topLayout
->addWidget(m_urlNavigator
);
172 m_topLayout
->addWidget(itemView());
173 m_topLayout
->addWidget(m_filterBar
);
174 m_topLayout
->addWidget(m_statusBar
);
177 DolphinView::~DolphinView()
183 void DolphinView::setUrl(const KUrl
& url
)
185 m_urlNavigator
->setUrl(url
);
186 m_controller
->setUrl(url
);
189 const KUrl
& DolphinView::url() const
191 return m_urlNavigator
->url();
194 bool DolphinView::isActive() const
196 return m_mainWindow
->activeView() == this;
199 void DolphinView::setMode(Mode mode
)
201 if (mode
== m_mode
) {
202 return; // the wished mode is already set
207 ViewProperties
props(m_urlNavigator
->url());
208 props
.setViewMode(m_mode
);
211 startDirLister(m_urlNavigator
->url());
216 DolphinView::Mode
DolphinView::mode() const
221 void DolphinView::setShowPreview(bool show
)
223 ViewProperties
props(m_urlNavigator
->url());
224 props
.setShowPreview(show
);
226 m_controller
->setShowPreview(show
);
228 emit
showPreviewChanged();
232 bool DolphinView::showPreview() const
234 return m_controller
->showPreview();
237 void DolphinView::setShowHiddenFiles(bool show
)
239 if (m_dirLister
->showingDotFiles() == show
) {
243 ViewProperties
props(m_urlNavigator
->url());
244 props
.setShowHiddenFiles(show
);
247 m_dirLister
->setShowingDotFiles(show
);
248 emit
showHiddenFilesChanged();
253 bool DolphinView::showHiddenFiles() const
255 return m_dirLister
->showingDotFiles();
258 void DolphinView::renameSelectedItems()
260 DolphinView
* view
= mainWindow()->activeView();
261 const KUrl::List urls
= selectedUrls();
262 if (urls
.count() > 1) {
263 // More than one item has been selected for renaming. Open
264 // a rename dialog and rename all items afterwards.
265 RenameDialog
dialog(urls
);
266 if (dialog
.exec() == QDialog::Rejected
) {
270 const QString
& newName
= dialog
.newName();
271 if (newName
.isEmpty()) {
272 view
->statusBar()->setMessage(dialog
.errorString(),
273 DolphinStatusBar::Error
);
275 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
276 // as one operation instead of n rename operations like it is done now...
277 Q_ASSERT(newName
.contains('#'));
279 // iterate through all selected items and rename them...
280 const int replaceIndex
= newName
.indexOf('#');
281 Q_ASSERT(replaceIndex
>= 0);
284 KUrl::List::const_iterator it
= urls
.begin();
285 KUrl::List::const_iterator end
= urls
.end();
287 const KUrl
& oldUrl
= *it
;
289 number
.setNum(index
++);
291 QString
name(newName
);
292 name
.replace(replaceIndex
, 1, number
);
294 if (oldUrl
.fileName() != name
) {
295 KUrl newUrl
= oldUrl
;
296 newUrl
.setFileName(name
);
297 m_mainWindow
->rename(oldUrl
, newUrl
);
303 // Only one item has been selected for renaming. Use the custom
304 // renaming mechanism from the views.
305 Q_ASSERT(urls
.count() == 1);
307 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
308 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
309 // is a benefit for the user at all -> let's wait for some input first...
310 RenameDialog
dialog(urls
);
311 if (dialog
.exec() == QDialog::Rejected
) {
315 const QString
& newName
= dialog
.newName();
316 if (newName
.isEmpty()) {
317 view
->statusBar()->setMessage(dialog
.errorString(),
318 DolphinStatusBar::Error
);
320 const KUrl
& oldUrl
= urls
.first();
321 KUrl newUrl
= oldUrl
;
322 newUrl
.setFileName(newName
);
323 m_mainWindow
->rename(oldUrl
, newUrl
);
328 void DolphinView::selectAll()
330 selectAll(QItemSelectionModel::Select
);
333 void DolphinView::invertSelection()
335 selectAll(QItemSelectionModel::Toggle
);
338 DolphinStatusBar
* DolphinView::statusBar() const
343 int DolphinView::contentsX() const
345 return itemView()->horizontalScrollBar()->value();
348 int DolphinView::contentsY() const
350 return itemView()->verticalScrollBar()->value();
353 void DolphinView::emitRequestItemInfo(const KUrl
& url
)
355 emit
requestItemInfo(url
);
358 bool DolphinView::isFilterBarVisible() const
360 return m_filterBar
->isVisible();
363 bool DolphinView::isUrlEditable() const
365 return m_urlNavigator
->isUrlEditable();
368 void DolphinView::zoomIn()
370 m_controller
->triggerZoomIn();
373 void DolphinView::zoomOut()
375 m_controller
->triggerZoomOut();
378 bool DolphinView::isZoomInPossible() const
380 return m_controller
->isZoomInPossible();
383 bool DolphinView::isZoomOutPossible() const
385 return m_controller
->isZoomOutPossible();
388 void DolphinView::setSorting(Sorting sorting
)
390 if (sorting
!= this->sorting()) {
391 updateSorting(sorting
);
395 DolphinView::Sorting
DolphinView::sorting() const
397 return m_proxyModel
->sorting();
400 void DolphinView::setSortOrder(Qt::SortOrder order
)
402 if (sortOrder() != order
) {
403 updateSortOrder(order
);
407 Qt::SortOrder
DolphinView::sortOrder() const
409 return m_proxyModel
->sortOrder();
412 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info
)
414 ViewProperties
props(m_urlNavigator
->url());
415 props
.setAdditionalInfo(info
);
417 m_fileItemDelegate
->setAdditionalInformation(info
);
419 emit
additionalInfoChanged(info
);
423 KFileItemDelegate::AdditionalInformation
DolphinView::additionalInfo() const
425 return m_fileItemDelegate
->additionalInformation();
428 void DolphinView::goBack()
430 m_urlNavigator
->goBack();
433 void DolphinView::goForward()
435 m_urlNavigator
->goForward();
438 void DolphinView::goUp()
440 m_urlNavigator
->goUp();
443 void DolphinView::goHome()
445 m_urlNavigator
->goHome();
448 void DolphinView::setUrlEditable(bool editable
)
450 m_urlNavigator
->setUrlEditable(editable
);
453 bool DolphinView::hasSelection() const
455 return itemView()->selectionModel()->hasSelection();
458 void DolphinView::clearSelection()
460 itemView()->selectionModel()->clear();
463 KFileItemList
DolphinView::selectedItems() const
465 const QAbstractItemView
* view
= itemView();
467 // Our view has a selection, we will map them back to the DirModel
468 // and then fill the KFileItemList.
469 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
471 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
472 KFileItemList itemList
;
474 const QModelIndexList indexList
= selection
.indexes();
475 QModelIndexList::const_iterator end
= indexList
.end();
476 for (QModelIndexList::const_iterator it
= indexList
.begin(); it
!= end
; ++it
) {
477 Q_ASSERT((*it
).isValid());
479 KFileItem
* item
= m_dirModel
->itemForIndex(*it
);
481 itemList
.append(item
);
488 KUrl::List
DolphinView::selectedUrls() const
492 const KFileItemList list
= selectedItems();
493 KFileItemList::const_iterator it
= list
.begin();
494 const KFileItemList::const_iterator end
= list
.end();
496 KFileItem
* item
= *it
;
497 urls
.append(item
->url());
504 KFileItem
* DolphinView::fileItem(const QModelIndex index
) const
506 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
507 return m_dirModel
->itemForIndex(dirModelIndex
);
510 void DolphinView::rename(const KUrl
& source
, const QString
& newName
)
514 if (newName
.isEmpty() || (source
.fileName() == newName
)) {
518 KUrl
dest(source
.upUrl());
519 dest
.addPath(newName
);
521 const bool destExists
= KIO::NetAccess::exists(dest
,
523 mainWindow()->activeView());
525 // the destination already exists, hence ask the user
527 KIO::RenameDialog
renameDialog(this,
528 i18n("File Already Exists"),
532 switch (renameDialog
.exec()) {
533 case KIO::R_OVERWRITE
:
534 // the destination should be overwritten
535 ok
= KIO::NetAccess::file_move(source
, dest
, -1, true);
538 case KIO::R_RENAME
: {
539 // a new name for the destination has been used
540 KUrl
newDest(renameDialog
.newDestUrl());
541 ok
= KIO::NetAccess::file_move(source
, newDest
);
546 // the renaming operation has been canceled
551 // no destination exists, hence just move the file to
553 ok
= KIO::NetAccess::file_move(source
, dest
);
556 const QString destFileName
= dest
.fileName();
558 m_statusBar
->setMessage(i18n("Renamed file '%1' to '%2'.", source
.fileName(), destFileName
),
559 DolphinStatusBar::OperationCompleted
);
561 KonqOperations::rename(this, source
, destFileName
);
563 m_statusBar
->setMessage(i18n("Renaming of file '%1' to '%2' failed.", source
.fileName(), destFileName
),
564 DolphinStatusBar::Error
);
569 void DolphinView::reload()
571 startDirLister(m_urlNavigator
->url(), true);
574 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
576 QWidget::mouseReleaseEvent(event
);
577 mainWindow()->setActiveView(this);
580 DolphinMainWindow
* DolphinView::mainWindow() const
585 void DolphinView::loadDirectory(const KUrl
& url
)
591 const ViewProperties
props(url
);
593 const Mode mode
= props
.viewMode();
594 bool changeMode
= (m_mode
!= mode
);
595 if (changeMode
&& isColumnViewActive()) {
596 // The column view is active. Only change the
597 // mode if the current URL is no child of the column view.
598 if (m_dirLister
->url().isParentOf(url
)) {
609 const bool showHiddenFiles
= props
.showHiddenFiles();
610 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
611 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
612 emit
showHiddenFilesChanged();
615 const DolphinView::Sorting sorting
= props
.sorting();
616 if (sorting
!= m_proxyModel
->sorting()) {
617 m_proxyModel
->setSorting(sorting
);
618 emit
sortingChanged(sorting
);
621 const Qt::SortOrder sortOrder
= props
.sortOrder();
622 if (sortOrder
!= m_proxyModel
->sortOrder()) {
623 m_proxyModel
->setSortOrder(sortOrder
);
624 emit
sortOrderChanged(sortOrder
);
627 KFileItemDelegate::AdditionalInformation info
= props
.additionalInfo();
628 if (info
!= m_fileItemDelegate
->additionalInformation()) {
629 m_fileItemDelegate
->setAdditionalInformation(info
);
631 emit
additionalInfoChanged(info
);
634 const bool showPreview
= props
.showPreview();
635 if (showPreview
!= m_controller
->showPreview()) {
636 m_controller
->setShowPreview(showPreview
);
637 emit
showPreviewChanged();
641 emit
urlChanged(url
);
643 m_statusBar
->clear();
646 void DolphinView::triggerItem(const QModelIndex
& index
)
648 if (!isValidNameIndex(index
)) {
652 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
653 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
654 // items are selected by the user, hence don't trigger the
655 // item specified by 'index'
659 KFileItem
* item
= m_dirModel
->itemForIndex(m_proxyModel
->mapToSource(index
));
664 // Prefer the local path over the URL. This assures that the
665 // volume space information is correct. Assuming that the URL is media:/sda1,
666 // and the local path is /windows/C: For the URL the space info is related
667 // to the root partition (and hence wrong) and for the local path the space
668 // info is related to the windows partition (-> correct).
669 const QString
localPath(item
->localPath());
671 if (localPath
.isEmpty()) {
679 } else if (item
->isFile()) {
680 // allow to browse through ZIP and tar files
681 KMimeType::Ptr mime
= item
->mimeTypePtr();
682 if (mime
->is("application/zip")) {
683 url
.setProtocol("zip");
685 } else if (mime
->is("application/x-tar") ||
686 mime
->is("application/x-tarz") ||
687 mime
->is("application/x-bzip-compressed-tar") ||
688 mime
->is("application/x-compressed-tar") ||
689 mime
->is("application/x-tzo")) {
690 url
.setProtocol("tar");
700 void DolphinView::updateProgress(int percent
)
702 if (m_showProgress
) {
703 m_statusBar
->setProgress(percent
);
707 void DolphinView::updateItemCount()
709 if (m_showProgress
) {
710 m_statusBar
->setProgressText(QString());
711 m_statusBar
->setProgress(100);
712 m_showProgress
= false;
715 KFileItemList
items(m_dirLister
->items());
716 KFileItemList::const_iterator it
= items
.begin();
717 const KFileItemList::const_iterator end
= items
.end();
723 KFileItem
* item
= *it
;
734 m_blockContentsMovedSignal
= false;
735 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
738 void DolphinView::generatePreviews(const KFileItemList
& items
)
740 if (m_controller
->showPreview()) {
741 KIO::PreviewJob
* job
= KIO::filePreview(items
, 128);
742 connect(job
, SIGNAL(gotPreview(const KFileItem
*, const QPixmap
&)),
743 this, SLOT(showPreview(const KFileItem
*, const QPixmap
&)));
747 void DolphinView::showPreview(const KFileItem
* item
, const QPixmap
& pixmap
)
750 const QModelIndex idx
= m_dirModel
->indexForItem(*item
);
751 if (idx
.isValid() && (idx
.column() == 0)) {
752 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
753 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(*item
)) {
754 KIconEffect iconEffect
;
755 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
756 m_dirModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
758 m_dirModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
763 void DolphinView::restoreContentsPos()
765 KUrl currentUrl
= m_urlNavigator
->url();
766 if (!currentUrl
.isEmpty()) {
767 QAbstractItemView
* view
= itemView();
768 // TODO: view->setCurrentItem(m_urlNavigator->currentFileName());
769 QPoint pos
= m_urlNavigator
->savedPosition();
770 view
->horizontalScrollBar()->setValue(pos
.x());
771 view
->verticalScrollBar()->setValue(pos
.y());
775 void DolphinView::showInfoMessage(const QString
& msg
)
777 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
780 void DolphinView::showErrorMessage(const QString
& msg
)
782 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
785 void DolphinView::emitSelectionChangedSignal()
787 emit
selectionChanged(DolphinView::selectedItems());
790 void DolphinView::closeFilterBar()
793 emit
showFilterBarChanged(false);
796 void DolphinView::startDirLister(const KUrl
& url
, bool reload
)
798 if (!url
.isValid()) {
799 const QString
location(url
.pathOrUrl());
800 if (location
.isEmpty()) {
801 m_statusBar
->setMessage(i18n("The location is empty."), DolphinStatusBar::Error
);
803 m_statusBar
->setMessage(i18n("The location '%1' is invalid.", location
),
804 DolphinStatusBar::Error
);
809 // Only show the directory loading progress if the status bar does
810 // not contain another progress information. This means that
811 // the directory loading progress information has the lowest priority.
812 const QString
progressText(m_statusBar
->progressText());
813 m_showProgress
= progressText
.isEmpty() ||
814 (progressText
== i18n("Loading directory..."));
815 if (m_showProgress
) {
816 m_statusBar
->setProgressText(i18n("Loading directory..."));
817 m_statusBar
->setProgress(0);
820 m_cutItemsCache
.clear();
821 m_blockContentsMovedSignal
= true;
825 bool keepOldDirs
= isColumnViewActive();
830 const KUrl
& dirListerUrl
= m_dirLister
->url();
831 if (dirListerUrl
.isValid()) {
832 const KUrl::List dirs
= m_dirLister
->directories();
835 m_dirLister
->updateDirectory(url
);
839 } else if (m_dirLister
->directories().contains(url
)) {
840 // The dir lister contains the directory already, so
841 // KDirLister::openUrl() may not been invoked twice.
842 m_dirLister
->updateDirectory(url
);
845 const KUrl
& dirListerUrl
= m_dirLister
->url();
846 if ((dirListerUrl
== url
) || !m_dirLister
->url().isParentOf(url
)) {
847 // The current URL is not a child of the dir lister
848 // URL. This may happen when e. g. a bookmark has been selected
849 // and hence the view must be reset.
856 m_dirLister
->openUrl(url
, keepOldDirs
, reload
);
860 QString
DolphinView::defaultStatusBarText() const
862 return KIO::itemsSummaryString(m_fileCount
+ m_folderCount
,
868 QString
DolphinView::selectionStatusBarText() const
871 const KFileItemList list
= selectedItems();
872 if (list
.isEmpty()) {
873 // when an item is triggered, it is temporary selected but selectedItems()
874 // will return an empty list
880 KIO::filesize_t byteSize
= 0;
881 KFileItemList::const_iterator it
= list
.begin();
882 const KFileItemList::const_iterator end
= list
.end();
884 KFileItem
* item
= *it
;
889 byteSize
+= item
->size();
894 if (folderCount
> 0) {
895 text
= i18np("1 Folder selected", "%1 Folders selected", folderCount
);
902 const QString
sizeText(KIO::convertSize(byteSize
));
903 text
+= i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount
, sizeText
);
909 void DolphinView::showFilterBar(bool show
)
911 Q_ASSERT(m_filterBar
!= 0);
919 void DolphinView::updateStatusBar()
921 // As the item count information is less important
922 // in comparison with other messages, it should only
924 // - the status bar is empty or
925 // - shows already the item count information or
926 // - shows only a not very important information
927 // - if any progress is given don't show the item count info at all
928 const QString
msg(m_statusBar
->message());
929 const bool updateStatusBarMsg
= (msg
.isEmpty() ||
930 (msg
== m_statusBar
->defaultText()) ||
931 (m_statusBar
->type() == DolphinStatusBar::Information
)) &&
932 (m_statusBar
->progress() == 100);
934 const QString
text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
935 m_statusBar
->setDefaultText(text
);
937 if (updateStatusBarMsg
) {
938 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
942 void DolphinView::requestActivation()
944 m_mainWindow
->setActiveView(this);
947 void DolphinView::changeSelection(const KFileItemList
& selection
)
950 if (selection
.isEmpty()) {
953 KUrl baseUrl
= url();
955 QItemSelection new_selection
;
956 foreach(KFileItem
* item
, selection
) {
957 url
= item
->url().upUrl();
958 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
959 QModelIndex index
= m_proxyModel
->mapFromSource(m_dirModel
->indexForItem(*item
));
960 new_selection
.select(index
, index
);
963 itemView()->selectionModel()->select(new_selection
,
964 QItemSelectionModel::ClearAndSelect
965 | QItemSelectionModel::Current
);
968 void DolphinView::changeNameFilter(const QString
& nameFilter
)
970 // The name filter of KDirLister does a 'hard' filtering, which
971 // means that only the items are shown where the names match
972 // exactly the filter. This is non-transparent for the user, which
973 // just wants to have a 'soft' filtering: does the name contain
974 // the filter string?
975 QString
adjustedFilter(nameFilter
);
976 adjustedFilter
.insert(0, '*');
977 adjustedFilter
.append('*');
979 // Use the ProxyModel to filter:
980 // This code is #ifdefed as setNameFilter behaves
981 // slightly different than the QSortFilterProxyModel
982 // as it will not remove directories. I will ask
983 // our beloved usability experts for input
986 m_dirLister
->setNameFilter(adjustedFilter
);
987 m_dirLister
->emitChanges();
989 m_proxyModel
->setFilterRegExp(nameFilter
);
993 void DolphinView::openContextMenu(const QPoint
& pos
)
997 const QModelIndex index
= itemView()->indexAt(pos
);
998 if (isValidNameIndex(index
)) {
999 item
= fileItem(index
);
1002 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url());
1006 void DolphinView::dropUrls(const KUrl::List
& urls
,
1007 const QModelIndex
& index
,
1010 KFileItem
* directory
= 0;
1011 if (isValidNameIndex(index
)) {
1012 KFileItem
* item
= fileItem(index
);
1013 Q_ASSERT(item
!= 0);
1014 if (item
->isDir()) {
1015 // the URLs are dropped above a directory
1020 if ((directory
== 0) && (source
== itemView())) {
1021 // The dropping is done into the same viewport where
1022 // the dragging has been started. Just ignore this...
1026 const KUrl
& destination
= (directory
== 0) ? url() :
1028 dropUrls(urls
, destination
);
1031 void DolphinView::dropUrls(const KUrl::List
& urls
,
1032 const KUrl
& destination
)
1034 m_mainWindow
->dropUrls(urls
, destination
);
1037 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
1039 ViewProperties
props(url());
1040 props
.setSorting(sorting
);
1042 m_proxyModel
->setSorting(sorting
);
1044 emit
sortingChanged(sorting
);
1047 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1049 ViewProperties
props(url());
1050 props
.setSortOrder(order
);
1052 m_proxyModel
->setSortOrder(order
);
1054 emit
sortOrderChanged(order
);
1057 void DolphinView::emitContentsMoved()
1059 if (!m_blockContentsMovedSignal
) {
1060 emit
contentsMoved(contentsX(), contentsY());
1064 void DolphinView::updateActivationState()
1066 m_urlNavigator
->setActive(isActive());
1068 emit
urlChanged(url());
1069 emit
selectionChanged(selectedItems());
1073 void DolphinView::updateCutItems()
1075 // restore the icons of all previously selected items to the
1076 // original state...
1077 QList
<CutItem
>::const_iterator it
= m_cutItemsCache
.begin();
1078 QList
<CutItem
>::const_iterator end
= m_cutItemsCache
.end();
1080 const QModelIndex index
= m_dirModel
->indexForUrl((*it
).url
);
1081 if (index
.isValid()) {
1082 m_dirModel
->setData(index
, QIcon((*it
).pixmap
), Qt::DecorationRole
);
1086 m_cutItemsCache
.clear();
1088 // ... and apply an item effect to all currently cut items
1089 applyCutItemEffect();
1092 void DolphinView::createView()
1094 // delete current view
1095 QAbstractItemView
* view
= itemView();
1097 m_topLayout
->removeWidget(view
);
1099 view
->deleteLater();
1104 m_fileItemDelegate
= 0;
1107 Q_ASSERT(m_iconsView
== 0);
1108 Q_ASSERT(m_detailsView
== 0);
1109 Q_ASSERT(m_columnView
== 0);
1111 // ... and recreate it representing the current mode
1114 m_iconsView
= new DolphinIconsView(this, m_controller
);
1119 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1120 view
= m_detailsView
;
1124 m_columnView
= new DolphinColumnView(this, m_controller
);
1125 view
= m_columnView
;
1129 Q_ASSERT(view
!= 0);
1131 m_fileItemDelegate
= new KFileItemDelegate(view
);
1132 view
->setItemDelegate(m_fileItemDelegate
);
1134 view
->setModel(m_proxyModel
);
1135 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1137 new KMimeTypeResolver(view
, m_dirModel
);
1138 m_topLayout
->insertWidget(1, view
);
1140 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1141 m_controller
, SLOT(indicateSelectionChange()));
1142 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1143 this, SLOT(emitContentsMoved()));
1144 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1145 this, SLOT(emitContentsMoved()));
1148 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags
)
1150 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
1151 const QAbstractItemModel
* itemModel
= selectionModel
->model();
1153 const QModelIndex topLeft
= itemModel
->index(0, 0);
1154 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
1155 itemModel
->columnCount() - 1);
1157 QItemSelection
selection(topLeft
, bottomRight
);
1158 selectionModel
->select(selection
, flags
);
1161 QAbstractItemView
* DolphinView::itemView() const
1163 if (m_detailsView
!= 0) {
1164 return m_detailsView
;
1165 } else if (m_columnView
!= 0) {
1166 return m_columnView
;
1172 bool DolphinView::isValidNameIndex(const QModelIndex
& index
) const
1174 return index
.isValid() && (index
.column() == KDirModel::Name
);
1177 bool DolphinView::isCutItem(const KFileItem
& item
) const
1179 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1180 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1182 const KUrl
& itemUrl
= item
.url();
1183 KUrl::List::const_iterator it
= cutUrls
.begin();
1184 const KUrl::List::const_iterator end
= cutUrls
.end();
1186 if (*it
== itemUrl
) {
1195 void DolphinView::applyCutItemEffect()
1197 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1198 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
1202 KFileItemList
items(m_dirLister
->items());
1203 KFileItemList::const_iterator it
= items
.begin();
1204 const KFileItemList::const_iterator end
= items
.end();
1206 KFileItem
* item
= *it
;
1207 if (isCutItem(*item
)) {
1208 const QModelIndex index
= m_dirModel
->indexForItem(*item
);
1209 const KFileItem
* item
= m_dirModel
->itemForIndex(index
);
1210 const QVariant value
= m_dirModel
->data(index
, Qt::DecorationRole
);
1211 if ((value
.type() == QVariant::Icon
) && (item
!= 0)) {
1212 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
1213 QPixmap pixmap
= icon
.pixmap(128, 128);
1215 // remember current pixmap for the item to be able
1216 // to restore it when other items get cut
1218 cutItem
.url
= item
->url();
1219 cutItem
.pixmap
= pixmap
;
1220 m_cutItemsCache
.append(cutItem
);
1222 // apply icon effect to the cut item
1223 KIconEffect iconEffect
;
1224 pixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
1225 m_dirModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
1232 #include "dolphinview.moc"