Restore last tab layout when loading a file.

This commit is contained in:
Leonardo Zide 2018-01-06 15:01:04 -08:00
parent 601401fa1b
commit dff8aac29b
5 changed files with 86 additions and 9 deletions

View file

@ -68,8 +68,37 @@ lcApplication::~lcApplication()
gApplication = nullptr;
}
void lcApplication::SaveTabLayout() const
{
if (!mProject || mProject->GetFileName().isEmpty())
return;
QSettings Settings;
QByteArray TabLayout = gMainWindow->GetTabLayout();
Settings.setValue(GetTabLayoutKey(), TabLayout);
}
QString lcApplication::GetTabLayoutKey() const
{
if (mProject)
{
QString FileName = mProject->GetFileName();
if (!FileName.isEmpty())
{
FileName.replace('\\', '?');
FileName.replace('/', '?');
return QString("TabLayouts/%1").arg(FileName);
}
}
return QString();
}
void lcApplication::SetProject(Project* Project)
{
SaveTabLayout();
delete mProject;
mProject = Project;
@ -77,6 +106,14 @@ void lcApplication::SetProject(Project* Project)
Project->SetActiveModel(0);
lcGetPiecesLibrary()->RemoveTemporaryPieces();
if (mProject && !mProject->GetFileName().isEmpty())
{
QSettings Settings;
QByteArray TabLayout = Settings.value(GetTabLayoutKey()).toByteArray();
gMainWindow->RestoreTabLayout(TabLayout);
}
}
void lcApplication::SetClipboard(const QByteArray& Clipboard)

View file

@ -45,6 +45,7 @@ public:
bool Initialize(QList<QPair<QString, bool>>& LibraryPaths, bool& ShowWindow);
void Shutdown();
void ShowPreferencesDialog();
void SaveTabLayout() const;
bool LoadPartsLibrary(const QList<QPair<QString, bool>>& LibraryPaths, bool OnlyUsePaths, bool ShowProgress);
@ -55,6 +56,9 @@ public:
lcPiecesLibrary* mLibrary;
lcPreferences mPreferences;
QByteArray mClipboard;
protected:
QString GetTabLayoutKey() const;
};
extern lcApplication* gApplication;

View file

@ -25,6 +25,7 @@
#include <functional>
lcMainWindow* gMainWindow;
#define LC_TAB_LAYOUT_VERSION 0x0001
void lcModelTabWidget::ResetLayout()
{
@ -48,6 +49,8 @@ void lcModelTabWidget::Clear()
{
ResetLayout();
mModel = nullptr;
for (View* View : mViews)
View->mModel = nullptr;
mViews.RemoveAll();
mActiveView = nullptr;
lcQGLWidget* Widget = (lcQGLWidget*)layout()->itemAt(0)->widget();
@ -731,6 +734,8 @@ void lcMainWindow::closeEvent(QCloseEvent* Event)
Settings.setValue("State", saveState());
mPartSelectionWidget->SaveState(Settings);
Settings.endGroup();
gApplication->SaveTabLayout();
}
else
Event->ignore();
@ -839,7 +844,7 @@ void lcMainWindow::ProjectFileChanged(const QString& Path)
if (NewProject->Load(Path))
{
QByteArray TabLayout = SaveTabLayout();
QByteArray TabLayout = GetTabLayout();
gApplication->SetProject(NewProject);
RestoreTabLayout(TabLayout);
UpdateAllViews();
@ -1110,13 +1115,12 @@ void lcMainWindow::SetSelectionMode(lcSelectionMode SelectionMode)
UpdateSelectionMode();
}
QByteArray lcMainWindow::SaveTabLayout()
QByteArray lcMainWindow::GetTabLayout()
{
QByteArray TabLayout;
QDataStream DataStream(&TabLayout, QIODevice::WriteOnly);
// todo: save version number and focus view
DataStream << (quint32)LC_TAB_LAYOUT_VERSION;
qint32 NumTabs = mModelTabWidget->count();
DataStream << NumTabs;
DataStream << ((lcModelTabWidget*)mModelTabWidget->currentWidget())->GetModel()->GetProperties().mName;
@ -1127,13 +1131,16 @@ QByteArray lcMainWindow::SaveTabLayout()
DataStream << TabWidget->GetModel()->GetProperties().mName;
std::function<void (QWidget*)> SaveWidget = [&DataStream, &SaveWidget](QWidget* Widget)
std::function<void (QWidget*)> SaveWidget = [&DataStream, &SaveWidget, &TabWidget](QWidget* Widget)
{
if (Widget->metaObject() == &lcQGLWidget::staticMetaObject)
{
DataStream << (qint32)0;
View* CurrentView = (View*)((lcQGLWidget*)Widget)->widget;
lcCamera* Camera = ((View*)((lcQGLWidget*)Widget)->widget)->mCamera;
DataStream << (qint32)0;
DataStream << (qint32)(TabWidget->GetActiveView() == CurrentView ? 1 : 0);
lcCamera* Camera = CurrentView->mCamera;
if (Camera->IsSimple())
{
@ -1172,8 +1179,17 @@ QByteArray lcMainWindow::SaveTabLayout()
void lcMainWindow::RestoreTabLayout(const QByteArray& TabLayout)
{
if (TabLayout.isEmpty())
return;
QDataStream DataStream(TabLayout);
quint32 Version;
DataStream >> Version;
if (Version != LC_TAB_LAYOUT_VERSION)
return;
qint32 NumTabs;
DataStream >> NumTabs;
QString CurrentTabName;
@ -1195,13 +1211,21 @@ void lcMainWindow::RestoreTabLayout(const QByteArray& TabLayout)
TabWidget = (lcModelTabWidget*)mModelTabWidget->widget(mModelTabWidget->count() - 1);
}
std::function<void(QWidget*)> LoadWidget = [&DataStream, &LoadWidget, Model, this](QWidget* ParentWidget)
QWidget* ActiveWidget;
std::function<void(QWidget*)> LoadWidget = [&DataStream, &LoadWidget, Model, &ActiveWidget, this](QWidget* ParentWidget)
{
qint32 WidgetType;
DataStream >> WidgetType;
if (WidgetType == 0)
{
qint32 IsActive;
DataStream >> IsActive;
if (IsActive)
ActiveWidget = ParentWidget;
qint32 CameraType;
DataStream >> CameraType;
@ -1272,6 +1296,12 @@ void lcMainWindow::RestoreTabLayout(const QByteArray& TabLayout)
};
LoadWidget(TabWidget ? TabWidget->layout()->itemAt(0)->widget() : nullptr);
if (ActiveWidget)
{
View* ActiveView = (View*)((lcQGLWidget*)ActiveWidget)->widget;
TabWidget->SetActiveView(ActiveView);
}
}
if (!mModelTabWidget->count())

View file

@ -229,7 +229,7 @@ public:
return mShadingMenu;
}
QByteArray SaveTabLayout();
QByteArray GetTabLayout();
void RestoreTabLayout(const QByteArray& TabLayout);
void RemoveAllModelTabs();
void SetCurrentModelTab(lcModel* Model);

View file

@ -656,6 +656,9 @@ void View::EndRenderToImage()
void View::OnDraw()
{
if (!mModel)
return;
bool DrawInterface = mWidget != nullptr;
mModel->GetScene(mScene, mCamera, DrawInterface, mHighlight);
@ -2695,6 +2698,9 @@ void View::OnForwardButtonUp()
void View::OnMouseMove()
{
if (!mModel)
return;
if (mTrackButton == LC_TRACKBUTTON_NONE)
{
UpdateTrackTool();