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