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

234 lines
7.2 KiB
C++
Raw Normal View History

2014-07-07 00:19:45 +08:00
/*
Copyright (C) 2014 by Project Tox <https://tox.im>
This file is part of qTox, a Qt-based graphical interface for Tox.
This program is libre 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 COPYING file for more details.
*/
2014-06-25 04:11:11 +08:00
#include "friendwidget.h"
#include "group.h"
#include "grouplist.h"
#include "groupwidget.h"
#include "friendlist.h"
#include "friend.h"
2014-09-11 21:44:34 +08:00
#include "core.h"
#include "widget/form/chatform.h"
2014-06-25 04:11:11 +08:00
#include <QContextMenuEvent>
#include <QMenu>
2014-09-26 00:03:25 +08:00
#include <QDrag>
#include <QMimeData>
#include <QApplication>
2014-06-25 04:11:11 +08:00
FriendWidget::FriendWidget(int FriendId, QString id)
: friendId(FriendId), isDefaultAvatar{true}
2014-06-25 04:11:11 +08:00
{
2014-08-12 01:46:14 +08:00
setMouseTracking(true);
setAutoFillBackground(true);
setFixedHeight(55);
setLayout(&layout);
2014-06-25 04:11:11 +08:00
layout.setSpacing(0);
layout.setMargin(0);
layout.setStretchFactor(this, 100);
2014-06-25 04:11:11 +08:00
textLayout.setSpacing(0);
textLayout.setMargin(0);
2014-08-12 01:46:14 +08:00
setLayoutDirection(Qt::LeftToRight); // parent might have set Qt::RightToLeft
2014-06-25 04:11:11 +08:00
avatar.setPixmap(QPixmap(":img/contact.png"));
2014-06-25 04:11:11 +08:00
name.setText(id);
2014-06-28 02:11:32 +08:00
//statusPic.setAlignment(Qt::AlignHCenter);
statusPic.setPixmap(QPixmap(":img/status/dot_away.png"));
2014-06-25 04:11:11 +08:00
QFont small;
small.setPixelSize(10);
statusMessage.setFont(small);
QPalette pal;
pal.setColor(QPalette::WindowText,Qt::gray);
statusMessage.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
textLayout.addStretch();
textLayout.addWidget(&name);
textLayout.addWidget(&statusMessage);
textLayout.addStretch();
layout.addSpacing(20);
layout.addWidget(&avatar);
layout.addSpacing(5);
layout.addLayout(&textLayout);
layout.addSpacing(5);
layout.addWidget(&statusPic);
layout.addSpacing(5);
2014-06-28 02:11:32 +08:00
2014-06-28 02:22:53 +08:00
isActiveWidget = 0;
layout.invalidate();
layout.update();
layout.activate();
updateGeometry();
2014-06-25 04:11:11 +08:00
}
void FriendWidget::contextMenuEvent(QContextMenuEvent * event)
{
QPoint pos = event->globalPos();
QMenu menu;
2014-07-04 22:41:29 +08:00
QAction* copyId = menu.addAction(tr("Copy friend ID","Menu to copy the Tox ID of that friend"));
2014-07-04 03:42:23 +08:00
QMenu* inviteMenu = menu.addMenu(tr("Invite in group","Menu to invite a friend in a groupchat"));
QMap<QAction*, Group*> groupActions;
for (Group* group : GroupList::groupList)
{
QAction* groupAction = inviteMenu->addAction(group->widget->name.text());
groupActions[groupAction] = group;
}
if (groupActions.isEmpty())
inviteMenu->setEnabled(false);
menu.addSeparator();
2014-07-04 22:41:29 +08:00
QAction* removeFriendAction = menu.addAction(tr("Remove friend", "Menu to remove the friend from our friendlist"));
2014-06-25 04:11:11 +08:00
QAction* selectedItem = menu.exec(pos);
if (selectedItem)
{
2014-07-04 22:41:29 +08:00
if (selectedItem == copyId)
{
emit copyFriendIdToClipboard(friendId);
return;
}
2014-07-04 22:41:29 +08:00
else if (selectedItem == removeFriendAction)
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-06-25 04:11:11 +08:00
hide();
emit removeFriend(friendId);
return;
}
else if (groupActions.contains(selectedItem))
{
Group* group = groupActions[selectedItem];
2014-09-11 21:44:34 +08:00
Core::getInstance()->groupInviteFriend(friendId, group->groupId);
}
2014-06-25 04:11:11 +08:00
}
}
2014-06-27 09:47:16 +08:00
void FriendWidget::setAsActiveChatroom()
{
2014-06-28 02:11:32 +08:00
isActiveWidget = 1;
QFont small;
small.setPixelSize(10);
statusMessage.setFont(small);
QPalette pal;
pal.setColor(QPalette::WindowText,Qt::darkGray);
statusMessage.setPalette(pal);
QPalette pal2;
pal2.setColor(QPalette::WindowText,Qt::black);
name.setPalette(pal2);
QPalette pal3;
pal3.setColor(QPalette::Background, Qt::white);
this->setPalette(pal3);
if (isDefaultAvatar)
avatar.setPixmap(QPixmap(":img/contact_dark.png"));
}
2014-06-27 09:47:16 +08:00
void FriendWidget::setAsInactiveChatroom()
{
2014-06-28 02:11:32 +08:00
isActiveWidget = 0;
QFont small;
small.setPixelSize(10);
statusMessage.setFont(small);
QPalette pal;
pal.setColor(QPalette::WindowText,Qt::gray);
statusMessage.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));
this->setPalette(pal3);
if (isDefaultAvatar)
avatar.setPixmap(QPixmap(":img/contact.png"));
}
2014-08-22 23:19:16 +08:00
void FriendWidget::updateStatusLight()
{
Friend* f = FriendList::findFriend(friendId);
Status status = f->friendStatus;
if (status == Status::Online && f->hasNewEvents == 0)
statusPic.setPixmap(QPixmap(":img/status/dot_online.png"));
else if (status == Status::Online && f->hasNewEvents == 1)
statusPic.setPixmap(QPixmap(":img/status/dot_online_notification.png"));
else if (status == Status::Away && f->hasNewEvents == 0)
statusPic.setPixmap(QPixmap(":img/status/dot_idle.png"));
else if (status == Status::Away && f->hasNewEvents == 1)
statusPic.setPixmap(QPixmap(":img/status/dot_idle_notification.png"));
else if (status == Status::Busy && f->hasNewEvents == 0)
statusPic.setPixmap(QPixmap(":img/status/dot_busy.png"));
else if (status == Status::Busy && f->hasNewEvents == 1)
statusPic.setPixmap(QPixmap(":img/status/dot_busy_notification.png"));
else if (status == Status::Offline && f->hasNewEvents == 0)
statusPic.setPixmap(QPixmap(":img/status/dot_away.png"));
else if (status == Status::Offline && f->hasNewEvents == 1)
statusPic.setPixmap(QPixmap(":img/status/dot_away_notification.png"));
}
void FriendWidget::setChatForm(Ui::MainWindow &ui)
{
Friend* f = FriendList::findFriend(friendId);
f->chatForm->show(ui);
}
void FriendWidget::resetEventFlags()
{
Friend* f = FriendList::findFriend(friendId);
f->hasNewEvents = 0;
}
2014-09-24 23:28:38 +08:00
void FriendWidget::onAvatarChange(int FriendId, const QPixmap& pic)
{
if (FriendId != friendId)
return;
isDefaultAvatar = false;
2014-09-24 23:42:10 +08:00
QPixmap scaled = pic.scaled(40,40,Qt::KeepAspectRatio,Qt::SmoothTransformation);
avatar.setPixmap(scaled);
2014-09-24 23:28:38 +08:00
}
2014-09-26 00:03:25 +08:00
void FriendWidget::mousePressEvent(QMouseEvent *ev)
{
if (ev->button() == Qt::LeftButton)
dragStartPos = ev->pos();
}
void FriendWidget::mouseMoveEvent(QMouseEvent *ev)
{
if (!(ev->buttons() & Qt::LeftButton))
return;
if ((dragStartPos - ev->pos()).manhattanLength() > QApplication::startDragDistance())
{
QDrag* drag = new QDrag(this);
QMimeData* mdata = new QMimeData;
mdata->setData("friend", QString::number(friendId).toLatin1());
drag->setMimeData(mdata);
if (avatar.pixmap())
drag->setPixmap(*avatar.pixmap());
drag->exec(Qt::CopyAction | Qt::MoveAction);
}
}