KFile
kfiletreeview.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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 d->mSourceModel->dirLister()->setShowingDotFiles(enabled);
00159 d->mSourceModel->dirLister()->openUrl(d->mSourceModel->dirLister()->url());
00160 }
00161
00162 void KFileTreeView::setCurrentUrl(const KUrl &url)
00163 {
00164 QModelIndex baseIndex = d->mSourceModel->indexForUrl(url);
00165
00166 if (!baseIndex.isValid()) {
00167 d->mSourceModel->expandToUrl(url);
00168 return;
00169 }
00170
00171 QModelIndex proxyIndex = d->mProxyModel->mapFromSource(baseIndex);
00172 selectionModel()->clearSelection();
00173 selectionModel()->setCurrentIndex(proxyIndex, QItemSelectionModel::SelectCurrent);
00174 scrollTo(proxyIndex);
00175 }
00176
00177 void KFileTreeView::setRootUrl(const KUrl &url)
00178 {
00179 d->mSourceModel->dirLister()->openUrl(url);
00180 }
00181
00182 void KFileTreeView::contextMenuEvent(QContextMenuEvent *event)
00183 {
00184 QMenu menu;
00185 KToggleAction *showHiddenAction = new KToggleAction(i18n("Show Hidden Folders"), &menu);
00186 showHiddenAction->setChecked(d->mSourceModel->dirLister()->showingDotFiles());
00187 connect(showHiddenAction, SIGNAL(toggled(bool)), this, SLOT(setShowHiddenFiles(bool)));
00188
00189 menu.addAction(showHiddenAction);
00190 menu.exec(event->globalPos());
00191 }
00192
00193 #include "kfiletreeview.moc"