diff --git a/src/blackgui/components/configurationwizard.cpp b/src/blackgui/components/configurationwizard.cpp index deb77eb38..b274afe27 100644 --- a/src/blackgui/components/configurationwizard.cpp +++ b/src/blackgui/components/configurationwizard.cpp @@ -46,12 +46,12 @@ namespace BlackGui if (page == ui->wp_CopyCaches) { ui->comp_CopyCaches->setCacheMode(); - ui->comp_CopyCaches->initAndPreselectDirectories(); + ui->comp_CopyCaches->initCurrentDirectories(true); } else if (page == ui->wp_CopySettings) { ui->comp_CopySettings->setSettingsMode(); - ui->comp_CopySettings->initAndPreselectDirectories(); + ui->comp_CopySettings->initCurrentDirectories(true); } } } // ns diff --git a/src/blackgui/components/copyconfigurationcomponent.cpp b/src/blackgui/components/copyconfigurationcomponent.cpp index 20b1c03df..6fd2af7d5 100644 --- a/src/blackgui/components/copyconfigurationcomponent.cpp +++ b/src/blackgui/components/copyconfigurationcomponent.cpp @@ -7,10 +7,11 @@ * contained in the LICENSE file. */ +#include "ui_copyconfigurationcomponent.h" #include "copyconfigurationcomponent.h" +#include "configurationwizard.h" #include "blackconfig/buildconfig.h" #include "blackmisc/directoryutils.h" -#include "ui_copyconfigurationcomponent.h" #include "blackmisc/settingscache.h" #include "blackmisc/datacache.h" @@ -34,8 +35,8 @@ namespace BlackGui ui->cb_OtherVersions->addItems(CDirectoryUtils::swiftApplicationDataDirectoryList(true, true)); m_otherVersionDirs = CDirectoryUtils::swiftApplicationDataDirectoryList(true, false); // not beautified - connect(ui->rb_Cache, &QRadioButton::toggled, this, &CCopyConfigurationComponent::initCurrentDirectories); - connect(ui->cb_OtherVersions, &QComboBox::currentTextChanged, this, &CCopyConfigurationComponent::initCurrentDirectories); + connect(ui->rb_Cache, &QRadioButton::toggled, [ = ](bool) { this->initCurrentDirectories(true); }); + connect(ui->cb_OtherVersions, &QComboBox::currentTextChanged, [ = ] { this->initCurrentDirectories(true); }); connect(ui->pb_SelectAll, &QPushButton::clicked, ui->tv_Source, &QTreeView::selectAll); connect(ui->pb_ClearSelection, &QPushButton::clicked, ui->tv_Source, &QTreeView::clearSelection); connect(ui->pb_CopyOver, &QPushButton::clicked, this, &CCopyConfigurationComponent::copySelectedFiles); @@ -60,22 +61,31 @@ namespace BlackGui if (files.isEmpty()) { return 0; } const QString destinationDir = this->getThisVersionDirectory(); + const QString sourceDir = this->getOtherVersionsSelectedDirectory(); if (destinationDir.isEmpty()) { return 0; } - const QDir d(destinationDir); - if (!d.exists()) { return 0; } + const QDir source(sourceDir); + const QDir destination(destinationDir); + if (!destination.exists()) { return 0; } int c = 0; for (const QString &file : files) { - const QFileInfo fileInfo(file); - const QString target = CFileUtils::appendFilePaths(destinationDir, fileInfo.fileName()); + const QString relativePath = source.relativeFilePath(file); + const QString target = CFileUtils::appendFilePaths(destinationDir, relativePath); + if (relativePath.contains('/')) + { + const QString targetDir = CFileUtils::stripFileFromPath(target); + const bool dirOk = destination.mkpath(targetDir); + if (!dirOk) { continue; } + } + QFile::remove(target); // copy does not overwrite const bool s = QFile::copy(file, target); if (s) { c++; } } return c; } - void CCopyConfigurationComponent::preselectMissingOurOutdated() + void CCopyConfigurationComponent::preselectMissingOrOutdated() { const QString dirOther = this->getOtherVersionsSelectedDirectory(); const QString dirCurrent = this->getThisVersionDirectory(); @@ -83,7 +93,7 @@ namespace BlackGui ui->tv_Source->clearSelection(); ui->tv_Destination->clearSelection(); - const CDirectoryUtils::DirComparison comp = CDirectoryUtils::compareTwoDirectories(dirOther, dirCurrent); + const CDirectoryUtils::DirComparison comp = CDirectoryUtils::compareTwoDirectories(dirOther, dirCurrent, true); const QFileSystemModel *sourceModel = qobject_cast(ui->tv_Source->model()); if (!sourceModel) { return; } @@ -97,11 +107,20 @@ namespace BlackGui } } - void CCopyConfigurationComponent::initCurrentDirectories() + void CCopyConfigurationComponent::initCurrentDirectories(bool preselectMissingOrOutdated) { - ui->le_CurrentVersion->setText(CDirectoryUtils::applicationDirectoryPath()); - this->setComboBoxWidth(); - const QString dir = this->getOtherVersionsSelectedDirectory(); + const QString thisVersionDir = this->getThisVersionDirectory(); // cache or settings dir + const QDir thisVersionDirectory(thisVersionDir); + if (!thisVersionDirectory.exists()) + { + const bool hasDir = thisVersionDirectory.mkpath(thisVersionDir); + if (!hasDir) + { + ui->le_CurrentVersion->setText("No swift target dir"); + return; + } + } + ui->le_CurrentVersion->setText(thisVersionDir); // source QFileSystemModel *sourceModel = qobject_cast(ui->tv_Source->model()); @@ -116,13 +135,22 @@ namespace BlackGui { Q_UNUSED(path); ui->tv_Source->resizeColumnToContents(0); + ui->tv_Source->expandAll(); + if (preselectMissingOrOutdated) + { + this->preselectMissingOrOutdated(); + } }); } + else + { + this->preselectMissingOrOutdated(); + } + const QString dir = this->getOtherVersionsSelectedDirectory(); const QModelIndex sourceIndex = sourceModel->setRootPath(dir); ui->tv_Source->setRootIndex(sourceIndex); ui->tv_Source->setSortingEnabled(true); // hide/disable only - ui->tv_Source->resizeColumnToContents(0); // destination QFileSystemModel *destinationModel = qobject_cast(ui->tv_Destination->model()); @@ -137,19 +165,13 @@ namespace BlackGui { Q_UNUSED(path); ui->tv_Destination->resizeColumnToContents(0); + ui->tv_Destination->expandAll(); }); } const QString destinationDir = this->getThisVersionDirectory(); const QModelIndex destinationIndex = destinationModel->setRootPath(destinationDir); ui->tv_Destination->setRootIndex(destinationIndex); ui->tv_Destination->setSortingEnabled(true); - ui->tv_Destination->resizeColumnToContents(0); - } - - void CCopyConfigurationComponent::initAndPreselectDirectories() - { - this->initCurrentDirectories(); - this->preselectMissingOurOutdated(); } bool CCopyConfigurationComponent::hasOtherVersionData() const @@ -173,11 +195,11 @@ namespace BlackGui if (ui->cb_OtherVersions->count() < 1) { return ""; } const QFileInfoList dirs(CDirectoryUtils::swiftApplicationDataDirectories()); if (dirs.isEmpty()) { return ""; } - const QString s = m_otherVersionDirs.at(ui->cb_OtherVersions->currentIndex()); + const QString otherVersionDir = m_otherVersionDirs.at(ui->cb_OtherVersions->currentIndex()); QString dir; for (const QFileInfo &info : dirs) { - if (info.absoluteFilePath().contains(s)) + if (info.absoluteFilePath().contains(otherVersionDir)) { dir = info.absoluteFilePath(); break; @@ -209,21 +231,19 @@ namespace BlackGui return files; } - void CCopyConfigurationComponent::setComboBoxWidth() - { - const int width = this->width() * 0.45; - ui->cb_OtherVersions->setFixedWidth(width); - } - void CCopyConfigurationWizardPage::initializePage() { Q_ASSERT_X(m_config, Q_FUNC_INFO, "Missing config"); - m_config->initCurrentDirectories(); + m_config->initCurrentDirectories(true); } bool CCopyConfigurationWizardPage::validatePage() { + CConfigurationWizard *wizard = qobject_cast(this->wizard()); Q_ASSERT_X(m_config, Q_FUNC_INFO, "Missing config"); + Q_ASSERT_X(wizard, Q_FUNC_INFO, "No wizard"); + + if (wizard->lastStepSkipped()) { return true; } m_config->copySelectedFiles(); return true; } diff --git a/src/blackgui/components/copyconfigurationcomponent.h b/src/blackgui/components/copyconfigurationcomponent.h index d03f85b0b..3e2364b46 100644 --- a/src/blackgui/components/copyconfigurationcomponent.h +++ b/src/blackgui/components/copyconfigurationcomponent.h @@ -46,13 +46,10 @@ namespace BlackGui int copySelectedFiles(); //! Preselect newer files - void preselectMissingOurOutdated(); + void preselectMissingOrOutdated(); //! Init file content - void initCurrentDirectories(); - - //! Init and preselect directories - void initAndPreselectDirectories(); + void initCurrentDirectories(bool preselectMissingOrOutdated = false); //! Are there other versions to copy from bool hasOtherVersionData() const; @@ -70,9 +67,6 @@ namespace BlackGui //! Get the selected files QStringList getSelectedFiles() const; - //! Set calculated combobox width - void setComboBoxWidth(); - QStringList m_otherVersionDirs; QScopedPointer ui; }; diff --git a/src/blackgui/components/copyconfigurationcomponent.ui b/src/blackgui/components/copyconfigurationcomponent.ui index 04e83405a..082d64d8d 100644 --- a/src/blackgui/components/copyconfigurationcomponent.ui +++ b/src/blackgui/components/copyconfigurationcomponent.ui @@ -20,66 +20,112 @@ QFrame::Raised - - - - Source (other versions) - - + + + + + 0 + + + 0 + + + 0 + + + 0 + - - - true + + + Source (other versions) - - QAbstractItemView::MultiSelection - - - true - - - true + + + 4 + + + 4 + + + 4 + + + 4 + + + + + false + + + + + + + true + + + QAbstractItemView::MultiSelection + + + true + + + true + + + + + + + + + + Destination (this version) + + + 4 + + + 4 + + + 4 + + + 4 + + + + + true + + + + + + + true + + + QAbstractItemView::NoSelection + + + true + + + true + + + + - - - - Destination (this version) - - - - - - true - - - QAbstractItemView::NoSelection - - - true - - - true - - - - - - - - - - true - - - - - - diff --git a/src/blackmisc/fileutils.cpp b/src/blackmisc/fileutils.cpp index 36458006a..9f47e7272 100644 --- a/src/blackmisc/fileutils.cpp +++ b/src/blackmisc/fileutils.cpp @@ -101,6 +101,13 @@ namespace BlackMisc return QDir::cleanPath(path1 + QChar('/') + path2); } + QString CFileUtils::stripFileFromPath(const QString &path) + { + if (path.endsWith('/')) { return path; } + if (!path.contains('/')) { return path; } + return path.left(path.lastIndexOf('/')); + } + QString CFileUtils::appendFilePaths(const QString &path1, const QString &path2, const QString &path3) { return CFileUtils::appendFilePaths(CFileUtils::appendFilePaths(path1, path2), path3); diff --git a/src/blackmisc/fileutils.h b/src/blackmisc/fileutils.h index 448871320..e305da0e9 100644 --- a/src/blackmisc/fileutils.h +++ b/src/blackmisc/fileutils.h @@ -63,6 +63,9 @@ namespace BlackMisc //! \sa CNetworkUtils::buildUrl for URLs static QString appendFilePaths(const QString &path1, const QString &path2); + //! Strip file from path a/b/c.json a/b + static QString stripFileFromPath(const QString &path); + //! Append file paths //! \sa CNetworkUtils::buildUrl for URLs static QString appendFilePaths(const QString &path1, const QString &path2, const QString &path3);