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