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

refactor: single group invite extracted into a widget

Previously GroupInviteForm included several collections of components
which were supposed to use as a single object. Now they are replaced
with a class and all functionality related to this collections moved
there too. Also fix #3621
This commit is contained in:
noavarice 2017-02-22 15:54:02 +03:00
parent 55081d2179
commit adba773c6d
7 changed files with 233 additions and 159 deletions

View File

@ -219,6 +219,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
@ -311,6 +313,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

34
src/groupinvite.cpp Normal file
View File

@ -0,0 +1,34 @@
#include "groupinvite.h"
GroupInvite::GroupInvite(const int32_t FriendId, const uint8_t Type, const QByteArray& Invite)
: friendId(FriendId), type(Type), invite(Invite), 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;
}

26
src/groupinvite.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef GROUPINVITE_H
#define GROUPINVITE_H
#include <cstdint>
#include <QByteArray>
#include <QDateTime>
class GroupInvite
{
public:
GroupInvite(const int32_t FriendId, const uint8_t Type, const QByteArray& Invite);
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

@ -19,45 +19,46 @@
#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 <algorithm>
#include <tox/tox.h>
#include <QVBoxLayout>
#include <QDateTime>
#include <QDebug>
#include <QGroupBox>
#include <QLabel>
#include <QPushButton>
#include <QSignalMapper>
#include <QWindow>
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, [=]()
{
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 +67,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);
@ -83,13 +82,12 @@ GroupInviteForm::~GroupInviteForm()
bool GroupInviteForm::isShown() const
{
if (this->isVisible())
bool result = isVisible();
if (result)
{
headWidget->window()->windowHandle()->alert(0);
return true;
}
return false;
return result;
}
void GroupInviteForm::show(ContentLayout* contentLayout)
@ -102,36 +100,23 @@ void GroupInviteForm::show(ContentLayout* contentLayout)
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* addingInviteWidget = new GroupInviteWidget(this, GroupInvite(friendId, type, invite));
scroll->widget()->layout()->addWidget(addingInviteWidget);
invites.append(addingInviteWidget);
connect(addingInviteWidget, &GroupInviteWidget::accepted,
[=] (const GroupInvite& inviteInfo) {
deleteInviteWidget(inviteInfo);
emit groupInviteAccepted(inviteInfo.getFriendId(),
inviteInfo.getType(),
inviteInfo.getInvite());
});
connect(addingInviteWidget, &GroupInviteWidget::rejected,
[=] (const GroupInvite& inviteInfo) {
deleteInviteWidget(inviteInfo);
});
if (isVisible()) {
emit groupInvitesSeen();
}
}
void GroupInviteForm::showEvent(QShowEvent* event)
@ -140,82 +125,27 @@ void GroupInviteForm::showEvent(QShowEvent* event)
emit groupInvitesSeen();
}
void GroupInviteForm::onGroupInviteAccepted()
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,64 @@
#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 invite information object
* @return Returns invite information object
*/
const GroupInvite GroupInviteWidget::getInviteInfo() const
{
return inviteInfo;
}

View File

@ -0,0 +1,33 @@
#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