diff --git a/src/blackgui/components/copyconfigurationcomponent.cpp b/src/blackgui/components/copyconfigurationcomponent.cpp index de787c995..f72dd8f51 100644 --- a/src/blackgui/components/copyconfigurationcomponent.cpp +++ b/src/blackgui/components/copyconfigurationcomponent.cpp @@ -8,7 +8,18 @@ */ #include "copyconfigurationcomponent.h" +#include "blackconfig/buildconfig.h" +#include "blackmisc/directoryutils.h" #include "ui_copyconfigurationcomponent.h" +#include "blackmisc/settingscache.h" +#include "blackmisc/datacache.h" + +#include +#include +#include + +using namespace BlackMisc; +using namespace BlackConfig; namespace BlackGui { @@ -19,9 +30,156 @@ namespace BlackGui ui(new Ui::CCopyConfigurationComponent) { ui->setupUi(this); + ui->cb_OtherVersions->clear(); + ui->cb_OtherVersions->addItems(CDirectoryUtils::swiftApplicationDataDirectoryList(true, true)); + m_versionDirs = CDirectoryUtils::swiftApplicationDataDirectoryList(true, false); // not beautified + + this->initCurrentDirectories(); + this->preselectMissingOurOutdated(); + + connect(ui->rb_Cache, &QRadioButton::toggled, this, &CCopyConfigurationComponent::initCurrentDirectories); + connect(ui->cb_OtherVersions, &QComboBox::currentTextChanged, this, &CCopyConfigurationComponent::initCurrentDirectories); } CCopyConfigurationComponent::~CCopyConfigurationComponent() { } + + void CCopyConfigurationComponent::setCacheMode() + { + ui->rb_Cache->setChecked(true); + } + + void CCopyConfigurationComponent::setSettingsMode() + { + ui->rb_Settings->setChecked(true); + } + + void CCopyConfigurationComponent::copySelectedFiles() + { + const QStringList files = this->getSelectedFiles(); + if (files.isEmpty()) { return; } + } + + void CCopyConfigurationComponent::preselectMissingOurOutdated() + { + const QString dirOther = this->getOtherVersionsSelectedDirectory(); + const QString dirCurrent = this->getThisVersionDirectory(); + + ui->tv_Destination->clearSelection(); + const CDirectoryUtils::DirComparison comp = CDirectoryUtils::compareTwoDirectories(dirOther, dirCurrent); + const QFileSystemModel *model = qobject_cast(ui->tv_Destination->model()); + + QStringList select = comp.missingInTarget.toList(); + select.append(comp.newerInSource.toList()); + for (const QString &file : as_const(comp.missingInTarget)) + { + ui->tv_Destination->setCurrentIndex(model->index(file)); + } + } + + void CCopyConfigurationComponent::initCurrentDirectories() + { + ui->le_CurrentVersion->setText(CDirectoryUtils::applicationDirectoryPath()); + this->setComboBoxWidth(); + const QString dir = this->getOtherVersionsSelectedDirectory(); + if (dir.isEmpty()) { return; } + + // source + QFileSystemModel *sourceModel = qobject_cast(ui->tv_Source->model()); + if (!sourceModel) + { + sourceModel = new QFileSystemModel(this); + sourceModel->setFilter(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs); + sourceModel->setNameFilterDisables(false); + sourceModel->setNameFilters(QStringList("*.json")); + ui->tv_Source->setModel(sourceModel); + connect(sourceModel, &QFileSystemModel::directoryLoaded, this, [ = ](const QString & path) + { + Q_UNUSED(path); + ui->tv_Source->resizeColumnToContents(0); + }); + } + const QModelIndex sourceIndex = sourceModel->setRootPath(dir); + ui->tv_Source->setRootIndex(sourceIndex); + ui->tv_Source->setSortingEnabled(true); + + // destination + QFileSystemModel *destinationModel = qobject_cast(ui->tv_Destination->model()); + if (!destinationModel) + { + destinationModel = new QFileSystemModel(this); + destinationModel->setFilter(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs); + destinationModel->setNameFilterDisables(false); + destinationModel->setNameFilters(QStringList("*.json")); + ui->tv_Destination->setModel(destinationModel); + connect(destinationModel, &QFileSystemModel::directoryLoaded, this, [ = ](const QString & path) + { + Q_UNUSED(path); + ui->tv_Destination->resizeColumnToContents(0); + }); + } + 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::currentVersionChanged(const QString &text) + { + Q_UNUSED(text); + this->initCurrentDirectories(); + } + + const QString &CCopyConfigurationComponent::getThisVersionDirectory() const + { + return ui->rb_Cache->isChecked() ? CDataCache::persistentStore() : CSettingsCache::persistentStore(); + } + + QString CCopyConfigurationComponent::getOtherVersionsSelectedDirectory() const + { + const QString s = m_versionDirs.at(ui->cb_OtherVersions->currentIndex()); + const QFileInfoList dirs(CDirectoryUtils::swiftApplicationDataDirectories()); + if (dirs.isEmpty()) { return ""; } + QString dir; + for (const QFileInfo &info : dirs) + { + if (info.absoluteFilePath().contains(s)) + { + dir = info.absoluteFilePath(); + break; + } + } + if (dir.isEmpty()) { return ""; } + dir = CFileUtils::appendFilePaths(dir, ui->rb_Cache->isChecked() ? + CDataCache::relativeFilePath() : + CSettingsCache::relativeFilePath()); + return dir; + } + + QStringList CCopyConfigurationComponent::getSelectedFiles() const + { + const QModelIndexList indexes = ui->tv_Source->selectionModel()->selectedIndexes(); + if (indexes.isEmpty()) { return QStringList(); } + const QFileSystemModel *sourceModel = qobject_cast(ui->tv_Source->model()); + + QStringList files; + for (const QModelIndex &index : indexes) + { + if (!index.isValid()) continue; + const QString file = sourceModel->filePath(index); + if (!files.contains(file)) + { + files.push_back(file); + } + } + return files; + } + + void CCopyConfigurationComponent::setComboBoxWidth() + { + const int width = this->width() * 0.45; + ui->cb_OtherVersions->setFixedWidth(width); + } } // ns } // ns diff --git a/src/blackgui/components/copyconfigurationcomponent.h b/src/blackgui/components/copyconfigurationcomponent.h index 270d56a01..4fc4b87b3 100644 --- a/src/blackgui/components/copyconfigurationcomponent.h +++ b/src/blackgui/components/copyconfigurationcomponent.h @@ -14,6 +14,7 @@ #include "blackgui/blackguiexport.h" #include +#include namespace Ui { class CCopyConfigurationComponent; } namespace BlackGui @@ -21,7 +22,7 @@ namespace BlackGui namespace Components { /** - * Copy configuration (ie settings and cache files) + * Copy configuration (i.e. settings and cache files) */ class BLACKGUI_EXPORT CCopyConfigurationComponent : public QFrame { @@ -34,7 +35,38 @@ namespace BlackGui //! Destructor virtual ~CCopyConfigurationComponent(); + //! Cache mode + void setCacheMode(); + + //! Settings mode + void setSettingsMode(); + + //! Selected files are copied + void copySelectedFiles(); + + //! Preselect newer files + void preselectMissingOurOutdated(); + + //! Init file content + void initCurrentDirectories(); + private: + //! The current version changed + void currentVersionChanged(const QString &text); + + //! This version's directory (cache or setting) + const QString &getThisVersionDirectory() const; + + //! Get the selected directory + QString getOtherVersionsSelectedDirectory() const; + + //! Get the selected files + QStringList getSelectedFiles() const; + + //! Combobox width + void setComboBoxWidth(); + + QStringList m_versionDirs; QScopedPointer ui; }; } // ns diff --git a/src/blackgui/components/copyconfigurationcomponent.ui b/src/blackgui/components/copyconfigurationcomponent.ui index 6035c2e12..965d310dc 100644 --- a/src/blackgui/components/copyconfigurationcomponent.ui +++ b/src/blackgui/components/copyconfigurationcomponent.ui @@ -20,32 +20,110 @@ QFrame::Raised - - - - Destination - - - - - - - - - + - Source + Source (other versions) - + + + true + + + QAbstractItemView::MultiSelection + + + true + + + true + + - - + + + + Destination (this version) + + + + + + true + + + QAbstractItemView::NoSelection + + + true + + + true + + + + + + + + + + true + + + + + + + + + + + 0 + + + 0 + + + 0 + + + + + Settings + + + true + + + + + + + Cache + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + +