]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
4d6a9ed68c9d854028a2b22391640a98fbf3451b
[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
109 m_proxyModel = new DolphinSortFilterProxyModel(this);
110 m_proxyModel->setSourceModel(m_dirModel);
111
112 m_iconsView->setModel(m_proxyModel);
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 /*
482 * Our view has a selection, we will map them back to the DirModel
483 * and then fill the KFileItemList
484 */
485 KFileItemList DolphinView::selectedItems() const
486 {
487 assert( m_iconsView && m_iconsView->selectionModel() );
488
489 const QItemSelection selection = m_proxyModel->mapSelectionToSource( m_iconsView->selectionModel()->selection() );
490 KFileItemList itemList;
491
492 const QModelIndexList indexList = selection.indexes();
493 QModelIndexList::const_iterator end = indexList.end();
494 for (QModelIndexList::const_iterator it = indexList.begin(); it != end; ++it) {
495 assert( (*it).isValid() );
496
497 KFileItem* item = m_dirModel->itemForIndex(*it);
498 if (item != 0) {
499 itemList.append(item);
500 }
501 }
502
503 return itemList;
504 }
505
506 KUrl::List DolphinView::selectedUrls() const
507 {
508 KUrl::List urls;
509
510 const KFileItemList list = selectedItems();
511 KFileItemList::const_iterator it = list.begin();
512 const KFileItemList::const_iterator end = list.end();
513 while (it != end) {
514 KFileItem* item = *it;
515 urls.append(item->url());
516 ++it;
517 }
518
519 return urls;
520 }
521
522 const KFileItem* DolphinView::currentFileItem() const
523 {
524 return 0; // fileView()->currentFileItem();
525 }
526
527 void DolphinView::openContextMenu(KFileItem* fileInfo, const QPoint& pos)
528 {
529 DolphinContextMenu contextMenu(this, fileInfo, pos);
530 contextMenu.open();
531 }
532
533 void DolphinView::rename(const KUrl& source, const QString& newName)
534 {
535 bool ok = false;
536
537 if (newName.isEmpty() || (source.fileName() == newName)) {
538 return;
539 }
540
541 KUrl dest(source.upUrl());
542 dest.addPath(newName);
543
544 const bool destExists = KIO::NetAccess::exists(dest,
545 false,
546 mainWindow()->activeView());
547 if (destExists) {
548 // the destination already exists, hence ask the user
549 // how to proceed...
550 KIO::RenameDialog renameDialog(this,
551 i18n("File Already Exists"),
552 source.path(),
553 dest.path(),
554 KIO::M_OVERWRITE);
555 switch (renameDialog.exec()) {
556 case KIO::R_OVERWRITE:
557 // the destination should be overwritten
558 ok = KIO::NetAccess::file_move(source, dest, -1, true);
559 break;
560
561 case KIO::R_RENAME: {
562 // a new name for the destination has been used
563 KUrl newDest(renameDialog.newDestUrl());
564 ok = KIO::NetAccess::file_move(source, newDest);
565 break;
566 }
567
568 default:
569 // the renaming operation has been canceled
570 reload();
571 return;
572 }
573 }
574 else {
575 // no destination exists, hence just move the file to
576 // do the renaming
577 ok = KIO::NetAccess::file_move(source, dest);
578 }
579
580 if (ok) {
581 m_statusBar->setMessage(i18n("Renamed file '%1' to '%2'.",source.fileName(), dest.fileName()),
582 DolphinStatusBar::OperationCompleted);
583
584 DolphinCommand command(DolphinCommand::Rename, source, dest);
585 UndoManager::instance().addCommand(command);
586 }
587 else {
588 m_statusBar->setMessage(i18n("Renaming of file '%1' to '%2' failed.",source.fileName(), dest.fileName()),
589 DolphinStatusBar::Error);
590 reload();
591 }
592 }
593
594 void DolphinView::reload()
595 {
596 startDirLister(m_urlNavigator->url(), true);
597 }
598
599 void DolphinView::slotUrlListDropped(QDropEvent* /* event */,
600 const KUrl::List& urls,
601 const KUrl& url)
602 {
603 KUrl destination(url);
604 if (destination.isEmpty()) {
605 destination = m_urlNavigator->url();
606 }
607 else {
608 // Check whether the destination Url is a directory. If this is not the
609 // case, use the navigator Url as destination (otherwise the destination,
610 // which represents a file, would be replaced by a copy- or move-operation).
611 KFileItem fileItem(KFileItem::Unknown, KFileItem::Unknown, destination);
612 if (!fileItem.isDir()) {
613 destination = m_urlNavigator->url();
614 }
615 }
616
617 mainWindow()->dropUrls(urls, destination);
618 }
619
620 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
621 {
622 QWidget::mouseReleaseEvent(event);
623 mainWindow()->setActiveView(this);
624 }
625
626 DolphinMainWindow* DolphinView::mainWindow() const
627 {
628 return m_mainWindow;
629 }
630
631 void DolphinView::loadDirectory(const KUrl& url)
632 {
633 const ViewProperties props(url);
634
635 const Mode mode = props.viewMode();
636 if (m_mode != mode) {
637 m_mode = mode;
638 applyModeToView();
639 emit modeChanged();
640 }
641
642 const bool showHiddenFiles = props.showHiddenFiles();
643 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
644 m_dirLister->setShowingDotFiles(showHiddenFiles);
645 emit showHiddenFilesChanged();
646 }
647
648 const DolphinView::Sorting sorting = props.sorting();
649 if (sorting != m_proxyModel->sorting()) {
650 m_proxyModel->setSorting(sorting);
651 emit sortingChanged(sorting);
652 }
653
654 const Qt::SortOrder sortOrder = props.sortOrder();
655 if (sortOrder != m_proxyModel->sortOrder()) {
656 m_proxyModel->setSortOrder(sortOrder);
657 emit sortOrderChanged(sortOrder);
658 }
659
660 // TODO: handle previews (props.showPreview())
661
662 startDirLister(url);
663 emit urlChanged(url);
664 }
665
666 void DolphinView::triggerItem(const QModelIndex& index)
667 {
668 KFileItem* item = m_dirModel->itemForIndex(m_proxyModel->mapToSource(index));
669 if (item == 0) {
670 return;
671 }
672
673 if (item->isDir()) {
674 // Prefer the local path over the Url. This assures that the
675 // volume space information is correct. Assuming that the Url is media:/sda1,
676 // and the local path is /windows/C: For the Url the space info is related
677 // to the root partition (and hence wrong) and for the local path the space
678 // info is related to the windows partition (-> correct).
679 //m_dirLister->stop();
680 //m_dirLister->openUrl(item->url());
681 //return;
682
683 const QString localPath(item->localPath());
684 if (localPath.isEmpty()) {
685 setUrl(item->url());
686 }
687 else {
688 setUrl(KUrl(localPath));
689 }
690 }
691 else {
692 item->run();
693 }
694 }
695
696 void DolphinView::slotPercent(int percent)
697 {
698 if (m_showProgress) {
699 m_statusBar->setProgress(percent);
700 }
701 }
702
703 void DolphinView::slotClear()
704 {
705 //fileView()->clearView();
706 updateStatusBar();
707 }
708
709 void DolphinView::slotDeleteItem(KFileItem* item)
710 {
711 //fileView()->removeItem(item);
712 updateStatusBar();
713 }
714
715 void DolphinView::slotCompleted()
716 {
717 m_refreshing = true;
718
719 //KFileView* view = fileView();
720 //view->clearView();
721
722 // TODO: in Qt4 the code should get a lot
723 // simpler and nicer due to Interview...
724 /*if (m_iconsView != 0) {
725 m_iconsView->beginItemUpdates();
726 }
727 if (m_iconsView != 0) {
728 m_iconsView->beginItemUpdates();
729 }*/
730
731 if (m_showProgress) {
732 m_statusBar->setProgressText(QString::null);
733 m_statusBar->setProgress(100);
734 m_showProgress = false;
735 }
736
737 KFileItemList items(m_dirLister->items());
738 KFileItemList::const_iterator it = items.begin();
739 const KFileItemList::const_iterator end = items.end();
740
741 m_fileCount = 0;
742 m_folderCount = 0;
743
744 while (it != end) {
745 KFileItem* item = *it;
746 //view->insertItem(item);
747 if (item->isDir()) {
748 ++m_folderCount;
749 }
750 else {
751 ++m_fileCount;
752 }
753 ++it;
754 }
755
756 updateStatusBar();
757
758 /*if (m_iconsView != 0) {
759 // Prevent a flickering of the icon view widget by giving a small
760 // timeslot to swallow asynchronous update events.
761 m_iconsView->setUpdatesEnabled(false);
762 QTimer::singleShot(10, this, SLOT(slotDelayedUpdate()));
763 }
764
765 if (m_iconsView != 0) {
766 m_iconsView->endItemUpdates();
767 m_refreshing = false;
768 }*/
769 }
770
771 void DolphinView::slotInfoMessage(const QString& msg)
772 {
773 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
774 }
775
776 void DolphinView::slotErrorMessage(const QString& msg)
777 {
778 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
779 }
780
781 void DolphinView::slotGrabActivation()
782 {
783 mainWindow()->setActiveView(this);
784 }
785
786 void DolphinView::emitSelectionChangedSignal()
787 {
788 emit selectionChanged();
789 }
790
791 void DolphinView::closeFilterBar()
792 {
793 m_filterBar->hide();
794 emit showFilterBarChanged(false);
795 }
796
797 void DolphinView::slotContentsMoving(int x, int y)
798 {
799 if (!m_refreshing) {
800 // Only emit a 'contents moved' signal if the user
801 // moved the content by adjusting the sliders. Adjustments
802 // resulted by refreshing a directory should not be respected.
803 emit contentsMoved(x, y);
804 }
805 }
806
807 void DolphinView::startDirLister(const KUrl& url, bool reload)
808 {
809 if (!url.isValid()) {
810 const QString location(url.pathOrUrl());
811 if (location.isEmpty()) {
812 m_statusBar->setMessage(i18n("The location is empty."), DolphinStatusBar::Error);
813 }
814 else {
815 m_statusBar->setMessage(i18n("The location '%1' is invalid.",location),
816 DolphinStatusBar::Error);
817 }
818 return;
819 }
820
821 // Only show the directory loading progress if the status bar does
822 // not contain another progress information. This means that
823 // the directory loading progress information has the lowest priority.
824 const QString progressText(m_statusBar->progressText());
825 m_showProgress = progressText.isEmpty() ||
826 (progressText == i18n("Loading directory..."));
827 if (m_showProgress) {
828 m_statusBar->setProgressText(i18n("Loading directory..."));
829 m_statusBar->setProgress(0);
830 }
831
832 m_refreshing = true;
833 m_dirLister->stop();
834 m_dirLister->openUrl(url, false, reload);
835 }
836
837 QString DolphinView::defaultStatusBarText() const
838 {
839 // TODO: the following code is not suitable for languages where multiple forms
840 // of plurals are given (e. g. in Poland three forms of plurals exist).
841 const int itemCount = m_folderCount + m_fileCount;
842
843 QString text;
844 if (itemCount == 1) {
845 text = i18n("1 Item");
846 }
847 else {
848 text = i18n("%1 Items",itemCount);
849 }
850
851 text += " (";
852
853 if (m_folderCount == 1) {
854 text += i18n("1 Folder");
855 }
856 else {
857 text += i18n("%1 Folders",m_folderCount);
858 }
859
860 text += ", ";
861
862 if (m_fileCount == 1) {
863 text += i18n("1 File");
864 }
865 else {
866 text += i18n("%1 Files",m_fileCount);
867 }
868
869 text += ")";
870
871 return text;
872 }
873
874 QString DolphinView::selectionStatusBarText() const
875 {
876 // TODO: the following code is not suitable for languages where multiple forms
877 // of plurals are given (e. g. in Poland three forms of plurals exist).
878 QString text;
879 const KFileItemList list = selectedItems();
880 if (list.isEmpty()) {
881 // TODO: assert(!list.isEmpty()) should be used, as this method is only invoked if
882 // DolphinView::hasSelection() is true. Inconsistent behavior?
883 return QString();
884 }
885
886 int fileCount = 0;
887 int folderCount = 0;
888 KIO::filesize_t byteSize = 0;
889 KFileItemList::const_iterator it = list.begin();
890 const KFileItemList::const_iterator end = list.end();
891 while (it != end){
892 KFileItem* item = *it;
893 if (item->isDir()) {
894 ++folderCount;
895 }
896 else {
897 ++fileCount;
898 byteSize += item->size();
899 }
900 ++it;
901 }
902
903 if (folderCount == 1) {
904 text = i18n("1 Folder selected");
905 }
906 else if (folderCount > 1) {
907 text = i18n("%1 Folders selected",folderCount);
908 }
909
910 if ((fileCount > 0) && (folderCount > 0)) {
911 text += ", ";
912 }
913
914 const QString sizeText(KIO::convertSize(byteSize));
915 if (fileCount == 1) {
916 text += i18n("1 File selected (%1)",sizeText);
917 }
918 else if (fileCount > 1) {
919 text += i18n("%1 Files selected (%1)",fileCount,sizeText);
920 }
921
922 return text;
923 }
924
925 QString DolphinView::renameIndexPresentation(int index, int itemCount) const
926 {
927 // assure that the string reprentation for all indicess have the same
928 // number of characters based on the given number of items
929 QString str(QString::number(index));
930 int chrCount = 1;
931 while (itemCount >= 10) {
932 ++chrCount;
933 itemCount /= 10;
934 }
935 str.reserve(chrCount);
936
937 const int insertCount = chrCount - str.length();
938 for (int i = 0; i < insertCount; ++i) {
939 str.insert(0, '0');
940 }
941 return str;
942 }
943
944 void DolphinView::slotShowFilterBar(bool show)
945 {
946 assert(m_filterBar != 0);
947 if (show) {
948 m_filterBar->show();
949 }
950 else {
951 m_filterBar->hide();
952 }
953 }
954
955 void DolphinView::declareViewActive()
956 {
957 mainWindow()->setActiveView( this );
958 }
959
960 void DolphinView::slotChangeNameFilter(const QString& nameFilter)
961 {
962 // The name filter of KDirLister does a 'hard' filtering, which
963 // means that only the items are shown where the names match
964 // exactly the filter. This is non-transparent for the user, which
965 // just wants to have a 'soft' filtering: does the name contain
966 // the filter string?
967 QString adjustedFilter(nameFilter);
968 adjustedFilter.insert(0, '*');
969 adjustedFilter.append('*');
970
971 /*
972 * Use the ProxyModel to filter:
973 * This code is #ifdefed as setNameFilter behaves
974 * slightly different than the QSortFilterProxyModel
975 * as it will not remove directories. I will ask
976 * our beloved usability experts for input
977 * -- z.
978 */
979 #if 0
980 m_dirLister->setNameFilter(adjustedFilter);
981 m_dirLister->emitChanges();
982 #else
983 m_proxyModel->setFilterRegExp( nameFilter );
984 #endif
985 }
986
987 void DolphinView::applyModeToView()
988 {
989 m_iconsView->setSelectionMode(QAbstractItemView::ExtendedSelection);
990
991 // TODO: the following code just tries to test some QListView capabilities
992 switch (m_mode) {
993 case IconsView:
994 m_iconsView->setViewMode(QListView::IconMode);
995 m_iconsView->setSpacing(32);
996 // m_iconsView->setAlternatingRowColors(false);
997 // m_iconsView->setGridSize(QSize(128, 64));
998 break;
999
1000 case DetailsView:
1001 m_iconsView->setViewMode(QListView::ListMode);
1002 m_iconsView->setSpacing(0);
1003 // m_iconsView->setAlternatingRowColors(true);
1004 // m_iconsView->setGridSize(QSize(256, 24));
1005 break;
1006 }
1007 }
1008
1009 int DolphinView::columnIndex(Sorting sorting) const
1010 {
1011 int index = 0;
1012 switch (sorting) {
1013 case SortByName: index = KDirModel::Name; break;
1014 case SortBySize: index = KDirModel::Size; break;
1015 case SortByDate: index = KDirModel::ModifiedTime; break;
1016 default: assert(false);
1017 }
1018 return index;
1019 }
1020
1021 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags)
1022 {
1023 QItemSelectionModel* selectionModel = m_iconsView->selectionModel();
1024 const QAbstractItemModel* itemModel = selectionModel->model();
1025
1026 const QModelIndex topLeft = itemModel->index(0, 0);
1027 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
1028 itemModel->columnCount() - 1);
1029
1030 QItemSelection selection(topLeft, bottomRight);
1031 selectionModel->select(selection, flags);
1032 }
1033
1034 #include "dolphinview.moc"