From e4d1e21ea0da4446792bcbaa2a6afc7e5efe7dee Mon Sep 17 00:00:00 2001 From: blue Date: Tue, 24 Sep 2019 12:21:29 +0300 Subject: [PATCH] some sorting in roster --- ui/models/contact.cpp | 10 +++++++ ui/models/contact.h | 2 ++ ui/models/item.cpp | 61 ++++++++++++++++++++++++++++++++++++++++--- ui/models/item.h | 5 ++++ ui/models/room.cpp | 10 +++++++ ui/models/room.h | 4 +++ ui/models/roster.cpp | 2 +- 7 files changed, 89 insertions(+), 5 deletions(-) diff --git a/ui/models/contact.cpp b/ui/models/contact.cpp index 4c7b440..5248eb2 100644 --- a/ui/models/contact.cpp +++ b/ui/models/contact.cpp @@ -313,3 +313,13 @@ void Models::Contact::toOfflineState() emit childRemoved(); refresh(); } + +QString Models::Contact::getDisplayedName() const +{ + return getContactName(); +} + +bool Models::Contact::columnInvolvedInDisplay(int col) +{ + return Item::columnInvolvedInDisplay(col) && col == 1; +} diff --git a/ui/models/contact.h b/ui/models/contact.h index d390aec..835f7b7 100644 --- a/ui/models/contact.h +++ b/ui/models/contact.h @@ -57,9 +57,11 @@ public: unsigned int getMessagesCount() const; void dropMessages(); void getMessages(Messages& container) const; + QString getDisplayedName() const override; protected: void _removeChild(int index) override; + bool columnInvolvedInDisplay(int col) override; protected slots: void refresh(); diff --git a/ui/models/item.cpp b/ui/models/item.cpp index 59457c3..db99a11 100644 --- a/ui/models/item.cpp +++ b/ui/models/item.cpp @@ -55,16 +55,28 @@ void Models::Item::setName(const QString& p_name) void Models::Item::appendChild(Models::Item* child) { bool moving = false; - int oldRow = child->row(); - int newRow = this->childCount(); + int newRow = 0; + std::deque::const_iterator before = childItems.begin(); + while (before != childItems.end()) { + Item* bfr = *before; + if (bfr->type > child->type) { + break; + } else if (bfr->type == child->type && bfr->getDisplayedName() > child->getDisplayedName()) { + break; + } + newRow++; + before++; + } + if (child->parent != 0) { + int oldRow = child->row(); moving = true; emit childIsAboutToBeMoved(child->parent, oldRow, oldRow, this, newRow); child->parent->_removeChild(oldRow); } else { emit childIsAboutToBeInserted(this, newRow, newRow); } - childItems.push_back(child); + childItems.insert(before, child); child->parent = this; QObject::connect(child, SIGNAL(childChanged(Models::Item*, int, int)), this, SIGNAL(childChanged(Models::Item*, int, int))); @@ -147,7 +159,7 @@ void Models::Item::_removeChild(int index) { Item* child = childItems[index]; - QObject::disconnect(child, SIGNAL(childChanged(Models::Item*, int, int)), this, SIGNAL(childChanged(Models::Item*, int, int))); + QObject::disconnect(child, SIGNAL(childChanged(Models::Item*, int, int)), this, SLOT(onChildChanged(Models::Item*, int, int))); QObject::disconnect(child, SIGNAL(childIsAboutToBeInserted(Item*, int, int)), this, SIGNAL(childIsAboutToBeInserted(Item*, int, int))); QObject::disconnect(child, SIGNAL(childInserted()), this, SIGNAL(childInserted())); QObject::disconnect(child, SIGNAL(childIsAboutToBeRemoved(Item*, int, int)), this, SIGNAL(childIsAboutToBeRemoved(Item*, int, int))); @@ -212,3 +224,44 @@ QString Models::Item::getAccountName() const } return acc->getName(); } + +QString Models::Item::getDisplayedName() const +{ + return name; +} + +void Models::Item::onChildChanged(Models::Item* item, int row, int col) +{ + Item* parent = item->parentItem(); + if (parent != 0 && parent == this) { + if (item->columnInvolvedInDisplay(col)) { + int newRow = 0; + std::deque::const_iterator before = childItems.begin(); + while (before != childItems.end()) { + Item* bfr = *before; + if (bfr->type > item->type) { + break; + } else if (bfr->type == item->type && bfr->getDisplayedName() > item->getDisplayedName()) { + break; + } + newRow++; + before++; + } + + if (newRow != row || (before != childItems.end() && *before != item)) { + emit childIsAboutToBeMoved(this, row, row, this, newRow); + std::deque::const_iterator old = childItems.begin(); + old += row; + childItems.erase(old); + childItems.insert(before, item); + emit childMoved(); + } + } + } + emit childChanged(item, row, col); +} + +bool Models::Item::columnInvolvedInDisplay(int col) +{ + return col == 0; +} diff --git a/ui/models/item.h b/ui/models/item.h index 4728e54..d9d8286 100644 --- a/ui/models/item.h +++ b/ui/models/item.h @@ -55,6 +55,7 @@ class Item : public QObject{ public: virtual void appendChild(Item *child); virtual void removeChild(int index); + virtual QString getDisplayedName() const; QString getName() const; void setName(const QString& name); @@ -76,8 +77,12 @@ class Item : public QObject{ protected: virtual void changed(int col); virtual void _removeChild(int index); + virtual bool columnInvolvedInDisplay(int col); const Item* getParentAccount() const; + protected slots: + void onChildChanged(Models::Item* item, int row, int col); + protected: QString name; std::deque childItems; diff --git a/ui/models/room.cpp b/ui/models/room.cpp index 972ed77..1752696 100644 --- a/ui/models/room.cpp +++ b/ui/models/room.cpp @@ -309,3 +309,13 @@ void Models::Room::setSubject(const QString& sub) changed(6); } } + +QString Models::Room::getDisplayedName() const +{ + return getRoomName(); +} + +bool Models::Room::columnInvolvedInDisplay(int col) +{ + return Item::columnInvolvedInDisplay(col) && col == 1; +} diff --git a/ui/models/room.h b/ui/models/room.h index cc87fa9..564a26b 100644 --- a/ui/models/room.h +++ b/ui/models/room.h @@ -68,10 +68,14 @@ public: void removeParticipant(const QString& name); void toOfflineState() override; + QString getDisplayedName() const override; private: void handleParticipantUpdate(std::map::const_iterator itr, const QMap& data); +protected: + bool columnInvolvedInDisplay(int col) override; + private: bool autoJoin; bool joined; diff --git a/ui/models/roster.cpp b/ui/models/roster.cpp index 70b03d5..6c37645 100644 --- a/ui/models/roster.cpp +++ b/ui/models/roster.cpp @@ -738,7 +738,7 @@ QString Models::Roster::getContactName(const QString& account, const QString& ji if (rItr == rooms.end()) { qDebug() << "An attempt to get a name of non existing contact/room " << account << ":" << jid << ", skipping"; } else { - name = rItr->second->getName(); + name = rItr->second->getRoomName(); } } else { name = cItr->second->getContactName();