From aca54d7a0dad1dc9fb51bd0bc1857ece200db3c5 Mon Sep 17 00:00:00 2001 From: Lukas Waslowski Date: Thu, 11 Apr 2024 19:35:14 +0000 Subject: [PATCH] Prepare loading the named column layouts from the settings. --- src/library/trackmodel.h | 13 +++++++++++-- src/proto/headers.proto | 8 ++++++++ src/widget/wtracktableviewheader.cpp | 22 ++++++++++++++++++++++ src/widget/wtracktableviewheader.h | 17 +++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/library/trackmodel.h b/src/library/trackmodel.h index 53bf53ddbc9..730d536bad1 100644 --- a/src/library/trackmodel.h +++ b/src/library/trackmodel.h @@ -191,15 +191,24 @@ class TrackModel { /*non-virtual*/ bool hasCapabilities(Capabilities caps) const { return (getCapabilities() & caps) == caps; } + + /*non-virtual*/ QString getKeyForModelSetting(const QString& name) const { + if (name.startsWith("shared_settings.")) { + return "mixxx.db.model." + name; + } else { + return m_settingsNamespace + "." + name; + } + } + virtual QString getModelSetting(const QString& name) { SettingsDAO settings(m_db); - QString key = m_settingsNamespace + "." + name; + QString key = getKeyForModelSetting(name); return settings.getValue(key); } virtual bool setModelSetting(const QString& name, const QVariant& value) { SettingsDAO settings(m_db); - QString key = m_settingsNamespace + "." + name; + QString key = getKeyForModelSetting(name); return settings.setValue(key, value); } diff --git a/src/proto/headers.proto b/src/proto/headers.proto index 2289e6517ec..e899ff0ddbe 100644 --- a/src/proto/headers.proto +++ b/src/proto/headers.proto @@ -23,3 +23,11 @@ message HeaderViewState { // This corresponds to the Qt::SortOrder enum. optional int32 sort_order = 4; } + +message NamedHeaderLayouts { + message NamedHeaderLayout { + required string layout_name = 1; + required HeaderViewState header_view_state = 2; + } + repeated NamedHeaderLayout layout = 1; +} diff --git a/src/widget/wtracktableviewheader.cpp b/src/widget/wtracktableviewheader.cpp index f13b86849e4..f033a8fe16c 100644 --- a/src/widget/wtracktableviewheader.cpp +++ b/src/widget/wtracktableviewheader.cpp @@ -11,6 +11,28 @@ #define WTTVH_MINIMUM_SECTION_SIZE 20 +NamedHeaderLayouts::NamedHeaderLayouts(const QString& base64serialized) { + // First decode the array from Base64, then initialize the protobuf from it. + QByteArray array = QByteArray::fromBase64(base64serialized.toLatin1()); + if (!m_named_layouts.ParseFromArray(array.constData(), array.size())) { + qWarning() << "Could not parse m_named_layouts from QByteArray of size " + << array.size(); + return; + } +} + +QString NamedHeaderLayouts::saveState() const { + // Serialize the proto to a byte array, then encode the array as Base64. +#if GOOGLE_PROTOBUF_VERSION >= 3001000 + int size = static_cast(m_named_layouts.ByteSizeLong()); +#else + int size = m_named_layouts.ByteSize(); +#endif + QByteArray array(size, '\0'); + m_named_layouts.SerializeToArray(array.data(), size); + return QString(array.toBase64()); +} + HeaderViewState::HeaderViewState(const QHeaderView& headers) { QAbstractItemModel* model = headers.model(); for (int vi = 0; vi < headers.count(); ++vi) { diff --git a/src/widget/wtracktableviewheader.h b/src/widget/wtracktableviewheader.h index 604b4ec3a97..887b1c54537 100644 --- a/src/widget/wtracktableviewheader.h +++ b/src/widget/wtracktableviewheader.h @@ -51,6 +51,23 @@ class HeaderViewState { mixxx::library::HeaderViewState m_view_state; }; +class NamedHeaderLayouts { +public: + NamedHeaderLayouts() {} + + // Populate from an existing protobuf, mostly for testing. + explicit NamedHeaderLayouts(const mixxx::library::NamedHeaderLayouts& pb) + : m_named_layouts(pb) { } + + // Populate the object with the serialized protobuf data provided. + explicit NamedHeaderLayouts(const QString& base64serialized); + + // Returns a serialized protobuf of the current state. + QString saveState() const; + +private: + mixxx::library::NamedHeaderLayouts m_named_layouts; +}; class WTrackTableViewHeader : public QHeaderView { Q_OBJECT