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