Source Code (Use browser search to find items of interest.)

Class Index

kdelibs'KFileView (./kdelibs/kfile/kfileview.h:70)

class KFileView {

public:
    KFileView();

    /** Destructor */
    virtual ~KFileView();

    /**
     * inserts a list of items.
     **/
    virtual void addItemList(const KFileViewItemList &list);

    /**
      * a pure virtual function to get a QWidget, that can be added to
      * other widgets. This function is needed to make it possible for
      * derived classes to derive from other widgets.
      **/
    virtual QWidget *widget() = 0;

    /**
      * set the current item in the view.
      *
      * the normal use of this function is to use the parameter filename to
      * set the highlight on this file.
      *
      * the extend use of this function is to set filename to 0. In this
      * case the @see KFileViewItem is used to find out the position of the
      * file. Since this is faster, this way is used in case a view has
      * other child views.
      **/
    virtual void setCurrentItem(const QString &filename,
				const KFileViewItem * entry = 0);

    /**
     * clears the view and all item lists
      */
    virtual void clear();

    /**
      * does a repaint of the view.
      *
      * The default implementation calls
      * <pre>widget()->repaint(f)</pre>
      **/
    virtual void updateView(bool f = true);

    virtual void updateView(const KFileViewItem*);

    /**
      * specifies the sorting of the internal list. Newly added files
      * are added through this sorting.
      *
      **/
    QDir::SortSpec sorting() const { return mySorting; }

    /**
      * set the sorting of the view
      *
      * Default is QDir::Name | QDir::IgnoreCase | QDir::DirsFirst
      * Don't use QDir::Reversed, use @ref sortReversed() if you want to
      * reverse the sort order.
      * Calling this method keeps the reversed-setting
      * If your view wants to get notified about sorting-changes (e.g. to show
      * a sorting indicator), override this method and call this implementation
      * in the beginning of your method.
      * @see #setSortMode
      **/
    virtual void setSorting(QDir::SortSpec sort);

    /**
      * set the sorting mode. Default mode is Increasing. Affects only
      * newly added items.
      * @see #setSorting
      **/
    void setSortMode(KFile::SortMode mode) { mySortMode = mode; }

    /**
     * @returns the current sort mode
     * @see #setSortMode
     * @see #setSorting
     */
    KFile::SortMode sortMode() const { return mySortMode; }

    /**
     * Toggles the current sort order, i.e. the order is reversed.
     * @see #isReversed
     */
    virtual void sortReversed();

    /**
     * Tells whether the current items are in reversed order (= contrary to
     * @ref sortMode).
     */
    bool isReversed() const { return reversed; }

    /**
      * @returns the number of dirs and files
      **/
    uint count() const { return filesNumber + dirsNumber; }

    /**
      * @returns the number of files.
      **/
    uint numFiles() const { return filesNumber; }

    /**
      * @returns the number of directories
      **/
    uint numDirs() const { return dirsNumber; }

    virtual void setSelectionMode( KFile::SelectionMode sm );
    virtual KFile::SelectionMode selectionMode() const;

    enum ViewMode {
	Files       = 1,
	Directories = 2,
	All = Files | Directories
    };
    virtual void setViewMode( ViewMode vm );
    virtual ViewMode viewMode() const { return view_mode; }

    /**
     * @returns the localized name of the view, which could be displayed
     * somewhere, e.g. in a menu, where the user can choose between views.
     * @see #setViewName
     */
    QString viewName() { return viewname; }

    /**
     * Sets the name of the view, which could be displayed somewhere.
     * E.g. "Image Preview".
     */
    void setViewName( const QString& name ) { viewname = name; }

    virtual void setOperator(QObject *ops);

    /**
     * The derived view must implement this function to add
     * the file in the widget.
     *
     **/
    virtual void insertItem( KFileViewItem *i) = 0;

    /**
     * pure virtual function, that should be implemented to clear
     * the view. At this moment the list is already empty
     **/
    virtual void clearView() = 0;

    /**
     * pure virtual function, that should be implemented to make item i
     * visible, i.e. by scrolling the view appropriately.
     */ 
    virtual void ensureItemVisible( const KFileViewItem *i ) = 0;
    
    /**
     * Clear any selection, unhighlight everything. Must be implemented by the
     * view.
     */
    virtual void clearSelection() = 0;

    /**
     * Selects all items. You may want to override this, if you can implement
     * it more efficiently than calling setSelected() with every item.
     * This works only in Multiselection mode of course.
     */
    virtual void selectAll();

    /**
     * Inverts the current selection, i.e. selects all items, that were up to
     * now not selected and deselects the other.
     */
    virtual void invertSelection();

    /**
      * Tells the view that it should highlight the item.
      * This function must be implemented by the view
      **/
    virtual void setSelected(const KFileViewItem *, bool enable) = 0;

    /**
     * @returns whether the given item is currently selected.
     * Must be implemented by the view.
     */
    virtual bool isSelected( const KFileViewItem * ) const = 0;

    /**
     * @returns all currently highlighted items.
     */
    const KFileViewItemList * selectedItems() const;

    /**
     * @returns all items currently available in the current sort-order
     */
    const KFileViewItemList * items() const;

    /**
     * Inserts "counter" KFileViewItems and sorts them conforming to the
     * current sort-order.
     * If you override this method, you have to call @ref setFirstItem()
     * afterwards, to set the first item of your newly sorted items.
     */
    virtual void insertSorted(KFileViewItem *tfirst, uint counter);

    /**
     * @returns the first (depending on sort order) item. It forms sort of a
     * list, as each item holds a pointer to the next item.
     * This is only public for internal reasons, DON'T call it unless you
     * implement a View yourself and really need to.
     */
    KFileViewItem *firstItem() const { return myFirstItem; }

protected:

    /**
      * Call this method when an item is selected (depends on single click /
      * double click configuration). Emits the appropriate signal.
      **/
    void select( const KFileViewItem *item );

    /**
     * emits the highlighted signal for item. Call this in your subclass,
     * whenever the selection changes.
     */
    void highlight( const KFileViewItem *item) { sig->highlightFile( item ); }

    /**
     * compares two items in the current context (sortMode and others)
     * returns -1, if i1 is before i2 and 1, if the other case is true
     * in case, both are equal (in current context), the behaviour is
     * undefined!
     **/
    int compareItems(const KFileViewItem *fi1, const KFileViewItem *fi2) const;

    /**
     * this is a help function for sorting, since I can't use the libc
     * version (because I have a variable sort function)
     *
     */
    void QuickSort(KFileViewItem* a[], int lo0, int hi0) const;

    bool updateNumbers(const KFileViewItem *i);

    KFileViewItem *mergeLists(KFileViewItem *list1, KFileViewItem *list2);

    void activateMenu( const KFileViewItem *i ) { sig->activateMenu(i); }

    /**
     * @internal
     * class to distribute the signals
     **/
    KFileViewSignaler *sig;

    /**
     * Call this method to set the first item after you call your own
     * insertSorted(). You only need to call it when you override
     * insertSorted().
     */
    void setFirstItem( KFileViewItem * item ) { myFirstItem = item; }

    /**
     * Call this if you changed the sort order and want to perform the actual
     * sorting and show the new items.
     */
    void resort() {
	if ( count() > 1 ) {
	    KFileViewItem *item = myFirstItem;
	    myFirstItem = 0L;
	    insertSorted( item, count() );
	}
    }

private:

    bool reversed;
    QDir::SortSpec mySorting;
    static QDir::SortSpec defaultSortSpec;
    KFile::SortMode mySortMode;
    QString viewname;

    /**
     * counters
     **/
    uint filesNumber;
    uint dirsNumber;

    ViewMode view_mode;
    KFile::SelectionMode selection_mode;

    KFileViewItem *myFirstItem;
    mutable KFileViewItemList *itemList, *selectedList;
    mutable  bool itemListDirty;

private:
    class KFileViewPrivate;
    KFileViewPrivate *d;
};

kdelibs'KFileView::KFileView() (./kdelibs/kfile/kfileview.cpp:38)

KFileView::KFileView()
{
    reversed   = false;        // defaults
    itemListDirty = true;
    mySortMode = KFile::Increasing;
    mySorting  = KFileView::defaultSortSpec;

    sig = new KFileViewSignaler();
    sig->setName("view-signaller");

    itemList = 0L;
    selectedList = 0L;
    filesNumber = 0;
    dirsNumber = 0;
    myFirstItem = 0;

    view_mode = All;
    selection_mode = KFile::Single;
    viewname = i18n("Unknown View");
}


kdelibs'KFileView::~KFileView() (./kdelibs/kfile/kfileview.cpp:59)

KFileView::~KFileView()
{
    delete sig;
    delete itemList;
    delete selectedList;
}


kdelibs'KFileView::setOperator() (./kdelibs/kfile/kfileview.cpp:66)

void KFileView::setOperator(QObject *ops)
{
    if (ops) {
	QObject::connect(sig,
			 SIGNAL( activatedMenu(const KFileViewItem * ) ),
			 ops, SLOT(activatedMenu(const KFileViewItem * ) ) );
	
	QObject::connect(sig,
			 SIGNAL( dirActivated(const KFileViewItem *) ),
			 ops, SLOT( selectDir(const KFileViewItem*) ) );
	
	QObject::connect(sig,
			 SIGNAL( fileSelected(const KFileViewItem *) ),
			 ops, SLOT( selectFile(const KFileViewItem*) ) );

	QObject::connect(sig,
			 SIGNAL( fileHighlighted(const KFileViewItem *) ),
			 ops, SLOT( highlightFile(const KFileViewItem*) ));

    } else
	sig->disconnect((QObject*)0);
}

/*
bool KFileView::addItem( KFileViewItem *i )
{
    if (!updateNumbers(i))
	return false;

    return insertItem(i);
}
*/

kdelibs'KFileView::updateNumbers() (./kdelibs/kfile/kfileview.cpp:98)

bool KFileView::updateNumbers(const KFileViewItem *i)
{
    if (i->isHidden())
	return false;

    if (!( viewMode() & Files ) && i->isFile())
	return false;

    if (!( viewMode() & Directories ) && i->isDir())
	return false;

    if (i->isDir())
	dirsNumber++;
    else
	filesNumber++;

    return true;
}


kdelibs'KFileView::addItemList() (./kdelibs/kfile/kfileview.cpp:117)

void KFileView::addItemList(const KFileViewItemList &list)
{
    KFileViewItem *tmp, *tfirst = 0;
    int counter = 0;

    for (KFileViewItemListIterator it(list); (tmp = it.current()); ++it) {

	if (!updateNumbers(tmp))
	    continue;

	counter++;

	if (!tfirst) {
	    tfirst = tmp;
	    tfirst->setNext(0);
	    continue;
	}
	
	tmp->setNext(tfirst);
	tfirst = tmp;
    }

    insertSorted(tfirst, counter);
}


kdelibs'KFileView::insertSorted() (./kdelibs/kfile/kfileview.cpp:144)

void KFileView::insertSorted(KFileViewItem *tfirst, uint counter)
{
    KFileViewItem **sortedArray = new KFileViewItem*[counter];
    KFileViewItem *it;
    uint index;

    for (it = tfirst, index = 0; it; index++, it = it->next())
	sortedArray[index] = it;

    ASSERT(index == counter);

    QuickSort(sortedArray, 0, counter - 1);
    tfirst = sortedArray[0];
    tfirst->setNext(0);

    KFileViewItem *tlast = tfirst;
    for (index = 1; index < counter; index++) {
	tlast->setNext(sortedArray[index]);
	tlast = sortedArray[index];
    }
    if ( counter >= 2 ) // terminate last item
	tlast->setNext(0);

    delete [] sortedArray;

    clearView();
    if ( myFirstItem == tfirst ) // we're probably just resorting, not adding atims
	myFirstItem = 0L;

    myFirstItem = mergeLists(myFirstItem, tfirst);

    for (it = myFirstItem; it; it = it->next())
	insertItem(it);

#ifdef Q2HELPER
    qt_qstring_stats();
#endif
}


kdelibs'KFileView::mergeLists() (./kdelibs/kfile/kfileview.cpp:183)

KFileViewItem *KFileView::mergeLists(KFileViewItem *list1, KFileViewItem *list2)
{
    // this is the central place where we know that our itemList is dirty
    itemListDirty = true;

    if (!list1)
	return list2;

    if (!list2)
	return list1;

    // We can't have the same item twice in the list. If the assertion fails,
    // there is a bug somewhere else, e.g. you tried to add an item which is
    // already there.
    ASSERT( list1 != list2 );

    KFileViewItem *newlist = 0L;

    if ( compareItems( list1, list2 ) < 0 ) {
	newlist = list1;
	list1 = list1->next();
    } else {
	newlist = list2;
	list2 = list2->next();
    }

    KFileViewItem *newstart = newlist;

    while (list1 || list2) {

	if (!list1) { // first list empty
	    newlist->setNext(list2);
	    break;
	}
	if (!list2) { // second list empty
	    newlist->setNext(list1);
	    break;
	}
	if (compareItems(list1, list2) < 0) {
	    newlist->setNext(list1);
	    newlist = list1;
	    list1 = list1->next();
	} else {
	    newlist->setNext(list2);
	    newlist = list2;
	    list2 = list2->next();
	}
    }

    return newstart;
}


kdelibs'KFileView::setSorting() (./kdelibs/kfile/kfileview.cpp:235)

void KFileView::setSorting(QDir::SortSpec new_sort)
{
    if ( mySorting == new_sort )
	return;

    mySorting = new_sort;
    resort();
}


kdelibs'KFileView::sortReversed() (./kdelibs/kfile/kfileview.cpp:244)

void KFileView::sortReversed()
{
    reversed = !reversed;
    resort();
}


kdelibs'KFileView::clear() (./kdelibs/kfile/kfileview.cpp:250)

void KFileView::clear()
{
    myFirstItem = 0;
    filesNumber = 0;
    dirsNumber = 0;
    clearView();
    itemListDirty = true;
}

// this implementation is from the jdk demo Sorting

kdelibs'KFileView::QuickSort() (./kdelibs/kfile/kfileview.cpp:260)

void KFileView::QuickSort(KFileViewItem* a[], int lo0, int hi0) const
{
    int lo = lo0;
    int hi = hi0;
    const KFileViewItem *mid;

    if ( hi0 >= lo0)
	{
	
	    /* Arbitrarily establishing partition element as the midpoint of
	     * the array.
	     */
	    mid = a[ ( lo0 + hi0 ) / 2 ];
	
	    // loop through the array until indices cross
	    while( lo <= hi )
		{
		    /* find the first element that is greater than or equal to
		     * the partition element starting from the left Index.
		     */
		    while( ( lo < hi0 ) && ( compareItems(a[lo], mid) < 0 ) )
			++lo;
		
		    /* find an element that is smaller than or equal to
		     * the partition element starting from the right Index.
		     */
		    while( ( hi > lo0 ) &&  ( compareItems(a[hi], mid) > 0) )
			--hi;

		    // if the indexes have not crossed, swap
		    if( lo <= hi )
			{
			    if (lo != hi) {
				const KFileViewItem *T = a[lo];
				a[lo] = a[hi];
				a[hi] = const_cast<KFileViewItem*>(T);
			    }
			    ++lo;
			    --hi;
			}
		}
	
	    /* If the right index has not reached the left side of array
	     * must now sort the left partition.
	     */
	    if( lo0 < hi )
		QuickSort( a, lo0, hi );
	
	    /* If the left index has not reached the right side of array
	     * must now sort the right partition.
	     */
	    if( lo < hi0 )
		QuickSort( a, lo, hi0 );
	
	}
}


kdelibs'KFileView::compareItems() (./kdelibs/kfile/kfileview.cpp:317)

int KFileView::compareItems(const KFileViewItem *fi1, const KFileViewItem *fi2) const
{
    static QString dirup = QString::fromLatin1("..");
    bool bigger = true;
    bool keepFirst = false;
    bool dirsFirst = ((mySorting & QDir::DirsFirst) == QDir::DirsFirst);

    if (fi1 == fi2)
	return 0;

    // .. is always bigger, independent of the sort criteria
    if ( fi1->name() == dirup ) {
	bigger = false;
	keepFirst = dirsFirst;
    }
    else if ( fi2->name() == dirup ) {
	bigger = true;
	keepFirst = dirsFirst;
    }
	
    else {
	if ( fi1->isDir() != fi2->isDir() && dirsFirst ) {
	    bigger = fi2->isDir();
	    keepFirst = true;
	}
	else {

	    QDir::SortSpec sort = static_cast<QDir::SortSpec>(mySorting & QDir::SortByMask);

	    if (fi1->isDir() || fi2->isDir())
		sort = static_cast<QDir::SortSpec>(KFileView::defaultSortSpec & QDir::SortByMask);

	    switch (sort) {
	    case QDir::Unsorted:
		bigger = true;  // nothing
		break;
	    case QDir::Size:
		bigger = (fi1->size() > fi2->size());
		break;
	    case QDir::Time:
		bigger = (fi1->mTime() > fi2->mTime());
		break;
	    case QDir::Name:
	    default:
		if ( (mySorting & QDir::IgnoreCase) == QDir::IgnoreCase )
		    bigger = (fi1->name( true ) > fi2->name( true ));
		else
		    bigger = (fi1->name() > fi2->name());
		break;
	    }
	}
    }

    if (reversed && !keepFirst ) // don't reverse dirs to the end!
      bigger = !bigger;

    return (bigger ? 1 : -1);
}


kdelibs'KFileView::select() (./kdelibs/kfile/kfileview.cpp:376)

void KFileView::select( const KFileViewItem *item )
{
    assert(item);

    if ( item->isDir() ) {
	sig->activateDir(item);
    } else {
	sig->activateFile(item);
    }
}



kdelibs'KFileView::updateView() (./kdelibs/kfile/kfileview.cpp:388)

void  KFileView::updateView(bool f)
{
    widget()->repaint(f);
}


kdelibs'KFileView::updateView() (./kdelibs/kfile/kfileview.cpp:393)

void KFileView::updateView(const KFileViewItem *)
{
}


kdelibs'KFileView::setCurrentItem() (./kdelibs/kfile/kfileview.cpp:397)

void KFileView::setCurrentItem(const QString &item,
			       const KFileViewItem *entry)
{
    if (!item.isNull()) {
	KFileViewItem *it = myFirstItem;
	while (it) {
	    if (it->name() == item) {
		setSelected(it, true);
		highlight(it);
		return;
	    }
	    it = it->next();
	}
    } else {
	setSelected(entry, true);
	return;
    }

    warning("setCurrentItem: no match found.");
}


kdelibs'KFileView::items() (./kdelibs/kfile/kfileview.cpp:418)

const KFileViewItemList * KFileView::items() const
{
    if ( itemListDirty ) {
	if ( !itemList )
	    itemList = new KFileViewItemList;

	itemListDirty = false;
	itemList->clear();
	if ( myFirstItem ) {
	    KFileViewItem *item = 0L;
	    for (item = myFirstItem; item; item = item->next())
		itemList->append( item );
	}
    }
    return itemList;
}



kdelibs'KFileView::selectedItems() (./kdelibs/kfile/kfileview.cpp:436)

const KFileViewItemList * KFileView::selectedItems() const
{
    if ( !selectedList )
	selectedList = new KFileViewItemList;

    selectedList->clear();

    if ( myFirstItem ) {
	KFileViewItem *item = 0L;
	for (item = myFirstItem; item; item = item->next()) {
	    if ( isSelected( item ) )
		selectedList->append( item );
	}
    }

    return selectedList;
}


kdelibs'KFileView::selectAll() (./kdelibs/kfile/kfileview.cpp:454)

void KFileView::selectAll()
{
    if (selection_mode == KFile::NoSelection || selection_mode== KFile::Single)
	return;

    KFileViewItem *item = 0L;
    for (item = myFirstItem; item; item = item->next())
	setSelected( item, true );
}



kdelibs'KFileView::invertSelection() (./kdelibs/kfile/kfileview.cpp:465)

void KFileView::invertSelection()
{
    KFileViewItem *item;
    const KFileViewItemList *list = items();
    KFileViewItemListIterator it ( *list );
    for ( ; (item = it.current()); ++it )
	setSelected( item, isSelected( item ) );
}



kdelibs'KFileView::setSelectionMode() (./kdelibs/kfile/kfileview.cpp:475)

void KFileView::setSelectionMode( KFile::SelectionMode sm )
{
    selection_mode = sm;
}

KFile::SelectionMode KFileView::selectionMode() const
{
    return selection_mode;
}


kdelibs'KFileView::setViewMode() (./kdelibs/kfile/kfileview.cpp:485)

void KFileView::setViewMode( ViewMode vm )
{
    view_mode = vm;
}