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