mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
refactor: onUserListChanged refactored
This commit is contained in:
parent
26b16386ae
commit
a29e80bbe0
|
@ -97,18 +97,6 @@ GroupChatForm::GroupChatForm(Group* chatGroup)
|
|||
msgEdit->setObjectName("group");
|
||||
|
||||
namesListLayout = new FlowLayout(0, 5, 0);
|
||||
|
||||
const QStringList names(group->getPeerList());
|
||||
for (const QString& fullName : names) {
|
||||
const QString editedName = editName(fullName);
|
||||
QLabel* l = new QLabel(editedName);
|
||||
if (editedName != fullName) {
|
||||
l->setToolTip(fullName);
|
||||
}
|
||||
l->setTextFormat(Qt::PlainText);
|
||||
namesListLayout->addWidget(l);
|
||||
}
|
||||
|
||||
headTextLayout->addWidget(nusersLabel);
|
||||
headTextLayout->addLayout(namesListLayout);
|
||||
headTextLayout->addStretch();
|
||||
|
@ -165,58 +153,21 @@ void GroupChatForm::onSendTriggered()
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This slot is intended to connect to Group::userListChanged signal.
|
||||
* Brief list of actions made by slot:
|
||||
* 1) sets text of how many people are in the group;
|
||||
* 2) creates lexicographically sorted comma-separated list of user names, each name in its own
|
||||
* label;
|
||||
* 3) sets call button style depending on peer count and etc.
|
||||
*/
|
||||
void GroupChatForm::onUserListChanged()
|
||||
{
|
||||
int peersCount = group->getPeersCount();
|
||||
if (peersCount == 1)
|
||||
nusersLabel->setText(tr("1 user in chat", "Number of users in chat"));
|
||||
else
|
||||
nusersLabel->setText(tr("%1 users in chat", "Number of users in chat").arg(peersCount));
|
||||
|
||||
QLayoutItem* child;
|
||||
while ((child = namesListLayout->takeAt(0))) {
|
||||
child->widget()->hide();
|
||||
delete child->widget();
|
||||
delete child;
|
||||
}
|
||||
peerLabels.clear();
|
||||
|
||||
// the list needs peers in peernumber order, nameLayout needs alphabetical
|
||||
QList<QLabel*> nickLabelList;
|
||||
|
||||
// first traverse in peer number order, storing the QLabels as necessary
|
||||
QStringList names = group->getPeerList();
|
||||
unsigned nNames = names.size();
|
||||
for (unsigned i = 0; i < nNames; ++i) {
|
||||
const QString editedName = editName(names[i]);
|
||||
peerLabels.append(new QLabel(editedName));
|
||||
if (editedName != names[i]) {
|
||||
peerLabels[i]->setToolTip(names[i]);
|
||||
}
|
||||
peerLabels[i]->setTextFormat(Qt::PlainText);
|
||||
nickLabelList.append(peerLabels[i]);
|
||||
if (group->isSelfPeerNumber(i))
|
||||
peerLabels[i]->setStyleSheet("QLabel {color : green;}");
|
||||
|
||||
if (netcam && !group->isSelfPeerNumber(i))
|
||||
static_cast<GroupNetCamView*>(netcam)->addPeer(i, names[i]);
|
||||
}
|
||||
|
||||
if (netcam)
|
||||
static_cast<GroupNetCamView*>(netcam)->clearPeers();
|
||||
|
||||
// now alphabetize and add to layout
|
||||
qSort(nickLabelList.begin(), nickLabelList.end(),
|
||||
[](QLabel* a, QLabel* b) { return a->text().toLower() < b->text().toLower(); });
|
||||
for (unsigned i = 0; i < nNames; ++i) {
|
||||
QLabel* label = nickLabelList.at(i);
|
||||
if (i != nNames - 1)
|
||||
label->setText(label->text() + ", ");
|
||||
|
||||
namesListLayout->addWidget(label);
|
||||
}
|
||||
updateUserCount();
|
||||
updateUserNames();
|
||||
|
||||
// Enable or disable call button
|
||||
const int peersCount = group->getPeersCount();
|
||||
if (peersCount > 1 && group->isAvGroupchat()) {
|
||||
// don't set button to green if call running
|
||||
if (!inCall) {
|
||||
|
@ -235,9 +186,64 @@ void GroupChatForm::onUserListChanged()
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Updates user names' labels at the top of group chat
|
||||
*/
|
||||
void GroupChatForm::updateUserNames()
|
||||
{
|
||||
QLayoutItem* child;
|
||||
while ((child = namesListLayout->takeAt(0))) {
|
||||
child->widget()->hide();
|
||||
delete child->widget();
|
||||
delete child;
|
||||
}
|
||||
|
||||
peerLabels.clear();
|
||||
const int peersCount = group->getPeersCount();
|
||||
peerLabels.reserve(peersCount);
|
||||
QVector<QLabel*> nickLabelList(peersCount);
|
||||
|
||||
/* the list needs peers in peernumber order, nameLayout needs alphabetical
|
||||
* first traverse in peer number order, storing the QLabels as necessary */
|
||||
const QStringList names = group->getPeerList();
|
||||
int peerNumber = 0;
|
||||
for (const QString& fullName : names) {
|
||||
const QString editedName = editName(fullName).append(QLatin1String(", "));
|
||||
QLabel* const label = new QLabel(editedName);
|
||||
if (editedName != fullName) {
|
||||
label->setToolTip(fullName);
|
||||
}
|
||||
label->setTextFormat(Qt::PlainText);
|
||||
if (group->isSelfPeerNumber(peerNumber)) {
|
||||
label->setStyleSheet(QStringLiteral("QLabel {color : green;}"));
|
||||
} else if (netcam != nullptr) {
|
||||
static_cast<GroupNetCamView*>(netcam)->addPeer(peerNumber, fullName);
|
||||
}
|
||||
peerLabels.append(label);
|
||||
nickLabelList[peerNumber++] = label;
|
||||
}
|
||||
|
||||
if (netcam != nullptr) {
|
||||
static_cast<GroupNetCamView*>(netcam)->clearPeers();
|
||||
}
|
||||
|
||||
qSort(nickLabelList.begin(), nickLabelList.end(), [](const QLabel* a, const QLabel* b)
|
||||
{
|
||||
return a->text().toLower() < b->text().toLower();
|
||||
});
|
||||
// remove comma from last sorted label
|
||||
QLabel* const lastLabel = nickLabelList.last();
|
||||
QString labelText = lastLabel->text();
|
||||
labelText.chop(2);
|
||||
lastLabel->setText(labelText);
|
||||
for (QLabel* l : nickLabelList) {
|
||||
namesListLayout->addWidget(l);
|
||||
}
|
||||
}
|
||||
|
||||
void GroupChatForm::peerAudioPlaying(int peer)
|
||||
{
|
||||
peerLabels[peer]->setStyleSheet("QLabel {color : red;}");
|
||||
peerLabels[peer]->setStyleSheet(QStringLiteral("QLabel {color : red;}"));
|
||||
if (!peerAudioTimers[peer]) {
|
||||
peerAudioTimers[peer] = new QTimer(this);
|
||||
peerAudioTimers[peer]->setSingleShot(true);
|
||||
|
@ -401,11 +407,20 @@ void GroupChatForm::keyReleaseEvent(QKeyEvent* ev)
|
|||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Updates users' count label text
|
||||
*/
|
||||
void GroupChatForm::updateUserCount()
|
||||
{
|
||||
const int peersCount = group->getPeersCount();
|
||||
if (peersCount == 1) {
|
||||
nusersLabel->setText(tr("1 user in chat", "Number of users in chat"));
|
||||
} else {
|
||||
nusersLabel->setText(tr("%1 users in chat", "Number of users in chat").arg(peersCount));
|
||||
}
|
||||
}
|
||||
|
||||
void GroupChatForm::retranslateUi()
|
||||
{
|
||||
int peersCount = group->getPeersCount();
|
||||
if (peersCount == 1)
|
||||
nusersLabel->setText(tr("1 user in chat", "Number of users in chat"));
|
||||
else
|
||||
nusersLabel->setText(tr("%1 users in chat", "Number of users in chat").arg(peersCount));
|
||||
updateUserCount();
|
||||
}
|
||||
|
|
|
@ -57,10 +57,12 @@ protected:
|
|||
|
||||
private:
|
||||
void retranslateUi();
|
||||
void updateUserCount();
|
||||
void updateUserNames();
|
||||
|
||||
private:
|
||||
Group* group;
|
||||
QList<QLabel*> peerLabels;
|
||||
QVector<QLabel*> peerLabels;
|
||||
QMap<int, QTimer*> peerAudioTimers;
|
||||
FlowLayout* namesListLayout;
|
||||
QLabel* nusersLabel;
|
||||
|
|
Loading…
Reference in New Issue
Block a user