Adding a sort command for the users in the chat

Everything about mods can be found here.

Moderator: Morax

Adding a sort command for the users in the chat

Postby odin002 » 12 Nov 2014, 19:20

Hai ! ( dw if you don't help, I mean I'm just messing with the source code and trying stuff, I understand that people have other stuffs to do than to help for that)

Okay It has been some days that I'm looking for modifying that (I've never used python in the past and I'm not a heavy coder. I'm rather doing that for fun, trying stuff, and help a bit maybe if I can).

So I've succeeded in making the list of users sorted by country(or even custom rating, but it can be whatever, it's just for test).
I've also added the "Sort users by country" when you right click on a user in the list of users . But uh, I've no clue on how to do more...
testonsorting.png
testonsorting.png (135.46 KiB) Viewed 2053 times


To add another choice/option when you right click on a user (for example ) :
In chatter.py
Code: Select all
def pressed(self, item):   
(..)

        actionSortUsersByCountry = QtGui.QAction("Sort users  by country", menu)
(..)
        menu.addAction(actionSortUsersByCountry)


First problem (the blind one):
So yeah I'm happy, I've added this button but it's just "shown", it does nothing, if I wanted for example to add a message on the tab "Server" (the thing no one looks on it...) with self.serverLogArea.appendPlainText("You clicked on sort by country, gj") or Debug.message (I don't remember the exact name of the function, but it's not that hard to find the exact name, anyway it's not important), I don't even know where to add that line to make it happen when I click on "sort users by country". I must be blind...

Second one (the wtf one):
I can sort the users when you connect, but I don't see where the call for showing the list is made.

So to be able to sort by country I've just modified the __lt__ function in chatter.py .
So I look in all the files of the source code of the client where the "__lt__" is written, to find it and to see who is calling it to sort the users in the list of users of chat).

So __ge__ is calling it. and that's all. and no one is calling __ge__(did the same, looked in all the files of the source code of the client), what kind of sorcery is that? :lol:
Obviously, I'm wrong the __lt__ or/and __gt__ is called, it's not a useless code, and if it was the case it shouldn't have an impact on the lobby(the list of users shouldn't be sorted), but, uh, dunno what to think about that.
odin002
Avatar-of-War
 
Posts: 96
Joined: 11 Jun 2014, 06:02
Has liked: 5 times
Been liked: 12 times
FAF User Name: odin002

Re: Adding a sort command for the users in the chat

Postby rootbeer23 » 12 Nov 2014, 20:04

odin002 wrote:First problem (the blind one):
So yeah I'm happy, I've added this button but it's just "shown", it does nothing, if I wanted for example to add a message on the tab "Server" (the thing no one looks on it...) with self.serverLogArea.appendPlainText("You clicked on sort by country, gj") or Debug.message (I don't remember the exact name of the function, but it's not that hard to find the exact name, anyway it's not important), I don't even know where to add that line to make it happen when I click on "sort users by country". I must be blind...

Code: Select all
        # Triggers
        actionStats.triggered.connect(self.viewStats)
        actionSelectAvatar.triggered.connect(self.selectAvatar)
        actionReplay.triggered.connect(self.viewReplay)
        actionVaultReplay.triggered.connect(self.viewVaultReplay)
        actionJoin.triggered.connect(self.joinInGame)
        #actionInvite.triggered.connect(self.invite)

you have to add a function an connect it to the menu item.
in the function you have to set a variable to indicate how the sorting should be done.
that variable should be used in __lt__.
the variable must be outside the Chatter class: there is one Chatter instance for each name
in the user list. so probably put that variable in the parent.
it might also be a bad idea to set the sorting when you click on a nickname, as the sorting
is not a thing that applies to a single nickname but to all nicknames. but then again you
might wanna do that instead of adding another button to the interface. in any case the
sort option is semantically not a thing that applies to a single nickname therefore the
variable that is set must be outside the Chatter class. But you can of course reference
the variable in the __lt__ function. The parent object is most likely the same for all
Chatter objects and therefore a good place to put the variable.

Second one (the wtf one):
I can sort the users when you connect, but I don't see where the call for showing the list is made.


there is currently no functionality to change the sort order and you have to somehow
execute the sorting again, i.e. "redraw" the whole nickname-list widget.
or try and call sortItems (see below).

So to be able to sort by country I've just modified the __lt__ function in chatter.py .
So I look in all the files of the source code of the client where the "__lt__" is written, to find it and to see who is calling it to sort the users in the list of users of chat).

So __ge__ is calling it. and that's all. and no one is calling __ge__(did the same, looked in all the files of the source code of the client), what kind of sorcery is that? :lol:
Obviously, I'm wrong the __lt__ or/and __gt__ is called, it's not a useless code, and if it was the case it shouldn't have an impact on the lobby(the list of users shouldn't be sorted), but, uh, dunno what to think about that.


the function is called when 'a < b' or 'a > b' is executed with a and b being Chatter objects.
and those comparisons are executed somewhere in
Code: Select all
    def setup(self):
        if not self.private:
            # Non-query channels have a sorted nicklist
            self.nickList.sortItems(Chatter.SORT_COLUMN)

the sortItems function. You dont have to concern yourself with that detail.
rootbeer23
Supreme Commander
 
Posts: 1001
Joined: 18 May 2012, 15:38
Has liked: 0 time
Been liked: 31 times
FAF User Name: root2342

Re: Adding a sort command for the users in the chat

Postby Dragonfire » 12 Nov 2014, 20:20

Welcome in the dev area :D

Sorting is possible with the friendlist in the future ;)
/viewtopic.php?p=85200#p85200


@1 (ButtonAction)
QT used Signals to fire events and you must connect this signals, e.g. to a method:
https://github.com/FAForever/lobby/blob ... er.py#L332

pyqt Signals & Slots:
http://pyqt.sourceforge.net/Docs/PyQt4/ ... slots.html

@2 (UserList)
As I implemented the Friendlist I can tell you:
Their is not single point. Each user is added to the list separately.
Normally a user is added to the chatter here:
https://github.com/FAForever/lobby/blob ... et.py#L297
The chatwidget is created in the startup process.

lt = less then
gt = greater then

This methods are "used" to sort the Chatter (QTableWidgetItem) in the List (QTableWidget).
You must fire a signal to notfiy the view that the data changed:
http://qt-project.org/doc/qt-4.8/qabstr ... ataChanged

Fire the "dataChanged" signal with QModelIndex(), QModexlIndex()
(two invalid QModelIndex are a trick to refresh the whole model)

The Nick Filter changes the model too, but it hides rows and must not call a signal.
https://github.com/FAForever/lobby/blob ... el.py#L147

BUT
A better solution is to enable the TableHeader and use the internal sorting algorithm ;)
Or use a SortFilterProxyModel:
http://qt-project.org/doc/qt-4.8/qsortf ... model.html

Ps.: I refactored the chatter:
https://github.com/FAForever/lobby/pull/122

New sorting functions:
https://github.com/IDragonfire/lobby/bl ... er.py#L117

Maybe I will add this idea ...
because it is hard to implement it as beginner :D
My native language is not english, please correct me, because I want to improve my skills # Resource Overview
User avatar
Dragonfire
Evaluator
 
Posts: 559
Joined: 19 Dec 2013, 10:18
Has liked: 39 times
Been liked: 61 times
FAF User Name: Dragonfire

Re: Adding a sort command for the users in the chat

Postby odin002 » 13 Nov 2014, 16:22

Woh, thanks a lot for these answers. I'll look into it when I've have time, but it sounds harder than planned.
odin002
Avatar-of-War
 
Posts: 96
Joined: 11 Jun 2014, 06:02
Has liked: 5 times
Been liked: 12 times
FAF User Name: odin002


Return to Mods & Tools

Who is online

Users browsing this forum: No registered users and 1 guest