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 <QtGui/QApplication>
24 #include <QtGui/QClipboard>
25 #include <QtGui/QKeyEvent>
26 #include <QtGui/QItemSelection>
27 #include <QtGui/QBoxLayout>
28 #include <QtCore/QTimer>
29 #include <QtGui/QScrollBar>
31 #include <kdirmodel.h>
32 #include <kfileitemdelegate.h>
33 #include <kfileplacesmodel.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 "dolphinitemcategorizer.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),
69 m_initializeColumnView(false),
74 m_mainWindow(mainWindow
),
81 m_fileItemDelegate(0),
89 setFocusPolicy(Qt::StrongFocus
);
90 m_topLayout
= new QVBoxLayout(this);
91 m_topLayout
->setSpacing(0);
92 m_topLayout
->setMargin(0);
94 connect(m_mainWindow
, SIGNAL(activeViewChanged()),
95 this, SLOT(updateActivationState()));
97 QClipboard
* clipboard
= QApplication::clipboard();
98 connect(clipboard
, SIGNAL(dataChanged()),
99 this, SLOT(updateCutItems()));
101 m_urlNavigator
= new KUrlNavigator(DolphinSettings::instance().placesModel(), url
, this);
102 m_urlNavigator
->setUrlEditable(DolphinSettings::instance().generalSettings()->editableUrl());
103 m_urlNavigator
->setHomeUrl(DolphinSettings::instance().generalSettings()->homeUrl());
104 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
105 this, SLOT(changeDirectory(const KUrl
&)));
106 connect(m_urlNavigator
, SIGNAL(urlsDropped(const KUrl::List
&, const KUrl
&)),
107 this, SLOT(dropUrls(const KUrl::List
&, const KUrl
&)));
108 connect(m_urlNavigator
, SIGNAL(activated()),
109 this, SLOT(requestActivation()));
110 connect(this, SIGNAL(contentsMoved(int, int)),
111 m_urlNavigator
, SLOT(savePosition(int, int)));
113 m_statusBar
= new DolphinStatusBar(this);
115 m_dirLister
= new DolphinDirLister();
116 m_dirLister
->setAutoUpdate(true);
117 m_dirLister
->setMainWindow(this);
118 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
119 m_dirLister
->setDelayedMimeTypes(true);
121 connect(m_dirLister
, SIGNAL(clear()),
122 this, SLOT(updateStatusBar()));
123 connect(m_dirLister
, SIGNAL(percent(int)),
124 this, SLOT(updateProgress(int)));
125 connect(m_dirLister
, SIGNAL(deleteItem(KFileItem
*)),
126 this, SLOT(updateStatusBar()));
127 connect(m_dirLister
, SIGNAL(completed()),
128 this, SLOT(updateItemCount()));
129 connect(m_dirLister
, SIGNAL(completed()),
130 this, SLOT(updateCutItems()));
131 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
132 this, SLOT(generatePreviews(const KFileItemList
&)));
133 connect(m_dirLister
, SIGNAL(infoMessage(const QString
&)),
134 this, SLOT(showInfoMessage(const QString
&)));
135 connect(m_dirLister
, SIGNAL(errorMessage(const QString
&)),
136 this, SLOT(showErrorMessage(const QString
&)));
138 m_dirModel
= new KDirModel();
139 m_dirModel
->setDirLister(m_dirLister
);
140 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
142 m_proxyModel
= new DolphinSortFilterProxyModel(this);
143 m_proxyModel
->setSourceModel(m_dirModel
);
145 m_controller
= new DolphinController(this);
146 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&)),
147 this, SLOT(openContextMenu(const QPoint
&)));
148 connect(m_controller
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&, QWidget
*)),
149 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&, QWidget
*)));
150 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
151 this, SLOT(updateSorting(DolphinView::Sorting
)));
152 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
153 this, SLOT(updateSortOrder(Qt::SortOrder
)));
154 connect(m_controller
, SIGNAL(itemTriggered(const QModelIndex
&)),
155 this, SLOT(triggerItem(const QModelIndex
&)));
156 connect(m_controller
, SIGNAL(selectionChanged()),
157 this, SLOT(emitSelectionChangedSignal()));
158 connect(m_controller
, SIGNAL(activated()),
159 this, SLOT(requestActivation()));
160 connect(m_controller
, SIGNAL(itemEntered(const QModelIndex
&)),
161 this, SLOT(showHoverInformation(const QModelIndex
&)));
162 connect(m_controller
, SIGNAL(viewportEntered()),
163 this, SLOT(clearHoverInformation()));
167 m_iconSize
= K3Icon::SizeMedium
;
169 m_filterBar
= new FilterBar(this);
171 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
172 this, SLOT(changeNameFilter(const QString
&)));
173 connect(m_filterBar
, SIGNAL(closeRequest()),
174 this, SLOT(closeFilterBar()));
176 m_topLayout
->addWidget(m_urlNavigator
);
177 m_topLayout
->addWidget(itemView());
178 m_topLayout
->addWidget(m_filterBar
);
179 m_topLayout
->addWidget(m_statusBar
);
182 DolphinView::~DolphinView()
188 void DolphinView::setUrl(const KUrl
& url
)
190 m_urlNavigator
->setUrl(url
);
191 m_controller
->setUrl(url
);
194 const KUrl
& DolphinView::url() const
196 return m_urlNavigator
->url();
199 bool DolphinView::isActive() const
201 return m_mainWindow
->activeView() == this;
204 void DolphinView::setMode(Mode mode
)
206 if (mode
== m_mode
) {
207 return; // the wished mode is already set
212 if (isColumnViewActive()) {
213 // When changing the mode in the column view, it makes sense
214 // to go back to the root URL of the column view automatically.
215 // Otherwise there it would not be possible to turn off the column view
216 // without focusing the first column.
217 setUrl(m_dirLister
->url());
220 ViewProperties
props(m_urlNavigator
->url());
221 props
.setViewMode(m_mode
);
224 startDirLister(m_urlNavigator
->url());
229 DolphinView::Mode
DolphinView::mode() const
234 void DolphinView::setShowPreview(bool show
)
236 ViewProperties
props(m_urlNavigator
->url());
237 props
.setShowPreview(show
);
239 m_controller
->setShowPreview(show
);
240 emit
showPreviewChanged();
242 startDirLister(m_urlNavigator
->url(), true);
245 bool DolphinView::showPreview() const
247 return m_controller
->showPreview();
250 void DolphinView::setShowHiddenFiles(bool show
)
252 if (m_dirLister
->showingDotFiles() == show
) {
256 ViewProperties
props(m_urlNavigator
->url());
257 props
.setShowHiddenFiles(show
);
259 m_dirLister
->setShowingDotFiles(show
);
260 emit
showHiddenFilesChanged();
262 startDirLister(m_urlNavigator
->url(), true);
265 bool DolphinView::showHiddenFiles() const
267 return m_dirLister
->showingDotFiles();
270 void DolphinView::setCategorizedSorting(bool categorized
)
272 if (!supportsCategorizedSorting() || (categorized
== categorizedSorting())) {
276 Q_ASSERT(m_iconsView
!= 0);
278 Q_ASSERT(m_iconsView
->itemCategorizer() == 0);
279 m_iconsView
->setItemCategorizer(new DolphinItemCategorizer());
281 KItemCategorizer
* categorizer
= m_iconsView
->itemCategorizer();
282 m_iconsView
->setItemCategorizer(0);
286 ViewProperties
props(m_urlNavigator
->url());
287 props
.setCategorizedSorting(categorized
);
290 emit
categorizedSortingChanged();
293 bool DolphinView::categorizedSorting() const
295 if (!supportsCategorizedSorting()) {
299 Q_ASSERT(m_iconsView
!= 0);
300 return m_iconsView
->itemCategorizer() != 0;
303 bool DolphinView::supportsCategorizedSorting() const
305 return m_iconsView
!= 0;
308 void DolphinView::renameSelectedItems()
310 DolphinView
* view
= mainWindow()->activeView();
311 const KUrl::List urls
= selectedUrls();
312 if (urls
.count() > 1) {
313 // More than one item has been selected for renaming. Open
314 // a rename dialog and rename all items afterwards.
315 RenameDialog
dialog(urls
);
316 if (dialog
.exec() == QDialog::Rejected
) {
320 const QString
& newName
= dialog
.newName();
321 if (newName
.isEmpty()) {
322 view
->statusBar()->setMessage(dialog
.errorString(),
323 DolphinStatusBar::Error
);
325 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
326 // as one operation instead of n rename operations like it is done now...
327 Q_ASSERT(newName
.contains('#'));
329 // iterate through all selected items and rename them...
330 const int replaceIndex
= newName
.indexOf('#');
331 Q_ASSERT(replaceIndex
>= 0);
334 KUrl::List::const_iterator it
= urls
.begin();
335 KUrl::List::const_iterator end
= urls
.end();
337 const KUrl
& oldUrl
= *it
;
339 number
.setNum(index
++);
341 QString
name(newName
);
342 name
.replace(replaceIndex
, 1, number
);
344 if (oldUrl
.fileName() != name
) {
345 KUrl newUrl
= oldUrl
;
346 newUrl
.setFileName(name
);
347 m_mainWindow
->rename(oldUrl
, newUrl
);
353 // Only one item has been selected for renaming. Use the custom
354 // renaming mechanism from the views.
355 Q_ASSERT(urls
.count() == 1);
357 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
358 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
359 // is a benefit for the user at all -> let's wait for some input first...
360 RenameDialog
dialog(urls
);
361 if (dialog
.exec() == QDialog::Rejected
) {
365 const QString
& newName
= dialog
.newName();
366 if (newName
.isEmpty()) {
367 view
->statusBar()->setMessage(dialog
.errorString(),
368 DolphinStatusBar::Error
);
370 const KUrl
& oldUrl
= urls
.first();
371 KUrl newUrl
= oldUrl
;
372 newUrl
.setFileName(newName
);
373 m_mainWindow
->rename(oldUrl
, newUrl
);
378 void DolphinView::selectAll()
380 selectAll(QItemSelectionModel::Select
);
383 void DolphinView::invertSelection()
385 selectAll(QItemSelectionModel::Toggle
);
388 DolphinStatusBar
* DolphinView::statusBar() const
393 int DolphinView::contentsX() const
395 return itemView()->horizontalScrollBar()->value();
398 int DolphinView::contentsY() const
400 return itemView()->verticalScrollBar()->value();
403 void DolphinView::emitRequestItemInfo(const KUrl
& url
)
405 emit
requestItemInfo(url
);
408 bool DolphinView::isFilterBarVisible() const
410 return m_filterBar
->isVisible();
413 bool DolphinView::isUrlEditable() const
415 return m_urlNavigator
->isUrlEditable();
418 void DolphinView::zoomIn()
420 m_controller
->triggerZoomIn();
423 void DolphinView::zoomOut()
425 m_controller
->triggerZoomOut();
428 bool DolphinView::isZoomInPossible() const
430 return m_controller
->isZoomInPossible();
433 bool DolphinView::isZoomOutPossible() const
435 return m_controller
->isZoomOutPossible();
438 void DolphinView::setSorting(Sorting sorting
)
440 if (sorting
!= this->sorting()) {
441 updateSorting(sorting
);
445 DolphinView::Sorting
DolphinView::sorting() const
447 return m_proxyModel
->sorting();
450 void DolphinView::setSortOrder(Qt::SortOrder order
)
452 if (sortOrder() != order
) {
453 updateSortOrder(order
);
457 Qt::SortOrder
DolphinView::sortOrder() const
459 return m_proxyModel
->sortOrder();
462 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info
)
464 ViewProperties
props(m_urlNavigator
->url());
465 props
.setAdditionalInfo(info
);
467 m_controller
->setShowAdditionalInfo(info
!= KFileItemDelegate::NoInformation
);
468 m_fileItemDelegate
->setAdditionalInformation(info
);
470 emit
additionalInfoChanged(info
);
471 startDirLister(m_urlNavigator
->url(), true);
474 KFileItemDelegate::AdditionalInformation
DolphinView::additionalInfo() const
476 return m_fileItemDelegate
->additionalInformation();
479 void DolphinView::goBack()
481 m_urlNavigator
->goBack();
484 void DolphinView::goForward()
486 m_urlNavigator
->goForward();
489 void DolphinView::goUp()
491 m_urlNavigator
->goUp();
494 void DolphinView::goHome()
496 m_urlNavigator
->goHome();
499 void DolphinView::setUrlEditable(bool editable
)
501 m_urlNavigator
->setUrlEditable(editable
);
504 bool DolphinView::hasSelection() const
506 return itemView()->selectionModel()->hasSelection();
509 void DolphinView::clearSelection()
511 itemView()->selectionModel()->clear();
514 KFileItemList
DolphinView::selectedItems() const
516 const QAbstractItemView
* view
= itemView();
518 // Our view has a selection, we will map them back to the DirModel
519 // and then fill the KFileItemList.
520 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
522 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
523 KFileItemList itemList
;
525 const QModelIndexList indexList
= selection
.indexes();
526 QModelIndexList::const_iterator end
= indexList
.end();
527 for (QModelIndexList::const_iterator it
= indexList
.begin(); it
!= end
; ++it
) {
528 Q_ASSERT((*it
).isValid());
530 KFileItem
* item
= m_dirModel
->itemForIndex(*it
);
532 itemList
.append(item
);
539 KUrl::List
DolphinView::selectedUrls() const
543 const KFileItemList list
= selectedItems();
544 KFileItemList::const_iterator it
= list
.begin();
545 const KFileItemList::const_iterator end
= list
.end();
547 KFileItem
* item
= *it
;
548 urls
.append(item
->url());
555 KFileItem
* DolphinView::fileItem(const QModelIndex index
) const
557 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
558 return m_dirModel
->itemForIndex(dirModelIndex
);
561 void DolphinView::rename(const KUrl
& source
, const QString
& newName
)
565 if (newName
.isEmpty() || (source
.fileName() == newName
)) {
569 KUrl
dest(source
.upUrl());
570 dest
.addPath(newName
);
572 const bool destExists
= KIO::NetAccess::exists(dest
,
574 mainWindow()->activeView());
576 // the destination already exists, hence ask the user
578 KIO::RenameDialog
renameDialog(this,
579 i18n("File Already Exists"),
583 switch (renameDialog
.exec()) {
584 case KIO::R_OVERWRITE
:
585 // the destination should be overwritten
586 ok
= KIO::NetAccess::file_move(source
, dest
, -1, true);
589 case KIO::R_RENAME
: {
590 // a new name for the destination has been used
591 KUrl
newDest(renameDialog
.newDestUrl());
592 ok
= KIO::NetAccess::file_move(source
, newDest
);
597 // the renaming operation has been canceled
601 // no destination exists, hence just move the file to
603 ok
= KIO::NetAccess::file_move(source
, dest
);
606 const QString destFileName
= dest
.fileName();
608 m_statusBar
->setMessage(i18n("Renamed file '%1' to '%2'.", source
.fileName(), destFileName
),
609 DolphinStatusBar::OperationCompleted
);
611 KonqOperations::rename(this, source
, destFileName
);
613 m_statusBar
->setMessage(i18n("Renaming of file '%1' to '%2' failed.", source
.fileName(), destFileName
),
614 DolphinStatusBar::Error
);
618 void DolphinView::reload()
620 const KUrl
& url
= m_urlNavigator
->url();
621 changeDirectory(url
);
622 startDirLister(url
, true);
625 void DolphinView::refresh()
631 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
633 QWidget::mouseReleaseEvent(event
);
634 mainWindow()->setActiveView(this);
637 DolphinMainWindow
* DolphinView::mainWindow() const
642 void DolphinView::changeDirectory(const KUrl
& url
)
648 const ViewProperties
props(url
);
650 const Mode mode
= props
.viewMode();
651 bool changeMode
= (m_mode
!= mode
);
652 if (changeMode
&& isColumnViewActive()) {
653 // The column view is active. Only change the
654 // mode if the current URL is no child of the column view.
655 if (m_dirLister
->url().isParentOf(url
)) {
665 if (m_mode
== ColumnView
) {
666 // The mode has been changed to the Column View. When starting the dir
667 // lister with DolphinView::startDirLister() it is important to give a
668 // hint that the dir lister may not keep the current directory
669 // although this is the default for showing a hierarchy.
670 m_initializeColumnView
= true;
674 const bool showHiddenFiles
= props
.showHiddenFiles();
675 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
676 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
677 emit
showHiddenFilesChanged();
680 const bool categorized
= props
.categorizedSorting();
681 if (categorized
!= categorizedSorting()) {
682 if (supportsCategorizedSorting()) {
683 Q_ASSERT(m_iconsView
!= 0);
685 Q_ASSERT(m_iconsView
->itemCategorizer() == 0);
686 m_iconsView
->setItemCategorizer(new DolphinItemCategorizer());
688 KItemCategorizer
* categorizer
= m_iconsView
->itemCategorizer();
689 m_iconsView
->setItemCategorizer(0);
693 emit
categorizedSortingChanged();
696 const DolphinView::Sorting sorting
= props
.sorting();
697 if (sorting
!= m_proxyModel
->sorting()) {
698 m_proxyModel
->setSorting(sorting
);
699 emit
sortingChanged(sorting
);
702 const Qt::SortOrder sortOrder
= props
.sortOrder();
703 if (sortOrder
!= m_proxyModel
->sortOrder()) {
704 m_proxyModel
->setSortOrder(sortOrder
);
705 emit
sortOrderChanged(sortOrder
);
708 KFileItemDelegate::AdditionalInformation info
= props
.additionalInfo();
709 if (info
!= m_fileItemDelegate
->additionalInformation()) {
710 m_controller
->setShowAdditionalInfo(info
!= KFileItemDelegate::NoInformation
);
711 m_fileItemDelegate
->setAdditionalInformation(info
);
712 emit
additionalInfoChanged(info
);
715 const bool showPreview
= props
.showPreview();
716 if (showPreview
!= m_controller
->showPreview()) {
717 m_controller
->setShowPreview(showPreview
);
718 emit
showPreviewChanged();
722 emit
urlChanged(url
);
724 m_statusBar
->clear();
727 void DolphinView::triggerItem(const QModelIndex
& index
)
729 if (!isValidNameIndex(index
)) {
733 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
734 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
735 // items are selected by the user, hence don't trigger the
736 // item specified by 'index'
740 KFileItem
* item
= m_dirModel
->itemForIndex(m_proxyModel
->mapToSource(index
));
745 // Prefer the local path over the URL. This assures that the
746 // volume space information is correct. Assuming that the URL is media:/sda1,
747 // and the local path is /windows/C: For the URL the space info is related
748 // to the root partition (and hence wrong) and for the local path the space
749 // info is related to the windows partition (-> correct).
750 const QString
localPath(item
->localPath());
752 if (localPath
.isEmpty()) {
760 } else if (item
->isFile()) {
761 // allow to browse through ZIP and tar files
762 KMimeType::Ptr mime
= item
->mimeTypePtr();
763 if (mime
->is("application/zip")) {
764 url
.setProtocol("zip");
766 } else if (mime
->is("application/x-tar") ||
767 mime
->is("application/x-tarz") ||
768 mime
->is("application/x-bzip-compressed-tar") ||
769 mime
->is("application/x-compressed-tar") ||
770 mime
->is("application/x-tzo")) {
771 url
.setProtocol("tar");
781 void DolphinView::updateProgress(int percent
)
783 if (m_showProgress
) {
784 m_statusBar
->setProgress(percent
);
788 void DolphinView::updateItemCount()
790 if (m_showProgress
) {
791 m_statusBar
->setProgressText(QString());
792 m_statusBar
->setProgress(100);
793 m_showProgress
= false;
796 KFileItemList
items(m_dirLister
->items());
797 KFileItemList::const_iterator it
= items
.begin();
798 const KFileItemList::const_iterator end
= items
.end();
804 KFileItem
* item
= *it
;
815 m_blockContentsMovedSignal
= false;
816 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
819 void DolphinView::generatePreviews(const KFileItemList
& items
)
821 if (m_controller
->showPreview()) {
823 // Must turn QList<KFileItem *> to QList<KFileItem>...
824 QList
<KFileItem
> itemsToPreview
;
825 foreach( KFileItem
* it
, items
)
826 itemsToPreview
.append( *it
);
828 KIO::PreviewJob
* job
= KIO::filePreview(itemsToPreview
, 128);
829 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
830 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
834 void DolphinView::showPreview(const KFileItem
& item
, const QPixmap
& pixmap
)
836 Q_ASSERT(!item
.isNull());
837 if (item
.url().directory() != m_dirLister
->url().path()) {
838 // the preview job is still working on items of an older URL, hence
839 // the item is not part of the directory model anymore
843 const QModelIndex idx
= m_dirModel
->indexForItem(item
);
844 if (idx
.isValid() && (idx
.column() == 0)) {
845 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
846 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
847 KIconEffect iconEffect
;
848 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
849 m_dirModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
851 m_dirModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
856 void DolphinView::restoreContentsPos()
858 KUrl currentUrl
= m_urlNavigator
->url();
859 if (!currentUrl
.isEmpty()) {
860 QAbstractItemView
* view
= itemView();
861 // TODO: view->setCurrentItem(m_urlNavigator->currentFileName());
862 QPoint pos
= m_urlNavigator
->savedPosition();
863 view
->horizontalScrollBar()->setValue(pos
.x());
864 view
->verticalScrollBar()->setValue(pos
.y());
868 void DolphinView::showInfoMessage(const QString
& msg
)
870 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
873 void DolphinView::showErrorMessage(const QString
& msg
)
875 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
878 void DolphinView::emitSelectionChangedSignal()
880 emit
selectionChanged(DolphinView::selectedItems());
883 void DolphinView::closeFilterBar()
886 emit
showFilterBarChanged(false);
889 void DolphinView::startDirLister(const KUrl
& url
, bool reload
)
891 if (!url
.isValid()) {
892 const QString
location(url
.pathOrUrl());
893 if (location
.isEmpty()) {
894 m_statusBar
->setMessage(i18n("The location is empty."), DolphinStatusBar::Error
);
896 m_statusBar
->setMessage(i18n("The location '%1' is invalid.", location
),
897 DolphinStatusBar::Error
);
902 // Only show the directory loading progress if the status bar does
903 // not contain another progress information. This means that
904 // the directory loading progress information has the lowest priority.
905 const QString
progressText(m_statusBar
->progressText());
906 m_showProgress
= progressText
.isEmpty() ||
907 (progressText
== i18n("Loading folder..."));
908 if (m_showProgress
) {
909 m_statusBar
->setProgressText(i18n("Loading folder..."));
910 m_statusBar
->setProgress(0);
913 m_cutItemsCache
.clear();
914 m_blockContentsMovedSignal
= true;
918 bool keepOldDirs
= isColumnViewActive() && !m_initializeColumnView
;
919 m_initializeColumnView
= false;
925 const KUrl
& dirListerUrl
= m_dirLister
->url();
926 if (dirListerUrl
.isValid()) {
927 const KUrl::List dirs
= m_dirLister
->directories();
930 m_dirLister
->updateDirectory(url
);
934 } else if (m_dirLister
->directories().contains(url
)) {
935 // The dir lister contains the directory already, so
936 // KDirLister::openUrl() may not been invoked twice.
937 m_dirLister
->updateDirectory(url
);
940 const KUrl
& dirListerUrl
= m_dirLister
->url();
941 if ((dirListerUrl
== url
) || !m_dirLister
->url().isParentOf(url
)) {
942 // The current URL is not a child of the dir lister
943 // URL. This may happen when e. g. a bookmark has been selected
944 // and hence the view must be reset.
951 m_dirLister
->openUrl(url
, keepOldDirs
, reload
);
955 QString
DolphinView::defaultStatusBarText() const
957 return KIO::itemsSummaryString(m_fileCount
+ m_folderCount
,
963 QString
DolphinView::selectionStatusBarText() const
966 const KFileItemList list
= selectedItems();
967 if (list
.isEmpty()) {
968 // when an item is triggered, it is temporary selected but selectedItems()
969 // will return an empty list
975 KIO::filesize_t byteSize
= 0;
976 KFileItemList::const_iterator it
= list
.begin();
977 const KFileItemList::const_iterator end
= list
.end();
979 KFileItem
* item
= *it
;
984 byteSize
+= item
->size();
989 if (folderCount
> 0) {
990 text
= i18np("1 Folder selected", "%1 Folders selected", folderCount
);
997 const QString
sizeText(KIO::convertSize(byteSize
));
998 text
+= i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount
, sizeText
);
1004 void DolphinView::showFilterBar(bool show
)
1006 Q_ASSERT(m_filterBar
!= 0);
1008 m_filterBar
->show();
1010 m_filterBar
->hide();
1014 void DolphinView::updateStatusBar()
1016 // As the item count information is less important
1017 // in comparison with other messages, it should only
1019 // - the status bar is empty or
1020 // - shows already the item count information or
1021 // - shows only a not very important information
1022 // - if any progress is given don't show the item count info at all
1023 const QString
msg(m_statusBar
->message());
1024 const bool updateStatusBarMsg
= (msg
.isEmpty() ||
1025 (msg
== m_statusBar
->defaultText()) ||
1026 (m_statusBar
->type() == DolphinStatusBar::Information
)) &&
1027 (m_statusBar
->progress() == 100);
1029 const QString
text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
1030 m_statusBar
->setDefaultText(text
);
1032 if (updateStatusBarMsg
) {
1033 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
1037 void DolphinView::requestActivation()
1039 m_mainWindow
->setActiveView(this);
1042 void DolphinView::changeSelection(const KFileItemList
& selection
)
1045 if (selection
.isEmpty()) {
1048 KUrl baseUrl
= url();
1050 QItemSelection new_selection
;
1051 foreach(KFileItem
* item
, selection
) {
1052 url
= item
->url().upUrl();
1053 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
1054 QModelIndex index
= m_proxyModel
->mapFromSource(m_dirModel
->indexForItem(*item
));
1055 new_selection
.select(index
, index
);
1058 itemView()->selectionModel()->select(new_selection
,
1059 QItemSelectionModel::ClearAndSelect
1060 | QItemSelectionModel::Current
);
1063 void DolphinView::changeNameFilter(const QString
& nameFilter
)
1065 // The name filter of KDirLister does a 'hard' filtering, which
1066 // means that only the items are shown where the names match
1067 // exactly the filter. This is non-transparent for the user, which
1068 // just wants to have a 'soft' filtering: does the name contain
1069 // the filter string?
1070 QString
adjustedFilter(nameFilter
);
1071 adjustedFilter
.insert(0, '*');
1072 adjustedFilter
.append('*');
1074 // Use the ProxyModel to filter:
1075 // This code is #ifdefed as setNameFilter behaves
1076 // slightly different than the QSortFilterProxyModel
1077 // as it will not remove directories. I will ask
1078 // our beloved usability experts for input
1081 m_dirLister
->setNameFilter(adjustedFilter
);
1082 m_dirLister
->emitChanges();
1084 m_proxyModel
->setFilterRegExp(nameFilter
);
1088 void DolphinView::openContextMenu(const QPoint
& pos
)
1090 KFileItem
* item
= 0;
1092 const QModelIndex index
= itemView()->indexAt(pos
);
1093 if (isValidNameIndex(index
)) {
1094 item
= fileItem(index
);
1097 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url());
1101 void DolphinView::dropUrls(const KUrl::List
& urls
,
1102 const QModelIndex
& index
,
1105 KFileItem
* directory
= 0;
1106 if (isValidNameIndex(index
)) {
1107 KFileItem
* item
= fileItem(index
);
1108 Q_ASSERT(item
!= 0);
1109 if (item
->isDir()) {
1110 // the URLs are dropped above a directory
1115 if ((directory
== 0) && (source
== itemView())) {
1116 // The dropping is done into the same viewport where
1117 // the dragging has been started. Just ignore this...
1121 const KUrl
& destination
= (directory
== 0) ? url() :
1123 dropUrls(urls
, destination
);
1126 void DolphinView::dropUrls(const KUrl::List
& urls
,
1127 const KUrl
& destination
)
1129 m_mainWindow
->dropUrls(urls
, destination
);
1132 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
1134 ViewProperties
props(url());
1135 props
.setSorting(sorting
);
1137 m_proxyModel
->setSorting(sorting
);
1139 emit
sortingChanged(sorting
);
1142 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1144 ViewProperties
props(url());
1145 props
.setSortOrder(order
);
1147 m_proxyModel
->setSortOrder(order
);
1149 emit
sortOrderChanged(order
);
1152 void DolphinView::emitContentsMoved()
1154 if (!m_blockContentsMovedSignal
) {
1155 emit
contentsMoved(contentsX(), contentsY());
1159 void DolphinView::updateActivationState()
1161 m_urlNavigator
->setActive(isActive());
1163 emit
urlChanged(url());
1164 emit
selectionChanged(selectedItems());
1168 void DolphinView::updateCutItems()
1170 // restore the icons of all previously selected items to the
1171 // original state...
1172 QList
<CutItem
>::const_iterator it
= m_cutItemsCache
.begin();
1173 QList
<CutItem
>::const_iterator end
= m_cutItemsCache
.end();
1175 const QModelIndex index
= m_dirModel
->indexForUrl((*it
).url
);
1176 if (index
.isValid()) {
1177 m_dirModel
->setData(index
, QIcon((*it
).pixmap
), Qt::DecorationRole
);
1181 m_cutItemsCache
.clear();
1183 // ... and apply an item effect to all currently cut items
1184 applyCutItemEffect();
1187 void DolphinView::showHoverInformation(const QModelIndex
& index
)
1189 if (hasSelection()) {
1193 const KFileItem
* item
= fileItem(index
);
1195 m_statusBar
->setMessage(item
->getStatusBarInfo(), DolphinStatusBar::Default
);
1196 emit
requestItemInfo(item
->url());
1200 void DolphinView::clearHoverInformation()
1202 m_statusBar
->clear();
1206 void DolphinView::createView()
1208 // delete current view
1209 QAbstractItemView
* view
= itemView();
1211 m_topLayout
->removeWidget(view
);
1213 if (view
== m_iconsView
) {
1214 KItemCategorizer
* categorizer
= m_iconsView
->itemCategorizer();
1215 m_iconsView
->setItemCategorizer(0);
1218 view
->deleteLater();
1223 m_fileItemDelegate
= 0;
1226 Q_ASSERT(m_iconsView
== 0);
1227 Q_ASSERT(m_detailsView
== 0);
1228 Q_ASSERT(m_columnView
== 0);
1230 // ... and recreate it representing the current mode
1233 m_iconsView
= new DolphinIconsView(this, m_controller
);
1238 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1239 view
= m_detailsView
;
1243 m_columnView
= new DolphinColumnView(this, m_controller
);
1244 view
= m_columnView
;
1248 Q_ASSERT(view
!= 0);
1250 m_fileItemDelegate
= new KFileItemDelegate(view
);
1251 view
->setItemDelegate(m_fileItemDelegate
);
1253 view
->setModel(m_proxyModel
);
1254 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1256 new KMimeTypeResolver(view
, m_dirModel
);
1257 m_topLayout
->insertWidget(1, view
);
1259 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1260 m_controller
, SLOT(indicateSelectionChange()));
1261 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1262 this, SLOT(emitContentsMoved()));
1263 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1264 this, SLOT(emitContentsMoved()));
1267 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags
)
1269 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
1270 const QAbstractItemModel
* itemModel
= selectionModel
->model();
1272 const QModelIndex topLeft
= itemModel
->index(0, 0);
1273 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
1274 itemModel
->columnCount() - 1);
1276 QItemSelection
selection(topLeft
, bottomRight
);
1277 selectionModel
->select(selection
, flags
);
1280 QAbstractItemView
* DolphinView::itemView() const
1282 if (m_detailsView
!= 0) {
1283 return m_detailsView
;
1284 } else if (m_columnView
!= 0) {
1285 return m_columnView
;
1291 bool DolphinView::isValidNameIndex(const QModelIndex
& index
) const
1293 return index
.isValid() && (index
.column() == KDirModel::Name
);
1296 bool DolphinView::isCutItem(const KFileItem
& item
) const
1298 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1299 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1301 const KUrl
& itemUrl
= item
.url();
1302 KUrl::List::const_iterator it
= cutUrls
.begin();
1303 const KUrl::List::const_iterator end
= cutUrls
.end();
1305 if (*it
== itemUrl
) {
1314 void DolphinView::applyCutItemEffect()
1316 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1317 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
1321 KFileItemList
items(m_dirLister
->items());
1322 KFileItemList::const_iterator it
= items
.begin();
1323 const KFileItemList::const_iterator end
= items
.end();
1325 KFileItem
* item
= *it
;
1326 if (isCutItem(*item
)) {
1327 const QModelIndex index
= m_dirModel
->indexForItem(*item
);
1328 const KFileItem
* item
= m_dirModel
->itemForIndex(index
);
1329 const QVariant value
= m_dirModel
->data(index
, Qt::DecorationRole
);
1330 if ((value
.type() == QVariant::Icon
) && (item
!= 0)) {
1331 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
1332 QPixmap pixmap
= icon
.pixmap(128, 128);
1334 // remember current pixmap for the item to be able
1335 // to restore it when other items get cut
1337 cutItem
.url
= item
->url();
1338 cutItem
.pixmap
= pixmap
;
1339 m_cutItemsCache
.append(cutItem
);
1341 // apply icon effect to the cut item
1342 KIconEffect iconEffect
;
1343 pixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
1344 m_dirModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
1351 #include "dolphinview.moc"