Freeciv21
Develop your civilization from humble roots to a global empire
chatline.h
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2023 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 #pragma once
14 
15 // Qt
16 #include <QEvent>
17 #include <QLineEdit>
18 #include <QTextBrowser>
19 // client
20 #include "listener.h"
21 #include "widgets/decorations.h"
22 
23 class QCheckBox;
24 class QMouseEvent;
25 class QObject;
26 class QPaintEvent;
27 class QPainter;
28 class QPushButton;
29 class QUrl;
30 
31 void set_chat_colors(const QHash<QString, QString> &colors);
32 QString apply_tags(QString str, const struct text_tag_list *tags,
33  QColor bg_color);
34 /***************************************************************************
35  Listener for chat. See listener<> for information about how to use it
36 ***************************************************************************/
37 class chat_listener : public listener<chat_listener> {
38  // History is shared among all instances...
39  static QStringList history;
40  // ...but each has its own position.
41  int position;
42 
43 public:
44  // Special value meaning "end of history".
45  static const int HISTORY_END = -1;
46 
47  explicit chat_listener();
48 
49  virtual void chat_message_received(const QString &,
50  const struct text_tag_list *);
51 
52  void send_chat_message(const QString &message);
53 
54  QString back_in_history();
55  QString forward_in_history();
57 };
58 
59 /***************************************************************************
60  Chat input widget
61 ***************************************************************************/
62 class chat_input : public QLineEdit, private chat_listener {
63  Q_OBJECT
64 
65 private slots:
66  void send();
67 
68 public:
69  explicit chat_input(QWidget *parent = nullptr);
70 
71 protected:
72  bool event(QEvent *event) override;
73  void focusInEvent(QFocusEvent *event) override;
74 
75 private:
76  void update_completion();
77 };
78 
79 /***************************************************************************
80  Text browser with mouse double click signal
81 ***************************************************************************/
82 class text_browser_dblclck : public QTextBrowser {
83  Q_OBJECT
84 
85 public:
86  explicit text_browser_dblclck(QWidget *parent = nullptr)
87  : QTextBrowser(parent)
88  {
89  }
90 signals:
91  void dbl_clicked();
92 
93 protected:
94  void mouseDoubleClickEvent(QMouseEvent *event) override;
95 };
96 
97 /***************************************************************************
98  Class for chat widget
99 ***************************************************************************/
100 class chat_widget : public resizable_widget, private chat_listener {
101  Q_OBJECT
102 
103 public:
104  chat_widget(QWidget *parent);
105  virtual ~chat_widget();
106 
107  void append(const QString &str);
109  void make_link(struct tile *ptile);
110  void update_widgets();
111  int default_size(int lines);
112  void take_focus();
113  void update_font();
114 
116  bool is_chat_visible() const { return m_chat_visible; }
117  void set_chat_visible(bool visible);
118 
119 private slots:
120  void update_menu() override {}
121  void rm_links();
122  void anchor_clicked(const QUrl &link);
123 
124 protected:
125  void paintEvent(QPaintEvent *event) override;
126  bool eventFilter(QObject *obj, QEvent *event) override;
127 
128 private:
129  void chat_message_received(const QString &message,
130  const struct text_tag_list *tags) override;
131 
132  bool m_chat_visible = true;
133  QTextBrowser *chat_output;
134  QPushButton *remove_links;
135  QPushButton *show_hide;
136  QToolButton *cb;
137  QMenu *cb_menu;
139 };
140 
141 void real_output_window_append(const QString &astring,
142  const text_tag_list *tags);
143 void version_message(const QString &vertext);
void real_output_window_append(const QString &astring, const text_tag_list *tags)
Appends the string to the chat output window.
Definition: chatline.cpp:881
void version_message(const QString &vertext)
Got version message from metaserver.
Definition: chatline.cpp:897
void set_chat_colors(const QHash< QString, QString > &colors)
Sets color substitution map.
Definition: chatline.cpp:61
QString apply_tags(QString str, const struct text_tag_list *tags, QColor bg_color)
Applies tags to text.
Definition: chatline.cpp:710
void send()
Sends the content of the input box.
Definition: chatline.cpp:178
void update_completion()
Called whenever the completion word list changes.
Definition: chatline.cpp:187
void focusInEvent(QFocusEvent *event) override
Event handler for chat_input, used for history.
Definition: chatline.cpp:333
chat_input(QWidget *parent=nullptr)
Constructor.
Definition: chatline.cpp:169
bool event(QEvent *event) override
Event handler for chat_input, used for history.
Definition: chatline.cpp:314
static const int HISTORY_END
Definition: chatline.h:45
QString forward_in_history()
Goes forward one position in history, and returns the message at the new position.
Definition: chatline.cpp:147
void send_chat_message(const QString &message)
Sends commands to server, but first searches for custom keys, if it finds then it makes custom action...
Definition: chatline.cpp:86
void reset_history_position()
Go to the end of the history.
Definition: chatline.cpp:164
QString back_in_history()
Goes back one position in history, and returns the message at the new position.
Definition: chatline.cpp:133
static QStringList history
Definition: chatline.h:39
chat_listener()
Constructor.
Definition: chatline.cpp:69
virtual void chat_message_received(const QString &, const struct text_tag_list *)
Called whenever a message is received.
Definition: chatline.cpp:75
int position
Definition: chatline.h:41
QTextBrowser * chat_output
Definition: chatline.h:133
void chat_message_received(const QString &message, const struct text_tag_list *tags) override
Adds news string to chat_widget (from chat_listener interface)
Definition: chatline.cpp:586
virtual ~chat_widget()
Destructor.
Definition: chatline.cpp:452
void anchor_clicked(const QUrl &link)
User clicked some custom link.
Definition: chatline.cpp:535
QPushButton * remove_links
Definition: chatline.h:134
chat_widget(QWidget *parent)
Constructor for chat_widget.
Definition: chatline.cpp:342
chat_input * chat_line
Definition: chatline.h:108
void update_widgets()
Hides allies and links button for local game.
Definition: chatline.cpp:643
void take_focus()
Shows the chat and ensures the chat line has focus.
Definition: chatline.cpp:513
move_widget * mw
Definition: chatline.h:138
QToolButton * cb
Definition: chatline.h:136
int default_size(int lines)
Returns how much space chatline of given number of lines would require, or zero if it can't be determ...
Definition: chatline.cpp:656
void update_font()
Updates font for chat_widget.
Definition: chatline.cpp:522
void update_menu() override
Definition: chatline.h:120
bool m_chat_visible
Definition: chatline.h:132
QMenu * cb_menu
Definition: chatline.h:137
void rm_links()
User clicked clear links button.
Definition: chatline.cpp:530
void append(const QString &str)
Adds news string to chat_widget.
Definition: chatline.cpp:596
QPushButton * show_hide
Definition: chatline.h:135
void set_chat_visible(bool visible)
Manages toggling minimization.
Definition: chatline.cpp:461
void make_link(struct tile *ptile)
Makes link to tile/unit or city.
Definition: chatline.cpp:690
void paintEvent(QPaintEvent *event) override
Paint event for chat_widget.
Definition: chatline.cpp:606
bool is_chat_visible() const
Returns whether the chat widget is currently visible.
Definition: chatline.h:116
bool eventFilter(QObject *obj, QEvent *event) override
Processess history for chat.
Definition: chatline.cpp:623
void mouseDoubleClickEvent(QMouseEvent *event) override
Definition: chatline.cpp:614
text_browser_dblclck(QWidget *parent=nullptr)
Definition: chatline.h:86
enum event_type event
Definition: events.cpp:68
Colors.
char * lines
Definition: packhand.cpp:131
Definition: tile.h:42