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