]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Using a QLinkedList mostly for "accessing element at index i" is not the best solutio...
[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_blockContentsMovedSignal(false),
63 m_mode(mode),
64 m_iconSize(0),
65 m_folderCount(0),
66 m_fileCount(0),
67 m_mainWindow(mainWindow),
68 m_topLayout(0),
69 m_urlNavigator(0),
70 m_controller(0),
71 m_iconsView(0),
72 m_detailsView(0),
73 m_fileItemDelegate(0),
74 m_filterBar(0),
75 m_statusBar(0),
76 m_dirModel(0),
77 m_dirLister(0),
78 m_proxyModel(0)
79 {
80 hide();
81 setFocusPolicy(Qt::StrongFocus);
82 m_topLayout = new QVBoxLayout(this);
83 m_topLayout->setSpacing(0);
84 m_topLayout->setMargin(0);
85
86 connect(m_mainWindow, SIGNAL(activeViewChanged()),
87 this, SLOT(updateActivationState()));
88
89 QClipboard* clipboard = QApplication::clipboard();
90 connect(clipboard, SIGNAL(dataChanged()),
91 this, SLOT(updateCutItems()));
92
93 m_urlNavigator = new UrlNavigator(url, this);
94 m_urlNavigator->setShowHiddenFiles(showHiddenFiles);
95 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
96 this, SLOT(loadDirectory(const KUrl&)));
97 connect(m_urlNavigator, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&)),
98 this, SLOT(dropUrls(const KUrl::List&, const KUrl&)));
99 connect(m_urlNavigator, SIGNAL(activated()),
100 this, SLOT(requestActivation()));
101 connect(this, SIGNAL(contentsMoved(int, int)),
102 m_urlNavigator, SLOT(storeContentsPosition(int, int)));
103
104 m_statusBar = new DolphinStatusBar(this);
105
106 m_dirLister = new DolphinDirLister();
107 m_dirLister->setAutoUpdate(true);
108 m_dirLister->setMainWindow(this);
109 m_dirLister->setShowingDotFiles(showHiddenFiles);
110 m_dirLister->setDelayedMimeTypes(true);
111
112 connect(m_dirLister, SIGNAL(clear()),
113 this, SLOT(updateStatusBar()));
114 connect(m_dirLister, SIGNAL(percent(int)),
115 this, SLOT(updateProgress(int)));
116 connect(m_dirLister, SIGNAL(deleteItem(KFileItem*)),
117 this, SLOT(updateStatusBar()));
118 connect(m_dirLister, SIGNAL(completed()),
119 this, SLOT(updateItemCount()));
120 connect(m_dirLister, SIGNAL(completed()),
121 this, SLOT(updateCutItems()));
122 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
123 this, SLOT(generatePreviews(const KFileItemList&)));
124 connect(m_dirLister, SIGNAL(infoMessage(const QString&)),
125 this, SLOT(showInfoMessage(const QString&)));
126 connect(m_dirLister, SIGNAL(errorMessage(const QString&)),
127 this, SLOT(showErrorMessage(const QString&)));
128
129 m_dirModel = new KDirModel();
130 m_dirModel->setDirLister(m_dirLister);
131 m_dirModel->setDropsAllowed(KDirModel::DropOnDirectory);
132
133 m_proxyModel = new DolphinSortFilterProxyModel(this);
134 m_proxyModel->setSourceModel(m_dirModel);
135
136 m_controller = new DolphinController(this);
137 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
138 this, SLOT(openContextMenu(const QPoint&)));
139 connect(m_controller, SIGNAL(urlsDropped(const KUrl::List&, const QModelIndex&, QWidget*)),
140 this, SLOT(dropUrls(const KUrl::List&, const QModelIndex&, QWidget*)));
141 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
142 this, SLOT(updateSorting(DolphinView::Sorting)));
143 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
144 this, SLOT(updateSortOrder(Qt::SortOrder)));
145 connect(m_controller, SIGNAL(itemTriggered(const QModelIndex&)),
146 this, SLOT(triggerItem(const QModelIndex&)));
147 connect(m_controller, SIGNAL(selectionChanged()),
148 this, SLOT(emitSelectionChangedSignal()));
149 connect(m_controller, SIGNAL(activated()),
150 this, SLOT(requestActivation()));
151
152 createView();
153
154 m_iconSize = K3Icon::SizeMedium;
155
156 m_filterBar = new FilterBar(this);
157 m_filterBar->hide();
158 connect(m_filterBar, SIGNAL(filterChanged(const QString&)),
159 this, SLOT(changeNameFilter(const QString&)));
160 connect(m_filterBar, SIGNAL(closeRequest()),
161 this, SLOT(closeFilterBar()));
162
163 m_topLayout->addWidget(m_urlNavigator);
164 m_topLayout->addWidget(itemView());
165 m_topLayout->addWidget(m_filterBar);
166 m_topLayout->addWidget(m_statusBar);
167 }
168
169 DolphinView::~DolphinView()
170 {
171 delete m_dirLister;
172 m_dirLister = 0;
173 }
174
175 void DolphinView::setUrl(const KUrl& url)
176 {
177 m_urlNavigator->setUrl(url);
178 m_controller->setUrl(url);
179 }
180
181 const KUrl& DolphinView::url() const
182 {
183 return m_urlNavigator->url();
184 }
185
186 bool DolphinView::isActive() const
187 {
188 return m_mainWindow->activeView() == this;
189 }
190
191 void DolphinView::setMode(Mode mode)
192 {
193 if (mode == m_mode) {
194 return; // the wished mode is already set
195 }
196
197 m_mode = mode;
198
199 ViewProperties props(m_urlNavigator->url());
200 props.setViewMode(m_mode);
201
202 createView();
203 startDirLister(m_urlNavigator->url());
204
205 emit modeChanged();
206 }
207
208 DolphinView::Mode DolphinView::mode() const
209 {
210 return m_mode;
211 }
212
213 void DolphinView::setShowPreview(bool show)
214 {
215 ViewProperties props(m_urlNavigator->url());
216 props.setShowPreview(show);
217
218 m_controller->setShowPreview(show);
219
220 emit showPreviewChanged();
221 reload();
222 }
223
224 bool DolphinView::showPreview() const
225 {
226 return m_controller->showPreview();
227 }
228
229 void DolphinView::setShowHiddenFiles(bool show)
230 {
231 if (m_dirLister->showingDotFiles() == show) {
232 return;
233 }
234
235 ViewProperties props(m_urlNavigator->url());
236 props.setShowHiddenFiles(show);
237 props.save();
238
239 m_dirLister->setShowingDotFiles(show);
240 m_urlNavigator->setShowHiddenFiles(show);
241
242 emit showHiddenFilesChanged();
243
244 reload();
245 }
246
247 bool DolphinView::showHiddenFiles() const
248 {
249 return m_dirLister->showingDotFiles();
250 }
251
252 void DolphinView::renameSelectedItems()
253 {
254 DolphinView* view = mainWindow()->activeView();
255 const KUrl::List urls = selectedUrls();
256 if (urls.count() > 1) {
257 // More than one item has been selected for renaming. Open
258 // a rename dialog and rename all items afterwards.
259 RenameDialog dialog(urls);
260 if (dialog.exec() == QDialog::Rejected) {
261 return;
262 }
263
264 const QString& newName = dialog.newName();
265 if (newName.isEmpty()) {
266 view->statusBar()->setMessage(dialog.errorString(),
267 DolphinStatusBar::Error);
268 }
269 else {
270 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
271 // as one operation instead of n rename operations like it is done now...
272 Q_ASSERT(newName.contains('#'));
273
274 // iterate through all selected items and rename them...
275 const int replaceIndex = newName.indexOf('#');
276 Q_ASSERT(replaceIndex >= 0);
277 int index = 1;
278
279 KUrl::List::const_iterator it = urls.begin();
280 KUrl::List::const_iterator end = urls.end();
281 while (it != end) {
282 const KUrl& oldUrl = *it;
283 QString number;
284 number.setNum(index++);
285
286 QString name(newName);
287 name.replace(replaceIndex, 1, number);
288
289 if (oldUrl.fileName() != name) {
290 KUrl newUrl(oldUrl.upUrl());
291 newUrl.addPath(name);
292 m_mainWindow->rename(oldUrl, newUrl);
293 }
294 ++it;
295 }
296 }
297 }
298 else {
299 // Only one item has been selected for renaming. Use the custom
300 // renaming mechanism from the views.
301 Q_ASSERT(urls.count() == 1);
302
303 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
304 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
305 // is a benefit for the user at all -> let's wait for some input first...
306 RenameDialog dialog(urls);
307 if (dialog.exec() == QDialog::Rejected) {
308 return;
309 }
310
311 const QString& newName = dialog.newName();
312 if (newName.isEmpty()) {
313 view->statusBar()->setMessage(dialog.errorString(),
314 DolphinStatusBar::Error);
315 }
316 else {
317 const KUrl& oldUrl = urls.first();
318 KUrl newUrl = oldUrl.upUrl();
319 newUrl.addPath(newName);
320 m_mainWindow->rename(oldUrl, newUrl);
321 }
322 }
323 }
324
325 void DolphinView::selectAll()
326 {
327 selectAll(QItemSelectionModel::Select);
328 }
329
330 void DolphinView::invertSelection()
331 {
332 selectAll(QItemSelectionModel::Toggle);
333 }
334
335 DolphinStatusBar* DolphinView::statusBar() const
336 {
337 return m_statusBar;
338 }
339
340 int DolphinView::contentsX() const
341 {
342
343 return itemView()->horizontalScrollBar()->value();
344 }
345
346 int DolphinView::contentsY() const
347 {
348 return itemView()->verticalScrollBar()->value();
349 }
350
351 void DolphinView::refreshSettings()
352 {
353 startDirLister(m_urlNavigator->url());
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->editUrl(editable);
454 }
455
456 const QList<UrlNavigator::HistoryElem> DolphinView::urlHistory(int& index) const
457 {
458 return m_urlNavigator->history(index);
459 }
460
461 bool DolphinView::hasSelection() const
462 {
463 return itemView()->selectionModel()->hasSelection();
464 }
465
466 void DolphinView::clearSelection()
467 {
468 itemView()->selectionModel()->clear();
469 }
470
471 KFileItemList DolphinView::selectedItems() const
472 {
473 const QAbstractItemView* view = itemView();
474
475 // Our view has a selection, we will map them back to the DirModel
476 // and then fill the KFileItemList.
477 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
478
479 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
480 KFileItemList itemList;
481
482 const QModelIndexList indexList = selection.indexes();
483 QModelIndexList::const_iterator end = indexList.end();
484 for (QModelIndexList::const_iterator it = indexList.begin(); it != end; ++it) {
485 Q_ASSERT((*it).isValid());
486
487 KFileItem* item = m_dirModel->itemForIndex(*it);
488 if (item != 0) {
489 itemList.append(item);
490 }
491 }
492
493 return itemList;
494 }
495
496 KUrl::List DolphinView::selectedUrls() const
497 {
498 KUrl::List urls;
499
500 const KFileItemList list = selectedItems();
501 KFileItemList::const_iterator it = list.begin();
502 const KFileItemList::const_iterator end = list.end();
503 while (it != end) {
504 KFileItem* item = *it;
505 urls.append(item->url());
506 ++it;
507 }
508
509 return urls;
510 }
511
512 KFileItem* DolphinView::fileItem(const QModelIndex index) const
513 {
514 const QModelIndex dirModelIndex = m_proxyModel->mapToSource(index);
515 return m_dirModel->itemForIndex(dirModelIndex);
516 }
517
518 void DolphinView::rename(const KUrl& source, const QString& newName)
519 {
520 bool ok = false;
521
522 if (newName.isEmpty() || (source.fileName() == newName)) {
523 return;
524 }
525
526 KUrl dest(source.upUrl());
527 dest.addPath(newName);
528
529 const bool destExists = KIO::NetAccess::exists(dest,
530 false,
531 mainWindow()->activeView());
532 if (destExists) {
533 // the destination already exists, hence ask the user
534 // how to proceed...
535 KIO::RenameDialog renameDialog(this,
536 i18n("File Already Exists"),
537 source.path(),
538 dest.path(),
539 KIO::M_OVERWRITE);
540 switch (renameDialog.exec()) {
541 case KIO::R_OVERWRITE:
542 // the destination should be overwritten
543 ok = KIO::NetAccess::file_move(source, dest, -1, true);
544 break;
545
546 case KIO::R_RENAME: {
547 // a new name for the destination has been used
548 KUrl newDest(renameDialog.newDestUrl());
549 ok = KIO::NetAccess::file_move(source, newDest);
550 break;
551 }
552
553 default:
554 // the renaming operation has been canceled
555 reload();
556 return;
557 }
558 }
559 else {
560 // no destination exists, hence just move the file to
561 // do the renaming
562 ok = KIO::NetAccess::file_move(source, dest);
563 }
564
565 const QString destFileName = dest.fileName();
566 if (ok) {
567 m_statusBar->setMessage(i18n("Renamed file '%1' to '%2'.",source.fileName(), destFileName),
568 DolphinStatusBar::OperationCompleted);
569
570 KonqOperations::rename(this, source, destFileName);
571 }
572 else {
573 m_statusBar->setMessage(i18n("Renaming of file '%1' to '%2' failed.",source.fileName(), destFileName),
574 DolphinStatusBar::Error);
575 reload();
576 }
577 }
578
579 void DolphinView::reload()
580 {
581 startDirLister(m_urlNavigator->url(), true);
582 }
583
584 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
585 {
586 QWidget::mouseReleaseEvent(event);
587 mainWindow()->setActiveView(this);
588 }
589
590 DolphinMainWindow* DolphinView::mainWindow() const
591 {
592 return m_mainWindow;
593 }
594
595 void DolphinView::loadDirectory(const KUrl& url)
596 {
597 if(!isActive()) {
598 requestActivation();
599 }
600
601 const ViewProperties props(url);
602
603 const Mode mode = props.viewMode();
604 if (m_mode != mode) {
605 m_mode = mode;
606 createView();
607 emit modeChanged();
608 }
609
610 const bool showHiddenFiles = props.showHiddenFiles();
611 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
612 m_dirLister->setShowingDotFiles(showHiddenFiles);
613 emit showHiddenFilesChanged();
614 }
615
616 const DolphinView::Sorting sorting = props.sorting();
617 if (sorting != m_proxyModel->sorting()) {
618 m_proxyModel->setSorting(sorting);
619 emit sortingChanged(sorting);
620 }
621
622 const Qt::SortOrder sortOrder = props.sortOrder();
623 if (sortOrder != m_proxyModel->sortOrder()) {
624 m_proxyModel->setSortOrder(sortOrder);
625 emit sortOrderChanged(sortOrder);
626 }
627
628 KFileItemDelegate::AdditionalInformation info = props.additionalInfo();
629 if (info != m_fileItemDelegate->additionalInformation()) {
630 m_fileItemDelegate->setAdditionalInformation(info);
631
632 emit additionalInfoChanged(info);
633 }
634
635 const bool showPreview = props.showPreview();
636 if (showPreview != m_controller->showPreview()) {
637 m_controller->setShowPreview(showPreview);
638 emit showPreviewChanged();
639 }
640
641 startDirLister(url);
642 emit urlChanged(url);
643
644 m_statusBar->clear();
645 }
646
647 void DolphinView::triggerItem(const QModelIndex& index)
648 {
649 if (!isValidNameIndex(index)) {
650 return;
651 }
652
653 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
654 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
655 // items are selected by the user, hence don't trigger the
656 // item specified by 'index'
657 return;
658 }
659
660 KFileItem* item = m_dirModel->itemForIndex(m_proxyModel->mapToSource(index));
661 if (item == 0) {
662 return;
663 }
664
665 // Prefer the local path over the URL. This assures that the
666 // volume space information is correct. Assuming that the URL is media:/sda1,
667 // and the local path is /windows/C: For the URL the space info is related
668 // to the root partition (and hence wrong) and for the local path the space
669 // info is related to the windows partition (-> correct).
670 const QString localPath(item->localPath());
671 KUrl url;
672 if (localPath.isEmpty()) {
673 url = item->url();
674 }
675 else {
676 url = localPath;
677 }
678
679 if (item->isDir()) {
680 setUrl(url);
681 }
682 else if (item->isFile()) {
683 // allow to browse through ZIP and tar files
684 KMimeType::Ptr mime = item->mimeTypePtr();
685 if (mime->is("application/zip")) {
686 url.setProtocol("zip");
687 setUrl(url);
688 }
689 else if (mime->is("application/x-tar") ||
690 mime->is("application/x-tarz") ||
691 mime->is("application/x-bzip-compressed-tar") ||
692 mime->is("application/x-compressed-tar") ||
693 mime->is("application/x-tzo")) {
694 url.setProtocol("tar");
695 setUrl(url);
696 }
697 else {
698 item->run();
699 }
700 }
701 else {
702 item->run();
703 }
704 }
705
706 void DolphinView::updateProgress(int percent)
707 {
708 if (m_showProgress) {
709 m_statusBar->setProgress(percent);
710 }
711 }
712
713 void DolphinView::updateItemCount()
714 {
715 if (m_showProgress) {
716 m_statusBar->setProgressText(QString());
717 m_statusBar->setProgress(100);
718 m_showProgress = false;
719 }
720
721 KFileItemList items(m_dirLister->items());
722 KFileItemList::const_iterator it = items.begin();
723 const KFileItemList::const_iterator end = items.end();
724
725 m_fileCount = 0;
726 m_folderCount = 0;
727
728 while (it != end) {
729 KFileItem* item = *it;
730 if (item->isDir()) {
731 ++m_folderCount;
732 }
733 else {
734 ++m_fileCount;
735 }
736 ++it;
737 }
738
739 updateStatusBar();
740
741 m_blockContentsMovedSignal = false;
742 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
743 }
744
745 void DolphinView::generatePreviews(const KFileItemList& items)
746 {
747 if (m_controller->showPreview()) {
748 KIO::PreviewJob* job = KIO::filePreview(items, 128);
749 connect(job, SIGNAL(gotPreview(const KFileItem*, const QPixmap&)),
750 this, SLOT(showPreview(const KFileItem*, const QPixmap&)));
751 }
752 }
753
754 void DolphinView::showPreview(const KFileItem* item, const QPixmap& pixmap)
755 {
756 Q_ASSERT(item != 0);
757 const QModelIndex idx = m_dirModel->indexForItem(*item);
758 if (idx.isValid() && (idx.column() == 0)) {
759 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
760 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(*item)) {
761 KIconEffect iconEffect;
762 const QPixmap cutPixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
763 m_dirModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
764 }
765 else {
766 m_dirModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
767 }
768 }
769 }
770
771 void DolphinView::restoreContentsPos()
772 {
773 int index = 0;
774 const QList<UrlNavigator::HistoryElem> history = urlHistory(index);
775 if (!history.isEmpty()) {
776 QAbstractItemView* view = itemView();
777 // TODO: view->setCurrentItem(history[index].currentFileName());
778
779 const UrlNavigator::HistoryElem& it = history[index];
780 view->horizontalScrollBar()->setValue(it.contentsX());
781 view->verticalScrollBar()->setValue(it.contentsY());
782 }
783 }
784
785 void DolphinView::showInfoMessage(const QString& msg)
786 {
787 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
788 }
789
790 void DolphinView::showErrorMessage(const QString& msg)
791 {
792 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
793 }
794
795 void DolphinView::emitSelectionChangedSignal()
796 {
797 emit selectionChanged(DolphinView::selectedItems());
798 }
799
800 void DolphinView::closeFilterBar()
801 {
802 m_filterBar->hide();
803 emit showFilterBarChanged(false);
804 }
805
806 void DolphinView::startDirLister(const KUrl& url, bool reload)
807 {
808 if (!url.isValid()) {
809 const QString location(url.pathOrUrl());
810 if (location.isEmpty()) {
811 m_statusBar->setMessage(i18n("The location is empty."), DolphinStatusBar::Error);
812 }
813 else {
814 m_statusBar->setMessage(i18n("The location '%1' is invalid.",location),
815 DolphinStatusBar::Error);
816 }
817 return;
818 }
819
820 // Only show the directory loading progress if the status bar does
821 // not contain another progress information. This means that
822 // the directory loading progress information has the lowest priority.
823 const QString progressText(m_statusBar->progressText());
824 m_showProgress = progressText.isEmpty() ||
825 (progressText == i18n("Loading directory..."));
826 if (m_showProgress) {
827 m_statusBar->setProgressText(i18n("Loading directory..."));
828 m_statusBar->setProgress(0);
829 }
830
831 m_cutItemsCache.clear();
832 m_blockContentsMovedSignal = true;
833 m_dirLister->stop();
834 m_dirLister->openUrl(url, false, reload);
835 }
836
837 QString DolphinView::defaultStatusBarText() const
838 {
839 return KIO::itemsSummaryString(m_fileCount + m_folderCount,
840 m_fileCount,
841 m_folderCount,
842 0, false);
843 }
844
845 QString DolphinView::selectionStatusBarText() const
846 {
847 QString text;
848 const KFileItemList list = selectedItems();
849 if (list.isEmpty()) {
850 // when an item is triggered, it is temporary selected but selectedItems()
851 // will return an empty list
852 return QString();
853 }
854
855 int fileCount = 0;
856 int folderCount = 0;
857 KIO::filesize_t byteSize = 0;
858 KFileItemList::const_iterator it = list.begin();
859 const KFileItemList::const_iterator end = list.end();
860 while (it != end){
861 KFileItem* item = *it;
862 if (item->isDir()) {
863 ++folderCount;
864 }
865 else {
866 ++fileCount;
867 byteSize += item->size();
868 }
869 ++it;
870 }
871
872 if (folderCount > 0) {
873 text = i18np("1 Folder selected", "%1 Folders selected", folderCount);
874 if (fileCount > 0) {
875 text += ", ";
876 }
877 }
878
879 if (fileCount > 0) {
880 const QString sizeText(KIO::convertSize(byteSize));
881 text += i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount, sizeText);
882 }
883
884 return text;
885 }
886
887 void DolphinView::showFilterBar(bool show)
888 {
889 Q_ASSERT(m_filterBar != 0);
890 if (show) {
891 m_filterBar->show();
892 }
893 else {
894 m_filterBar->hide();
895 }
896 }
897
898 void DolphinView::updateStatusBar()
899 {
900 // As the item count information is less important
901 // in comparison with other messages, it should only
902 // be shown if:
903 // - the status bar is empty or
904 // - shows already the item count information or
905 // - shows only a not very important information
906 // - if any progress is given don't show the item count info at all
907 const QString msg(m_statusBar->message());
908 const bool updateStatusBarMsg = (msg.isEmpty() ||
909 (msg == m_statusBar->defaultText()) ||
910 (m_statusBar->type() == DolphinStatusBar::Information)) &&
911 (m_statusBar->progress() == 100);
912
913 const QString text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
914 m_statusBar->setDefaultText(text);
915
916 if (updateStatusBarMsg) {
917 m_statusBar->setMessage(text, DolphinStatusBar::Default);
918 }
919 }
920
921 void DolphinView::requestActivation()
922 {
923 m_mainWindow->setActiveView(this);
924 }
925
926 void DolphinView::changeSelection(const KFileItemList& selection)
927 {
928 clearSelection();
929 if (selection.isEmpty()) {
930 return;
931 }
932 KUrl baseUrl = url();
933 KUrl url;
934 QItemSelection new_selection;
935 foreach (KFileItem* item, selection) {
936 url = item->url().upUrl();
937 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
938 QModelIndex index = m_proxyModel->mapFromSource(m_dirModel->indexForItem(*item));
939 new_selection.select(index, index);
940 }
941 }
942 itemView()->selectionModel()->select(new_selection,
943 QItemSelectionModel::ClearAndSelect
944 | QItemSelectionModel::Current);
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 dropUrls(urls, destination);
1008 }
1009
1010 void DolphinView::dropUrls(const KUrl::List& urls,
1011 const KUrl& destination)
1012 {
1013 m_mainWindow->dropUrls(urls, destination);
1014 }
1015
1016 void DolphinView::updateSorting(DolphinView::Sorting sorting)
1017 {
1018 ViewProperties props(url());
1019 props.setSorting(sorting);
1020
1021 m_proxyModel->setSorting(sorting);
1022
1023 emit sortingChanged(sorting);
1024 }
1025
1026 void DolphinView::updateSortOrder(Qt::SortOrder order)
1027 {
1028 ViewProperties props(url());
1029 props.setSortOrder(order);
1030
1031 m_proxyModel->setSortOrder(order);
1032
1033 emit sortOrderChanged(order);
1034 }
1035
1036 void DolphinView::emitContentsMoved()
1037 {
1038 if (!m_blockContentsMovedSignal) {
1039 emit contentsMoved(contentsX(), contentsY());
1040 }
1041 }
1042
1043 void DolphinView::updateActivationState()
1044 {
1045 m_urlNavigator->setActive(isActive());
1046 if(isActive()) {
1047 emit urlChanged(url());
1048 emit selectionChanged(selectedItems());
1049 }
1050 }
1051
1052 void DolphinView::updateCutItems()
1053 {
1054 // restore the icons of all previously selected items to the
1055 // original state...
1056 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
1057 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
1058 while (it != end) {
1059 const QModelIndex index = m_dirModel->indexForUrl((*it).url);
1060 if (index.isValid()) {
1061 m_dirModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
1062 }
1063 ++it;
1064 }
1065 m_cutItemsCache.clear();
1066
1067 // ... and apply an item effect to all currently cut items
1068 applyCutItemEffect();
1069 }
1070
1071 void DolphinView::createView()
1072 {
1073 // delete current view
1074 QAbstractItemView* view = itemView();
1075 if (view != 0) {
1076 m_topLayout->removeWidget(view);
1077 view->close();
1078 view->deleteLater();
1079 view = 0;
1080 m_iconsView = 0;
1081 m_detailsView = 0;
1082 m_fileItemDelegate = 0;
1083 }
1084
1085 Q_ASSERT(m_iconsView == 0);
1086 Q_ASSERT(m_detailsView == 0);
1087
1088 // ... and recreate it representing the current mode
1089 switch (m_mode) {
1090 case IconsView:
1091 m_iconsView = new DolphinIconsView(this, m_controller);
1092 view = m_iconsView;
1093 break;
1094
1095 case DetailsView:
1096 m_detailsView = new DolphinDetailsView(this, m_controller);
1097 view = m_detailsView;
1098 break;
1099 }
1100
1101 Q_ASSERT(view != 0);
1102
1103 m_fileItemDelegate = new KFileItemDelegate(view);
1104 view->setItemDelegate(m_fileItemDelegate);
1105
1106 view->setModel(m_proxyModel);
1107 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
1108
1109 new KMimeTypeResolver(view, m_dirModel);
1110 m_topLayout->insertWidget(1, view);
1111
1112 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
1113 m_controller, SLOT(indicateSelectionChange()));
1114 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
1115 this, SLOT(emitContentsMoved()));
1116 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1117 this, SLOT(emitContentsMoved()));
1118 }
1119
1120 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags)
1121 {
1122 QItemSelectionModel* selectionModel = itemView()->selectionModel();
1123 const QAbstractItemModel* itemModel = selectionModel->model();
1124
1125 const QModelIndex topLeft = itemModel->index(0, 0);
1126 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
1127 itemModel->columnCount() - 1);
1128
1129 QItemSelection selection(topLeft, bottomRight);
1130 selectionModel->select(selection, flags);
1131 }
1132
1133 QAbstractItemView* DolphinView::itemView() const
1134 {
1135 Q_ASSERT((m_iconsView == 0) || (m_detailsView == 0));
1136 if (m_detailsView != 0) {
1137 return m_detailsView;
1138 }
1139 return m_iconsView;
1140 }
1141
1142 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
1143 {
1144 return index.isValid() && (index.column() == KDirModel::Name);
1145 }
1146
1147 bool DolphinView::isCutItem(const KFileItem& item) const
1148 {
1149 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1150 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
1151
1152 const KUrl& itemUrl = item.url();
1153 KUrl::List::const_iterator it = cutUrls.begin();
1154 const KUrl::List::const_iterator end = cutUrls.end();
1155 while (it != end){
1156 if (*it == itemUrl) {
1157 return true;
1158 }
1159 ++it;
1160 }
1161
1162 return false;
1163 }
1164
1165 void DolphinView::applyCutItemEffect()
1166 {
1167 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
1168 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
1169 return;
1170 }
1171
1172 KFileItemList items(m_dirLister->items());
1173 KFileItemList::const_iterator it = items.begin();
1174 const KFileItemList::const_iterator end = items.end();
1175 while (it != end) {
1176 KFileItem* item = *it;
1177 if (isCutItem(*item)) {
1178 const QModelIndex index = m_dirModel->indexForItem(*item);
1179 const KFileItem* item = m_dirModel->itemForIndex(index);
1180 const QVariant value = m_dirModel->data(index, Qt::DecorationRole);
1181 if ((value.type() == QVariant::Icon) && (item != 0)) {
1182 const QIcon icon(qvariant_cast<QIcon>(value));
1183 QPixmap pixmap = icon.pixmap(128, 128);
1184
1185 // remember current pixmap for the item to be able
1186 // to restore it when other items get cut
1187 CutItem cutItem;
1188 cutItem.url = item->url();
1189 cutItem.pixmap = pixmap;
1190 m_cutItemsCache.append(cutItem);
1191
1192 // apply icon effect to the cut item
1193 KIconEffect iconEffect;
1194 pixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
1195 m_dirModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
1196 }
1197 }
1198 ++it;
1199 }
1200 }
1201
1202 #include "dolphinview.moc"