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