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