1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
qTox/widget/groupwidget.cpp

172 lines
4.3 KiB
C++
Raw Normal View History

2014-06-25 04:11:11 +08:00
#include "groupwidget.h"
#include "grouplist.h"
#include "group.h"
#include <QPalette>
#include <QMenu>
#include <QContextMenuEvent>
GroupWidget::GroupWidget(int GroupId, QString Name)
: groupId{GroupId}
{
this->setMouseTracking(true);
2014-06-27 09:47:16 +08:00
this->setAutoFillBackground(true);
2014-06-25 04:11:11 +08:00
this->setLayout(&layout);
this->setFixedWidth(225);
this->setFixedHeight(55);
layout.setSpacing(0);
layout.setMargin(0);
textLayout.setSpacing(0);
textLayout.setMargin(0);
avatar.setPixmap(QPixmap(":img/group.png"));
statusPic.setPixmap(QPixmap(":img/status/dot_groupchat.png"));
2014-06-25 04:11:11 +08:00
name.setText(Name);
QFont small;
small.setPixelSize(10);
nusers.setFont(small);
QPalette pal;
pal.setColor(QPalette::WindowText,Qt::gray);
nusers.setPalette(pal);
QPalette pal2;
pal2.setColor(QPalette::WindowText,Qt::white);
name.setPalette(pal2);
QPalette pal3;
pal3.setColor(QPalette::Background, QColor(65,65,65,255));
this->setPalette(pal3);
2014-06-25 04:11:11 +08:00
Group* g = GroupList::findGroup(groupId);
if (g)
nusers.setText(GroupWidget::tr("%1 users in chat").arg(g->peers.size()));
2014-06-25 04:11:11 +08:00
else
nusers.setText(GroupWidget::tr("0 users in chat"));
2014-06-25 04:11:11 +08:00
textLayout.addStretch();
textLayout.addWidget(&name);
textLayout.addWidget(&nusers);
textLayout.addStretch();
layout.addSpacing(20);
layout.addWidget(&avatar);
layout.addSpacing(5);
layout.addLayout(&textLayout);
layout.addStretch();
2014-06-28 07:10:02 +08:00
layout.addSpacing(5);
layout.addWidget(&statusPic);
layout.addSpacing(5);
2014-06-28 02:22:53 +08:00
isActiveWidget = 0;
2014-06-25 04:11:11 +08:00
}
void GroupWidget::setNewFixedWidth(int newWidth)
{
this->setFixedWidth(newWidth);
}
2014-06-25 04:11:11 +08:00
void GroupWidget::mouseReleaseEvent (QMouseEvent*)
{
emit groupWidgetClicked(this);
}
void GroupWidget::contextMenuEvent(QContextMenuEvent * event)
{
QPoint pos = event->globalPos();
QMenu menu;
2014-07-04 22:41:29 +08:00
QAction* quitGroup = menu.addAction(tr("Quit group","Menu to quit a groupchat"));
2014-06-25 04:11:11 +08:00
QAction* selectedItem = menu.exec(pos);
2014-07-04 22:41:29 +08:00
if (selectedItem == quitGroup)
2014-06-25 04:11:11 +08:00
{
hide();
show(); //Toggle visibility to work around bug of repaintEvent() not being fired on parent widget when this is hidden
2014-07-04 22:41:29 +08:00
hide();
emit removeGroup(groupId);
return;
2014-06-25 04:11:11 +08:00
}
}
2014-06-28 02:22:53 +08:00
void GroupWidget::mousePressEvent(QMouseEvent *event)
{
if ((event->buttons() & Qt::LeftButton) == Qt::LeftButton)
{
2014-06-28 07:10:02 +08:00
if (isActiveWidget)
{
QPalette pal;
pal.setColor(QPalette::Background, QColor(250,250,250,255));
this->setPalette(pal);
}
else
{
QPalette pal;
pal.setColor(QPalette::Background, QColor(85,85,85,255));
this->setPalette(pal);
}
2014-06-28 02:22:53 +08:00
}
}
void GroupWidget::enterEvent(QEvent*)
{
if (isActiveWidget != 1)
{
QPalette pal;
pal.setColor(QPalette::Background, QColor(75,75,75,255));
lastColor = this->palette().background().color();
this->setPalette(pal);
}
}
void GroupWidget::leaveEvent(QEvent*)
{
if (isActiveWidget != 1)
{
QPalette pal;
pal.setColor(QPalette::Background, lastColor);
this->setPalette(pal);
}
}
2014-06-25 04:11:11 +08:00
void GroupWidget::onUserListChanged()
{
Group* g = GroupList::findGroup(groupId);
if (g)
2014-07-04 03:42:23 +08:00
nusers.setText(tr("%1 users in chat").arg(g->nPeers));
2014-06-25 04:11:11 +08:00
else
2014-07-04 03:42:23 +08:00
nusers.setText(tr("0 users in chat"));
2014-06-25 04:11:11 +08:00
}
2014-06-27 09:47:16 +08:00
void GroupWidget::setAsActiveChatroom()
{
2014-06-28 02:22:53 +08:00
isActiveWidget = 1;
2014-06-27 09:47:16 +08:00
QFont small;
small.setPixelSize(10);
nusers.setFont(small);
QPalette pal;
pal.setColor(QPalette::WindowText,Qt::darkGray);
nusers.setPalette(pal);
QPalette pal2;
pal2.setColor(QPalette::WindowText,Qt::black);
name.setPalette(pal2);
QPalette pal3;
pal3.setColor(QPalette::Background, Qt::white);
this->setPalette(pal3);
avatar.setPixmap(QPixmap(":img/group_dark.png"));
2014-06-27 09:47:16 +08:00
}
void GroupWidget::setAsInactiveChatroom()
{
2014-06-28 02:22:53 +08:00
isActiveWidget = 0;
2014-06-27 09:47:16 +08:00
QFont small;
small.setPixelSize(10);
nusers.setFont(small);
QPalette pal;
pal.setColor(QPalette::WindowText,Qt::gray);
nusers.setPalette(pal);
QPalette pal2;
pal2.setColor(QPalette::WindowText,Qt::white);
name.setPalette(pal2);
QPalette pal3;
2014-06-28 01:34:58 +08:00
pal3.setColor(QPalette::Background, QColor(65,65,65,255));
2014-06-27 09:47:16 +08:00
this->setPalette(pal3);
avatar.setPixmap(QPixmap(":img/group.png"));
2014-06-27 09:47:16 +08:00
}