Adds prototype classes for better serialization.

This commit is contained in:
mfreiholz 2016-03-31 14:47:19 +02:00
parent a566ea64be
commit 6c587ff8c4
6 changed files with 390 additions and 6 deletions

View File

@ -8,11 +8,11 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TEMPLATE = lib TEMPLATE = lib
adsBuildShared { adsBuildShared {
CONFIG += shared CONFIG += shared
DEFINES += ADS_EXPORT DEFINES += ADS_EXPORT
} }
!adsBuildShared { !adsBuildShared {
CONFIG += staticlib CONFIG += staticlib
} }
INCLUDEPATH += $$PWD/src INCLUDEPATH += $$PWD/src
@ -40,7 +40,8 @@ SOURCES += \
src/SectionContentWidget.cpp \ src/SectionContentWidget.cpp \
src/DropOverlay.cpp \ src/DropOverlay.cpp \
src/FloatingWidget.cpp \ src/FloatingWidget.cpp \
src/Internal.cpp src/Internal.cpp \
src/Serialization.cpp
HEADERS += \ HEADERS += \
include/ads/API.h \ include/ads/API.h \
@ -51,7 +52,8 @@ HEADERS += \
include/ads/SectionContentWidget.h \ include/ads/SectionContentWidget.h \
include/ads/DropOverlay.h \ include/ads/DropOverlay.h \
include/ads/FloatingWidget.h \ include/ads/FloatingWidget.h \
include/ads/Internal.h include/ads/Internal.h \
include/ads/Serialization.h
RESOURCES += \ RESOURCES += \
res/ads.qrc res/ads.qrc

View File

@ -17,7 +17,7 @@ class QSplitter;
#endif #endif
// Use namespace // Use namespace
// Disabled with Qt4! // Disabled with Qt4, it makes problems with signals and slots.
#ifdef ADS_NAMESPACE_ENABLED #ifdef ADS_NAMESPACE_ENABLED
#define ADS_NAMESPACE_BEGIN namespace ads { #define ADS_NAMESPACE_BEGIN namespace ads {
#define ADS_NAMESPACE_END } #define ADS_NAMESPACE_END }
@ -28,6 +28,12 @@ class QSplitter;
#define ADS_NS #define ADS_NS
#endif #endif
// Always enable "serialization" namespace.
// It is not required for signals and slots.
#define ADS_NAMESPACE_SER_BEGIN namespace ads { namespace serialization {
#define ADS_NAMESPACE_SER_END }}
#define ADS_NS_SER ::ads::serialization
// Width of the native window frame border (based on OS). // Width of the native window frame border (based on OS).
#define ADS_WINDOW_FRAME_BORDER_WIDTH 7 #define ADS_WINDOW_FRAME_BORDER_WIDTH 7

View File

@ -0,0 +1,116 @@
#ifndef ADS_SERIALIZATION_H
#define ADS_SERIALIZATION_H
#include <QtGlobal>
#include <QList>
#include <QString>
#include <QDataStream>
#include "ads/API.h"
ADS_NAMESPACE_SER_BEGIN
class Header
{
public:
Header();
qint32 magic;
qint32 version;
};
QDataStream& operator<<(QDataStream& out, const Header& data);
QDataStream& operator>>(QDataStream& in, Header& data);
class OffsetHeader
{
public:
OffsetHeader();
qint64 length;
QList<class OffsetHeaderEntry> entries;
};
QDataStream& operator<<(QDataStream& out, const OffsetHeader& data);
QDataStream& operator>>(QDataStream& in, OffsetHeader& data);
class OffsetHeaderEntry
{
public:
enum Type
{
Unknown = 0x00000001,
Hierarchy = 0x00000002,
SectionIndex = 0x00000003
};
OffsetHeaderEntry();
qint32 type;
qint64 offset;
qint64 length;
};
QDataStream& operator<<(QDataStream& out, const OffsetHeaderEntry& data);
QDataStream& operator>>(QDataStream& in, OffsetHeaderEntry& data);
class Section
{
public:
Section();
qint32 width;
qint32 height;
qint32 currentIndex;
qint32 sectionContentsCount;
QList<class SectionContent> sectionContents;
};
QDataStream& operator<<(QDataStream& out, const Section& data);
QDataStream& operator>>(QDataStream& in, Section& data);
class SectionContent
{
public:
SectionContent();
QString uniqueName;
bool visible;
qint32 preferredIndex;
};
QDataStream& operator<<(QDataStream& out, const SectionContent& data);
QDataStream& operator>>(QDataStream& in, SectionContent& data);
class FloatingContent
{
public:
FloatingContent();
QString uniqueName;
qint32 xpos;
qint32 ypos;
qint32 width;
qint32 height;
bool visible;
};
QDataStream& operator<<(QDataStream& out, const FloatingContent& data);
QDataStream& operator>>(QDataStream& in, FloatingContent& data);
// Type: OffsetHeaderEntry::Hierarchy
class HierarchyData
{
public:
HierarchyData();
QByteArray data;
};
QDataStream& operator<<(QDataStream& out, const HierarchyData& data);
QDataStream& operator>>(QDataStream& in, HierarchyData& data);
// Type: OffsetHeaderEntry::SectionIndex
class SectionIndexData
{
public:
SectionIndexData();
qint32 sectionsCount;
QList<Section> sections;
};
ADS_NAMESPACE_SER_END
#endif

View File

@ -0,0 +1,239 @@
#include "ads/Serialization.h"
ADS_NAMESPACE_SER_BEGIN
/*
\namespace ads::serialization
Serialization of ContainerWidget
--------------------------------
# Data Format Header
quint32 Magic
quint32 Version
# Offsets of available contents
qint32 Number of offset headers
LOOP
qint32 Type (e.g. Hierachy, SectionIndex)
qint64 Offset
qint64 Length
# Type: Hierachy
# Used to recreate the GUI geometry and state.
int Number of floating widgets
LOOP Floating widgets
QString Unique name of content
QByteArray Geometry of floating widget
bool Visibility
int Number of layout items (Valid values: 0, 1)
IF 0
int Number of hidden contents
LOOP Contents
QString Unique name of content
ELSEIF 1
... todo ...
ENDIF
# Type: SectionIndex
# Can be used for quick lookups on details for SectionWidgets.
# It includes sizes and its contents.
qint32 Number of section-widgets
LOOP
qint32 Width
qint32 Height
qint32 Current active tab index
qint32 Number of contents
LOOP
QString Unique name of content
bool Visibility
qint32 Preferred tab index
*/
///////////////////////////////////////////////////////////////////////////////
Header::Header() :
magic(0), version(0)
{
}
QDataStream& operator<<(QDataStream& out, const Header& data)
{
out << data.magic;
out << data.version;
return out;
}
QDataStream& operator>>(QDataStream& in, Header& data)
{
in >> data.magic;
in >> data.version;
return in;
}
///////////////////////////////////////////////////////////////////////////////
OffsetHeader::OffsetHeader() :
length(0)
{
}
QDataStream& operator<<(QDataStream& out, const OffsetHeader& data)
{
out << data.length;
for (int i = 0; i < data.length; ++i)
{
out << data.entries.at(i);
}
return out;
}
QDataStream& operator>>(QDataStream& in, OffsetHeader& data)
{
in >> data.length;
for (int i = 0; i < data.length; ++i)
{
OffsetHeaderEntry entry;
in >> entry;
data.entries.append(entry);
}
return in;
}
///////////////////////////////////////////////////////////////////////////////
OffsetHeaderEntry::OffsetHeaderEntry() :
type(Unknown), offset(0), length(0)
{
}
QDataStream& operator<<(QDataStream& out, const OffsetHeaderEntry& data)
{
out << data.type;
out << data.offset;
out << data.length;
return out;
}
QDataStream& operator>>(QDataStream& in, OffsetHeaderEntry& data)
{
in >> data.type;
in >> data.offset;
in >> data.length;
return in;
}
///////////////////////////////////////////////////////////////////////////////
Section::Section() :
width(0), height(0), currentIndex(0), sectionContentsCount(0)
{
}
QDataStream& operator<<(QDataStream& out, const Section& data)
{
out << data.width;
out << data.height;
out << data.currentIndex;
out << data.sectionContentsCount;
for (int i = 0; i < data.sectionContentsCount; ++i)
{
out << data.sectionContents.at(i);
}
return out;
}
QDataStream& operator>>(QDataStream& in, Section& data)
{
in >> data.width;
in >> data.height;
in >> data.currentIndex;
in >> data.sectionContentsCount;
for (int i = 0; i < data.sectionContentsCount; ++i)
{
SectionContent sc;
in >> sc;
data.sectionContents.append(sc);
}
return in;
}
///////////////////////////////////////////////////////////////////////////////
SectionContent::SectionContent() :
visible(false), preferredIndex(0)
{
}
QDataStream& operator<<(QDataStream& out, const SectionContent& data)
{
out << data.uniqueName;
out << data.visible;
out << data.preferredIndex;
return out;
}
QDataStream& operator>>(QDataStream& in, SectionContent& data)
{
in >> data.uniqueName;
in >> data.visible;
in >> data.preferredIndex;
return in;
}
///////////////////////////////////////////////////////////////////////////////
FloatingContent::FloatingContent() :
xpos(0), ypos(0), width(0), height(0), visible(false)
{
}
QDataStream& operator<<(QDataStream& out, const FloatingContent& data)
{
out << data.uniqueName;
out << data.xpos;
out << data.ypos;
out << data.width;
out << data.height;
out << data.visible;
return out;
}
QDataStream& operator>>(QDataStream& in, FloatingContent& data)
{
in >> data.uniqueName;
in >> data.xpos;
in >> data.ypos;
in >> data.width;
in >> data.height;
in >> data.visible;
return in;
}
///////////////////////////////////////////////////////////////////////////////
HierarchyData::HierarchyData()
{
}
QDataStream& operator<<(QDataStream& out, const HierarchyData& data)
{
out << data.data;
return out;
}
QDataStream& operator>>(QDataStream& in, HierarchyData& data)
{
in >> data.data;
return in;
}
///////////////////////////////////////////////////////////////////////////////
ADS_NAMESPACE_SER_END

View File

@ -22,6 +22,7 @@ void SectionContentListModel::init(ADS_NS::ContainerWidget* cw)
int SectionContentListModel::columnCount(const QModelIndex& parent) const int SectionContentListModel::columnCount(const QModelIndex& parent) const
{ {
Q_UNUSED(parent)
return _headers.count(); return _headers.count();
} }
@ -34,6 +35,7 @@ QVariant SectionContentListModel::headerData(int section, Qt::Orientation orient
int SectionContentListModel::rowCount(const QModelIndex& parent) const int SectionContentListModel::rowCount(const QModelIndex& parent) const
{ {
Q_UNUSED(parent)
return _contents.count(); return _contents.count();
} }

View File

@ -20,6 +20,25 @@ You can run the demo project and test it yourself.
The `master` branch is not guaranteed to be stable or does not even build, since it is the main working branch. The `master` branch is not guaranteed to be stable or does not even build, since it is the main working branch.
If you want a version that builds, you should always use a release/beta tag. If you want a version that builds, you should always use a release/beta tag.
## Getting started / Example
_MyWindow.h_
```cpp
#include <QMainWindow>
#include "ads/API.h"
#include "ads/ContainerWidget.h"
#include "ads/SectionContent.h"
class MyWindow : public QMainWindow
{
Q_OBJECT
public:
QMainWindow(QWidget* parent);
private:
ADS_NS::ContainerWidget _container;
};
```
## Developers ## Developers
[Manuel Freiholz](https://mfreiholz.de), Project Maintainer [Manuel Freiholz](https://mfreiholz.de), Project Maintainer