/
MosaicLayout.cpp
171 lines (149 loc) · 3.97 KB
/
MosaicLayout.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
159
160
161
162
163
164
165
166
167
168
169
// This file is part of MosaicLayout.
//
// MosaicLayout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// MosaicLayout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with MosaicLayout. If not, see <http://www.gnu.org/licenses/>.
#include "MosaicLayout.h"
#include <QStyle>
MosaicLayout::MosaicLayout(QWidget* parent): QLayout(parent)
{
setContentsMargins(9,9,9,9);
setSpacing(4);
minItemHSize = 100;
minItemVSize = 100;
maxItemHSize = 160;
maxItemVSize = 160;
}
MosaicLayout::MosaicLayout()
{
setContentsMargins(9,9,9,9);
setSpacing(4);
minItemHSize = 100;
minItemVSize = 100;
maxItemHSize = 160;
maxItemVSize = 160;
}
MosaicLayout::~MosaicLayout()
{
QLayoutItem *item;
while ((item = takeAt(0)))
delete item;
}
void MosaicLayout::removeAllItems()
{
itemsList.clear();
}
void MosaicLayout::refreshLayout(const QRect& rect)
{
//item size
int itemHSize, itemVSize;
int totalSpacing = spacing() * itemsList.count();
// horizontal max number of items
int HMaxItems = contentsRect().width() / (minItemHSize + totalSpacing);
bool oneRow = false;
if (HMaxItems >= itemsList.count())
{
HMaxItems = itemsList.count();
if (HMaxItems == 0)
HMaxItems += 1; // one avoid division by 0
oneRow = true;
}
// horizontal dimension that could be added to each item
// since there is room
int HPlusSize = ( contentsRect().width() -
(minItemHSize * HMaxItems) ) / HMaxItems;
//TODO: check this, too many operations
if (oneRow)
{
if (minItemHSize + HPlusSize > maxItemHSize)
itemHSize = maxItemHSize;
else
itemHSize = minItemHSize + HPlusSize;
}
else {
if (HPlusSize + minItemHSize > maxItemHSize)
itemHSize = minItemHSize;
else
itemHSize = minItemHSize + HPlusSize;
}
//repeat same story for vertical size
int VMaxItems = contentsRect().height() / (minItemVSize + totalSpacing);
int VPlusSize = ( contentsRect().height() %
(minItemVSize * VMaxItems) ) / VMaxItems;
if (VPlusSize + minItemVSize > maxItemVSize)
itemVSize = minItemVSize;
else
itemVSize = minItemVSize + VPlusSize;
//next lines add widgets to layout
// absolute position to set
int x = 0;
int y = 0;
// count of horizontal added items,
// increment it for each added item and
// when it reachs HMaxItems set it and
// x to zero
int HCountItem = 0;
QLayoutItem *item;
foreach (item, itemsList) {
item->setGeometry(QRect(QPoint(x,y),QPoint(itemHSize+x,itemVSize+y)));
HCountItem++;
if (HCountItem >= HMaxItems) {
HCountItem = x = 0;
y += itemVSize + spacing();
}
else
x += itemHSize + spacing();
}
}
void MosaicLayout::setGeometry(const QRect& rect)
{
QLayout::setGeometry(rect);
refreshLayout(rect);
}
void MosaicLayout::setItemsMinDimension(int x, int y)
{
minItemHSize = x;
minItemVSize = y;
}
void MosaicLayout::setItemsMaxDimension(int x, int y)
{
maxItemHSize = x;
minItemVSize = y;
}
int MosaicLayout::count() const
{
return itemsList.count();
}
//TODO: Implement this some day
QLayoutItem* MosaicLayout::itemAt(int index) const
{
MosaicLayout* a = NULL;
return a;
}
void MosaicLayout::addItem(QLayoutItem* item)
{
itemsList.append(item);
}
//TODO: Implement this some day
QSize MosaicLayout::sizeHint() const
{
QSize a;
return a;
}
//TODO: Implement this some day
QLayoutItem* MosaicLayout::takeAt(int index)
{
if (index >= 0 && index < itemsList.size())
return itemsList.takeAt(index);
else
return 0;
}