Skip to content

Commit

Permalink
reverted changes about generating projects ID. Also, now next unique …
Browse files Browse the repository at this point in the history
…ID is stored in the project file.
  • Loading branch information
vt4a2h committed Jan 21, 2016
1 parent 8278ee0 commit 1afb444
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 63 deletions.
26 changes: 0 additions & 26 deletions models/applicationmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ namespace models {
*/
ApplicationModel::ApplicationModel(QObject *parent)
: QObject(parent)
, m_ProjectsIDsCounter(0)
, m_GlobalDatabase(std::make_shared<db::Database>())
, m_TreeModel(std::make_shared<ProjectTreeModel>())
{
Expand All @@ -56,7 +55,6 @@ namespace models {
project::SharedProject ApplicationModel::makeProject()
{
auto project = makeProject(DEFAULT_PROJECT_NAME, DEFAULT_PROJECT_PATH);
project->setID(QString::number(genProjectID()));
return project;
}

Expand All @@ -70,7 +68,6 @@ namespace models {
{
auto newProject(std::make_shared<project::Project>(name, path));
newProject->setGloablDatabase(globalDatabase());
newProject->setID(QString::number(genProjectID()));
m_TreeModel->addProject(newProject);
return *m_Projects.insert(newProject->id(), newProject);
}
Expand All @@ -85,20 +82,6 @@ namespace models {
if (!m_Projects.contains(pr->id())) {
m_Projects[pr->id()] = pr;
m_TreeModel->addProject(pr);

bool ok = false;
long prID = pr->id().toLong(&ok);
if (ok) {
if (prID >= m_ProjectsIDsCounter)
m_ProjectsIDsCounter = prID + 1;
else if (prID <= m_ProjectsIDsCounter) {
prID = genProjectID();
m_Projects.remove(pr->id());
pr->setID(QString::number(prID));
m_Projects[QString::number(prID)] = pr;
}
}

return true;
}

Expand Down Expand Up @@ -273,13 +256,4 @@ namespace models {
return m_TreeModel;
}

/**
* @brief ApplicationModel::genProjectID
* @return
*/
long ApplicationModel::genProjectID()
{
return m_ProjectsIDsCounter++;
}

} // namespace models
3 changes: 0 additions & 3 deletions models/applicationmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,8 @@ namespace models {
void currentProjectChanged(const project::SharedProject &p);

private:
long genProjectID();

project::Projects m_Projects;
project::SharedProject m_CurrentProject;
long m_ProjectsIDsCounter;

db::SharedDatabase m_GlobalDatabase;

Expand Down
69 changes: 44 additions & 25 deletions project/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,17 @@ namespace project {
/**
* @brief Project::Project
*/
Project::Project(QObject *parent)
: Project(DEFAULT_PROJECT_NAME, DEFAULT_PROJECT_PATH, parent)
Project::Project()
: Project(DEFAULT_PROJECT_NAME, DEFAULT_PROJECT_PATH)
{}

/**
* @brief Project::Project
* @param name
* @param path
*/
Project::Project(const QString &name, const QString &path, QObject *parent)
: QObject(parent)
, m_Name(name)
Project::Project(const QString &name, const QString &path)
: m_Name(name)
, m_Path(path)
, m_ID(utility::genId())
, m_nextUniqueID(0)
Expand All @@ -68,23 +67,43 @@ namespace project {
* @brief Project::Project
* @param src
*/
Project::Project(const Project &src)
: QObject(nullptr)
, m_Name(src.name())
, m_Path(src.path())
, m_ID(utility::genId()) // new UUID, TODO: clarify why?
, m_SaveStatus(false) // not saved
// deep copy of project database and shallow copy of global database
, m_Database(std::make_shared<db::ProjectDatabase>(*src.m_Database))
, m_CommandsStack(std::make_unique<QUndoStack>())
{}
Project::Project(Project &&src)
: m_Name(std::move(src.m_Name))
, m_Path(std::move(src.m_Path))
, m_ID(std::move(src.m_ID))
, m_nextUniqueID(std::move(src.m_nextUniqueID))
, m_SaveStatus(std::move(src.m_SaveStatus))
, m_Database(std::move(src.m_Database))
, m_CommandsStack(std::move(src.m_CommandsStack))
{
}

/**
* @brief Project::~Project
*/
Project::~Project()
{}

/**
* @brief Project::operator =
* @param lhs
* @return
*/
Project &Project::operator =(Project &&lhs)
{
if (this != &lhs) {
m_Name = std::move(lhs.m_Name);
m_Path = std::move(lhs.m_Path);
m_ID = std::move(lhs.m_ID);
m_nextUniqueID = std::move(lhs.m_nextUniqueID);
m_SaveStatus = std::move(lhs.m_SaveStatus);
m_Database = std::move(lhs.m_Database);
m_CommandsStack = std::move(lhs.m_CommandsStack);
}

return *this;
}

/**
* @brief operator ==
* @param lhs
Expand All @@ -99,7 +118,10 @@ namespace project {
return lhs.m_Name == rhs.m_Name &&
lhs.m_Path == rhs.m_Path &&
lhs.m_ID == rhs.m_ID &&
*lhs.m_Database == *rhs.m_Database;
lhs.m_nextUniqueID == rhs.m_nextUniqueID &&
lhs.m_SaveStatus == rhs.m_SaveStatus &&
lhs.m_Errors == rhs.m_Errors &&
*lhs.m_Database == *rhs.m_Database;
}

/**
Expand Down Expand Up @@ -220,6 +242,9 @@ namespace project {
result.insert("Name", m_Name);
result.insert("ID" , m_ID );

// Workaround for correct saving
result.insert("NextID", QString::number(m_nextUniqueID));

return result;
}

Expand All @@ -236,6 +261,9 @@ namespace project {
utility::checkAndSet(src, "ID", errorList, [&src, this](){
m_ID = src["ID"].toString();
});
utility::checkAndSet(src, "NextID", errorList, [&src, this](){
m_nextUniqueID = src["NextID"].toString().toLongLong();
});
}

/**
Expand Down Expand Up @@ -335,15 +363,6 @@ namespace project {
return m_ID;
}

/**
* @brief Project::setID
* @param newID
*/
void Project::setID(const QString &newID)
{
m_ID = newID;
}

/**
* @brief Project::setName
* @param name
Expand Down
15 changes: 7 additions & 8 deletions project/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,20 @@ namespace project {
Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged)

public:
explicit Project(QObject *parent = 0);
Project(const QString &name, const QString &path, QObject *parent = 0);
Project(const Project &src);
Q_DISABLE_COPY(Project)

explicit Project();
Project(const QString &name, const QString &path);
Project(Project &&src);
~Project();

Project& operator =(Project &&lhs);
friend bool operator ==(const Project &lhs, const Project &rhs);

QString name() const;
QString path() const;

QString id() const;
/// ID should be created by separate class after project loaded or created
void setID(const QString& newID);

void load(const QString &path); // don't forget install global database after load

Expand Down Expand Up @@ -107,7 +108,7 @@ namespace project {
QString m_Path;
QString m_ID;

long m_nextUniqueID; // TODO: add saving/loading code
qlonglong m_nextUniqueID;

bool m_SaveStatus;

Expand All @@ -119,5 +120,3 @@ namespace project {
};

} // namespace project

Q_DECLARE_METATYPE(project::Project)
3 changes: 2 additions & 1 deletion tests/test_data/new.qut
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"ID": "{1cc5a5a9-cbb5-4c40-9794-d4bc6289a38a}",
"Name": "New",
"Path": "/home/vt4a2h/Projects/uml_tst-out_"
"Path": "/home/vt4a2h/Projects/uml_tst-out_",
"NextID": "0"
}

0 comments on commit 1afb444

Please sign in to comment.