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

284 lines
7.3 KiB
C++
Raw Normal View History

2014-07-07 00:19:45 +08:00
/*
Copyright © 2014-2015 by The qTox Project Contributors
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 "groupwidget.h"
2014-06-25 04:11:11 +08:00
#include <QPalette>
#include <QMenu>
#include <QContextMenuEvent>
2014-10-16 21:48:40 +08:00
#include <QDragEnterEvent>
2015-06-11 00:11:50 +08:00
#include <QMimeData>
#include <QApplication>
#include <QDrag>
#include "contentdialog.h"
#include "form/groupchatform.h"
#include "maskablepixmapwidget.h"
#include "core/core.h"
#include "friend.h"
#include "friendlist.h"
#include "group.h"
#include "grouplist.h"
#include "persistence/settings.h"
#include "widget/friendwidget.h"
#include "widget/style.h"
#include "widget/translator.h"
#include "tool/croppinglabel.h"
2014-06-25 04:11:11 +08:00
GroupWidget::GroupWidget(int GroupId, QString Name)
: groupId{GroupId}
{
2016-01-21 11:47:26 +08:00
avatar->setPixmap(Style::scaleSvgImage(":img/group.svg", avatar->width(), avatar->height()));
statusPic.setPixmap(QPixmap(":img/status/dot_online.svg"));
2015-03-18 07:51:22 +08:00
statusPic.setMargin(3);
2014-10-01 17:13:49 +08:00
nameLabel->setText(Name);
2015-06-11 00:11:50 +08:00
onUserListChanged();
2014-10-16 21:48:40 +08:00
setAcceptDrops(true);
connect(nameLabel, &CroppingLabel::editFinished, [this](const QString &newName)
{
if (!newName.isEmpty())
{
Group* g = GroupList::findGroup(groupId);
emit renameRequested(this, newName);
emit g->getChatForm()->groupTitleChanged(groupId, newName.left(128));
}
});
Translator::registerHandler(std::bind(&GroupWidget::retranslateUi, this), this);
2014-06-25 04:11:11 +08:00
}
GroupWidget::~GroupWidget()
{
Translator::unregister(this);
}
2015-06-11 00:11:50 +08:00
void GroupWidget::contextMenuEvent(QContextMenuEvent* event)
2014-06-25 04:11:11 +08:00
{
2015-06-11 00:11:50 +08:00
if (!active)
setBackgroundRole(QPalette::Highlight);
installEventFilter(this); // Disable leave event.
QMenu menu(this);
QAction* openChatWindow = nullptr;
QAction* removeChatWindow = nullptr;
ContentDialog* contentDialog = ContentDialog::getGroupDialog(groupId);
bool notAlone = contentDialog != nullptr && contentDialog->chatroomWidgetCount() > 1;
if (contentDialog == nullptr || notAlone)
openChatWindow = menu.addAction(tr("Open chat in new window"));
2016-01-16 09:23:56 +08:00
if (contentDialog && contentDialog->hasGroupWidget(groupId, this))
removeChatWindow = menu.addAction(tr("Remove chat from this window"));
menu.addSeparator();
QAction* setTitle = menu.addAction(tr("Set title..."));
QAction* quitGroup = menu.addAction(tr("Quit group", "Menu to quit a groupchat"));
2014-06-25 04:11:11 +08:00
2015-06-11 00:11:50 +08:00
QAction* selectedItem = menu.exec(event->globalPos());
removeEventFilter(this);
if (!active)
setBackgroundRole(QPalette::Window);
if (selectedItem)
{
if (selectedItem == quitGroup)
2015-04-24 19:01:50 +08:00
{
emit removeGroup(groupId);
2015-04-24 19:01:50 +08:00
}
else if (selectedItem == openChatWindow)
{
emit chatroomWidgetClicked(this, true);
return;
}
else if (selectedItem == removeChatWindow)
{
ContentDialog* contentDialog = ContentDialog::getGroupDialog(groupId);
contentDialog->removeGroup(groupId);
return;
}
else if (selectedItem == setTitle)
{
2015-06-11 00:11:50 +08:00
editName();
}
}
2014-06-25 04:11:11 +08:00
}
void GroupWidget::mousePressEvent(QMouseEvent *ev)
{
if (ev->button() == Qt::LeftButton)
dragStartPos = ev->pos();
GenericChatroomWidget::mousePressEvent(ev);
}
void GroupWidget::mouseMoveEvent(QMouseEvent *ev)
{
if (!(ev->buttons() & Qt::LeftButton))
return;
if ((dragStartPos - ev->pos()).manhattanLength() > QApplication::startDragDistance())
{
QMimeData* mdata = new QMimeData;
mdata->setText(getGroup()->getName());
QDrag* drag = new QDrag(this);
drag->setMimeData(mdata);
drag->setPixmap(avatar->getPixmap());
drag->exec(Qt::CopyAction | Qt::MoveAction);
}
}
2014-06-25 04:11:11 +08:00
void GroupWidget::onUserListChanged()
{
Group* g = GroupList::findGroup(groupId);
if (g)
{
int peersCount = g->getPeersCount();
if (peersCount == 1)
statusMessageLabel->setText(tr("1 user in chat"));
else
statusMessageLabel->setText(tr("%1 users in chat").arg(peersCount));
}
2014-06-25 04:11:11 +08:00
}
2014-06-27 09:47:16 +08:00
void GroupWidget::setAsActiveChatroom()
{
setActive(true);
2016-01-21 11:47:26 +08:00
avatar->setPixmap(Style::scaleSvgImage(":img/group_dark.svg", avatar->width(), avatar->height()));
2014-06-27 09:47:16 +08:00
}
void GroupWidget::setAsInactiveChatroom()
{
setActive(false);
2016-01-21 11:47:26 +08:00
avatar->setPixmap(Style::scaleSvgImage(":img/group.svg", avatar->width(), avatar->height()));
2014-06-27 09:47:16 +08:00
}
void GroupWidget::updateStatusLight()
{
Group *g = GroupList::findGroup(groupId);
2014-11-23 23:22:29 +08:00
if (!g->getEventFlag())
2015-03-18 07:51:22 +08:00
{
statusPic.setPixmap(QPixmap(":img/status/dot_online.svg"));
2015-03-18 07:51:22 +08:00
statusPic.setMargin(3);
}
else
2015-03-18 07:51:22 +08:00
{
statusPic.setPixmap(QPixmap(":img/status/dot_online_notification.svg"));
2015-03-18 07:51:22 +08:00
statusPic.setMargin(0);
}
}
QString GroupWidget::getStatusString() const
2015-03-26 03:08:46 +08:00
{
Group *g = GroupList::findGroup(groupId);
if (!g->getEventFlag())
return "Online";
else
return "New Message";
}
2015-06-11 00:11:50 +08:00
void GroupWidget::editName()
{
2015-06-11 00:11:50 +08:00
nameLabel->editBegin();
}
Group* GroupWidget::getGroup() const
{
return GroupList::findGroup(groupId);
}
bool GroupWidget::chatFormIsSet(bool focus) const
{
(void)focus;
Group* g = GroupList::findGroup(groupId);
return ContentDialog::existsGroupWidget(groupId, focus) || g->getChatForm()->isVisible();
}
void GroupWidget::setChatForm(ContentLayout* contentLayout)
{
Group* g = GroupList::findGroup(groupId);
g->getChatForm()->show(contentLayout);
}
void GroupWidget::resetEventFlags()
{
Group* g = GroupList::findGroup(groupId);
2014-11-23 23:22:29 +08:00
g->setEventFlag(false);
g->setMentionedFlag(false);
}
2014-10-16 21:48:40 +08:00
void GroupWidget::dragEnterEvent(QDragEnterEvent *ev)
{
ToxId toxId = ToxId(ev->mimeData()->text());
Friend *frnd = FriendList::findFriend(toxId);
if (frnd)
2014-10-16 21:48:40 +08:00
ev->acceptProposedAction();
2015-06-11 00:11:50 +08:00
if (!active)
setBackgroundRole(QPalette::Highlight);
2015-05-28 22:28:11 +08:00
}
void GroupWidget::dragLeaveEvent(QDragLeaveEvent *)
{
2015-06-11 00:11:50 +08:00
if (!active)
setBackgroundRole(QPalette::Window);
2014-10-16 21:48:40 +08:00
}
void GroupWidget::dropEvent(QDropEvent *ev)
{
ToxId toxId = ToxId(ev->mimeData()->text());
Friend *frnd = FriendList::findFriend(toxId);
if (!frnd)
return;
2015-05-28 22:28:11 +08:00
int friendId = frnd->getFriendID();
Core::getInstance()->groupInviteFriend(friendId, groupId);
if (!active)
setBackgroundRole(QPalette::Window);
2014-10-16 21:48:40 +08:00
}
2014-11-06 08:22:50 +08:00
void GroupWidget::setName(const QString& name)
{
nameLabel->setText(name);
}
void GroupWidget::retranslateUi()
{
Group* g = GroupList::findGroup(groupId);
if (g)
{
int peersCount = g->getPeersCount();
if (peersCount == 1)
statusMessageLabel->setText(tr("1 user in chat"));
else
statusMessageLabel->setText(tr("%1 users in chat").arg(peersCount));
}
}