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 ¤tIndex, 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 setLayoutDirection(Qt::LeftToRight); 00095 00096 d->mSourceModel->dirLister()->openUrl(KUrl(QDir::root().absolutePath()), KDirLister::Keep); 00097 00098 connect(this, SIGNAL(activated(const QModelIndex&)), 00099 this, SLOT(_k_activated(const QModelIndex&))); 00100 connect(selectionModel(), SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)), 00101 this, SLOT(_k_currentChanged(const QModelIndex&, const QModelIndex&))); 00102 00103 connect(d->mSourceModel, SIGNAL(expand(const QModelIndex&)), 00104 this, SLOT(_k_expanded(const QModelIndex&))); 00105 } 00106 00107 KFileTreeView::~KFileTreeView() 00108 { 00109 delete d; 00110 } 00111 00112 KUrl KFileTreeView::currentUrl() const 00113 { 00114 return d->urlForProxyIndex(currentIndex()); 00115 } 00116 00117 KUrl KFileTreeView::selectedUrl() const 00118 { 00119 if (!selectionModel()->hasSelection()) 00120 return KUrl(); 00121 00122 const QItemSelection selection = selectionModel()->selection(); 00123 const QModelIndex firstIndex = selection.indexes().first(); 00124 00125 return d->urlForProxyIndex(firstIndex); 00126 } 00127 00128 KUrl::List KFileTreeView::selectedUrls() const 00129 { 00130 KUrl::List urls; 00131 00132 if (!selectionModel()->hasSelection()) 00133 return urls; 00134 00135 const QModelIndexList indexes = selectionModel()->selection().indexes(); 00136 foreach (const QModelIndex &index, indexes) { 00137 const KUrl url = d->urlForProxyIndex(index); 00138 if (url.isValid()) 00139 urls.append(url); 00140 } 00141 00142 return urls; 00143 } 00144 00145 KUrl KFileTreeView::rootUrl() const 00146 { 00147 return d->mSourceModel->dirLister()->url(); 00148 } 00149 00150 void KFileTreeView::setDirOnlyMode(bool enabled) 00151 { 00152 d->mSourceModel->dirLister()->setDirOnlyMode(enabled); 00153 d->mSourceModel->dirLister()->openUrl(d->mSourceModel->dirLister()->url()); 00154 } 00155 00156 void KFileTreeView::setShowHiddenFiles(bool enabled) 00157 { 00158 KUrl url = currentUrl(); 00159 d->mSourceModel->dirLister()->setShowingDotFiles(enabled); 00160 d->mSourceModel->dirLister()->openUrl(d->mSourceModel->dirLister()->url()); 00161 setCurrentUrl(url); 00162 } 00163 00164 void KFileTreeView::setCurrentUrl(const KUrl &url) 00165 { 00166 QModelIndex baseIndex = d->mSourceModel->indexForUrl(url); 00167 00168 if (!baseIndex.isValid()) { 00169 d->mSourceModel->expandToUrl(url); 00170 return; 00171 } 00172 00173 QModelIndex proxyIndex = d->mProxyModel->mapFromSource(baseIndex); 00174 selectionModel()->clearSelection(); 00175 selectionModel()->setCurrentIndex(proxyIndex, QItemSelectionModel::SelectCurrent); 00176 scrollTo(proxyIndex); 00177 } 00178 00179 void KFileTreeView::setRootUrl(const KUrl &url) 00180 { 00181 d->mSourceModel->dirLister()->openUrl(url); 00182 } 00183 00184 void KFileTreeView::contextMenuEvent(QContextMenuEvent *event) 00185 { 00186 QMenu menu; 00187 KToggleAction *showHiddenAction = new KToggleAction(i18n("Show Hidden Folders"), &menu); 00188 showHiddenAction->setChecked(d->mSourceModel->dirLister()->showingDotFiles()); 00189 connect(showHiddenAction, SIGNAL(toggled(bool)), this, SLOT(setShowHiddenFiles(bool))); 00190 00191 menu.addAction(showHiddenAction); 00192 menu.exec(event->globalPos()); 00193 } 00194 00195 bool KFileTreeView::showHiddenFiles() const 00196 { 00197 return d->mSourceModel->dirLister()->showingDotFiles(); 00198 } 00199 00200 #include "kfiletreeview.moc"
KDE 4.6 API Reference