Freeciv-3.2
Loading...
Searching...
No Matches
chatline.cpp
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18// Qt
19#include <QApplication>
20#include <QCheckBox>
21#include <QCompleter>
22#include <QGridLayout>
23#include <QKeyEvent>
24#include <QPainter>
25#include <QScrollBar>
26#include <QStyleFactory>
27#include <QTextBlock>
28#include <QTextLayout>
29#include <QTextBrowser>
30
31// common
32#include "chat.h"
33
34// client
35#include "audio.h"
36#include "climap.h"
37#include "climisc.h" // For write_chatline_content()
38#include "connectdlg_common.h"
39#include "control.h"
40#include "game.h"
41
42// gui-qt
43#include "chatline.h"
44#include "colors.h"
45#include "fc_client.h"
46#include "gui_main.h"
47#include "qtg_cxxside.h"
48
49static bool is_plain_public_message(QString s);
50
54
55/***********************************************************************/
59{
61
63 if (pconn->playing) {
64 word_list << pconn->playing->name;
65 word_list << pconn->playing->username;
66 } else {
67 word_list << pconn->username;
68 }
70
71 players_iterate(pplayer) {
72 str = pplayer->name;
73 if (!word_list.contains(str)) {
74 word_list << str;
75 }
77
79}
80
81/***********************************************************************/
85 position(HISTORY_END)
86{}
87
88/***********************************************************************/
93 const struct text_tag_list *)
94{}
95
96/***********************************************************************/
102
103/***********************************************************************/
110{
111 int index;
112 QString splayer, s;
113
114 /* FIXME
115 * Key == PICK: used for picking nation, it was put here cause those
116 * Qt slots are a bit limited ...I'm unable to pass custom player pointer
117 * or idk how to do that
118 */
119 s = message;
120 index = message.indexOf("PICK:");
121
122 if (index != -1) {
123 s = s.remove("PICK:");
124 // Now should be playername left in string
125 players_iterate(pplayer) {
126 splayer = QString(pplayer->name);
127
128 if (!splayer.compare(s)) {
129 popup_races_dialog(pplayer);
130 }
132 return;
133 }
134
135 history << message;
137
138 // If client send commands to take ai, set /away to disable AI
139 if (message.startsWith("/take ")) {
140 s = s.remove("/take ");
141 players_iterate(pplayer) {
142 splayer = QString(pplayer->name);
143 splayer = "\"" + splayer + "\"";
144
145 if (!splayer.compare(s)) {
146 if (is_ai(pplayer)) {
147 send_chat(message.toUtf8());
148 send_chat("/away");
149 return;
150 }
151 }
153 }
154
155 // Option to send to allies by default
156 if (!message.isEmpty()) {
160 + " " + message).toUtf8());
161 } else {
162 send_chat(message.toUtf8());
163 }
164 }
165 // Empty messages aren't sent
166 // FIXME Inconsistent behavior: "." will send an empty message to allies
167}
168
169/***********************************************************************/
174{
175 if (!history.empty() && position == HISTORY_END) {
176 position = history.size() - 1;
177 } else if (position > 0) {
178 position--;
179 }
180 return history.empty() ? "" : history.at(position);
181}
182
183/***********************************************************************/
188{
189 if (position == HISTORY_END) {
190 return "";
191 }
192 position++;
193 if (position >= history.size()) {
195 return "";
196 } else {
197 return history.at(position);
198 }
199}
200
201/***********************************************************************/
208
209/***********************************************************************/
213 QLineEdit(parent)
214{
215 connect(this, &QLineEdit::returnPressed, this, &chat_input::send);
218}
219
220/***********************************************************************/
224{
225 send_chat_message(text());
226 clear();
227}
228
229/***********************************************************************/
233{
235
236 if (cmplt != nullptr) {
237 delete cmplt;
238 }
239
241 cmplt->setCaseSensitivity(Qt::CaseInsensitive);
242 cmplt->setCompletionMode(QCompleter::InlineCompletion);
244}
245
246/***********************************************************************/
250{
251 if (event->type() == QEvent::KeyPress) {
252 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
253 switch (keyEvent->key()) {
254 case Qt::Key_Up:
256 return true;
257 case Qt::Key_Down:
259 return true;
260 }
261 }
262 return QLineEdit::event(event);
263}
264
265/***********************************************************************/
269{
271
273 cb = new QCheckBox("");
274 cb->setToolTip(_("Allies only"));
276 gl = new QGridLayout;
277 chat_line = new chat_input;
280 remove_links = new QPushButton("");
281 remove_links->setIcon(style()->standardPixmap(QStyle::SP_DialogCancelButton));
282 remove_links->setToolTip(_("Clear links"));
283 gl->setVerticalSpacing(0);
284 gl->addWidget(chat_output,0 , 0, 1 ,3);
285 gl->addWidget(chat_line,1, 0);
286 gl->addWidget(cb,1,1);
287 gl->addWidget(remove_links,1,2);
288 gl->setContentsMargins(0, 0, 6, 0);
289 setLayout(gl);
290 chat_output->setReadOnly(true);
291 chat_line->installEventFilter(this);
292 chat_output->setVisible(true);
293 chat_output->setAcceptRichText(true);
294 chat_output->setOpenLinks(false);
295 chat_output->setReadOnly(true);
296 connect(chat_output, &QTextBrowser::anchorClicked,
298 connect(chat_output, &QTextBrowser::anchorClicked,
301 this, &chatwdg::toggle_size);
302 connect(remove_links, &QAbstractButton::clicked, this, &chatwdg::rm_links);
303 connect(cb, &QCheckBox::stateChanged, this, &chatwdg::state_changed);
304 setMouseTracking(true);
305
307}
308
309/***********************************************************************/
313{
314 if (state > 0) {
316 } else {
318 }
319}
320
321/***********************************************************************/
325{
326 if (gui()->infotab->chat_maximized) {
327 gui()->infotab->restore_chat();
328 return;
329 } else {
330 gui()->infotab->maximize_chat();
331 chat_line->setFocus();
332 }
333}
334
335/***********************************************************************/
339{
340 chat_output->verticalScrollBar()->setSliderPosition(
341 chat_output->verticalScrollBar()->maximum());
342}
343
344
345/***********************************************************************/
349{
350 QFont *qf;
352 chat_output->setFont(*qf);
353}
354
355/***********************************************************************/
362
363/***********************************************************************/
367{
368 int n;
370 int id;
371 enum text_link_type type;
372 struct tile *ptile = nullptr;
373
374 sl = link.toString().split(",");
375 n = sl.at(0).toInt();
376 id = sl.at(1).toInt();
377
378 type = static_cast<text_link_type>(n);
379
380 switch (type) {
381 case TLT_CITY: {
382 struct city *pcity = game_city_by_number(id);
383
384 if (pcity) {
385 ptile = client_city_tile(pcity);
386 } else {
387 output_window_append(ftc_client, _("This city isn't known!"));
388 }
389 }
390 break;
391 case TLT_TILE:
392 ptile = index_to_tile(&(wld.map), id);
393
394 if (!ptile) {
396 _("This tile doesn't exist in this game!"));
397 }
398 break;
399 case TLT_UNIT: {
400 struct unit *punit = game_unit_by_number(id);
401
402 if (punit) {
403 ptile = unit_tile(punit);
404 } else {
405 output_window_append(ftc_client, _("This unit isn't known!"));
406 }
407 }
408 break;
409 }
410 if (ptile != nullptr) {
413 }
414}
415
416/***********************************************************************/
420 const struct text_tag_list *tags)
421{
422 QColor col = chat_output->palette().color(QPalette::Text);
423
424 append(apply_tags(message, tags, col));
425}
426
427/***********************************************************************/
431{
432 QTextCursor cursor;
433
434 chat_output->append(str);
435 chat_output->verticalScrollBar()->setSliderPosition(
436 chat_output->verticalScrollBar()->maximum());
437}
438
439/***********************************************************************/
443{
444 painter->setBrush(QColor(0, 0, 0, 35));
445 painter->drawRect(0, 0, width(), height());
446}
447
448/***********************************************************************/
452{
454
455 painter.begin(this);
457 painter.end();
458}
459
460/***********************************************************************/
463bool chatwdg::eventFilter(QObject *obj, QEvent *event)
464{
465 if (obj == chat_line) {
466 if (event->type() == QEvent::KeyPress) {
467 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
468
469 if (keyEvent->key() == Qt::Key_Escape) {
470 gui()->infotab->restore_chat();
471 gui()->mapview_wdg->setFocus();
472 return true;
473 }
474 }
475 if (event->type() == QEvent::ShortcutOverride) {
476 event->setAccepted(true);
477 }
478 }
479
480 return QObject::eventFilter(obj, event);
481}
482
483/***********************************************************************/
487{
488 if (is_server_running()) {
489 cb->hide();
490 remove_links->hide();
491 } else {
492 cb->show();
493 remove_links->show();
494 }
495}
496
497/***********************************************************************/
502{
503 int line_count = 0;
504 int line_height;
505 int size;
507
508 qtb = chat_output->document()->firstBlock();
509 /* Count all lines in all text blocks layouts
510 * document()->lineCount returns number of lines without wordwrap */
511
512 while (qtb.isValid()) {
513 line_count = line_count + qtb.layout()->lineCount();
514 qtb = qtb.next();
515 }
516
517 if (line_count == 0) {
518 return 0;
519 }
520
521 line_height = (chat_output->document()->size().height()
522 - 2 * chat_output->document()->documentMargin())
523 / line_count;
524
526 + chat_line->size().height() + chat_output->document()->documentMargin();
527 size = qMax(0, size);
528
529 return size;
530}
531
532/***********************************************************************/
535void chatwdg::make_link(struct tile *ptile)
536{
537 struct unit *punit;
538 char buf[MAX_LEN_MSG];
539
540 punit = find_visible_unit(ptile);
541 if (tile_city(ptile)) {
543 } else if (punit) {
545 } else {
546 sz_strlcpy(buf, tile_link(ptile));
547 }
548 chat_line->insert(QString(buf));
549 chat_line->setFocus();
550}
551
552/***********************************************************************/
557{
558 int start, stop, last_i;
563 QColor qc;
566
567 if (tags == nullptr) {
568 return str;
569 }
570 str_bytes = str.toUtf8();
571 qba = str_bytes.data();
572
575 stop = qba.length();
576 } else {
578 }
579
581 start = 0;
582 } else {
584 }
585 switch (text_tag_type(ptag)) {
586 case TTT_BOLD:
587 mm.insert(stop, "</b>");
588 mm.insert(start, "<b>");
589 break;
590 case TTT_ITALIC:
591 mm.insert(stop, "</i>");
592 mm.insert(start, "<i>");
593 break;
594 case TTT_STRIKE:
595 mm.insert(stop, "</s>");
596 mm.insert(start, "<s>");
597 break;
598 case TTT_UNDERLINE:
599 mm.insert(stop, "</u>");
600 mm.insert(start, "<u>");
601 break;
602 case TTT_COLOR:
605 if (color == "#00008B") {
606 color = bg_color.name();
607 } else {
608 qc.setNamedColor(color);
609 qc = qc.lighter(200);
610 color = qc.name();
611 }
612 str_col = QString("<span style=color:%1>").arg(color);
613 mm.insert(stop, "</span>");
614 mm.insert(start, str_col);
615 }
618 if (QColor::isValidColor(color)) {
619 str_col = QString("<span style= background-color:%1;>").arg(color);
620 mm.insert(stop, "</span>");
621 mm.insert(start, str_col);
622 }
623 }
624 break;
625 case TTT_LINK: {
626 struct color *pcolor = nullptr;
627
628 switch (text_tag_link_type(ptag)) {
629 case TLT_CITY:
631 break;
632 case TLT_TILE:
634 break;
635 case TLT_UNIT:
637 break;
638 }
639
640 if (pcolor == nullptr) {
641 break; // Not a valid link type case.
642 }
643 color = pcolor->qcolor.name(QColor::HexRgb);
644 str_col = QString("<font color=\"%1\">").arg(color);
645 mm.insert(stop, "</a></font>");
646
647 color = QString(str_col + "<a href=%1,%2>").
648 arg(QString::number(text_tag_link_type(ptag)),
649 QString::number(text_tag_link_id(ptag)));
650 mm.insert(start, color);
651 }
652 }
654
655 // Insert html starting from last items
656 last_i = str.length();
657 QMultiMap<int, QString>::const_iterator i = mm.constEnd();
658 QMultiMap<int, QString>::const_iterator j = mm.constEnd();
659
660 while (i != mm.constBegin()) {
661 i--;
662 if (i.key() < last_i) {
663 final_string = final_string.prepend(QString(qba.mid(i.key(),
664 last_i - i.key()))
665 .toHtmlEscaped());
666 }
667 last_i = i.key();
668 j = i;
669 if (i != mm.constBegin()) {
670 j--;
671 }
672 if (j.key() == i.key() && i != j) {
673 final_string = final_string.prepend(j.value());
674 final_string = final_string.prepend(i.value());
675 i--;
676 } else {
677 final_string = final_string.prepend(i.value());
678 }
679 }
680
681 if (last_i == str.length()) {
682 return str;
683 }
684
685 return final_string;
686}
687
688/***********************************************************************/
693{
694 QString s1, str;
695 int i;
696
697 str = s.trimmed();
698 if (str.at(0) == SERVER_COMMAND_PREFIX
699 || str.at(0) == CHAT_ALLIES_PREFIX
700 || str.at(0) == CHAT_DIRECT_PREFIX) {
701 return false;
702 }
703
704 // Search for private message
705 if (!str.contains(CHAT_DIRECT_PREFIX)) {
706 return true;
707 }
708 i = str.indexOf(CHAT_DIRECT_PREFIX);
709 str = str.left(i);
710
711 // Compare all players and connections looking for match
713 s1 = pconn->username;
714 if (s1.length() < i) {
715 continue;
716 }
717 if (!QString::compare(s1.left(i), str, Qt::CaseInsensitive)) {
718 return false;
719 }
721 players_iterate(pplayer) {
722 s1 = pplayer->name;
723 if (s1.length() < i) {
724 continue;
725 }
726 if (!QString::compare(s1.left(i), str, Qt::CaseInsensitive)) {
727 return false;
728 }
730
731 return true;
732}
733
734/***********************************************************************/
739 const struct text_tag_list *tags,
740 int conn_id)
741{
742 QString str;
745
746 str = QString::fromUtf8(astring);
747 gui()->set_status_bar(str);
748
750
751 // Format wakeup string if needed
752 if (wakeup.contains("%1")) {
754 }
755
756 qapp = current_app();
757
758 if (str.contains(client.conn.username)) {
759 qapp->alert(gui()->central_wdg);
760 }
761
762 // Play sound if we encountered wakeup string
763 if (str.contains(wakeup) && client_state() < C_S_RUNNING
764 && !wakeup.isEmpty()) {
765 qapp->alert(gui()->central_wdg);
766 audio_play_sound("e_player_wake", nullptr, nullptr);
767 }
768
771 str, tags);
772}
773
774/***********************************************************************/
779{
780 // PORTME
782}
783
784/***********************************************************************/
788{
789 // PORTME
790#if 0
791 set_output_window_text(_("Cleared output window."));
792#endif
793}
794
795/***********************************************************************/
799 QEvent(QEvent::User),
800 message(msg)
801{}
802
803/***********************************************************************/
807{
808 current_app()->postEvent(gui(), new version_message_event(vertext));
809}
#define str
Definition astring.c:76
#define n
Definition astring.c:77
void audio_play_sound(const char *const tag, const char *const alt_tag, const char *const alt_tag2)
Definition audio.c:528
static QFont * get_font(enum client_font font)
Definition canvas.cpp:379
struct canvas int int struct sprite int int int int height
Definition canvas_g.h:44
struct canvas int int int int struct sprite *sprite struct canvas struct color * pcolor
Definition canvas_g.h:56
struct canvas int int struct sprite int int int width
Definition canvas_g.h:44
#define CHAT_DIRECT_PREFIX
Definition chat.h:31
#define CHAT_ALLIES_PREFIX
Definition chat.h:30
#define SERVER_COMMAND_PREFIX
Definition chat.h:28
void log_output_window(void)
Definition chatline.cpp:778
void clear_output_window(void)
Definition chatline.cpp:787
QString apply_tags(QString str, const struct text_tag_list *tags, QColor bg_color)
Definition chatline.cpp:555
void qtg_version_message(const char *vertext)
Definition chatline.cpp:806
void qtg_real_output_window_append(const char *astring, const struct text_tag_list *tags, int conn_id)
Definition chatline.cpp:738
static bool is_plain_public_message(QString s)
Definition chatline.cpp:692
int send_chat(const char *message)
void output_window_append(const struct ft_color color, const char *featured_text)
void write_chatline_content(const char *txt)
bool event(QEvent *event)
Definition chatline.cpp:249
virtual void chat_word_list_changed(const QStringList &cmplt_word_list)
Definition chatline.cpp:232
void send()
Definition chatline.cpp:223
chat_input(QWidget *parent=nullptr)
Definition chatline.cpp:212
QStringList current_word_list()
Definition chatline.h:75
static const int HISTORY_END
Definition chatline.h:58
QString forward_in_history()
Definition chatline.cpp:187
void send_chat_message(const QString &message)
Definition chatline.cpp:109
void reset_history_position()
Definition chatline.cpp:204
QString back_in_history()
Definition chatline.cpp:173
static QStringList history
Definition chatline.h:49
virtual void chat_word_list_changed(const QStringList &cmplt_word_list)
Definition chatline.cpp:100
static void update_word_list()
Definition chatline.cpp:58
static QStringList word_list
Definition chatline.h:54
virtual void chat_message_received(const QString &, const struct text_tag_list *)
Definition chatline.cpp:92
bool eventFilter(QObject *obj, QEvent *event)
Definition chatline.cpp:463
chat_input * chat_line
Definition chatline.h:121
chatwdg(QWidget *parent)
Definition chatline.cpp:268
void paintEvent(QPaintEvent *event)
Definition chatline.cpp:451
void update_font()
Definition chatline.cpp:348
void anchor_clicked(const QUrl &link)
Definition chatline.cpp:366
int default_size(int lines)
Definition chatline.cpp:501
text_browser_dblclck * chat_output
Definition chatline.h:140
void make_link(struct tile *ptile)
Definition chatline.cpp:535
void chat_message_received(const QString &message, const struct text_tag_list *tags)
Definition chatline.cpp:419
void scroll_to_bottom()
Definition chatline.cpp:338
void paint(QPainter *painter, QPaintEvent *event)
Definition chatline.cpp:442
void update_widgets()
Definition chatline.cpp:486
QCheckBox * cb
Definition chatline.h:142
void rm_links()
Definition chatline.cpp:358
QPushButton * remove_links
Definition chatline.h:141
void state_changed(int state)
Definition chatline.cpp:312
void append(const QString &str)
Definition chatline.cpp:430
void toggle_size()
Definition chatline.cpp:324
static fc_font * instance()
Definition fonts.cpp:41
QFont * get_font(QString name)
Definition fonts.cpp:63
static void invoke(_member_fct_ function)
Definition listener.h:175
version_message_event(const QString &msg)
Definition chatline.cpp:798
struct civclient client
enum client_states client_state(void)
@ C_S_RUNNING
Definition client_main.h:47
struct tile * client_city_tile(const struct city *pcity)
Definition climap.c:87
struct color * get_color(const struct tileset *t, enum color_std stdcolor)
char * incite_cost
Definition comments.c:75
#define MAX_LEN_MSG
Definition conn_types.h:37
bool is_server_running(void)
#define conn_list_iterate(connlist, pconn)
Definition connection.h:108
#define conn_list_iterate_end
Definition connection.h:110
struct unit * find_visible_unit(struct tile *ptile)
Definition control.c:822
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:74
int int id
Definition editgui_g.h:28
enum event_type event
Definition events.c:81
#define _(String)
Definition fcintl.h:67
enum text_link_type text_tag_link_type(const struct text_tag *ptag)
const char * tile_link(const struct tile *ptile)
ft_offset_t text_tag_stop_offset(const struct text_tag *ptag)
const char * text_tag_color_foreground(const struct text_tag *ptag)
const struct ft_color ftc_client
const char * city_link(const struct city *pcity)
int text_tag_link_id(const struct text_tag *ptag)
ft_offset_t text_tag_start_offset(const struct text_tag *ptag)
const char * unit_link(const struct unit *punit)
const char * text_tag_color_background(const struct text_tag *ptag)
#define text_tag_list_iterate_end
#define text_tag_list_iterate(tags, ptag)
#define FT_OFFSET_UNSET
text_link_type
@ TLT_TILE
@ TLT_UNIT
@ TLT_CITY
text_tag_type
@ TTT_LINK
@ TTT_BOLD
@ TTT_ITALIC
@ TTT_STRIKE
@ TTT_COLOR
@ TTT_UNDERLINE
struct civ_game game
Definition game.c:62
struct world wld
Definition game.c:63
struct unit * game_unit_by_number(int id)
Definition game.c:116
struct city * game_city_by_number(int id)
Definition game.c:107
static bool is_plain_public_message(const char *s)
Definition chatline.c:97
void popup_races_dialog(struct player *pplayer)
Definition dialogs.c:1215
GType type
Definition repodlgs.c:1313
QString apply_tags(QString str, const struct text_tag_list *tags, QColor bg_color)
Definition chatline.cpp:555
#define set_output_window_text(_pstr_)
Definition chatline.h:31
static QApplication * qapp
Definition gui_main.cpp:71
QApplication * current_app()
Definition gui_main.cpp:229
#define FC_CPP_DECLARE_LISTENER(_type_)
Definition listener.h:134
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Definition map.c:456
void link_mark_restore(enum text_link_type type, int id)
void center_tile_mapcanvas(const struct tile *ptile)
void link_marks_clear_all(void)
static mpgui * gui
Definition mpgui_qt.cpp:52
const char *const chatline
Definition fonts.h:31
struct client_options gui_options
Definition options.c:71
char * lines
Definition packhand.c:131
#define players_iterate_end
Definition player.h:537
#define players_iterate(_pplayer)
Definition player.h:532
#define is_ai(plr)
Definition player.h:230
size_t size
Definition specvec.h:72
Definition city.h:320
struct conn_list * est_connections
Definition game.h:97
struct conn_list * all_connections
Definition game.h:96
struct connection conn
Definition client_main.h:96
bool gui_qt_allied_chat_only
Definition options.h:407
char gui_qt_wakeup_text[512]
Definition options.h:419
Definition colors.h:21
char username[MAX_LEN_NAME]
Definition connection.h:164
Definition tile.h:50
Definition unit.h:138
struct civ_map map
#define sz_strlcpy(dest, src)
Definition support.h:195
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define unit_tile(_pu)
Definition unit.h:397