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