Preserve file comments when saving.

This commit is contained in:
leo 2015-03-21 20:12:04 +00:00
parent 39ef058aaa
commit 35fe45ada8
8 changed files with 156 additions and 89 deletions

View file

@ -360,6 +360,11 @@ inline bool operator==(const lcVector3& a, const lcVector3& b)
return a.x == b.x && a.y == b.y && a.z == b.z; return a.x == b.x && a.y == b.y && a.z == b.z;
} }
inline bool operator!=(const lcVector3& a, const lcVector3& b)
{
return a.x != b.x || a.y != b.y || a.z != b.z;
}
inline void lcVector3::Normalize() inline void lcVector3::Normalize()
{ {
float InvLength = 1.0f / Length(); float InvLength = 1.0f / Length();

View file

@ -63,26 +63,30 @@ void lcModelProperties::SaveLDraw(QTextStream& Stream, bool MPD) const
Stream << QLatin1String("0 !LEOCAD MODEL COMMENT ") << Comment << LineEnding; Stream << QLatin1String("0 !LEOCAD MODEL COMMENT ") << Comment << LineEnding;
} }
for (int BackgroundIdx = 0; BackgroundIdx < LC_NUM_BACKGROUND_TYPES; BackgroundIdx++) bool TypeChanged = (mBackgroundType != lcGetDefaultProfileInt(LC_PROFILE_DEFAULT_BACKGROUND_TYPE));
switch (mBackgroundType)
{ {
switch ((mBackgroundType + 1 + BackgroundIdx) % LC_NUM_BACKGROUND_TYPES) case LC_BACKGROUND_SOLID:
{ if (mBackgroundSolidColor != lcVector3FromColor(lcGetDefaultProfileInt(LC_PROFILE_DEFAULT_BACKGROUND_COLOR)) || TypeChanged)
case LC_BACKGROUND_SOLID:
Stream << QLatin1String("0 !LEOCAD MODEL BACKGROUND COLOR ") << mBackgroundSolidColor[0] << ' ' << mBackgroundSolidColor[1] << ' ' << mBackgroundSolidColor[2] << LineEnding; Stream << QLatin1String("0 !LEOCAD MODEL BACKGROUND COLOR ") << mBackgroundSolidColor[0] << ' ' << mBackgroundSolidColor[1] << ' ' << mBackgroundSolidColor[2] << LineEnding;
break; break;
case LC_BACKGROUND_GRADIENT:
case LC_BACKGROUND_GRADIENT:
if (mBackgroundGradientColor1 != lcVector3FromColor(lcGetProfileInt(LC_PROFILE_DEFAULT_GRADIENT_COLOR1)) ||
mBackgroundGradientColor2 != lcVector3FromColor(lcGetProfileInt(LC_PROFILE_DEFAULT_GRADIENT_COLOR2)) || TypeChanged)
Stream << QLatin1String("0 !LEOCAD MODEL BACKGROUND GRADIENT ") << mBackgroundGradientColor1[0] << ' ' << mBackgroundGradientColor1[1] << ' ' << mBackgroundGradientColor1[2] << ' ' << mBackgroundGradientColor2[0] << ' ' << mBackgroundGradientColor2[1] << ' ' << mBackgroundGradientColor2[2] << LineEnding; Stream << QLatin1String("0 !LEOCAD MODEL BACKGROUND GRADIENT ") << mBackgroundGradientColor1[0] << ' ' << mBackgroundGradientColor1[1] << ' ' << mBackgroundGradientColor1[2] << ' ' << mBackgroundGradientColor2[0] << ' ' << mBackgroundGradientColor2[1] << ' ' << mBackgroundGradientColor2[2] << LineEnding;
break; break;
case LC_BACKGROUND_IMAGE:
if (!mBackgroundImage.isEmpty()) case LC_BACKGROUND_IMAGE:
{ if (!mBackgroundImage.isEmpty())
Stream << QLatin1String("0 !LEOCAD MODEL BACKGROUND IMAGE "); {
if (mBackgroundImageTile) Stream << QLatin1String("0 !LEOCAD MODEL BACKGROUND IMAGE ");
Stream << QLatin1String("TILE "); if (mBackgroundImageTile)
Stream << QLatin1String("NAME ") << mBackgroundImage << LineEnding; Stream << QLatin1String("TILE ");
} Stream << QLatin1String("NAME ") << mBackgroundImage << LineEnding;
break;
} }
break;
} }
// bool mFogEnabled; // bool mFogEnabled;
@ -205,7 +209,7 @@ void lcModel::DeleteModel()
mCameras.DeleteAll(); mCameras.DeleteAll();
mLights.DeleteAll(); mLights.DeleteAll();
mGroups.DeleteAll(); mGroups.DeleteAll();
mMeshLines.clear(); mFileLines.clear();
} }
void lcModel::CreatePieceInfo(Project* Project) void lcModel::CreatePieceInfo(Project* Project)
@ -275,73 +279,86 @@ void lcModel::SaveLDraw(QTextStream& Stream, bool MPD, bool SelectedOnly) const
mProperties.SaveLDraw(Stream, MPD); mProperties.SaveLDraw(Stream, MPD);
lcStep LastStep = GetLastStep(); if (mCurrentStep != GetLastStep())
if (mCurrentStep != LastStep)
Stream << QLatin1String("0 !LEOCAD MODEL CURRENT_STEP") << mCurrentStep << LineEnding; Stream << QLatin1String("0 !LEOCAD MODEL CURRENT_STEP") << mCurrentStep << LineEnding;
lcArray<lcGroup*> CurrentGroups; lcArray<lcGroup*> CurrentGroups;
lcStep Step = 1;
int CurrentLine = 0;
for (lcStep Step = 1; Step <= LastStep; Step++) for (int PieceIdx = 0; PieceIdx < mPieces.GetSize(); PieceIdx++)
{ {
for (int PieceIdx = 0; PieceIdx < mPieces.GetSize(); PieceIdx++) lcPiece* Piece = mPieces[PieceIdx];
if (SelectedOnly && !Piece->IsSelected())
continue;
while (Piece->GetFileLine() > CurrentLine && CurrentLine < mFileLines.size())
{ {
lcPiece* Piece = mPieces[PieceIdx]; Stream << mFileLines[CurrentLine];
CurrentLine++;
if (Piece->GetStepShow() != Step || (SelectedOnly && !Piece->IsSelected()))
continue;
lcGroup* PieceGroup = Piece->GetGroup();
if (PieceGroup)
{
if (CurrentGroups.IsEmpty() || (!CurrentGroups.IsEmpty() && PieceGroup != CurrentGroups[CurrentGroups.GetSize() - 1]))
{
lcArray<lcGroup*> PieceParents;
for (lcGroup* Group = PieceGroup; Group; Group = Group->mGroup)
PieceParents.InsertAt(0, Group);
int FoundParent = -1;
while (!CurrentGroups.IsEmpty())
{
lcGroup* Group = CurrentGroups[CurrentGroups.GetSize() - 1];
int Index = PieceParents.FindIndex(Group);
if (Index == -1)
{
CurrentGroups.RemoveIndex(CurrentGroups.GetSize() - 1);
Stream << QLatin1String("0 !LEOCAD GROUP END\r\n");
}
else
{
FoundParent = Index;
break;
}
}
for (int ParentIdx = FoundParent + 1; ParentIdx < PieceParents.GetSize(); ParentIdx++)
{
lcGroup* Group = PieceParents[ParentIdx];
CurrentGroups.Add(Group);
Stream << QLatin1String("0 !LEOCAD GROUP BEGIN ") << Group->m_strName << LineEnding;
}
}
}
else
{
while (CurrentGroups.GetSize())
{
CurrentGroups.RemoveIndex(CurrentGroups.GetSize() - 1);
Stream << QLatin1String("0 !LEOCAD GROUP END\r\n");
}
}
Piece->SaveLDraw(Stream);
} }
if (Step != LastStep) while (Piece->GetStepShow() < Step)
{
Stream << QLatin1String("0 STEP\r\n"); Stream << QLatin1String("0 STEP\r\n");
Step++;
}
lcGroup* PieceGroup = Piece->GetGroup();
if (PieceGroup)
{
if (CurrentGroups.IsEmpty() || (!CurrentGroups.IsEmpty() && PieceGroup != CurrentGroups[CurrentGroups.GetSize() - 1]))
{
lcArray<lcGroup*> PieceParents;
for (lcGroup* Group = PieceGroup; Group; Group = Group->mGroup)
PieceParents.InsertAt(0, Group);
int FoundParent = -1;
while (!CurrentGroups.IsEmpty())
{
lcGroup* Group = CurrentGroups[CurrentGroups.GetSize() - 1];
int Index = PieceParents.FindIndex(Group);
if (Index == -1)
{
CurrentGroups.RemoveIndex(CurrentGroups.GetSize() - 1);
Stream << QLatin1String("0 !LEOCAD GROUP END\r\n");
}
else
{
FoundParent = Index;
break;
}
}
for (int ParentIdx = FoundParent + 1; ParentIdx < PieceParents.GetSize(); ParentIdx++)
{
lcGroup* Group = PieceParents[ParentIdx];
CurrentGroups.Add(Group);
Stream << QLatin1String("0 !LEOCAD GROUP BEGIN ") << Group->m_strName << LineEnding;
}
}
}
else
{
while (CurrentGroups.GetSize())
{
CurrentGroups.RemoveIndex(CurrentGroups.GetSize() - 1);
Stream << QLatin1String("0 !LEOCAD GROUP END\r\n");
}
}
Piece->SaveLDraw(Stream);
}
while (CurrentLine < mFileLines.size())
{
Stream << mFileLines[CurrentLine];
CurrentLine++;
} }
while (CurrentGroups.GetSize()) while (CurrentGroups.GetSize())
@ -380,9 +397,9 @@ void lcModel::LoadLDraw(QIODevice& Device, Project* Project)
while (!Device.atEnd()) while (!Device.atEnd())
{ {
qint64 Pos = Device.pos(); qint64 Pos = Device.pos();
QString Line = Device.readLine().trimmed(); QString OriginalLine = Device.readLine();
QString Line = OriginalLine.trimmed();
QTextStream LineStream(&Line, QIODevice::ReadOnly); QTextStream LineStream(&Line, QIODevice::ReadOnly);
bool MeshLine = false;
QString Token; QString Token;
LineStream >> Token; LineStream >> Token;
@ -413,7 +430,10 @@ void lcModel::LoadLDraw(QIODevice& Device, Project* Project)
} }
if (Token != QLatin1String("!LEOCAD")) if (Token != QLatin1String("!LEOCAD"))
{
mFileLines.append(OriginalLine);
continue; continue;
}
LineStream >> Token; LineStream >> Token;
@ -489,7 +509,9 @@ void lcModel::LoadLDraw(QIODevice& Device, Project* Project)
lcPiecesLibrary* Library = lcGetPiecesLibrary(); lcPiecesLibrary* Library = lcGetPiecesLibrary();
if (Library->IsPrimitive(PartID.toLatin1().constData())) if (Library->IsPrimitive(PartID.toLatin1().constData()))
MeshLine = true; {
mFileLines.append(OriginalLine);
}
else else
{ {
if (!Piece) if (!Piece)
@ -507,6 +529,7 @@ void lcModel::LoadLDraw(QIODevice& Device, Project* Project)
lcMatrix44 Transform(lcVector4(Matrix[0], Matrix[2], -Matrix[1], 0.0f), lcVector4(Matrix[8], Matrix[10], -Matrix[9], 0.0f), lcMatrix44 Transform(lcVector4(Matrix[0], Matrix[2], -Matrix[1], 0.0f), lcVector4(Matrix[8], Matrix[10], -Matrix[9], 0.0f),
lcVector4(-Matrix[4], -Matrix[6], Matrix[5], 0.0f), lcVector4(Matrix[12], Matrix[14], -Matrix[13], 1.0f)); lcVector4(-Matrix[4], -Matrix[6], Matrix[5], 0.0f), lcVector4(Matrix[12], Matrix[14], -Matrix[13], 1.0f));
Piece->SetFileLine(mFileLines.size());
Piece->SetPieceInfo(Info); Piece->SetPieceInfo(Info);
Piece->Initialize(Transform, CurrentStep); Piece->Initialize(Transform, CurrentStep);
Piece->SetColorCode(ColorCode); Piece->SetColorCode(ColorCode);
@ -514,11 +537,8 @@ void lcModel::LoadLDraw(QIODevice& Device, Project* Project)
Piece = NULL; Piece = NULL;
} }
} }
else if (Token == QLatin1String("2") || Token == QLatin1String("3") || Token == QLatin1String("4")) else
MeshLine = true; mFileLines.append(OriginalLine);
if (MeshLine)
mMeshLines.append(Line);
} }
mCurrentStep = CurrentStep; mCurrentStep = CurrentStep;
@ -816,6 +836,7 @@ void lcModel::Merge(lcModel* Other)
for (int PieceIdx = 0; PieceIdx < Other->mPieces.GetSize(); PieceIdx++) for (int PieceIdx = 0; PieceIdx < Other->mPieces.GetSize(); PieceIdx++)
{ {
lcPiece* Piece = Other->mPieces[PieceIdx]; lcPiece* Piece = Other->mPieces[PieceIdx];
Piece->SetFileLine(-1);
AddPiece(Piece); AddPiece(Piece);
} }
@ -1909,7 +1930,11 @@ void lcModel::ShowSelectedPiecesEarlier()
return; return;
for (int PieceIdx = 0; PieceIdx < MovedPieces.GetSize(); PieceIdx++) for (int PieceIdx = 0; PieceIdx < MovedPieces.GetSize(); PieceIdx++)
AddPiece(MovedPieces[PieceIdx]); {
lcPiece* Piece = MovedPieces[PieceIdx];
Piece->SetFileLine(-1);
AddPiece(Piece);
}
SaveCheckpoint("Modifying"); SaveCheckpoint("Modifying");
gMainWindow->UpdateAllViews(); gMainWindow->UpdateAllViews();
@ -1950,7 +1975,11 @@ void lcModel::ShowSelectedPiecesLater()
return; return;
for (int PieceIdx = 0; PieceIdx < MovedPieces.GetSize(); PieceIdx++) for (int PieceIdx = 0; PieceIdx < MovedPieces.GetSize(); PieceIdx++)
AddPiece(MovedPieces[PieceIdx]); {
lcPiece* Piece = MovedPieces[PieceIdx];
Piece->SetFileLine(-1);
AddPiece(Piece);
}
SaveCheckpoint("Modifying"); SaveCheckpoint("Modifying");
gMainWindow->UpdateAllViews(); gMainWindow->UpdateAllViews();

View file

@ -165,9 +165,9 @@ public:
mProperties.mName = Name; mProperties.mName = Name;
} }
const QStringList& GetMeshLines() const const QStringList& GetFileLines() const
{ {
return mMeshLines; return mFileLines;
} }
lcStep GetLastStep() const; lcStep GetLastStep() const;
@ -345,7 +345,7 @@ protected:
lcArray<lcCamera*> mCameras; lcArray<lcCamera*> mCameras;
lcArray<lcLight*> mLights; lcArray<lcLight*> mLights;
lcArray<lcGroup*> mGroups; lcArray<lcGroup*> mGroups;
QStringList mMeshLines; QStringList mFileLines;
lcModelHistoryEntry* mSavedHistory; lcModelHistoryEntry* mSavedHistory;
lcArray<lcModelHistoryEntry*> mUndoHistory; lcArray<lcModelHistoryEntry*> mUndoHistory;

View file

@ -112,6 +112,21 @@ void lcRemoveProfileKey(LC_PROFILE_KEY Key)
Settings.remove(QString("%1/%2").arg(Entry.mSection, Entry.mKey)); Settings.remove(QString("%1/%2").arg(Entry.mSection, Entry.mKey));
} }
int lcGetDefaultProfileInt(LC_PROFILE_KEY Key)
{
return gProfileEntries[Key].mDefault.IntValue;
}
float lcGetDefaultProfileFloat(LC_PROFILE_KEY Key)
{
return gProfileEntries[Key].mDefault.FloatValue;
}
QString lcGetDefaultProfileString(LC_PROFILE_KEY Key)
{
return QString::fromLatin1(gProfileEntries[Key].mDefault.StringValue);
}
int lcGetProfileInt(LC_PROFILE_KEY Key) int lcGetProfileInt(LC_PROFILE_KEY Key)
{ {
lcProfileEntry& Entry = gProfileEntries[Key]; lcProfileEntry& Entry = gProfileEntries[Key];

View file

@ -97,6 +97,10 @@ extern lcProfileEntry gProfileEntries[LC_NUM_PROFILE_KEYS];
void lcRemoveProfileKey(LC_PROFILE_KEY Key); void lcRemoveProfileKey(LC_PROFILE_KEY Key);
int lcGetDefaultProfileInt(LC_PROFILE_KEY Key);
float lcGetDefaultProfileFloat(LC_PROFILE_KEY Key);
QString lcGetDefaultProfileString(LC_PROFILE_KEY Key);
int lcGetProfileInt(LC_PROFILE_KEY Key); int lcGetProfileInt(LC_PROFILE_KEY Key);
float lcGetProfileFloat(LC_PROFILE_KEY Key); float lcGetProfileFloat(LC_PROFILE_KEY Key);
QString lcGetProfileString(LC_PROFILE_KEY Key); QString lcGetProfileString(LC_PROFILE_KEY Key);

View file

@ -32,6 +32,7 @@ lcPiece::lcPiece(PieceInfo* pPieceInfo)
mStepShow = 1; mStepShow = 1;
mStepHide = LC_STEP_MAX; mStepHide = LC_STEP_MAX;
mGroup = NULL; mGroup = NULL;
mFileLine = -1;
if (mPieceInfo != NULL) if (mPieceInfo != NULL)
mPieceInfo->AddRef(); mPieceInfo->AddRef();

View file

@ -91,6 +91,16 @@ public:
void SaveLDraw(QTextStream& Stream) const; void SaveLDraw(QTextStream& Stream) const;
bool ParseLDrawLine(QTextStream& Stream); bool ParseLDrawLine(QTextStream& Stream);
void SetFileLine(int Line)
{
mFileLine = Line;
}
int GetFileLine() const
{
return mFileLine;
}
virtual void RayTest(lcObjectRayTest& ObjectRayTest) const; virtual void RayTest(lcObjectRayTest& ObjectRayTest) const;
virtual void BoxTest(lcObjectBoxTest& ObjectBoxTest) const; virtual void BoxTest(lcObjectBoxTest& ObjectBoxTest) const;
virtual void DrawInterface(lcContext* Context, const lcMatrix44& ViewMatrix) const; virtual void DrawInterface(lcContext* Context, const lcMatrix44& ViewMatrix) const;
@ -150,6 +160,7 @@ public:
void SetStepShow(lcStep Step) void SetStepShow(lcStep Step)
{ {
mFileLine = -1;
mStepShow = Step; mStepShow = Step;
} }
@ -187,6 +198,8 @@ protected:
lcArray<lcObjectKey<lcVector3>> mPositionKeys; lcArray<lcObjectKey<lcVector3>> mPositionKeys;
lcArray<lcObjectKey<lcMatrix33>> mRotationKeys; lcArray<lcObjectKey<lcMatrix33>> mRotationKeys;
int mFileLine;
// Atributes // Atributes
lcGroup* mGroup; lcGroup* mGroup;

View file

@ -64,7 +64,7 @@ void PieceInfo::SetModel(lcModel* Model, bool UpdateMesh)
strncpy(m_strDescription, Model->GetProperties().mName.toLatin1().data(), sizeof(m_strDescription)); strncpy(m_strDescription, Model->GetProperties().mName.toLatin1().data(), sizeof(m_strDescription));
m_strDescription[sizeof(m_strDescription)-1] = 0; m_strDescription[sizeof(m_strDescription)-1] = 0;
const QStringList& MeshLines = Model->GetMeshLines(); const QStringList& MeshLines = Model->GetFileLines();
if (UpdateMesh && !MeshLines.isEmpty()) if (UpdateMesh && !MeshLines.isEmpty())
{ {