]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.h
minor adjustment to prevent code duplication
[dolphin.git] / src / dolphinview.h
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21
22 #ifndef DOLPHINVIEW_H
23 #define DOLPHINVIEW_H
24
25 #include <config-nepomuk.h>
26
27 #include "libdolphin_export.h"
28
29 #include <kparts/part.h>
30 #include <kfileitem.h>
31 #include <kfileitemdelegate.h>
32 #include <kio/fileundomanager.h>
33 #include <kio/job.h>
34
35 #include <QBoxLayout>
36 #include <QKeyEvent>
37 #include <QLinkedList>
38 #include <QListView>
39 #include <QWidget>
40
41 class DolphinController;
42 class DolphinColumnView;
43 class DolphinDetailsView;
44 class DolphinIconsView;
45 class DolphinMainWindow;
46 class DolphinModel;
47 class DolphinSortFilterProxyModel;
48 class IconManager;
49 class KAction;
50 class KActionCollection;
51 class KDirLister;
52 class KFileItemDelegate;
53 class KUrl;
54 class KToggleAction;
55 class ToolTipManager;
56 class QModelIndex;
57 class ViewProperties;
58
59 /**
60 * @short Represents a view for the directory content.
61 *
62 * View modes for icons, details and columns are supported. It's
63 * possible to adjust:
64 * - sort order
65 * - sort type
66 * - show hidden files
67 * - show previews
68 *
69 * @see DolphinIconsView
70 * @see DolphinDetailsView
71 * @see DolphinColumnView
72 */
73 class LIBDOLPHINPRIVATE_EXPORT DolphinView : public QWidget
74 {
75 Q_OBJECT
76
77 public:
78 /**
79 * Defines the view mode for a directory. The view mode
80 * can be defined when constructing a DolphinView. The
81 * view mode is automatically updated if the directory itself
82 * defines a view mode (see class ViewProperties for details).
83 */
84 enum Mode
85 {
86 /**
87 * The directory items are shown as icons including an
88 * icon name. */
89 IconsView = 0,
90
91 /**
92 * The icon, the name and at least the size of the directory
93 * items are shown in a table. It is possible to add columns
94 * for date, group and permissions.
95 */
96 DetailsView = 1,
97
98 /**
99 * Each folder is shown in a separate column.
100 */
101 ColumnView = 2,
102 MaxModeEnum = ColumnView
103 };
104
105 /** Defines the sort order for the items of a directory. */
106 enum Sorting
107 {
108 SortByName = 0,
109 SortBySize,
110 SortByDate,
111 SortByPermissions,
112 SortByOwner,
113 SortByGroup,
114 SortByType,
115 SortByRating,
116 SortByTags,
117 MaxSortEnum = SortByTags
118 };
119
120 /**
121 * @param parent Parent widget of the view.
122 * @param url Specifies the content which should be shown.
123 * @param dirLister Used directory lister. The lister is not owned
124 * by the view and won't get deleted.
125 * @param dolphinModel Used directory model. The model is not owned
126 * by the view and won't get deleted.
127 * @param proxyModel Used proxy model which specifies the sorting. The
128 * model is not owned by the view and won't get
129 * deleted.
130 */
131 DolphinView(QWidget* parent,
132 const KUrl& url,
133 KDirLister* dirLister,
134 DolphinModel* dolphinModel,
135 DolphinSortFilterProxyModel* proxyModel);
136
137 virtual ~DolphinView();
138
139 /**
140 * Returns the current active URL, where all actions are applied.
141 * The URL navigator is synchronized with this URL.
142 */
143 const KUrl& url() const;
144
145 /**
146 * Returns the root URL of the view, which is defined as the first
147 * visible path of DolphinView::url(). Usually the root URL is
148 * equal to DolphinView::url(), but in the case of the column view
149 * when 2 columns are shown, the root URL might be:
150 * /home/peter/Documents
151 * and DolphinView::url() might return
152 * /home/peter/Documents/Music/
153 */
154 KUrl rootUrl() const;
155
156 /**
157 * If \a active is true, the view will marked as active. The active
158 * view is defined as view where all actions are applied to.
159 */
160 void setActive(bool active);
161 bool isActive() const;
162
163 /**
164 * Changes the view mode for the current directory to \a mode.
165 * If the view properties should be remembered for each directory
166 * (GeneralSettings::globalViewProps() returns false), then the
167 * changed view mode will be stored automatically.
168 */
169 void setMode(Mode mode);
170 Mode mode() const;
171
172 /** See setShowPreview */
173 bool showPreview() const;
174
175 /** See setShowHiddenFiles */
176 bool showHiddenFiles() const;
177
178 /** See setCategorizedSorting */
179 bool categorizedSorting() const;
180
181 /**
182 * Returns true, if the categorized sorting is supported by the current
183 * used mode (see DolphinView::setMode()). Currently only DolphinView::IconsView
184 * supports categorizations. To check whether the categorized
185 * sorting is set, use DolphinView::categorizedSorting().
186 */
187 bool supportsCategorizedSorting() const;
188
189 /**
190 * Selects all items.
191 * @see DolphinView::selectedItems()
192 */
193 void selectAll();
194
195 /**
196 * Inverts the current selection: selected items get unselected,
197 * unselected items get selected.
198 * @see DolphinView::selectedItems()
199 */
200 void invertSelection();
201
202 /** Returns true, if at least one item is selected. */
203 bool hasSelection() const;
204
205 void clearSelection();
206
207 /**
208 * Returns the selected items. The list is empty if no item has been
209 * selected.
210 * @see DolphinView::selectedUrls()
211 */
212 KFileItemList selectedItems() const;
213
214 /**
215 * Returns a list of URLs for all selected items. An empty list
216 * is returned, if no item is selected.
217 * @see DolphinView::selectedItems()
218 */
219 KUrl::List selectedUrls() const;
220
221 /**
222 * Returns the number of selected items (this is faster than
223 * invoking selectedItems().count()).
224 */
225 int selectedItemsCount() const;
226
227 /**
228 * Sets the upper left position of the view content
229 * to (x,y). The content of the view might be larger than the visible area
230 * and hence a scrolling must be done.
231 */
232 void setContentsPosition(int x, int y);
233
234 /** Returns the upper left position of the view content. */
235 QPoint contentsPosition() const;
236
237 /**
238 * Sets the zoom level to \a level. It is assured that the used
239 * level is adjusted to be inside the range DolphinView::zoomLevelMinimum() and
240 * DolphinView::zoomLevelMaximum().
241 */
242 void setZoomLevel(int level);
243 int zoomLevel() const;
244
245 int zoomLevelMinimum() const;
246 int zoomLevelMaximum() const;
247
248 /**
249 * Returns true, if zooming in is possible. If false is returned,
250 * the minimal zoom size is possible.
251 */
252 bool isZoomInPossible() const;
253
254 /**
255 * Returns true, if zooming out is possible. If false is returned,
256 * the maximum zoom size is possible.
257 */
258 bool isZoomOutPossible() const;
259
260 /** Sets the sort order of the items inside a directory (see DolphinView::Sorting). */
261 void setSorting(Sorting sorting);
262
263 /** Returns the sort order of the items inside a directory (see DolphinView::Sorting). */
264 Sorting sorting() const;
265
266 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
267 void setSortOrder(Qt::SortOrder order);
268
269 /** Returns the current used sort order (Qt::Ascending or Qt::Descending). */
270 Qt::SortOrder sortOrder() const;
271
272 /** Sets the additional information which should be shown for the items. */
273 void setAdditionalInfo(KFileItemDelegate::InformationList info);
274
275 /** Returns the additional information which should be shown for the items. */
276 KFileItemDelegate::InformationList additionalInfo() const;
277
278 /** Reloads the current directory. */
279 void reload();
280
281 /**
282 * Refreshes the view to get synchronized with the (updated) Dolphin settings.
283 * This method only needs to get invoked if the view settings for the Icons View,
284 * Details View or Columns View have been changed.
285 */
286 void refresh();
287
288 /**
289 * Changes the directory of the view to \a url. If \a rootUrl is empty, the view
290 * properties from \a url are used for adjusting the view mode and the other properties.
291 * If \a rootUrl is not empty, the view properties from the root URL are considered
292 * instead. Specifying a root URL is only required if a view having a different root URL
293 * (e. g. the column view) should be restored. Usually using DolphinView::setUrl()
294 * is enough for changing the current URL.
295 */
296 void updateView(const KUrl& url, const KUrl& rootUrl);
297
298 /**
299 * Filters the currently shown items by \a nameFilter. All items
300 * which contain the given filter string will be shown.
301 */
302 void setNameFilter(const QString& nameFilter);
303
304 /**
305 * Calculates the number of currently shown files into
306 * \a fileCount and the number of folders into \a folderCount.
307 * It is recommend using this method instead of asking the
308 * directory lister or the model directly, as it takes
309 * filtering and hierarchical previews into account.
310 */
311 void calculateItemCount(int& fileCount, int& folderCount);
312
313 /**
314 * Updates the state of the 'Additional Information' actions in \a collection.
315 */
316 void updateAdditionalInfoActions(KActionCollection* collection);
317
318 /**
319 * Returns the state of the paste action:
320 * first is whether the action should be enabled
321 * second is the text for the action
322 */
323 QPair<bool, QString> pasteInfo() const;
324
325 /**
326 * If \a tabsForFiles is true, the signal tabRequested() will also
327 * emitted also for files. Per default tabs for files is disabled
328 * and hence the signal tabRequested() will only be emitted for
329 * directories.
330 */
331 void setTabsForFilesEnabled(bool tabsForFiles);
332 bool isTabsForFilesEnabled() const;
333
334 public slots:
335 /**
336 * Changes the directory to \a url. If the current directory is equal to
337 * \a url, nothing will be done (use DolphinView::reload() instead).
338 */
339 void setUrl(const KUrl& url);
340
341 /**
342 * Request of a selection change. The view will do its best to accommodate
343 * the request, but it is not guaranteed that all items in \a selection
344 * will actually get selected. The view will e.g. not select items which
345 * are not in the currently displayed folder.
346 */
347 void changeSelection(const KFileItemList& selection);
348
349 /**
350 * Triggers the renaming of the currently selected items, where
351 * the user must input a new name for the items.
352 */
353 void renameSelectedItems();
354
355 /**
356 * Moves all selected items to the trash.
357 */
358 void trashSelectedItems();
359
360 /**
361 * Deletes all selected items.
362 */
363 void deleteSelectedItems();
364
365 /**
366 * Copies all selected items to the clipboard and marks
367 * the items as cutted.
368 */
369 void cutSelectedItems();
370
371 /** Copies all selected items to the clipboard. */
372 void copySelectedItems();
373
374 /** Pastes the clipboard data to this view. */
375 void paste();
376
377 /**
378 * Pastes the clipboard data into the currently selected
379 * folder. If the current selection is not exactly one folder, no
380 * paste operation is done.
381 */
382 void pasteIntoFolder();
383
384 /**
385 * Turns on the file preview for the all files of the current directory,
386 * if \a show is true.
387 * If the view properties should be remembered for each directory
388 * (GeneralSettings::globalViewProps() returns false), then the
389 * preview setting will be stored automatically.
390 */
391 void setShowPreview(bool show);
392
393 /**
394 * Shows all hidden files of the current directory,
395 * if \a show is true.
396 * If the view properties should be remembered for each directory
397 * (GeneralSettings::globalViewProps() returns false), then the
398 * show hidden file setting will be stored automatically.
399 */
400 void setShowHiddenFiles(bool show);
401
402 /**
403 * Summarizes all sorted items by their category \a categorized
404 * is true.
405 * If the view properties should be remembered for each directory
406 * (GeneralSettings::globalViewProps() returns false), then the
407 * categorized sorting setting will be stored automatically.
408 */
409 void setCategorizedSorting(bool categorized);
410
411 /** Switches between an ascending and descending sorting order. */
412 void toggleSortOrder();
413
414 /**
415 * Switches on or off the displaying of additional information
416 * as specified by \a action.
417 */
418 void toggleAdditionalInfo(QAction* action);
419
420 signals:
421 /**
422 * Is emitted if the view has been activated by e. g. a mouse click.
423 */
424 void activated();
425
426 /** Is emitted if URL of the view has been changed to \a url. */
427 void urlChanged(const KUrl& url);
428
429 /**
430 * Is emitted if the view requests a changing of the current
431 * URL to \a url (see DolphinController::triggerUrlChangeRequest()).
432 */
433 void requestUrlChange(const KUrl& url);
434
435 /**
436 * Is emitted when clicking on an item with the left mouse button.
437 */
438 void itemTriggered(const KFileItem& item);
439
440 /**
441 * Is emitted if a new tab should be opened for the URL \a url.
442 */
443 void tabRequested(const KUrl& url);
444
445 /**
446 * Is emitted if the view mode (IconsView, DetailsView,
447 * PreviewsView) has been changed.
448 */
449 void modeChanged();
450
451 /** Is emitted if the 'show preview' property has been changed. */
452 void showPreviewChanged();
453
454 /** Is emitted if the 'show hidden files' property has been changed. */
455 void showHiddenFilesChanged();
456
457 /** Is emitted if the 'categorized sorting' property has been changed. */
458 void categorizedSortingChanged();
459
460 /** Is emitted if the sorting by name, size or date has been changed. */
461 void sortingChanged(DolphinView::Sorting sorting);
462
463 /** Is emitted if the sort order (ascending or descending) has been changed. */
464 void sortOrderChanged(Qt::SortOrder order);
465
466 /** Is emitted if the additional information shown for this view has been changed. */
467 void additionalInfoChanged();
468
469 /** Is emitted if the zoom level has been changed by zooming in or out. */
470 void zoomLevelChanged(int level);
471
472 /**
473 * Is emitted if information of an item is requested to be shown e. g. in the sidebar.
474 * If item is null, no item information request is pending.
475 */
476 void requestItemInfo(const KFileItem& item);
477
478 /** Is emitted if the contents has been moved to \a x, \a y. */
479 void contentsMoved(int x, int y);
480
481 /**
482 * Is emitted whenever the selection has been changed.
483 */
484 void selectionChanged(const KFileItemList& selection);
485
486 /**
487 * Is emitted if a context menu is requested for the item \a item,
488 * which is part of \a url. If the item is 0, the context menu
489 * for the URL should be shown.
490 */
491 void requestContextMenu(const KFileItem& item, const KUrl& url);
492
493 /**
494 * Is emitted if an information message with the content \a msg
495 * should be shown.
496 */
497 void infoMessage(const QString& msg);
498
499 /**
500 * Is emitted if an error message with the content \a msg
501 * should be shown.
502 */
503 void errorMessage(const QString& msg);
504
505 /**
506 * Is emitted if an "operation completed" message with the content \a msg
507 * should be shown.
508 */
509 void operationCompletedMessage(const QString& msg);
510
511 /**
512 * Is emitted after DolphinView::setUrl() has been invoked and
513 * the path \a url is currently loaded. If this signal is emitted,
514 * it is assured that the view contains already the correct root
515 * URL and property settings.
516 */
517 void startedPathLoading(const KUrl& url);
518
519 /**
520 * Is emitted when renaming, copying, moving, linking etc.
521 * Used for feedback in the mainwindow.
522 */
523 void doingOperation(KIO::FileUndoManager::CommandType type);
524
525 protected:
526 /** @see QWidget::mouseReleaseEvent */
527 virtual void mouseReleaseEvent(QMouseEvent* event);
528 virtual void wheelEvent(QWheelEvent* event);
529 virtual bool eventFilter(QObject* watched, QEvent* event);
530
531 private slots:
532 /**
533 * Marks the view as active (DolphinView:isActive() will return true)
534 * and emits the 'activated' signal if it is not already active.
535 */
536 void activate();
537
538 /**
539 * If the item \a item is a directory, then this
540 * directory will be loaded. If the item is a file, the corresponding
541 * application will get started.
542 */
543 void triggerItem(const KFileItem& index);
544
545 void emitSelectionChangedSignal();
546
547 /**
548 * Opens the context menu on position \a pos. The position
549 * is used to check whether the context menu is related to an
550 * item or to the viewport.
551 */
552 void openContextMenu(const QPoint& pos);
553
554 /**
555 * Drops the URLs \a urls to the destination path \a destPath. If
556 * the URLs are dropped above an item inside the destination path,
557 * the item is indicated by \a destItem.
558 */
559 void dropUrls(const KUrl::List& urls,
560 const KUrl& destPath,
561 const KFileItem& destItem);
562
563 /**
564 * Updates the view properties of the current URL to the
565 * sorting given by \a sorting.
566 */
567 void updateSorting(DolphinView::Sorting sorting);
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 * additional information given by \a info.
578 */
579 void updateAdditionalInfo(const KFileItemDelegate::InformationList& info);
580
581 /**
582 * Emits the signal contentsMoved with the current coordinates
583 * of the viewport as parameters.
584 */
585 void emitContentsMoved();
586
587 /**
588 * Updates the status bar to show hover information for the
589 * item \a item. If currently other items are selected,
590 * no hover information is shown.
591 * @see DolphinView::clearHoverInformation()
592 */
593 void showHoverInformation(const KFileItem& item);
594
595 /**
596 * Clears the hover information shown in the status bar.
597 * @see DolphinView::showHoverInformation().
598 */
599 void clearHoverInformation();
600
601 /**
602 * Indicates in the status bar that the delete operation
603 * of the job \a job has been finished.
604 */
605 void slotDeleteFileFinished(KJob* job);
606
607 /**
608 * Called when KDirLister emits redirection.
609 * Testcase: fish://localhost
610 */
611 void slotRedirection(const KUrl& oldUrl, const KUrl& newUrl);
612
613 /**
614 * Is emitted if the controller requests a changing of the current
615 * URL to \a url
616 */
617 void slotRequestUrlChange(const KUrl& url);
618
619 /**
620 * Restores the current item (= item that has the keyboard focus)
621 * to m_currentItemUrl.
622 */
623 void restoreCurrentItem();
624
625 private:
626 void loadDirectory(const KUrl& url, bool reload = false);
627
628 /**
629 * Returns the URL where the view properties should be stored. Usually
630 * DolphinView::url() is returned, but in the case of a Column View the
631 * view properties are always stored in the directory represented by the
632 * first column. It is recommendend whenever using the ViewProperties class
633 * to use DolphinView::viewPropertiesUrl() as URL.
634 */
635 KUrl viewPropertiesUrl() const;
636
637 /**
638 * Applies the view properties which are defined by the current URL
639 * m_url to the DolphinView properties.
640 */
641 void applyViewProperties(const KUrl& url);
642
643 /**
644 * Creates a new view representing the given view mode (DolphinView::mode()).
645 * The current view will get deleted.
646 */
647 void createView();
648
649 void deleteView();
650
651 /**
652 * Returns a pointer to the currently used item view, which is either
653 * a ListView or a TreeView.
654 */
655 QAbstractItemView* itemView() const;
656
657 /**
658 * Returns true, if the item \a item has been cut into
659 * the clipboard.
660 */
661 bool isCutItem(const KFileItem& item) const;
662
663 /**
664 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
665 * Pastes the clipboard data into the URL \a url.
666 */
667 void pasteToUrl(const KUrl& url);
668
669 /**
670 * Checks whether the current item view has the same zoom level
671 * as \a oldZoomLevel. If this is not the case, the zoom level
672 * of the controller is updated and a zoomLevelChanged() signal
673 * is emitted.
674 */
675 void updateZoomLevel(int oldZoomLevel);
676
677 /**
678 * Returns a list of URLs for all selected items. The list is
679 * simplified, so that when the URLs are part of different tree
680 * levels, only the parent is returned.
681 */
682 KUrl::List simplifiedSelectedUrls() const;
683
684 /**
685 * Returns true, if the ColumnView is activated. As the column view
686 * requires some special handling for iterating through directories,
687 * this method has been introduced for convenience.
688 */
689 bool isColumnViewActive() const;
690
691 private:
692 bool m_active : 1;
693 bool m_showPreview : 1;
694 bool m_loadingDirectory : 1;
695 bool m_storedCategorizedSorting : 1;
696 bool m_tabsForFiles : 1;
697 bool m_isContextMenuOpen : 1; // TODO: workaround for Qt-issue 207192
698
699 Mode m_mode;
700
701 DolphinMainWindow* m_mainWindow;
702 QVBoxLayout* m_topLayout;
703
704 DolphinController* m_controller;
705 DolphinIconsView* m_iconsView;
706 DolphinDetailsView* m_detailsView;
707 DolphinColumnView* m_columnView;
708 KFileItemDelegate* m_fileItemDelegate;
709 QItemSelectionModel* m_selectionModel;
710
711 DolphinModel* m_dolphinModel;
712 KDirLister* m_dirLister;
713 DolphinSortFilterProxyModel* m_proxyModel;
714
715 IconManager* m_iconManager;
716 ToolTipManager* m_toolTipManager;
717
718 KUrl m_rootUrl;
719 KUrl m_currentItemUrl;
720 };
721
722 inline bool DolphinView::isColumnViewActive() const
723 {
724 return m_columnView != 0;
725 }
726
727 /// Allow using DolphinView::Mode in QVariant
728 Q_DECLARE_METATYPE(DolphinView::Mode)
729
730 #endif // DOLPHINVIEW_H