1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "dolphinview.h"
23 #include <QApplication>
26 #include <QItemSelectionModel>
27 #include <QMouseEvent>
28 #include <QVBoxLayout>
32 #include <kdirmodel.h>
33 #include <kfileitemdelegate.h>
34 #include <kfileplacesmodel.h>
36 #include <kiconeffect.h>
37 #include <kio/netaccess.h>
38 #include <kio/renamedialog.h>
39 #include <kio/previewjob.h>
40 #include <kmimetyperesolver.h>
41 #include <konqmimedata.h>
42 #include <konq_operations.h>
45 #include "dolphincolumnview.h"
46 #include "dolphincontroller.h"
47 #include "dolphinstatusbar.h"
48 #include "dolphinmainwindow.h"
49 #include "dolphindirlister.h"
50 #include "dolphinsortfilterproxymodel.h"
51 #include "dolphindetailsview.h"
52 #include "dolphiniconsview.h"
53 #include "dolphincontextmenu.h"
54 #include "dolphinitemcategorizer.h"
55 #include "filterbar.h"
56 #include "renamedialog.h"
57 #include "kurlnavigator.h"
58 #include "viewproperties.h"
59 #include "dolphinsettings.h"
60 #include "dolphin_generalsettings.h"
62 DolphinView::DolphinView(DolphinMainWindow
* mainWindow
,
66 bool showHiddenFiles
) :
68 m_showProgress(false),
69 m_blockContentsMovedSignal(false),
70 m_initializeColumnView(false),
75 m_mainWindow(mainWindow
),
82 m_fileItemDelegate(0),
90 setFocusPolicy(Qt::StrongFocus
);
91 m_topLayout
= new QVBoxLayout(this);
92 m_topLayout
->setSpacing(0);
93 m_topLayout
->setMargin(0);
95 connect(m_mainWindow
, SIGNAL(activeViewChanged()),
96 this, SLOT(updateActivationState()));
98 QClipboard
* clipboard
= QApplication::clipboard();
99 connect(clipboard
, SIGNAL(dataChanged()),
100 this, SLOT(updateCutItems()));
102 m_urlNavigator
= new KUrlNavigator(DolphinSettings::instance().placesModel(), url
, this);
103 m_urlNavigator
->setUrlEditable(DolphinSettings::instance().generalSettings()->editableUrl());
104 m_urlNavigator
->setHomeUrl(DolphinSettings::instance().generalSettings()->homeUrl());
105 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
106 this, SLOT(changeDirectory(const KUrl
&)));
107 connect(m_urlNavigator
, SIGNAL(urlsDropped(const KUrl::List
&, const KUrl
&)),
108 this, SLOT(dropUrls(const KUrl::List
&, const KUrl
&)));
109 connect(m_urlNavigator
, SIGNAL(activated()),
110 this, SLOT(requestActivation()));
111 connect(this, SIGNAL(contentsMoved(int, int)),
112 m_urlNavigator
, SLOT(savePosition(int, int)));
114 m_statusBar
= new DolphinStatusBar(this);
116 m_dirLister
= new DolphinDirLister();
117 m_dirLister
->setAutoUpdate(true);
118 m_dirLister
->setMainWindow(this);
119 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
120 m_dirLister
->setDelayedMimeTypes(true);
122 connect(m_dirLister
, SIGNAL(clear()),
123 this, SLOT(updateStatusBar()));
124 connect(m_dirLister
, SIGNAL(percent(int)),
125 this, SLOT(updateProgress(int)));
126 connect(m_dirLister
, SIGNAL(deleteItem(KFileItem
*)),
127 this, SLOT(updateStatusBar()));
128 connect(m_dirLister
, SIGNAL(completed()),
129 this, SLOT(updateItemCount()));
130 connect(m_dirLister
, SIGNAL(completed()),
131 this, SLOT(updateCutItems()));
132 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
133 this, SLOT(generatePreviews(const KFileItemList
&)));
134 connect(m_dirLister
, SIGNAL(infoMessage(const QString
&)),
135 this, SLOT(showInfoMessage(const QString
&)));
136 connect(m_dirLister
, SIGNAL(errorMessage(const QString
&)),
137 this, SLOT(showErrorMessage(const QString
&)));
139 m_dirModel
= new KDirModel();
140 m_dirModel
->setDirLister(m_dirLister
);
141 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
143 m_proxyModel
= new DolphinSortFilterProxyModel(this);
144 m_proxyModel
->setSourceModel(m_dirModel
);
146 m_controller
= new DolphinController(this);
147 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&)),
148 this, SLOT(openContextMenu(const QPoint
&)));
149 connect(m_controller
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&, QWidget
*)),
150 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&, QWidget
*)));
151 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
152 this, SLOT(updateSorting(DolphinView::Sorting
)));
153 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
154 this, SLOT(updateSortOrder(Qt::SortOrder
)));
155 connect(m_controller
, SIGNAL(itemTriggered(const QModelIndex
&)),
156 this, SLOT(triggerItem(const QModelIndex
&)));
157 connect(m_controller
, SIGNAL(selectionChanged()),
158 this, SLOT(emitSelectionChangedSignal()));
159 connect(m_controller
, SIGNAL(activated()),
160 this, SLOT(requestActivation()));
164 m_iconSize
= K3Icon::SizeMedium
;
166 m_filterBar
= new FilterBar(this);
168 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
169 this, SLOT(changeNameFilter(const QString
&)));
170 connect(m_filterBar
, SIGNAL(closeRequest()),
171 this, SLOT(closeFilterBar()));
173 m_topLayout
->addWidget(m_urlNavigator
);
174 m_topLayout
->addWidget(itemView());
175 m_topLayout
->addWidget(m_filterBar
);
176 m_topLayout
->addWidget(m_statusBar
);
179 DolphinView::~DolphinView()
185 void DolphinView::setUrl(const KUrl
& url
)
187 m_urlNavigator
->setUrl(url
);
188 m_controller
->setUrl(url
);
191 const KUrl
& DolphinView::url() const
193 return m_urlNavigator
->url();
196 bool DolphinView::isActive() const
198 return m_mainWindow
->activeView() == this;
201 void DolphinView::setMode(Mode mode
)
203 if (mode
== m_mode
) {
204 return; // the wished mode is already set
209 if (isColumnViewActive()) {
210 // When changing the mode in the column view, it makes sense
211 // to go back to the root URL of the column view automatically.
212 // Otherwise there it would not be possible to turn off the column view
213 // without focusing the first column.
214 setUrl(m_dirLister
->url());
217 ViewProperties
props(m_urlNavigator
->url());
218 props
.setViewMode(m_mode
);
221 startDirLister(m_urlNavigator
->url());
226 DolphinView::Mode
DolphinView::mode() const
231 void DolphinView::setShowPreview(bool show
)
233 ViewProperties
props(m_urlNavigator
->url());
234 props
.setShowPreview(show
);
236 m_controller
->setShowPreview(show
);
237 emit
showPreviewChanged();
239 startDirLister(m_urlNavigator
->url(), true);
242 bool DolphinView::showPreview() const
244 return m_controller
->showPreview();
247 void DolphinView::setShowHiddenFiles(bool show
)
249 if (m_dirLister
->showingDotFiles() == show
) {
253 ViewProperties
props(m_urlNavigator
->url());
254 props
.setShowHiddenFiles(show
);
256 m_dirLister
->setShowingDotFiles(show
);
257 emit
showHiddenFilesChanged();
259 startDirLister(m_urlNavigator
->url(), true);
262 bool DolphinView::showHiddenFiles() const
264 return m_dirLister
->showingDotFiles();
267 void DolphinView::setCategorizedSorting(bool categorized
)
269 if (!supportsCategorizedSorting() || (categorized
== categorizedSorting())) {
273 Q_ASSERT(m_iconsView
!= 0);
275 Q_ASSERT(m_iconsView
->itemCategorizer() == 0);
276 m_iconsView
->setItemCategorizer(new DolphinItemCategorizer());
278 KItemCategorizer
* categorizer
= m_iconsView
->itemCategorizer();
279 m_iconsView
->setItemCategorizer(0);
283 ViewProperties
props(m_urlNavigator
->url());
284 props
.setCategorizedSorting(categorized
);
287 emit
categorizedSortingChanged();
290 bool DolphinView::categorizedSorting() const
292 if (!supportsCategorizedSorting()) {
296 Q_ASSERT(m_iconsView
!= 0);
297 return m_iconsView
->itemCategorizer() != 0;
300 bool DolphinView::supportsCategorizedSorting() const
302 return m_iconsView
!= 0;
305 void DolphinView::renameSelectedItems()
307 DolphinView
* view
= mainWindow()->activeView();
308 const KUrl::List urls
= selectedUrls();
309 if (urls
.count() > 1) {
310 // More than one item has been selected for renaming. Open
311 // a rename dialog and rename all items afterwards.
312 RenameDialog
dialog(urls
);
313 if (dialog
.exec() == QDialog::Rejected
) {
317 const QString
& newName
= dialog
.newName();
318 if (newName
.isEmpty()) {
319 view
->statusBar()->setMessage(dialog
.errorString(),
320 DolphinStatusBar::Error
);
322 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
323 // as one operation instead of n rename operations like it is done now...
324 Q_ASSERT(newName
.contains('#'));
326 // iterate through all selected items and rename them...
327 const int replaceIndex
= newName
.indexOf('#');
328 Q_ASSERT(replaceIndex
>= 0);
331 KUrl::List::const_iterator it
= urls
.begin();
332 KUrl::List::const_iterator end
= urls
.end();
334 const KUrl
& oldUrl
= *it
;
336 number
.setNum(index
++);
338 QString
name(newName
);
339 name
.replace(replaceIndex
, 1, number
);
341 if (oldUrl
.fileName() != name
) {
342 KUrl newUrl
= oldUrl
;
343 newUrl
.setFileName(name
);
344 m_mainWindow
->rename(oldUrl
, newUrl
);
350 // Only one item has been selected for renaming. Use the custom
351 // renaming mechanism from the views.
352 Q_ASSERT(urls
.count() == 1);
354 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
355 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
356 // is a benefit for the user at all -> let's wait for some input first...
357 RenameDialog
dialog(urls
);
358 if (dialog
.exec() == QDialog::Rejected
) {
362 const QString
& newName
= dialog
.newName();
363 if (newName
.isEmpty()) {
364 view
->statusBar()->setMessage(dialog
.errorString(),
365 DolphinStatusBar::Error
);
367 const KUrl
& oldUrl
= urls
.first();
368 KUrl newUrl
= oldUrl
;
369 newUrl
.setFileName(newName
);
370 m_mainWindow
->rename(oldUrl
, newUrl
);
375 void DolphinView::selectAll()
377 selectAll(QItemSelectionModel::Select
);
380 void DolphinView::invertSelection()
382 selectAll(QItemSelectionModel::Toggle
);
385 DolphinStatusBar
* DolphinView::statusBar() const
390 int DolphinView::contentsX() const
392 return itemView()->horizontalScrollBar()->value();
395 int DolphinView::contentsY() const
397 return itemView()->verticalScrollBar()->value();
400 void DolphinView::emitRequestItemInfo(const KUrl
& url
)
402 emit
requestItemInfo(url
);
405 bool DolphinView::isFilterBarVisible() const
407 return m_filterBar
->isVisible();
410 bool DolphinView::isUrlEditable() const
412 return m_urlNavigator
->isUrlEditable();
415 void DolphinView::zoomIn()
417 m_controller
->triggerZoomIn();
420 void DolphinView::zoomOut()
422 m_controller
->triggerZoomOut();
425 bool DolphinView::isZoomInPossible() const
427 return m_controller
->isZoomInPossible();
430 bool DolphinView::isZoomOutPossible() const
432 return m_controller
->isZoomOutPossible();
435 void DolphinView::setSorting(Sorting sorting
)
437 if (sorting
!= this->sorting()) {
438 updateSorting(sorting
);
442 DolphinView::Sorting
DolphinView::sorting() const
444 return m_proxyModel
->sorting();
447 void DolphinView::setSortOrder(Qt::SortOrder order
)
449 if (sortOrder() != order
) {
450 updateSortOrder(order
);
454 Qt::SortOrder
DolphinView::sortOrder() const
456 return m_proxyModel
->sortOrder();
459 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info
)
461 ViewProperties
props(m_urlNavigator
->url());
462 props
.setAdditionalInfo(info
);
464 m_fileItemDelegate
->setAdditionalInformation(info
);
466 emit
additionalInfoChanged(info
);
467 startDirLister(m_urlNavigator
->url(), true);
470 KFileItemDelegate::AdditionalInformation
DolphinView::additionalInfo() const
472 return m_fileItemDelegate
->additionalInformation();
475 void DolphinView::goBack()
477 m_urlNavigator
->goBack();
480 void DolphinView::goForward()
482 m_urlNavigator
->goForward();
485 void DolphinView::goUp()
487 m_urlNavigator
->goUp();
490 void DolphinView::goHome()
492 m_urlNavigator
->goHome();
495 void DolphinView::setUrlEditable(bool editable
)
497 m_urlNavigator
->setUrlEditable(editable
);
500 bool DolphinView::hasSelection() const
502 return itemView()->selectionModel()->hasSelection();
505 void DolphinView::clearSelection()
507 itemView()->selectionModel()->clear();
510 KFileItemList
DolphinView::selectedItems() const
512 const QAbstractItemView
* view
= itemView();
514 // Our view has a selection, we will map them back to the DirModel
515 // and then fill the KFileItemList.
516 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
518 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
519 KFileItemList itemList
;
521 const QModelIndexList indexList
= selection
.indexes();
522 QModelIndexList::const_iterator end
= indexList
.end();
523 for (QModelIndexList::const_iterator it
= indexList
.begin(); it
!= end
; ++it
) {
524 Q_ASSERT((*it
).isValid());
526 KFileItem
* item
= m_dirModel
->itemForIndex(*it
);
528 itemList
.append(item
);
535 KUrl::List
DolphinView::selectedUrls() const
539 const KFileItemList list
= selectedItems();
540 KFileItemList::const_iterator it
= list
.begin();
541 const KFileItemList::const_iterator end
= list
.end();
543 KFileItem
* item
= *it
;
544 urls
.append(item
->url());
551 KFileItem
* DolphinView::fileItem(const QModelIndex index
) const
553 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
554 return m_dirModel
->itemForIndex(dirModelIndex
);
557 void DolphinView::rename(const KUrl
& source
, const QString
& newName
)
561 if (newName
.isEmpty() || (source
.fileName() == newName
)) {
565 KUrl
dest(source
.upUrl());
566 dest
.addPath(newName
);
568 const bool destExists
= KIO::NetAccess::exists(dest
,
570 mainWindow()->activeView());
572 // the destination already exists, hence ask the user
574 KIO::RenameDialog
renameDialog(this,
575 i18n("File Already Exists"),
579 switch (renameDialog
.exec()) {
580 case KIO::R_OVERWRITE
:
581 // the destination should be overwritten
582 ok
= KIO::NetAccess::file_move(source
, dest
, -1, true);
585 case KIO::R_RENAME
: {
586 // a new name for the destination has been used
587 KUrl
newDest(renameDialog
.newDestUrl());
588 ok
= KIO::NetAccess::file_move(source
, newDest
);
593 // the renaming operation has been canceled
597 // no destination exists, hence just move the file to
599 ok
= KIO::NetAccess::file_move(source
, dest
);
602 const QString destFileName
= dest
.fileName();
604 m_statusBar
->setMessage(i18n("Renamed file '%1' to '%2'.", source
.fileName(), destFileName
),
605 DolphinStatusBar::OperationCompleted
);
607 KonqOperations::rename(this, source
, destFileName
);
609 m_statusBar
->setMessage(i18n("Renaming of file '%1' to '%2' failed.", source
.fileName(), destFileName
),
610 DolphinStatusBar::Error
);
614 void DolphinView::reload()
616 const KUrl
& url
= m_urlNavigator
->url();
617 changeDirectory(url
);
618 startDirLister(url
, true);
621 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
623 QWidget::mouseReleaseEvent(event
);
624 mainWindow()->setActiveView(this);
627 DolphinMainWindow
* DolphinView::mainWindow() const
632 void DolphinView::changeDirectory(const KUrl
& url
)
638 const ViewProperties
props(url
);
640 const Mode mode
= props
.viewMode();
641 bool changeMode
= (m_mode
!= mode
);
642 if (changeMode
&& isColumnViewActive()) {
643 // The column view is active. Only change the
644 // mode if the current URL is no child of the column view.
645 if (m_dirLister
->url().isParentOf(url
)) {
655 if (m_mode
== ColumnView
) {
656 // The mode has been changed to the Column View. When starting the dir
657 // lister with DolphinView::startDirLister() it is important to give a
658 // hint that the dir lister may not keep the current directory
659 // although this is the default for showing a hierarchy.
660 m_initializeColumnView
= true;
664 const bool showHiddenFiles
= props
.showHiddenFiles();
665 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
666 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
667 emit
showHiddenFilesChanged();
670 const bool categorized
= props
.categorizedSorting();
671 if (categorized
!= categorizedSorting()) {
672 if (supportsCategorizedSorting()) {
673 Q_ASSERT(m_iconsView
!= 0);
675 Q_ASSERT(m_iconsView
->itemCategorizer() == 0);
676 m_iconsView
->setItemCategorizer(new DolphinItemCategorizer());
678 KItemCategorizer
* categorizer
= m_iconsView
->itemCategorizer();
679 m_iconsView
->setItemCategorizer(0);
683 emit
categorizedSortingChanged();
686 const DolphinView::Sorting sorting
= props
.sorting();
687 if (sorting
!= m_proxyModel
->sorting()) {
688 m_proxyModel
->setSorting(sorting
);
689 emit
sortingChanged(sorting
);
692 const Qt::SortOrder sortOrder
= props
.sortOrder();
693 if (sortOrder
!= m_proxyModel
->sortOrder()) {
694 m_proxyModel
->setSortOrder(sortOrder
);
695 emit
sortOrderChanged(sortOrder
);
698 KFileItemDelegate::AdditionalInformation info
= props
.additionalInfo();
699 if (info
!= m_fileItemDelegate
->additionalInformation()) {
700 m_fileItemDelegate
->setAdditionalInformation(info
);
702 emit
additionalInfoChanged(info
);
705 const bool showPreview
= props
.showPreview();
706 if (showPreview
!= m_controller
->showPreview()) {
707 m_controller
->setShowPreview(showPreview
);
708 emit
showPreviewChanged();
712 emit
urlChanged(url
);
714 m_statusBar
->clear();
717 void DolphinView::triggerItem(const QModelIndex
& index
)
719 if (!isValidNameIndex(index
)) {
723 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
724 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
725 // items are selected by the user, hence don't trigger the
726 // item specified by 'index'
730 KFileItem
* item
= m_dirModel
->itemForIndex(m_proxyModel
->mapToSource(index
));
735 // Prefer the local path over the URL. This assures that the
736 // volume space information is correct. Assuming that the URL is media:/sda1,
737 // and the local path is /windows/C: For the URL the space info is related
738 // to the root partition (and hence wrong) and for the local path the space
739 // info is related to the windows partition (-> correct).
740 const QString
localPath(item
->localPath());
742 if (localPath
.isEmpty()) {
750 } else if (item
->isFile()) {
751 // allow to browse through ZIP and tar files
752 KMimeType::Ptr mime
= item
->mimeTypePtr();
753 if (mime
->is("application/zip")) {
754 url
.setProtocol("zip");
756 } else if (mime
->is("application/x-tar") ||
757 mime
->is("application/x-tarz") ||
758 mime
->is("application/x-bzip-compressed-tar") ||
759 mime
->is("application/x-compressed-tar") ||
760 mime
->is("application/x-tzo")) {
761 url
.setProtocol("tar");
771 void DolphinView::updateProgress(int percent
)
773 if (m_showProgress
) {
774 m_statusBar
->setProgress(percent
);
778 void DolphinView::updateItemCount()
780 if (m_showProgress
) {
781 m_statusBar
->setProgressText(QString());
782 m_statusBar
->setProgress(100);
783 m_showProgress
= false;
786 KFileItemList
items(m_dirLister
->items());
787 KFileItemList::const_iterator it
= items
.begin();
788 const KFileItemList::const_iterator end
= items
.end();
794 KFileItem
* item
= *it
;
805 m_blockContentsMovedSignal
= false;
806 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
809 void DolphinView::generatePreviews(const KFileItemList
& items
)
811 if (m_controller
->showPreview()) {
813 // Must turn QList<KFileItem *> to QList<KFileItem>...
814 QList
<KFileItem
> itemsToPreview
;
815 foreach( KFileItem
* it
, items
)
816 itemsToPreview
.append( *it
);
818 KIO::PreviewJob
* job
= KIO::filePreview(itemsToPreview
, 128);
819 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
820 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
824 void DolphinView::showPreview(const KFileItem
& item
, const QPixmap
& pixmap
)
826 Q_ASSERT(!item
.isNull());
827 if (item
.url().directory() != m_dirLister
->url().path()) {
828 // the preview job is still working on items of an older URL, hence
829 // the item is not part of the directory model anymore
833 const QModelIndex idx
= m_dirModel
->indexForItem(item
);
834 if (idx
.isValid() && (idx
.column() == 0)) {
835 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
836 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
837 KIconEffect iconEffect
;
838 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
839 m_dirModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
841 m_dirModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
846 void DolphinView::restoreContentsPos()
848 KUrl currentUrl
= m_urlNavigator
->url();
849 if (!currentUrl
.isEmpty()) {
850 QAbstractItemView
* view
= itemView();
851 // TODO: view->setCurrentItem(m_urlNavigator->currentFileName());
852 QPoint pos
= m_urlNavigator
->savedPosition();
853 view
->horizontalScrollBar()->setValue(pos
.x());
854 view
->verticalScrollBar()->setValue(pos
.y());
858 void DolphinView::showInfoMessage(const QString
& msg
)
860 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
863 void DolphinView::showErrorMessage(const QString
& msg
)
865 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
868 void DolphinView::emitSelectionChangedSignal()
870 emit
selectionChanged(DolphinView::selectedItems());
873 void DolphinView::closeFilterBar()
876 emit
showFilterBarChanged(false);
879 void DolphinView::startDirLister(const KUrl
& url
, bool reload
)
881 if (!url
.isValid()) {
882 const QString
location(url
.pathOrUrl());
883 if (location
.isEmpty()) {
884 m_statusBar
->setMessage(i18n("The location is empty."), DolphinStatusBar::Error
);
886 m_statusBar
->setMessage(i18n("The location '%1' is invalid.", location
),
887 DolphinStatusBar::Error
);
892 // Only show the directory loading progress if the status bar does
893 // not contain another progress information. This means that
894 // the directory loading progress information has the lowest priority.
895 const QString
progressText(m_statusBar
->progressText());
896 m_showProgress
= progressText
.isEmpty() ||
897 (progressText
== i18n("Loading folder..."));
898 if (m_showProgress
) {
899 m_statusBar
->setProgressText(i18n("Loading folder..."));
900 m_statusBar
->setProgress(0);
903 m_cutItemsCache
.clear();
904 m_blockContentsMovedSignal
= true;
908 bool keepOldDirs
= isColumnViewActive() && !m_initializeColumnView
;
909 m_initializeColumnView
= false;
915 const KUrl
& dirListerUrl
= m_dirLister
->url();
916 if (dirListerUrl
.isValid()) {
917 const KUrl::List dirs
= m_dirLister
->directories();
920 m_dirLister
->updateDirectory(url
);
924 } else if (m_dirLister
->directories().contains(url
)) {
925 // The dir lister contains the directory already, so
926 // KDirLister::openUrl() may not been invoked twice.
927 m_dirLister
->updateDirectory(url
);
930 const KUrl
& dirListerUrl
= m_dirLister
->url();
931 if ((dirListerUrl
== url
) || !m_dirLister
->url().isParentOf(url
)) {
932 // The current URL is not a child of the dir lister
933 // URL. This may happen when e. g. a bookmark has been selected
934 // and hence the view must be reset.
941 m_dirLister
->openUrl(url
, keepOldDirs
, reload
);
945 QString
DolphinView::defaultStatusBarText() const
947 return KIO::itemsSummaryString(m_fileCount
+ m_folderCount
,
953 QString
DolphinView::selectionStatusBarText() const
956 const KFileItemList list
= selectedItems();
957 if (list
.isEmpty()) {
958 // when an item is triggered, it is temporary selected but selectedItems()
959 // will return an empty list
965 KIO::filesize_t byteSize
= 0;
966 KFileItemList::const_iterator it
= list
.begin();
967 const KFileItemList::const_iterator end
= list
.end();
969 KFileItem
* item
= *it
;
974 byteSize
+= item
->size();
979 if (folderCount
> 0) {
980 text
= i18np("1 Folder selected", "%1 Folders selected", folderCount
);
987 const QString
sizeText(KIO::convertSize(byteSize
));
988 text
+= i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount
, sizeText
);
994 void DolphinView::showFilterBar(bool show
)
996 Q_ASSERT(m_filterBar
!= 0);
1000 m_filterBar
->hide();
1004 void DolphinView::updateStatusBar()
1006 // As the item count information is less important
1007 // in comparison with other messages, it should only
1009 // - the status bar is empty or
1010 // - shows already the item count information or
1011 // - shows only a not very important information
1012 // - if any progress is given don't show the item count info at all
1013 const QString
msg(m_statusBar
->message());
1014 const bool updateStatusBarMsg
= (msg
.isEmpty() ||
1015 (msg
== m_statusBar
->defaultText()) ||
1016 (m_statusBar
->type() == DolphinStatusBar::Information
)) &&
1017 (m_statusBar
->progress() == 100);
1019 const QString
text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
1020 m_statusBar
->setDefaultText(text
);
1022 if (updateStatusBarMsg
) {
1023 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
1027 void DolphinView::requestActivation()
1029 m_mainWindow
->setActiveView(this);
1032 void DolphinView::changeSelection(const KFileItemList
& selection
)
1035 if (selection
.isEmpty()) {
1038 KUrl baseUrl
= url();
1040 QItemSelection new_selection
;
1041 foreach(KFileItem
* item
, selection
) {
1042 url
= item
->url().upUrl();
1043 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
1044 QModelIndex index
= m_proxyModel
->mapFromSource(m_dirModel
->indexForItem(*item
));
1045 new_selection
.select(index
, index
);
1048 itemView()->selectionModel()->select(new_selection
,
1049 QItemSelectionModel::ClearAndSelect
1050 | QItemSelectionModel::Current
);
1053 void DolphinView::changeNameFilter(const QString
& nameFilter
)
1055 // The name filter of KDirLister does a 'hard' filtering, which
1056 // means that only the items are shown where the names match
1057 // exactly the filter. This is non-transparent for the user, which
1058 // just wants to have a 'soft' filtering: does the name contain
1059 // the filter string?
1060 QString
adjustedFilter(nameFilter
);
1061 adjustedFilter
.insert(0, '*');
1062 adjustedFilter
.append('*');
1064 // Use the ProxyModel to filter:
1065 // This code is #ifdefed as setNameFilter behaves
1066 // slightly different than the QSortFilterProxyModel
1067 // as it will not remove directories. I will ask
1068 // our beloved usability experts for input
1071 m_dirLister
->setNameFilter(adjustedFilter
);
1072 m_dirLister
->emitChanges();
1074 m_proxyModel
->setFilterRegExp(nameFilter
);
1078 void DolphinView::openContextMenu(const QPoint
& pos
)
1080 KFileItem
* item
= 0;
1082 const QModelIndex index
= itemView()->indexAt(pos
);
1083 if (isValidNameIndex(index
)) {
1084 item
= fileItem(index
);
1087 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url());
1091 void DolphinView::dropUrls(const KUrl::List
& urls
,
1092 const QModelIndex
& index
,
1095 KFileItem
* directory
= 0;
1096 if (isValidNameIndex(index
)) {
1097 KFileItem
* item
= fileItem(index
);
1098 Q_ASSERT(item
!= 0);
1099 if (item
->isDir()) {
1100 // the URLs are dropped above a directory
1105 if ((directory
== 0) && (source
== itemView())) {
1106 // The dropping is done into the same viewport where
1107 // the dragging has been started. Just ignore this...
1111 const KUrl
& destination
= (directory
== 0) ? url() :
1113 dropUrls(urls
, destination
);
1116 void DolphinView::dropUrls(const KUrl::List
& urls
,
1117 const KUrl
& destination
)
1119 m_mainWindow
->dropUrls(urls
, destination
);
1122 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
1124 ViewProperties
props(url());
1125 props
.setSorting(sorting
);
1127 m_proxyModel
->setSorting(sorting
);
1129 emit
sortingChanged(sorting
);
1132 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1134 ViewProperties
props(url());
1135 props
.setSortOrder(order
);
1137 m_proxyModel
->setSortOrder(order
);
1139 emit
sortOrderChanged(order
);
1142 void DolphinView::emitContentsMoved()
1144 if (!m_blockContentsMovedSignal
) {
1145 emit
contentsMoved(contentsX(), contentsY());
1149 void DolphinView::updateActivationState()
1151 m_urlNavigator
->setActive(isActive());
1153 emit
urlChanged(url());
1154 emit
selectionChanged(selectedItems());
1158 void DolphinView::updateCutItems()
1160 // restore the icons of all previously selected items to the
1161 // original state...
1162 QList
<CutItem
>::const_iterator it
= m_cutItemsCache
.begin();
1163 QList
<CutItem
>::const_iterator end
= m_cutItemsCache
.end();
1165 const QModelIndex index
= m_dirModel
->indexForUrl((*it
).url
);
1166 if (index
.isValid()) {
1167 m_dirModel
->setData(index
, QIcon((*it
).pixmap
), Qt::DecorationRole
);
1171 m_cutItemsCache
.clear();
1173 // ... and apply an item effect to all currently cut items
1174 applyCutItemEffect();
1177 void DolphinView::createView()
1179 // delete current view
1180 QAbstractItemView
* view
= itemView();
1182 m_topLayout
->removeWidget(view
);
1184 if (view
== m_iconsView
) {
1185 KItemCategorizer
* categorizer
= m_iconsView
->itemCategorizer();
1186 m_iconsView
->setItemCategorizer(0);
1189 view
->deleteLater();
1194 m_fileItemDelegate
= 0;
1197 Q_ASSERT(m_iconsView
== 0);
1198 Q_ASSERT(m_detailsView
== 0);
1199 Q_ASSERT(m_columnView
== 0);
1201 // ... and recreate it representing the current mode
1204 m_iconsView
= new DolphinIconsView(this, m_controller
);
1209 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1210 view
= m_detailsView
;
1214 m_columnView
= new DolphinColumnView(this, m_controller
);
1215 view
= m_columnView
;
1219 Q_ASSERT(view
!= 0);
1221 m_fileItemDelegate
= new KFileItemDelegate(view
);
1222 view
->setItemDelegate(m_fileItemDelegate
);
1224 view
->setModel(m_proxyModel
);
1225 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1227 new KMimeTypeResolver(view
, m_dirModel
);
1228 m_topLayout
->insertWidget(1, view
);
1230 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1231 m_controller
, SLOT(indicateSelectionChange()));
1232 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1233 this, SLOT(emitContentsMoved()));
1234 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1235 this, SLOT(emitContentsMoved()));
1238 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags
)
1240 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
1241 const QAbstractItemModel
* itemModel
= selectionModel
->model();
1243 const QModelIndex topLeft
= itemModel
->index(0, 0);
1244 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
1245 itemModel
->columnCount() - 1);
1247 QItemSelection
selection(topLeft
, bottomRight
);
1248 selectionModel
->select(selection
, flags
);
1251 QAbstractItemView
* DolphinView::itemView() const
1253 if (m_detailsView
!= 0) {
1254 return m_detailsView
;
1255 } else if (m_columnView
!= 0) {
1256 return m_columnView
;
1262 bool DolphinView::isValidNameIndex(const QModelIndex
& index
) const
1264 return index
.isValid() && (index
.column() == KDirModel::Name
);
1267 bool DolphinView::isCutItem(const KFileItem
& item
) const
1269 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1270 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1272 const KUrl
& itemUrl
= item
.url();
1273 KUrl::List::const_iterator it
= cutUrls
.begin();
1274 const KUrl::List::const_iterator end
= cutUrls
.end();
1276 if (*it
== itemUrl
) {
1285 void DolphinView::applyCutItemEffect()
1287 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1288 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
1292 KFileItemList
items(m_dirLister
->items());
1293 KFileItemList::const_iterator it
= items
.begin();
1294 const KFileItemList::const_iterator end
= items
.end();
1296 KFileItem
* item
= *it
;
1297 if (isCutItem(*item
)) {
1298 const QModelIndex index
= m_dirModel
->indexForItem(*item
);
1299 const KFileItem
* item
= m_dirModel
->itemForIndex(index
);
1300 const QVariant value
= m_dirModel
->data(index
, Qt::DecorationRole
);
1301 if ((value
.type() == QVariant::Icon
) && (item
!= 0)) {
1302 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
1303 QPixmap pixmap
= icon
.pixmap(128, 128);
1305 // remember current pixmap for the item to be able
1306 // to restore it when other items get cut
1308 cutItem
.url
= item
->url();
1309 cutItem
.pixmap
= pixmap
;
1310 m_cutItemsCache
.append(cutItem
);
1312 // apply icon effect to the cut item
1313 KIconEffect iconEffect
;
1314 pixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
1315 m_dirModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
1322 #include "dolphinview.moc"