home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kfileview.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  13.1 KB  |  445 lines

  1. // -*- c++ -*-
  2. /* This file is part of the KDE libraries
  3.     Copyright (C) 1997 Stephan Kulow <coolo@kde.org>
  4.     Copyright (C) 2001 Carsten Pfeiffer <pfeiffer@kde.org>
  5.  
  6.     This library is free software; you can redistribute it and/or
  7.     modify it under the terms of the GNU Library General Public
  8.     License as published by the Free Software Foundation; either
  9.     version 2 of the License, or (at your option) any later version.
  10.  
  11.     This library is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.     Library General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU Library General Public License
  17.     along with this library; see the file COPYING.LIB.  If not, write to
  18.     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19.     Boston, MA 02110-1301, USA.
  20. */
  21.  
  22. #ifndef KFILEVIEW_H
  23. #define KFILEVIEW_H
  24.  
  25. class QPoint;
  26. class KActionCollection;
  27.  
  28. #include <qwidget.h>
  29.  
  30. #include "kfileitem.h"
  31. #include "kfile.h"
  32.  
  33. /**
  34.  * internal class to make easier to use signals possible
  35.  * @internal
  36.  **/
  37. class KIO_EXPORT KFileViewSignaler : public QObject
  38. {
  39.     Q_OBJECT
  40.  
  41. public:
  42.     /**
  43.       * Call this method when an item is selected (depends on single click /
  44.       * double click configuration). Emits the appropriate signal.
  45.       **/
  46.     void activate( const KFileItem *item ) {
  47.         if ( item->isDir() )
  48.             emit dirActivated( item );
  49.         else
  50.             emit fileSelected( item );
  51.     }
  52.     /**
  53.      * emits the highlighted signal for item. Call this in your view class
  54.      * whenever the selection changes.
  55.      */
  56.     void highlightFile(const KFileItem *i) { emit fileHighlighted(i); }
  57.  
  58.     void activateMenu( const KFileItem *i, const QPoint& pos ) {
  59.         emit activatedMenu( i, pos );
  60.     }
  61.  
  62.     void changeSorting( QDir::SortSpec sorting ) {
  63.         emit sortingChanged( sorting );
  64.     }
  65.     
  66.     void dropURLs(const KFileItem *i, QDropEvent*e, const KURL::List&urls) {
  67.         emit dropped(i, e, urls);
  68.     }
  69.  
  70. signals:
  71.     void dirActivated(const KFileItem*);
  72.  
  73.     void sortingChanged( QDir::SortSpec );
  74.  
  75.     /**
  76.      * the item maybe be 0L, indicating that we're in multiselection mode and
  77.      * the selection has changed.
  78.      */
  79.     void fileHighlighted(const KFileItem*);
  80.     void fileSelected(const KFileItem*);
  81.     void activatedMenu( const KFileItem *i, const QPoint& );
  82.     void dropped(const KFileItem *, QDropEvent*, const KURL::List&);
  83. };
  84.  
  85. /**
  86.   * This class defines an interface to all file views. Its intent is
  87.   * to allow to switch the view of the files in the selector very easily.
  88.   * It defines some pure virtual functions, that must be implemented to
  89.   * make a file view working.
  90.   *
  91.   * Since this class is not a widget, but it's meant to be added to other
  92.   * widgets, its most important function is widget. This should return
  93.   * a pointer to the implemented widget.
  94.   *
  95.   * @short A base class for views of the KDE file selector
  96.   * @author Stephan Kulow <coolo@kde.org>
  97.   **/
  98. class KIO_EXPORT KFileView {
  99.  
  100. public:
  101.     KFileView();
  102.  
  103.     /**
  104.      * Destructor
  105.      */
  106.     virtual ~KFileView();
  107.  
  108.     /**
  109.      * inserts a list of items.
  110.      **/
  111.     void addItemList(const KFileItemList &list);
  112.  
  113.     /**
  114.       * a pure virtual function to get a QWidget, that can be added to
  115.       * other widgets. This function is needed to make it possible for
  116.       * derived classes to derive from other widgets.
  117.       **/
  118.     virtual QWidget *widget() = 0;
  119.  
  120.     /**
  121.      * ### As const-method, to be fixed in 3.0
  122.      */
  123.     QWidget *widget() const { return const_cast<KFileView*>(this)->widget(); }
  124.  
  125.     /**
  126.      * Sets @p filename the current item in the view, if available.
  127.      */
  128.     void setCurrentItem( const QString &filename );
  129.  
  130.     /**
  131.      * Reimplement this to set @p item the current item in the view, e.g.
  132.      * the item having focus.
  133.      */
  134.     virtual void setCurrentItem( const KFileItem *item ) = 0;
  135.  
  136.     /**
  137.      * @returns the "current" KFileItem, e.g. where the cursor is.
  138.      * Returns 0L when there is no current item (e.g. in an empty view).
  139.      * Subclasses have to implement this.
  140.      */
  141.     virtual KFileItem *currentFileItem() const = 0;
  142.  
  143.     /**
  144.      * Clears the view and all item lists.
  145.      */
  146.     virtual void clear();
  147.  
  148.     /**
  149.       * does a repaint of the view.
  150.       *
  151.       * The default implementation calls
  152.       * \code
  153.       * widget()->repaint(f)
  154.       * \endcode
  155.       **/
  156.     virtual void updateView(bool f = true);
  157.  
  158.     virtual void updateView(const KFileItem*);
  159.  
  160.     /**
  161.      * Removes an item from the list; has to be implemented by the view.
  162.      * Call KFileView::removeItem( item ) after removing it.
  163.      */
  164.     virtual void removeItem(const KFileItem *item);
  165.  
  166.     /**
  167.      * This hook is called when all items of the currently listed directory
  168.      * are listed and inserted into the view, i.e. there won't come any new
  169.      * items anymore.
  170.      */
  171.     virtual void listingCompleted();
  172.  
  173.     /**
  174.       * Returns the sorting order of the internal list. Newly added files
  175.       * are added through this sorting.
  176.       */
  177.     QDir::SortSpec sorting() const { return m_sorting; }
  178.  
  179.     /**
  180.       * Sets the sorting order of the view.
  181.       *
  182.       * Default is QDir::Name | QDir::IgnoreCase | QDir::DirsFirst
  183.       * Override this in your subclass and sort accordingly (usually by
  184.       * setting the sorting-key for every item and telling QIconView
  185.       * or QListView to sort.
  186.       *
  187.       * A view may choose to use a different sorting than QDir::Name, Time
  188.       * or Size. E.g. to sort by mimetype or any possible string. Set the
  189.       * sorting to QDir::Unsorted for that and do the rest internally.
  190.       *
  191.       * @see sortingKey
  192.       */
  193.     virtual void setSorting(QDir::SortSpec sort);
  194.  
  195.     /**
  196.      * Tells whether the current items are in reversed order (shortcut to
  197.      * sorting() & QDir::Reversed).
  198.      */
  199.     bool isReversed() const { return (m_sorting & QDir::Reversed); }
  200.  
  201.     void sortReversed();
  202.  
  203.     /**
  204.       * @returns the number of dirs and files
  205.       **/
  206.     uint count() const { return filesNumber + dirsNumber; }
  207.  
  208.     /**
  209.       * @returns the number of files.
  210.       **/
  211.     uint numFiles() const { return filesNumber; }
  212.  
  213.     /**
  214.       * @returns the number of directories
  215.       **/
  216.     uint numDirs() const { return dirsNumber; }
  217.  
  218.     virtual void setSelectionMode( KFile::SelectionMode sm );
  219.     virtual KFile::SelectionMode selectionMode() const;
  220.  
  221.     enum ViewMode {
  222.     Files       = 1,
  223.     Directories = 2,
  224.     All = Files | Directories
  225.     };
  226.     virtual void setViewMode( ViewMode vm );
  227.     virtual ViewMode viewMode() const { return view_mode; }
  228.  
  229.     /**
  230.      * @returns the localized name of the view, which could be displayed
  231.      * somewhere, e.g. in a menu, where the user can choose between views.
  232.      * @see setViewName
  233.      */
  234.     QString viewName() const { return m_viewName; }
  235.  
  236.     /**
  237.      * Sets the name of the view, which could be displayed somewhere.
  238.      * E.g. "Image Preview".
  239.      */
  240.     void setViewName( const QString& name ) { m_viewName = name; }
  241.  
  242.     virtual void setParentView(KFileView *parent);
  243.  
  244.     /**
  245.      * The derived view must implement this function to add
  246.      * the file in the widget.
  247.      *
  248.      * Make sure to call this implementation, i.e.
  249.      * KFileView::insertItem( i );
  250.      *
  251.      */
  252.     virtual void insertItem( KFileItem *i);
  253.  
  254.     /**
  255.      * pure virtual function, that should be implemented to clear
  256.      * the view. At this moment the list is already empty
  257.      **/
  258.     virtual void clearView() = 0;
  259.  
  260.     /**
  261.      * pure virtual function, that should be implemented to make item i
  262.      * visible, i.e. by scrolling the view appropriately.
  263.      */
  264.     virtual void ensureItemVisible( const KFileItem *i ) = 0;
  265.  
  266.     /**
  267.      * Clears any selection, unhighlights everything. Must be implemented by
  268.      * the view.
  269.      */
  270.     virtual void clearSelection() = 0;
  271.  
  272.     /**
  273.      * Selects all items. You may want to override this, if you can implement
  274.      * it more efficiently than calling setSelected() with every item.
  275.      * This works only in Multiselection mode of course.
  276.      */
  277.     virtual void selectAll();
  278.  
  279.     /**
  280.      * Inverts the current selection, i.e. selects all items, that were up to
  281.      * now not selected and deselects the other.
  282.      */
  283.     virtual void invertSelection();
  284.  
  285.     /**
  286.      * Tells the view that it should highlight the item.
  287.      * This function must be implemented by the view.
  288.      **/
  289.     virtual void setSelected(const KFileItem *, bool enable) = 0;
  290.  
  291.     /**
  292.      * @returns whether the given item is currently selected.
  293.      * Must be implemented by the view.
  294.      */
  295.     virtual bool isSelected( const KFileItem * ) const = 0;
  296.  
  297.     /**
  298.      * @returns all currently highlighted items.
  299.      */
  300.     const KFileItemList * selectedItems() const;
  301.  
  302.     /**
  303.      * @returns all items currently available in the current sort-order
  304.      */
  305.     const KFileItemList * items() const;
  306.  
  307.     virtual KFileItem * firstFileItem() const = 0;
  308.     virtual KFileItem * nextItem( const KFileItem * ) const = 0;
  309.     virtual KFileItem * prevItem( const KFileItem * ) const = 0;
  310.  
  311.     /**
  312.      * This is a KFileDialog specific hack: we want to select directories with
  313.      * single click, but not files. But as a generic class, we have to be able
  314.      * to select files on single click as well.
  315.      *
  316.      * This gives us the opportunity to do both.
  317.      *
  318.      * Every view has to decide when to call select( item ) when a file was
  319.      * single-clicked, based on onlyDoubleClickSelectsFiles().
  320.      */
  321.     void setOnlyDoubleClickSelectsFiles( bool enable ) {
  322.     myOnlyDoubleClickSelectsFiles = enable;
  323.     }
  324.  
  325.     /**
  326.      * @returns whether files (not directories) should only be select()ed by
  327.      * double-clicks.
  328.      * @see setOnlyDoubleClickSelectsFiles
  329.      */
  330.     bool onlyDoubleClickSelectsFiles() const {
  331.     return myOnlyDoubleClickSelectsFiles;
  332.     }
  333.  
  334.     /**
  335.      * increases the number of dirs and files.
  336.      * @returns true if the item fits the view mode
  337.      */
  338.     bool updateNumbers(const KFileItem *i);
  339.  
  340.     /**
  341.      * @returns the view-specific action-collection. Every view should
  342.      * add its actions here (if it has any) to make them available to
  343.      * e.g. the KDirOperator's popup-menu.
  344.      */
  345.     virtual KActionCollection * actionCollection() const;
  346.  
  347.     KFileViewSignaler * signaler() const { return sig; }
  348.  
  349.     virtual void readConfig( KConfig *, const QString& group = QString::null );
  350.     virtual void writeConfig( KConfig *, const QString& group = QString::null);
  351.  
  352.     /**
  353.      * Various options for drag and drop support. 
  354.      * These values can be or'd together.
  355.      * @li @p AutoOpenDirs Automatically open directory after hovering above it 
  356.      * for a short while while dragging.
  357.      * @since 3.2
  358.      */
  359.     enum DropOptions {
  360.     AutoOpenDirs  = 1
  361.     };
  362.     /**
  363.      * Specify DND options. See DropOptions for details.
  364.      * All options are disabled by default.
  365.      * @since 3.2
  366.      */
  367.     // KDE 4: Make virtual
  368.     void setDropOptions(int options);
  369.     
  370.     /**
  371.      * Returns the DND options in effect.
  372.      * See DropOptions for details.
  373.      * @since 3.2
  374.      */
  375.     int dropOptions();
  376.     
  377.     /**
  378.      * This method calculates a QString from the given parameters, that is
  379.      * suitable for sorting with e.g. QIconView or QListView. Their
  380.      * Item-classes usually have a setKey( const QString& ) method or a virtual
  381.      * method QString key() that is used for sorting.
  382.      *
  383.      * @param value Any string that should be used as sort criterion
  384.      * @param isDir Tells whether the key is computed for an item representing
  385.      *              a directory (directories are usually sorted before files)
  386.      * @param sortSpec An ORed combination of QDir::SortSpec flags.
  387.      *                 Currently, the values IgnoreCase, Reversed and
  388.      *                 DirsFirst are taken into account.
  389.      */
  390.     static QString sortingKey( const QString& value, bool isDir, int sortSpec);
  391.  
  392.     /**
  393.      * An overloaded method that takes not a QString, but a number as sort
  394.      * criterion. You can use this for file-sizes or dates/times for example.
  395.      * If you use a time_t, you need to cast that to KIO::filesize_t because
  396.      * of ambiguity problems.
  397.      */
  398.     static QString sortingKey( KIO::filesize_t value, bool isDir,int sortSpec);
  399.  
  400.     /**
  401.      * @internal
  402.      * delay before auto opening a directory
  403.      */
  404.     static int autoOpenDelay();
  405.  
  406. protected:
  407.     /**
  408.      * @internal
  409.      * class to distribute the signals
  410.      **/
  411.     KFileViewSignaler *sig;
  412.  
  413. private:
  414.     static QDir::SortSpec defaultSortSpec;
  415.     QDir::SortSpec m_sorting;
  416.     QString m_viewName;
  417.  
  418.     /**
  419.      * counters
  420.      **/
  421.     uint filesNumber;
  422.     uint dirsNumber;
  423.  
  424.     ViewMode view_mode;
  425.     KFile::SelectionMode selection_mode;
  426.  
  427.     // never use! It's only guaranteed to contain valid items in the items()
  428.     // method!
  429.     mutable KFileItemList m_itemList;
  430.  
  431.     mutable KFileItemList *m_selectedList;
  432.     bool myOnlyDoubleClickSelectsFiles;
  433.  
  434. protected:
  435.     virtual void virtual_hook( int id, void* data );
  436.     /* @internal for virtual_hook */
  437.     enum { VIRTUAL_SET_DROP_OPTIONS = 1 };
  438.     void setDropOptions_impl(int options);
  439. private:
  440.     class KFileViewPrivate;
  441.     KFileViewPrivate *d;
  442. };
  443.  
  444. #endif // KFILEINFOLISTWIDGET_H
  445.