OpenXLSX 1.10.0
Loading...
Searching...
No Matches
XLSlicer.hpp
Go to the documentation of this file.
1#ifndef OPENXLSX_XLSLICER_HPP
2#define OPENXLSX_XLSLICER_HPP
3
4#include "OpenXLSX-Exports.hpp"
5#include "XLXmlFile.hpp"
6#include "XLXmlParser.hpp"
7#include <string>
8#include <string_view>
9#include <vector>
10#include <cstdint>
11
12namespace OpenXLSX
13{
14 class XLWorksheet;
15 class XLDocument;
16
17 // ─────────────────────────────────────────────────────────────────────────
18 // XLSlicerStyle — strongly-typed enum replacing magic strings
19 // ─────────────────────────────────────────────────────────────────────────
20 enum class XLSlicerStyle : uint8_t {
21 // Light styles
23 // Dark styles
25 // Other styles
27 // Custom (use setStyleRaw for arbitrary names)
28 Custom = 255
29 };
30
32 OPENXLSX_EXPORT std::string xlSlicerStyleToString(XLSlicerStyle style);
34 OPENXLSX_EXPORT XLSlicerStyle xlSlicerStyleFromString(std::string_view s);
35
36 // ─────────────────────────────────────────────────────────────────────────
37 // XLSlicer — three-source unified handle
38 //
39 // A slicer's full state lives across three XML parts:
40 // 1. xl/slicers/slicerN.xml → name, caption, style, showCaption …
41 // 2. xl/slicerCaches/slicerCacheN.xml → selectedItems, sortOrder …
42 // 3. xl/drawings/drawingM.xml → position anchor (cellRef, width, height)
43 //
44 // This class holds references to all three so every property is readable
45 // and writable from a single handle. All setters return *this for chaining.
46 // ─────────────────────────────────────────────────────────────────────────
47 class OPENXLSX_EXPORT XLSlicer
48 {
49 public:
50 // ── Constructors ────────────────────────────────────────────────────
51 XLSlicer() = default;
52
54 explicit XLSlicer(XLXmlData* slicerXml);
55
57 XLSlicer(XLXmlData* slicerXml,
58 XMLNode slicerNode,
59 XLXmlData* cacheXml,
60 XMLNode anchorNode,
61 XLWorksheet* worksheet);
62
63 XLSlicer(const XLSlicer&) = default;
64 XLSlicer(XLSlicer&&) noexcept = default;
65 XLSlicer& operator=(const XLSlicer&) = default;
66 XLSlicer& operator=(XLSlicer&&) = default;
67 ~XLSlicer() = default;
68
69 // ── Validity ────────────────────────────────────────────────────────
70 [[nodiscard]] bool valid() const { return m_slicerXml != nullptr; }
71 explicit operator bool() const { return valid(); }
72
73 // ── Source 1: slicer XML properties ─────────────────────────────────
74
75 [[nodiscard]] std::string name() const;
76 [[nodiscard]] std::string caption() const;
77 [[nodiscard]] std::string cache() const;
78 [[nodiscard]] XLSlicerStyle style() const;
79 [[nodiscard]] std::string styleRaw() const;
80 [[nodiscard]] bool showCaption() const;
81 [[nodiscard]] int columnCount() const;
82 [[nodiscard]] bool lockedPosition() const;
83 [[nodiscard]] int rowHeight() const;
84
85 void setName(std::string_view name);
86 XLSlicer& setCaption(std::string_view caption);
87 XLSlicer& setStyle(XLSlicerStyle style);
88 XLSlicer& setStyleRaw(std::string_view rawStyleName);
89 XLSlicer& setShowCaption(bool show);
90 XLSlicer& setColumnCount(int cols);
91 XLSlicer& setLockedPosition(bool locked);
92 XLSlicer& setRowHeight(int emuHeight);
93
94 // ── Source 2: cache XML — filter items ──────────────────────────────
95
97 [[nodiscard]] std::vector<std::string> items() const;
99 [[nodiscard]] std::vector<std::string> selectedItems() const;
101 [[nodiscard]] bool isSortDescending() const;
102
104 XLSlicer& showOnly(const std::vector<std::string>& itemsToShow);
106 XLSlicer& showAll();
108 XLSlicer& hideItems(const std::vector<std::string>& itemsToHide);
110 XLSlicer& setSortDescending(bool desc);
111
112 // ── Source 3: drawing anchor — position & size ───────────────────────
113
115 [[nodiscard]] std::string cellRef() const;
117 [[nodiscard]] uint32_t width() const;
119 [[nodiscard]] uint32_t height() const;
120
122 XLSlicer& moveTo(std::string_view cellRef);
124 XLSlicer& resize(uint32_t widthPx, uint32_t heightPx);
125
126 // ── Internals used by XLSlicerCollection ────────────────────────────
127 XLXmlData* slicerXml() const { return m_slicerXml; }
128 XLXmlData* cacheXml() const { return m_cacheXml; }
129
130 private:
132 XMLNode slicerNode() const;
134 XMLNode cacheRoot() const;
135
136 XLXmlData* m_slicerXml{nullptr};
137 XMLNode m_slicerNode;
138 XLXmlData* m_cacheXml{nullptr};
139 XMLNode m_anchorNode;
140 XLWorksheet* m_worksheet{nullptr};
141 };
142
143} // namespace OpenXLSX
144
145#endif // OPENXLSX_XLSLICER_HPP
Definition XLXmlParser.hpp:84
Definition XLSlicer.hpp:48
XLXmlData * slicerXml() const
Definition XLSlicer.hpp:127
XLSlicer(const XLSlicer &)=default
XLXmlData * cacheXml() const
Definition XLSlicer.hpp:128
XLSlicer(XLSlicer &&) noexcept=default
A class encapsulating an Excel worksheet. Access to XLWorksheet objects should be via the workbook ob...
Definition XLWorksheet.hpp:120
The XLXmlData class encapsulates the properties and behaviour of the .xml files in an ....
Definition XLXmlData.hpp:68
Definition IZipArchive.hpp:18
OpenXLSX_xml_node XMLNode
Definition XLXmlParser.hpp:63
XLSlicerStyle
Definition XLSlicer.hpp:20
OPENXLSX_EXPORT XLSlicerStyle xlSlicerStyleFromString(std::string_view s)
Convert a raw style string to XLSlicerStyle enum (Custom if unrecognized).
Definition XLSlicer.cpp:35
OPENXLSX_EXPORT std::string xlSlicerStyleToString(XLSlicerStyle style)
Convert XLSlicerStyle enum to its Excel XML string representation.
Definition XLSlicer.cpp:14