From 737ded4b7dde6e336a427f28d09b5d053f22f407 Mon Sep 17 00:00:00 2001 From: Svetlana Abramenkova Date: Wed, 12 Dec 2018 16:42:25 +0100 Subject: Bench: QmlLive project document stores path as relative paths The JSON project format now stores the import path and workspace folder as relative paths to the project document Change-Id: I72d71be59fd3887f244e17b9f963771c02804f1f Fixes: QTAUTO-1107 Reviewed-by: Nikolay Zamotaev --- src/bench/mainwindow.cpp | 7 +-- src/bench/newprojectwizard.cpp | 105 +++++++++++++++++++++++++++++++++++++---- src/bench/newprojectwizard.h | 21 +++++++++ src/projectmanager.cpp | 21 +++++---- src/projectmanager.h | 2 + 5 files changed, 136 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/bench/mainwindow.cpp b/src/bench/mainwindow.cpp index 982d962..960dcab 100644 --- a/src/bench/mainwindow.cpp +++ b/src/bench/mainwindow.cpp @@ -641,7 +641,8 @@ void MainWindow::openProject() s.endArray(); setImportPaths(paths); - setWorkspace(m_projectManager->workspace()); + QString path = QDir(m_projectManager->projectLocation()).absoluteFilePath(m_projectManager->workspace()); + setWorkspace(path); activateDocument(LiveDocument(m_projectManager->mainDocument())); } } @@ -661,11 +662,11 @@ void MainWindow::newProject() m_projectManager->setImports(m_newProjectWizard->imports()); m_projectManager->setMainDocument(m_newProjectWizard->mainDocument()); m_projectManager->setWorkspace(m_newProjectWizard->workspace()); - m_projectManager->create(m_newProjectWizard->projectName()); setImportPaths(m_newProjectWizard->imports()); - setWorkspace(m_newProjectWizard->workspace()); + QString path = QDir(m_projectManager->projectLocation()).absoluteFilePath(m_newProjectWizard->workspace()); + setWorkspace(path); activateDocument(LiveDocument(m_newProjectWizard->mainDocument())); } diff --git a/src/bench/newprojectwizard.cpp b/src/bench/newprojectwizard.cpp index f2fcbbc..61967d1 100644 --- a/src/bench/newprojectwizard.cpp +++ b/src/bench/newprojectwizard.cpp @@ -37,12 +37,15 @@ #include #include "livedocument.h" +const QString QmlLiveProjectsLocation(QDir(QDir().homePath()).absoluteFilePath("QMLLive")); + NewProjectWizard::NewProjectWizard(QWidget *parent) : QWizard(parent) , m_importListWidget(nullptr) , m_projectPage(new ProjectPage()) , m_workspacePage(new WorkspacePage()) , m_mainDocumentPage(new MainDocumentPage()) + , m_projectFileDir(nullptr) { setWizardStyle(QWizard::ClassicStyle); setOptions(QWizard::NoBackButtonOnStartPage); @@ -50,10 +53,16 @@ NewProjectWizard::NewProjectWizard(QWidget *parent) addPage(m_workspacePage); addPage(createImportsPage()); addPage(m_mainDocumentPage); + + connect(m_projectPage, &ProjectPage::updateProjectDir, m_workspacePage, &WorkspacePage::onUpdateProjectDir); + connect(m_projectPage, &ProjectPage::updateProjectDir, m_mainDocumentPage, &MainDocumentPage::onUpdateProjectDir); + connect(m_projectPage, &ProjectPage::updateProjectDir, this, &NewProjectWizard::onUpdateProjectDir); } MainDocumentPage::MainDocumentPage(QWidget *parent) : QWizardPage (parent) + , m_mainDocumentField(new QLineEdit) + , m_projectFileDir(nullptr) { setTitle("Main Document"); QGridLayout *layout = new QGridLayout; @@ -61,7 +70,6 @@ MainDocumentPage::MainDocumentPage(QWidget *parent) QLabel *label = new QLabel("Main document: "); layout->addWidget(label, 0, 0); - m_mainDocumentField = new QLineEdit; registerField("mainDocument*", m_mainDocumentField); layout->addWidget(m_mainDocumentField, 0, 1); @@ -79,16 +87,29 @@ QString MainDocumentPage::mainDocument() const return ""; } +void MainDocumentPage::onUpdateProjectDir(const QString &path) +{ + if (m_projectFileDir == nullptr){ + m_projectFileDir = new QDir(path); + } + else { + m_projectFileDir->setPath(path); + } +} + WorkspacePage::WorkspacePage(QWidget *parent) : QWizardPage (parent) + , m_workspaceField(new QLineEdit) + , m_warningLabel(new QLabel) + , m_projectFileDir(nullptr) { setTitle("Select Workspace"); + setSubTitle("The path should be relative to the project file location."); QGridLayout *layout = new QGridLayout; QLabel *label = new QLabel("Workspace: "); layout->addWidget(label, 0, 0); - m_workspaceField = new QLineEdit; registerField("workspace*", m_workspaceField); layout->addWidget(m_workspaceField, 0, 1); @@ -96,7 +117,6 @@ WorkspacePage::WorkspacePage(QWidget *parent) layout->addWidget(button, 0, 2); connect(button, SIGNAL(clicked()), this, SLOT(selectWorkspacePath())); - m_warningLabel = new QLabel; layout->addWidget(m_warningLabel, 1, 0, 1, 3, Qt::AlignTop); layout->setColumnStretch(1, 1); @@ -115,7 +135,8 @@ QString WorkspacePage::workspace() const void WorkspacePage::selectWorkspacePath() { m_warningLabel->setText(""); - QString workspace = QFileDialog::getExistingDirectory(this, "Select Workspace"); + QString path = QFileDialog::getExistingDirectory(this, "Select Workspace"); + QString workspace = m_projectFileDir->relativeFilePath(path); if (!workspace.isEmpty() && m_workspaceField) { m_workspaceField->setText(workspace); } @@ -123,7 +144,7 @@ void WorkspacePage::selectWorkspacePath() bool WorkspacePage::validatePage() { - if (QDir(workspace()).exists()) { + if (m_projectFileDir->exists(workspace())) { m_warningLabel->setText(""); return true; } else { @@ -132,6 +153,16 @@ bool WorkspacePage::validatePage() } } +void WorkspacePage::onUpdateProjectDir(const QString &path) +{ + if (m_projectFileDir == nullptr){ + m_projectFileDir = new QDir(path); + } + else { + m_projectFileDir->setPath(path); + } +} + QWizardPage *NewProjectWizard::createImportsPage() { QWizardPage *page = new QWizardPage; @@ -160,6 +191,9 @@ QWizardPage *NewProjectWizard::createImportsPage() ProjectPage::ProjectPage(QWidget *parent) : QWizardPage (parent) + , m_projectField(new QLineEdit) + , m_warningLabel(new QLabel) + , m_dirField(new QLineEdit) { setTitle("Project Name"); setSubTitle("This wizard generates a Qt QmlLive project. The QmlLive project file shall describe the" @@ -171,11 +205,22 @@ ProjectPage::ProjectPage(QWidget *parent) QLabel *label = new QLabel("Project name: "); layout->addWidget(label, 0, 0); - m_projectField = new QLineEdit; registerField("projectName*", m_projectField); m_projectField->setPlaceholderText("MyQmlLiveProject"); layout->addWidget(m_projectField, 0, 1); + QLabel *dirlabel = new QLabel("Create in: "); + layout->addWidget(dirlabel, 1, 0); + + layout->addWidget(m_dirField, 1, 1); + m_dirField->setText(QmlLiveProjectsLocation); + + QPushButton *button = new QPushButton("Select"); + layout->addWidget(button, 1, 2); + connect(button, &QPushButton::clicked, this, &ProjectPage::selectProjectPath); + + layout->addWidget(m_warningLabel, 1, 0, 1, 3, Qt::AlignTop); + layout->setColumnStretch(1, 1); layout->setRowStretch(1, 1); setLayout(layout); @@ -184,11 +229,44 @@ ProjectPage::ProjectPage(QWidget *parent) QString ProjectPage::projectName() const { if (m_projectField) { - return m_projectField->text(); + if (m_dirField->text().isEmpty()) + return m_projectField->text(); + else { + return m_dirField->text() + QDir().separator() + m_projectField->text(); + } } return ""; } +bool ProjectPage::validatePage() +{ + QDir dir(m_dirField->text()); + if (dir.exists()) { + m_warningLabel->setText(""); + emit updateProjectDir(m_dirField->text()); + return true; + } else { + if (QDir().mkpath(m_dirField->text())) { + emit updateProjectDir(m_dirField->text()); + return true; + } + else { + qWarning()<< "Unable to create directory: "<< m_dirField->text(); + m_warningLabel->setText("Unable to create directory: "+ m_dirField->text()); + return false; + } + } +} + +void ProjectPage::selectProjectPath() +{ + m_warningLabel->setText(""); + QString projectPath = QFileDialog::getExistingDirectory(this, "Create in"); + if (!projectPath.isEmpty() && m_dirField) { + m_dirField->setText(projectPath); + } +} + QString NewProjectWizard::mainDocument() const { return m_mainDocumentPage->mainDocument(); @@ -222,7 +300,8 @@ void NewProjectWizard::addImportPath() if (path.isEmpty()) { return; } - QListWidgetItem *item = new QListWidgetItem(path); + QString relativepath = m_projectFileDir->relativeFilePath(path); + QListWidgetItem *item = new QListWidgetItem(relativepath); item->setFlags(item->flags () | Qt::ItemIsEditable); m_importListWidget->addItem(item); @@ -244,3 +323,13 @@ void NewProjectWizard::removeImportPath() delete item; } } + +void NewProjectWizard::onUpdateProjectDir(const QString &path) +{ + if (m_projectFileDir == nullptr){ + m_projectFileDir = new QDir(path); + } + else { + m_projectFileDir->setPath(path); + } +} diff --git a/src/bench/newprojectwizard.h b/src/bench/newprojectwizard.h index b2f5fd6..936152d 100644 --- a/src/bench/newprojectwizard.h +++ b/src/bench/newprojectwizard.h @@ -36,6 +36,7 @@ #include #include #include +#include class ProjectPage : public QWizardPage { @@ -44,9 +45,17 @@ class ProjectPage : public QWizardPage public: ProjectPage(QWidget *parent = nullptr); QString projectName() const; + bool validatePage() override; + +signals: + void updateProjectDir(const QString &path); +private slots: + void selectProjectPath(); private: QLineEdit *m_projectField; + QLabel *m_warningLabel; + QLineEdit *m_dirField; }; class WorkspacePage : public QWizardPage @@ -61,9 +70,13 @@ public: private slots: void selectWorkspacePath(); +public slots: + void onUpdateProjectDir(const QString &path); + private: QLineEdit *m_workspaceField; QLabel *m_warningLabel; + QDir *m_projectFileDir; }; class MainDocumentPage : public QWizardPage @@ -74,8 +87,12 @@ public: MainDocumentPage(QWidget *parent = nullptr); QString mainDocument() const; +public slots: + void onUpdateProjectDir(const QString &path); + private: QLineEdit *m_mainDocumentField; + QDir *m_projectFileDir; }; class NewProjectWizard : public QWizard @@ -98,9 +115,13 @@ private slots: void editImportPath(); void removeImportPath(); +public slots: + void onUpdateProjectDir(const QString &path); + private: QListWidget *m_importListWidget; ProjectPage *m_projectPage; WorkspacePage *m_workspacePage; MainDocumentPage *m_mainDocumentPage; + QDir *m_projectFileDir; }; diff --git a/src/projectmanager.cpp b/src/projectmanager.cpp index 514ffec..158c4ad 100644 --- a/src/projectmanager.cpp +++ b/src/projectmanager.cpp @@ -47,6 +47,7 @@ ProjectManager::ProjectManager(QObject *parent) , m_mainDocument("main.qml") , m_workspace("") , m_projectName("") + , m_projectLocation("") { } @@ -60,6 +61,7 @@ bool ProjectManager::read(const QString &path) qWarning() << "Unable to open document " << path; return false; } + m_projectLocation = QFileInfo(path).absolutePath(); m_projectName = file.fileName(); QByteArray data = file.readAll(); QJsonParseError error; @@ -77,13 +79,7 @@ bool ProjectManager::read(const QString &path) m_mainDocument = root.value(MainKey).toString(); if (root.contains(WorkspaceKey)) { - QString workspace = root.value(WorkspaceKey).toString(); - if ((workspace.compare(".") == 0) || (workspace.compare("./") == 0)) { - m_workspace = QFileInfo(path).absolutePath(); - } - else { - m_workspace = QDir(workspace).absolutePath(); - } + m_workspace = root.value(WorkspaceKey).toString(); } if (root.contains(ImportsKey) && root.value(ImportsKey).isArray()) { QJsonArray imports = root.value(ImportsKey).toArray(); @@ -95,11 +91,13 @@ bool ProjectManager::read(const QString &path) void ProjectManager::write(const QString &path) { + qInfo()<<"Writing into " << path; QFile file(path); if (!file.open(QIODevice::WriteOnly)) { qWarning() << "Unable to write to document: " << path; return; } + m_projectLocation = QFileInfo(path).absolutePath(); QJsonObject root; root.insert(MainKey, QJsonValue(m_mainDocument)); root.insert(WorkspaceKey, QJsonValue(m_workspace)); @@ -114,8 +112,8 @@ void ProjectManager::write(const QString &path) void ProjectManager::create(const QString &projectName) { m_projectName = projectName; - QString path = QString(m_workspace).append(QDir::separator()).append(projectName).append(QmlLiveExtension); - qWarning() << path; + QString path = QString(projectName).append(QmlLiveExtension); + qInfo()<<"CREATING Project------ "<