From 6c587ff8c43296173e487dae2f1b6daab554f406 Mon Sep 17 00:00:00 2001 From: mfreiholz Date: Thu, 31 Mar 2016 14:47:19 +0200 Subject: [PATCH] Adds prototype classes for better serialization. --- .../AdvancedDockingSystem.pro | 12 +- AdvancedDockingSystem/include/ads/API.h | 8 +- .../include/ads/Serialization.h | 116 +++++++++ AdvancedDockingSystem/src/Serialization.cpp | 239 ++++++++++++++++++ .../src/dialogs/SectionContentListModel.cpp | 2 + README.md | 19 ++ 6 files changed, 390 insertions(+), 6 deletions(-) create mode 100644 AdvancedDockingSystem/include/ads/Serialization.h create mode 100644 AdvancedDockingSystem/src/Serialization.cpp diff --git a/AdvancedDockingSystem/AdvancedDockingSystem.pro b/AdvancedDockingSystem/AdvancedDockingSystem.pro index ac85b97..cff61e0 100644 --- a/AdvancedDockingSystem/AdvancedDockingSystem.pro +++ b/AdvancedDockingSystem/AdvancedDockingSystem.pro @@ -8,11 +8,11 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TEMPLATE = lib adsBuildShared { - CONFIG += shared - DEFINES += ADS_EXPORT + CONFIG += shared + DEFINES += ADS_EXPORT } !adsBuildShared { - CONFIG += staticlib + CONFIG += staticlib } INCLUDEPATH += $$PWD/src @@ -40,7 +40,8 @@ SOURCES += \ src/SectionContentWidget.cpp \ src/DropOverlay.cpp \ src/FloatingWidget.cpp \ - src/Internal.cpp + src/Internal.cpp \ + src/Serialization.cpp HEADERS += \ include/ads/API.h \ @@ -51,7 +52,8 @@ HEADERS += \ include/ads/SectionContentWidget.h \ include/ads/DropOverlay.h \ include/ads/FloatingWidget.h \ - include/ads/Internal.h + include/ads/Internal.h \ + include/ads/Serialization.h RESOURCES += \ res/ads.qrc diff --git a/AdvancedDockingSystem/include/ads/API.h b/AdvancedDockingSystem/include/ads/API.h index 6c721db..c263979 100644 --- a/AdvancedDockingSystem/include/ads/API.h +++ b/AdvancedDockingSystem/include/ads/API.h @@ -17,7 +17,7 @@ class QSplitter; #endif // Use namespace -// Disabled with Qt4! +// Disabled with Qt4, it makes problems with signals and slots. #ifdef ADS_NAMESPACE_ENABLED #define ADS_NAMESPACE_BEGIN namespace ads { #define ADS_NAMESPACE_END } @@ -28,6 +28,12 @@ class QSplitter; #define ADS_NS #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). #define ADS_WINDOW_FRAME_BORDER_WIDTH 7 diff --git a/AdvancedDockingSystem/include/ads/Serialization.h b/AdvancedDockingSystem/include/ads/Serialization.h new file mode 100644 index 0000000..bcd0fae --- /dev/null +++ b/AdvancedDockingSystem/include/ads/Serialization.h @@ -0,0 +1,116 @@ +#ifndef ADS_SERIALIZATION_H +#define ADS_SERIALIZATION_H + +#include +#include +#include +#include + +#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 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 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
sections; +}; + +ADS_NAMESPACE_SER_END +#endif diff --git a/AdvancedDockingSystem/src/Serialization.cpp b/AdvancedDockingSystem/src/Serialization.cpp new file mode 100644 index 0000000..f51e96d --- /dev/null +++ b/AdvancedDockingSystem/src/Serialization.cpp @@ -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 diff --git a/AdvancedDockingSystemDemo/src/dialogs/SectionContentListModel.cpp b/AdvancedDockingSystemDemo/src/dialogs/SectionContentListModel.cpp index 7eeeb30..0151176 100644 --- a/AdvancedDockingSystemDemo/src/dialogs/SectionContentListModel.cpp +++ b/AdvancedDockingSystemDemo/src/dialogs/SectionContentListModel.cpp @@ -22,6 +22,7 @@ void SectionContentListModel::init(ADS_NS::ContainerWidget* cw) int SectionContentListModel::columnCount(const QModelIndex& parent) const { + Q_UNUSED(parent) return _headers.count(); } @@ -34,6 +35,7 @@ QVariant SectionContentListModel::headerData(int section, Qt::Orientation orient int SectionContentListModel::rowCount(const QModelIndex& parent) const { + Q_UNUSED(parent) return _contents.count(); } diff --git a/README.md b/README.md index 7912f35..af6b050 100644 --- a/README.md +++ b/README.md @@ -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. If you want a version that builds, you should always use a release/beta tag. +## Getting started / Example + +_MyWindow.h_ +```cpp +#include +#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 [Manuel Freiholz](https://mfreiholz.de), Project Maintainer