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