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