API Documentation
Loading...
Searching...
No Matches
QCustomSplitter.h
Go to the documentation of this file.
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 <QStyle>
35#include <QEvent>
36#include <QSplitter>
37namespace NDEVR
38{
39 /**--------------------------------------------------------------------------------------------------
40 \brief A splitter lets the user control the size of 2 child widgets by dragging the boundary between them.
41 **/
42 class NDEVR_WIDGETS_API QCustomSplitter : public QSplitter
43 {
44 Q_OBJECT
45 public:
46 explicit QCustomSplitter(QWidget* parent = nullptr)
47 : QSplitter(parent)
48 , m_is_animating(false)
49 {}
50 explicit QCustomSplitter(Qt::Orientation orientation, QWidget* parent = nullptr)
51 : QSplitter(orientation, parent)
52 , m_is_animating(false)
53 {}
55 {};
56
57 void setAnimating(bool animating)
58 {
59 m_is_animating = animating;
60 if (!m_is_animating)
61 {
62 refreshSizes();
63 }
64 }
66 {
67 sint08 total = 0;
68 for (int i = 0; i < count(); i++)
69 {
70 total += (orientation() == Qt::Orientation::Horizontal) ? widget(i)->sizeHint().width() : widget(i)->sizeHint().height();
71 }
72 int our_size = availableSize();
73 if (our_size == 0 || total == 0)
74 return 1.0;
75 return cast<fltp08>(total) / cast<fltp08>(our_size);
76 }
77 int availableSize() const
78 {
79 int available_size = orientation() == Qt::Orientation::Horizontal ? width() : height();
80 available_size -= count() * style()->pixelMetric(QStyle::PixelMetric::PM_SplitterWidth, nullptr, this);
81 return available_size;
82 }
83 sint04 sizeIfAdded(sint04 preferred_size, sint04 min_size, sint04 current_size) const
84 {
85 preferred_size = getMax(preferred_size, min_size);
86 int available_size = availableSize();
87
88 if (available_size == 0)
89 return preferred_size;
90 if (count() == 1)
91 return available_size;
92 sint08 total = 0;
93 for (int i = 0; i < count(); i++)
94 {
95 total += (orientation() == Qt::Orientation::Horizontal) ? widget(i)->sizeHint().width() : widget(i)->sizeHint().height();
96 }
97 if (total <= 0)
98 return available_size;
99 fltp08 multiple = cast<fltp08>(available_size) / cast<fltp08>(total + preferred_size - current_size);
100 if (preferred_size * multiple < min_size)
101 return min_size;
102 else
103 return cast<sint04>(preferred_size * multiple);
104 }
105 /*void resizeEvent(QResizeEvent* event) override
106 {
107 refreshSizes();
108 QSplitter::resizeEvent(event);
109 }*/
110 QSize minimumSizeHint() const override
111 {
112 return QSize(1, 1);
113 }
114 bool eventFilter(QObject* object, QEvent* event) override
115 {
116 switch (event->type())
117 {
118 case QEvent::LayoutRequest:
119 refreshSizes();
120 break;
121 default:
122 break;
123 }
124 return QSplitter::eventFilter(object, event);
125 }
127 {
128 QList<int> current_sizes;
129 int total_size = 0;
130 for (int i = 0; i < count(); i++)
131 {
132 QWidget* w = widget(i);
133 QSize size_hint = w->sizeHint();
134 int local_size = getMax(1, orientation() == Qt::Orientation::Horizontal ? size_hint.width() : size_hint.height());
135 total_size += local_size;
136 current_sizes.append(local_size);
137 }
138 int our_size = (orientation() == Qt::Orientation::Horizontal) ? width() : height();
139 if (total_size < our_size)
140 {
141 fltp08 size_multiple = cast<fltp08>(our_size) / cast<fltp08>(total_size);
142 //QSize our_size = size();
143 for (int i = 0; i < count(); i++)
144 {
145 current_sizes[i] = cast<int>(size_multiple * current_sizes[i]);
146 /*if (orientation() == Qt::Orientation::Horizontal)
147 our_size.setWidth(current_sizes[i]);
148 else
149 our_size.setHeight(current_sizes[i]);
150 widget(i)->resize(our_size);*/
151 }
152 }
153 setSizes(current_sizes);
154 //repaint();
155 //if(m_is_animating)
156 //update();
157 }
158 void refreshSizes(int offset)
159 {
160 QList<int> current_sizes;
161 int total_size = 0;
162 for (int i = 0; i < count(); i++)
163 {
164 QWidget* w = widget(i);
165 QSize size_hint = w->sizeHint();
166 size_hint = size_hint.expandedTo(w->minimumSize());
167 int local_size = getMax(1, orientation() == Qt::Orientation::Horizontal ? size_hint.width() : size_hint.height());
168 total_size += local_size;
169 current_sizes.append(local_size + offset);
170 }
171 int our_size = availableSize();
172 if (total_size < our_size)
173 {
174 fltp08 size_multiple = cast<fltp08>(our_size) / cast<fltp08>(total_size);
175 for (int i = 0; i < count(); i++)
176 {
177 current_sizes[i] = cast<int>(size_multiple * current_sizes[i]);
178 }
179 }
180 setSizes(current_sizes);
181 //repaint();
182 //if(m_is_animating)
183 //update();
184 }
186 };
187}
#define NDEVR_WIDGETS_API
Definition DLLInfo.h:59
A splitter lets the user control the size of 2 child widgets by dragging the boundary between them.
Definition QCustomSplitter.h:43
QSize minimumSizeHint() const override
Definition QCustomSplitter.h:110
QCustomSplitter(Qt::Orientation orientation, QWidget *parent=nullptr)
Definition QCustomSplitter.h:50
bool eventFilter(QObject *object, QEvent *event) override
Definition QCustomSplitter.h:114
bool m_is_animating
Definition QCustomSplitter.h:185
sint04 sizeIfAdded(sint04 preferred_size, sint04 min_size, sint04 current_size) const
Definition QCustomSplitter.h:83
void refreshSizes(int offset)
Definition QCustomSplitter.h:158
void refreshSizes()
Definition QCustomSplitter.h:126
int availableSize() const
Definition QCustomSplitter.h:77
QCustomSplitter(QWidget *parent=nullptr)
Definition QCustomSplitter.h:46
void setAnimating(bool animating)
Definition QCustomSplitter.h:57
fltp08 sizeMultiple()
Definition QCustomSplitter.h:65
virtual ~QCustomSplitter()
Definition QCustomSplitter.h:54
Definition ACIColor.h:37
int32_t sint04
-Defines an alias representing a 4 byte, signed integer. -Can represent exact integer values -2147483...
Definition BaseValues.hpp:64
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 > ...
Definition BaseFunctions.hpp:94
int64_t sint08
-Defines an alias representing an 8 byte, signed integer -Can represent exact integer values -9223372...
Definition BaseValues.hpp:71
double fltp08
Defines an alias representing an 8 byte floating-point number.
Definition BaseValues.hpp:149