NDEVR
API Documentation
QCustomSplitter.h
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Widgets
28File: QCustomSplitter
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/BaseValues.h>
35#include <QStyle>
36#include <QEvent>
37#include <QSplitter>
38namespace NDEVR
39{
44 class NDEVR_WIDGETS_API QCustomSplitter : public QSplitter
45 {
46 Q_OBJECT
47 public:
52 explicit QCustomSplitter(QWidget* parent = nullptr)
53 : QSplitter(parent)
54 , m_is_animating(false)
55 {}
56
61 explicit QCustomSplitter(Qt::Orientation orientation, QWidget* parent = nullptr)
62 : QSplitter(orientation, parent)
63 , m_is_animating(false)
64 {}
65 virtual ~QCustomSplitter()
66 {};
67
73 void setAnimating(bool animating)
74 {
75 m_is_animating = animating;
76 if (!m_is_animating)
77 {
79 }
80 }
81
87 {
88 sint08 total = 0;
89 for (int i = 0; i < count(); i++)
90 {
91 total += (orientation() == Qt::Orientation::Horizontal) ? widget(i)->sizeHint().width() : widget(i)->sizeHint().height();
92 }
93 int our_size = availableSize();
94 if (our_size == 0 || total == 0)
95 return 1.0;
96 return cast<fltp08>(total) / cast<fltp08>(our_size);
97 }
98
102 int availableSize() const
103 {
104 int available_size = orientation() == Qt::Orientation::Horizontal ? width() : height();
105 available_size -= count() * style()->pixelMetric(QStyle::PixelMetric::PM_SplitterWidth, nullptr, this);
106 return available_size;
107 }
108
116 sint04 sizeIfAdded(sint04 preferred_size, sint04 min_size, sint04 current_size) const
117 {
118 preferred_size = getMax(preferred_size, min_size);
119 int available_size = availableSize();
120
121 if (available_size == 0)
122 return preferred_size;
123 if (count() == 1)
124 return available_size;
125 sint08 total = 0;
126 for (int i = 0; i < count(); i++)
127 {
128 total += (orientation() == Qt::Orientation::Horizontal) ? widget(i)->sizeHint().width() : widget(i)->sizeHint().height();
129 }
130 if (total <= 0)
131 return available_size;
132 fltp08 multiple = cast<fltp08>(available_size) / cast<fltp08>(total + preferred_size - current_size);
133 if (preferred_size * multiple < min_size)
134 return min_size;
135 else
136 return cast<sint04>(preferred_size * multiple);
137 }
138 /*void resizeEvent(QResizeEvent* event) override
139 {
140 refreshSizes();
141 QSplitter::resizeEvent(event);
142 }*/
147 QSize minimumSizeHint() const override
148 {
149 return QSize(1, 1);
150 }
151
157 bool eventFilter(QObject* object, QEvent* event) override
158 {
159 switch (event->type())
160 {
161 case QEvent::LayoutRequest:
162 refreshSizes();
163 break;
164 default:
165 break;
166 }
167 return QSplitter::eventFilter(object, event);
168 }
169
174 {
175 QList<int> current_sizes;
176 int total_size = 0;
177 for (int i = 0; i < count(); i++)
178 {
179 QWidget* w = widget(i);
180 QSize size_hint = w->sizeHint();
181 int local_size = getMax(1, orientation() == Qt::Orientation::Horizontal ? size_hint.width() : size_hint.height());
182 total_size += local_size;
183 current_sizes.append(local_size);
184 }
185 int our_size = (orientation() == Qt::Orientation::Horizontal) ? width() : height();
186 if (total_size < our_size)
187 {
188 fltp08 size_multiple = cast<fltp08>(our_size) / cast<fltp08>(total_size);
189 //QSize our_size = size();
190 for (int i = 0; i < count(); i++)
191 {
192 current_sizes[i] = cast<int>(size_multiple * current_sizes[i]);
193 /*if (orientation() == Qt::Orientation::Horizontal)
194 our_size.setWidth(current_sizes[i]);
195 else
196 our_size.setHeight(current_sizes[i]);
197 widget(i)->resize(our_size);*/
198 }
199 }
200 setSizes(current_sizes);
201 //repaint();
202 //if(m_is_animating)
203 //update();
204 }
205
210 void refreshSizes(int offset)
211 {
212 QList<int> current_sizes;
213 int total_size = 0;
214 for (int i = 0; i < count(); i++)
215 {
216 QWidget* w = widget(i);
217 QSize size_hint = w->sizeHint();
218 size_hint = size_hint.expandedTo(w->minimumSize());
219 int local_size = getMax(1, orientation() == Qt::Orientation::Horizontal ? size_hint.width() : size_hint.height());
220 total_size += local_size;
221 current_sizes.append(local_size + offset);
222 }
223 int our_size = availableSize();
224 if (total_size < our_size)
225 {
226 fltp08 size_multiple = cast<fltp08>(our_size) / cast<fltp08>(total_size);
227 for (int i = 0; i < count(); i++)
228 {
229 current_sizes[i] = cast<int>(size_multiple * current_sizes[i]);
230 }
231 }
232 setSizes(current_sizes);
233 //repaint();
234 //if(m_is_animating)
235 //update();
236 }
237
243 bool event(QEvent* e) override
244 {
245 if (m_block_events)
246 return QWidget::event(e);
247 else
248 return QSplitter::event(e);
249 }
250
255 void childEvent(QChildEvent* c) override
256 {
257 if (c->removed())
258 {
259 bool visible = isVisible();
260 if (visible)
261 setVisible(false);
262 QSplitter::childEvent(c);
263 if (visible)
264 {
265 m_block_events = true;
266 setVisible(true);
267 m_block_events = false;
268 }
269 return;
270 }
271 else
272 {
273 return QSplitter::childEvent(c);
274 }
275 }
276 bool m_block_events = false;
277 bool m_is_animating = false;
278 };
279}
A splitter lets the user control the size of 2 child widgets by dragging the boundary between them.
QSize minimumSizeHint() const override
Returns a minimal size hint to allow the splitter to be as small as possible.
QCustomSplitter(QWidget *parent=nullptr)
Constructs a splitter with default orientation.
bool m_is_animating
Whether the splitter is currently in an animation state.
sint04 sizeIfAdded(sint04 preferred_size, sint04 min_size, sint04 current_size) const
Calculates the size a widget would be given if added to the splitter, accounting for proportional sca...
bool event(QEvent *e) override
Handles events, optionally blocking them during visibility toggling to prevent recursive layout issue...
fltp08 sizeMultiple()
Calculates the ratio of the total preferred size of all child widgets to the available splitter space...
void childEvent(QChildEvent *c) override
Handles child widget add and remove events, temporarily hiding and re-showing the splitter on removal...
bool m_block_events
Whether to block events during visibility toggling.
void setAnimating(bool animating)
Sets whether the splitter is currently in an animation state.
void refreshSizes()
Recalculates and applies proportional sizes for all child widgets based on their size hints and the a...
int availableSize() const
Returns the available space for child widgets, excluding splitter handle widths.
bool eventFilter(QObject *object, QEvent *event) override
Filters events for child widgets, refreshing sizes on layout requests.
QCustomSplitter(Qt::Orientation orientation, QWidget *parent=nullptr)
Constructs a splitter with the specified orientation.
void refreshSizes(int offset)
Recalculates and applies proportional sizes for all child widgets with an additional offset applied t...
The primary namespace for the NDEVR SDK.
constexpr t_type getMax(const t_type &left, const t_type &right)
Finds the max of the given arguments using the > operator The only requirement is that t_type have > ...
double fltp08
Defines an alias representing an 8 byte floating-point number.
int32_t sint04
-Defines an alias representing a 4 byte, signed integer.
int64_t sint08
-Defines an alias representing an 8 byte, signed integer -Can represent exact integer values -9223372...
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
Definition Angle.h:408