2014-07-07 00:19:45 +08:00
|
|
|
/*
|
2016-11-28 21:33:38 +08:00
|
|
|
Copyright © 2014-2015 by The qTox Project Contributors
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2014-07-07 00:19:45 +08:00
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
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.
|
2015-06-06 09:40:08 +08:00
|
|
|
|
|
|
|
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
|
2015-06-06 09:40:08 +08:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2014-07-07 00:19:45 +08:00
|
|
|
|
2015-06-06 09:40:08 +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"
|
2016-08-07 03:28:40 +08:00
|
|
|
|
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>
|
2015-06-27 04:13:20 +08:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QDrag>
|
2015-02-12 20:18:04 +08:00
|
|
|
|
2016-08-07 03:28:40 +08:00
|
|
|
#include "contentdialog.h"
|
|
|
|
#include "form/groupchatform.h"
|
|
|
|
#include "maskablepixmapwidget.h"
|
2016-12-19 10:26:26 +08:00
|
|
|
#include "src/core/core.h"
|
|
|
|
#include "src/friend.h"
|
|
|
|
#include "src/friendlist.h"
|
|
|
|
#include "src/group.h"
|
|
|
|
#include "src/grouplist.h"
|
|
|
|
#include "src/persistence/settings.h"
|
|
|
|
#include "src/widget/friendwidget.h"
|
|
|
|
#include "src/widget/style.h"
|
|
|
|
#include "src/widget/translator.h"
|
2016-08-07 03:28:40 +08:00
|
|
|
#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()));
|
2015-02-12 20:18:04 +08:00
|
|
|
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);
|
2014-10-01 02:10:42 +08:00
|
|
|
|
2015-06-11 00:11:50 +08:00
|
|
|
onUserListChanged();
|
2014-10-16 21:48:40 +08:00
|
|
|
|
|
|
|
setAcceptDrops(true);
|
2015-06-02 02:39:40 +08:00
|
|
|
|
2015-06-08 02:25:05 +08:00
|
|
|
connect(nameLabel, &CroppingLabel::editFinished, [this](const QString &newName)
|
2015-06-02 02:39:40 +08:00
|
|
|
{
|
2015-06-08 02:25:05 +08:00
|
|
|
if (!newName.isEmpty())
|
2015-06-02 02:39:40 +08:00
|
|
|
{
|
2015-06-08 02:25:05 +08:00
|
|
|
Group* g = GroupList::findGroup(groupId);
|
|
|
|
emit renameRequested(this, newName);
|
2015-06-07 11:20:06 +08:00
|
|
|
emit g->getChatForm()->groupTitleChanged(groupId, newName.left(128));
|
2015-06-02 02:39:40 +08:00
|
|
|
}
|
|
|
|
});
|
2015-09-27 01:37:17 +08:00
|
|
|
Translator::registerHandler(std::bind(&GroupWidget::retranslateUi, this), this);
|
2014-06-25 04:11:11 +08:00
|
|
|
}
|
|
|
|
|
2015-11-08 21:17:34 +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);
|
2015-06-23 02:01:50 +08:00
|
|
|
|
|
|
|
QAction* openChatWindow = nullptr;
|
|
|
|
QAction* removeChatWindow = nullptr;
|
|
|
|
|
2015-06-27 04:13:20 +08:00
|
|
|
ContentDialog* contentDialog = ContentDialog::getGroupDialog(groupId);
|
|
|
|
bool notAlone = contentDialog != nullptr && contentDialog->chatroomWidgetCount() > 1;
|
2015-06-23 02:01:50 +08:00
|
|
|
|
2015-06-27 04:13:20 +08:00
|
|
|
if (contentDialog == nullptr || notAlone)
|
|
|
|
openChatWindow = menu.addAction(tr("Open chat in new window"));
|
2015-06-23 02:01:50 +08:00
|
|
|
|
2016-01-16 09:23:56 +08:00
|
|
|
if (contentDialog && contentDialog->hasGroupWidget(groupId, this))
|
2015-06-27 04:13:20 +08:00
|
|
|
removeChatWindow = menu.addAction(tr("Remove chat from this window"));
|
2015-06-23 02:01:50 +08:00
|
|
|
|
|
|
|
menu.addSeparator();
|
|
|
|
|
2015-03-11 19:20:27 +08:00
|
|
|
QAction* setTitle = menu.addAction(tr("Set title..."));
|
2015-09-27 01:37:17 +08:00
|
|
|
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);
|
2015-06-08 02:25:05 +08:00
|
|
|
|
2014-11-18 19:00:09 +08:00
|
|
|
if (selectedItem)
|
|
|
|
{
|
|
|
|
if (selectedItem == quitGroup)
|
2015-04-24 19:01:50 +08:00
|
|
|
{
|
2014-11-18 19:00:09 +08:00
|
|
|
emit removeGroup(groupId);
|
2015-04-24 19:01:50 +08:00
|
|
|
}
|
2015-06-23 02: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;
|
|
|
|
}
|
2015-03-11 19:20:27 +08:00
|
|
|
else if (selectedItem == setTitle)
|
2014-11-18 19:00:09 +08:00
|
|
|
{
|
2015-06-11 00:11:50 +08:00
|
|
|
editName();
|
2014-11-18 19:00:09 +08:00
|
|
|
}
|
|
|
|
}
|
2014-06-25 04:11:11 +08:00
|
|
|
}
|
|
|
|
|
2015-06-27 04:13:20 +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;
|
2016-08-07 03:28:40 +08:00
|
|
|
mdata->setText(getGroup()->getName());
|
2015-06-27 04:13:20 +08:00
|
|
|
|
2016-08-07 03:28:40 +08:00
|
|
|
QDrag* drag = new QDrag(this);
|
2015-06-27 04:13:20 +08:00
|
|
|
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)
|
2015-09-27 01:37:17 +08:00
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
2014-10-01 02:10:42 +08:00
|
|
|
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()
|
|
|
|
{
|
2014-10-01 02:10:42 +08:00
|
|
|
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
|
|
|
}
|
2014-09-01 23:02:26 +08:00
|
|
|
|
|
|
|
void GroupWidget::updateStatusLight()
|
|
|
|
{
|
2014-09-02 00:20:28 +08:00
|
|
|
Group *g = GroupList::findGroup(groupId);
|
|
|
|
|
2014-11-23 23:22:29 +08:00
|
|
|
if (!g->getEventFlag())
|
2015-03-18 07:51:22 +08:00
|
|
|
{
|
2015-02-12 20:18:04 +08:00
|
|
|
statusPic.setPixmap(QPixmap(":img/status/dot_online.svg"));
|
2015-03-18 07:51:22 +08:00
|
|
|
statusPic.setMargin(3);
|
|
|
|
}
|
2014-10-04 16:54:50 +08:00
|
|
|
else
|
2015-03-18 07:51:22 +08:00
|
|
|
{
|
2015-02-12 20:18:04 +08:00
|
|
|
statusPic.setPixmap(QPixmap(":img/status/dot_online_notification.svg"));
|
2015-03-18 07:51:22 +08:00
|
|
|
statusPic.setMargin(0);
|
|
|
|
}
|
2014-09-02 00:20:28 +08:00
|
|
|
}
|
|
|
|
|
2015-06-23 02:01:50 +08:00
|
|
|
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-07 11:20:06 +08:00
|
|
|
{
|
2015-06-11 00:11:50 +08:00
|
|
|
nameLabel->editBegin();
|
2015-06-07 11:20:06 +08:00
|
|
|
}
|
|
|
|
|
2015-06-23 02:01:50 +08:00
|
|
|
Group* GroupWidget::getGroup() const
|
|
|
|
{
|
|
|
|
return GroupList::findGroup(groupId);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GroupWidget::chatFormIsSet(bool focus) const
|
2014-09-02 00:20:28 +08:00
|
|
|
{
|
2015-06-23 02:01:50 +08:00
|
|
|
(void)focus;
|
2014-09-02 00:20:28 +08:00
|
|
|
Group* g = GroupList::findGroup(groupId);
|
2015-06-23 02:01:50 +08:00
|
|
|
return ContentDialog::existsGroupWidget(groupId, focus) || g->getChatForm()->isVisible();
|
2015-06-17 02:25:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GroupWidget::setChatForm(ContentLayout* contentLayout)
|
|
|
|
{
|
|
|
|
Group* g = GroupList::findGroup(groupId);
|
|
|
|
g->getChatForm()->show(contentLayout);
|
2014-09-02 00:20:28 +08:00
|
|
|
}
|
2014-09-01 23:02:26 +08:00
|
|
|
|
2014-09-02 00:20:28 +08:00
|
|
|
void GroupWidget::resetEventFlags()
|
|
|
|
{
|
|
|
|
Group* g = GroupList::findGroup(groupId);
|
2014-11-23 23:22:29 +08:00
|
|
|
g->setEventFlag(false);
|
|
|
|
g->setMentionedFlag(false);
|
2014-09-01 23:02:26 +08:00
|
|
|
}
|
2014-10-16 21:48:40 +08:00
|
|
|
|
|
|
|
void GroupWidget::dragEnterEvent(QDragEnterEvent *ev)
|
|
|
|
{
|
2016-08-07 03:28:40 +08:00
|
|
|
ToxId toxId = ToxId(ev->mimeData()->text());
|
2017-01-06 19:02:54 +08:00
|
|
|
Friend* frnd = FriendList::findFriend(toxId.getPublicKey());
|
2016-08-07 03:28:40 +08:00
|
|
|
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)
|
|
|
|
{
|
2016-08-07 03:28:40 +08:00
|
|
|
ToxId toxId = ToxId(ev->mimeData()->text());
|
2017-01-06 19:02:54 +08:00
|
|
|
Friend* frnd = FriendList::findFriend(toxId.getPublicKey());
|
2016-08-07 03:28:40 +08:00
|
|
|
if (!frnd)
|
|
|
|
return;
|
2015-05-28 22:28:11 +08:00
|
|
|
|
2016-08-22 05:30:07 +08:00
|
|
|
int friendId = frnd->getFriendId();
|
2016-08-07 03:28:40 +08:00
|
|
|
Core::getInstance()->groupInviteFriend(friendId, groupId);
|
|
|
|
|
|
|
|
if (!active)
|
|
|
|
setBackgroundRole(QPalette::Window);
|
2014-10-16 21:48:40 +08:00
|
|
|
}
|
2014-11-14 02:25:45 +08:00
|
|
|
|
2014-11-06 08:22:50 +08:00
|
|
|
void GroupWidget::setName(const QString& name)
|
|
|
|
{
|
|
|
|
nameLabel->setText(name);
|
|
|
|
}
|
2015-09-27 01:37:17 +08:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|