]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Dolphin is now KBookmark* free.
[dolphin.git] / src / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 #include "dolphinview.h"
22
23 #include <QApplication>
24 #include <QClipboard>
25 #include <QDropEvent>
26 #include <QItemSelectionModel>
27 #include <QMouseEvent>
28 #include <QVBoxLayout>
29 #include <QTimer>
30 #include <QScrollBar>
31
32 #include <kdirmodel.h>
33 #include <kfileitemdelegate.h>
34 #include <kfileplacesmodel.h>
35 #include <klocale.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>
43 #include <kurl.h>
44
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 "filterbar.h"
55 #include "renamedialog.h"
56 #include "kurlnavigator.h"
57 #include "viewproperties.h"
58 #include "dolphinsettings.h"
59 #include "dolphin_generalsettings.h"
60
61 DolphinView::DolphinView(DolphinMainWindow* mainWindow,
62 QWidget* parent,
63 const KUrl& url,
64 Mode mode,
65 bool showHiddenFiles) :
66 QWidget(parent),
67 m_showProgress(false),
68 m_blockContentsMovedSignal(false),
69 m_mode(mode),
70 m_iconSize(0),
71 m_folderCount(0),
72 m_fileCount(0),
73 m_mainWindow(mainWindow),
74 m_topLayout(0),
75 m_urlNavigator(0),
76 m_controller(0),
77 m_iconsView(0),
78 m_detailsView(0),
79 m_columnView(0),
80 m_fileItemDelegate(0),
81 m_filterBar(0),
82 m_statusBar(0),
83 m_dirModel(0),
84 m_dirLister(0),
85 m_proxyModel(0)
86 {
87 hide();
88 setFocusPolicy(Qt::StrongFocus);
89 m_topLayout = new QVBoxLayout(this);
90 m_topLayout->setSpacing(0);
91 m_topLayout->setMargin(0);
92
93 connect(m_mainWindow, SIGNAL(activeViewChanged()),
94 this, SLOT(updateActivationState()));
95
96 QClipboard* clipboard = QApplication::clipboard();
97 connect(clipboard, SIGNAL(dataChanged()),
98 this, SLOT(updateCutItems()));
99
100 m_urlNavigator = new KUrlNavigator(DolphinSettings::instance().placesModel(), url, this);
101 m_urlNavigator->setUrlEditable(DolphinSettings::instance().generalSettings()->editableUrl());
102 m_urlNavigator->setHomeUrl(DolphinSettings::instance().generalSettings()->homeUrl());
103 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
104 this, SLOT(loadDirectory(const KUrl&)));
105 connect(m_urlNavigator, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&)),
106 this, SLOT(dropUrls(const KUrl::List&, const KUrl&)));
107 connect(m_urlNavigator, SIGNAL(activated()),
108 this, SLOT(requestActivation()));
109 connect(this, SIGNAL(contentsMoved(int, int)),
110 m_urlNavigator, SLOT(savePosition(int, int)));
111
112 m_statusBar = new DolphinStatusBar(this);
113
114 m_dirLister = new DolphinDirLister();
115 m_dirLister->setAutoUpdate(true);
116 m_dirLister->setMainWindow(this);
117 m_dirLister->setShowingDotFiles(showHiddenFiles);
118 m_dirLister->setDelayedMimeTypes(true);
119
120 connect(m_dirLister, SIGNAL(clear()),
121 this, SLOT(updateStatusBar()));
122 connect(m_dirLister, SIGNAL(percent(int)),
123 this, SLOT(updateProgress(int)));
124 connect(m_dirLister, SIGNAL(deleteItem(KFileItem*)),
125 this, SLOT(updateStatusBar()));
126 connect(m_dirLister, SIGNAL(completed()),
127 this, SLOT(updateItemCount()));
128 connect(m_dirLister, SIGNAL(completed()),
129 this, SLOT(updateCutItems()));
130 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
131 this, SLOT(generatePreviews(const KFileItemList&)));
132 connect(m_dirLister, SIGNAL(infoMessage(const QString&)),
133 this, SLOT(showInfoMessage(const QString&)));
134 connect(m_dirLister, SIGNAL(errorMessage(const QString&)),
135 this, SLOT(showErrorMessage(const QString&)));
136
137 m_dirModel = new KDirModel();
138 m_dirModel->setDirLister(m_dirLister);
139 m_dirModel->setDropsAllowed(KDirModel::DropOnDirectory);
140
141 m_proxyModel = new DolphinSortFilterProxyModel(this);
142 m_proxyModel->setSourceModel(m_dirModel);
143
144 m_controller = new DolphinController(this);
145 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
146 this, SLOT(openContextMenu(const QPoint&)));
147 connect(m_controller, SIGNAL(urlsDropped(const KUrl::List&, const QModelIndex&, QWidget*)),
148 this, SLOT(dropUrls(const KUrl::List&, const QModelIndex&, QWidget*)));
149 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
150 this, SLOT(updateSorting(DolphinView::Sorting)));
151 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
152 this, SLOT(updateSortOrder(Qt::SortOrder)));
153 connect(m_controller, SIGNAL(itemTriggered(const QModelIndex&)),
154 this, SLOT(triggerItem(const QModelIndex&)));
155 connect(m_controller, SIGNAL(selectionChanged()),
156 this, SLOT(emitSelectionChangedSignal()));
157 connect(m_controller, SIGNAL(activated()),
158 this, SLOT(requestActivation()));
159
160 createView();
161
162 m_iconSize = K3Icon::SizeMedium;
163
164 m_filterBar = new FilterBar(this);
165 m_filterBar->hide();
166 connect(m_filterBar, SIGNAL(filterChanged(const QString&)),
167 this, SLOT(changeNameFilter(const QString&)));
168 connect(m_filterBar, SIGNAL(closeRequest()),
169 this, SLOT(closeFilterBar()));
170
171 m_topLayout->addWidget(m_urlNavigator);
172 m_topLayout->addWidget(itemView());
173 m_topLayout->addWidget(m_filterBar);
174 m_topLayout->addWidget(m_statusBar);
175 }
176
177 DolphinView::~DolphinView()
178 {
179 delete m_dirLister;
180 m_dirLister = 0;
181 }
182
183 void DolphinView::setUrl(const KUrl& url)
184 {
185 m_urlNavigator->setUrl(url);
186 m_controller->setUrl(url);
187 }
188
189 const KUrl& DolphinView::url() const
190 {
191 return m_urlNavigator->url();
192 }
193
194 bool DolphinView::isActive() const
195 {
196 return m_mainWindow->activeView() == this;
197 }
198
199 void DolphinView::setMode(Mode mode)
200 {
201 if (mode == m_mode) {
202 return; // the wished mode is already set
203 }
204
205 m_mode = mode;
206
207 ViewProperties props(m_urlNavigator->url());
208 props.setViewMode(m_mode);
209
210 createView();
211 startDirLister(m_urlNavigator->url());
212
213 emit modeChanged();
214 }
215
216 DolphinView::Mode DolphinView::mode() const
217 {
218 return m_mode;
219 }
220
221 void DolphinView::setShowPreview(bool show)
222 {
223 ViewProperties props(m_urlNavigator->url());
224 props.setShowPreview(show);
225
226 m_controller->setShowPreview(show);
227
228 emit showPreviewChanged();
229 reload();
230 }
231
232 bool DolphinView::showPreview() const
233 {
234 return m_controller->showPreview();
235 }
236
237 void DolphinView::setShowHiddenFiles(bool show)
238 {
239 if (m_dirLister->showingDotFiles() == show) {
240 return;
241 }
242
243 ViewProperties props(m_urlNavigator->url());
244 props.setShowHiddenFiles(show);
245 props.save();
246
247 m_dirLister->setShowingDotFiles(show);
248 emit showHiddenFilesChanged();
249
250 reload();
251 }
252
253 bool DolphinView::showHiddenFiles() const
254 {
255 return m_dirLister->showingDotFiles();
256 }
257
258 void DolphinView::renameSelectedItems()
259 {
260 DolphinView* view = mainWindow()->activeView();
261 const KUrl::List urls = selectedUrls();
262 if (urls.count() > 1) {
263 // More than one item has been selected for renaming. Open
264 // a rename dialog and rename all items afterwards.
265 RenameDialog dialog(urls);
266 if (dialog.exec() == QDialog::Rejected) {
267 return;
268 }
269
270 const QString& newName = dialog.newName();
271 if (newName.isEmpty()) {
272 view->statusBar()->setMessage(dialog.errorString(),
273 DolphinStatusBar::Error);
274 }
275 else {
276 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
277 // as one operation instead of n rename operations like it is done now...
278 Q_ASSERT(newName.contains('#'));
279
280 // iterate through all selected items and rename them...
281 const int replaceIndex = newName.indexOf('#');
282 Q_ASSERT(replaceIndex >= 0);
283 int index = 1;
284
285 KUrl::List::const_iterator it = urls.begin();
286 KUrl::List::const_iterator end = urls.end();
287 while (it != end) {
288 const KUrl& oldUrl = *it;
289 QString number;
290 number.setNum(index++);
291
292 QString name(newName);
293 name.replace(replaceIndex, 1, number);
294
295 if (oldUrl.fileName() != name) {
296 KUrl newUrl = oldUrl;
297 newUrl.setFileName(name);
298 m_mainWindow->rename(oldUrl, newUrl);
299 }
300 ++it;
301 }
302 }
303 }
304 else {
305 // Only one item has been selected for renaming. Use the custom
306 // renaming mechanism from the views.
307 Q_ASSERT(urls.count() == 1);
308
309 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
310 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
311 // is a benefit for the user at all -> let's wait for some input first...
312 RenameDialog dialog(urls);
313 if (dialog.exec() == QDialog::Rejected) {
314 return;
315 }
316
317 const QString& newName = dialog.newName();
318 if (newName.isEmpty()) {
319 view->statusBar()->setMessage(dialog.errorString(),
320 DolphinStatusBar::Error);
321 }
322 else {
323 const KUrl& oldUrl = urls.first();
324 KUrl newUrl = oldUrl;
325 newUrl.setFileName(newName);
326 m_mainWindow->rename(oldUrl, newUrl);
327 }
328 }
329 }
330
331 void DolphinView::selectAll()
332 {
333 selectAll(QItemSelectionModel::Select);
334 }
335
336 void DolphinView::invertSelection()
337 {
338 selectAll(QItemSelectionModel::Toggle);
339 }
340
341 DolphinStatusBar* DolphinView::statusBar() const
342 {
343 return m_statusBar;
344 }
345
346 int DolphinView::contentsX() const
347 {
348 return itemView()->horizontalScrollBar()->value();
349 }
350
351 int DolphinView::contentsY() const
352 {
353 return itemView()->verticalScrollBar()->value();
354 }
355
356 void DolphinView::emitRequestItemInfo(const KUrl& url)
357 {
358 emit requestItemInfo(url);
359 }
360
361 bool DolphinView::isFilterBarVisible() const
362 {
363 return m_filterBar->isVisible();
364 }
365
366 bool DolphinView::isUrlEditable() const
367 {
368 return m_urlNavigator->isUrlEditable();
369 }
370
371 void DolphinView::zoomIn()
372 {
373 m_controller->triggerZoomIn();
374 }
375
376 void DolphinView::zoomOut()
377 {
378 m_controller->triggerZoomOut();
379 }
380
381 bool DolphinView::isZoomInPossible() const
382 {
383 return m_controller->isZoomInPossible();
384 }
385
386 bool DolphinView::isZoomOutPossible() const
387 {
388 return m_controller->isZoomOutPossible();
389 }
390
391 void DolphinView::setSorting(Sorting sorting)
392 {
393 if (sorting != this->sorting()) {
394 updateSorting(sorting);
395 }
396 }
397
398 DolphinView::Sorting DolphinView::sorting() const
399 {
400 return m_proxyModel->sorting();
401 }
402
403 void DolphinView::setSortOrder(Qt::SortOrder order)
404 {
405 if (sortOrder() != order) {
406 updateSortOrder(order);
407 }
408 }
409
410 Qt::SortOrder DolphinView::sortOrder() const
411 {
412 return m_proxyModel->sortOrder();
413 }
414
415 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info)
416 {
417 ViewProperties props(m_urlNavigator->url());
418 props.setAdditionalInfo(info);
419
420 m_fileItemDelegate->setAdditionalInformation(info);
421
422 emit additionalInfoChanged(info);
423 reload();
424 }
425
426 KFileItemDelegate::AdditionalInformation DolphinView::additionalInfo() const
427 {
428 return m_fileItemDelegate->additionalInformation();
429 }
430
431 void DolphinView::goBack()
432 {
433 m_urlNavigator->goBack();
434 }
435
436 void DolphinView::goForward()
437 {
438 m_urlNavigator->goForward();
439 }
440
441 void DolphinView::goUp()
442 {
443 m_urlNavigator->goUp();
444 }
445
446 void DolphinView::goHome()
447 {
448 m_urlNavigator->goHome();
449 }
450
451 void DolphinView::setUrlEditable(bool editable)
452 {
453 m_urlNavigator->setUrlEditable(editable);
454 }
455
456 bool DolphinView::hasSelection() const
457 {
458 return itemView()->selectionModel()->hasSelection();
459 }
460
461 void DolphinView::clearSelection()
462 {
463 itemView()->selectionModel()->clear();
464 }
465
466 KFileItemList DolphinView::selectedItems() const
467 {
468 const QAbstractItemView* view = itemView();
469
470 // Our view has a selection, we will map them back to the DirModel
471 // and then fill the KFileItemList.
472 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
473
474 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
475 KFileItemList itemList;
476
477 const QModelIndexList indexList = selection.indexes();
478 QModelIndexList::const_iterator end = indexList.end();
479 for (QModelIndexList::const_iterator it = indexList.begin(); it != end; ++it) {
480 Q_ASSERT((*it).isValid());
481
482 KFileItem* item = m_dirModel->itemForIndex(*it);
483 if (item != 0) {
484 itemList.append(item);
485 }
486 }
487
488 return itemList;
489 }
490
491 KUrl::List DolphinView::selectedUrls() const
492 {
493 KUrl::List urls;
494
495 const KFileItemList list = selectedItems();
496 KFileItemList::const_iterator it = list.begin();
497 const KFileItemList::const_iterator end = list.end();
498 while (it != end) {
499 KFileItem* item = *it;
500 urls.append(item->url());
501 ++it;
502 }
503
504 return urls;
505 }
506
507 KFileItem* DolphinView::fileItem(const QModelIndex index) const
508 {
509 const QModelIndex dirModelIndex = m_proxyModel->mapToSource(index);
510 return m_dirModel->itemForIndex(dirModelIndex);
511 }
512
513 void DolphinView::rename(const KUrl& source, const QString& newName)
514 {
515 bool ok = false;
516
517 if (newName.isEmpty() || (source.fileName() == newName)) {
518 return;
519 }
520
521 KUrl dest(source.upUrl());
522 dest.addPath(newName);
523
524 const bool destExists = KIO::NetAccess::exists(dest,
525 false,
526 mainWindow()->activeView());
527 if (destExists) {
528 // the destination already exists, hence ask the user
529 // how to proceed...
530 KIO::RenameDialog renameDialog(this,
531 i18n("File Already Exists"),
532 source.path(),
533 dest.path(),
534 KIO::M_OVERWRITE);
535 switch (renameDialog.exec()) {
536 case KIO::R_OVERWRITE:
537 // the destination should be overwritten
538 ok = KIO::NetAccess::file_move(source, dest, -1, true);
539 break;
540
541 case KIO::R_RENAME: {
542 // a new name for the destination has been used
543 KUrl newDest(renameDialog.newDestUrl());
544 ok = KIO::NetAccess::file_move(source, newDest);
545 break;
546 }
547
548 default:
549 // the renaming operation has been canceled
550 reload();
551 return;
552 }
553 }
554 else {
555 // no destination exists, hence just move the file to
556 // do the renaming
557 ok = KIO::NetAccess::file_move(source, dest);
558 }
559
560 const QString destFileName = dest.fileName();
561 if (ok) {
562 m_statusBar->setMessage(i18n("Renamed file '%1' to '%2'.",source.fileName(), destFileName),
563 DolphinStatusBar::OperationCompleted);
564
565 KonqOperations::rename(this, source, destFileName);
566 }
567 else {
568 m_statusBar->setMessage(i18n("Renaming of file '%1' to '%2' failed.",source.fileName(), destFileName),
569 DolphinStatusBar::Error);
570 reload();
571 }
572 }
573
574 void DolphinView::reload()
575 {
576 startDirLister(m_urlNavigator->url(), true);
577 }
578
579 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
580 {
581 QWidget::mouseReleaseEvent(event);
582 mainWindow()->setActiveView(this);
583 }
584
585 DolphinMainWindow* DolphinView::mainWindow() const
586 {
587 return m_mainWindow;
588 }
589
590 void DolphinView::loadDirectory(const KUrl& url)
591 {
592 if(!isActive()) {
593 requestActivation();
594 }
595
596 const ViewProperties props(url);
597
598 const Mode mode = props.viewMode();
599 bool changeMode = (m_mode != mode);
600 if (changeMode && isColumnViewActive()) {
601 // The column view is active. Only change the
602 // mode if the current URL is no child of the column view.
603 if (m_dirLister->url().isParentOf(url)) {
604 changeMode = false;
605 }
606 }
607
608 if (changeMode) {
609 m_mode = mode;
610 createView();
611 emit modeChanged();
612 }
613
614 const bool showHiddenFiles = props.showHiddenFiles();
615 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
616 m_dirLister->setShowingDotFiles(showHiddenFiles);
617 emit showHiddenFilesChanged();
618 }
619
620 const DolphinView::Sorting sorting = props.sorting();
621 if (sorting != m_proxyModel->sorting()) {
622 m_proxyModel->setSorting(sorting);
623 emit sortingChanged(sorting);
624 }
625
626 const Qt::SortOrder sortOrder = props.sortOrder();
627 if (sortOrder != m_proxyModel->sortOrder()) {
628 m_proxyModel->setSortOrder(sortOrder);
629 emit sortOrderChanged(sortOrder);
630 }
631
632 KFileItemDelegate::AdditionalInformation info = props.additionalInfo();
633 if (info != m_fileItemDelegate->additionalInformation()) {
634 m_fileItemDelegate->setAdditionalInformation(info);
635
636 emit additionalInfoChanged(info);
637 }
638
639 const bool showPreview = props.showPreview();
640 if (showPreview != m_controller->showPreview()) {
641 m_controller->setShowPreview(showPreview);
642 emit showPreviewChanged();
643 }
644
645 startDirLister(url);
646 emit urlChanged(url);
647
648 m_statusBar->clear();
649 }
650
651 void DolphinView::triggerItem(const QModelIndex& index)
652 {
653 if (!isValidNameIndex(index)) {
654 return;
655 }
656
657 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
658 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
659 // items are selected by the user, hence don't trigger the
660 // item specified by 'index'
661 return;
662 }
663
664 KFileItem* item = m_dirModel->itemForIndex(m_proxyModel->mapToSource(index));
665 if (item == 0) {
666 return;
667 }
668
669 // Prefer the local path over the URL. This assures that the
670 // volume space information is correct. Assuming that the URL is media:/sda1,
671 // and the local path is /windows/C: For the URL the space info is related
672 // to the root partition (and hence wrong) and for the local path the space
673 // info is related to the windows partition (-> correct).
674 const QString localPath(item->localPath());
675 KUrl url;
676 if (localPath.isEmpty()) {
677 url = item->url();
678 }
679 else {
680 url = localPath;
681 }
682
683 if (item->isDir()) {
684 setUrl(url);
685 }
686 else if (item->isFile()) {
687 // allow to browse through ZIP and tar files
688 KMimeType::Ptr mime = item->mimeTypePtr();
689 if (mime->is("application/zip")) {
690 url.setProtocol("zip");
691 setUrl(url);
692 }
693 else if (mime->is("application/x-tar") ||
694 mime->is("application/x-tarz") ||
695 mime->is("application/x-bzip-compressed-tar") ||
696 mime->is("application/x-compressed-tar") ||
697 mime->is("application/x-tzo")) {
698 url.setProtocol("tar");
699 setUrl(url);
700 }
701 else {
702 item->run();
703 }
704 }
705 else {
706 item->run();
707 }
708 }
709
710 void DolphinView::updateProgress(int percent)
711 {
712 if (m_showProgress) {
713 m_statusBar->setProgress(percent);
714 }
715 }
716
717 void DolphinView::updateItemCount()
718 {
719 if (m_showProgress) {
720 m_statusBar->setProgressText(QString());
721 m_statusBar->setProgress(100);
722 m_showProgress = false;
723 }
724
725 KFileItemList items(m_dirLister->items());
726 KFileItemList::const_iterator it = items.begin();
727 const KFileItemList::const_iterator end = items.end();
728
729 m_fileCount = 0;
730 m_folderCount = 0;
731
732 while (it != end) {
733 KFileItem* item = *it;
734 if (item->isDir()) {
735 ++m_folderCount;
736 }
737 else {
738 ++m_fileCount;
739 }
740 ++it;
741 }
742
743 updateStatusBar();
744
745 m_blockContentsMovedSignal = false;
746 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
747 }
748
749 void DolphinView::generatePreviews(const KFileItemList& items)
750 {
751 if (m_controller->showPreview()) {
752 KIO::PreviewJob* job = KIO::filePreview(items, 128);
753 connect(job, SIGNAL(gotPreview(const KFileItem*, const QPixmap&)),
754 this, SLOT(showPreview(const KFileItem*, const QPixmap&)));
755 }
756 }
757
758 void DolphinView::showPreview(const KFileItem* item, const QPixmap& pixmap)
759 {
760 Q_ASSERT(item != 0);
761 const QModelIndex idx = m_dirModel->indexForItem(*item);
762 if (idx.isValid() && (idx.column() == 0)) {
763 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
764 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(*item)) {
765 KIconEffect iconEffect;
766 const QPixmap cutPixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
767 m_dirModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
768 }
769 else {
770 m_dirModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
771 }
772 }
773 }
774
775 void DolphinView::restoreContentsPos()
776 {
777 KUrl currentUrl = m_urlNavigator->url();
778 if (!currentUrl.isEmpty()) {
779 QAbstractItemView* view = itemView();
780 // TODO: view->setCurrentItem(m_urlNavigator->currentFileName());
781 QPoint pos = m_urlNavigator->savedPosition();
782 view->horizontalScrollBar()->setValue(pos.x());
783 view->verticalScrollBar()->setValue(pos.y());
784 }
785 }
786
787 void DolphinView::showInfoMessage(const QString& msg)
788 {
789 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
790 }
791
792 void DolphinView::showErrorMessage(const QString& msg)
793 {
794 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
795 }
796
797 void DolphinView::emitSelectionChangedSignal()
798 {
799 emit selectionChanged(DolphinView::selectedItems());
800 }
801
802 void DolphinView::closeFilterBar()
803 {
804 m_filterBar->hide();
805 emit showFilterBarChanged(false);
806 }
807
808 void DolphinView::startDirLister(const KUrl& url, bool reload)
809 {
810 if (!url.isValid()) {
811 const QString location(url.pathOrUrl());
812 if (location.isEmpty()) {
813 m_statusBar->setMessage(i18n("The location is empty."), DolphinStatusBar::Error);
814 }
815 else {
816 m_statusBar->setMessage(i18n("The location '%1' is invalid.",location),
817 DolphinStatusBar::Error);
818 }
819 return;
820 }
821
822 // Only show the directory loading progress if the status bar does
823 // not contain another progress information. This means that
824 // the directory loading progress information has the lowest priority.
825 const QString progressText(m_statusBar->progressText());
826 m_showProgress = progressText.isEmpty() ||
827 (progressText == i18n("Loading directory..."));
828 if (m_showProgress) {
829 m_statusBar->setProgressText(i18n("Loading directory..."));
830 m_statusBar->setProgress(0);
831 }
832
833 m_cutItemsCache.clear();
834 m_blockContentsMovedSignal = true;
835 m_dirLister->stop();
836
837 bool openDir = true;
838 bool keepOldDirs = isColumnViewActive();
839 if (keepOldDirs) {
840 if (reload) {
841 keepOldDirs = false;
842
843 const KUrl& dirListerUrl = m_dirLister->url();
844 if (dirListerUrl.isValid()) {
845 const KUrl::List dirs = m_dirLister->directories();
846 KUrl url;
847 foreach (url, dirs) {
848 m_dirLister->updateDirectory(url);
849 }
850 openDir = false;
851 }
852 }
853 else if (m_dirLister->directories().contains(url)) {
854 // The dir lister contains the directory already, so
855 // KDirLister::openUrl() may not been invoked twice.
856 m_dirLister->updateDirectory(url);
857 openDir = false;
858 }
859 else {
860 const KUrl& dirListerUrl = m_dirLister->url();
861 if ((dirListerUrl == url) || !m_dirLister->url().isParentOf(url)) {
862 // The current URL is not a child of the dir lister
863 // URL. This may happen when e. g. a bookmark has been selected
864 // and hence the view must be reset.
865 keepOldDirs = false;
866 }
867 }
868 }
869
870 if (openDir) {
871 m_dirLister->openUrl(url, keepOldDirs, reload);
872 }
873 }
874
875 QString DolphinView::defaultStatusBarText() const
876 {
877 return KIO::itemsSummaryString(m_fileCount + m_folderCount,
878 m_fileCount,
879 m_folderCount,
880 0, false);
881 }
882
883 QString DolphinView::selectionStatusBarText() const
884 {
885 QString text;
886 const KFileItemList list = selectedItems();
887 if (list.isEmpty()) {
888 // when an item is triggered, it is temporary selected but selectedItems()
889 // will return an empty list
890 return QString();
891 }
892
893 int fileCount = 0;
894 int folderCount = 0;
895 KIO::filesize_t byteSize = 0;
896 KFileItemList::const_iterator it = list.begin();
897 const KFileItemList::const_iterator end = list.end();
898 while (it != end){
899 KFileItem* item = *it;
900 if (item->isDir()) {
901 ++folderCount;
902 }
903 else {
904 ++fileCount;
905 byteSize += item->size();
906 }
907 ++it;
908 }
909
910 if (folderCount > 0) {
911 text = i18np("1 Folder selected", "%1 Folders selected", folderCount);
912 if (fileCount > 0) {
913 text += ", ";
914 }
915 }
916
917 if (fileCount > 0) {
918 const QString sizeText(KIO::convertSize(byteSize));
919 text += i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount, sizeText);
920 }
921
922 return text;
923 }
924
925 void DolphinView::showFilterBar(bool show)
926 {
927 Q_ASSERT(m_filterBar != 0);
928 if (show) {
929 m_filterBar->show();
930 }
931 else {
932 m_filterBar->hide();
933 }
934 }
935
936 void DolphinView::updateStatusBar()
937 {
938 // As the item count information is less important
939 // in comparison with other messages, it should only
940 // be shown if:
941 // - the status bar is empty or
942 // - shows already the item count information or
943 // - shows only a not very important information
944 // - if any progress is given don't show the item count info at all
945 const QString msg(m_statusBar->message());
946 const bool updateStatusBarMsg = (msg.isEmpty() ||
947 (msg == m_statusBar->defaultText()) ||
948 (m_statusBar->type() == DolphinStatusBar::Information)) &&
949 (m_statusBar->progress() == 100);
950
951 const QString text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
952 m_statusBar->setDefaultText(text);
953
954 if (updateStatusBarMsg) {
955 m_statusBar->setMessage(text, DolphinStatusBar::Default);
956 }
957 }
958
959 void DolphinView::requestActivation()
960 {
961 m_mainWindow->setActiveView(this);
962 }
963
964 void DolphinView::changeSelection(const KFileItemList& selection)
965 {
966 clearSelection();
967 if (selection.isEmpty()) {
968 return;
969 }
970 KUrl baseUrl = url();
971 KUrl url;
972 QItemSelection new_selection;
973 foreach (KFileItem* item, selection) {
974 url = item->url().upUrl();
975 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
976 QModelIndex index = m_proxyModel->mapFromSource(m_dirModel->indexForItem(*item));
977 new_selection.select(index, index);
978 }
979 }
980 itemView()->selectionModel()->select(new_selection,
981 QItemSelectionModel::ClearAndSelect
982 | QItemSelectionModel::Current);
983 }
984
985 void DolphinView::changeNameFilter(const QString& nameFilter)
986 {
987 // The name filter of KDirLister does a 'hard' filtering, which
988 // means that only the items are shown where the names match
989 // exactly the filter. This is non-transparent for the user, which
990 // just wants to have a 'soft' filtering: does the name contain
991 // the filter string?
992 QString adjustedFilter(nameFilter);
993 adjustedFilter.insert(0, '*');
994 adjustedFilter.append('*');
995
996 // Use the ProxyModel to filter:
997 // This code is #ifdefed as setNameFilter behaves
998 // slightly different than the QSortFilterProxyModel
999 // as it will not remove directories. I will ask
1000 // our beloved usability experts for input
1001 // -- z.
1002 #if 0
1003 m_dirLister->setNameFilter(adjustedFilter);
1004 m_dirLister->emitChanges();
1005 #else
1006 m_proxyModel->setFilterRegExp( nameFilter );
1007 #endif
1008 }
1009
1010 void DolphinView::openContextMenu(const QPoint& pos)
1011 {
1012 KFileItem* item = 0;
1013
1014 const QModelIndex index = itemView()->indexAt(pos);
1015 if (isValidNameIndex(index)) {
1016 item = fileItem(index);
1017 }
1018
1019 DolphinContextMenu contextMenu(m_mainWindow, item, url());
1020 contextMenu.open();
1021 }
1022
1023 void DolphinView::dropUrls(const KUrl::List& urls,
1024 const QModelIndex& index,
1025 QWidget* source)
1026 {
1027 KFileItem* directory = 0;
1028 if (isValidNameIndex(index)) {
1029 KFileItem* item = fileItem(index);
1030 Q_ASSERT(item != 0);
1031 if (item->isDir()) {
1032 // the URLs are dropped above a directory
1033 directory = item;
1034 }
1035 }
1036
1037 if ((directory == 0) && (source == itemView())) {
1038 // The dropping is done into the same viewport where
1039 // the dragging has been started. Just ignore this...
1040 return;
1041 }
1042
1043 const KUrl& destination = (directory == 0) ? url() :
1044 directory->url();
1045 dropUrls(urls, destination);
1046 }
1047
1048 void DolphinView::dropUrls(const KUrl::List& urls,
1049 const KUrl& destination)
1050 {
1051 m_mainWindow->dropUrls(urls, destination);
1052 }
1053
1054 void DolphinView::updateSorting(DolphinView::Sorting sorting)
1055 {
1056 ViewProperties props(url());
1057 props.setSorting(sorting);
1058
1059 m_proxyModel->setSorting(sorting);
1060
1061 emit sortingChanged(sorting);
1062 }
1063
1064 void DolphinView::updateSortOrder(Qt::SortOrder order)
1065 {
1066 ViewProperties props(url());
1067 props.setSortOrder(order);
1068
1069 m_proxyModel->setSortOrder(order);
1070
1071 emit sortOrderChanged(order);
1072 }
1073
1074 void DolphinView::emitContentsMoved()
1075 {
1076 if (!m_blockContentsMovedSignal) {
1077 emit contentsMoved(contentsX(), contentsY());
1078 }
1079 }
1080
1081 void DolphinView::updateActivationState()
1082 {
1083 m_urlNavigator->setActive(isActive());
1084 if(isActive()) {
1085 emit urlChanged(url());
1086 emit selectionChanged(selectedItems());
1087 }
1088 }
1089
1090 void DolphinView::updateCutItems()
1091 {
1092 // restore the icons of all previously selected items to the
1093 // original state...
1094 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
1095 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
1096 while (it != end) {
1097 const QModelIndex index = m_dirModel->indexForUrl((*it).url);
1098 if (index.isValid()) {
1099 m_dirModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
1100 }
1101 ++it;
1102 }
1103 m_cutItemsCache.clear();
1104
1105 // ... and apply an item effect to all currently cut items
1106 applyCutItemEffect();
1107 }
1108
1109 void DolphinView::createView()
1110 {
1111 // delete current view
1112 QAbstractItemView* view = itemView();
1113 if (view != 0) {
1114 m_topLayout->removeWidget(view);
1115 view->close();
1116 view->deleteLater();
1117 view = 0;
1118 m_iconsView = 0;
1119 m_detailsView = 0;
1120 m_columnView = 0;
1121 m_fileItemDelegate = 0;
1122 }
1123
1124 Q_ASSERT(m_iconsView == 0);
1125 Q_ASSERT(m_detailsView == 0);
1126 Q_ASSERT(m_columnView == 0);
1127
1128 // ... and recreate it representing the current mode
1129 switch (m_mode) {
1130 case IconsView:
1131 m_iconsView = new DolphinIconsView(this, m_controller);
1132 view = m_iconsView;
1133 break;
1134
1135 case DetailsView:
1136 m_detailsView = new DolphinDetailsView(this, m_controller);
1137 view = m_detailsView;
1138 break;
1139
1140 case ColumnView:
1141 m_columnView = new DolphinColumnView(this, m_controller);
1142 view = m_columnView;
1143 break;
1144 }
1145
1146 Q_ASSERT(view != 0);
1147
1148 m_fileItemDelegate = new KFileItemDelegate(view);
1149 view->setItemDelegate(m_fileItemDelegate);
1150
1151 view->setModel(m_proxyModel);
1152 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
1153
1154 new KMimeTypeResolver(view, m_dirModel);
1155 m_topLayout->insertWidget(1, view);
1156
1157 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
1158 m_controller, SLOT(indicateSelectionChange()));
1159 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1160 this, SLOT(emitContentsMoved()));
1161 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1162 this, SLOT(emitContentsMoved()));
1163 }
1164
1165 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags)
1166 {
1167 QItemSelectionModel* selectionModel = itemView()->selectionModel();
1168 const QAbstractItemModel* itemModel = selectionModel->model();
1169
1170 const QModelIndex topLeft = itemModel->index(0, 0);
1171 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
1172 itemModel->columnCount() - 1);
1173
1174 QItemSelection selection(topLeft, bottomRight);
1175 selectionModel->select(selection, flags);
1176 }
1177
1178 QAbstractItemView* DolphinView::itemView() const
1179 {
1180 if (m_detailsView != 0) {
1181 return m_detailsView;
1182 }
1183 else if (m_columnView != 0) {
1184 return m_columnView;
1185 }
1186
1187 return m_iconsView;
1188 }
1189
1190 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
1191 {
1192 return index.isValid() && (index.column() == KDirModel::Name);
1193 }
1194
1195 bool DolphinView::isCutItem(const KFileItem& item) const
1196 {
1197 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1198 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
1199
1200 const KUrl& itemUrl = item.url();
1201 KUrl::List::const_iterator it = cutUrls.begin();
1202 const KUrl::List::const_iterator end = cutUrls.end();
1203 while (it != end){
1204 if (*it == itemUrl) {
1205 return true;
1206 }
1207 ++it;
1208 }
1209
1210 return false;
1211 }
1212
1213 void DolphinView::applyCutItemEffect()
1214 {
1215 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1216 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
1217 return;
1218 }
1219
1220 KFileItemList items(m_dirLister->items());
1221 KFileItemList::const_iterator it = items.begin();
1222 const KFileItemList::const_iterator end = items.end();
1223 while (it != end) {
1224 KFileItem* item = *it;
1225 if (isCutItem(*item)) {
1226 const QModelIndex index = m_dirModel->indexForItem(*item);
1227 const KFileItem* item = m_dirModel->itemForIndex(index);
1228 const QVariant value = m_dirModel->data(index, Qt::DecorationRole);
1229 if ((value.type() == QVariant::Icon) && (item != 0)) {
1230 const QIcon icon(qvariant_cast<QIcon>(value));
1231 QPixmap pixmap = icon.pixmap(128, 128);
1232
1233 // remember current pixmap for the item to be able
1234 // to restore it when other items get cut
1235 CutItem cutItem;
1236 cutItem.url = item->url();
1237 cutItem.pixmap = pixmap;
1238 m_cutItemsCache.append(cutItem);
1239
1240 // apply icon effect to the cut item
1241 KIconEffect iconEffect;
1242 pixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
1243 m_dirModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
1244 }
1245 }
1246 ++it;
1247 }
1248 }
1249
1250 #include "dolphinview.moc"