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