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>
34 #include <kglobalsettings.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);
104 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
105 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
106 m_urlNavigator
->setHomeUrl(settings
->homeUrl());
108 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
109 this, SLOT(changeDirectory(const KUrl
&)));
110 connect(m_urlNavigator
, SIGNAL(urlsDropped(const KUrl::List
&, const KUrl
&)),
111 this, SLOT(dropUrls(const KUrl::List
&, const KUrl
&)));
112 connect(m_urlNavigator
, SIGNAL(activated()),
113 this, SLOT(requestActivation()));
114 connect(this, SIGNAL(contentsMoved(int, int)),
115 m_urlNavigator
, SLOT(savePosition(int, int)));
117 m_statusBar
= new DolphinStatusBar(this);
119 m_dirLister
= new DolphinDirLister();
120 m_dirLister
->setAutoUpdate(true);
121 m_dirLister
->setMainWindow(this);
122 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
123 m_dirLister
->setDelayedMimeTypes(true);
125 connect(m_dirLister
, SIGNAL(clear()),
126 this, SLOT(updateStatusBar()));
127 connect(m_dirLister
, SIGNAL(percent(int)),
128 this, SLOT(updateProgress(int)));
129 connect(m_dirLister
, SIGNAL(deleteItem(KFileItem
*)),
130 this, SLOT(updateStatusBar()));
131 connect(m_dirLister
, SIGNAL(completed()),
132 this, SLOT(updateItemCount()));
133 connect(m_dirLister
, SIGNAL(completed()),
134 this, SLOT(updateCutItems()));
135 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
136 this, SLOT(generatePreviews(const KFileItemList
&)));
137 connect(m_dirLister
, SIGNAL(infoMessage(const QString
&)),
138 this, SLOT(showInfoMessage(const QString
&)));
139 connect(m_dirLister
, SIGNAL(errorMessage(const QString
&)),
140 this, SLOT(showErrorMessage(const QString
&)));
142 m_dirModel
= new KDirModel();
143 m_dirModel
->setDirLister(m_dirLister
);
144 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
146 m_proxyModel
= new DolphinSortFilterProxyModel(this);
147 m_proxyModel
->setSourceModel(m_dirModel
);
149 m_controller
= new DolphinController(this);
150 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&)),
151 this, SLOT(openContextMenu(const QPoint
&)));
152 connect(m_controller
, SIGNAL(urlsDropped(const KUrl::List
&, const QModelIndex
&, QWidget
*)),
153 this, SLOT(dropUrls(const KUrl::List
&, const QModelIndex
&, QWidget
*)));
154 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
155 this, SLOT(updateSorting(DolphinView::Sorting
)));
156 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
157 this, SLOT(updateSortOrder(Qt::SortOrder
)));
158 connect(m_controller
, SIGNAL(itemTriggered(const QModelIndex
&)),
159 this, SLOT(triggerItem(const QModelIndex
&)));
160 connect(m_controller
, SIGNAL(selectionChanged()),
161 this, SLOT(emitSelectionChangedSignal()));
162 connect(m_controller
, SIGNAL(activated()),
163 this, SLOT(requestActivation()));
164 connect(m_controller
, SIGNAL(itemEntered(const QModelIndex
&)),
165 this, SLOT(showHoverInformation(const QModelIndex
&)));
166 connect(m_controller
, SIGNAL(viewportEntered()),
167 this, SLOT(clearHoverInformation()));
171 m_iconSize
= K3Icon::SizeMedium
;
173 m_filterBar
= new FilterBar(this);
174 m_filterBar
->setVisible(settings
->filterBar());
175 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
176 this, SLOT(changeNameFilter(const QString
&)));
177 connect(m_filterBar
, SIGNAL(closeRequest()),
178 this, SLOT(closeFilterBar()));
180 m_topLayout
->addWidget(m_urlNavigator
);
181 m_topLayout
->addWidget(itemView());
182 m_topLayout
->addWidget(m_filterBar
);
183 m_topLayout
->addWidget(m_statusBar
);
186 DolphinView::~DolphinView()
192 void DolphinView::setUrl(const KUrl
& url
)
194 m_urlNavigator
->setUrl(url
);
195 m_controller
->setUrl(url
);
198 const KUrl
& DolphinView::url() const
200 return m_urlNavigator
->url();
203 KUrl
DolphinView::rootUrl() const
205 return isColumnViewActive() ? m_dirLister
->url() : url();
208 bool DolphinView::isActive() const
210 return m_mainWindow
->activeView() == this;
213 void DolphinView::setMode(Mode mode
)
215 if (mode
== m_mode
) {
216 return; // the wished mode is already set
221 if (isColumnViewActive()) {
222 // When changing the mode in the column view, it makes sense
223 // to go back to the root URL of the column view automatically.
224 // Otherwise there it would not be possible to turn off the column view
225 // without focusing the first column.
226 setUrl(m_dirLister
->url());
229 ViewProperties
props(m_urlNavigator
->url());
230 props
.setViewMode(m_mode
);
233 startDirLister(m_urlNavigator
->url());
238 DolphinView::Mode
DolphinView::mode() const
243 void DolphinView::setShowPreview(bool show
)
245 ViewProperties
props(m_urlNavigator
->url());
246 props
.setShowPreview(show
);
248 m_controller
->setShowPreview(show
);
249 emit
showPreviewChanged();
251 startDirLister(m_urlNavigator
->url(), true);
254 bool DolphinView::showPreview() const
256 return m_controller
->showPreview();
259 void DolphinView::setShowHiddenFiles(bool show
)
261 if (m_dirLister
->showingDotFiles() == show
) {
265 ViewProperties
props(m_urlNavigator
->url());
266 props
.setShowHiddenFiles(show
);
268 m_dirLister
->setShowingDotFiles(show
);
269 emit
showHiddenFilesChanged();
271 startDirLister(m_urlNavigator
->url(), true);
274 bool DolphinView::showHiddenFiles() const
276 return m_dirLister
->showingDotFiles();
279 void DolphinView::setCategorizedSorting(bool categorized
)
281 if (!supportsCategorizedSorting() || (categorized
== categorizedSorting())) {
285 Q_ASSERT(m_iconsView
!= 0);
287 Q_ASSERT(m_iconsView
->itemCategorizer() == 0);
288 m_iconsView
->setItemCategorizer(new DolphinItemCategorizer());
290 KItemCategorizer
* categorizer
= m_iconsView
->itemCategorizer();
291 m_iconsView
->setItemCategorizer(0);
295 ViewProperties
props(m_urlNavigator
->url());
296 props
.setCategorizedSorting(categorized
);
299 emit
categorizedSortingChanged();
302 bool DolphinView::categorizedSorting() const
304 if (!supportsCategorizedSorting()) {
308 Q_ASSERT(m_iconsView
!= 0);
309 return m_iconsView
->itemCategorizer() != 0;
312 bool DolphinView::supportsCategorizedSorting() const
314 return m_iconsView
!= 0;
317 void DolphinView::renameSelectedItems()
319 DolphinView
* view
= mainWindow()->activeView();
320 const KUrl::List urls
= selectedUrls();
321 if (urls
.count() > 1) {
322 // More than one item has been selected for renaming. Open
323 // a rename dialog and rename all items afterwards.
324 RenameDialog
dialog(urls
);
325 if (dialog
.exec() == QDialog::Rejected
) {
329 const QString
& newName
= dialog
.newName();
330 if (newName
.isEmpty()) {
331 view
->statusBar()->setMessage(dialog
.errorString(),
332 DolphinStatusBar::Error
);
334 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
335 // as one operation instead of n rename operations like it is done now...
336 Q_ASSERT(newName
.contains('#'));
338 // iterate through all selected items and rename them...
339 const int replaceIndex
= newName
.indexOf('#');
340 Q_ASSERT(replaceIndex
>= 0);
343 KUrl::List::const_iterator it
= urls
.begin();
344 KUrl::List::const_iterator end
= urls
.end();
346 const KUrl
& oldUrl
= *it
;
348 number
.setNum(index
++);
350 QString
name(newName
);
351 name
.replace(replaceIndex
, 1, number
);
353 if (oldUrl
.fileName() != name
) {
354 KUrl newUrl
= oldUrl
;
355 newUrl
.setFileName(name
);
356 m_mainWindow
->rename(oldUrl
, newUrl
);
362 // Only one item has been selected for renaming. Use the custom
363 // renaming mechanism from the views.
364 Q_ASSERT(urls
.count() == 1);
366 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
367 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
368 // is a benefit for the user at all -> let's wait for some input first...
369 RenameDialog
dialog(urls
);
370 if (dialog
.exec() == QDialog::Rejected
) {
374 const QString
& newName
= dialog
.newName();
375 if (newName
.isEmpty()) {
376 view
->statusBar()->setMessage(dialog
.errorString(),
377 DolphinStatusBar::Error
);
379 const KUrl
& oldUrl
= urls
.first();
380 KUrl newUrl
= oldUrl
;
381 newUrl
.setFileName(newName
);
382 m_mainWindow
->rename(oldUrl
, newUrl
);
387 void DolphinView::selectAll()
389 selectAll(QItemSelectionModel::Select
);
392 void DolphinView::invertSelection()
394 selectAll(QItemSelectionModel::Toggle
);
397 DolphinStatusBar
* DolphinView::statusBar() const
402 int DolphinView::contentsX() const
404 return itemView()->horizontalScrollBar()->value();
407 int DolphinView::contentsY() const
409 return itemView()->verticalScrollBar()->value();
412 bool DolphinView::isFilterBarVisible() const
414 return m_filterBar
->isVisible();
417 bool DolphinView::isUrlEditable() const
419 return m_urlNavigator
->isUrlEditable();
422 void DolphinView::zoomIn()
424 m_controller
->triggerZoomIn();
427 void DolphinView::zoomOut()
429 m_controller
->triggerZoomOut();
432 bool DolphinView::isZoomInPossible() const
434 return m_controller
->isZoomInPossible();
437 bool DolphinView::isZoomOutPossible() const
439 return m_controller
->isZoomOutPossible();
442 void DolphinView::setSorting(Sorting sorting
)
444 if (sorting
!= this->sorting()) {
445 updateSorting(sorting
);
449 DolphinView::Sorting
DolphinView::sorting() const
451 return m_proxyModel
->sorting();
454 void DolphinView::setSortOrder(Qt::SortOrder order
)
456 if (sortOrder() != order
) {
457 updateSortOrder(order
);
461 Qt::SortOrder
DolphinView::sortOrder() const
463 return m_proxyModel
->sortOrder();
466 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info
)
468 ViewProperties
props(m_urlNavigator
->url());
469 props
.setAdditionalInfo(info
);
471 m_controller
->setShowAdditionalInfo(info
!= KFileItemDelegate::NoInformation
);
472 m_fileItemDelegate
->setAdditionalInformation(info
);
474 emit
additionalInfoChanged(info
);
475 startDirLister(m_urlNavigator
->url(), true);
478 KFileItemDelegate::AdditionalInformation
DolphinView::additionalInfo() const
480 return m_fileItemDelegate
->additionalInformation();
483 void DolphinView::goBack()
485 m_urlNavigator
->goBack();
488 void DolphinView::goForward()
490 m_urlNavigator
->goForward();
493 void DolphinView::goUp()
495 m_urlNavigator
->goUp();
498 void DolphinView::goHome()
500 m_urlNavigator
->goHome();
503 void DolphinView::setUrlEditable(bool editable
)
505 m_urlNavigator
->setUrlEditable(editable
);
508 bool DolphinView::hasSelection() const
510 return itemView()->selectionModel()->hasSelection();
513 void DolphinView::clearSelection()
515 itemView()->selectionModel()->clear();
518 KFileItemList
DolphinView::selectedItems() const
520 const QAbstractItemView
* view
= itemView();
522 // Our view has a selection, we will map them back to the DirModel
523 // and then fill the KFileItemList.
524 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
526 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
527 KFileItemList itemList
;
529 const QModelIndexList indexList
= selection
.indexes();
530 QModelIndexList::const_iterator end
= indexList
.end();
531 for (QModelIndexList::const_iterator it
= indexList
.begin(); it
!= end
; ++it
) {
532 Q_ASSERT((*it
).isValid());
534 KFileItem
* item
= m_dirModel
->itemForIndex(*it
);
536 itemList
.append(item
);
543 KUrl::List
DolphinView::selectedUrls() const
547 const KFileItemList list
= selectedItems();
548 KFileItemList::const_iterator it
= list
.begin();
549 const KFileItemList::const_iterator end
= list
.end();
551 KFileItem
* item
= *it
;
552 urls
.append(item
->url());
559 KFileItem
* DolphinView::fileItem(const QModelIndex index
) const
561 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
562 return m_dirModel
->itemForIndex(dirModelIndex
);
565 void DolphinView::rename(const KUrl
& source
, const QString
& newName
)
569 if (newName
.isEmpty() || (source
.fileName() == newName
)) {
573 KUrl
dest(source
.upUrl());
574 dest
.addPath(newName
);
576 const bool destExists
= KIO::NetAccess::exists(dest
,
578 mainWindow()->activeView());
580 // the destination already exists, hence ask the user
582 KIO::RenameDialog
renameDialog(this,
583 i18n("File Already Exists"),
587 switch (renameDialog
.exec()) {
588 case KIO::R_OVERWRITE
:
589 // the destination should be overwritten
590 ok
= KIO::NetAccess::file_move(source
, dest
, -1, true);
593 case KIO::R_RENAME
: {
594 // a new name for the destination has been used
595 KUrl
newDest(renameDialog
.newDestUrl());
596 ok
= KIO::NetAccess::file_move(source
, newDest
);
601 // the renaming operation has been canceled
605 // no destination exists, hence just move the file to
607 ok
= KIO::NetAccess::file_move(source
, dest
);
610 const QString destFileName
= dest
.fileName();
612 m_statusBar
->setMessage(i18n("Renamed file '%1' to '%2'.", source
.fileName(), destFileName
),
613 DolphinStatusBar::OperationCompleted
);
615 KonqOperations::rename(this, source
, destFileName
);
617 m_statusBar
->setMessage(i18n("Renaming of file '%1' to '%2' failed.", source
.fileName(), destFileName
),
618 DolphinStatusBar::Error
);
622 void DolphinView::reload()
624 const KUrl
& url
= m_urlNavigator
->url();
625 changeDirectory(url
);
626 startDirLister(url
, true);
629 void DolphinView::refresh()
635 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
637 QWidget::mouseReleaseEvent(event
);
638 mainWindow()->setActiveView(this);
641 DolphinMainWindow
* DolphinView::mainWindow() const
646 void DolphinView::changeDirectory(const KUrl
& url
)
652 const ViewProperties
props(url
);
654 const Mode mode
= props
.viewMode();
655 bool changeMode
= (m_mode
!= mode
);
656 if (changeMode
&& isColumnViewActive()) {
657 // The column view is active. Only change the
658 // mode if the current URL is no child of the column view.
659 if (m_dirLister
->url().isParentOf(url
)) {
669 if (m_mode
== ColumnView
) {
670 // The mode has been changed to the Column View. When starting the dir
671 // lister with DolphinView::startDirLister() it is important to give a
672 // hint that the dir lister may not keep the current directory
673 // although this is the default for showing a hierarchy.
674 m_initializeColumnView
= true;
678 const bool showHiddenFiles
= props
.showHiddenFiles();
679 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
680 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
681 emit
showHiddenFilesChanged();
684 const bool categorized
= props
.categorizedSorting();
685 if (categorized
!= categorizedSorting()) {
686 if (supportsCategorizedSorting()) {
687 Q_ASSERT(m_iconsView
!= 0);
689 Q_ASSERT(m_iconsView
->itemCategorizer() == 0);
690 m_iconsView
->setItemCategorizer(new DolphinItemCategorizer());
692 KItemCategorizer
* categorizer
= m_iconsView
->itemCategorizer();
693 m_iconsView
->setItemCategorizer(0);
697 emit
categorizedSortingChanged();
700 const DolphinView::Sorting sorting
= props
.sorting();
701 if (sorting
!= m_proxyModel
->sorting()) {
702 m_proxyModel
->setSorting(sorting
);
703 emit
sortingChanged(sorting
);
706 const Qt::SortOrder sortOrder
= props
.sortOrder();
707 if (sortOrder
!= m_proxyModel
->sortOrder()) {
708 m_proxyModel
->setSortOrder(sortOrder
);
709 emit
sortOrderChanged(sortOrder
);
712 KFileItemDelegate::AdditionalInformation info
= props
.additionalInfo();
713 if (info
!= m_fileItemDelegate
->additionalInformation()) {
714 m_controller
->setShowAdditionalInfo(info
!= KFileItemDelegate::NoInformation
);
715 m_fileItemDelegate
->setAdditionalInformation(info
);
716 emit
additionalInfoChanged(info
);
719 const bool showPreview
= props
.showPreview();
720 if (showPreview
!= m_controller
->showPreview()) {
721 m_controller
->setShowPreview(showPreview
);
722 emit
showPreviewChanged();
726 emit
urlChanged(url
);
728 m_statusBar
->clear();
731 void DolphinView::triggerItem(const QModelIndex
& index
)
733 if (!isValidNameIndex(index
)) {
735 showHoverInformation(index
);
739 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
740 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
741 // items are selected by the user, hence don't trigger the
742 // item specified by 'index'
746 KFileItem
* item
= m_dirModel
->itemForIndex(m_proxyModel
->mapToSource(index
));
751 // Prefer the local path over the URL. This assures that the
752 // volume space information is correct. Assuming that the URL is media:/sda1,
753 // and the local path is /windows/C: For the URL the space info is related
754 // to the root partition (and hence wrong) and for the local path the space
755 // info is related to the windows partition (-> correct).
756 const QString
localPath(item
->localPath());
758 if (localPath
.isEmpty()) {
766 } else if (item
->isFile()) {
767 // allow to browse through ZIP and tar files
768 KMimeType::Ptr mime
= item
->mimeTypePtr();
769 if (mime
->is("application/zip")) {
770 url
.setProtocol("zip");
772 } else if (mime
->is("application/x-tar") ||
773 mime
->is("application/x-tarz") ||
774 mime
->is("application/x-bzip-compressed-tar") ||
775 mime
->is("application/x-compressed-tar") ||
776 mime
->is("application/x-tzo")) {
777 url
.setProtocol("tar");
787 void DolphinView::updateProgress(int percent
)
789 if (m_showProgress
) {
790 m_statusBar
->setProgress(percent
);
794 void DolphinView::updateItemCount()
796 if (m_showProgress
) {
797 m_statusBar
->setProgressText(QString());
798 m_statusBar
->setProgress(100);
799 m_showProgress
= false;
802 KFileItemList
items(m_dirLister
->items());
803 KFileItemList::const_iterator it
= items
.begin();
804 const KFileItemList::const_iterator end
= items
.end();
810 KFileItem
* item
= *it
;
821 m_blockContentsMovedSignal
= false;
822 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
825 void DolphinView::generatePreviews(const KFileItemList
& items
)
827 if (m_controller
->showPreview()) {
829 // Must turn QList<KFileItem *> to QList<KFileItem>...
830 QList
<KFileItem
> itemsToPreview
;
831 foreach( KFileItem
* it
, items
)
832 itemsToPreview
.append( *it
);
834 KIO::PreviewJob
* job
= KIO::filePreview(itemsToPreview
, 128);
835 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
836 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
840 void DolphinView::showPreview(const KFileItem
& item
, const QPixmap
& pixmap
)
842 Q_ASSERT(!item
.isNull());
843 if (item
.url().directory() != m_dirLister
->url().path()) {
844 // the preview job is still working on items of an older URL, hence
845 // the item is not part of the directory model anymore
849 const QModelIndex idx
= m_dirModel
->indexForItem(item
);
850 if (idx
.isValid() && (idx
.column() == 0)) {
851 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
852 if (KonqMimeData::decodeIsCutSelection(mimeData
) && isCutItem(item
)) {
853 KIconEffect iconEffect
;
854 const QPixmap cutPixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
855 m_dirModel
->setData(idx
, QIcon(cutPixmap
), Qt::DecorationRole
);
857 m_dirModel
->setData(idx
, QIcon(pixmap
), Qt::DecorationRole
);
862 void DolphinView::restoreContentsPos()
864 KUrl currentUrl
= m_urlNavigator
->url();
865 if (!currentUrl
.isEmpty()) {
866 QAbstractItemView
* view
= itemView();
867 // TODO: view->setCurrentItem(m_urlNavigator->currentFileName());
868 QPoint pos
= m_urlNavigator
->savedPosition();
869 view
->horizontalScrollBar()->setValue(pos
.x());
870 view
->verticalScrollBar()->setValue(pos
.y());
874 void DolphinView::showInfoMessage(const QString
& msg
)
876 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
879 void DolphinView::showErrorMessage(const QString
& msg
)
881 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
884 void DolphinView::emitSelectionChangedSignal()
886 emit
selectionChanged(DolphinView::selectedItems());
889 void DolphinView::closeFilterBar()
892 emit
showFilterBarChanged(false);
895 void DolphinView::startDirLister(const KUrl
& url
, bool reload
)
897 if (!url
.isValid()) {
898 const QString
location(url
.pathOrUrl());
899 if (location
.isEmpty()) {
900 m_statusBar
->setMessage(i18n("The location is empty."), DolphinStatusBar::Error
);
902 m_statusBar
->setMessage(i18n("The location '%1' is invalid.", location
),
903 DolphinStatusBar::Error
);
908 // Only show the directory loading progress if the status bar does
909 // not contain another progress information. This means that
910 // the directory loading progress information has the lowest priority.
911 const QString
progressText(m_statusBar
->progressText());
912 m_showProgress
= progressText
.isEmpty() ||
913 (progressText
== i18n("Loading folder..."));
914 if (m_showProgress
) {
915 m_statusBar
->setProgressText(i18n("Loading folder..."));
916 m_statusBar
->setProgress(0);
919 m_cutItemsCache
.clear();
920 m_blockContentsMovedSignal
= true;
924 bool keepOldDirs
= isColumnViewActive() && !m_initializeColumnView
;
925 m_initializeColumnView
= false;
931 const KUrl
& dirListerUrl
= m_dirLister
->url();
932 if (dirListerUrl
.isValid()) {
933 const KUrl::List dirs
= m_dirLister
->directories();
936 m_dirLister
->updateDirectory(url
);
940 } else if (m_dirLister
->directories().contains(url
)) {
941 // The dir lister contains the directory already, so
942 // KDirLister::openUrl() may not been invoked twice.
943 m_dirLister
->updateDirectory(url
);
946 const KUrl
& dirListerUrl
= m_dirLister
->url();
947 if ((dirListerUrl
== url
) || !m_dirLister
->url().isParentOf(url
)) {
948 // The current URL is not a child of the dir lister
949 // URL. This may happen when e. g. a bookmark has been selected
950 // and hence the view must be reset.
957 m_dirLister
->openUrl(url
, keepOldDirs
, reload
);
961 QString
DolphinView::defaultStatusBarText() const
963 return KIO::itemsSummaryString(m_fileCount
+ m_folderCount
,
969 QString
DolphinView::selectionStatusBarText() const
972 const KFileItemList list
= selectedItems();
973 if (list
.isEmpty()) {
974 // when an item is triggered, it is temporary selected but selectedItems()
975 // will return an empty list
981 KIO::filesize_t byteSize
= 0;
982 KFileItemList::const_iterator it
= list
.begin();
983 const KFileItemList::const_iterator end
= list
.end();
985 KFileItem
* item
= *it
;
990 byteSize
+= item
->size();
995 if (folderCount
> 0) {
996 text
= i18np("1 Folder selected", "%1 Folders selected", folderCount
);
1002 if (fileCount
> 0) {
1003 const QString
sizeText(KIO::convertSize(byteSize
));
1004 text
+= i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount
, sizeText
);
1010 void DolphinView::showFilterBar(bool show
)
1012 Q_ASSERT(m_filterBar
!= 0);
1014 m_filterBar
->show();
1016 m_filterBar
->hide();
1020 void DolphinView::updateStatusBar()
1022 // As the item count information is less important
1023 // in comparison with other messages, it should only
1025 // - the status bar is empty or
1026 // - shows already the item count information or
1027 // - shows only a not very important information
1028 // - if any progress is given don't show the item count info at all
1029 const QString
msg(m_statusBar
->message());
1030 const bool updateStatusBarMsg
= (msg
.isEmpty() ||
1031 (msg
== m_statusBar
->defaultText()) ||
1032 (m_statusBar
->type() == DolphinStatusBar::Information
)) &&
1033 (m_statusBar
->progress() == 100);
1035 const QString
text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
1036 m_statusBar
->setDefaultText(text
);
1038 if (updateStatusBarMsg
) {
1039 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
1043 void DolphinView::requestActivation()
1045 m_mainWindow
->setActiveView(this);
1048 void DolphinView::changeSelection(const KFileItemList
& selection
)
1051 if (selection
.isEmpty()) {
1054 KUrl baseUrl
= url();
1056 QItemSelection new_selection
;
1057 foreach(KFileItem
* item
, selection
) {
1058 url
= item
->url().upUrl();
1059 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
1060 QModelIndex index
= m_proxyModel
->mapFromSource(m_dirModel
->indexForItem(*item
));
1061 new_selection
.select(index
, index
);
1064 itemView()->selectionModel()->select(new_selection
,
1065 QItemSelectionModel::ClearAndSelect
1066 | QItemSelectionModel::Current
);
1069 void DolphinView::changeNameFilter(const QString
& nameFilter
)
1071 // The name filter of KDirLister does a 'hard' filtering, which
1072 // means that only the items are shown where the names match
1073 // exactly the filter. This is non-transparent for the user, which
1074 // just wants to have a 'soft' filtering: does the name contain
1075 // the filter string?
1076 QString
adjustedFilter(nameFilter
);
1077 adjustedFilter
.insert(0, '*');
1078 adjustedFilter
.append('*');
1080 // Use the ProxyModel to filter:
1081 // This code is #ifdefed as setNameFilter behaves
1082 // slightly different than the QSortFilterProxyModel
1083 // as it will not remove directories. I will ask
1084 // our beloved usability experts for input
1087 m_dirLister
->setNameFilter(adjustedFilter
);
1088 m_dirLister
->emitChanges();
1090 m_proxyModel
->setFilterRegExp(nameFilter
);
1094 void DolphinView::openContextMenu(const QPoint
& pos
)
1096 KFileItem
* item
= 0;
1098 const QModelIndex index
= itemView()->indexAt(pos
);
1099 if (isValidNameIndex(index
)) {
1100 item
= fileItem(index
);
1103 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url());
1107 void DolphinView::dropUrls(const KUrl::List
& urls
,
1108 const QModelIndex
& index
,
1111 KFileItem
* directory
= 0;
1112 if (isValidNameIndex(index
)) {
1113 KFileItem
* item
= fileItem(index
);
1114 Q_ASSERT(item
!= 0);
1115 if (item
->isDir()) {
1116 // the URLs are dropped above a directory
1121 if ((directory
== 0) && (source
== itemView())) {
1122 // The dropping is done into the same viewport where
1123 // the dragging has been started. Just ignore this...
1127 const KUrl
& destination
= (directory
== 0) ? url() :
1129 dropUrls(urls
, destination
);
1132 void DolphinView::dropUrls(const KUrl::List
& urls
,
1133 const KUrl
& destination
)
1135 m_mainWindow
->dropUrls(urls
, destination
);
1138 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
1140 ViewProperties
props(url());
1141 props
.setSorting(sorting
);
1143 m_proxyModel
->setSorting(sorting
);
1145 emit
sortingChanged(sorting
);
1148 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1150 ViewProperties
props(url());
1151 props
.setSortOrder(order
);
1153 m_proxyModel
->setSortOrder(order
);
1155 emit
sortOrderChanged(order
);
1158 void DolphinView::emitContentsMoved()
1160 if (!m_blockContentsMovedSignal
) {
1161 emit
contentsMoved(contentsX(), contentsY());
1165 void DolphinView::updateActivationState()
1167 m_urlNavigator
->setActive(isActive());
1169 QColor color
= KGlobalSettings::baseColor();
1171 emit
urlChanged(url());
1172 emit
selectionChanged(selectedItems());
1174 // darken the background if the view is inactive
1175 // TODO: does not work for a black background
1176 color
= color
.darker(105);
1179 QWidget
* viewport
= itemView()->viewport();
1181 palette
.setColor(viewport
->backgroundRole(), color
);
1182 viewport
->setPalette(palette
);
1187 void DolphinView::updateCutItems()
1189 // restore the icons of all previously selected items to the
1190 // original state...
1191 QList
<CutItem
>::const_iterator it
= m_cutItemsCache
.begin();
1192 QList
<CutItem
>::const_iterator end
= m_cutItemsCache
.end();
1194 const QModelIndex index
= m_dirModel
->indexForUrl((*it
).url
);
1195 if (index
.isValid()) {
1196 m_dirModel
->setData(index
, QIcon((*it
).pixmap
), Qt::DecorationRole
);
1200 m_cutItemsCache
.clear();
1202 // ... and apply an item effect to all currently cut items
1203 applyCutItemEffect();
1206 void DolphinView::showHoverInformation(const QModelIndex
& index
)
1208 if (hasSelection()) {
1212 const KFileItem
* item
= fileItem(index
);
1214 m_statusBar
->setMessage(item
->getStatusBarInfo(), DolphinStatusBar::Default
);
1215 emit
requestItemInfo(item
->url());
1219 void DolphinView::clearHoverInformation()
1221 m_statusBar
->clear();
1222 emit
requestItemInfo(KUrl());
1226 void DolphinView::createView()
1228 // delete current view
1229 QAbstractItemView
* view
= itemView();
1231 m_topLayout
->removeWidget(view
);
1233 if (view
== m_iconsView
) {
1234 KItemCategorizer
* categorizer
= m_iconsView
->itemCategorizer();
1235 m_iconsView
->setItemCategorizer(0);
1238 view
->deleteLater();
1243 m_fileItemDelegate
= 0;
1246 Q_ASSERT(m_iconsView
== 0);
1247 Q_ASSERT(m_detailsView
== 0);
1248 Q_ASSERT(m_columnView
== 0);
1250 // ... and recreate it representing the current mode
1253 m_iconsView
= new DolphinIconsView(this, m_controller
);
1258 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1259 view
= m_detailsView
;
1263 m_columnView
= new DolphinColumnView(this, m_controller
);
1264 view
= m_columnView
;
1268 Q_ASSERT(view
!= 0);
1270 m_fileItemDelegate
= new KFileItemDelegate(view
);
1271 view
->setItemDelegate(m_fileItemDelegate
);
1273 view
->setModel(m_proxyModel
);
1274 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1276 new KMimeTypeResolver(view
, m_dirModel
);
1277 m_topLayout
->insertWidget(1, view
);
1279 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1280 m_controller
, SLOT(indicateSelectionChange()));
1281 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1282 this, SLOT(emitContentsMoved()));
1283 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1284 this, SLOT(emitContentsMoved()));
1287 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags
)
1289 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
1290 const QAbstractItemModel
* itemModel
= selectionModel
->model();
1292 const QModelIndex topLeft
= itemModel
->index(0, 0);
1293 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
1294 itemModel
->columnCount() - 1);
1296 QItemSelection
selection(topLeft
, bottomRight
);
1297 selectionModel
->select(selection
, flags
);
1300 QAbstractItemView
* DolphinView::itemView() const
1302 if (m_detailsView
!= 0) {
1303 return m_detailsView
;
1304 } else if (m_columnView
!= 0) {
1305 return m_columnView
;
1311 bool DolphinView::isValidNameIndex(const QModelIndex
& index
) const
1313 return index
.isValid() && (index
.column() == KDirModel::Name
);
1316 bool DolphinView::isCutItem(const KFileItem
& item
) const
1318 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1319 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1321 const KUrl
& itemUrl
= item
.url();
1322 KUrl::List::const_iterator it
= cutUrls
.begin();
1323 const KUrl::List::const_iterator end
= cutUrls
.end();
1325 if (*it
== itemUrl
) {
1334 void DolphinView::applyCutItemEffect()
1336 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1337 if (!KonqMimeData::decodeIsCutSelection(mimeData
)) {
1341 KFileItemList
items(m_dirLister
->items());
1342 KFileItemList::const_iterator it
= items
.begin();
1343 const KFileItemList::const_iterator end
= items
.end();
1345 KFileItem
* item
= *it
;
1346 if (isCutItem(*item
)) {
1347 const QModelIndex index
= m_dirModel
->indexForItem(*item
);
1348 const KFileItem
* item
= m_dirModel
->itemForIndex(index
);
1349 const QVariant value
= m_dirModel
->data(index
, Qt::DecorationRole
);
1350 if ((value
.type() == QVariant::Icon
) && (item
!= 0)) {
1351 const QIcon
icon(qvariant_cast
<QIcon
>(value
));
1352 QPixmap pixmap
= icon
.pixmap(128, 128);
1354 // remember current pixmap for the item to be able
1355 // to restore it when other items get cut
1357 cutItem
.url
= item
->url();
1358 cutItem
.pixmap
= pixmap
;
1359 m_cutItemsCache
.append(cutItem
);
1361 // apply icon effect to the cut item
1362 KIconEffect iconEffect
;
1363 pixmap
= iconEffect
.apply(pixmap
, K3Icon::Desktop
, K3Icon::DisabledState
);
1364 m_dirModel
->setData(index
, QIcon(pixmap
), Qt::DecorationRole
);
1371 #include "dolphinview.moc"