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