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