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>
35 #include <kiconeffect.h>
36 #include <kio/netaccess.h>
37 #include <kio/renamedialog.h>
38 #include <kio/previewjob.h>
39 #include <kmimetyperesolver.h>
40 #include <konqmimedata.h>
41 #include <konq_operations.h>
44 #include "dolphincolumnview.h"
45 #include "dolphincontroller.h"
46 #include "dolphinstatusbar.h"
47 #include "dolphinmainwindow.h"
48 #include "dolphindirlister.h"
49 #include "dolphinsortfilterproxymodel.h"
50 #include "dolphindetailsview.h"
51 #include "dolphiniconsview.h"
52 #include "dolphincontextmenu.h"
53 #include "filterbar.h"
54 #include "renamedialog.h"
55 #include "urlnavigator.h"
56 #include "viewproperties.h"
57 #include "dolphinsettings.h"
58 #include "dolphin_generalsettings.h"
60 DolphinView::DolphinView(DolphinMainWindow
* mainWindow
,
64 bool showHiddenFiles
) :
66 m_showProgress(false),
67 m_blockContentsMovedSignal(false),
72 m_mainWindow(mainWindow
),
79 m_fileItemDelegate(0),
87 setFocusPolicy(Qt::StrongFocus
);
88 m_topLayout
= new QVBoxLayout(this);
89 m_topLayout
->setSpacing(0);
90 m_topLayout
->setMargin(0);
92 connect(m_mainWindow
, SIGNAL(activeViewChanged()),
93 this, SLOT(updateActivationState()));
95 QClipboard
* clipboard
= QApplication::clipboard();
96 connect(clipboard
, SIGNAL(dataChanged()),
97 this, SLOT(updateCutItems()));
99 m_urlNavigator
= new UrlNavigator(DolphinSettings::instance().bookmarkManager(), url
, this);
100 m_urlNavigator
->setUrlEditable(DolphinSettings::instance().generalSettings()->editableUrl());
101 m_urlNavigator
->setHomeUrl(DolphinSettings::instance().generalSettings()->homeUrl());
102 m_urlNavigator
->setShowHiddenFiles(showHiddenFiles
);
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(storeContentsPosition(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 m_urlNavigator
->setShowHiddenFiles(show
);
250 emit
showHiddenFilesChanged();
255 bool DolphinView::showHiddenFiles() const
257 return m_dirLister
->showingDotFiles();
260 void DolphinView::renameSelectedItems()
262 DolphinView
* view
= mainWindow()->activeView();
263 const KUrl::List urls
= selectedUrls();
264 if (urls
.count() > 1) {
265 // More than one item has been selected for renaming. Open
266 // a rename dialog and rename all items afterwards.
267 RenameDialog
dialog(urls
);
268 if (dialog
.exec() == QDialog::Rejected
) {
272 const QString
& newName
= dialog
.newName();
273 if (newName
.isEmpty()) {
274 view
->statusBar()->setMessage(dialog
.errorString(),
275 DolphinStatusBar::Error
);
278 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
279 // as one operation instead of n rename operations like it is done now...
280 Q_ASSERT(newName
.contains('#'));
282 // iterate through all selected items and rename them...
283 const int replaceIndex
= newName
.indexOf('#');
284 Q_ASSERT(replaceIndex
>= 0);
287 KUrl::List::const_iterator it
= urls
.begin();
288 KUrl::List::const_iterator end
= urls
.end();
290 const KUrl
& oldUrl
= *it
;
292 number
.setNum(index
++);
294 QString
name(newName
);
295 name
.replace(replaceIndex
, 1, number
);
297 if (oldUrl
.fileName() != name
) {
298 KUrl newUrl
= oldUrl
;
299 newUrl
.setFileName(name
);
300 m_mainWindow
->rename(oldUrl
, newUrl
);
307 // Only one item has been selected for renaming. Use the custom
308 // renaming mechanism from the views.
309 Q_ASSERT(urls
.count() == 1);
311 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
312 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
313 // is a benefit for the user at all -> let's wait for some input first...
314 RenameDialog
dialog(urls
);
315 if (dialog
.exec() == QDialog::Rejected
) {
319 const QString
& newName
= dialog
.newName();
320 if (newName
.isEmpty()) {
321 view
->statusBar()->setMessage(dialog
.errorString(),
322 DolphinStatusBar::Error
);
325 const KUrl
& oldUrl
= urls
.first();
326 KUrl newUrl
= oldUrl
;
327 newUrl
.setFileName(newName
);
328 m_mainWindow
->rename(oldUrl
, newUrl
);
333 void DolphinView::selectAll()
335 selectAll(QItemSelectionModel::Select
);
338 void DolphinView::invertSelection()
340 selectAll(QItemSelectionModel::Toggle
);
343 DolphinStatusBar
* DolphinView::statusBar() const
348 int DolphinView::contentsX() const
350 return itemView()->horizontalScrollBar()->value();
353 int DolphinView::contentsY() const
355 return itemView()->verticalScrollBar()->value();
358 void DolphinView::emitRequestItemInfo(const KUrl
& url
)
360 emit
requestItemInfo(url
);
363 bool DolphinView::isFilterBarVisible() const
365 return m_filterBar
->isVisible();
368 bool DolphinView::isUrlEditable() const
370 return m_urlNavigator
->isUrlEditable();
373 void DolphinView::zoomIn()
375 m_controller
->triggerZoomIn();
378 void DolphinView::zoomOut()
380 m_controller
->triggerZoomOut();
383 bool DolphinView::isZoomInPossible() const
385 return m_controller
->isZoomInPossible();
388 bool DolphinView::isZoomOutPossible() const
390 return m_controller
->isZoomOutPossible();
393 void DolphinView::setSorting(Sorting sorting
)
395 if (sorting
!= this->sorting()) {
396 updateSorting(sorting
);
400 DolphinView::Sorting
DolphinView::sorting() const
402 return m_proxyModel
->sorting();
405 void DolphinView::setSortOrder(Qt::SortOrder order
)
407 if (sortOrder() != order
) {
408 updateSortOrder(order
);
412 Qt::SortOrder
DolphinView::sortOrder() const
414 return m_proxyModel
->sortOrder();
417 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info
)
419 ViewProperties
props(m_urlNavigator
->url());
420 props
.setAdditionalInfo(info
);
422 m_fileItemDelegate
->setAdditionalInformation(info
);
424 emit
additionalInfoChanged(info
);
428 KFileItemDelegate::AdditionalInformation
DolphinView::additionalInfo() const
430 return m_fileItemDelegate
->additionalInformation();
433 void DolphinView::goBack()
435 m_urlNavigator
->goBack();
438 void DolphinView::goForward()
440 m_urlNavigator
->goForward();
443 void DolphinView::goUp()
445 m_urlNavigator
->goUp();
448 void DolphinView::goHome()
450 m_urlNavigator
->goHome();
453 void DolphinView::setUrlEditable(bool editable
)
455 m_urlNavigator
->setUrlEditable(editable
);
458 bool DolphinView::hasSelection() const
460 return itemView()->selectionModel()->hasSelection();
463 void DolphinView::clearSelection()
465 itemView()->selectionModel()->clear();
468 KFileItemList
DolphinView::selectedItems() const
470 const QAbstractItemView
* view
= itemView();
472 // Our view has a selection, we will map them back to the DirModel
473 // and then fill the KFileItemList.
474 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
476 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
477 KFileItemList itemList
;
479 const QModelIndexList indexList
= selection
.indexes();
480 QModelIndexList::const_iterator end
= indexList
.end();
481 for (QModelIndexList::const_iterator it
= indexList
.begin(); it
!= end
; ++it
) {
482 Q_ASSERT((*it
).isValid());
484 KFileItem
* item
= m_dirModel
->itemForIndex(*it
);
486 itemList
.append(item
);
493 KUrl::List
DolphinView::selectedUrls() const
497 const KFileItemList list
= selectedItems();
498 KFileItemList::const_iterator it
= list
.begin();
499 const KFileItemList::const_iterator end
= list
.end();
501 KFileItem
* item
= *it
;
502 urls
.append(item
->url());
509 KFileItem
* DolphinView::fileItem(const QModelIndex index
) const
511 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
512 return m_dirModel
->itemForIndex(dirModelIndex
);
515 void DolphinView::rename(const KUrl
& source
, const QString
& newName
)
519 if (newName
.isEmpty() || (source
.fileName() == newName
)) {
523 KUrl
dest(source
.upUrl());
524 dest
.addPath(newName
);
526 const bool destExists
= KIO::NetAccess::exists(dest
,
528 mainWindow()->activeView());
530 // the destination already exists, hence ask the user
532 KIO::RenameDialog
renameDialog(this,
533 i18n("File Already Exists"),
537 switch (renameDialog
.exec()) {
538 case KIO::R_OVERWRITE
:
539 // the destination should be overwritten
540 ok
= KIO::NetAccess::file_move(source
, dest
, -1, true);
543 case KIO::R_RENAME
: {
544 // a new name for the destination has been used
545 KUrl
newDest(renameDialog
.newDestUrl());
546 ok
= KIO::NetAccess::file_move(source
, newDest
);
551 // the renaming operation has been canceled
557 // no destination exists, hence just move the file to
559 ok
= KIO::NetAccess::file_move(source
, dest
);
562 const QString destFileName
= dest
.fileName();
564 m_statusBar
->setMessage(i18n("Renamed file '%1' to '%2'.",source
.fileName(), destFileName
),
565 DolphinStatusBar::OperationCompleted
);
567 KonqOperations::rename(this, source
, destFileName
);
570 m_statusBar
->setMessage(i18n("Renaming of file '%1' to '%2' failed.",source
.fileName(), destFileName
),
571 DolphinStatusBar::Error
);
576 void DolphinView::reload()
578 startDirLister(m_urlNavigator
->url(), true);
581 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
583 QWidget::mouseReleaseEvent(event
);
584 mainWindow()->setActiveView(this);
587 DolphinMainWindow
* DolphinView::mainWindow() const
592 void DolphinView::loadDirectory(const KUrl
& url
)
598 const ViewProperties
props(url
);
600 const Mode mode
= props
.viewMode();
601 bool changeMode
= (m_mode
!= mode
);
602 if (changeMode
&& isColumnViewActive()) {
603 // The column view is active. Only change the
604 // mode if the current URL is no child of the column view.
605 if (m_dirLister
->url().isParentOf(url
)) {
616 const bool showHiddenFiles
= props
.showHiddenFiles();
617 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
618 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
619 emit
showHiddenFilesChanged();
622 const DolphinView::Sorting sorting
= props
.sorting();
623 if (sorting
!= m_proxyModel
->sorting()) {
624 m_proxyModel
->setSorting(sorting
);
625 emit
sortingChanged(sorting
);
628 const Qt::SortOrder sortOrder
= props
.sortOrder();
629 if (sortOrder
!= m_proxyModel
->sortOrder()) {
630 m_proxyModel
->setSortOrder(sortOrder
);
631 emit
sortOrderChanged(sortOrder
);
634 KFileItemDelegate::AdditionalInformation info
= props
.additionalInfo();
635 if (info
!= m_fileItemDelegate
->additionalInformation()) {
636 m_fileItemDelegate
->setAdditionalInformation(info
);
638 emit
additionalInfoChanged(info
);
641 const bool showPreview
= props
.showPreview();
642 if (showPreview
!= m_controller
->showPreview()) {
643 m_controller
->setShowPreview(showPreview
);
644 emit
showPreviewChanged();
648 emit
urlChanged(url
);
650 m_statusBar
->clear();
653 void DolphinView::triggerItem(const QModelIndex
& index
)
655 if (!isValidNameIndex(index
)) {
659 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
660 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
661 // items are selected by the user, hence don't trigger the
662 // item specified by 'index'
666 KFileItem
* item
= m_dirModel
->itemForIndex(m_proxyModel
->mapToSource(index
));
671 // Prefer the local path over the URL. This assures that the
672 // volume space information is correct. Assuming that the URL is media:/sda1,
673 // and the local path is /windows/C: For the URL the space info is related
674 // to the root partition (and hence wrong) and for the local path the space
675 // info is related to the windows partition (-> correct).
676 const QString
localPath(item
->localPath());
678 if (localPath
.isEmpty()) {
688 else if (item
->isFile()) {
689 // allow to browse through ZIP and tar files
690 KMimeType::Ptr mime
= item
->mimeTypePtr();
691 if (mime
->is("application/zip")) {
692 url
.setProtocol("zip");
695 else if (mime
->is("application/x-tar") ||
696 mime
->is("application/x-tarz") ||
697 mime
->is("application/x-bzip-compressed-tar") ||
698 mime
->is("application/x-compressed-tar") ||
699 mime
->is("application/x-tzo")) {
700 url
.setProtocol("tar");
712 void DolphinView::updateProgress(int percent
)
714 if (m_showProgress
) {
715 m_statusBar
->setProgress(percent
);
719 void DolphinView::updateItemCount()
721 if (m_showProgress
) {
722 m_statusBar
->setProgressText(QString());
723 m_statusBar
->setProgress(100);
724 m_showProgress
= false;
727 KFileItemList
items(m_dirLister
->items());
728 KFileItemList::const_iterator it
= items
.begin();
729 const KFileItemList::const_iterator end
= items
.end();
735 KFileItem
* item
= *it
;
747 m_blockContentsMovedSignal
= false;
748 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
751 void DolphinView::generatePreviews(const KFileItemList
& items
)
753 if (m_controller
->showPreview()) {
754 KIO::PreviewJob
* job
= KIO::filePreview(items
, 128);
755 connect(job
, SIGNAL(gotPreview(const KFileItem
*, const QPixmap
&)),
756 this, SLOT(showPreview(const KFileItem
*, const QPixmap
&)));
760 void DolphinView::showPreview(const KFileItem
* item
, const QPixmap
& pixmap
)
763 const QModelIndex idx
= m_dirModel
->indexForItem(*item
);
764 if (idx
.isValid() && (idx
.column() == 0)) {
765 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
766 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(*item
)) {
767 KIconEffect iconEffect
;
768 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
769 m_dirModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
772 m_dirModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
777 void DolphinView::restoreContentsPos()
779 KUrl currentUrl
= m_urlNavigator
->url();
780 if (!currentUrl
.isEmpty()) {
781 QAbstractItemView
* view
= itemView();
782 // TODO: view->setCurrentItem(m_urlNavigator->currentFileName());
783 QPoint pos
= m_urlNavigator
->savedPosition();
784 view
->horizontalScrollBar()->setValue(pos
.x());
785 view
->verticalScrollBar()->setValue(pos
.y());
789 void DolphinView::showInfoMessage(const QString
& msg
)
791 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
794 void DolphinView::showErrorMessage(const QString
& msg
)
796 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
799 void DolphinView::emitSelectionChangedSignal()
801 emit
selectionChanged(DolphinView::selectedItems());
804 void DolphinView::closeFilterBar()
807 emit
showFilterBarChanged(false);
810 void DolphinView::startDirLister(const KUrl
& url
, bool reload
)
812 if (!url
.isValid()) {
813 const QString
location(url
.pathOrUrl());
814 if (location
.isEmpty()) {
815 m_statusBar
->setMessage(i18n("The location is empty."), DolphinStatusBar::Error
);
818 m_statusBar
->setMessage(i18n("The location '%1' is invalid.",location
),
819 DolphinStatusBar::Error
);
824 // Only show the directory loading progress if the status bar does
825 // not contain another progress information. This means that
826 // the directory loading progress information has the lowest priority.
827 const QString
progressText(m_statusBar
->progressText());
828 m_showProgress
= progressText
.isEmpty() ||
829 (progressText
== i18n("Loading directory..."));
830 if (m_showProgress
) {
831 m_statusBar
->setProgressText(i18n("Loading directory..."));
832 m_statusBar
->setProgress(0);
835 m_cutItemsCache
.clear();
836 m_blockContentsMovedSignal
= true;
840 bool keepOldDirs
= isColumnViewActive();
845 const KUrl
& dirListerUrl
= m_dirLister
->url();
846 if (dirListerUrl
.isValid()) {
847 const KUrl::List dirs
= m_dirLister
->directories();
849 foreach (url
, dirs
) {
850 m_dirLister
->updateDirectory(url
);
855 else if (m_dirLister
->directories().contains(url
)) {
856 // The dir lister contains the directory already, so
857 // KDirLister::openUrl() may not been invoked twice.
858 m_dirLister
->updateDirectory(url
);
862 const KUrl
& dirListerUrl
= m_dirLister
->url();
863 if ((dirListerUrl
== url
) || !m_dirLister
->url().isParentOf(url
)) {
864 // The current URL is not a child of the dir lister
865 // URL. This may happen when e. g. a bookmark has been selected
866 // and hence the view must be reset.
873 m_dirLister
->openUrl(url
, keepOldDirs
, reload
);
877 QString
DolphinView::defaultStatusBarText() const
879 return KIO::itemsSummaryString(m_fileCount
+ m_folderCount
,
885 QString
DolphinView::selectionStatusBarText() const
888 const KFileItemList list
= selectedItems();
889 if (list
.isEmpty()) {
890 // when an item is triggered, it is temporary selected but selectedItems()
891 // will return an empty list
897 KIO::filesize_t byteSize
= 0;
898 KFileItemList::const_iterator it
= list
.begin();
899 const KFileItemList::const_iterator end
= list
.end();
901 KFileItem
* item
= *it
;
907 byteSize
+= item
->size();
912 if (folderCount
> 0) {
913 text
= i18np("1 Folder selected", "%1 Folders selected", folderCount
);
920 const QString
sizeText(KIO::convertSize(byteSize
));
921 text
+= i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount
, sizeText
);
927 void DolphinView::showFilterBar(bool show
)
929 Q_ASSERT(m_filterBar
!= 0);
938 void DolphinView::updateStatusBar()
940 // As the item count information is less important
941 // in comparison with other messages, it should only
943 // - the status bar is empty or
944 // - shows already the item count information or
945 // - shows only a not very important information
946 // - if any progress is given don't show the item count info at all
947 const QString
msg(m_statusBar
->message());
948 const bool updateStatusBarMsg
= (msg
.isEmpty() ||
949 (msg
== m_statusBar
->defaultText()) ||
950 (m_statusBar
->type() == DolphinStatusBar::Information
)) &&
951 (m_statusBar
->progress() == 100);
953 const QString
text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
954 m_statusBar
->setDefaultText(text
);
956 if (updateStatusBarMsg
) {
957 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
961 void DolphinView::requestActivation()
963 m_mainWindow
->setActiveView(this);
966 void DolphinView::changeSelection(const KFileItemList
& selection
)
969 if (selection
.isEmpty()) {
972 KUrl baseUrl
= url();
974 QItemSelection new_selection
;
975 foreach (KFileItem
* item
, selection
) {
976 url
= item
->url().upUrl();
977 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
978 QModelIndex index
= m_proxyModel
->mapFromSource(m_dirModel
->indexForItem(*item
));
979 new_selection
.select(index
, index
);
982 itemView()->selectionModel()->select(new_selection
,
983 QItemSelectionModel::ClearAndSelect
984 | QItemSelectionModel::Current
);
987 void DolphinView::changeNameFilter(const QString
& nameFilter
)
989 // The name filter of KDirLister does a 'hard' filtering, which
990 // means that only the items are shown where the names match
991 // exactly the filter. This is non-transparent for the user, which
992 // just wants to have a 'soft' filtering: does the name contain
993 // the filter string?
994 QString
adjustedFilter(nameFilter
);
995 adjustedFilter
.insert(0, '*');
996 adjustedFilter
.append('*');
998 // Use the ProxyModel to filter:
999 // This code is #ifdefed as setNameFilter behaves
1000 // slightly different than the QSortFilterProxyModel
1001 // as it will not remove directories. I will ask
1002 // our beloved usability experts for input
1005 m_dirLister
->setNameFilter(adjustedFilter
);
1006 m_dirLister
->emitChanges();
1008 m_proxyModel
->setFilterRegExp( nameFilter
);
1012 void DolphinView::openContextMenu(const QPoint
& pos
)
1014 KFileItem
* item
= 0;
1016 const QModelIndex index
= itemView()->indexAt(pos
);
1017 if (isValidNameIndex(index
)) {
1018 item
= fileItem(index
);
1021 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url());
1025 void DolphinView::dropUrls(const KUrl::List
& urls
,
1026 const QModelIndex
& index
,
1029 KFileItem
* directory
= 0;
1030 if (isValidNameIndex(index
)) {
1031 KFileItem
* item
= fileItem(index
);
1032 Q_ASSERT(item
!= 0);
1033 if (item
->isDir()) {
1034 // the URLs are dropped above a directory
1039 if ((directory
== 0) && (source
== itemView())) {
1040 // The dropping is done into the same viewport where
1041 // the dragging has been started. Just ignore this...
1045 const KUrl
& destination
= (directory
== 0) ? url() :
1047 dropUrls(urls
, destination
);
1050 void DolphinView::dropUrls(const KUrl::List
& urls
,
1051 const KUrl
& destination
)
1053 m_mainWindow
->dropUrls(urls
, destination
);
1056 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
1058 ViewProperties
props(url());
1059 props
.setSorting(sorting
);
1061 m_proxyModel
->setSorting(sorting
);
1063 emit
sortingChanged(sorting
);
1066 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1068 ViewProperties
props(url());
1069 props
.setSortOrder(order
);
1071 m_proxyModel
->setSortOrder(order
);
1073 emit
sortOrderChanged(order
);
1076 void DolphinView::emitContentsMoved()
1078 if (!m_blockContentsMovedSignal
) {
1079 emit
contentsMoved(contentsX(), contentsY());
1083 void DolphinView::updateActivationState()
1085 m_urlNavigator
->setActive(isActive());
1087 emit
urlChanged(url());
1088 emit
selectionChanged(selectedItems());
1092 void DolphinView::updateCutItems()
1094 // restore the icons of all previously selected items to the
1095 // original state...
1096 QList
<CutItem
>::const_iterator it
= m_cutItemsCache
.begin();
1097 QList
<CutItem
>::const_iterator end
= m_cutItemsCache
.end();
1099 const QModelIndex index
= m_dirModel
->indexForUrl((*it
).url
);
1100 if (index
.isValid()) {
1101 m_dirModel
->setData(index
, QIcon((*it
).pixmap
), Qt::DecorationRole
);
1105 m_cutItemsCache
.clear();
1107 // ... and apply an item effect to all currently cut items
1108 applyCutItemEffect();
1111 void DolphinView::createView()
1113 // delete current view
1114 QAbstractItemView
* view
= itemView();
1116 m_topLayout
->removeWidget(view
);
1118 view
->deleteLater();
1123 m_fileItemDelegate
= 0;
1126 Q_ASSERT(m_iconsView
== 0);
1127 Q_ASSERT(m_detailsView
== 0);
1128 Q_ASSERT(m_columnView
== 0);
1130 // ... and recreate it representing the current mode
1133 m_iconsView
= new DolphinIconsView(this, m_controller
);
1138 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1139 view
= m_detailsView
;
1143 m_columnView
= new DolphinColumnView(this, m_controller
);
1144 view
= m_columnView
;
1148 Q_ASSERT(view
!= 0);
1150 m_fileItemDelegate
= new KFileItemDelegate(view
);
1151 view
->setItemDelegate(m_fileItemDelegate
);
1153 view
->setModel(m_proxyModel
);
1154 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1156 new KMimeTypeResolver(view
, m_dirModel
);
1157 m_topLayout
->insertWidget(1, view
);
1159 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1160 m_controller
, SLOT(indicateSelectionChange()));
1161 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1162 this, SLOT(emitContentsMoved()));
1163 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1164 this, SLOT(emitContentsMoved()));
1167 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags
)
1169 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
1170 const QAbstractItemModel
* itemModel
= selectionModel
->model();
1172 const QModelIndex topLeft
= itemModel
->index(0, 0);
1173 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
1174 itemModel
->columnCount() - 1);
1176 QItemSelection
selection(topLeft
, bottomRight
);
1177 selectionModel
->select(selection
, flags
);
1180 QAbstractItemView
* DolphinView::itemView() const
1182 if (m_detailsView
!= 0) {
1183 return m_detailsView
;
1185 else if (m_columnView
!= 0) {
1186 return m_columnView
;
1192 bool DolphinView::isValidNameIndex(const QModelIndex
& index
) const
1194 return index
.isValid() && (index
.column() == KDirModel::Name
);
1197 bool DolphinView::isCutItem(const KFileItem
& item
) const
1199 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1200 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1202 const KUrl
& itemUrl
= item
.url();
1203 KUrl::List::const_iterator it
= cutUrls
.begin();
1204 const KUrl::List::const_iterator end
= cutUrls
.end();
1206 if (*it
== itemUrl
) {
1215 void DolphinView::applyCutItemEffect()
1217 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1218 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
1222 KFileItemList
items(m_dirLister
->items());
1223 KFileItemList::const_iterator it
= items
.begin();
1224 const KFileItemList::const_iterator end
= items
.end();
1226 KFileItem
* item
= *it
;
1227 if (isCutItem(*item
)) {
1228 const QModelIndex index
= m_dirModel
->indexForItem(*item
);
1229 const KFileItem
* item
= m_dirModel
->itemForIndex(index
);
1230 const QVariant value
= m_dirModel
->data(index
, Qt::DecorationRole
);
1231 if ((value
.type() == QVariant::Icon
) && (item
!= 0)) {
1232 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
1233 QPixmap pixmap
= icon
.pixmap(128, 128);
1235 // remember current pixmap for the item to be able
1236 // to restore it when other items get cut
1238 cutItem
.url
= item
->url();
1239 cutItem
.pixmap
= pixmap
;
1240 m_cutItemsCache
.append(cutItem
);
1242 // apply icon effect to the cut item
1243 KIconEffect iconEffect
;
1244 pixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
1245 m_dirModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
1252 #include "dolphinview.moc"