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