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

318 lines
10 KiB
C++
Raw Normal View History

2014-07-07 00:19:45 +08:00
/*
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
2014-07-07 00:19:45 +08:00
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.
qTox is distributed in the hope that it will be useful,
2014-07-07 00:19:45 +08:00
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.
2014-07-07 00:19:45 +08:00
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
2014-07-07 00:19:45 +08:00
*/
2014-06-25 04:11:11 +08:00
#include "friendwidget.h"
2014-10-08 12:26:25 +08:00
#include "src/group.h"
#include "src/grouplist.h"
#include "groupwidget.h"
2014-10-08 12:26:25 +08:00
#include "src/friendlist.h"
#include "src/friend.h"
#include "src/core/core.h"
2014-10-08 12:26:25 +08:00
#include "form/chatform.h"
#include "maskablepixmapwidget.h"
2015-06-06 07:44:47 +08:00
#include "src/widget/tool/croppinglabel.h"
#include "src/widget/style.h"
#include "src/persistence/settings.h"
2014-11-08 00:50:39 +08:00
#include "src/widget/widget.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>
#include <QBitmap>
#include <QFileDialog>
#include <QDebug>
#include <QInputDialog>
2014-06-25 04:11:11 +08:00
2015-05-29 21:26:43 +08:00
#include "circlewidget.h"
#include "friendlistwidget.h"
#include <cassert>
2014-06-25 04:11:11 +08:00
FriendWidget::FriendWidget(int FriendId, QString id)
: friendId(FriendId)
, isDefaultAvatar{true}
, historyLoaded{false}
2014-06-25 04:11:11 +08:00
{
avatar->setPixmap(QPixmap(":img/contact.svg"), Qt::transparent);
statusPic.setPixmap(QPixmap(":img/status/dot_offline.svg"));
2015-03-18 07:45:57 +08:00
statusPic.setMargin(3);
2014-10-01 17:13:49 +08:00
nameLabel->setText(id);
nameLabel->setTextFormat(Qt::PlainText);
connect(nameLabel, &CroppingLabel::editFinished, this, &FriendWidget::setAlias);
statusMessageLabel->setTextFormat(Qt::PlainText);
2014-06-25 04:11:11 +08:00
}
void FriendWidget::contextMenuEvent(QContextMenuEvent * event)
{
QPoint pos = event->globalPos();
ToxId id = FriendList::findFriend(friendId)->getToxId();
QString dir = Settings::getInstance().getAutoAcceptDir(id);
2014-06-25 04:11:11 +08:00
QMenu menu;
2014-10-20 12:25:52 +08:00
QMenu* inviteMenu = menu.addMenu(tr("Invite to group","Menu to invite a friend to a groupchat"));
QMenu* circleMenu = menu.addMenu(tr("Move to circle...", "Menu to move a friend into a different circle"));
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"));
QMap<QAction*, Group*> groupActions;
2015-05-29 06:36:21 +08:00
2014-11-20 17:13:13 +08:00
for (Group* group : GroupList::getAllGroups())
{
2014-11-23 23:22:29 +08:00
QAction* groupAction = inviteMenu->addAction(group->getGroupWidget()->getName());
groupActions[groupAction] = group;
}
2015-05-29 06:36:21 +08:00
if (groupActions.isEmpty())
inviteMenu->setEnabled(false);
2015-05-29 06:36:21 +08:00
2015-05-29 21:26:43 +08:00
CircleWidget *circleWidget = dynamic_cast<CircleWidget*>(parentWidget());
QAction* newCircleAction = circleMenu->addAction(tr("To new circle"));
QAction *removeCircleAction;
if (circleWidget != nullptr)
removeCircleAction = circleMenu->addAction(tr("Remove from this circle"));
2015-05-29 06:36:21 +08:00
circleMenu->addSeparator();
2015-05-29 21:26:43 +08:00
FriendListWidget *friendList;
if (circleWidget == nullptr)
friendList = dynamic_cast<FriendListWidget*>(parentWidget());
else
friendList = dynamic_cast<FriendListWidget*>(circleWidget->parentWidget());
assert(friendList != nullptr);
QVector<CircleWidget*> circleVec = friendList->getAllCircles();
QMap<QAction*, CircleWidget*> circleActions;
for (CircleWidget* circle : circleVec)
{
if (circle != circleWidget)
{
QAction* circleAction = circleMenu->addAction(tr("Move to circle \"%1\"").arg(circle->getName()));
circleActions[circleAction] = circle;
}
2015-05-29 21:26:43 +08:00
}
QAction* setAlias = menu.addAction(tr("Set alias..."));
2014-10-20 12:25:52 +08:00
menu.addSeparator();
QAction* autoAccept = menu.addAction(tr("Auto accept files from this friend", "context menu entry"));
autoAccept->setCheckable(true);
autoAccept->setChecked(!dir.isEmpty());
menu.addSeparator();
2015-05-29 06:36:21 +08:00
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;
} else if (selectedItem == setAlias)
{
nameLabel->editStart();
}
2014-07-04 22:41:29 +08:00
else if (selectedItem == removeFriendAction)
2014-06-25 04:11:11 +08:00
{
emit removeFriend(friendId);
return;
}
else if (selectedItem == autoAccept)
{
if (!autoAccept->isChecked())
{
qDebug() << "not checked";
dir = QDir::homePath();
autoAccept->setChecked(false);
Settings::getInstance().setAutoAcceptDir(id, "");
}
2015-05-29 06:36:21 +08:00
if (autoAccept->isChecked())
{
dir = QFileDialog::getExistingDirectory(0, tr("Choose an auto accept directory","popup title"), dir);
autoAccept->setChecked(true);
qDebug() << "setting auto accept dir for" << friendId << "to" << dir;
Settings::getInstance().setAutoAcceptDir(id, dir);
}
}
2015-05-29 21:26:43 +08:00
else if (selectedItem == newCircleAction)
{
if (circleWidget != nullptr)
circleWidget->updateStatus();
friendList->addCircleWidget(this);
2015-05-29 21:26:43 +08:00
}
else if (groupActions.contains(selectedItem))
{
Group* group = groupActions[selectedItem];
2014-11-23 23:22:29 +08:00
Core::getInstance()->groupInviteFriend(friendId, group->getGroupId());
}
2015-05-29 21:26:43 +08:00
else if (removeCircleAction != nullptr && selectedItem == removeCircleAction)
{
friendList->moveWidget(this, FriendList::findFriend(friendId)->getStatus(), true);
}
else if (circleActions.contains(selectedItem))
{
if (circleWidget != nullptr)
circleWidget->updateStatus();
2015-05-29 21:26:43 +08:00
CircleWidget* circle = circleActions[selectedItem];
circle->addFriendWidget(this, FriendList::findFriend(friendId)->getStatus());
circle->expand();
}
2014-06-25 04:11:11 +08:00
}
}
2014-06-27 09:47:16 +08:00
void FriendWidget::setAsActiveChatroom()
{
setActive(true);
2014-06-28 02:11:32 +08:00
if (isDefaultAvatar)
avatar->setPixmap(QPixmap(":img/contact_dark.svg"), Qt::transparent);
}
2014-06-27 09:47:16 +08:00
void FriendWidget::setAsInactiveChatroom()
{
setActive(false);
2014-06-28 02:11:32 +08:00
if (isDefaultAvatar)
avatar->setPixmap(QPixmap(":img/contact.svg"), Qt::transparent);
}
2014-08-22 23:19:16 +08:00
void FriendWidget::updateStatusLight()
{
Friend* f = FriendList::findFriend(friendId);
2014-11-06 23:26:22 +08:00
Status status = f->getStatus();
2014-11-06 23:26:22 +08:00
if (status == Status::Online && f->getEventFlag() == 0)
statusPic.setPixmap(QPixmap(":img/status/dot_online.svg"));
2014-11-06 23:26:22 +08:00
else if (status == Status::Online && f->getEventFlag() == 1)
statusPic.setPixmap(QPixmap(":img/status/dot_online_notification.svg"));
2014-11-06 23:26:22 +08:00
else if (status == Status::Away && f->getEventFlag() == 0)
statusPic.setPixmap(QPixmap(":img/status/dot_away.svg"));
2014-11-06 23:26:22 +08:00
else if (status == Status::Away && f->getEventFlag() == 1)
statusPic.setPixmap(QPixmap(":img/status/dot_away_notification.svg"));
2014-11-06 23:26:22 +08:00
else if (status == Status::Busy && f->getEventFlag() == 0)
statusPic.setPixmap(QPixmap(":img/status/dot_busy.svg"));
2014-11-06 23:26:22 +08:00
else if (status == Status::Busy && f->getEventFlag() == 1)
statusPic.setPixmap(QPixmap(":img/status/dot_busy_notification.svg"));
2014-11-06 23:26:22 +08:00
else if (status == Status::Offline && f->getEventFlag() == 0)
statusPic.setPixmap(QPixmap(":img/status/dot_offline.svg"));
2014-11-06 23:26:22 +08:00
else if (status == Status::Offline && f->getEventFlag() == 1)
statusPic.setPixmap(QPixmap(":img/status/dot_offline_notification.svg"));
2015-03-18 07:45:57 +08:00
if (f->getEventFlag())
{
CircleWidget* circleWidget = dynamic_cast<CircleWidget*>(parentWidget());
if (circleWidget != nullptr)
circleWidget->expand();
}
2015-03-18 07:51:22 +08:00
if (!f->getEventFlag())
2015-03-18 07:45:57 +08:00
statusPic.setMargin(3);
else
statusPic.setMargin(0);
}
2015-03-26 03:08:46 +08:00
QString FriendWidget::getStatusString()
{
Friend* f = FriendList::findFriend(friendId);
Status status = f->getStatus();
if (f->getEventFlag() == 1)
return tr("New message");
else if (status == Status::Online)
return tr("Online");
else if (status == Status::Away)
return tr("Away");
else if (status == Status::Busy)
return tr("Busy");
else if (status == Status::Offline)
return tr("Offline");
return QString::null;
}
void FriendWidget::setChatForm(Ui::MainWindow &ui)
{
Friend* f = FriendList::findFriend(friendId);
2014-11-06 23:26:22 +08:00
f->getChatForm()->show(ui);
}
void FriendWidget::resetEventFlags()
{
Friend* f = FriendList::findFriend(friendId);
2014-11-06 23:26:22 +08:00
f->setEventFlag(false);
}
2014-09-24 23:28:38 +08:00
void FriendWidget::onAvatarChange(int FriendId, const QPixmap& pic)
{
if (FriendId != friendId)
return;
isDefaultAvatar = false;
avatar->setPixmap(pic);
avatar->autopickBackground();
2014-09-24 23:28:38 +08:00
}
2014-09-26 00:03:25 +08:00
2014-09-27 03:23:20 +08:00
void FriendWidget::onAvatarRemoved(int FriendId)
{
if (FriendId != friendId)
return;
isDefaultAvatar = true;
2014-10-05 20:22:28 +08:00
if (isActive())
avatar->setPixmap(QPixmap(":img/contact_dark.svg"), Qt::transparent);
2014-10-05 20:22:28 +08:00
else
avatar->setPixmap(QPixmap(":img/contact.svg"), Qt::transparent);
2014-09-27 03:23:20 +08:00
}
2014-09-26 00:03:25 +08:00
void FriendWidget::mousePressEvent(QMouseEvent *ev)
{
if (ev->button() == Qt::LeftButton)
dragStartPos = ev->pos();
GenericChatroomWidget::mousePressEvent(ev);
2014-09-26 00:03:25 +08:00
}
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);
drag->setPixmap(avatar->getPixmap());
2014-09-26 00:03:25 +08:00
drag->exec(Qt::CopyAction | Qt::MoveAction);
}
}
2014-11-07 22:55:32 +08:00
void FriendWidget::setAlias(const QString& _alias)
{
QString alias = _alias.left(128); // same as TOX_MAX_NAME_LENGTH
Friend* f = FriendList::findFriend(friendId);
f->setAlias(alias);
Settings::getInstance().setFriendAlias(f->getToxId(), alias);
Settings::getInstance().savePersonal();
hide();
show();
}