]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.h
Make sort/descending available in dolphinpart
[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 <konq_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 QModelIndex;
56 class ViewProperties;
57
58 /**
59 * @short Represents a view for the directory content.
60 *
61 * View modes for icons, details and columns are supported. It's
62 * possible to adjust:
63 * - sort order
64 * - sort type
65 * - show hidden files
66 * - show previews
67 *
68 * @see DolphinIconsView
69 * @see DolphinDetailsView
70 * @see DolphinColumnView
71 */
72 class LIBDOLPHINPRIVATE_EXPORT DolphinView : public QWidget
73 {
74 Q_OBJECT
75
76 public:
77 /**
78 * Defines the view mode for a directory. The view mode
79 * can be defined when constructing a DolphinView. The
80 * view mode is automatically updated if the directory itself
81 * defines a view mode (see class ViewProperties for details).
82 */
83 enum Mode
84 {
85 /**
86 * The directory items are shown as icons including an
87 * icon name. */
88 IconsView = 0,
89
90 /**
91 * The icon, the name and at least the size of the directory
92 * items are shown in a table. It is possible to add columns
93 * for date, group and permissions.
94 */
95 DetailsView = 1,
96
97 /**
98 * Each folder is shown in a separate column.
99 */
100 ColumnView = 2,
101 MaxModeEnum = ColumnView
102 };
103
104 /** Defines the sort order for the items of a directory. */
105 enum Sorting
106 {
107 SortByName = 0,
108 SortBySize,
109 SortByDate,
110 SortByPermissions,
111 SortByOwner,
112 SortByGroup,
113 SortByType,
114 SortByRating,
115 SortByTags,
116 MaxSortEnum = SortByTags
117 };
118
119 /**
120 * @param parent Parent widget of the view.
121 * @param url Specifies the content which should be shown.
122 * @param dirLister Used directory lister. The lister is not owned
123 * by the view and won't get deleted.
124 * @param dolphinModel Used directory model. The model is not owned
125 * by the view and won't get deleted.
126 * @param proxyModel Used proxy model which specifies the sorting. The
127 * model is not owned by the view and won't get
128 * deleted.
129 */
130 DolphinView(QWidget* parent,
131 const KUrl& url,
132 KDirLister* dirLister,
133 DolphinModel* dolphinModel,
134 DolphinSortFilterProxyModel* proxyModel);
135
136 virtual ~DolphinView();
137
138 /**
139 * Returns the current active URL, where all actions are applied.
140 * The URL navigator is synchronized with this URL.
141 */
142 const KUrl& url() const;
143
144 /**
145 * Returns the root URL of the view, which is defined as the first
146 * visible path of DolphinView::url(). Usually the root URL is
147 * equal to DolphinView::url(), but in the case of the column view
148 * when 2 columns are shown, the root URL might be:
149 * /home/peter/Documents
150 * and DolphinView::url() might return
151 * /home/peter/Documents/Music/
152 */
153 KUrl rootUrl() const;
154
155 /**
156 * If \a active is true, the view will marked as active. The active
157 * view is defined as view where all actions are applied to.
158 */
159 void setActive(bool active);
160 bool isActive() const;
161
162 /**
163 * Changes the view mode for the current directory to \a mode.
164 * If the view properties should be remembered for each directory
165 * (GeneralSettings::globalViewProps() returns false), then the
166 * changed view mode will be be stored automatically.
167 */
168 void setMode(Mode mode);
169 Mode mode() const;
170
171 /**
172 * Turns on the file preview for the all files of the current directory,
173 * if \a show is true.
174 * If the view properties should be remembered for each directory
175 * (GeneralSettings::globalViewProps() returns false), then the
176 * preview setting will be be stored automatically.
177 */
178 void setShowPreview(bool show);
179 bool showPreview() const;
180
181 /**
182 * Shows all hidden files of the current directory,
183 * if \a show is true.
184 * If the view properties should be remembered for each directory
185 * (GeneralSettings::globalViewProps() returns false), then the
186 * show hidden file setting will be be stored automatically.
187 */
188 void setShowHiddenFiles(bool show);
189 bool showHiddenFiles() const;
190
191 /**
192 * Summarizes all sorted items by their category \a categorized
193 * is true.
194 * If the view properties should be remembered for each directory
195 * (GeneralSettings::globalViewProps() returns false), then the
196 * categorized sorting setting will be be stored automatically.
197 */
198 void setCategorizedSorting(bool categorized);
199 bool categorizedSorting() const;
200
201 /**
202 * Returns true, if the categorized sorting is supported by the current
203 * used mode (see DolphinView::setMode()). Currently only DolphinView::IconsView
204 * supports categorizations. To check whether the categorized
205 * sorting is set, use DolphinView::categorizedSorting().
206 */
207 bool supportsCategorizedSorting() const;
208
209 /**
210 * Selects all items.
211 * @see DolphinView::selectedItems()
212 */
213 void selectAll();
214
215 /**
216 * Inverts the current selection: selected items get unselected,
217 * unselected items get selected.
218 * @see DolphinView::selectedItems()
219 */
220 void invertSelection();
221
222 /** Returns true, if at least one item is selected. */
223 bool hasSelection() const;
224
225 void clearSelection();
226
227 /**
228 * Returns the selected items. The list is empty if no item has been
229 * selected.
230 * @see DolphinView::selectedUrls()
231 */
232 KFileItemList selectedItems() const;
233
234 /**
235 * Returns a list of URLs for all selected items. An empty list
236 * is returned, if no item is selected.
237 * @see DolphinView::selectedItems()
238 */
239 KUrl::List selectedUrls() const;
240
241 /**
242 * Returns the file item for the given model index \a index.
243 */
244 KFileItem fileItem(const QModelIndex& index) const;
245
246 /**
247 * Sets the upper left position of the view content
248 * to (x,y). The content of the view might be larger than the visible area
249 * and hence a scrolling must be done.
250 */
251 void setContentsPosition(int x, int y);
252
253 /** Returns the upper left position of the view content. */
254 QPoint contentsPosition() const;
255
256 /** Increases the size of the current set view mode. */
257 void zoomIn();
258
259 /** Decreases the size of the current set view mode. */
260 void zoomOut();
261
262 /**
263 * Returns true, if zooming in is possible. If false is returned,
264 * the minimal zoom size is possible.
265 */
266 bool isZoomInPossible() const;
267
268 /**
269 * Returns true, if zooming out is possible. If false is returned,
270 * the maximum zoom size is possible.
271 */
272 bool isZoomOutPossible() const;
273
274 /** Sets the sort order of the items inside a directory (see DolphinView::Sorting). */
275 void setSorting(Sorting sorting);
276
277 /** Returns the sort order of the items inside a directory (see DolphinView::Sorting). */
278 Sorting sorting() const;
279
280 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
281 void setSortOrder(Qt::SortOrder order);
282
283 /** Returns the current used sort order (Qt::Ascending or Qt::Descending). */
284 Qt::SortOrder sortOrder() const;
285
286 /** Sets the additional information which should be shown for the items. */
287 void setAdditionalInfo(KFileItemDelegate::InformationList info);
288
289 /** Returns the additional information which should be shown for the items. */
290 KFileItemDelegate::InformationList additionalInfo() const;
291
292 /** Reloads the current directory. */
293 void reload();
294
295 /**
296 * Refreshes the view to get synchronized with the (updated) Dolphin settings.
297 * This method only needs to get invoked if the view settings for the Icons View,
298 * Details View or Columns View have been changed.
299 */
300 void refresh();
301
302 /**
303 * Changes the directory of the view to \a url. If \a rootUrl is empty, the view
304 * properties from \a url are used for adjusting the view mode and the other properties.
305 * If \a rootUrl is not empty, the view properties from the root URL are considered
306 * instead. Specifying a root URL is only required if a view having a different root URL
307 * (e. g. the column view) should be restored. Usually using DolphinView::setUrl()
308 * is enough for changing the current URL.
309 */
310 void updateView(const KUrl& url, const KUrl& rootUrl);
311
312 /**
313 * Filters the currently shown items by \a nameFilter. All items
314 * which contain the given filter string will be shown.
315 */
316 void setNameFilter(const QString& nameFilter);
317
318 /**
319 * Calculates the number of currently shown files into
320 * \a fileCount and the number of folders into \a folderCount.
321 * It is recommend using this method instead of asking the
322 * directory lister or the model directly, as it takes
323 * filtering and hierarchical previews into account.
324 */
325 void calculateItemCount(int& fileCount, int& folderCount);
326
327 /**
328 * Returns the "switch to icons mode" action.
329 * This code is here to share it between the mainwindow and the part
330 */
331 static KToggleAction* iconsModeAction(KActionCollection* collection);
332
333 /**
334 * Returns the "switch to details mode" action.
335 * This code is here to share it between the mainwindow and the part
336 */
337 static KToggleAction* detailsModeAction(KActionCollection* collection);
338
339 /**
340 * Returns the "switch to columns mode" action.
341 * This code is here to share it between the mainwindow and the part
342 */
343 static KToggleAction* columnsModeAction(KActionCollection* collection);
344
345 /**
346 * Creates the rename action.
347 * This code is here to share it between the mainwindow and the part
348 */
349 static KAction* createRenameAction(KActionCollection* collection);
350
351 /**
352 * Creates the "move to trash" action.
353 * This code is here to share it between the mainwindow and the part
354 */
355 static KAction* createMoveToTrashAction(KActionCollection* collection);
356
357 /**
358 * Creates the delete action.
359 * This code is here to share it between the mainwindow and the part
360 */
361 static KAction* createDeleteAction(KActionCollection* collection);
362
363 /**
364 * Creates the "new directory" action.
365 * This code is here to share it between the mainwindow and the part
366 */
367 static KAction* createNewDirAction(KActionCollection* collection);
368
369 /**
370 * Creates the "sort descending" action.
371 * This code is here to share it between the mainwindow and the part
372 */
373 static KAction* createSortDescendingAction(KActionCollection* collection);
374
375 /**
376 * Returns the action name corresponding to the current view mode
377 */
378 QString currentViewModeActionName() const;
379
380 /**
381 * Returns the state of the paste action:
382 * first is whether the action should be enabled
383 * second is the text for the action
384 */
385 QPair<bool, QString> pasteInfo() const;
386
387 public slots:
388 /**
389 * Changes the directory to \a url. If the current directory is equal to
390 * \a url, nothing will be done (use DolphinView::reload() instead).
391 */
392 void setUrl(const KUrl& url);
393
394 /**
395 * Request of a selection change. The view will do its best to accommodate
396 * the request, but it is not guaranteed that all items in \a selection
397 * will actually get selected. The view will e.g. not select items which
398 * are not in the currently displayed folder.
399 */
400 void changeSelection(const KFileItemList& selection);
401
402 /**
403 * Triggers the renaming of the currently selected items, where
404 * the user must input a new name for the items.
405 */
406 void renameSelectedItems();
407
408 /**
409 * Moves all selected items to the trash.
410 */
411 void trashSelectedItems();
412
413 /**
414 * Deletes all selected items.
415 */
416 void deleteSelectedItems();
417
418 /**
419 * Copies all selected items to the clipboard and marks
420 * the items as cutted.
421 */
422 void cutSelectedItems();
423
424 /** Copies all selected items to the clipboard. */
425 void copySelectedItems();
426
427 /** Pastes the clipboard data to this view. */
428 void paste();
429
430 /** Switches between an ascending and descending sorting order. */
431 void toggleSortOrder();
432
433 signals:
434 /**
435 * Is emitted if the view has been activated by e. g. a mouse click.
436 */
437 void activated();
438
439 /** Is emitted if URL of the view has been changed to \a url. */
440 void urlChanged(const KUrl& url);
441
442 /**
443 * Is emitted when clicking on an item
444 */
445 void itemTriggered(const KFileItem& item);
446
447 /**
448 * Is emitted if the view mode (IconsView, DetailsView,
449 * PreviewsView) has been changed.
450 */
451 void modeChanged();
452
453 /** Is emitted if the 'show preview' property has been changed. */
454 void showPreviewChanged();
455
456 /** Is emitted if the 'show hidden files' property has been changed. */
457 void showHiddenFilesChanged();
458
459 /** Is emitted if the 'categorized sorting' property has been changed. */
460 void categorizedSortingChanged();
461
462 /** Is emitted if the sorting by name, size or date has been changed. */
463 void sortingChanged(DolphinView::Sorting sorting);
464
465 /** Is emitted if the sort order (ascending or descending) has been changed. */
466 void sortOrderChanged(Qt::SortOrder order);
467
468 /** Is emitted if the additional information for an item has been changed. */
469 void additionalInfoChanged(const KFileItemDelegate::InformationList& info);
470
471 /**
472 * Is emitted if information of an item is requested to be shown e. g. in the sidebar.
473 * If item is null, no item information request is pending.
474 */
475 void requestItemInfo(const KFileItem& item);
476
477 /** Is emitted if the contents has been moved to \a x, \a y. */
478 void contentsMoved(int x, int y);
479
480 /**
481 * Is emitted whenever the selection has been changed.
482 */
483 void selectionChanged(const KFileItemList& selection);
484
485 /**
486 * Is emitted if a context menu is requested for the item \a item,
487 * which is part of \a url. If the item is 0, the context menu
488 * for the URL should be shown.
489 */
490 void requestContextMenu(const KFileItem& item, const KUrl& url);
491
492 /**
493 * Is emitted if an information message with the content \a msg
494 * should be shown.
495 */
496 void infoMessage(const QString& msg);
497
498 /**
499 * Is emitted if an error message with the content \a msg
500 * should be shown.
501 */
502 void errorMessage(const QString& msg);
503
504 /**
505 * Is emitted if an "operation completed" message with the content \a msg
506 * should be shown.
507 */
508 void operationCompletedMessage(const QString& msg);
509
510 /**
511 * Is emitted after DolphinView::setUrl() has been invoked and
512 * the path \a url is currently loaded. If this signal is emitted,
513 * it is assured that the view contains already the correct root
514 * URL and property settings.
515 */
516 void startedPathLoading(const KUrl& url);
517
518 /**
519 * Is emitted when renaming, copying, moving, linking etc.
520 * Used for feedback in the mainwindow.
521 */
522 void doingOperation(KonqFileUndoManager::CommandType type);
523
524 protected:
525 /** @see QWidget::mouseReleaseEvent */
526 virtual void mouseReleaseEvent(QMouseEvent* 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 /**
536 * If the item \a item is a directory, then this
537 * directory will be loaded. If the item is a file, the corresponding
538 * application will get started.
539 */
540 void triggerItem(const KFileItem& index);
541
542 void emitSelectionChangedSignal();
543
544 /**
545 * Opens the context menu on position \a pos. The position
546 * is used to check whether the context menu is related to an
547 * item or to the viewport.
548 */
549 void openContextMenu(const QPoint& pos);
550
551 /**
552 * Drops the URLs \a urls to the destination path \a destPath. If
553 * the URLs are dropped above an item inside the destination path,
554 * the item is indicated by \a destItem.
555 */
556 void dropUrls(const KUrl::List& urls,
557 const KUrl& destPath,
558 const KFileItem& destItem);
559
560 /**
561 * Handles the dropping of URLs to the given destination.
562 * @see DolphinDropController
563 */
564 void dropUrls(const KUrl::List& urls,
565 const KUrl& destination);
566 /**
567 * Updates the view properties of the current URL to the
568 * sorting given by \a sorting.
569 */
570 void updateSorting(DolphinView::Sorting sorting);
571
572 /**
573 * Updates the view properties of the current URL to the
574 * sort order given by \a order.
575 */
576 void updateSortOrder(Qt::SortOrder order);
577
578 /**
579 * Updates the view properties of the current URL to the
580 * additional information given by \a info.
581 */
582 void updateAdditionalInfo(const KFileItemDelegate::InformationList& info);
583
584 /**
585 * Emits the signal contentsMoved with the current coordinates
586 * of the viewport as parameters.
587 */
588 void emitContentsMoved();
589
590 /**
591 * Updates the status bar to show hover information for the
592 * item \a item. If currently other items are selected,
593 * no hover information is shown.
594 * @see DolphinView::clearHoverInformation()
595 */
596 void showHoverInformation(const KFileItem& item);
597
598 /**
599 * Clears the hover information shown in the status bar.
600 * @see DolphinView::showHoverInformation().
601 */
602 void clearHoverInformation();
603
604 /**
605 * Indicates in the status bar that the delete operation
606 * of the job \a job has been finished.
607 */
608 void slotDeleteFileFinished(KJob* job);
609
610 private:
611 void loadDirectory(const KUrl& url, bool reload = false);
612
613 /**
614 * Returns the URL where the view properties should be stored. Usually
615 * DolphinView::url() is returned, but in the case of a Column View the
616 * view properties are always stored in the directory represented by the
617 * first column. It is recommendend whenever using the ViewProperties class
618 * to use DolphinView::viewPropertiesUrl() as URL.
619 */
620 KUrl viewPropertiesUrl() const;
621
622 /**
623 * Applies the view properties which are defined by the current URL
624 * m_url to the DolphinView properties.
625 */
626 void applyViewProperties(const KUrl& url);
627
628 /**
629 * Creates a new view representing the given view mode (DolphinView::mode()).
630 * The current view will get deleted.
631 */
632 void createView();
633
634 void deleteView();
635
636 /**
637 * Returns a pointer to the currently used item view, which is either
638 * a ListView or a TreeView.
639 */
640 QAbstractItemView* itemView() const;
641
642 /**
643 * Returns true, if the item \a item has been cut into
644 * the clipboard.
645 */
646 bool isCutItem(const KFileItem& item) const;
647
648 /**
649 * Returns true, if the ColumnView is activated. As the column view
650 * requires some special handling for iterating through directories,
651 * this method has been introduced for convenience.
652 */
653 bool isColumnViewActive() const
654 {
655 return m_columnView != 0;
656 }
657
658 private:
659 bool m_active;
660 bool m_showPreview;
661 bool m_loadingDirectory;
662 bool m_storedCategorizedSorting;
663 Mode m_mode;
664
665 DolphinMainWindow* m_mainWindow;
666 QVBoxLayout* m_topLayout;
667
668 DolphinController* m_controller;
669 DolphinIconsView* m_iconsView;
670 DolphinDetailsView* m_detailsView;
671 DolphinColumnView* m_columnView;
672 KFileItemDelegate* m_fileItemDelegate;
673 QItemSelectionModel* m_selectionModel;
674
675 DolphinModel* m_dolphinModel;
676 KDirLister* m_dirLister;
677 DolphinSortFilterProxyModel* m_proxyModel;
678
679 IconManager* m_iconManager;
680
681 KUrl m_rootUrl;
682 };
683
684 /// Allow using DolphinView::Mode in QVariant
685 Q_DECLARE_METATYPE(DolphinView::Mode)
686
687 #endif // DOLPHINVIEW_H