]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.h
assure that the preview job gets killed when the directory has been changed
[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 <kparts/part.h>
26 #include <kfileitem.h>
27 #include <kfileitemdelegate.h>
28 #include <kio/job.h>
29
30 #include <kurlnavigator.h>
31
32 #include <QDropEvent>
33 #include <QLinkedList>
34 #include <QListView>
35 #include <QMouseEvent>
36 #include <QVBoxLayout>
37 #include <QWidget>
38
39 class DolphinController;
40 class FilterBar;
41 class KFileItemDelegate;
42 class KUrl;
43 class KDirModel;
44 class KUrlNavigator;
45 class DolphinColumnView;
46 class DolphinDetailsView;
47 class DolphinDirLister;
48 class DolphinIconsView;
49 class DolphinMainWindow;
50 class DolphinSortFilterProxyModel;
51 class DolphinStatusBar;
52 class QModelIndex;
53 class QPainter;
54 class QTimer;
55 class ViewProperties;
56
57 namespace KIO
58 {
59 class PreviewJob;
60 }
61
62 /**
63 * @short Represents a view for the directory content
64 * including the navigation bar, filter bar and status bar.
65 *
66 * View modes for icons, details and columns are supported. Currently
67 * Dolphin allows to have up to two views inside the main window.
68 *
69 * @see DolphinIconsView
70 * @see DolphinDetailsView
71 * @see DolphinColumnView
72 * @see KUrlNavigator
73 * @see DolphinStatusBar
74 */
75 class DolphinView : public QWidget
76 {
77 Q_OBJECT
78
79 public:
80 /**
81 * Defines the view mode for a directory. The view mode
82 * can be defined when constructing a DolphinView. The
83 * view mode is automatically updated if the directory itself
84 * defines a view mode (see class ViewProperties for details).
85 */
86 enum Mode
87 {
88 /**
89 * The directory items are shown as icons including an
90 * icon name. */
91 IconsView = 0,
92
93 /**
94 * The icon, the name and at least the size of the directory
95 * items are shown in a table. It is possible to add columns
96 * for date, group and permissions.
97 */
98 DetailsView = 1,
99
100 /**
101 * Each folder is shown in a separate column.
102 */
103 ColumnView = 2,
104 MaxModeEnum = ColumnView
105 };
106
107 /** Defines the sort order for the items of a directory. */
108 enum Sorting
109 {
110 SortByName = 0,
111 SortBySize,
112 SortByDate,
113 SortByPermissions,
114 SortByOwner,
115 SortByGroup,
116 MaxSortEnum = SortByGroup
117 };
118
119 DolphinView(DolphinMainWindow* mainwindow,
120 QWidget *parent,
121 const KUrl& url,
122 Mode mode = IconsView,
123 bool showHiddenFiles = false);
124
125 virtual ~DolphinView();
126
127 /**
128 * Sets the current active URL.
129 * The signals KUrlNavigator::urlChanged() and KUrlNavigator::historyChanged()
130 * are emitted.
131 */
132 void setUrl(const KUrl& url);
133
134 /** Returns the current active URL. */
135 const KUrl& url() const;
136
137 /**
138 * Returns true if the view is active and hence all actions are
139 * applied to this view.
140 */
141 bool isActive() const;
142
143 /**
144 * Changes the view mode for the current directory to \a mode.
145 * If the view properties should be remembered for each directory
146 * (GeneralSettings::globalViewProps() returns false), then the
147 * changed view mode will be be stored automatically.
148 */
149 void setMode(Mode mode);
150 Mode mode() const;
151
152 /**
153 * Turns on the file preview for the all files of the current directory,
154 * if \a show is true.
155 * If the view properties should be remembered for each directory
156 * (GeneralSettings::globalViewProps() returns false), then the
157 * preview setting will be be stored automatically.
158 */
159 void setShowPreview(bool show);
160 bool showPreview() const;
161
162 /**
163 * Shows all hidden files of the current directory,
164 * if \a show is true.
165 * If the view properties should be remembered for each directory
166 * (GeneralSettings::globalViewProps() returns false), then the
167 * show hidden file setting will be be stored automatically.
168 */
169 void setShowHiddenFiles(bool show);
170 bool showHiddenFiles() const;
171
172 /**
173 * Summarizes all sorted items by their category \a categorized
174 * is true.
175 * If the view properties should be remembered for each directory
176 * (GeneralSettings::globalViewProps() returns false), then the
177 * categorized sorting setting will be be stored automatically.
178 */
179 void setCategorizedSorting(bool categorized);
180 bool categorizedSorting() const;
181
182 /**
183 * Returns true, if the categorized sorting is supported by the current
184 * used mode (see DolphinView::setMode()). Currently only DolphinView::IconsView
185 * supports categorizations. To check whether the categorized
186 * sorting is set, use DolphinView::categorizedSorting().
187 */
188 bool supportsCategorizedSorting() const;
189
190 /**
191 * Triggers the renaming of the currently selected items, where
192 * the user must input a new name for the items.
193 */
194 void renameSelectedItems();
195
196 /**
197 * Selects all items.
198 * @see DolphinView::selectedItems()
199 */
200 void selectAll();
201
202 /**
203 * Inverts the current selection: selected items get unselected,
204 * unselected items get selected.
205 * @see DolphinView::selectedItems()
206 */
207 void invertSelection();
208
209 /**
210 * Goes back one step in the URL history. The signals
211 * KUrlNavigator::urlChanged() and KUrlNavigator::historyChanged()
212 * are submitted.
213 */
214 void goBack();
215
216 /**
217 * Goes forward one step in the Url history. The signals
218 * KUrlNavigator::urlChanged() and KUrlNavigator::historyChanged()
219 * are submitted.
220 */
221 void goForward();
222
223 /**
224 * Goes up one step of the Url path. The signals
225 * KUrlNavigator::urlChanged() and KUrlNavigator::historyChanged()
226 * are submitted.
227 */
228 void goUp();
229
230 /**
231 * Goes to the home URL. The signals KUrlNavigator::urlChanged()
232 * and KUrlNavigator::historyChanged() are submitted.
233 */
234 void goHome();
235
236 /**
237 * Sets the URL of the navigation bar to an editable state
238 * if \a editable is true. If \a editable is false, each part of
239 * the location is presented by a button for a fast navigation.
240 */
241 void setUrlEditable(bool editable);
242
243 /** Returns true, if at least one item is selected. */
244 bool hasSelection() const;
245
246 void clearSelection();
247
248 /**
249 * Returns the selected items. The list is empty if no item has been
250 * selected.
251 * @see DolphinView::selectedUrls()
252 */
253 KFileItemList selectedItems() const;
254
255 /**
256 * Returns a list of URLs for all selected items. An empty list
257 * is returned, if no item is selected.
258 * @see DolphinView::selectedItems()
259 */
260 KUrl::List selectedUrls() const;
261
262 /**
263 * Returns the file item for the given model index \a index.
264 */
265 KFileItem* fileItem(const QModelIndex index) const;
266
267 /**
268 * Renames the filename of the source URL by the new file name.
269 * If the new file name already exists, a dialog is opened which
270 * asks the user to enter a new name.
271 */
272 void rename(const KUrl& source, const QString& newName);
273
274 DolphinStatusBar* statusBar() const;
275
276 /**
277 * Returns the x-position of the view content.
278 * The content of the view might be larger than the visible area
279 * and hence a scrolling must be done.
280 */
281 int contentsX() const;
282
283 /**
284 * Returns the y-position of the view content.
285 * The content of the view might be larger than the visible area
286 * and hence a scrolling must be done.
287 */
288 int contentsY() const;
289
290 /**
291 * Returns true, if the URL shown by the navigation bar is editable.
292 * @see KUrlNavigator
293 */
294 bool isUrlEditable() const;
295
296 /** Increases the size of the current set view mode. */
297 void zoomIn();
298
299 /** Decreases the size of the current set view mode. */
300 void zoomOut();
301
302 /**
303 * Returns true, if zooming in is possible. If false is returned,
304 * the minimal zoom size is possible.
305 */
306 bool isZoomInPossible() const;
307
308 /**
309 * Returns true, if zooming out is possible. If false is returned,
310 * the maximum zoom size is possible.
311 */
312 bool isZoomOutPossible() const;
313
314 /** Sets the sort order of the items inside a directory (see DolphinView::Sorting). */
315 void setSorting(Sorting sorting);
316
317 /** Returns the sort order of the items inside a directory (see DolphinView::Sorting). */
318 Sorting sorting() const;
319
320 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
321 void setSortOrder(Qt::SortOrder order);
322
323 /** Returns the current used sort order (Qt::Ascending or Qt::Descending). */
324 Qt::SortOrder sortOrder() const;
325
326 /** Sets the additional information which should be shown for the items. */
327 void setAdditionalInfo(KFileItemDelegate::AdditionalInformation info);
328
329 /** Returns the additional information which should be shown for the items. */
330 KFileItemDelegate::AdditionalInformation additionalInfo() const;
331
332 /** Returns the KUrlNavigator of the view for read access. */
333 const KUrlNavigator* urlNavigator() const
334 {
335 return m_urlNavigator;
336 }
337
338 /**
339 * Triggers to request user information for the item given
340 * by the URL \a url. The signal requestItemInfo is emitted,
341 * which provides a way for widgets to get an indication to update
342 * the item information.
343 */
344 void emitRequestItemInfo(const KUrl& url);
345
346 /** Returns true, if the filter bar is visible. */
347 bool isFilterBarVisible() const;
348
349 /**
350 * Return the DolphinMainWindow this View belongs to. It is guranteed
351 * that we have one.
352 */
353 DolphinMainWindow* mainWindow() const ;
354
355 /** Reloads the current directory. */
356 void reload();
357
358 public slots:
359 /**
360 * Popups the filter bar above the status bar if \a show is true.
361 */
362 void showFilterBar(bool show);
363
364 /**
365 * Updates the number of items (= number of files + number of
366 * directories) in the statusbar. If files are selected, the number
367 * of selected files and the sum of the filesize is shown.
368 */
369 void updateStatusBar();
370
371 /**
372 * Requests the main window to set this view as active view, which
373 * means that all actions are applied to this view.
374 */
375 void requestActivation();
376
377 /**
378 * Request of a selection change. The view will do its best to accomodate
379 * the request, but it is not guaranteed that all items in \a selection
380 * will actually get selected. The view will e.g. not select items which
381 * are not in the currently displayed folder.
382 */
383 void changeSelection(const KFileItemList& selection);
384
385 signals:
386 /** Is emitted if URL of the view has been changed to \a url. */
387 void urlChanged(const KUrl& url);
388
389 /**
390 * Is emitted if the view mode (IconsView, DetailsView,
391 * PreviewsView) has been changed.
392 */
393 void modeChanged();
394
395 /** Is emitted if the 'show preview' property has been changed. */
396 void showPreviewChanged();
397
398 /** Is emitted if the 'show hidden files' property has been changed. */
399 void showHiddenFilesChanged();
400
401 /** Is emitted if the 'categorized sorting' property has been changed. */
402 void categorizedSortingChanged();
403
404 /** Is emitted if the sorting by name, size or date has been changed. */
405 void sortingChanged(DolphinView::Sorting sorting);
406
407 /** Is emitted if the sort order (ascending or descending) has been changed. */
408 void sortOrderChanged(Qt::SortOrder order);
409
410 /** Is emitted if the addtional information for an item has been changed. */
411 void additionalInfoChanged(KFileItemDelegate::AdditionalInformation info);
412
413 /**
414 * Is emitted if information of an item is requested to be shown e. g. in the sidebar.
415 * It the URL is empty, no item information request is pending.
416 */
417 void requestItemInfo(const KUrl& url);
418
419 /** Is emitted if the contents has been moved to \a x, \a y. */
420 void contentsMoved(int x, int y);
421
422 /**
423 * Is emitted whenever the selection has been changed.
424 */
425 void selectionChanged(const KFileItemList& selection);
426
427 /**
428 * Is emitted whenever the filter bar has been turned show or hidden.
429 */
430 void showFilterBarChanged(bool shown);
431
432 protected:
433 /** @see QWidget::mouseReleaseEvent */
434 virtual void mouseReleaseEvent(QMouseEvent* event);
435
436 private slots:
437 void changeDirectory(const KUrl& url);
438 void triggerItem(const QModelIndex& index);
439 void updateProgress(int percent);
440
441 /**
442 * Updates the number of items (= number of directories + number of files)
443 * and shows this information in the statusbar.
444 */
445 void updateItemCount();
446
447 /**
448 * Generates a preview image for each file item in \a items.
449 * The current preview settings (maximum size, 'Show Preview' menu)
450 * are respected.
451 */
452 void generatePreviews(const KFileItemList& items);
453
454 /**
455 * Replaces the icon of the item \a item by the preview pixmap
456 * \a pixmap.
457 */
458 void showPreview(const KFileItem* item, const QPixmap& pixmap);
459
460 /**
461 * Restores the x- and y-position of the contents if the
462 * current view is part of the history.
463 */
464 void restoreContentsPos();
465
466 /** Shows the information \a msg inside the statusbar. */
467 void showInfoMessage(const QString& msg);
468
469 /** Shows the error message \a msg inside the statusbar. */
470 void showErrorMessage(const QString& msg);
471
472 void emitSelectionChangedSignal();
473 void closeFilterBar();
474
475 /**
476 * Filters the currently shown items by \a nameFilter. All items
477 * which contain the given filter string will be shown.
478 */
479 void changeNameFilter(const QString& nameFilter);
480
481 /**
482 * Opens the context menu on position \a pos. The position
483 * is used to check whether the context menu is related to an
484 * item or to the viewport.
485 */
486 void openContextMenu(const QPoint& pos);
487
488 /**
489 * Drops the URLs \a urls to the index \a index. \a source
490 * indicates the widget where the dragging has been started from.
491 */
492 void dropUrls(const KUrl::List& urls,
493 const QModelIndex& index,
494 QWidget* source);
495
496 /**
497 * Drops the URLs \a urls at the
498 * destination \a destination.
499 */
500 void dropUrls(const KUrl::List& urls,
501 const KUrl& destination);
502 /**
503 * Updates the view properties of the current URL to the
504 * sorting given by \a sorting.
505 */
506 void updateSorting(DolphinView::Sorting sorting);
507
508 /**
509 * Updates the view properties of the current URL to the
510 * sort order given by \a order.
511 */
512 void updateSortOrder(Qt::SortOrder order);
513
514 /**
515 * Emits the signal contentsMoved with the current coordinates
516 * of the viewport as parameters.
517 */
518 void emitContentsMoved();
519
520 /**
521 * Updates the activation state of the view by checking whether
522 * the currently active view is this view.
523 */
524 void updateActivationState();
525
526 /** Applies an item effect to all cut items of the clipboard. */
527 void updateCutItems();
528
529 /** Is invoked when the preview job has been finished. */
530 void slotPreviewResult(KJob* job);
531
532 private:
533 void startDirLister(const KUrl& url, bool reload = false);
534
535 /**
536 * Returns the default text of the status bar, if no item is
537 * selected.
538 */
539 QString defaultStatusBarText() const;
540
541 /**
542 * Returns the text for the status bar, if at least one item
543 * is selected.
544 */
545 QString selectionStatusBarText() const;
546
547 /**
548 * Creates a new view representing the given view mode (DolphinView::mode()).
549 * The current view will get deleted.
550 */
551 void createView();
552
553 /**
554 * Selects all items by using the selection flags \a flags. This is a helper
555 * method for the slots DolphinView::selectAll() and DolphinView::invertSelection().
556 */
557 void selectAll(QItemSelectionModel::SelectionFlags flags);
558
559 /**
560 * Returns a pointer to the currently used item view, which is either
561 * a ListView or a TreeView.
562 */
563 QAbstractItemView* itemView() const;
564
565 /**
566 * Returns true if the index is valid and represents
567 * the column KDirModel::Name.
568 */
569 bool isValidNameIndex(const QModelIndex& index) const;
570
571 /**
572 * Returns true, if the item \a item has been cut into
573 * the clipboard.
574 */
575 bool isCutItem(const KFileItem& item) const;
576
577 /** Applies an item effect to all cut items. */
578 void applyCutItemEffect();
579
580 /**
581 * Returns true, if the ColumnView is activated. As the column view
582 * requires some special handling for iterating through directories,
583 * this method has been introduced for convenience.
584 */
585 bool isColumnViewActive() const
586 {
587 return m_columnView != 0;
588 }
589
590 private:
591 /**
592 * Remembers the original pixmap for an item before
593 * the cut effect is applied.
594 */
595 struct CutItem
596 {
597 KUrl url;
598 QPixmap pixmap;
599 };
600
601 bool m_showProgress;
602 bool m_blockContentsMovedSignal;
603 Mode m_mode;
604
605 int m_iconSize;
606 int m_folderCount;
607 int m_fileCount;
608
609 DolphinMainWindow* m_mainWindow;
610 QVBoxLayout* m_topLayout;
611 KUrlNavigator* m_urlNavigator;
612
613 DolphinController* m_controller;
614 DolphinIconsView* m_iconsView;
615 DolphinDetailsView* m_detailsView;
616 DolphinColumnView* m_columnView;
617 KFileItemDelegate* m_fileItemDelegate;
618
619 FilterBar* m_filterBar;
620 DolphinStatusBar* m_statusBar;
621
622 KDirModel* m_dirModel;
623 DolphinDirLister* m_dirLister;
624 DolphinSortFilterProxyModel* m_proxyModel;
625
626 KIO::PreviewJob* m_previewJob;
627
628 QList<CutItem> m_cutItemsCache;
629 };
630
631 #endif // _DOLPHINVIEW_H_