]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.h
Scroll to newly pasted files.
[dolphin.git] / src / views / dolphinview.h
1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz19@gmail.com> *
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 #ifndef DOLPHINVIEW_H
22 #define DOLPHINVIEW_H
23
24 #include <config-nepomuk.h>
25
26 #include "libdolphin_export.h"
27
28 #include <kparts/part.h>
29 #include <KFileItem>
30 #include <KFileItemDelegate>
31 #include <kio/fileundomanager.h>
32 #include <KIO/Job>
33
34 #include <QBoxLayout>
35 #include <QKeyEvent>
36 #include <QLinkedList>
37 #include <QSet>
38 #include <QWidget>
39
40 typedef KIO::FileUndoManager::CommandType CommandType;
41
42 class DolphinItemListView;
43 class KAction;
44 class KActionCollection;
45 class KFileItemModel;
46 class KItemListContainer;
47 class KItemModelBase;
48 class KUrl;
49 class ToolTipManager;
50 class VersionControlObserver;
51 class ViewProperties;
52 class QGraphicsSceneDragDropEvent;
53 class QRegExp;
54
55 /**
56 * @short Represents a view for the directory content.
57 *
58 * View modes for icons, compact and details are supported. It's
59 * possible to adjust:
60 * - sort order
61 * - sort type
62 * - show hidden files
63 * - show previews
64 * - enable grouping
65 */
66 class LIBDOLPHINPRIVATE_EXPORT DolphinView : public QWidget
67 {
68 Q_OBJECT
69
70 public:
71 /**
72 * Defines the view mode for a directory. The
73 * view mode is automatically updated if the directory itself
74 * defines a view mode (see class ViewProperties for details).
75 */
76 enum Mode
77 {
78 /**
79 * The items are shown as icons with a name-label below.
80 */
81 IconsView = 0,
82
83 /**
84 * The icon, the name and the size of the items are
85 * shown per default as a table.
86 */
87 DetailsView,
88
89 /**
90 * The items are shown as icons with the name-label aligned
91 * to the right side.
92 */
93 CompactView
94 };
95
96 /**
97 * @param url Specifies the content which should be shown.
98 * @param parent Parent widget of the view.
99 */
100 DolphinView(const KUrl& url, QWidget* parent);
101
102 virtual ~DolphinView();
103
104 /**
105 * Returns the current active URL, where all actions are applied.
106 * The URL navigator is synchronized with this URL.
107 */
108 KUrl url() const;
109
110 /**
111 * If \a active is true, the view will marked as active. The active
112 * view is defined as view where all actions are applied to.
113 */
114 void setActive(bool active);
115 bool isActive() const;
116
117 /**
118 * Changes the view mode for the current directory to \a mode.
119 * If the view properties should be remembered for each directory
120 * (GeneralSettings::globalViewProps() returns false), then the
121 * changed view mode will be stored automatically.
122 */
123 void setMode(Mode mode);
124 Mode mode() const;
125
126 /**
127 * Turns on the file preview for the all files of the current directory,
128 * if \a show is true.
129 * If the view properties should be remembered for each directory
130 * (GeneralSettings::globalViewProps() returns false), then the
131 * preview setting will be stored automatically.
132 */
133 void setPreviewsShown(bool show);
134 bool previewsShown() const;
135
136 /**
137 * Shows all hidden files of the current directory,
138 * if \a show is true.
139 * If the view properties should be remembered for each directory
140 * (GeneralSettings::globalViewProps() returns false), then the
141 * show hidden file setting will be stored automatically.
142 */
143 void setHiddenFilesShown(bool show);
144 bool hiddenFilesShown() const;
145
146 /**
147 * Turns on sorting by groups if \a enable is true.
148 */
149 void setGroupedSorting(bool grouped);
150 bool groupedSorting() const;
151
152 /**
153 * Returns the items of the view.
154 */
155 KFileItemList items() const;
156
157 /**
158 * @return The number of items. itemsCount() is faster in comparison
159 * to items().count().
160 */
161 int itemsCount() const;
162
163 /**
164 * Returns the selected items. The list is empty if no item has been
165 * selected.
166 */
167 KFileItemList selectedItems() const;
168
169 /**
170 * Returns the number of selected items (this is faster than
171 * invoking selectedItems().count()).
172 */
173 int selectedItemsCount() const;
174
175 /**
176 * Marks the items indicated by \p urls to get selected after the
177 * directory DolphinView::url() has been loaded. Note that nothing
178 * gets selected if no loading of a directory has been triggered
179 * by DolphinView::setUrl() or DolphinView::reload().
180 */
181 void markUrlsAsSelected(const QList<KUrl>& urls);
182
183 /**
184 * Marks the item indicated by \p url to be scrolled to and as the
185 * current item after directory DolphinView::url() has been loaded.
186 */
187 void markUrlAsCurrent(const KUrl& url);
188
189 /**
190 * All items that match to the pattern \a pattern will get selected
191 * if \a enabled is true and deselected if \a enabled is false.
192 */
193 void selectItems(const QRegExp& pattern, bool enabled);
194
195 /**
196 * Sets the zoom level to \a level. It is assured that the used
197 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
198 * ZoomLevelInfo::maximumLevel().
199 */
200 void setZoomLevel(int level);
201 int zoomLevel() const;
202
203 void setSortRole(const QByteArray& role);
204 QByteArray sortRole() const;
205
206 void setSortOrder(Qt::SortOrder order);
207 Qt::SortOrder sortOrder() const;
208
209 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
210 void setSortFoldersFirst(bool foldersFirst);
211 bool sortFoldersFirst() const;
212
213 /** Sets the additional information which should be shown for the items. */
214 void setVisibleRoles(const QList<QByteArray>& roles);
215
216 /** Returns the additional information which should be shown for the items. */
217 QList<QByteArray> visibleRoles() const;
218
219 void reload();
220 void stopLoading();
221
222 /**
223 * Refreshes the view to get synchronized with the settings (e.g. icons size,
224 * font, ...).
225 */
226 void readSettings();
227
228 /**
229 * Saves the current settings (e.g. icons size, font, ..).
230 */
231 void writeSettings();
232
233 /**
234 * Filters the currently shown items by \a nameFilter. All items
235 * which contain the given filter string will be shown.
236 */
237 void setNameFilter(const QString& nameFilter);
238 QString nameFilter() const;
239
240 /**
241 * Filters the currently shown items by \a filters. All items
242 * whose content-type matches those given by the list of filters
243 * will be shown.
244 */
245 void setMimeTypeFilters(const QStringList& filters);
246 QStringList mimeTypeFilters() const;
247
248 /**
249 * Returns a textual representation of the state of the current
250 * folder or selected items, suitable for use in the status bar.
251 */
252 QString statusBarText() const;
253
254 /**
255 * Returns the version control actions that are provided for the items \p items.
256 * Usually the actions are presented in the context menu.
257 */
258 QList<QAction*> versionControlActions(const KFileItemList& items) const;
259
260 /**
261 * Returns the state of the paste action:
262 * first is whether the action should be enabled
263 * second is the text for the action
264 */
265 QPair<bool, QString> pasteInfo() const;
266
267 /**
268 * If \a tabsForFiles is true, the signal tabRequested() will also
269 * emitted also for files. Per default tabs for files is disabled
270 * and hence the signal tabRequested() will only be emitted for
271 * directories.
272 */
273 void setTabsForFilesEnabled(bool tabsForFiles);
274 bool isTabsForFilesEnabled() const;
275
276 /**
277 * Returns true if the current view allows folders to be expanded,
278 * i.e. presents a hierarchical view to the user.
279 */
280 bool itemsExpandable() const;
281
282 /**
283 * Restores the view state (current item, contents position, details view expansion state)
284 */
285 void restoreState(QDataStream& stream);
286
287 /**
288 * Saves the view state (current item, contents position, details view expansion state)
289 */
290 void saveState(QDataStream& stream);
291
292 /**
293 * Returns the root item which represents the current URL.
294 */
295 KFileItem rootItem() const;
296
297 /**
298 * Sets a context that is used for remembering the view-properties.
299 * Per default the context is empty and the path of the currently set URL
300 * is used for remembering the view-properties. Setting a custom context
301 * makes sense if specific types of URLs (e.g. search-URLs) should
302 * share common view-properties.
303 */
304 void setViewPropertiesContext(const QString& context);
305 QString viewPropertiesContext() const;
306
307 public slots:
308 /**
309 * Changes the directory to \a url. If the current directory is equal to
310 * \a url, nothing will be done (use DolphinView::reload() instead).
311 */
312 void setUrl(const KUrl& url);
313
314 /**
315 * Selects all items.
316 * @see DolphinView::selectedItems()
317 */
318 void selectAll();
319
320 /**
321 * Inverts the current selection: selected items get unselected,
322 * unselected items get selected.
323 * @see DolphinView::selectedItems()
324 */
325 void invertSelection();
326
327 void clearSelection();
328
329 /**
330 * Triggers the renaming of the currently selected items, where
331 * the user must input a new name for the items.
332 */
333 void renameSelectedItems();
334
335 /**
336 * Moves all selected items to the trash.
337 */
338 void trashSelectedItems();
339
340 /**
341 * Deletes all selected items.
342 */
343 void deleteSelectedItems();
344
345 /**
346 * Copies all selected items to the clipboard and marks
347 * the items as cut.
348 */
349 void cutSelectedItems();
350
351 /** Copies all selected items to the clipboard. */
352 void copySelectedItems();
353
354 /** Pastes the clipboard data to this view. */
355 void paste();
356
357 /**
358 * Pastes the clipboard data into the currently selected
359 * folder. If the current selection is not exactly one folder, no
360 * paste operation is done.
361 */
362 void pasteIntoFolder();
363
364 /** Activates the view if the item list container gets focus. */
365 virtual bool eventFilter(QObject* watched, QEvent* event);
366
367 signals:
368 /**
369 * Is emitted if the view has been activated by e. g. a mouse click.
370 */
371 void activated();
372
373 /**
374 * Is emitted if the URL of the view will be changed to \a url.
375 * After the URL has been changed the signal urlChanged() will
376 * be emitted.
377 */
378 void urlAboutToBeChanged(const KUrl& url);
379
380 /** Is emitted if the URL of the view has been changed to \a url. */
381 void urlChanged(const KUrl& url);
382
383 /**
384 * Is emitted when clicking on an item with the left mouse button.
385 */
386 void itemActivated(const KFileItem& item);
387
388 /**
389 * Is emitted when multiple items have been activated by e. g.
390 * context menu open with.
391 */
392 void itemsActivated(const KFileItemList& items);
393
394 /**
395 * Is emitted if items have been added or deleted.
396 */
397 void itemCountChanged();
398
399 /**
400 * Is emitted if a new tab should be opened for the URL \a url.
401 */
402 void tabRequested(const KUrl& url);
403
404 /**
405 * Is emitted if the view mode (IconsView, DetailsView,
406 * PreviewsView) has been changed.
407 */
408 void modeChanged(DolphinView::Mode current, DolphinView::Mode previous);
409
410 /** Is emitted if the 'show preview' property has been changed. */
411 void previewsShownChanged(bool shown);
412
413 /** Is emitted if the 'show hidden files' property has been changed. */
414 void hiddenFilesShownChanged(bool shown);
415
416 /** Is emitted if the 'grouped sorting' property has been changed. */
417 void groupedSortingChanged(bool groupedSorting);
418
419 /** Is emitted if the sorting by name, size or date has been changed. */
420 void sortRoleChanged(const QByteArray& role);
421
422 /** Is emitted if the sort order (ascending or descending) has been changed. */
423 void sortOrderChanged(Qt::SortOrder order);
424
425 /**
426 * Is emitted if the sorting of files and folders (separate with folders
427 * first or mixed) has been changed.
428 */
429 void sortFoldersFirstChanged(bool foldersFirst);
430
431 /** Is emitted if the additional information shown for this view has been changed. */
432 void visibleRolesChanged(const QList<QByteArray>& current,
433 const QList<QByteArray>& previous);
434
435 /** Is emitted if the zoom level has been changed by zooming in or out. */
436 void zoomLevelChanged(int current, int previous);
437
438 /**
439 * Is emitted if information of an item is requested to be shown e. g. in the panel.
440 * If item is null, no item information request is pending.
441 */
442 void requestItemInfo(const KFileItem& item);
443
444 /**
445 * Is emitted whenever the selection has been changed.
446 */
447 void selectionChanged(const KFileItemList& selection);
448
449 /**
450 * Is emitted if a context menu is requested for the item \a item,
451 * which is part of \a url. If the item is null, the context menu
452 * for the URL should be shown and the custom actions \a customActions
453 * will be added.
454 */
455 void requestContextMenu(const QPoint& pos,
456 const KFileItem& item,
457 const KUrl& url,
458 const QList<QAction*>& customActions);
459
460 /**
461 * Is emitted if an information message with the content \a msg
462 * should be shown.
463 */
464 void infoMessage(const QString& msg);
465
466 /**
467 * Is emitted if an error message with the content \a msg
468 * should be shown.
469 */
470 void errorMessage(const QString& msg);
471
472 /**
473 * Is emitted if an "operation completed" message with the content \a msg
474 * should be shown.
475 */
476 void operationCompletedMessage(const QString& msg);
477
478 /**
479 * Is emitted after DolphinView::setUrl() has been invoked and
480 * the current directory is loaded. If this signal is emitted,
481 * it is assured that the view contains already the correct root
482 * URL and property settings.
483 */
484 void directoryLoadingStarted();
485
486 /**
487 * Is emitted after the directory triggered by DolphinView::setUrl()
488 * has been loaded.
489 */
490 void directoryLoadingCompleted();
491
492 /**
493 * Is emitted after the directory loading triggered by DolphinView::setUrl()
494 * has been canceled.
495 */
496 void directoryLoadingCanceled();
497
498 /**
499 * Is emitted after DolphinView::setUrl() has been invoked and provides
500 * the information how much percent of the current directory have been loaded.
501 */
502 void directoryLoadingProgress(int percent);
503
504 /**
505 * Is emitted if the sorting is done asynchronously and provides the
506 * progress information of the sorting.
507 */
508 void directorySortingProgress(int percent);
509
510 /**
511 * Emitted when the file-item-model emits redirection.
512 * Testcase: fish://localhost
513 */
514 void redirection(const KUrl& oldUrl, const KUrl& newUrl);
515
516 /**
517 * Is emitted when the URL set by DolphinView::setUrl() represents a file.
518 * In this case no signal errorMessage() will be emitted.
519 */
520 void urlIsFileError(const KUrl& url);
521
522 /**
523 * Is emitted when the write state of the folder has been changed. The application
524 * should disable all actions like "Create New..." that depend on the write
525 * state.
526 */
527 void writeStateChanged(bool isFolderWritable);
528
529 /**
530 * Is emitted if the URL should be changed to the previous URL of the
531 * history (e.g. because the "back"-mousebutton has been pressed).
532 */
533 void goBackRequested();
534
535 /**
536 * Is emitted if the URL should be changed to the next URL of the
537 * history (e.g. because the "next"-mousebutton has been pressed).
538 */
539 void goForwardRequested();
540
541 protected:
542 /** Changes the zoom level if Control is pressed during a wheel event. */
543 virtual void wheelEvent(QWheelEvent* event);
544
545 /** @reimp */
546 virtual void hideEvent(QHideEvent* event);
547 virtual bool event(QEvent* event);
548
549 private slots:
550 /**
551 * Marks the view as active (DolphinView:isActive() will return true)
552 * and emits the 'activated' signal if it is not already active.
553 */
554 void activate();
555
556 void slotItemActivated(int index);
557 void slotItemsActivated(const QSet<int>& indexes);
558 void slotItemMiddleClicked(int index);
559 void slotItemContextMenuRequested(int index, const QPointF& pos);
560 void slotViewContextMenuRequested(const QPointF& pos);
561 void slotHeaderContextMenuRequested(const QPointF& pos);
562 void slotHeaderColumnWidthChanged(const QByteArray& role, qreal current, qreal previous);
563 void slotItemHovered(int index);
564 void slotItemUnhovered(int index);
565 void slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event);
566 void slotModelChanged(KItemModelBase* current, KItemModelBase* previous);
567 void slotMouseButtonPressed(int itemIndex, Qt::MouseButtons buttons);
568
569 /*
570 * Is called when new items get pasted or dropped.
571 */
572 void slotAboutToCreate(const KUrl::List& urls);
573
574 /**
575 * Emits the signal \a selectionChanged() with a small delay. This is
576 * because getting all file items for the selection can be an expensive
577 * operation. Fast selection changes are collected in this case and
578 * the signal is emitted only after no selection change has been done
579 * within a small delay.
580 */
581 void slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous);
582
583 /**
584 * Is called by emitDelayedSelectionChangedSignal() and emits the
585 * signal \a selectionChanged() with all selected file items as parameter.
586 */
587 void emitSelectionChangedSignal();
588
589 /**
590 * Updates the view properties of the current URL to the
591 * sorting given by \a role.
592 */
593 void updateSortRole(const QByteArray& role);
594
595 /**
596 * Updates the view properties of the current URL to the
597 * sort order given by \a order.
598 */
599 void updateSortOrder(Qt::SortOrder order);
600
601 /**
602 * Updates the view properties of the current URL to the
603 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
604 */
605 void updateSortFoldersFirst(bool foldersFirst);
606
607 /**
608 * Updates the status bar to show hover information for the
609 * item \a item. If currently other items are selected,
610 * no hover information is shown.
611 * @see DolphinView::clearHoverInformation()
612 */
613 void showHoverInformation(const KFileItem& item);
614
615 /**
616 * Clears the hover information shown in the status bar.
617 * @see DolphinView::showHoverInformation().
618 */
619 void clearHoverInformation();
620
621 /**
622 * Indicates in the status bar that the delete operation
623 * of the job \a job has been finished.
624 */
625 void slotDeleteFileFinished(KJob* job);
626
627 /**
628 * Invoked when the file item model has started the loading
629 * of the directory specified by DolphinView::url().
630 */
631 void slotDirectoryLoadingStarted();
632
633 /**
634 * Invoked when the file item model indicates that the loading of a directory has
635 * been completed. Assures that pasted items and renamed items get seleced.
636 */
637 void slotDirectoryLoadingCompleted();
638
639 /**
640 * Is invoked when items of KFileItemModel have been changed.
641 */
642 void slotItemsChanged();
643
644 /**
645 * Is invoked when the sort order has been changed by the user by clicking
646 * on a header item. The view properties of the directory will get updated.
647 */
648 void slotSortOrderChangedByHeader(Qt::SortOrder current, Qt::SortOrder previous);
649
650 /**
651 * Is invoked when the sort role has been changed by the user by clicking
652 * on a header item. The view properties of the directory will get updated.
653 */
654 void slotSortRoleChangedByHeader(const QByteArray& current, const QByteArray& previous);
655
656 /**
657 * Is invoked when the visible roles have been changed by the user by dragging
658 * a header item. The view properties of the directory will get updated.
659 */
660 void slotVisibleRolesChangedByHeader(const QList<QByteArray>& current,
661 const QList<QByteArray>& previous);
662
663 void slotRoleEditingFinished(int index, const QByteArray& role, const QVariant& value);
664
665 /**
666 * Observes the item with the URL \a url. As soon as the directory
667 * model indicates that the item is available, the item will
668 * get selected and it is assured that the item stays visible.
669 */
670 void observeCreatedItem(const KUrl& url);
671
672 /**
673 * Called when a redirection happens.
674 * Testcase: fish://localhost
675 */
676 void slotDirectoryRedirection(const KUrl& oldUrl, const KUrl& newUrl);
677
678 /**
679 * Applies the state that has been restored by restoreViewState()
680 * to the view.
681 */
682 void updateViewState();
683
684 void hideToolTip();
685
686 /**
687 * Calculates the number of currently shown files into
688 * \a fileCount and the number of folders into \a folderCount.
689 * The size of all files is written into \a totalFileSize.
690 * It is recommend using this method instead of asking the
691 * directory lister or the model directly, as it takes
692 * filtering and hierarchical previews into account.
693 */
694 void calculateItemCount(int& fileCount, int& folderCount, KIO::filesize_t& totalFileSize) const;
695
696 private:
697 void loadDirectory(const KUrl& url, bool reload = false);
698
699 /**
700 * Applies the view properties which are defined by the current URL
701 * to the DolphinView properties.
702 */
703 void applyViewProperties();
704
705 /**
706 * Applies the m_mode property to the corresponding
707 * itemlayout-property of the KItemListView.
708 */
709 void applyModeToView();
710
711 /**
712 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
713 * Pastes the clipboard data into the URL \a url.
714 */
715 void pasteToUrl(const KUrl& url);
716
717 /**
718 * Returns a list of URLs for all selected items. The list is
719 * simplified, so that when the URLs are part of different tree
720 * levels, only the parent is returned.
721 */
722 KUrl::List simplifiedSelectedUrls() const;
723
724 /**
725 * Returns the MIME data for all selected items.
726 */
727 QMimeData* selectionMimeData() const;
728
729 /**
730 * Updates m_isFolderWritable dependent on whether the folder represented by
731 * the current URL is writable. If the state has changed, the signal
732 * writeableStateChanged() will be emitted.
733 */
734 void updateWritableState();
735
736 /**
737 * @return The current URL if no viewproperties-context is given (see
738 * DolphinView::viewPropertiesContext(), otherwise the context
739 * is returned.
740 */
741 KUrl viewPropertiesUrl() const;
742
743 private:
744 bool m_active;
745 bool m_tabsForFiles;
746 bool m_assureVisibleCurrentIndex;
747 bool m_isFolderWritable;
748 bool m_dragging; // True if a dragging is done. Required to be able to decide whether a
749 // tooltip may be shown when hovering an item.
750
751 KUrl m_url;
752 QString m_viewPropertiesContext;
753 Mode m_mode;
754 QList<QByteArray> m_visibleRoles;
755
756 QVBoxLayout* m_topLayout;
757
758 KFileItemModel* m_model;
759 DolphinItemListView* m_view;
760 KItemListContainer* m_container;
761
762 ToolTipManager* m_toolTipManager;
763
764 QTimer* m_selectionChangedTimer;
765
766 KUrl m_currentItemUrl; // Used for making the view to remember the current URL after F5
767 bool m_scrollToCurrentItem; // Used for marking we need to scroll to current item or not
768 QPoint m_restoredContentsPosition;
769
770 QList<KUrl> m_selectedUrls; // Used for making the view to remember selections after F5
771 bool m_clearSelectionBeforeSelectingNewItems;
772 bool m_markFirstNewlySelectedItemAsCurrent;
773
774 VersionControlObserver* m_versionControlObserver;
775
776 // For unit tests
777 friend class TestBase;
778 friend class DolphinDetailsViewTest;
779 friend class DolphinPart; // Accesses m_model
780 };
781
782 /// Allow using DolphinView::Mode in QVariant
783 Q_DECLARE_METATYPE(DolphinView::Mode)
784
785 #endif // DOLPHINVIEW_H