• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KFile

kfiletreeview.cpp

Go to the documentation of this file.
00001 /*
00002    This file is part of the KDE project
00003 
00004    Copyright (C) 2007 Tobias Koenig <tokoe@kde.org>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019    Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "kfiletreeview.h"
00023 
00024 #include <QtCore/QDir>
00025 #include <QtGui/QContextMenuEvent>
00026 #include <QtGui/QMenu>
00027 
00028 #include <kdirlister.h>
00029 #include <kdirmodel.h>
00030 #include <kdirsortfilterproxymodel.h>
00031 #include <kfileitemdelegate.h>
00032 #include <klocale.h>
00033 #include <ktoggleaction.h>
00034 #include <kurl.h>
00035 
00036 class KFileTreeView::Private
00037 {
00038     public:
00039         Private(KFileTreeView *parent)
00040             : q(parent)
00041         {
00042         }
00043 
00044         KUrl urlForProxyIndex(const QModelIndex &index) const;
00045 
00046         void _k_activated(const QModelIndex&);
00047         void _k_currentChanged(const QModelIndex&, const QModelIndex&);
00048         void _k_expanded(const QModelIndex&);
00049 
00050         KFileTreeView *q;
00051         KDirModel *mSourceModel;
00052         KDirSortFilterProxyModel *mProxyModel;
00053 };
00054 
00055 KUrl KFileTreeView::Private::urlForProxyIndex(const QModelIndex &index) const
00056 {
00057     const KFileItem item = mSourceModel->itemForIndex(mProxyModel->mapToSource(index));
00058 
00059     return !item.isNull() ? item.url() : KUrl();
00060 }
00061 
00062 void KFileTreeView::Private::_k_activated(const QModelIndex &index)
00063 {
00064     const KUrl url = urlForProxyIndex(index);
00065     if (url.isValid())
00066         emit q->activated(url);
00067 }
00068 
00069 void KFileTreeView::Private::_k_currentChanged(const QModelIndex &currentIndex, const QModelIndex&)
00070 {
00071     const KUrl url = urlForProxyIndex(currentIndex);
00072     if (url.isValid())
00073         emit q->currentChanged(url);
00074 }
00075 
00076 void KFileTreeView::Private::_k_expanded(const QModelIndex &baseIndex)
00077 {
00078     QModelIndex index = mProxyModel->mapFromSource(baseIndex);
00079 
00080     q->selectionModel()->clearSelection();
00081     q->selectionModel()->setCurrentIndex(index, QItemSelectionModel::SelectCurrent);
00082     q->scrollTo(index);
00083 }
00084 
00085 KFileTreeView::KFileTreeView(QWidget *parent)
00086     : QTreeView(parent), d(new Private(this))
00087 {
00088     d->mSourceModel = new KDirModel(this);
00089     d->mProxyModel = new KDirSortFilterProxyModel(this);
00090     d->mProxyModel->setSourceModel(d->mSourceModel);
00091 
00092     setModel(d->mProxyModel);
00093     setItemDelegate(new KFileItemDelegate(this));
00094 
00095     d->mSourceModel->dirLister()->openUrl(KUrl(QDir::root().absolutePath()), KDirLister::Keep);
00096 
00097     connect(this, SIGNAL(activated(const QModelIndex&)),
00098             this, SLOT(_k_activated(const QModelIndex&)));
00099     connect(selectionModel(), SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
00100             this, SLOT(_k_currentChanged(const QModelIndex&, const QModelIndex&)));
00101 
00102     connect(d->mSourceModel, SIGNAL(expand(const QModelIndex&)),
00103             this, SLOT(_k_expanded(const QModelIndex&)));
00104 }
00105 
00106 KFileTreeView::~KFileTreeView()
00107 {
00108     delete d;
00109 }
00110 
00111 KUrl KFileTreeView::currentUrl() const
00112 {
00113     return d->urlForProxyIndex(currentIndex());
00114 }
00115 
00116 KUrl KFileTreeView::selectedUrl() const
00117 {
00118     if (!selectionModel()->hasSelection())
00119         return KUrl();
00120 
00121     const QItemSelection selection = selectionModel()->selection();
00122     const QModelIndex firstIndex = selection.indexes().first();
00123 
00124     return d->urlForProxyIndex(firstIndex);
00125 }
00126 
00127 KUrl::List KFileTreeView::selectedUrls() const
00128 {
00129     KUrl::List urls;
00130 
00131     if (!selectionModel()->hasSelection())
00132         return urls;
00133 
00134     const QModelIndexList indexes = selectionModel()->selection().indexes();
00135     foreach (const QModelIndex &index, indexes) {
00136         const KUrl url = d->urlForProxyIndex(index);
00137         if (url.isValid())
00138             urls.append(url);
00139     }
00140 
00141     return urls;
00142 }
00143 
00144 KUrl KFileTreeView::rootUrl() const
00145 {
00146     return d->mSourceModel->dirLister()->url();
00147 }
00148 
00149 void KFileTreeView::setDirOnlyMode(bool enabled)
00150 {
00151     d->mSourceModel->dirLister()->setDirOnlyMode(enabled);
00152     d->mSourceModel->dirLister()->openUrl(d->mSourceModel->dirLister()->url());
00153 }
00154 
00155 void KFileTreeView::setShowHiddenFiles(bool enabled)
00156 {
00157     d->mSourceModel->dirLister()->setShowingDotFiles(enabled);
00158     d->mSourceModel->dirLister()->openUrl(d->mSourceModel->dirLister()->url());
00159 }
00160 
00161 void KFileTreeView::setCurrentUrl(const KUrl &url)
00162 {
00163     QModelIndex baseIndex = d->mSourceModel->indexForUrl(url);
00164 
00165     if (!baseIndex.isValid()) {
00166         d->mSourceModel->expandToUrl(url);
00167         return;
00168     }
00169 
00170     QModelIndex proxyIndex = d->mProxyModel->mapFromSource(baseIndex);
00171     selectionModel()->clearSelection();
00172     selectionModel()->setCurrentIndex(proxyIndex, QItemSelectionModel::SelectCurrent);
00173     scrollTo(proxyIndex);
00174 }
00175 
00176 void KFileTreeView::setRootUrl(const KUrl &url)
00177 {
00178     d->mSourceModel->dirLister()->openUrl(url);
00179 }
00180 
00181 void KFileTreeView::contextMenuEvent(QContextMenuEvent *event)
00182 {
00183     QMenu menu;
00184     KToggleAction *showHiddenAction = new KToggleAction(i18n("Show Hidden Folders"), &menu);
00185     showHiddenAction->setChecked(d->mSourceModel->dirLister()->showingDotFiles());
00186     connect(showHiddenAction, SIGNAL(toggled(bool)), this, SLOT(setShowHiddenFiles(bool)));
00187 
00188     menu.addAction(showHiddenAction);
00189     menu.exec(event->globalPos());
00190 }
00191 
00192 #include "kfiletreeview.moc"

KFile

Skip menu "KFile"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal