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

refactor(ui): code improvement

This commit is contained in:
bodwok 2021-06-22 21:26:17 +03:00
parent 1d5dd8063e
commit 35244db50e
No known key found for this signature in database
GPG Key ID: A279D059178DA7BA
4 changed files with 54 additions and 45 deletions

View File

@ -20,14 +20,12 @@
#include "friendlistmanager.h" #include "friendlistmanager.h"
#include "src/widget/genericchatroomwidget.h" #include "src/widget/genericchatroomwidget.h"
bool FriendListManager::groupsOnTop = true;
FriendListManager::FriendListManager(QObject *parent) : QObject(parent) FriendListManager::FriendListManager(QObject *parent) : QObject(parent)
{ {
} }
QVector<FriendListManager::IFriendItemPtr> FriendListManager::getItems() const QVector<FriendListManager::IFriendListItemPtr> FriendListManager::getItems() const
{ {
return items; return items;
} }
@ -37,22 +35,20 @@ bool FriendListManager::needHideCircles() const
return hideCircles; return hideCircles;
} }
void FriendListManager::addFriendItem(IFriendListItem *item) void FriendListManager::addFriendListItem(IFriendListItem *item)
{ {
removeAll(item);
if (item->isGroup()) { if (item->isGroup()) {
items.push_back(IFriendItemPtr(item, [](IFriendListItem* groupItem){ items.push_back(IFriendListItemPtr(item, [](IFriendListItem* groupItem){
groupItem->getWidget()->deleteLater();})); groupItem->getWidget()->deleteLater();}));
} else { } else {
items.push_back(IFriendItemPtr(item)); items.push_back(IFriendListItemPtr(item));
} }
updatePositions(); updatePositions();
emit itemsChanged(); emit itemsChanged();
} }
void FriendListManager::removeFriendItem(IFriendListItem *item) void FriendListManager::removeFriendListItem(IFriendListItem *item)
{ {
removeAll(item); removeAll(item);
updatePositions(); updatePositions();
@ -98,7 +94,7 @@ void FriendListManager::applyFilter()
{ {
QString searchString = filterParams.searchString; QString searchString = filterParams.searchString;
for (IFriendItemPtr itemTmp : items) { for (IFriendListItemPtr itemTmp : items) {
if (searchString.isEmpty()) { if (searchString.isEmpty()) {
itemTmp->getWidget()->setVisible(true); itemTmp->getWidget()->setVisible(true);
} else { } else {
@ -131,9 +127,17 @@ void FriendListManager::applyFilter()
void FriendListManager::updatePositions() void FriendListManager::updatePositions()
{ {
if (byName) { if (byName) {
std::sort(items.begin(), items.end(), cmpByName); std::sort(items.begin(), items.end(),
[&](const IFriendListItemPtr &a, const IFriendListItemPtr &b) {
return cmpByName(a, b, groupsOnTop);
}
);
} else { } else {
std::sort(items.begin(), items.end(), cmpByActivity); std::sort(items.begin(), items.end(),
[&](const IFriendListItemPtr &a, const IFriendListItemPtr &b) {
return cmpByActivity(a, b);
}
);
} }
} }
@ -152,7 +156,8 @@ void FriendListManager::removeAll(IFriendListItem* item)
} }
} }
bool FriendListManager::cmpByName(const IFriendItemPtr &a, const IFriendItemPtr &b) bool FriendListManager::cmpByName(const IFriendListItemPtr &a, const IFriendListItemPtr &b,
bool groupsOnTop)
{ {
if (a->isGroup() && !b->isGroup()) { if (a->isGroup() && !b->isGroup()) {
if (groupsOnTop) { if (groupsOnTop) {
@ -179,7 +184,7 @@ bool FriendListManager::cmpByName(const IFriendItemPtr &a, const IFriendItemPtr
return a->getNameItem().toUpper() < b->getNameItem().toUpper(); return a->getNameItem().toUpper() < b->getNameItem().toUpper();
} }
bool FriendListManager::cmpByActivity(const IFriendItemPtr &a, const IFriendItemPtr &b) bool FriendListManager::cmpByActivity(const IFriendListItemPtr &a, const IFriendListItemPtr &b)
{ {
if (a->isGroup() || b->isGroup()) { if (a->isGroup() || b->isGroup()) {
if (a->isGroup() && !b->isGroup()) { if (a->isGroup() && !b->isGroup()) {

View File

@ -30,15 +30,15 @@ class FriendListManager : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
using IFriendItemPtr = std::shared_ptr<IFriendListItem>; using IFriendListItemPtr = std::shared_ptr<IFriendListItem>;
explicit FriendListManager(QObject *parent = nullptr); explicit FriendListManager(QObject *parent = nullptr);
QVector<IFriendItemPtr> getItems() const; QVector<IFriendListItemPtr> getItems() const;
bool needHideCircles() const; bool needHideCircles() const;
void addFriendItem(IFriendListItem* item); void addFriendListItem(IFriendListItem* item);
void removeFriendItem(IFriendListItem* item); void removeFriendListItem(IFriendListItem* item);
void sortByName(); void sortByName();
void sortByActivity(); void sortByActivity();
void resetParents(); void resetParents();
@ -47,7 +47,7 @@ public:
void applyFilter(); void applyFilter();
void updatePositions(); void updatePositions();
static void setGroupsOnTop(bool v); void setGroupsOnTop(bool v);
signals: signals:
void itemsChanged(); void itemsChanged();
@ -61,12 +61,12 @@ private:
} filterParams; } filterParams;
void removeAll(IFriendListItem*); void removeAll(IFriendListItem*);
static bool cmpByName(const IFriendItemPtr&, const IFriendItemPtr&); bool cmpByName(const IFriendListItemPtr&, const IFriendListItemPtr&, bool groupsOnTop);
static bool cmpByActivity(const IFriendItemPtr&, const IFriendItemPtr&); bool cmpByActivity(const IFriendListItemPtr&, const IFriendListItemPtr&);
bool byName = true; bool byName = true;
bool hideCircles = false; bool hideCircles = false;
static bool groupsOnTop; bool groupsOnTop;
QVector<IFriendItemPtr> items; QVector<IFriendListItemPtr> items;
}; };

View File

@ -41,16 +41,16 @@ public:
return -1; return -1;
} }
int getPosForName() const int getNameSortedPos() const
{ {
return posForName; return nameSortedPos;
} }
void setPosForName(int pos) void setNameSortedPos(int pos)
{ {
posForName = pos; nameSortedPos = pos;
} }
private: private:
int posForName = -1; int nameSortedPos = -1;
}; };

View File

@ -125,9 +125,7 @@ FriendListWidget::~FriendListWidget()
{ {
for (int i = 0; i < Settings::getInstance().getCircleCount(); ++i) { for (int i = 0; i < Settings::getInstance().getCircleCount(); ++i) {
CircleWidget* circle = CircleWidget::getFromID(i); CircleWidget* circle = CircleWidget::getFromID(i);
if (circle != nullptr) { delete circle;
delete circle;
}
} }
} }
@ -144,7 +142,6 @@ void FriendListWidget::setMode(SortingMode mode)
void FriendListWidget::sortByMode(SortingMode mode) void FriendListWidget::sortByMode(SortingMode mode)
{ {
manager->resetParents();
cleanMainLayout(); cleanMainLayout();
if (mode == SortingMode::Name) { if (mode == SortingMode::Name) {
@ -154,21 +151,23 @@ void FriendListWidget::sortByMode(SortingMode mode)
addCircleWidget(i); addCircleWidget(i);
} }
QVector<std::shared_ptr<IFriendListItem>> itemsTmp = manager->getItems(); QVector<std::shared_ptr<IFriendListItem>> itemsTmp = manager->getItems(); // Sorted items
QVector<IFriendListItem*> friendItems; QVector<IFriendListItem*> friendItems; // Items that are not included in the circle
int posByName = 0; // Needed for scroll contacts int posByName = 0; // Needed for scroll contacts
// Linking a friend with a circle and setting scroll position // Linking a friend with a circle and setting scroll position
for (int i = 0; i < itemsTmp.size(); ++i) { for (int i = 0; i < itemsTmp.size(); ++i) {
if (itemsTmp[i]->isFriend() && itemsTmp[i]->getCircleId() >= 0) { if (itemsTmp[i]->isFriend() && itemsTmp[i]->getCircleId() >= 0) {
CircleWidget* circleWgt = CircleWidget::getFromID(itemsTmp[i]->getCircleId()); CircleWidget* circleWgt = CircleWidget::getFromID(itemsTmp[i]->getCircleId());
if (circleWgt != nullptr) { if (circleWgt != nullptr) {
// Place a friend in the circle and continue
FriendWidget* frndTmp = FriendWidget* frndTmp =
qobject_cast<FriendWidget*>((itemsTmp[i].get())->getWidget()); qobject_cast<FriendWidget*>((itemsTmp[i].get())->getWidget());
circleWgt->addFriendWidget(frndTmp, frndTmp->getFriend()->getStatus()); circleWgt->addFriendWidget(frndTmp, frndTmp->getFriend()->getStatus());
continue; continue;
} }
} }
itemsTmp[i]->setPosForName(posByName++); // Place the item without the circle in the vector and set the position
itemsTmp[i]->setNameSortedPos(posByName++);
friendItems.push_back(itemsTmp[i].get()); friendItems.push_back(itemsTmp[i].get());
} }
@ -177,6 +176,7 @@ void FriendListWidget::sortByMode(SortingMode mode)
listLayout->addWidget(friendItems[i]->getWidget()); listLayout->addWidget(friendItems[i]->getWidget());
} }
// TODO: Try to remove
manager->applyFilter(); manager->applyFilter();
if (!manager->needHideCircles()) { if (!manager->needHideCircles()) {
@ -195,7 +195,7 @@ void FriendListWidget::sortByMode(SortingMode mode)
QVector<std::shared_ptr<IFriendListItem>> itemsInCircle = getItemsFromCircle(circles.at(i)); QVector<std::shared_ptr<IFriendListItem>> itemsInCircle = getItemsFromCircle(circles.at(i));
for (int i = 0; i < itemsInCircle.size(); ++i) { for (int i = 0; i < itemsInCircle.size(); ++i) {
itemsInCircle.at(i)->setPosForName(posByName++); itemsInCircle.at(i)->setNameSortedPos(posByName++);
} }
listLayout->addWidget(circles.at(i)); listLayout->addWidget(circles.at(i));
@ -237,6 +237,7 @@ void FriendListWidget::sortByMode(SortingMode mode)
activityLayout->addWidget(category); activityLayout->addWidget(category);
} }
// TODO: Try to remove
manager->applyFilter(); manager->applyFilter();
// Insert widgets to CategoryWidget // Insert widgets to CategoryWidget
@ -263,8 +264,13 @@ void FriendListWidget::sortByMode(SortingMode mode)
} }
} }
/**
* @brief Clears the listLayout by performing the creation and ownership inverse of sortByMode.
*/
void FriendListWidget::cleanMainLayout() void FriendListWidget::cleanMainLayout()
{ {
manager->resetParents();
QLayoutItem* itemForDel; QLayoutItem* itemForDel;
while ((itemForDel = listLayout->takeAt(0)) != nullptr) { while ((itemForDel = listLayout->takeAt(0)) != nullptr) {
listLayout->removeWidget(itemForDel->widget()); listLayout->removeWidget(itemForDel->widget());
@ -276,9 +282,7 @@ void FriendListWidget::cleanMainLayout()
QLayoutItem* itemTmp; QLayoutItem* itemTmp;
while ((itemTmp = layout->takeAt(0)) != nullptr) { while ((itemTmp = layout->takeAt(0)) != nullptr) {
wgt = itemTmp->widget(); wgt = itemTmp->widget();
if (wgt != nullptr) { delete wgt;
delete wgt;
}
delete itemTmp; delete itemTmp;
} }
} }
@ -288,7 +292,7 @@ void FriendListWidget::cleanMainLayout()
QWidget* FriendListWidget::getNextWidgetForName(IFriendListItem *currentPos, bool forward) const QWidget* FriendListWidget::getNextWidgetForName(IFriendListItem *currentPos, bool forward) const
{ {
int pos = currentPos->getPosForName(); int pos = currentPos->getNameSortedPos();
int nextPos = forward ? pos + 1 : pos - 1; int nextPos = forward ? pos + 1 : pos - 1;
if (nextPos >= manager->getItems().size()) { if (nextPos >= manager->getItems().size()) {
nextPos = 0; nextPos = 0;
@ -297,7 +301,7 @@ QWidget* FriendListWidget::getNextWidgetForName(IFriendListItem *currentPos, boo
} }
for (int i = 0; i < manager->getItems().size(); ++i) { for (int i = 0; i < manager->getItems().size(); ++i) {
if (manager->getItems().at(i)->getPosForName() == nextPos) { if (manager->getItems().at(i)->getNameSortedPos() == nextPos) {
return manager->getItems().at(i)->getWidget(); return manager->getItems().at(i)->getWidget();
} }
} }
@ -339,17 +343,17 @@ void FriendListWidget::addGroupWidget(GroupWidget* widget)
renameGroupWidget(widget, name); renameGroupWidget(widget, name);
}); });
manager->addFriendItem(widget); manager->addFriendListItem(widget);
} }
void FriendListWidget::addFriendWidget(FriendWidget* w) void FriendListWidget::addFriendWidget(FriendWidget* w)
{ {
manager->addFriendItem(w); manager->addFriendListItem(w);
} }
void FriendListWidget::removeGroupWidget(GroupWidget* w) void FriendListWidget::removeGroupWidget(GroupWidget* w)
{ {
manager->removeFriendItem(w); manager->removeFriendListItem(w);
} }
void FriendListWidget::removeFriendWidget(FriendWidget* w) void FriendListWidget::removeFriendWidget(FriendWidget* w)
@ -362,7 +366,7 @@ void FriendListWidget::removeFriendWidget(FriendWidget* w)
emit searchCircle(*circleWidget); emit searchCircle(*circleWidget);
} }
manager->removeFriendItem(w); manager->removeFriendListItem(w);
} }
void FriendListWidget::addCircleWidget(int id) void FriendListWidget::addCircleWidget(int id)