ChatList.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*\
  2. |*| Copyright 2015-2016 bill-auger <https://github.com/bill-auger/av-caster/issues>
  3. |*|
  4. |*| This file is part of the AvCaster program.
  5. |*|
  6. |*| AvCaster is free software: you can redistribute it and/or modify
  7. |*| it under the terms of the GNU General Public License version 3
  8. |*| as published by the Free Software Foundation.
  9. |*|
  10. |*| AvCaster is distributed in the hope that it will be useful,
  11. |*| but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. |*| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. |*| GNU General Public License for more details.
  14. |*|
  15. |*| You should have received a copy of the GNU General Public License
  16. |*| along with AvCaster. If not, see <http://www.gnu.org/licenses/>.
  17. \*/
  18. //[Headers] You can add your own extra header files here...
  19. #include "../Controllers/AvCaster.h"
  20. #include "../Trace/TraceChat.h"
  21. //[/Headers]
  22. #include "ChatList.h"
  23. //[MiscUserDefs] You can add your own user definitions and misc code here...
  24. //[/MiscUserDefs]
  25. //==============================================================================
  26. ChatList::ChatList ()
  27. {
  28. //[Constructor_pre] You can add your own custom stuff here..
  29. //[/Constructor_pre]
  30. addAndMakeVisible (chattersGroup = new GroupComponent ("chattersGroup",
  31. TRANS("Nicks")));
  32. chattersGroup->setTextLabelPosition (Justification::centred);
  33. chattersGroup->setColour (GroupComponent::outlineColourId, Colours::white);
  34. chattersGroup->setColour (GroupComponent::textColourId, Colours::white);
  35. addAndMakeVisible (dummyChatListItem = new ChatListItem (ValueTree::invalid));
  36. dummyChatListItem->setName ("dummyChatListItem");
  37. //[UserPreSize]
  38. //[/UserPreSize]
  39. setSize (128, 48);
  40. //[Constructor] You can add your own custom stuff here..
  41. //[/Constructor]
  42. }
  43. ChatList::~ChatList()
  44. {
  45. //[Destructor_pre]. You can add your own custom destruction code here..
  46. //[/Destructor_pre]
  47. chattersGroup = nullptr;
  48. dummyChatListItem = nullptr;
  49. //[Destructor]. You can add your own custom destruction code here..
  50. this->chatListItems.clear() ;
  51. //[/Destructor]
  52. }
  53. //==============================================================================
  54. void ChatList::paint (Graphics& g)
  55. {
  56. //[UserPrePaint] Add your own custom painting code here..
  57. //[/UserPrePaint]
  58. g.setColour (Colour (0xff202020));
  59. g.fillRoundedRectangle (4.0f, 10.0f, 120.0f, 34.0f, 4.000f);
  60. //[UserPaint] Add your own custom painting code here..
  61. g.fillRoundedRectangle(4.0f , 10.0f , getWidth() - 8.0f , getHeight() - 14.0f , 4.000f) ;
  62. //[/UserPaint]
  63. }
  64. void ChatList::resized()
  65. {
  66. //[UserPreResize] Add your own custom resize code here..
  67. //[/UserPreResize]
  68. chattersGroup->setBounds (0, 0, 128, 48);
  69. dummyChatListItem->setBounds (12, 16, 104, 24);
  70. //[UserResized] Add your own custom resize handling here..
  71. this->chattersGroup->setSize(getWidth() , getHeight()) ;
  72. //[/UserResized]
  73. }
  74. //[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
  75. void ChatList::valueTreeChildAdded(ValueTree& a_parent_node , ValueTree& a_node)
  76. {
  77. if (a_parent_node != this->chattersStore) return ;
  78. ValueTree chatter_store = a_node ;
  79. int item_idx = sortedChatterIdx(chatter_store) + GUI::N_STATIC_CHATLIST_CHILDREN ;
  80. ChatListItem* chat_list_item = new ChatListItem(chatter_store) ;
  81. DEBUG_TRACE_ADD_CHATLIST_ITEM
  82. const MessageManagerLock mmLock ; addAndMakeVisible(chat_list_item , item_idx) ;
  83. this->chatListItems.add(chat_list_item) ;
  84. refresh() ;
  85. }
  86. void ChatList::valueTreeChildRemoved(ValueTree& a_parent_node , ValueTree& a_node , int idx)
  87. {
  88. if (a_parent_node != this->chattersStore) return ;
  89. ValueTree chatter_store = a_node ;
  90. int item_idx = sortedChatterIdx(chatter_store) + GUI::N_STATIC_CHATLIST_CHILDREN ;
  91. ChatListItem* chat_list_item = static_cast<ChatListItem*>(getChildComponent(item_idx)) ;
  92. DEBUG_TRACE_REMOVE_CHATLIST_ITEM
  93. const MessageManagerLock mmLock ;
  94. this->chatListItems.removeObject(chat_list_item) ;
  95. refresh() ;
  96. }
  97. void ChatList::valueTreePropertyChanged(ValueTree& a_node , const Identifier& a_key)
  98. {
  99. if (a_node != this->networkStore || a_key != CONFIG::RETRIES_ID) return ;
  100. // show/hide dummy user ChatListItem (asynchronously)
  101. int connected_state = int(this->networkStore[CONFIG::RETRIES_ID]) ;
  102. bool should_show_dummy = connected_state != IRC::STATE_CONNECTED &&
  103. connected_state != IRC::STATE_FAILED ;
  104. StringArray dummy_chatter = StringArray::fromTokens(GUI::CONNECTING_TEXT , false) ;
  105. StringArray no_chatters = StringArray() ;
  106. StringArray nicks = (should_show_dummy) ? dummy_chatter : no_chatters ;
  107. if (connected_state != IRC::STATE_CONNECTED) AvCaster::UpdateChatters(nicks) ;
  108. }
  109. void ChatList::initialize(ValueTree network_store , ValueTree chatters_store)
  110. {
  111. // register interest in chat network connection state
  112. (this->networkStore = network_store ).addListener(this) ;
  113. // register interest in channel joins/parts
  114. (this->chattersStore = chatters_store).addListener(this) ;
  115. }
  116. int ChatList::sortedChatterIdx(ValueTree& chatter_store)
  117. {
  118. StringArray nicks = AvCaster::GetChatNicks() ;
  119. String nick = STRING(chatter_store[CONFIG::NICK_ID]) ;
  120. nicks.add(nick) ; nicks.sort(true) ;
  121. DEBUG_TRACE_LOCATE_SORTED_CHILD
  122. return nicks.indexOf(nick) ;
  123. }
  124. void ChatList::refresh()
  125. {
  126. int n_chatters = getNumChildComponents() - GUI::N_STATIC_CHATLIST_CHILDREN ;
  127. int list_h = GUI::EMPTY_CHATLIST_H + (n_chatters * GUI::PADDED_CHATLIST_ITEM_H) ;
  128. int list_item_x = GUI::PAD3 ;
  129. int list_item_y = GUI::PAD4 ;
  130. bool is_visible = n_chatters > 0 ;
  131. DEBUG_TRACE_RESIZE_CHATLIST
  132. const MessageManagerLock mmLock ;
  133. // arrange list entries
  134. for (int chatter_n = 0 ; chatter_n < n_chatters ; ++chatter_n)
  135. {
  136. int child_n = chatter_n + GUI::N_STATIC_CHATLIST_CHILDREN ;
  137. Component* list_item = getChildComponent(child_n) ;
  138. DEBUG_TRACE_MOVE_CHATLIST_ITEM
  139. list_item->setTopLeftPosition(list_item_x , list_item_y) ;
  140. list_item_y += GUI::PADDED_CHATLIST_ITEM_H ;
  141. }
  142. setSize(GUI::CHATLIST_W , list_h) ;
  143. (static_cast<Chat*>(getParentComponent()))->updateVisiblilty(is_visible) ;
  144. }
  145. //[/MiscUserCode]
  146. //==============================================================================
  147. #if 0
  148. /* -- Projucer information section --
  149. This is where the Projucer stores the metadata that describe this GUI layout, so
  150. make changes in here at your peril!
  151. BEGIN_JUCER_METADATA
  152. <JUCER_COMPONENT documentType="Component" className="ChatList" componentName=""
  153. parentClasses="public Component, public ValueTree::Listener"
  154. constructorParams="" variableInitialisers="" snapPixels="4" snapActive="1"
  155. snapShown="1" overlayOpacity="0.330" fixedSize="0" initialWidth="128"
  156. initialHeight="48">
  157. <BACKGROUND backgroundColour="0">
  158. <ROUNDRECT pos="4 10 120 34" cornerSize="4" fill="solid: ff202020" hasStroke="0"/>
  159. </BACKGROUND>
  160. <GROUPCOMPONENT name="chattersGroup" id="49d2cfd1b8779149" memberName="chattersGroup"
  161. virtualName="" explicitFocusOrder="0" pos="0 0 128 48" outlinecol="ffffffff"
  162. textcol="ffffffff" title="Nicks" textpos="36"/>
  163. <GENERICCOMPONENT name="dummyChatListItem" id="56b906e64be2f4b6" memberName="dummyChatListItem"
  164. virtualName="" explicitFocusOrder="0" pos="12 16 104 24" class="ChatListItem"
  165. params="ValueTree::invalid"/>
  166. </JUCER_COMPONENT>
  167. END_JUCER_METADATA
  168. */
  169. #endif
  170. //[EndFile] You can add extra defines here...
  171. //[/EndFile]