forked from mika314/texteditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayout.cpp
158 lines (145 loc) · 3.61 KB
/
layout.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "layout.hpp"
#include "widget.hpp"
#include <limits>
#include <algorithm>
Layout::Layout(Style style):
style_(style),
left_(0),
top_(0),
width_(0),
height_(0)
{
}
void Layout::addLayoutable(Layoutable *value)
{
layoutablesList_.push_back(value);
value->setParentLayout(this);
resize(width_, height_);
}
void Layout::removeLayoutable(Layoutable *value)
{
auto iter = std::find(begin(layoutablesList_), end(layoutablesList_), value);
if (iter != end(layoutablesList_))
{
layoutablesList_.erase(iter);
resize(width_, height_);
}
}
void Layout::setLeft(int value)
{
left_ = value;
}
void Layout::setTop(int value)
{
top_ = value;
}
static std::vector<int> calculateDimensions(std::vector<std::pair<int, int>> minMax, int dimension)
{
std::vector<int> result;
std::vector<bool> untouchableList;
for (size_t i = 0; i < minMax.size(); ++i)
{
result.push_back((i + 1) * dimension / minMax.size() - i * dimension / minMax.size());
untouchableList.push_back(false);
}
auto touchableCount = minMax.size();
auto remDim = dimension;
auto resultIter = begin(result);
auto untouchableListIter = begin(untouchableList);
for (auto &l: minMax)
{
if (*resultIter < l.first)
{
*resultIter = l.first;
*untouchableListIter = true;
--touchableCount;
remDim -= *resultIter;
}
else if (*resultIter > l.second)
{
*resultIter = l.second;
*untouchableListIter = true;
--touchableCount;
remDim -= *resultIter;
}
++resultIter;
++untouchableListIter;
}
untouchableListIter = begin(untouchableList);
int i = 0;
for (auto &dim: result)
{
if (!*untouchableListIter)
dim = (i + 1) * remDim / touchableCount - i * remDim / touchableCount;
++i;
++untouchableListIter;
}
return result;
}
void Layout::resize(int width, int height)
{
width_ = width;
height_ = height;
if (style_ == Vertical)
{
std::vector<std::pair<int, int> > minMax;
for (auto &l: layoutablesList_)
minMax.push_back(std::make_pair(l->minHeight(), l->maxHeight()));
auto heights = calculateDimensions(minMax, height_);
auto heightsIter = begin(heights);
auto t = top_;
for (auto &l: layoutablesList_)
{
l->setLeft(left_);
l->setTop(t);
l->resize(width, *heightsIter);
t += *heightsIter;
++heightsIter;
}
}
else // if style == Horizontal
{
std::vector<std::pair<int, int> > minMax;
for (auto &l: layoutablesList_)
minMax.push_back(std::make_pair(l->minWidth(), l->maxWidth()));
auto widths = calculateDimensions(minMax, width_);
auto widthsIter = begin(widths);
auto lt = left_;
for (auto &l: layoutablesList_)
{
l->setLeft(lt);
l->setTop(top_);
l->resize(*widthsIter, height_);
lt += *widthsIter;
++widthsIter;
}
}
}
int Layout::maxHeight() const
{
return std::numeric_limits<int>::max();
}
int Layout::minHeight() const
{
return 0;
}
int Layout::maxWidth() const
{
return std::numeric_limits<int>::max();
}
int Layout::minWidth() const
{
return 0;
}
void Layout::setStyle(Style value)
{
if (style_ != value)
{
style_ = value;
resize(width_, height_);
}
}
std::vector<Layoutable *> Layout::children() const
{
return layoutablesList_;
}