Fixed a bug in restore functionality that caused application crash, added initial support for perspectives

This commit is contained in:
Uwe Kindler
2018-02-13 12:00:58 +01:00
parent 805e97946e
commit 8a401ebd68
11 changed files with 517 additions and 226 deletions

View File

@@ -522,9 +522,10 @@ bool DockContainerWidgetPrivate::restoreDockArea(QXmlStreamReader& s,
else
{
DockArea->setProperty("currentIndex", CurrentIndex);
DockAreas.append(DockArea);
}
CreatedWidget = DockArea;
DockAreas.append(DockArea);
return true;
}

View File

@@ -42,6 +42,7 @@
#include <QAction>
#include <QXmlStreamWriter>
#include <QXmlStreamReader>
#include <QSettings>
#include "FloatingDockContainer.h"
#include "DockOverlay.h"
@@ -53,7 +54,6 @@
namespace ads
{
/**
* Private data class of CDockManager class (pimpl)
*/
@@ -65,6 +65,7 @@ struct DockManagerPrivate
CDockOverlay* ContainerOverlay;
CDockOverlay* DockAreaOverlay;
QMap<QString, CDockWidget*> DockWidgetsMap;
QMap<QString, QByteArray> Perspectives;
/**
* Private data constructor
@@ -386,6 +387,80 @@ CDockWidget* CDockManager::findDockWidget(const QString& ObjectName)
{
return d->DockWidgetsMap.value(ObjectName, nullptr);
}
//============================================================================
void CDockManager::addPerspective(const QString& UniquePrespectiveName)
{
d->Perspectives.insert(UniquePrespectiveName, saveState());
emit perspectiveListChanged();
}
//============================================================================
QStringList CDockManager::perspectiveNames() const
{
return d->Perspectives.keys();
}
//============================================================================
void CDockManager::openPerspective(const QString& PerspectiveName)
{
std::cout << "CDockManager::openPerspective " << PerspectiveName.toStdString() << std::endl;
const auto Iterator = d->Perspectives.find(PerspectiveName);
if (d->Perspectives.end() == Iterator)
{
return;
}
std::cout << "CDockManager::openPerspective - restoring state" << std::endl;
restoreState(Iterator.value());
}
//============================================================================
void CDockManager::savePerspectives(QSettings& Settings) const
{
Settings.beginWriteArray("Perspectives", d->Perspectives.size());
int i = 0;
for (auto it = d->Perspectives.constBegin(); it != d->Perspectives.constEnd(); ++it)
{
Settings.setArrayIndex(i);
Settings.setValue("Name", it.key());
Settings.setValue("State", it.value());
++i;
}
Settings.endArray();
}
//============================================================================
void CDockManager::loadPerspectives(QSettings& Settings)
{
d->Perspectives.clear();
int Size = Settings.beginReadArray("Perspectives");
if (!Size)
{
Settings.endArray();
return;
}
for (int i = 0; i < Size; ++i)
{
Settings.setArrayIndex(i);
QString Name = Settings.value("Name").toString();
QByteArray Data = Settings.value("State").toByteArray();
if (Name.isEmpty() || Data.isEmpty())
{
continue;
}
d->Perspectives.insert(Name, Data);
}
Settings.endArray();
}
} // namespace ads
//---------------------------------------------------------------------------

View File

@@ -32,6 +32,8 @@
//============================================================================
#include "DockContainerWidget.h"
class QSettings;
namespace ads
{
struct DockManagerPrivate;
@@ -134,6 +136,7 @@ public:
/**
* Saves the current state of the dockmanger and all its dock widgets
* into the returned QByteArray
*/
QByteArray saveState(int version = 0) const;
@@ -145,6 +148,43 @@ public:
* returns true.
*/
bool restoreState(const QByteArray &state, int version = 0);
/**
* Saves the current perspective to the internal list of perspectives.
* A perspective is the current state of the dock manager assigned
* with a certain name. This makes it possible for the user,
* to switch between different perspectives quickly.
* If a perspective with the given name already exists, then
* it will be overwritten with the new state.
*/
void addPerspective(const QString& UniquePrespectiveName);
/**
* Returns the names of all available perspectives
*/
QStringList perspectiveNames() const;
/**
* Saves the perspectives to the given settings file.
*/
void savePerspectives(QSettings& Settings) const;
/**
* Loads the perspectives from the given settings file
*/
void loadPerspectives(QSettings& Settings);
public slots:
/**
* Opens the perspective with the given name.
*/
void openPerspective(const QString& PerspectiveName);
signals:
/**
* This signal is emitted if the list of perspectives changed
*/
void perspectiveListChanged();
}; // class DockManager
} // namespace ads
//-----------------------------------------------------------------------------