]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.h
Make --select can automatically scroll to the selected file.
[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 * Returns a textual representation of the state of the current
242 * folder or selected items, suitable for use in the status bar.
243 */
244 QString statusBarText() const;
245
246 /**
247 * Returns the version control actions that are provided for the items \p items.
248 * Usually the actions are presented in the context menu.
249 */
250 QList<QAction*> versionControlActions(const KFileItemList& items) const;
251
252 /**
253 * Returns the state of the paste action:
254 * first is whether the action should be enabled
255 * second is the text for the action
256 */
257 QPair<bool, QString> pasteInfo() const;
258
259 /**
260 * If \a tabsForFiles is true, the signal tabRequested() will also
261 * emitted also for files. Per default tabs for files is disabled
262 * and hence the signal tabRequested() will only be emitted for
263 * directories.
264 */
265 void setTabsForFilesEnabled(bool tabsForFiles);
266 bool isTabsForFilesEnabled() const;
267
268 /**
269 * Returns true if the current view allows folders to be expanded,
270 * i.e. presents a hierarchical view to the user.
271 */
272 bool itemsExpandable() const;
273
274 /**
275 * Restores the view state (current item, contents position, details view expansion state)
276 */
277 void restoreState(QDataStream& stream);
278
279 /**
280 * Saves the view state (current item, contents position, details view expansion state)
281 */
282 void saveState(QDataStream& stream);
283
284 /**
285 * Returns the root item which represents the current URL.
286 */
287 KFileItem rootItem() const;
288
289 /**
290 * Sets a context that is used for remembering the view-properties.
291 * Per default the context is empty and the path of the currently set URL
292 * is used for remembering the view-properties. Setting a custom context
293 * makes sense if specific types of URLs (e.g. search-URLs) should
294 * share common view-properties.
295 */
296 void setViewPropertiesContext(const QString& context);
297 QString viewPropertiesContext() const;
298
299 public slots:
300 /**
301 * Changes the directory to \a url. If the current directory is equal to
302 * \a url, nothing will be done (use DolphinView::reload() instead).
303 */
304 void setUrl(const KUrl& url);
305
306 /**
307 * Selects all items.
308 * @see DolphinView::selectedItems()
309 */
310 void selectAll();
311
312 /**
313 * Inverts the current selection: selected items get unselected,
314 * unselected items get selected.
315 * @see DolphinView::selectedItems()
316 */
317 void invertSelection();
318
319 void clearSelection();
320
321 /**
322 * Triggers the renaming of the currently selected items, where
323 * the user must input a new name for the items.
324 */
325 void renameSelectedItems();
326
327 /**
328 * Moves all selected items to the trash.
329 */
330 void trashSelectedItems();
331
332 /**
333 * Deletes all selected items.
334 */
335 void deleteSelectedItems();
336
337 /**
338 * Copies all selected items to the clipboard and marks
339 * the items as cut.
340 */
341 void cutSelectedItems();
342
343 /** Copies all selected items to the clipboard. */
344 void copySelectedItems();
345
346 /** Pastes the clipboard data to this view. */
347 void paste();
348
349 /**
350 * Pastes the clipboard data into the currently selected
351 * folder. If the current selection is not exactly one folder, no
352 * paste operation is done.
353 */
354 void pasteIntoFolder();
355
356 /** Activates the view if the item list container gets focus. */
357 virtual bool eventFilter(QObject* watched, QEvent* event);
358
359 signals:
360 /**
361 * Is emitted if the view has been activated by e. g. a mouse click.
362 */
363 void activated();
364
365 /**
366 * Is emitted if the URL of the view will be changed to \a url.
367 * After the URL has been changed the signal urlChanged() will
368 * be emitted.
369 */
370 void urlAboutToBeChanged(const KUrl& url);
371
372 /** Is emitted if the URL of the view has been changed to \a url. */
373 void urlChanged(const KUrl& url);
374
375 /**
376 * Is emitted when clicking on an item with the left mouse button.
377 */
378 void itemActivated(const KFileItem& item);
379
380 /**
381 * Is emitted if items have been added or deleted.
382 */
383 void itemCountChanged();
384
385 /**
386 * Is emitted if a new tab should be opened for the URL \a url.
387 */
388 void tabRequested(const KUrl& url);
389
390 /**
391 * Is emitted if the view mode (IconsView, DetailsView,
392 * PreviewsView) has been changed.
393 */
394 void modeChanged(DolphinView::Mode current, DolphinView::Mode previous);
395
396 /** Is emitted if the 'show preview' property has been changed. */
397 void previewsShownChanged(bool shown);
398
399 /** Is emitted if the 'show hidden files' property has been changed. */
400 void hiddenFilesShownChanged(bool shown);
401
402 /** Is emitted if the 'grouped sorting' property has been changed. */
403 void groupedSortingChanged(bool groupedSorting);
404
405 /** Is emitted if the sorting by name, size or date has been changed. */
406 void sortRoleChanged(const QByteArray& role);
407
408 /** Is emitted if the sort order (ascending or descending) has been changed. */
409 void sortOrderChanged(Qt::SortOrder order);
410
411 /**
412 * Is emitted if the sorting of files and folders (separate with folders
413 * first or mixed) has been changed.
414 */
415 void sortFoldersFirstChanged(bool foldersFirst);
416
417 /** Is emitted if the additional information shown for this view has been changed. */
418 void visibleRolesChanged(const QList<QByteArray>& current,
419 const QList<QByteArray>& previous);
420
421 /** Is emitted if the zoom level has been changed by zooming in or out. */
422 void zoomLevelChanged(int current, int previous);
423
424 /**
425 * Is emitted if information of an item is requested to be shown e. g. in the panel.
426 * If item is null, no item information request is pending.
427 */
428 void requestItemInfo(const KFileItem& item);
429
430 /**
431 * Is emitted whenever the selection has been changed.
432 */
433 void selectionChanged(const KFileItemList& selection);
434
435 /**
436 * Is emitted if a context menu is requested for the item \a item,
437 * which is part of \a url. If the item is null, the context menu
438 * for the URL should be shown and the custom actions \a customActions
439 * will be added.
440 */
441 void requestContextMenu(const QPoint& pos,
442 const KFileItem& item,
443 const KUrl& url,
444 const QList<QAction*>& customActions);
445
446 /**
447 * Is emitted if an information message with the content \a msg
448 * should be shown.
449 */
450 void infoMessage(const QString& msg);
451
452 /**
453 * Is emitted if an error message with the content \a msg
454 * should be shown.
455 */
456 void errorMessage(const QString& msg);
457
458 /**
459 * Is emitted if an "operation completed" message with the content \a msg
460 * should be shown.
461 */
462 void operationCompletedMessage(const QString& msg);
463
464 /**
465 * Is emitted after DolphinView::setUrl() has been invoked and
466 * the current directory is loaded. If this signal is emitted,
467 * it is assured that the view contains already the correct root
468 * URL and property settings.
469 */
470 void directoryLoadingStarted();
471
472 /**
473 * Is emitted after the directory triggered by DolphinView::setUrl()
474 * has been loaded.
475 */
476 void directoryLoadingCompleted();
477
478 /**
479 * Is emitted after DolphinView::setUrl() has been invoked and provides
480 * the information how much percent of the current directory have been loaded.
481 */
482 void directoryLoadingProgress(int percent);
483
484 /**
485 * Is emitted if the sorting is done asynchronously and provides the
486 * progress information of the sorting.
487 */
488 void directorySortingProgress(int percent);
489
490 /**
491 * Emitted when the file-item-model emits redirection.
492 * Testcase: fish://localhost
493 */
494 void redirection(const KUrl& oldUrl, const KUrl& newUrl);
495
496 /**
497 * Is emitted when the URL set by DolphinView::setUrl() represents a file.
498 * In this case no signal errorMessage() will be emitted.
499 */
500 void urlIsFileError(const KUrl& url);
501
502 /**
503 * Is emitted when the write state of the folder has been changed. The application
504 * should disable all actions like "Create New..." that depend on the write
505 * state.
506 */
507 void writeStateChanged(bool isFolderWritable);
508
509 /**
510 * Is emitted if the URL should be changed to the previous URL of the
511 * history (e.g. because the "back"-mousebutton has been pressed).
512 */
513 void goBackRequested();
514
515 /**
516 * Is emitted if the URL should be changed to the next URL of the
517 * history (e.g. because the "next"-mousebutton has been pressed).
518 */
519 void goForwardRequested();
520
521 protected:
522 /** Changes the zoom level if Control is pressed during a wheel event. */
523 virtual void wheelEvent(QWheelEvent* event);
524
525 /** @reimp */
526 virtual void hideEvent(QHideEvent* event);
527
528 private slots:
529 /**
530 * Marks the view as active (DolphinView:isActive() will return true)
531 * and emits the 'activated' signal if it is not already active.
532 */
533 void activate();
534
535 void slotItemActivated(int index);
536 void slotItemsActivated(const QSet<int>& indexes);
537 void slotItemMiddleClicked(int index);
538 void slotItemContextMenuRequested(int index, const QPointF& pos);
539 void slotViewContextMenuRequested(const QPointF& pos);
540 void slotHeaderContextMenuRequested(const QPointF& pos);
541 void slotHeaderColumnWidthChanged(const QByteArray& role, qreal current, qreal previous);
542 void slotItemHovered(int index);
543 void slotItemUnhovered(int index);
544 void slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event);
545 void slotModelChanged(KItemModelBase* current, KItemModelBase* previous);
546 void slotMouseButtonPressed(int itemIndex, Qt::MouseButtons buttons);
547
548 /**
549 * Emits the signal \a selectionChanged() with a small delay. This is
550 * because getting all file items for the selection can be an expensive
551 * operation. Fast selection changes are collected in this case and
552 * the signal is emitted only after no selection change has been done
553 * within a small delay.
554 */
555 void slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous);
556
557 /**
558 * Is called by emitDelayedSelectionChangedSignal() and emits the
559 * signal \a selectionChanged() with all selected file items as parameter.
560 */
561 void emitSelectionChangedSignal();
562
563 /**
564 * Updates the view properties of the current URL to the
565 * sorting given by \a role.
566 */
567 void updateSortRole(const QByteArray& role);
568
569 /**
570 * Updates the view properties of the current URL to the
571 * sort order given by \a order.
572 */
573 void updateSortOrder(Qt::SortOrder order);
574
575 /**
576 * Updates the view properties of the current URL to the
577 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
578 */
579 void updateSortFoldersFirst(bool foldersFirst);
580
581 /**
582 * Updates the status bar to show hover information for the
583 * item \a item. If currently other items are selected,
584 * no hover information is shown.
585 * @see DolphinView::clearHoverInformation()
586 */
587 void showHoverInformation(const KFileItem& item);
588
589 /**
590 * Clears the hover information shown in the status bar.
591 * @see DolphinView::showHoverInformation().
592 */
593 void clearHoverInformation();
594
595 /**
596 * Indicates in the status bar that the delete operation
597 * of the job \a job has been finished.
598 */
599 void slotDeleteFileFinished(KJob* job);
600
601 /**
602 * Invoked when the file item model has started the loading
603 * of the directory specified by DolphinView::url().
604 */
605 void slotDirectoryLoadingStarted();
606
607 /**
608 * Invoked when the file item model indicates that the loading of a directory has
609 * been completed. Assures that pasted items and renamed items get seleced.
610 */
611 void slotDirectoryLoadingCompleted();
612
613 /**
614 * Is invoked when items of KFileItemModel have been changed.
615 */
616 void slotItemsChanged();
617
618 /**
619 * Is invoked when the sort order has been changed by the user by clicking
620 * on a header item. The view properties of the directory will get updated.
621 */
622 void slotSortOrderChangedByHeader(Qt::SortOrder current, Qt::SortOrder previous);
623
624 /**
625 * Is invoked when the sort role has been changed by the user by clicking
626 * on a header item. The view properties of the directory will get updated.
627 */
628 void slotSortRoleChangedByHeader(const QByteArray& current, const QByteArray& previous);
629
630 /**
631 * Is invoked when the visible roles have been changed by the user by dragging
632 * a header item. The view properties of the directory will get updated.
633 */
634 void slotVisibleRolesChangedByHeader(const QList<QByteArray>& current,
635 const QList<QByteArray>& previous);
636
637 void slotRoleEditingFinished(int index, const QByteArray& role, const QVariant& value);
638
639 /**
640 * Observes the item with the URL \a url. As soon as the directory
641 * model indicates that the item is available, the item will
642 * get selected and it is assured that the item stays visible.
643 */
644 void observeCreatedItem(const KUrl& url);
645
646 /**
647 * Called when a redirection happens.
648 * Testcase: fish://localhost
649 */
650 void slotDirectoryRedirection(const KUrl& oldUrl, const KUrl& newUrl);
651
652 /**
653 * Applies the state that has been restored by restoreViewState()
654 * to the view.
655 */
656 void updateViewState();
657
658 void hideToolTip();
659
660 /**
661 * Calculates the number of currently shown files into
662 * \a fileCount and the number of folders into \a folderCount.
663 * The size of all files is written into \a totalFileSize.
664 * It is recommend using this method instead of asking the
665 * directory lister or the model directly, as it takes
666 * filtering and hierarchical previews into account.
667 */
668 void calculateItemCount(int& fileCount, int& folderCount, KIO::filesize_t& totalFileSize) const;
669
670 private:
671 void loadDirectory(const KUrl& url, bool reload = false);
672
673 /**
674 * Applies the view properties which are defined by the current URL
675 * to the DolphinView properties.
676 */
677 void applyViewProperties();
678
679 /**
680 * Applies the m_mode property to the corresponding
681 * itemlayout-property of the KItemListView.
682 */
683 void applyModeToView();
684
685 /**
686 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
687 * Pastes the clipboard data into the URL \a url.
688 */
689 void pasteToUrl(const KUrl& url);
690
691 /**
692 * Returns a list of URLs for all selected items. The list is
693 * simplified, so that when the URLs are part of different tree
694 * levels, only the parent is returned.
695 */
696 KUrl::List simplifiedSelectedUrls() const;
697
698 /**
699 * Returns the MIME data for all selected items.
700 */
701 QMimeData* selectionMimeData() const;
702
703 /**
704 * Is invoked after a paste operation or a drag & drop
705 * operation and URLs from \a mimeData as selected.
706 * This allows to select all newly pasted
707 * items in restoreViewState().
708 */
709 void markPastedUrlsAsSelected(const QMimeData* mimeData);
710
711 /**
712 * Updates m_isFolderWritable dependent on whether the folder represented by
713 * the current URL is writable. If the state has changed, the signal
714 * writeableStateChanged() will be emitted.
715 */
716 void updateWritableState();
717
718 /**
719 * @return The current URL if no viewproperties-context is given (see
720 * DolphinView::viewPropertiesContext(), otherwise the context
721 * is returned.
722 */
723 KUrl viewPropertiesUrl() const;
724
725 private:
726 bool m_active;
727 bool m_tabsForFiles;
728 bool m_assureVisibleCurrentIndex;
729 bool m_isFolderWritable;
730 bool m_dragging; // True if a dragging is done. Required to be able to decide whether a
731 // tooltip may be shown when hovering an item.
732
733 KUrl m_url;
734 QString m_viewPropertiesContext;
735 Mode m_mode;
736 QList<QByteArray> m_visibleRoles;
737
738 QVBoxLayout* m_topLayout;
739
740 KFileItemModel* m_model;
741 DolphinItemListView* m_view;
742 KItemListContainer* m_container;
743
744 ToolTipManager* m_toolTipManager;
745
746 QTimer* m_selectionChangedTimer;
747
748 KUrl m_currentItemUrl; // Used for making the view to remember the current URL after F5
749 bool m_scrollToCurrentItem; // Used for marking we need to scroll to current item or not
750 QPoint m_restoredContentsPosition;
751
752 QList<KUrl> m_selectedUrls; // Used for making the view to remember selections after F5
753
754 VersionControlObserver* m_versionControlObserver;
755
756 // For unit tests
757 friend class TestBase;
758 friend class DolphinDetailsViewTest;
759 };
760
761 /// Allow using DolphinView::Mode in QVariant
762 Q_DECLARE_METATYPE(DolphinView::Mode)
763
764 #endif // DOLPHINVIEW_H