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

Merge pull request #4194

noavarice (2):
      refactor: single group invite extracted into a widget
      docs: added documentation for GroupInviteForm class
This commit is contained in:
Diadlo 2017-02-25 20:41:36 +03:00
commit 1d0a49c2b7
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
7 changed files with 343 additions and 160 deletions

View File

@ -236,6 +236,8 @@ set(${PROJECT_NAME}_SOURCES
src/friendlist.h
src/group.cpp
src/group.h
src/groupinvite.cpp
src/groupinvite.h
src/grouplist.cpp
src/grouplist.h
src/ipc.cpp
@ -328,6 +330,8 @@ set(${PROJECT_NAME}_SOURCES
src/widget/form/groupchatform.h
src/widget/form/groupinviteform.cpp
src/widget/form/groupinviteform.h
src/widget/form/groupinvitewidget.cpp
src/widget/form/groupinvitewidget.h
src/widget/form/loadhistorydialog.cpp
src/widget/form/loadhistorydialog.h
src/widget/form/profileform.cpp

62
src/groupinvite.cpp Normal file
View File

@ -0,0 +1,62 @@
/*
Copyright © 2017 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox 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.
qTox 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
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#include "groupinvite.h"
/**
* @class GroupInvite
*
* @brief This class contains information needed to create a group invite
*/
GroupInvite::GroupInvite(int32_t friendID, uint8_t inviteType, const QByteArray& data)
: friendId { friendID }
, type { inviteType }
, invite { data }
, date { QDateTime::currentDateTime() }
{
}
bool GroupInvite::operator ==(const GroupInvite& other) const
{
return friendId == other.friendId &&
type == other.type &&
invite == other.invite &&
date == other.date;
}
int32_t GroupInvite::getFriendId() const
{
return friendId;
}
uint8_t GroupInvite::getType() const
{
return type;
}
QByteArray GroupInvite::getInvite() const
{
return invite;
}
QDateTime GroupInvite::getInviteDate() const
{
return date;
}

45
src/groupinvite.h Normal file
View File

@ -0,0 +1,45 @@
/*
Copyright © 2017 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox 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.
qTox 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
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GROUPINVITE_H
#define GROUPINVITE_H
#include <cstdint>
#include <QByteArray>
#include <QDateTime>
class GroupInvite
{
public:
GroupInvite(int32_t friendID, uint8_t inviteType, const QByteArray& data);
bool operator ==(const GroupInvite& other) const;
int32_t getFriendId() const;
uint8_t getType() const;
QByteArray getInvite() const;
QDateTime getInviteDate() const;
private:
int32_t friendId;
uint8_t type;
QByteArray invite;
QDateTime date;
};
#endif // GROUPINVITE_H

View File

@ -1,5 +1,5 @@
/*
Copyright © 2015 by The qTox Project Contributors
Copyright © 2015-2017 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
@ -19,45 +19,52 @@
#include "groupinviteform.h"
#include <tox/tox.h>
#include <QDebug>
#include <QSignalMapper>
#include <QPushButton>
#include <QBoxLayout>
#include <QGroupBox>
#include <QDateTime>
#include <QLabel>
#include <QWindow>
#include "ui_mainwindow.h"
#include "src/persistence/settings.h"
#include "src/widget/tool/croppinglabel.h"
#include "src/widget/translator.h"
#include "src/nexus.h"
#include "src/widget/form/groupinvitewidget.h"
#include "src/core/core.h"
#include "src/widget/gui.h"
#include "src/widget/translator.h"
#include "src/nexus.h"
#include "src/persistence/settings.h"
#include "src/widget/contentlayout.h"
#include "src/widget/translator.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include <QDateTime>
#include <QDebug>
#include <QGroupBox>
#include <QLabel>
#include <QPushButton>
#include <QSignalMapper>
#include <QWindow>
#include <algorithm>
#include <tox/tox.h>
/**
* @class GroupInviteForm
*
* @brief This form contains all group invites you received
*/
GroupInviteForm::GroupInviteForm()
: createButton(new QPushButton(this))
, inviteBox(new QGroupBox(this))
, scroll(new QScrollArea(this))
, headLabel(new QLabel(this))
, headWidget(new QWidget(this))
{
QVBoxLayout* layout = new QVBoxLayout(this);
createButton = new QPushButton(this);
connect(createButton, &QPushButton::released, [this]()
connect(createButton, &QPushButton::clicked, [this]()
{
emit groupCreate(TOX_CONFERENCE_TYPE_AV);
});
inviteBox = new QGroupBox(this);
inviteLayout = new QVBoxLayout(inviteBox);
scroll = new QScrollArea(this);
QWidget* innerWidget = new QWidget(scroll);
innerWidget->setLayout(new QVBoxLayout());
innerWidget->layout()->setAlignment(Qt::AlignTop);
scroll->setWidget(innerWidget);
scroll->setWidgetResizable(true);
QVBoxLayout* inviteLayout = new QVBoxLayout(inviteBox);
inviteLayout->addWidget(scroll);
layout->addWidget(createButton);
@ -66,9 +73,7 @@ GroupInviteForm::GroupInviteForm()
QFont bold;
bold.setBold(true);
headLabel = new QLabel(this);
headLabel->setFont(bold);
headWidget = new QWidget(this);
QHBoxLayout* headLayout = new QHBoxLayout(headWidget);
headLayout->addWidget(headLabel);
@ -81,17 +86,24 @@ GroupInviteForm::~GroupInviteForm()
Translator::unregister(this);
}
/**
* @brief Detects that form is shown
* @return True if form is visible
*/
bool GroupInviteForm::isShown() const
{
if (this->isVisible())
bool result = isVisible();
if (result)
{
headWidget->window()->windowHandle()->alert(0);
return true;
}
return false;
return result;
}
/**
* @brief Shows the form
* @param contentLayout Main layout that contains all components of the form
*/
void GroupInviteForm::show(ContentLayout* contentLayout)
{
contentLayout->mainContent->layout()->addWidget(this);
@ -100,38 +112,31 @@ void GroupInviteForm::show(ContentLayout* contentLayout)
headWidget->show();
}
/**
* @brief Adds group invite
* @param friendId Id of a friend that invited you
* @param type Type of the invitation - text or AV
* @param invite Information that invited person needs to see an invitation
*/
void GroupInviteForm::addGroupInvite(int32_t friendId, uint8_t type, QByteArray invite)
{
QWidget* groupWidget = new QWidget(this);
QHBoxLayout* groupLayout = new QHBoxLayout(groupWidget);
GroupInvite group;
group.friendId = friendId;
group.type = type;
group.invite = invite;
group.time = QDateTime::currentDateTime();
groupInvites.push_front(group);
CroppingLabel* groupLabel = new CroppingLabel(this);
groupLabels.insert(groupLabel);
groupLayout->addWidget(groupLabel);
scroll->widget()->layout()->addWidget(groupWidget);
retranslateGroupLabel(groupLabel);
QPushButton* acceptButton = new QPushButton(this);
acceptButtons.insert(acceptButton);
connect(acceptButton, &QPushButton::released, this, &GroupInviteForm::onGroupInviteAccepted);
groupLayout->addWidget(acceptButton);
retranslateAcceptButton(acceptButton);
QPushButton* rejectButton = new QPushButton(this);
rejectButtons.insert(rejectButton);
connect(rejectButton, &QPushButton::released, this, &GroupInviteForm::onGroupInviteRejected);
groupLayout->addWidget(rejectButton);
retranslateRejectButton(rejectButton);
if (isVisible())
GroupInviteWidget* widget = new GroupInviteWidget(this, GroupInvite(friendId, type, invite));
scroll->widget()->layout()->addWidget(widget);
invites.append(widget);
connect(widget, &GroupInviteWidget::accepted,
[this] (const GroupInvite& inviteInfo) {
deleteInviteWidget(inviteInfo);
emit groupInviteAccepted(inviteInfo.getFriendId(),
inviteInfo.getType(),
inviteInfo.getInvite());
});
connect(widget, &GroupInviteWidget::rejected,
[this] (const GroupInvite& inviteInfo) {
deleteInviteWidget(inviteInfo);
});
if (isVisible()) {
emit groupInvitesSeen();
}
}
void GroupInviteForm::showEvent(QShowEvent* event)
@ -140,82 +145,31 @@ void GroupInviteForm::showEvent(QShowEvent* event)
emit groupInvitesSeen();
}
void GroupInviteForm::onGroupInviteAccepted()
/**
* @brief Deletes accepted/declined group invite widget
* @param inviteInfo Invite information of accepted/declined widget
*/
void GroupInviteForm::deleteInviteWidget(const GroupInvite &inviteInfo)
{
QPushButton* acceptButton = static_cast<QPushButton*>(sender());
QWidget* groupWidget = acceptButton->parentWidget();
int index = scroll->widget()->layout()->indexOf(groupWidget);
GroupInvite invite = groupInvites.at(index);
groupInvites.removeAt(index);
deleteInviteButtons(groupWidget);
emit groupInviteAccepted(invite.friendId, invite.type, invite.invite);
}
void GroupInviteForm::onGroupInviteRejected()
{
QPushButton* rejectButton = static_cast<QPushButton*>(sender());
QWidget* groupWidget = rejectButton->parentWidget();
int index = scroll->widget()->layout()->indexOf(groupWidget);
groupInvites.removeAt(index);
deleteInviteButtons(groupWidget);
}
void GroupInviteForm::deleteInviteButtons(QWidget* widget)
{
QList<QPushButton*> buttons = widget->findChildren<QPushButton*>();
QSet<QPushButton*> set = QSet<QPushButton*>::fromList(buttons);
acceptButtons.subtract(set);
rejectButtons.subtract(set);
QList<CroppingLabel*> labels = widget->findChildren<CroppingLabel*>();
groupLabels.remove(labels.at(0));
widget->deleteLater();
scroll->widget()->layout()->removeWidget(widget);
auto deletingWidget = std::find_if(invites.begin(), invites.end(),
[=] (const GroupInviteWidget* widget) {
return inviteInfo == widget->getInviteInfo();
});
(*deletingWidget)->deleteLater();
scroll->widget()->layout()->removeWidget(*deletingWidget);
invites.erase(deletingWidget);
}
void GroupInviteForm::retranslateUi()
{
headLabel->setText(tr("Groups"));
if (createButton)
{
createButton->setText(tr("Create new group"));
}
inviteBox->setTitle(tr("Group invites"));
for (QPushButton* acceptButton : acceptButtons)
retranslateAcceptButton(acceptButton);
for (QPushButton* rejectButton : rejectButtons)
retranslateRejectButton(rejectButton);
for (CroppingLabel* label : groupLabels)
retranslateGroupLabel(label);
}
void GroupInviteForm::retranslateGroupLabel(CroppingLabel* label)
{
QWidget* groupWidget = label->parentWidget();
int index = scroll->widget()->layout()->indexOf(groupWidget);
GroupInvite invite = groupInvites.at(index);
QString name = Nexus::getCore()->getFriendUsername(invite.friendId);
QString date = invite.time.toString(Settings::getInstance().getDateFormat());
QString time = invite.time.toString(Settings::getInstance().getTimestampFormat());
label->setText(tr("Invited by %1 on %2 at %3.").arg("<b>" + name.toHtmlEscaped() + "</b>", date, time));
}
void GroupInviteForm::retranslateAcceptButton(QPushButton* acceptButton)
{
acceptButton->setText(tr("Join"));
}
void GroupInviteForm::retranslateRejectButton(QPushButton* rejectButton)
{
rejectButton->setText(tr("Decline"));
for (GroupInviteWidget* invite : invites)
{
invite->retranslateUi();
}
}

View File

@ -20,22 +20,24 @@
#ifndef GROUPINVITEFORM_H
#define GROUPINVITEFORM_H
#include <QWidget>
#include <QDateTime>
#include <QSet>
#include "src/widget/tool/croppinglabel.h"
#include "src/groupinvite.h"
#include "src/widget/gui.h"
#include <QScrollArea>
class QLabel;
class QVBoxLayout;
class QPushButton;
class QGroupBox;
class QSignalMapper;
#include <QWidget>
class ContentLayout;
class GroupInviteWidget;
namespace Ui {class MainWindow;}
class QGroupBox;
class QLabel;
class QPushButton;
class QScrollArea;
class QSignalMapper;
namespace Ui
{
class MainWindow;
}
class GroupInviteForm : public QWidget
{
@ -56,35 +58,16 @@ signals:
protected:
void showEvent(QShowEvent* event) final override;
private slots:
void onGroupInviteAccepted();
void onGroupInviteRejected();
private:
void retranslateUi();
void retranslateAcceptButton(QPushButton* acceptButton);
void retranslateRejectButton(QPushButton* rejectButton);
void retranslateGroupLabel(CroppingLabel* label);
void deleteInviteButtons(QWidget* widget);
void deleteInviteWidget(const GroupInvite& inviteInfo);
private:
struct GroupInvite
{
int32_t friendId;
uint8_t type;
QByteArray invite;
QDateTime time;
};
QWidget* headWidget;
QLabel* headLabel;
QPushButton* createButton;
QGroupBox* inviteBox;
QVBoxLayout* inviteLayout;
QSet<QPushButton*> acceptButtons;
QSet<QPushButton*> rejectButtons;
QSet<CroppingLabel*> groupLabels;
QList<GroupInvite> groupInvites;
QList<GroupInviteWidget*> invites;
QScrollArea* scroll;
};

View File

@ -0,0 +1,83 @@
/*
Copyright © 2017 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox 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.
qTox 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
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#include "groupinvitewidget.h"
#include "src/core/core.h"
#include "src/nexus.h"
#include "src/persistence/settings.h"
#include "src/widget/tool/croppinglabel.h"
#include <QHBoxLayout>
#include <QPushButton>
#include <QSignalMapper>
/**
* @class GroupInviteWidget
*
* @brief This class shows information about single group invite
* and provides buttons to accept/reject it
*/
GroupInviteWidget::GroupInviteWidget(QWidget* parent, const GroupInvite& invite)
: QWidget(parent)
, widgetLayout(new QHBoxLayout(this))
, acceptButton(new QPushButton(this))
, rejectButton(new QPushButton(this))
, inviteMessageLabel(new CroppingLabel(this))
, inviteInfo(invite)
{
connect(acceptButton, &QPushButton::clicked, [=] () {
emit accepted(inviteInfo);
});
connect(rejectButton, &QPushButton::clicked, [=] () {
emit rejected(inviteInfo);
});
widgetLayout->addWidget(inviteMessageLabel);
widgetLayout->addWidget(acceptButton);
widgetLayout->addWidget(rejectButton);
setLayout(widgetLayout);
retranslateUi();
}
/**
* @brief Retranslate all elements in the form.
*/
void GroupInviteWidget::retranslateUi()
{
QString name = Nexus::getCore()->getFriendUsername(inviteInfo.getFriendId());
QDateTime inviteDate = inviteInfo.getInviteDate();
QString date = inviteDate.toString(Settings::getInstance().getDateFormat());
QString time = inviteDate.toString(Settings::getInstance().getTimestampFormat());
inviteMessageLabel->setText(tr("Invited by %1 on %2 at %3.")
.arg("<b>%1</b>")
.arg(name.toHtmlEscaped(), date, time));
acceptButton->setText(tr("Join"));
rejectButton->setText(tr("Decline"));
}
/**
* @brief Returns infomation about invitation - e.g., who and when sent
* @return Invite information object
*/
const GroupInvite GroupInviteWidget::getInviteInfo() const
{
return inviteInfo;
}

View File

@ -0,0 +1,52 @@
/*
Copyright © 2017 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox 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.
qTox 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
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GROUPINVITEWIDGET_H
#define GROUPINVITEWIDGET_H
#include "src/groupinvite.h"
#include <QWidget>
class CroppingLabel;
class QHBoxLayout;
class QPushButton;
class GroupInviteWidget : public QWidget
{
Q_OBJECT
public:
GroupInviteWidget(QWidget* parent, const GroupInvite& invite);
void retranslateUi();
const GroupInvite getInviteInfo() const;
signals:
void accepted(const GroupInvite& invite);
void rejected(const GroupInvite& invite);
private:
QPushButton* acceptButton;
QPushButton* rejectButton;
CroppingLabel* inviteMessageLabel;
QHBoxLayout* widgetLayout;
GroupInvite inviteInfo;
};
#endif // GROUPINVITEWIDGET_H