From f05c1af767dbe96d003ca7d82d676acf1c4c31c7 Mon Sep 17 00:00:00 2001 From: Simon Persson Date: Fri, 1 May 2015 18:44:32 +0800 Subject: [PATCH] Break out folder selection tree from kcm and reuse in filedigger. Needed now that KDirSelectDialog has been removed. --- filedigger/CMakeLists.txt | 1 + filedigger/restoredialog.cpp | 21 ++++---- filedigger/restoredialog.h | 5 +- kcm/CMakeLists.txt | 1 + kcm/backupplanwidget.cpp | 78 +++++------------------------ kcm/backupplanwidget.h | 12 ++--- kcm/dirselector.cpp | 96 ++++++++++++++++++++++++++++++++++++ kcm/dirselector.h | 46 +++++++++++++++++ 8 files changed, 173 insertions(+), 87 deletions(-) create mode 100644 kcm/dirselector.cpp create mode 100644 kcm/dirselector.h diff --git a/filedigger/CMakeLists.txt b/filedigger/CMakeLists.txt index fecf900..de324c8 100644 --- a/filedigger/CMakeLists.txt +++ b/filedigger/CMakeLists.txt @@ -11,6 +11,7 @@ restorejob.cpp versionlistdelegate.cpp versionlistmodel.cpp ../kioslave/vfshelpers.cpp +../kcm/dirselector.cpp ) add_definitions(-fexceptions) diff --git a/filedigger/restoredialog.cpp b/filedigger/restoredialog.cpp index d7bc435..578da4a 100644 --- a/filedigger/restoredialog.cpp +++ b/filedigger/restoredialog.cpp @@ -21,10 +21,10 @@ #include "restoredialog.h" #include "ui_restoredialog.h" #include "restorejob.h" +#include "../kcm/dirselector.h" #include #include -#include #include #include #include @@ -55,7 +55,7 @@ RestoreDialog::RestoreDialog(const BupSourceInfo &pPathInfo, QWidget *parent) mUI->setupUi(this); mFileWidget = NULL; - mDirDialog = NULL; + mDirSelector = NULL; mJobTracker = NULL; mUI->mRestoreOriginalButton->setMinimumHeight(mUI->mRestoreOriginalButton->sizeHint().height() * 2); @@ -107,10 +107,13 @@ void RestoreDialog::setOriginalDestination() { } void RestoreDialog::setCustomDestination() { - if(mSourceInfo.mIsDirectory && mDirDialog == NULL) { - mDirDialog = new KDirSelectDialog(mSourceInfo.mPathInRepo, true, this); - mDirDialog->setButtons(0); - mUI->mDestinationVLayout->insertWidget(0, mDirDialog); + if(mSourceInfo.mIsDirectory && mDirSelector == NULL) { + mDirSelector = new DirSelector(this); + mDirSelector->setRootUrl(QUrl::fromLocalFile(QStringLiteral("/"))); + QString lDirPath = mSourceInfo.mPathInRepo.section(QDir::separator(), 0, -2); + mDirSelector->expandToUrl(QUrl::fromLocalFile(lDirPath)); + mUI->mDestinationVLayout->insertWidget(0, mDirSelector); + QPushButton *lNewFolderButton = new QPushButton(QIcon::fromTheme(QLatin1String("folder-new")), i18nc("@action:button","New Folder...")); connect(lNewFolderButton, SIGNAL(clicked()), SLOT(createNewFolder())); @@ -134,7 +137,7 @@ void RestoreDialog::setCustomDestination() { void RestoreDialog::checkDestinationSelection() { if(mSourceInfo.mIsDirectory) { - QUrl lUrl = mDirDialog->url(); + QUrl lUrl = mDirSelector->url(); if(!lUrl.isEmpty()) { mDestination.setFile(lUrl.path()); startPrechecks(); @@ -369,7 +372,7 @@ void RestoreDialog::folderMoveCompleted(KJob *pJob) { void RestoreDialog::createNewFolder() { bool lUserAccepted; - QUrl lUrl = mDirDialog->url(); + QUrl lUrl = mDirSelector->url(); QString lNameSuggestion = i18nc("default folder name when creating a new folder", "New Folder"); if(QFileInfo(lUrl.path(QUrl::AddTrailingSlash) + lNameSuggestion).exists()) { lNameSuggestion = KIO::RenameDialog::suggestName(lUrl, lNameSuggestion); @@ -400,7 +403,7 @@ void RestoreDialog::createNewFolder() { lPartialUrl = lPartialUrl.adjusted(QUrl::StripTrailingSlash); lPartialUrl.setPath(lPartialUrl.path() + '/' + (lSubDirectory)); } - mDirDialog->setCurrentUrl(lPartialUrl); + mDirSelector->expandToUrl(lPartialUrl); } void RestoreDialog::openDestinationFolder() { diff --git a/filedigger/restoredialog.h b/filedigger/restoredialog.h index 8724707..34334fd 100644 --- a/filedigger/restoredialog.h +++ b/filedigger/restoredialog.h @@ -31,8 +31,7 @@ namespace Ui { class RestoreDialog; } -//class KFileTreeView; -class KDirSelectDialog; +class DirSelector; class KFileWidget; class KMessageWidget; class KWidgetJobTracker; @@ -72,7 +71,7 @@ protected slots: void moveFolder(); Ui::RestoreDialog *mUI; KFileWidget *mFileWidget; - KDirSelectDialog *mDirDialog; + DirSelector *mDirSelector; QFileInfo mDestination; QFileInfo mFolderToCreate; QString mRestorationPath; // not neccesarily same as destination diff --git a/kcm/CMakeLists.txt b/kcm/CMakeLists.txt index 67b0f58..c4fec91 100644 --- a/kcm/CMakeLists.txt +++ b/kcm/CMakeLists.txt @@ -4,6 +4,7 @@ include_directories("../daemon") set(kcm_kup_SRCS backupplanwidget.cpp +dirselector.cpp driveselection.cpp driveselectiondelegate.cpp folderselectionmodel.cpp diff --git a/kcm/backupplanwidget.cpp b/kcm/backupplanwidget.cpp index dc571fb..36cf054 100644 --- a/kcm/backupplanwidget.cpp +++ b/kcm/backupplanwidget.cpp @@ -21,6 +21,7 @@ #include "backupplanwidget.h" #include "backupplan.h" #include "folderselectionmodel.h" +#include "dirselector.h" #include "driveselection.h" #include "kbuttongroup.h" @@ -28,7 +29,6 @@ #include #include #include -#include #include #include #include @@ -38,11 +38,8 @@ #include #include #include -#include -#include #include #include -#include #include #include @@ -145,86 +142,35 @@ DirDialog::DirDialog(const QUrl &pRootDir, const QString &pStartSubDir, QWidget : QDialog(pParent) { setWindowTitle(i18nc("@title:window","Select Folder")); + + mDirSelector = new DirSelector(this); + mDirSelector->setRootUrl(pRootDir); + QUrl lSubUrl = QUrl::fromLocalFile(pRootDir.adjusted(QUrl::StripTrailingSlash).path() + '/' + + pStartSubDir); + mDirSelector->expandToUrl(lSubUrl); + QDialogButtonBox *lButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel); connect(lButtonBox, SIGNAL(accepted()), this, SLOT(accept())); connect(lButtonBox, SIGNAL(rejected()), this, SLOT(reject())); QPushButton *lNewFolderButton = new QPushButton(i18nc("@action:button", "New Folder...")); - connect(lNewFolderButton, SIGNAL(clicked()), this, SLOT(createNewFolder())); + connect(lNewFolderButton, SIGNAL(clicked()), mDirSelector, SLOT(createNewFolder())); lButtonBox->addButton(lNewFolderButton, QDialogButtonBox::ActionRole); QPushButton *lOkButton = lButtonBox->button(QDialogButtonBox::Ok); lOkButton->setDefault(true); lOkButton->setShortcut(Qt::Key_Return); - - mTreeView = new QTreeView(this); - mDirModel = new KDirModel(this); - mDirModel->dirLister()->setDirOnlyMode(true); - mTreeView->setModel(mDirModel); - mTreeView->setContextMenuPolicy(Qt::CustomContextMenu); - for (int i = 1; i < mDirModel->columnCount(); ++i) { - mTreeView->hideColumn(i); - } - mTreeView->setHeaderHidden(true); - connect(mDirModel, SIGNAL(expand(QModelIndex)), mTreeView, SLOT(expand(QModelIndex))); - connect(mDirModel, SIGNAL(expand(QModelIndex)), SLOT(selectEntry(QModelIndex))); - QVBoxLayout *lMainLayout = new QVBoxLayout; - lMainLayout->addWidget(mTreeView); + lMainLayout->addWidget(mDirSelector); lMainLayout->addWidget(lButtonBox); setLayout(lMainLayout); - mDirModel->dirLister()->openUrl(pRootDir); - QUrl lSubUrl = QUrl::fromLocalFile(pRootDir.adjusted(QUrl::StripTrailingSlash).path() + '/' + - pStartSubDir); - mDirModel->expandToUrl(lSubUrl); - mTreeView->setFocus(); + mDirSelector->setFocus(); } QUrl DirDialog::url() const { - const KFileItem lFileItem = mDirModel->itemForIndex(mTreeView->currentIndex()); - return !lFileItem.isNull() ? lFileItem.url() : QUrl(); + return mDirSelector->url(); } -void DirDialog::createNewFolder() { - bool lUserAccepted; - QString lNameSuggestion = i18nc("default folder name when creating a new folder", "New Folder"); - if(QFileInfo(url().adjusted(QUrl::StripTrailingSlash).path() + '/' + lNameSuggestion).exists()) { - lNameSuggestion = KIO::suggestName(url(), lNameSuggestion); - } - - QString lSelectedName = QInputDialog::getText(this, i18nc("@title:window", "New Folder" ), - i18nc("@label:textbox", "Create new folder in:\n%1", url().path()), - QLineEdit::Normal, lNameSuggestion, &lUserAccepted); - if (!lUserAccepted) - return; - - QUrl lPartialUrl(url()); - const QStringList lDirectories = lSelectedName.split(QLatin1Char('/'), QString::SkipEmptyParts); - foreach(QString lSubDirectory, lDirectories) { - QDir lDir(lPartialUrl.path()); - if(lDir.exists(lSubDirectory)) { - lPartialUrl = lPartialUrl.adjusted(QUrl::StripTrailingSlash); - lPartialUrl.setPath(lPartialUrl.path() + '/' + (lSubDirectory)); - KMessageBox::sorry(this, i18n("A folder named %1 already exists.", lPartialUrl.path())); - return; - } - if(!lDir.mkdir(lSubDirectory)) { - lPartialUrl = lPartialUrl.adjusted(QUrl::StripTrailingSlash); - lPartialUrl.setPath(lPartialUrl.path() + '/' + (lSubDirectory)); - KMessageBox::sorry(this, i18n("You do not have permission to create %1.", lPartialUrl.path())); - return; - } - lPartialUrl = lPartialUrl.adjusted(QUrl::StripTrailingSlash); - lPartialUrl.setPath(lPartialUrl.path() + '/' + (lSubDirectory)); - } - mDirModel->expandToUrl(lPartialUrl); -} - -void DirDialog::selectEntry(QModelIndex pIndex) { - mTreeView->selectionModel()->clearSelection(); - mTreeView->selectionModel()->setCurrentIndex(pIndex, QItemSelectionModel::SelectCurrent); - mTreeView->scrollTo(pIndex); -} BackupPlanWidget::BackupPlanWidget(BackupPlan *pBackupPlan, const QString &pBupVersion, const QString &pRsyncVersion, bool pPar2Available) diff --git a/kcm/backupplanwidget.h b/kcm/backupplanwidget.h index 74fba34..b1aa735 100644 --- a/kcm/backupplanwidget.h +++ b/kcm/backupplanwidget.h @@ -26,10 +26,10 @@ #include class BackupPlan; +class DirSelector; class DriveSelection; class FolderSelectionModel; -class KDirModel; class KLineEdit; class KPageWidget; class KPageWidgetItem; @@ -82,14 +82,8 @@ class DirDialog: public QDialog public: explicit DirDialog(const QUrl &pRootDir, const QString &pStartSubDir, QWidget *pParent = NULL); QUrl url() const; - -public slots: - void createNewFolder(); - void selectEntry(QModelIndex pIndex); - -protected: - QTreeView *mTreeView; - KDirModel *mDirModel; +private: + DirSelector *mDirSelector; }; class BackupPlanWidget : public QWidget diff --git a/kcm/dirselector.cpp b/kcm/dirselector.cpp new file mode 100644 index 0000000..3a75976 --- /dev/null +++ b/kcm/dirselector.cpp @@ -0,0 +1,96 @@ +/*************************************************************************** + * Copyright Simon Persson * + * simonpersson1@gmail.com * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include "dirselector.h" + +#include +#include +#include +#include + +#include +#include + +DirSelector::DirSelector(QWidget *pParent) : QTreeView(pParent) { + mDirModel = new KDirModel(this); + mDirModel->dirLister()->setDirOnlyMode(true); + setModel(mDirModel); + for (int i = 1; i < mDirModel->columnCount(); ++i) { + hideColumn(i); + } + setHeaderHidden(true); + connect(mDirModel, SIGNAL(expand(QModelIndex)), SLOT(expand(QModelIndex))); + connect(mDirModel, SIGNAL(expand(QModelIndex)), SLOT(selectEntry(QModelIndex))); +} + +QUrl DirSelector::url() const { + const KFileItem lFileItem = mDirModel->itemForIndex(currentIndex()); + return !lFileItem.isNull() ? lFileItem.url() : QUrl(); +} + +void DirSelector::createNewFolder() { + bool lUserAccepted; + QString lNameSuggestion = i18nc("default folder name when creating a new folder", "New Folder"); + if(QFileInfo(url().adjusted(QUrl::StripTrailingSlash).path() + '/' + lNameSuggestion).exists()) { + lNameSuggestion = KIO::suggestName(url(), lNameSuggestion); + } + + QString lSelectedName = QInputDialog::getText(this, i18nc("@title:window", "New Folder" ), + i18nc("@label:textbox", "Create new folder in:\n%1", url().path()), + QLineEdit::Normal, lNameSuggestion, &lUserAccepted); + if (!lUserAccepted) + return; + + QUrl lPartialUrl(url()); + const QStringList lDirectories = lSelectedName.split(QLatin1Char('/'), QString::SkipEmptyParts); + foreach(QString lSubDirectory, lDirectories) { + QDir lDir(lPartialUrl.path()); + if(lDir.exists(lSubDirectory)) { + lPartialUrl = lPartialUrl.adjusted(QUrl::StripTrailingSlash); + lPartialUrl.setPath(lPartialUrl.path() + '/' + (lSubDirectory)); + KMessageBox::sorry(this, i18n("A folder named %1 already exists.", lPartialUrl.path())); + return; + } + if(!lDir.mkdir(lSubDirectory)) { + lPartialUrl = lPartialUrl.adjusted(QUrl::StripTrailingSlash); + lPartialUrl.setPath(lPartialUrl.path() + '/' + (lSubDirectory)); + KMessageBox::sorry(this, i18n("You do not have permission to create %1.", lPartialUrl.path())); + return; + } + lPartialUrl = lPartialUrl.adjusted(QUrl::StripTrailingSlash); + lPartialUrl.setPath(lPartialUrl.path() + '/' + (lSubDirectory)); + } + mDirModel->expandToUrl(lPartialUrl); +} + +void DirSelector::selectEntry(QModelIndex pIndex) { + selectionModel()->clearSelection(); + selectionModel()->setCurrentIndex(pIndex, QItemSelectionModel::SelectCurrent); + scrollTo(pIndex); +} + +void DirSelector::expandToUrl(const QUrl &pUrl) { + mDirModel->expandToUrl(pUrl); +} + +void DirSelector::setRootUrl(const QUrl &pUrl) { + mDirModel->dirLister()->openUrl(pUrl); +} + diff --git a/kcm/dirselector.h b/kcm/dirselector.h new file mode 100644 index 0000000..c0162d8 --- /dev/null +++ b/kcm/dirselector.h @@ -0,0 +1,46 @@ +/*************************************************************************** + * Copyright Simon Persson * + * simonpersson1@gmail.com * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifndef DIRSELECTOR_H +#define DIRSELECTOR_H + +#include + +class KDirModel; + +class DirSelector : public QTreeView +{ + Q_OBJECT +public: + explicit DirSelector(QWidget *pParent = 0); + QUrl url() const; + +signals: + +public slots: + void createNewFolder(); + void selectEntry(QModelIndex pIndex); + void expandToUrl(const QUrl &pUrl); + void setRootUrl(const QUrl &pUrl); +private: + KDirModel *mDirModel; +}; + +#endif // DIRSELECTOR_H