1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
/*
SPDX-FileCopyrightText: 2010 Jan Lepper <dehtris@yahoo.de>
SPDX-FileCopyrightText: 2010-2022 Krusader Krew <https://krusader.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "krfiletreeview.h"
#include "../FileSystem/sizecalculator.h"
#include "../FileSystem/filesystemprovider.h"
#include "../compat.h"
#include "../defaults.h"
#include "../icon.h"
#include "../krglobal.h"
#include "panelfunc.h"
#include <QAction>
#include <QApplication>
#include <QActionGroup>
#include <QCursor>
#include <QDir>
#include <QDropEvent>
#include <QHeaderView>
#include <QMenu>
#include <QMimeData>
#include <QProxyStyle>
#include <KFileItemListProperties>
#include <KJobWidgets>
#include <KUrlMimeData>
#include <KDirLister>
#include <KFileItem>
#include <KFileItemDelegate>
#include <KIO/DropJob>
#include <KIO/Paste>
#include <KIO/PasteJob>
#include <KLocalizedString>
#include <KPropertiesDialog>
class KrDirModel : public KDirModel
{
public:
KrDirModel(QWidget *parent)
: KDirModel(parent)
{
}
protected:
Qt::ItemFlags flags(const QModelIndex &index) const override
{
Qt::ItemFlags itflags = KDirModel::flags(index);
if (index.column() != KDirModel::Name)
itflags &= ~Qt::ItemIsDropEnabled;
return itflags;
}
};
class TreeStyle : public QProxyStyle
{
public:
explicit TreeStyle(QStyle *style)
: QProxyStyle(style)
{
}
int styleHint(StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const override
{
if (hint == QStyle::SH_ItemView_ActivateItemOnSingleClick) {
return true;
}
return QProxyStyle::styleHint(hint, option, widget, returnData);
}
};
KrFileTreeView::KrFileTreeView(QWidget *parent)
: QTreeView(parent)
, mStartTreeFromCurrent(false)
, mStartTreeFromPlace(true)
{
mSourceModel = new KrDirModel(this);
mSourceModel->dirLister()->setDirOnlyMode(true);
mProxyModel = new KDirSortFilterProxyModel(this);
mProxyModel->setSourceModel(mSourceModel);
setModel(mProxyModel);
mFilePlacesModel = new KFilePlacesModel(this);
setItemDelegate(new KFileItemDelegate(this));
setUniformRowHeights(true);
// drag&drop
setAcceptDrops(true);
setDragEnabled(true);
setDropIndicatorShown(true);
mSourceModel->setDropsAllowed(KDirModel::DropOnDirectory);
setStyle(new TreeStyle(style()));
connect(this, &KrFileTreeView::activated, this, &KrFileTreeView::slotActivated);
connect(mSourceModel, &KDirModel::expand, this, &KrFileTreeView::slotExpanded);
QFontMetrics fontMetrics(viewport()->font());
header()->resizeSection(KDirModel::Name, fontMetrics.horizontalAdvance("WWWWWWWWWWWWWWW"));
header()->setContextMenuPolicy(Qt::CustomContextMenu);
connect(header(), &QHeaderView::customContextMenuRequested, this, &KrFileTreeView::showHeaderContextMenu);
setBriefMode(true);
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, &KrFileTreeView::customContextMenuRequested, this, &KrFileTreeView::slotCustomContextMenuRequested);
setTree(mStartTreeFromCurrent, mStartTreeFromPlace);
}
void KrFileTreeView::setCurrentUrl(const QUrl &url)
{
mCurrentUrl = url;
if (mStartTreeFromCurrent) {
setTreeRoot(url);
} else {
if (mStartTreeFromPlace) {
const QModelIndex index = mFilePlacesModel->closestItem(url); // magic here
const QUrl rootBase = index.isValid() ? mFilePlacesModel->url(index) : QUrl::fromLocalFile(QDir::root().path());
setTreeRoot(rootBase);
}
if (isVisible(url)) {
// avoid unwanted scrolling by KDirModel::expandToUrl()
setCurrentIndex(mProxyModel->mapFromSource(mSourceModel->indexForUrl(url)));
} else {
mSourceModel->expandToUrl(url);
}
}
}
QUrl KrFileTreeView::urlForProxyIndex(const QModelIndex &index) const
{
const KFileItem item = mSourceModel->itemForIndex(mProxyModel->mapToSource(index));
return !item.isNull() ? item.url() : QUrl();
}
void KrFileTreeView::slotActivated(const QModelIndex &index)
{
const QUrl url = urlForProxyIndex(index);
if (url.isValid()) {
emit urlActivated(url);
}
}
void KrFileTreeView::dropEvent(QDropEvent *event)
{
QUrl destination = urlForProxyIndex(indexAt(event->position().toPoint()));
if (destination.isEmpty()) {
return;
}
FileSystemProvider::instance().startDropFiles(event, destination, this);
}
void KrFileTreeView::slotExpanded(const QModelIndex &baseIndex)
{
const QModelIndex index = mProxyModel->mapFromSource(baseIndex);
expand(index); // expand view now after model was expanded
selectionModel()->clearSelection();
selectionModel()->setCurrentIndex(index, QItemSelectionModel::SelectCurrent);
scrollTo(index);
}
void KrFileTreeView::showHeaderContextMenu()
{
QMenu popup(this);
popup.setToolTipsVisible(true);
QAction *detailAction = popup.addAction(i18n("Show Details"));
detailAction->setCheckable(true);
detailAction->setChecked(!briefMode());
detailAction->setToolTip(i18n("Show columns with details"));
QAction *showHiddenAction = popup.addAction(i18n("Show Hidden Folders"));
showHiddenAction->setCheckable(true);
showHiddenAction->setChecked(mSourceModel->dirLister()->showHiddenFiles());
showHiddenAction->setToolTip(i18n("Show folders starting with a dot"));
popup.addSeparator();
auto *rootActionGroup = new QActionGroup(this);
QAction *startFromRootAction = popup.addAction(i18n("Start From Root"));
startFromRootAction->setCheckable(true);
startFromRootAction->setChecked(!mStartTreeFromCurrent && !mStartTreeFromPlace);
startFromRootAction->setToolTip(i18n("Set root of the tree to root of filesystem"));
startFromRootAction->setActionGroup(rootActionGroup);
QAction *startFromCurrentAction = popup.addAction(i18n("Start From Current"));
startFromCurrentAction->setCheckable(true);
startFromCurrentAction->setChecked(mStartTreeFromCurrent);
startFromCurrentAction->setToolTip(i18n("Set root of the tree to the current folder"));
startFromCurrentAction->setActionGroup(rootActionGroup);
QAction *startFromPlaceAction = popup.addAction(i18n("Start From Place"));
startFromPlaceAction->setCheckable(true);
startFromPlaceAction->setChecked(mStartTreeFromPlace);
startFromPlaceAction->setToolTip(i18n("Set root of the tree to closest folder listed in 'Places'"));
startFromPlaceAction->setActionGroup(rootActionGroup);
QAction *triggeredAction = popup.exec(QCursor::pos());
if (triggeredAction == detailAction) {
setBriefMode(!detailAction->isChecked());
} else if (triggeredAction == showHiddenAction) {
KDirLister *dirLister = mSourceModel->dirLister();
dirLister->setShowHiddenFiles(showHiddenAction->isChecked());
dirLister->emitChanges();
} else if (triggeredAction && triggeredAction->actionGroup() == rootActionGroup) {
setTree(startFromCurrentAction->isChecked(), startFromPlaceAction->isChecked());
}
}
void KrFileTreeView::slotCustomContextMenuRequested(const QPoint &point)
{
const QModelIndex index = indexAt(point);
if (!index.isValid())
return;
const KFileItem fileItem = mSourceModel->itemForIndex(mProxyModel->mapToSource(index));
const KFileItemListProperties capabilities(KFileItemList() << fileItem);
auto *popup = new QMenu(this);
// TODO nice to have: "open with"
// cut/copy/paste
QAction *cutAction = new QAction(Icon(QStringLiteral("edit-cut")), i18nc("@action:inmenu", "Cut"), this);
cutAction->setEnabled(capabilities.supportsMoving());
connect(cutAction, &QAction::triggered, this, [=]() {
copyToClipBoard(fileItem, true);
});
popup->addAction(cutAction);
QAction *copyAction = new QAction(Icon(QStringLiteral("edit-copy")), i18nc("@action:inmenu", "Copy"), this);
connect(copyAction, &QAction::triggered, this, [=]() {
copyToClipBoard(fileItem, false);
});
popup->addAction(copyAction);
const QMimeData *mimeData = QApplication::clipboard()->mimeData();
bool canPaste;
const QString text = KIO::pasteActionText(mimeData, &canPaste, fileItem);
QAction *pasteAction = new QAction(Icon(QStringLiteral("edit-paste")), text, this);
connect(pasteAction, &QAction::triggered, this, [=]() {
KIO::PasteJob *job = KIO::paste(QApplication::clipboard()->mimeData(), fileItem.url());
KJobWidgets::setWindow(job, this);
});
pasteAction->setEnabled(canPaste);
popup->addAction(pasteAction);
popup->addSeparator();
// TODO nice to have: rename
// trash
if (KConfigGroup(krConfig, "General").readEntry("Move To Trash", _MoveToTrash)) {
QAction *moveToTrashAction = new QAction(Icon(QStringLiteral("user-trash")), i18nc("@action:inmenu", "Move to Trash"), this);
const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
moveToTrashAction->setEnabled(enableMoveToTrash);
connect(moveToTrashAction, &QAction::triggered, this, [=]() {
deleteFile(fileItem, true);
});
popup->addAction(moveToTrashAction);
}
// delete
QAction *deleteAction = new QAction(Icon(QStringLiteral("edit-delete")), i18nc("@action:inmenu", "Delete"), this);
deleteAction->setEnabled(capabilities.supportsDeleting());
connect(deleteAction, &QAction::triggered, this, [=]() {
deleteFile(fileItem, false);
});
popup->addAction(deleteAction);
popup->addSeparator();
// properties
if (!fileItem.isNull()) {
QAction *propertiesAction = new QAction(i18nc("@action:inmenu", "Properties"), this);
propertiesAction->setIcon(Icon(QStringLiteral("document-properties")));
connect(propertiesAction, &QAction::triggered, this, [=]() {
KPropertiesDialog *dialog = new KPropertiesDialog(fileItem.url(), this);
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->show();
});
popup->addAction(propertiesAction);
}
QPointer<QMenu> popupPtr = popup;
popup->exec(QCursor::pos());
if (popupPtr.data()) {
popupPtr.data()->deleteLater();
}
}
void KrFileTreeView::copyToClipBoard(const KFileItem &fileItem, bool cut) const
{
auto *mimeData = new QMimeData();
QList<QUrl> kdeUrls;
kdeUrls.append(fileItem.url());
QList<QUrl> mostLocalUrls;
bool dummy;
mostLocalUrls.append(fileItem.mostLocalUrl(&dummy));
KIO::setClipboardDataCut(mimeData, cut);
KUrlMimeData::setUrls(kdeUrls, mostLocalUrls, mimeData);
QApplication::clipboard()->setMimeData(mimeData);
}
void KrFileTreeView::deleteFile(const KFileItem &fileItem, bool moveToTrash) const
{
const QList<QUrl> confirmedFiles = ListPanelFunc::confirmDeletion(QList<QUrl>() << fileItem.url(), moveToTrash, false, true);
if (confirmedFiles.isEmpty())
return;
FileSystemProvider::instance().startDeleteFiles(confirmedFiles, moveToTrash);
}
bool KrFileTreeView::briefMode() const
{
return isColumnHidden(mProxyModel->columnCount() - 1); // find out by last column
}
void KrFileTreeView::setBriefMode(bool brief)
{
for (int i = 1; i < mProxyModel->columnCount(); i++) { // show only first column
setColumnHidden(i, brief);
}
}
void KrFileTreeView::setTree(bool startFromCurrent, bool startFromPlace)
{
mStartTreeFromCurrent = startFromCurrent;
mStartTreeFromPlace = startFromPlace;
if (!mStartTreeFromCurrent && !mStartTreeFromPlace) {
setTreeRoot(QUrl::fromLocalFile(QDir::root().path()));
}
setCurrentUrl(mCurrentUrl); // refresh
}
void KrFileTreeView::setTreeRoot(const QUrl &rootBase)
{
if (rootBase == mCurrentTreeBase) // avoid collapsing the subdirs in tree
return;
mCurrentTreeBase = rootBase;
mSourceModel->dirLister()->openUrl(mCurrentTreeBase);
}
void KrFileTreeView::saveSettings(KConfigGroup cfg) const
{
KConfigGroup group = KConfigGroup(&cfg, "TreeView");
group.writeEntry("BriefMode", briefMode());
group.writeEntry("ShowHiddenFolders", mSourceModel->dirLister()->showHiddenFiles());
group.writeEntry("StartFromCurrent", mStartTreeFromCurrent);
group.writeEntry("StartFromPlace", mStartTreeFromPlace);
}
void KrFileTreeView::restoreSettings(const KConfigGroup &cfg)
{
const KConfigGroup group = KConfigGroup(&cfg, "TreeView");
setBriefMode(group.readEntry("BriefMode", true));
mSourceModel->dirLister()->setShowHiddenFiles(group.readEntry("ShowHiddenFolders", false));
setTree(group.readEntry("StartFromCurrent", false), group.readEntry("StartFromPlace", false));
}
bool KrFileTreeView::isVisible(const QUrl &url)
{
QModelIndex index = indexAt(rect().topLeft());
while (index.isValid()) {
if (url == urlForProxyIndex(index)) {
return true;
}
index = indexBelow(index);
}
return false;
}
|