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:
parent
1d5dd8063e
commit
35244db50e
|
@ -20,14 +20,12 @@
|
|||
#include "friendlistmanager.h"
|
||||
#include "src/widget/genericchatroomwidget.h"
|
||||
|
||||
bool FriendListManager::groupsOnTop = true;
|
||||
|
||||
FriendListManager::FriendListManager(QObject *parent) : QObject(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QVector<FriendListManager::IFriendItemPtr> FriendListManager::getItems() const
|
||||
QVector<FriendListManager::IFriendListItemPtr> FriendListManager::getItems() const
|
||||
{
|
||||
return items;
|
||||
}
|
||||
|
@ -37,22 +35,20 @@ bool FriendListManager::needHideCircles() const
|
|||
return hideCircles;
|
||||
}
|
||||
|
||||
void FriendListManager::addFriendItem(IFriendListItem *item)
|
||||
void FriendListManager::addFriendListItem(IFriendListItem *item)
|
||||
{
|
||||
removeAll(item);
|
||||
|
||||
if (item->isGroup()) {
|
||||
items.push_back(IFriendItemPtr(item, [](IFriendListItem* groupItem){
|
||||
items.push_back(IFriendListItemPtr(item, [](IFriendListItem* groupItem){
|
||||
groupItem->getWidget()->deleteLater();}));
|
||||
} else {
|
||||
items.push_back(IFriendItemPtr(item));
|
||||
items.push_back(IFriendListItemPtr(item));
|
||||
}
|
||||
|
||||
updatePositions();
|
||||
emit itemsChanged();
|
||||
}
|
||||
|
||||
void FriendListManager::removeFriendItem(IFriendListItem *item)
|
||||
void FriendListManager::removeFriendListItem(IFriendListItem *item)
|
||||
{
|
||||
removeAll(item);
|
||||
updatePositions();
|
||||
|
@ -98,7 +94,7 @@ void FriendListManager::applyFilter()
|
|||
{
|
||||
QString searchString = filterParams.searchString;
|
||||
|
||||
for (IFriendItemPtr itemTmp : items) {
|
||||
for (IFriendListItemPtr itemTmp : items) {
|
||||
if (searchString.isEmpty()) {
|
||||
itemTmp->getWidget()->setVisible(true);
|
||||
} else {
|
||||
|
@ -131,9 +127,17 @@ void FriendListManager::applyFilter()
|
|||
void FriendListManager::updatePositions()
|
||||
{
|
||||
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 {
|
||||
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 (groupsOnTop) {
|
||||
|
@ -179,7 +184,7 @@ bool FriendListManager::cmpByName(const IFriendItemPtr &a, const IFriendItemPtr
|
|||
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()) {
|
||||
|
|
|
@ -30,15 +30,15 @@ class FriendListManager : public QObject
|
|||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
using IFriendItemPtr = std::shared_ptr<IFriendListItem>;
|
||||
using IFriendListItemPtr = std::shared_ptr<IFriendListItem>;
|
||||
|
||||
explicit FriendListManager(QObject *parent = nullptr);
|
||||
|
||||
QVector<IFriendItemPtr> getItems() const;
|
||||
QVector<IFriendListItemPtr> getItems() const;
|
||||
bool needHideCircles() const;
|
||||
|
||||
void addFriendItem(IFriendListItem* item);
|
||||
void removeFriendItem(IFriendListItem* item);
|
||||
void addFriendListItem(IFriendListItem* item);
|
||||
void removeFriendListItem(IFriendListItem* item);
|
||||
void sortByName();
|
||||
void sortByActivity();
|
||||
void resetParents();
|
||||
|
@ -47,7 +47,7 @@ public:
|
|||
void applyFilter();
|
||||
void updatePositions();
|
||||
|
||||
static void setGroupsOnTop(bool v);
|
||||
void setGroupsOnTop(bool v);
|
||||
|
||||
signals:
|
||||
void itemsChanged();
|
||||
|
@ -61,12 +61,12 @@ private:
|
|||
} filterParams;
|
||||
|
||||
void removeAll(IFriendListItem*);
|
||||
static bool cmpByName(const IFriendItemPtr&, const IFriendItemPtr&);
|
||||
static bool cmpByActivity(const IFriendItemPtr&, const IFriendItemPtr&);
|
||||
bool cmpByName(const IFriendListItemPtr&, const IFriendListItemPtr&, bool groupsOnTop);
|
||||
bool cmpByActivity(const IFriendListItemPtr&, const IFriendListItemPtr&);
|
||||
|
||||
bool byName = true;
|
||||
bool hideCircles = false;
|
||||
static bool groupsOnTop;
|
||||
QVector<IFriendItemPtr> items;
|
||||
bool groupsOnTop;
|
||||
QVector<IFriendListItemPtr> items;
|
||||
|
||||
};
|
||||
|
|
|
@ -41,16 +41,16 @@ public:
|
|||
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:
|
||||
int posForName = -1;
|
||||
int nameSortedPos = -1;
|
||||
};
|
||||
|
|
|
@ -125,11 +125,9 @@ FriendListWidget::~FriendListWidget()
|
|||
{
|
||||
for (int i = 0; i < Settings::getInstance().getCircleCount(); ++i) {
|
||||
CircleWidget* circle = CircleWidget::getFromID(i);
|
||||
if (circle != nullptr) {
|
||||
delete circle;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FriendListWidget::setMode(SortingMode mode)
|
||||
{
|
||||
|
@ -144,7 +142,6 @@ void FriendListWidget::setMode(SortingMode mode)
|
|||
|
||||
void FriendListWidget::sortByMode(SortingMode mode)
|
||||
{
|
||||
manager->resetParents();
|
||||
cleanMainLayout();
|
||||
|
||||
if (mode == SortingMode::Name) {
|
||||
|
@ -154,21 +151,23 @@ void FriendListWidget::sortByMode(SortingMode mode)
|
|||
addCircleWidget(i);
|
||||
}
|
||||
|
||||
QVector<std::shared_ptr<IFriendListItem>> itemsTmp = manager->getItems();
|
||||
QVector<IFriendListItem*> friendItems;
|
||||
QVector<std::shared_ptr<IFriendListItem>> itemsTmp = manager->getItems(); // Sorted items
|
||||
QVector<IFriendListItem*> friendItems; // Items that are not included in the circle
|
||||
int posByName = 0; // Needed for scroll contacts
|
||||
// Linking a friend with a circle and setting scroll position
|
||||
for (int i = 0; i < itemsTmp.size(); ++i) {
|
||||
if (itemsTmp[i]->isFriend() && itemsTmp[i]->getCircleId() >= 0) {
|
||||
CircleWidget* circleWgt = CircleWidget::getFromID(itemsTmp[i]->getCircleId());
|
||||
if (circleWgt != nullptr) {
|
||||
// Place a friend in the circle and continue
|
||||
FriendWidget* frndTmp =
|
||||
qobject_cast<FriendWidget*>((itemsTmp[i].get())->getWidget());
|
||||
circleWgt->addFriendWidget(frndTmp, frndTmp->getFriend()->getStatus());
|
||||
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());
|
||||
}
|
||||
|
||||
|
@ -177,6 +176,7 @@ void FriendListWidget::sortByMode(SortingMode mode)
|
|||
listLayout->addWidget(friendItems[i]->getWidget());
|
||||
}
|
||||
|
||||
// TODO: Try to remove
|
||||
manager->applyFilter();
|
||||
|
||||
if (!manager->needHideCircles()) {
|
||||
|
@ -195,7 +195,7 @@ void FriendListWidget::sortByMode(SortingMode mode)
|
|||
|
||||
QVector<std::shared_ptr<IFriendListItem>> itemsInCircle = getItemsFromCircle(circles.at(i));
|
||||
for (int i = 0; i < itemsInCircle.size(); ++i) {
|
||||
itemsInCircle.at(i)->setPosForName(posByName++);
|
||||
itemsInCircle.at(i)->setNameSortedPos(posByName++);
|
||||
}
|
||||
|
||||
listLayout->addWidget(circles.at(i));
|
||||
|
@ -237,6 +237,7 @@ void FriendListWidget::sortByMode(SortingMode mode)
|
|||
activityLayout->addWidget(category);
|
||||
}
|
||||
|
||||
// TODO: Try to remove
|
||||
manager->applyFilter();
|
||||
|
||||
// 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()
|
||||
{
|
||||
manager->resetParents();
|
||||
|
||||
QLayoutItem* itemForDel;
|
||||
while ((itemForDel = listLayout->takeAt(0)) != nullptr) {
|
||||
listLayout->removeWidget(itemForDel->widget());
|
||||
|
@ -276,9 +282,7 @@ void FriendListWidget::cleanMainLayout()
|
|||
QLayoutItem* itemTmp;
|
||||
while ((itemTmp = layout->takeAt(0)) != nullptr) {
|
||||
wgt = itemTmp->widget();
|
||||
if (wgt != nullptr) {
|
||||
delete wgt;
|
||||
}
|
||||
delete itemTmp;
|
||||
}
|
||||
}
|
||||
|
@ -288,7 +292,7 @@ void FriendListWidget::cleanMainLayout()
|
|||
|
||||
QWidget* FriendListWidget::getNextWidgetForName(IFriendListItem *currentPos, bool forward) const
|
||||
{
|
||||
int pos = currentPos->getPosForName();
|
||||
int pos = currentPos->getNameSortedPos();
|
||||
int nextPos = forward ? pos + 1 : pos - 1;
|
||||
if (nextPos >= manager->getItems().size()) {
|
||||
nextPos = 0;
|
||||
|
@ -297,7 +301,7 @@ QWidget* FriendListWidget::getNextWidgetForName(IFriendListItem *currentPos, boo
|
|||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -339,17 +343,17 @@ void FriendListWidget::addGroupWidget(GroupWidget* widget)
|
|||
renameGroupWidget(widget, name);
|
||||
});
|
||||
|
||||
manager->addFriendItem(widget);
|
||||
manager->addFriendListItem(widget);
|
||||
}
|
||||
|
||||
void FriendListWidget::addFriendWidget(FriendWidget* w)
|
||||
{
|
||||
manager->addFriendItem(w);
|
||||
manager->addFriendListItem(w);
|
||||
}
|
||||
|
||||
void FriendListWidget::removeGroupWidget(GroupWidget* w)
|
||||
{
|
||||
manager->removeFriendItem(w);
|
||||
manager->removeFriendListItem(w);
|
||||
}
|
||||
|
||||
void FriendListWidget::removeFriendWidget(FriendWidget* w)
|
||||
|
@ -362,7 +366,7 @@ void FriendListWidget::removeFriendWidget(FriendWidget* w)
|
|||
emit searchCircle(*circleWidget);
|
||||
}
|
||||
|
||||
manager->removeFriendItem(w);
|
||||
manager->removeFriendListItem(w);
|
||||
}
|
||||
|
||||
void FriendListWidget::addCircleWidget(int id)
|
||||
|
|
Loading…
Reference in New Issue
Block a user