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