Wt examples  4.2.2
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
PopupChatWidget Class Reference

A popup chat widget. More...

#include <PopupChatWidget.h>

Inheritance diagram for PopupChatWidget:
Inheritance graph
[legend]

Public Member Functions

 PopupChatWidget (SimpleChatServer &server, const std::string &id)
 
void setName (const Wt::WString &name)
 
- Public Member Functions inherited from SimpleChatWidget
 SimpleChatWidget (SimpleChatServer &server)
 Create a chat widget that will connect to the given server. More...
 
 ~SimpleChatWidget ()
 Delete a chat widget. More...
 
void connect ()
 
void disconnect ()
 
void letLogin ()
 Show a simple login screen. More...
 
bool startChat (const Wt::WString &user)
 Start a chat for the given user. More...
 
void logout ()
 
SimpleChatServerserver ()
 
int userCount ()
 
const Wt::WString & userName () const
 

Protected Member Functions

virtual void createLayout (std::unique_ptr< WWidget > messages, std::unique_ptr< WWidget > userList, std::unique_ptr< WWidget > messageEdit, std::unique_ptr< WWidget > sendButton, std::unique_ptr< WWidget > logoutButton)
 
virtual void updateUsers ()
 
virtual void newMessage ()
 
- Protected Member Functions inherited from SimpleChatWidget
virtual void createLayout (std::unique_ptr< Wt::WWidget > messages, std::unique_ptr< Wt::WWidget > userList, std::unique_ptr< Wt::WWidget > messageEdit, std::unique_ptr< Wt::WWidget > sendButton, std::unique_ptr< Wt::WWidget > logoutButton)
 
virtual void render (Wt::WFlags< Wt::RenderFlag > flags)
 
bool loggedIn () const
 

Private Member Functions

void toggleSize ()
 
void goOnline ()
 
bool minimized () const
 
std::unique_ptr< Wt::WContainerWidget > createBar ()
 

Private Attributes

Wt::WString name_
 
Wt::WText * title_
 
Wt::WWidget * bar_
 
bool online_
 
bool minimized_
 
int missedMessages_
 

Detailed Description

A popup chat widget.

Definition at line 20 of file PopupChatWidget.h.

Constructor & Destructor Documentation

◆ PopupChatWidget()

PopupChatWidget::PopupChatWidget ( SimpleChatServer server,
const std::string &  id 
)

Definition at line 19 of file PopupChatWidget.C.

23 {
24  setId(id);
25 
26  if (Wt::WApplication::instance()->environment().agentIsIE()) {
27  if (Wt::WApplication::instance()->environment().agent()
28  == Wt::UserAgent::IE6)
29  setPositionScheme(Wt::PositionScheme::Absolute);
30  else
31  setPositionScheme(Wt::PositionScheme::Fixed);
32  }
33 
34  implementJavaScript
36  "{"
37  """var s = $('#" + id + "');"
38  """s.toggleClass('chat-maximized chat-minimized');"
39  "}");
40 
41  online_ = false;
42  minimized_ = true;
43  setStyleClass("chat-widget chat-minimized");
44 
45  clear();
46  addWidget(createBar());
47  updateUsers();
48 
49  connect();
50 }
SimpleChatServer & server()
virtual void updateUsers()
std::unique_ptr< Wt::WContainerWidget > createBar()
SimpleChatWidget(SimpleChatServer &server)
Create a chat widget that will connect to the given server.

Member Function Documentation

◆ createBar()

std::unique_ptr< Wt::WContainerWidget > PopupChatWidget::createBar ( )
private

Definition at line 68 of file PopupChatWidget.C.

69 {
70  auto bar(Wt::cpp14::make_unique<Wt::WContainerWidget>());
71  bar->setStyleClass("chat-bar");
72 
73  auto toggleButton(Wt::cpp14::make_unique<Wt::WText>());
74  toggleButton->setInline(false);
75  toggleButton->setStyleClass("chat-minmax");
76  bar->clicked().connect(this, &PopupChatWidget::toggleSize);
77  bar->clicked().connect(this, &PopupChatWidget::goOnline);
78 
79  bar->addWidget(std::move(toggleButton));
80 
81  title_ = bar->addWidget(Wt::cpp14::make_unique<Wt::WText>());
82 
83  bar_ = bar.get();
84 
85  return bar;
86 }
Wt::WText * title_
Wt::WWidget * bar_

◆ createLayout()

void PopupChatWidget::createLayout ( std::unique_ptr< WWidget >  messages,
std::unique_ptr< WWidget >  userList,
std::unique_ptr< WWidget >  messageEdit,
std::unique_ptr< WWidget >  sendButton,
std::unique_ptr< WWidget >  logoutButton 
)
protectedvirtual

Definition at line 117 of file PopupChatWidget.C.

122 {
123  auto layout(Wt::cpp14::make_unique<Wt::WVBoxLayout>());
124  layout->setContentsMargins(0, 0, 0, 0);
125  layout->setSpacing(0);
126 
127  auto bar = layout->addWidget(createBar());
128  bar->setMinimumSize(Wt::WLength::Auto, 20);
129  layout->addWidget(std::move(messages), 1);
130  layout->addWidget(std::move(messageEdit));
131 
132  setLayout(std::move(layout));
133 }
std::unique_ptr< Wt::WContainerWidget > createBar()

◆ goOnline()

void PopupChatWidget::goOnline ( )
private

Definition at line 93 of file PopupChatWidget.C.

94 {
95  if (!online_) {
96  online_ = true;
97 
98  int tries = 1;
99  Wt::WString name = name_;
100  if (name.empty())
101  name = server().suggestGuest();
102 
103  while (!startChat(name)) {
104  if (name_.empty())
105  name = server().suggestGuest();
106  else
107  name = name_ + std::to_string(++tries);
108  }
109 
110  name_ = name;
111  }
112 
113  missedMessages_ = 0;
114  bar_->removeStyleClass("alert");
115 }
SimpleChatServer & server()
Wt::WString name_
Wt::WWidget * bar_
bool startChat(const Wt::WString &user)
Start a chat for the given user.
Wt::WString suggestGuest()
Get a suggestion for a guest user name.

◆ minimized()

bool PopupChatWidget::minimized ( ) const
private

Definition at line 165 of file PopupChatWidget.C.

166 {
167  return minimized_;
168 }

◆ newMessage()

void PopupChatWidget::newMessage ( )
protectedvirtual

Reimplemented from SimpleChatWidget.

Definition at line 155 of file PopupChatWidget.C.

156 {
157  if (loggedIn() && minimized()) {
158  ++missedMessages_;
159  if (missedMessages_ == 1) {
160  bar_->addStyleClass("alert");
161  }
162  }
163 }
bool minimized() const
Wt::WWidget * bar_
bool loggedIn() const

◆ setName()

void PopupChatWidget::setName ( const Wt::WString &  name)

Definition at line 52 of file PopupChatWidget.C.

53 {
54  if (name.empty())
55  return;
56 
57  if (online_) {
58  int tries = 1;
59  Wt::WString n = name;
60  while (!server().changeName(name_, n))
61  n = name + std::to_string(++tries);
62 
63  name_ = n;
64  } else
65  name_ = name;
66 }
SimpleChatServer & server()
Wt::WString name_
bool changeName(const Wt::WString &user, const Wt::WString &newUser)
Changes the name.

◆ toggleSize()

void PopupChatWidget::toggleSize ( )
private

Definition at line 88 of file PopupChatWidget.C.

89 {
91 }

◆ updateUsers()

void PopupChatWidget::updateUsers ( )
protectedvirtual

Reimplemented from SimpleChatWidget.

Definition at line 135 of file PopupChatWidget.C.

136 {
138 
139  int count = server().users().size();
140 
141  if (!loggedIn()) {
142  if (count == 0)
143  title_->setText("Thoughts? Ventilate.");
144  else if (count == 1)
145  title_->setText("Chat: 1 user online");
146  else
147  title_->setText(Wt::WString("Chat: {1} users online").arg(count));
148  } else {
149  title_->setText(Wt::WString("Chat: <span class=\"self\">{1}</span>"
150  " <span class=\"online\">({2} user{3})</span>")
151  .arg(userName()).arg(count).arg(count == 1 ? "" : "s"));
152  }
153 }
Wt::WText * title_
SimpleChatServer & server()
UserSet users()
Get the users currently logged in.
virtual void updateUsers()
const Wt::WString & userName() const
bool loggedIn() const

Member Data Documentation

◆ bar_

Wt::WWidget* PopupChatWidget::bar_
private

Definition at line 38 of file PopupChatWidget.h.

◆ minimized_

bool PopupChatWidget::minimized_
private

Definition at line 39 of file PopupChatWidget.h.

◆ missedMessages_

int PopupChatWidget::missedMessages_
private

Definition at line 40 of file PopupChatWidget.h.

◆ name_

Wt::WString PopupChatWidget::name_
private

Definition at line 36 of file PopupChatWidget.h.

◆ online_

bool PopupChatWidget::online_
private

Definition at line 39 of file PopupChatWidget.h.

◆ title_

Wt::WText* PopupChatWidget::title_
private

Definition at line 37 of file PopupChatWidget.h.


The documentation for this class was generated from the following files:

Generated on Fri Mar 20 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.15