-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathgridwidget.cpp
162 lines (145 loc) · 4.31 KB
/
gridwidget.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
/*
* Copyright (C) 2023, Bjørn D. Rasmussen, BearWare.dk
*
* This program 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "gridwidget.h"
#include <math.h>
GridWidget::GridWidget(QWidget * parent)
: QWidget(parent)
{
m_grid = new QGridLayout(this);
}
void GridWidget::addUserWidget(int userid, UserImageWidget* widget)
{
m_activewidgets.insert(userid, widget);
RepositionWidgets();
}
void GridWidget::removeUserWidget(int userid)
{
userwidgets_t::iterator ite = m_activewidgets.find(userid);
if(ite != m_activewidgets.end())
{
QWidget* vid = *ite;
m_activewidgets.erase(ite);
vid->close();
RepositionWidgets();
}
}
UserImageWidget* GridWidget::getUserWidget(int userid) const
{
userwidgets_t::const_iterator ite = m_activewidgets.find(userid);
if(ite != m_activewidgets.end())
return *ite;
return nullptr;
}
bool GridWidget::userExists(int userid) const
{
return m_activewidgets.find(userid) != m_activewidgets.end();
}
int GridWidget::selectedUser() const
{
userwidgets_t::const_iterator ite = m_activewidgets.begin();
while(ite != m_activewidgets.end())
{
if((*ite)->hasFocus())
return ite.key();
ite++;
}
return -1;
}
QSize GridWidget::getUserImageSize(int userid) const
{
UserImageWidget* widget = getUserWidget(userid);
if(widget)
return widget->imageSize();
return QSize();
}
void GridWidget::paintEvent(QPaintEvent *p)
{
QWidget::paintEvent(p);
if(m_activewidgets.isEmpty() && !getEmptyGridText().isEmpty())
{
QPainter painter(this);
painter.drawText(rect(), Qt::AlignCenter, getEmptyGridText());
}
}
QVector<int> GridWidget::activeUsers() const
{
QVector<int> users;
userwidgets_t::const_iterator ite = m_activewidgets.begin();
while(ite != m_activewidgets.end())
{
users.push_back(ite.key());
ite++;
}
return users;
}
int GridWidget::getActiveUsersCount() const
{
return m_activewidgets.size();
}
void GridWidget::resetGrid()
{
QVector<int> userids = activeUsers();
for(int i=0;i<userids.size();i++)
removeUserWidget(userids[i]);
}
void GridWidget::RepositionWidgets()
{
int colums = (int)sqrt((double)m_activewidgets.size());
int rows = (int)sqrt((double)m_activewidgets.size());
rows = (m_activewidgets.size() <= rows*colums)? rows : rows + 1;
colums = (m_activewidgets.size() <= rows*colums)? colums : colums + 1;
if(m_grid->columnCount() > colums || m_grid->rowCount() > rows)
{
int x = m_grid->columnCount();
for(int r = m_grid->rowCount()-1;r>=0;r--)
{
while(x > colums)
{
QLayoutItem* item = m_grid->itemAtPosition(r, x);
if(item)
m_grid->removeItem(item);
x--;
}
}
int y = m_grid->rowCount();
for(int c = m_grid->columnCount()-1;c>=0;c--)
{
while(y > rows)
{
QLayoutItem* item = m_grid->itemAtPosition(c, y);
if(item)
m_grid->removeItem(item);
y--;
}
}
}
userwidgets_t::iterator ite = m_activewidgets.begin();
for(int x=0;x<colums;x++)
{
for(int y=0;y<rows;y++)
{
QLayoutItem* item = m_grid->itemAtPosition(y, x);
if(item)
m_grid->removeItem(item);
if(ite != m_activewidgets.end())
{
m_grid->addWidget(*ite, y, x);
ite++;
}
}
}
}