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()));
163 m_iconSize
= K3Icon::SizeMedium
;
165 m_filterBar
= new FilterBar(this);
167 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
168 this, SLOT(changeNameFilter(const QString
&)));
169 connect(m_filterBar
, SIGNAL(closeRequest()),
170 this, SLOT(closeFilterBar()));
172 m_topLayout
->addWidget(m_urlNavigator
);
173 m_topLayout
->addWidget(itemView());
174 m_topLayout
->addWidget(m_filterBar
);
175 m_topLayout
->addWidget(m_statusBar
);
178 DolphinView::~DolphinView()
184 void DolphinView::setUrl(const KUrl
& url
)
186 m_urlNavigator
->setUrl(url
);
187 m_controller
->setUrl(url
);
190 const KUrl
& DolphinView::url() const
192 return m_urlNavigator
->url();
195 bool DolphinView::isActive() const
197 return m_mainWindow
->activeView() == this;
200 void DolphinView::setMode(Mode mode
)
202 if (mode
== m_mode
) {
203 return; // the wished mode is already set
208 if (isColumnViewActive()) {
209 // When changing the mode in the column view, it makes sense
210 // to go back to the root URL of the column view automatically.
211 // Otherwise there it would not be possible to turn off the column view
212 // without focusing the first column.
213 setUrl(m_dirLister
->url());
216 ViewProperties
props(m_urlNavigator
->url());
217 props
.setViewMode(m_mode
);
220 startDirLister(m_urlNavigator
->url());
225 DolphinView::Mode
DolphinView::mode() const
230 void DolphinView::setShowPreview(bool show
)
232 ViewProperties
props(m_urlNavigator
->url());
233 props
.setShowPreview(show
);
235 m_controller
->setShowPreview(show
);
236 emit
showPreviewChanged();
238 startDirLister(m_urlNavigator
->url(), true);
241 bool DolphinView::showPreview() const
243 return m_controller
->showPreview();
246 void DolphinView::setShowHiddenFiles(bool show
)
248 if (m_dirLister
->showingDotFiles() == show
) {
252 ViewProperties
props(m_urlNavigator
->url());
253 props
.setShowHiddenFiles(show
);
255 m_dirLister
->setShowingDotFiles(show
);
256 emit
showHiddenFilesChanged();
258 startDirLister(m_urlNavigator
->url(), true);
261 bool DolphinView::showHiddenFiles() const
263 return m_dirLister
->showingDotFiles();
266 void DolphinView::setCategorizedSorting(bool categorized
)
268 if (!supportsCategorizedSorting() || (categorized
== categorizedSorting())) {
272 Q_ASSERT(m_iconsView
!= 0);
274 Q_ASSERT(m_iconsView
->itemCategorizer() == 0);
275 m_iconsView
->setItemCategorizer(new DolphinItemCategorizer());
277 KItemCategorizer
* categorizer
= m_iconsView
->itemCategorizer();
278 m_iconsView
->setItemCategorizer(0);
282 ViewProperties
props(m_urlNavigator
->url());
283 props
.setCategorizedSorting(categorized
);
286 emit
categorizedSortingChanged();
289 bool DolphinView::categorizedSorting() const
291 if (!supportsCategorizedSorting()) {
295 Q_ASSERT(m_iconsView
!= 0);
296 return m_iconsView
->itemCategorizer() != 0;
299 bool DolphinView::supportsCategorizedSorting() const
301 return m_iconsView
!= 0;
304 void DolphinView::renameSelectedItems()
306 DolphinView
* view
= mainWindow()->activeView();
307 const KUrl::List urls
= selectedUrls();
308 if (urls
.count() > 1) {
309 // More than one item has been selected for renaming. Open
310 // a rename dialog and rename all items afterwards.
311 RenameDialog
dialog(urls
);
312 if (dialog
.exec() == QDialog::Rejected
) {
316 const QString
& newName
= dialog
.newName();
317 if (newName
.isEmpty()) {
318 view
->statusBar()->setMessage(dialog
.errorString(),
319 DolphinStatusBar::Error
);
321 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
322 // as one operation instead of n rename operations like it is done now...
323 Q_ASSERT(newName
.contains('#'));
325 // iterate through all selected items and rename them...
326 const int replaceIndex
= newName
.indexOf('#');
327 Q_ASSERT(replaceIndex
>= 0);
330 KUrl::List::const_iterator it
= urls
.begin();
331 KUrl::List::const_iterator end
= urls
.end();
333 const KUrl
& oldUrl
= *it
;
335 number
.setNum(index
++);
337 QString
name(newName
);
338 name
.replace(replaceIndex
, 1, number
);
340 if (oldUrl
.fileName() != name
) {
341 KUrl newUrl
= oldUrl
;
342 newUrl
.setFileName(name
);
343 m_mainWindow
->rename(oldUrl
, newUrl
);
349 // Only one item has been selected for renaming. Use the custom
350 // renaming mechanism from the views.
351 Q_ASSERT(urls
.count() == 1);
353 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
354 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
355 // is a benefit for the user at all -> let's wait for some input first...
356 RenameDialog
dialog(urls
);
357 if (dialog
.exec() == QDialog::Rejected
) {
361 const QString
& newName
= dialog
.newName();
362 if (newName
.isEmpty()) {
363 view
->statusBar()->setMessage(dialog
.errorString(),
364 DolphinStatusBar::Error
);
366 const KUrl
& oldUrl
= urls
.first();
367 KUrl newUrl
= oldUrl
;
368 newUrl
.setFileName(newName
);
369 m_mainWindow
->rename(oldUrl
, newUrl
);
374 void DolphinView::selectAll()
376 selectAll(QItemSelectionModel::Select
);
379 void DolphinView::invertSelection()
381 selectAll(QItemSelectionModel::Toggle
);
384 DolphinStatusBar
* DolphinView::statusBar() const
389 int DolphinView::contentsX() const
391 return itemView()->horizontalScrollBar()->value();
394 int DolphinView::contentsY() const
396 return itemView()->verticalScrollBar()->value();
399 void DolphinView::emitRequestItemInfo(const KUrl
& url
)
401 emit
requestItemInfo(url
);
404 bool DolphinView::isFilterBarVisible() const
406 return m_filterBar
->isVisible();
409 bool DolphinView::isUrlEditable() const
411 return m_urlNavigator
->isUrlEditable();
414 void DolphinView::zoomIn()
416 m_controller
->triggerZoomIn();
419 void DolphinView::zoomOut()
421 m_controller
->triggerZoomOut();
424 bool DolphinView::isZoomInPossible() const
426 return m_controller
->isZoomInPossible();
429 bool DolphinView::isZoomOutPossible() const
431 return m_controller
->isZoomOutPossible();
434 void DolphinView::setSorting(Sorting sorting
)
436 if (sorting
!= this->sorting()) {
437 updateSorting(sorting
);
441 DolphinView::Sorting
DolphinView::sorting() const
443 return m_proxyModel
->sorting();
446 void DolphinView::setSortOrder(Qt::SortOrder order
)
448 if (sortOrder() != order
) {
449 updateSortOrder(order
);
453 Qt::SortOrder
DolphinView::sortOrder() const
455 return m_proxyModel
->sortOrder();
458 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info
)
460 ViewProperties
props(m_urlNavigator
->url());
461 props
.setAdditionalInfo(info
);
463 m_controller
->setShowAdditionalInfo(info
!= KFileItemDelegate::NoInformation
);
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::refresh()
626 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
628 QWidget::mouseReleaseEvent(event
);
629 mainWindow()->setActiveView(this);
632 DolphinMainWindow
* DolphinView::mainWindow() const
637 void DolphinView::changeDirectory(const KUrl
& url
)
643 const ViewProperties
props(url
);
645 const Mode mode
= props
.viewMode();
646 bool changeMode
= (m_mode
!= mode
);
647 if (changeMode
&& isColumnViewActive()) {
648 // The column view is active. Only change the
649 // mode if the current URL is no child of the column view.
650 if (m_dirLister
->url().isParentOf(url
)) {
660 if (m_mode
== ColumnView
) {
661 // The mode has been changed to the Column View. When starting the dir
662 // lister with DolphinView::startDirLister() it is important to give a
663 // hint that the dir lister may not keep the current directory
664 // although this is the default for showing a hierarchy.
665 m_initializeColumnView
= true;
669 const bool showHiddenFiles
= props
.showHiddenFiles();
670 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
671 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
672 emit
showHiddenFilesChanged();
675 const bool categorized
= props
.categorizedSorting();
676 if (categorized
!= categorizedSorting()) {
677 if (supportsCategorizedSorting()) {
678 Q_ASSERT(m_iconsView
!= 0);
680 Q_ASSERT(m_iconsView
->itemCategorizer() == 0);
681 m_iconsView
->setItemCategorizer(new DolphinItemCategorizer());
683 KItemCategorizer
* categorizer
= m_iconsView
->itemCategorizer();
684 m_iconsView
->setItemCategorizer(0);
688 emit
categorizedSortingChanged();
691 const DolphinView::Sorting sorting
= props
.sorting();
692 if (sorting
!= m_proxyModel
->sorting()) {
693 m_proxyModel
->setSorting(sorting
);
694 emit
sortingChanged(sorting
);
697 const Qt::SortOrder sortOrder
= props
.sortOrder();
698 if (sortOrder
!= m_proxyModel
->sortOrder()) {
699 m_proxyModel
->setSortOrder(sortOrder
);
700 emit
sortOrderChanged(sortOrder
);
703 KFileItemDelegate::AdditionalInformation info
= props
.additionalInfo();
704 if (info
!= m_fileItemDelegate
->additionalInformation()) {
705 m_controller
->setShowAdditionalInfo(info
!= KFileItemDelegate::NoInformation
);
706 m_fileItemDelegate
->setAdditionalInformation(info
);
707 emit
additionalInfoChanged(info
);
710 const bool showPreview
= props
.showPreview();
711 if (showPreview
!= m_controller
->showPreview()) {
712 m_controller
->setShowPreview(showPreview
);
713 emit
showPreviewChanged();
717 emit
urlChanged(url
);
719 m_statusBar
->clear();
722 void DolphinView::triggerItem(const QModelIndex
& index
)
724 if (!isValidNameIndex(index
)) {
728 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
729 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
730 // items are selected by the user, hence don't trigger the
731 // item specified by 'index'
735 KFileItem
* item
= m_dirModel
->itemForIndex(m_proxyModel
->mapToSource(index
));
740 // Prefer the local path over the URL. This assures that the
741 // volume space information is correct. Assuming that the URL is media:/sda1,
742 // and the local path is /windows/C: For the URL the space info is related
743 // to the root partition (and hence wrong) and for the local path the space
744 // info is related to the windows partition (-> correct).
745 const QString
localPath(item
->localPath());
747 if (localPath
.isEmpty()) {
755 } else if (item
->isFile()) {
756 // allow to browse through ZIP and tar files
757 KMimeType::Ptr mime
= item
->mimeTypePtr();
758 if (mime
->is("application/zip")) {
759 url
.setProtocol("zip");
761 } else if (mime
->is("application/x-tar") ||
762 mime
->is("application/x-tarz") ||
763 mime
->is("application/x-bzip-compressed-tar") ||
764 mime
->is("application/x-compressed-tar") ||
765 mime
->is("application/x-tzo")) {
766 url
.setProtocol("tar");
776 void DolphinView::updateProgress(int percent
)
778 if (m_showProgress
) {
779 m_statusBar
->setProgress(percent
);
783 void DolphinView::updateItemCount()
785 if (m_showProgress
) {
786 m_statusBar
->setProgressText(QString());
787 m_statusBar
->setProgress(100);
788 m_showProgress
= false;
791 KFileItemList
items(m_dirLister
->items());
792 KFileItemList::const_iterator it
= items
.begin();
793 const KFileItemList::const_iterator end
= items
.end();
799 KFileItem
* item
= *it
;
810 m_blockContentsMovedSignal
= false;
811 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
814 void DolphinView::generatePreviews(const KFileItemList
& items
)
816 if (m_controller
->showPreview()) {
818 // Must turn QList<KFileItem *> to QList<KFileItem>...
819 QList
<KFileItem
> itemsToPreview
;
820 foreach( KFileItem
* it
, items
)
821 itemsToPreview
.append( *it
);
823 KIO::PreviewJob
* job
= KIO::filePreview(itemsToPreview
, 128);
824 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
825 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
829 void DolphinView::showPreview(const KFileItem
& item
, const QPixmap
& pixmap
)
831 Q_ASSERT(!item
.isNull());
832 if (item
.url().directory() != m_dirLister
->url().path()) {
833 // the preview job is still working on items of an older URL, hence
834 // the item is not part of the directory model anymore
838 const QModelIndex idx
= m_dirModel
->indexForItem(item
);
839 if (idx
.isValid() && (idx
.column() == 0)) {
840 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
841 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
842 KIconEffect iconEffect
;
843 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
844 m_dirModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
846 m_dirModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
851 void DolphinView::restoreContentsPos()
853 KUrl currentUrl
= m_urlNavigator
->url();
854 if (!currentUrl
.isEmpty()) {
855 QAbstractItemView
* view
= itemView();
856 // TODO: view->setCurrentItem(m_urlNavigator->currentFileName());
857 QPoint pos
= m_urlNavigator
->savedPosition();
858 view
->horizontalScrollBar()->setValue(pos
.x());
859 view
->verticalScrollBar()->setValue(pos
.y());
863 void DolphinView::showInfoMessage(const QString
& msg
)
865 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
868 void DolphinView::showErrorMessage(const QString
& msg
)
870 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
873 void DolphinView::emitSelectionChangedSignal()
875 emit
selectionChanged(DolphinView::selectedItems());
878 void DolphinView::closeFilterBar()
881 emit
showFilterBarChanged(false);
884 void DolphinView::startDirLister(const KUrl
& url
, bool reload
)
886 if (!url
.isValid()) {
887 const QString
location(url
.pathOrUrl());
888 if (location
.isEmpty()) {
889 m_statusBar
->setMessage(i18n("The location is empty."), DolphinStatusBar::Error
);
891 m_statusBar
->setMessage(i18n("The location '%1' is invalid.", location
),
892 DolphinStatusBar::Error
);
897 // Only show the directory loading progress if the status bar does
898 // not contain another progress information. This means that
899 // the directory loading progress information has the lowest priority.
900 const QString
progressText(m_statusBar
->progressText());
901 m_showProgress
= progressText
.isEmpty() ||
902 (progressText
== i18n("Loading folder..."));
903 if (m_showProgress
) {
904 m_statusBar
->setProgressText(i18n("Loading folder..."));
905 m_statusBar
->setProgress(0);
908 m_cutItemsCache
.clear();
909 m_blockContentsMovedSignal
= true;
913 bool keepOldDirs
= isColumnViewActive() && !m_initializeColumnView
;
914 m_initializeColumnView
= false;
920 const KUrl
& dirListerUrl
= m_dirLister
->url();
921 if (dirListerUrl
.isValid()) {
922 const KUrl::List dirs
= m_dirLister
->directories();
925 m_dirLister
->updateDirectory(url
);
929 } else if (m_dirLister
->directories().contains(url
)) {
930 // The dir lister contains the directory already, so
931 // KDirLister::openUrl() may not been invoked twice.
932 m_dirLister
->updateDirectory(url
);
935 const KUrl
& dirListerUrl
= m_dirLister
->url();
936 if ((dirListerUrl
== url
) || !m_dirLister
->url().isParentOf(url
)) {
937 // The current URL is not a child of the dir lister
938 // URL. This may happen when e. g. a bookmark has been selected
939 // and hence the view must be reset.
946 m_dirLister
->openUrl(url
, keepOldDirs
, reload
);
950 QString
DolphinView::defaultStatusBarText() const
952 return KIO::itemsSummaryString(m_fileCount
+ m_folderCount
,
958 QString
DolphinView::selectionStatusBarText() const
961 const KFileItemList list
= selectedItems();
962 if (list
.isEmpty()) {
963 // when an item is triggered, it is temporary selected but selectedItems()
964 // will return an empty list
970 KIO::filesize_t byteSize
= 0;
971 KFileItemList::const_iterator it
= list
.begin();
972 const KFileItemList::const_iterator end
= list
.end();
974 KFileItem
* item
= *it
;
979 byteSize
+= item
->size();
984 if (folderCount
> 0) {
985 text
= i18np("1 Folder selected", "%1 Folders selected", folderCount
);
992 const QString
sizeText(KIO::convertSize(byteSize
));
993 text
+= i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount
, sizeText
);
999 void DolphinView::showFilterBar(bool show
)
1001 Q_ASSERT(m_filterBar
!= 0);
1003 m_filterBar
->show();
1005 m_filterBar
->hide();
1009 void DolphinView::updateStatusBar()
1011 // As the item count information is less important
1012 // in comparison with other messages, it should only
1014 // - the status bar is empty or
1015 // - shows already the item count information or
1016 // - shows only a not very important information
1017 // - if any progress is given don't show the item count info at all
1018 const QString
msg(m_statusBar
->message());
1019 const bool updateStatusBarMsg
= (msg
.isEmpty() ||
1020 (msg
== m_statusBar
->defaultText()) ||
1021 (m_statusBar
->type() == DolphinStatusBar::Information
)) &&
1022 (m_statusBar
->progress() == 100);
1024 const QString
text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
1025 m_statusBar
->setDefaultText(text
);
1027 if (updateStatusBarMsg
) {
1028 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
1032 void DolphinView::requestActivation()
1034 m_mainWindow
->setActiveView(this);
1037 void DolphinView::changeSelection(const KFileItemList
& selection
)
1040 if (selection
.isEmpty()) {
1043 KUrl baseUrl
= url();
1045 QItemSelection new_selection
;
1046 foreach(KFileItem
* item
, selection
) {
1047 url
= item
->url().upUrl();
1048 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
1049 QModelIndex index
= m_proxyModel
->mapFromSource(m_dirModel
->indexForItem(*item
));
1050 new_selection
.select(index
, index
);
1053 itemView()->selectionModel()->select(new_selection
,
1054 QItemSelectionModel::ClearAndSelect
1055 | QItemSelectionModel::Current
);
1058 void DolphinView::changeNameFilter(const QString
& nameFilter
)
1060 // The name filter of KDirLister does a 'hard' filtering, which
1061 // means that only the items are shown where the names match
1062 // exactly the filter. This is non-transparent for the user, which
1063 // just wants to have a 'soft' filtering: does the name contain
1064 // the filter string?
1065 QString
adjustedFilter(nameFilter
);
1066 adjustedFilter
.insert(0, '*');
1067 adjustedFilter
.append('*');
1069 // Use the ProxyModel to filter:
1070 // This code is #ifdefed as setNameFilter behaves
1071 // slightly different than the QSortFilterProxyModel
1072 // as it will not remove directories. I will ask
1073 // our beloved usability experts for input
1076 m_dirLister
->setNameFilter(adjustedFilter
);
1077 m_dirLister
->emitChanges();
1079 m_proxyModel
->setFilterRegExp(nameFilter
);
1083 void DolphinView::openContextMenu(const QPoint
& pos
)
1085 KFileItem
* item
= 0;
1087 const QModelIndex index
= itemView()->indexAt(pos
);
1088 if (isValidNameIndex(index
)) {
1089 item
= fileItem(index
);
1092 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url());
1096 void DolphinView::dropUrls(const KUrl::List
& urls
,
1097 const QModelIndex
& index
,
1100 KFileItem
* directory
= 0;
1101 if (isValidNameIndex(index
)) {
1102 KFileItem
* item
= fileItem(index
);
1103 Q_ASSERT(item
!= 0);
1104 if (item
->isDir()) {
1105 // the URLs are dropped above a directory
1110 if ((directory
== 0) && (source
== itemView())) {
1111 // The dropping is done into the same viewport where
1112 // the dragging has been started. Just ignore this...
1116 const KUrl
& destination
= (directory
== 0) ? url() :
1118 dropUrls(urls
, destination
);
1121 void DolphinView::dropUrls(const KUrl::List
& urls
,
1122 const KUrl
& destination
)
1124 m_mainWindow
->dropUrls(urls
, destination
);
1127 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
1129 ViewProperties
props(url());
1130 props
.setSorting(sorting
);
1132 m_proxyModel
->setSorting(sorting
);
1134 emit
sortingChanged(sorting
);
1137 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1139 ViewProperties
props(url());
1140 props
.setSortOrder(order
);
1142 m_proxyModel
->setSortOrder(order
);
1144 emit
sortOrderChanged(order
);
1147 void DolphinView::emitContentsMoved()
1149 if (!m_blockContentsMovedSignal
) {
1150 emit
contentsMoved(contentsX(), contentsY());
1154 void DolphinView::updateActivationState()
1156 m_urlNavigator
->setActive(isActive());
1158 emit
urlChanged(url());
1159 emit
selectionChanged(selectedItems());
1163 void DolphinView::updateCutItems()
1165 // restore the icons of all previously selected items to the
1166 // original state...
1167 QList
<CutItem
>::const_iterator it
= m_cutItemsCache
.begin();
1168 QList
<CutItem
>::const_iterator end
= m_cutItemsCache
.end();
1170 const QModelIndex index
= m_dirModel
->indexForUrl((*it
).url
);
1171 if (index
.isValid()) {
1172 m_dirModel
->setData(index
, QIcon((*it
).pixmap
), Qt::DecorationRole
);
1176 m_cutItemsCache
.clear();
1178 // ... and apply an item effect to all currently cut items
1179 applyCutItemEffect();
1182 void DolphinView::createView()
1184 // delete current view
1185 QAbstractItemView
* view
= itemView();
1187 m_topLayout
->removeWidget(view
);
1189 if (view
== m_iconsView
) {
1190 KItemCategorizer
* categorizer
= m_iconsView
->itemCategorizer();
1191 m_iconsView
->setItemCategorizer(0);
1194 view
->deleteLater();
1199 m_fileItemDelegate
= 0;
1202 Q_ASSERT(m_iconsView
== 0);
1203 Q_ASSERT(m_detailsView
== 0);
1204 Q_ASSERT(m_columnView
== 0);
1206 // ... and recreate it representing the current mode
1209 m_iconsView
= new DolphinIconsView(this, m_controller
);
1214 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1215 view
= m_detailsView
;
1219 m_columnView
= new DolphinColumnView(this, m_controller
);
1220 view
= m_columnView
;
1224 Q_ASSERT(view
!= 0);
1226 m_fileItemDelegate
= new KFileItemDelegate(view
);
1227 view
->setItemDelegate(m_fileItemDelegate
);
1229 view
->setModel(m_proxyModel
);
1230 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1232 new KMimeTypeResolver(view
, m_dirModel
);
1233 m_topLayout
->insertWidget(1, view
);
1235 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1236 m_controller
, SLOT(indicateSelectionChange()));
1237 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1238 this, SLOT(emitContentsMoved()));
1239 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1240 this, SLOT(emitContentsMoved()));
1243 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags
)
1245 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
1246 const QAbstractItemModel
* itemModel
= selectionModel
->model();
1248 const QModelIndex topLeft
= itemModel
->index(0, 0);
1249 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
1250 itemModel
->columnCount() - 1);
1252 QItemSelection
selection(topLeft
, bottomRight
);
1253 selectionModel
->select(selection
, flags
);
1256 QAbstractItemView
* DolphinView::itemView() const
1258 if (m_detailsView
!= 0) {
1259 return m_detailsView
;
1260 } else if (m_columnView
!= 0) {
1261 return m_columnView
;
1267 bool DolphinView::isValidNameIndex(const QModelIndex
& index
) const
1269 return index
.isValid() && (index
.column() == KDirModel::Name
);
1272 bool DolphinView::isCutItem(const KFileItem
& item
) const
1274 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1275 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1277 const KUrl
& itemUrl
= item
.url();
1278 KUrl::List::const_iterator it
= cutUrls
.begin();
1279 const KUrl::List::const_iterator end
= cutUrls
.end();
1281 if (*it
== itemUrl
) {
1290 void DolphinView::applyCutItemEffect()
1292 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1293 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
1297 KFileItemList
items(m_dirLister
->items());
1298 KFileItemList::const_iterator it
= items
.begin();
1299 const KFileItemList::const_iterator end
= items
.end();
1301 KFileItem
* item
= *it
;
1302 if (isCutItem(*item
)) {
1303 const QModelIndex index
= m_dirModel
->indexForItem(*item
);
1304 const KFileItem
* item
= m_dirModel
->itemForIndex(index
);
1305 const QVariant value
= m_dirModel
->data(index
, Qt::DecorationRole
);
1306 if ((value
.type() == QVariant::Icon
) && (item
!= 0)) {
1307 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
1308 QPixmap pixmap
= icon
.pixmap(128, 128);
1310 // remember current pixmap for the item to be able
1311 // to restore it when other items get cut
1313 cutItem
.url
= item
->url();
1314 cutItem
.pixmap
= pixmap
;
1315 m_cutItemsCache
.append(cutItem
);
1317 // apply icon effect to the cut item
1318 KIconEffect iconEffect
;
1319 pixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
1320 m_dirModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
1327 #include "dolphinview.moc"