Skip to content

Commit

Permalink
Break out folder selection tree from kcm and reuse in filedigger.
Browse files Browse the repository at this point in the history
Needed now that KDirSelectDialog has been removed.
  • Loading branch information
spersson committed May 1, 2015
1 parent 4c6f116 commit f05c1af
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 87 deletions.
1 change: 1 addition & 0 deletions filedigger/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ restorejob.cpp
versionlistdelegate.cpp
versionlistmodel.cpp
../kioslave/vfshelpers.cpp
../kcm/dirselector.cpp
)

add_definitions(-fexceptions)
Expand Down
21 changes: 12 additions & 9 deletions filedigger/restoredialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
#include "restoredialog.h"
#include "ui_restoredialog.h"
#include "restorejob.h"
#include "../kcm/dirselector.h"

#include <KIO/RenameDialog>
#include <KIO/CopyJob>
#include <KDirSelectDialog>
#include <KDiskFreeSpaceInfo>
#include <KFilePlacesModel>
#include <KFilePlacesView>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()));
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down
5 changes: 2 additions & 3 deletions filedigger/restoredialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ namespace Ui {
class RestoreDialog;
}

//class KFileTreeView;
class KDirSelectDialog;
class DirSelector;
class KFileWidget;
class KMessageWidget;
class KWidgetJobTracker;
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions kcm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ include_directories("../daemon")

set(kcm_kup_SRCS
backupplanwidget.cpp
dirselector.cpp
driveselection.cpp
driveselectiondelegate.cpp
folderselectionmodel.cpp
Expand Down
78 changes: 12 additions & 66 deletions kcm/backupplanwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
#include "backupplanwidget.h"
#include "backupplan.h"
#include "folderselectionmodel.h"
#include "dirselector.h"
#include "driveselection.h"
#include "kbuttongroup.h"

#include <QBoxLayout>
#include <QCheckBox>
#include <QDialogButtonBox>
#include <QIcon>
#include <QInputDialog>
#include <QLabel>
#include <QPushButton>
#include <QRadioButton>
Expand All @@ -38,11 +38,8 @@
#include <KComboBox>
#include <KConfigDialogManager>
#include <KConfigGroup>
#include <KDirLister>
#include <KDirModel>
#include <KLineEdit>
#include <KLocalizedString>
#include <KMessageBox>
#include <KPageWidget>
#include <KUrlRequester>

Expand Down Expand Up @@ -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)
Expand Down
12 changes: 3 additions & 9 deletions kcm/backupplanwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
#include <QWidget>

class BackupPlan;
class DirSelector;
class DriveSelection;
class FolderSelectionModel;

class KDirModel;
class KLineEdit;
class KPageWidget;
class KPageWidgetItem;
Expand Down Expand Up @@ -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
Expand Down
96 changes: 96 additions & 0 deletions kcm/dirselector.cpp
Original file line number Diff line number Diff line change
@@ -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 <KDirLister>
#include <KDirModel>
#include <KLocalizedString>
#include <KMessageBox>

#include <QDir>
#include <QInputDialog>

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);
}

46 changes: 46 additions & 0 deletions kcm/dirselector.h
Original file line number Diff line number Diff line change
@@ -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 <QTreeView>

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

0 comments on commit f05c1af

Please sign in to comment.