Author: mir3x Date: Wed Aug 24 15:31:03 2016 New Revision: 33698 URL: http://svn.gna.org/viewcvs/freeciv?rev=33698&view=rev Log: Qt client - use helper<> to propagate output_window_append Patch by Louis Moureaux <louis94>
See patch #7491 Modified: branches/S2_6/client/gui-qt/chatline.cpp branches/S2_6/client/gui-qt/chatline.h branches/S2_6/client/gui-qt/fc_client.cpp branches/S2_6/client/gui-qt/fc_client.h branches/S2_6/client/gui-qt/listener.h branches/S2_6/client/gui-qt/pages.cpp Modified: branches/S2_6/client/gui-qt/chatline.cpp URL: http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/chatline.cpp?rev=33698&r1=33697&r2=33698&view=diff ============================================================================== --- branches/S2_6/client/gui-qt/chatline.cpp (original) +++ branches/S2_6/client/gui-qt/chatline.cpp Wed Aug 24 15:31:03 2016 @@ -40,6 +40,83 @@ #include "chatline.h" static bool is_plain_public_message(QString s); + +FC_CPP_DECLARE_LISTENER(chat_listener) + +/*************************************************************************** + Called whenever a message is received. Default implementation does + nothing. +***************************************************************************/ +void chat_listener::chat_message_received(const QString &, + const struct text_tag_list *) +{} + +/*************************************************************************** + Sends commands to server, but first searches for cutom keys, if it finds + then it makes custom action +***************************************************************************/ +void chat_listener::send_chat_message(const QString &message) +{ + int index; + QString splayer, s; + + /* FIXME + * Key == PICK: used for picking nation, it was put here cause those + * Qt slots are a bit limited ...I'm unable to pass custom player pointer + * or idk how to do that + */ + s = message; + index = message.indexOf("PICK:"); + + if (index != -1) { + s = s.remove("PICK:"); + /* now should be playername left in string */ + players_iterate(pplayer) { + splayer = QString(pplayer->name); + + if (!splayer.compare(s)) { + popup_races_dialog(pplayer); + } + } players_iterate_end; + return; + } + + /* + * If client send commands to take ai, set /away to disable AI + */ + if (message.startsWith("/take ")) { + s = s.remove("/take "); + players_iterate(pplayer) { + splayer = QString(pplayer->name); + splayer = "\"" + splayer + "\""; + + if (!splayer.compare(s)) { + if (pplayer->ai_controlled) { + send_chat(message.toLocal8Bit()); + send_chat("/away"); + return; + } + } + } players_iterate_end; + } + + /* + * Option to send to allies by default + */ + gui()->chat_history.prepend(message); + if (!message.isEmpty()) { + if (client_state() >= C_S_RUNNING && gui_options.gui_qt_allied_chat_only + && is_plain_public_message(message)) { + send_chat((QString(CHAT_ALLIES_PREFIX) + + " " + message).toLocal8Bit()); + } else { + send_chat(message.toLocal8Bit()); + } + } + // Empty messages aren't sent + // FIXME Inconsistent behavior: "." will send an empty message to allies +} + /*************************************************************************** Constructor for chatwdg ***************************************************************************/ @@ -94,6 +171,8 @@ connect(remove_links, SIGNAL(clicked()), this, SLOT(rm_links())); connect(cb, SIGNAL(stateChanged(int)), this, SLOT(state_changed(int))); setMouseTracking(true); + + chat_listener::listen(); } /*************************************************************************** @@ -178,11 +257,19 @@ } } +/*************************************************************************** + Adds news string to chatwdg (from chat_listener interface) +***************************************************************************/ +void chatwdg::chat_message_received(const QString& message, + const struct text_tag_list *tags) +{ + append(apply_tags(message, tags, true)); +} /*************************************************************************** Adds news string to chatwdg ***************************************************************************/ -void chatwdg::append(QString str) +void chatwdg::append(const QString &str) { QTextCursor cursor; @@ -197,17 +284,7 @@ ***************************************************************************/ void chatwdg::send() { - gui()->chat_history.prepend(chat_line->text()); - if (chat_line->text() != "") { - if (client_state() >= C_S_RUNNING && gui_options.gui_qt_allied_chat_only - && is_plain_public_message(chat_line->text())) { - send_chat(QString("%1 %2") - .arg(CHAT_ALLIES_PREFIX) - .arg(chat_line->text().toUtf8().data()).toUtf8().data()); - } else { - send_chat(chat_line->text().toUtf8().data()); - } - } + send_chat_message(chat_line->text()); chat_line->clear(); } @@ -530,10 +607,9 @@ && !wakeup.isEmpty()) { audio_play_sound("e_player_wake", NULL); } - gui()->append_output_window(apply_tags(str, tags, false)); - if (gui()->infotab != NULL) { - gui()->infotab->chtwdg->append(apply_tags(str, tags, true)); - } + + chat_listener::invoke(&chat_listener::chat_message_received, + str, tags); } /************************************************************************** Modified: branches/S2_6/client/gui-qt/chatline.h URL: http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/chatline.h?rev=33698&r1=33697&r2=33698&view=diff ============================================================================== --- branches/S2_6/client/gui-qt/chatline.h (original) +++ branches/S2_6/client/gui-qt/chatline.h Wed Aug 24 15:31:03 2016 @@ -22,6 +22,8 @@ #include "chatline_g.h" } +#include "listener.h" + //Qt #include <QEvent> #include <QTextBrowser> @@ -32,15 +34,28 @@ QString apply_tags(QString str, const struct text_tag_list *tags, bool colors_change); + +/*************************************************************************** + Listener for chat. See listener<> for information about how to use it +***************************************************************************/ +class chat_listener : public listener<chat_listener> +{ +public: + virtual void chat_message_received(const QString &, + const struct text_tag_list *); + + void send_chat_message(const QString &message); +}; + /*************************************************************************** Class for chat widget ***************************************************************************/ -class chatwdg : public QWidget +class chatwdg : public QWidget, private chat_listener { Q_OBJECT public: chatwdg(QWidget *parent); - void append(QString str); + void append(const QString &str); QLineEdit *chat_line; void make_link(struct tile *ptile); void update_font(); @@ -57,6 +72,9 @@ void paintEvent(QPaintEvent *event); bool eventFilter(QObject *obj, QEvent *event); private: + void chat_message_received(const QString &message, + const struct text_tag_list *tags); + QTextBrowser *chat_output; QPushButton *remove_links; QCheckBox *cb; Modified: branches/S2_6/client/gui-qt/fc_client.cpp URL: http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/fc_client.cpp?rev=33698&r1=33697&r2=33698&view=diff ============================================================================== --- branches/S2_6/client/gui-qt/fc_client.cpp (original) +++ branches/S2_6/client/gui-qt/fc_client.cpp Wed Aug 24 15:31:03 2016 @@ -178,6 +178,7 @@ this, SLOT(switch_page(int))); setVisible(true); + chat_listener::listen(); } /**************************************************************************** @@ -227,13 +228,14 @@ quitting = true; } - /**************************************************************************** Appends text to chat window ****************************************************************************/ -void fc_client::append_output_window(const QString &str) +void fc_client::chat_message_received(const QString &message, + const struct text_tag_list *tags) { QTextCursor cursor; + QString str = apply_tags(message, tags, false); if (output_window != NULL) { output_window->append(str); @@ -446,8 +448,7 @@ ****************************************************************************/ void fc_client::chat() { - send_chat(chat_line->text().toUtf8().data()); - chat_history.prepend(chat_line->text()); + send_chat_message(chat_line->text()); chat_line->clear(); history_pos = -1; } Modified: branches/S2_6/client/gui-qt/fc_client.h URL: http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/fc_client.h?rev=33698&r1=33697&r2=33698&view=diff ============================================================================== --- branches/S2_6/client/gui-qt/fc_client.h (original) +++ branches/S2_6/client/gui-qt/fc_client.h Wed Aug 24 15:31:03 2016 @@ -149,7 +149,7 @@ }; -class fc_client : public QMainWindow +class fc_client : public QMainWindow, private chat_listener { Q_OBJECT QWidget *main_wdg; @@ -216,7 +216,6 @@ enum client_pages current_page(); - void append_output_window(const QString &); void set_status_bar(QString str, int timeout = 2000); int add_game_tab(QWidget *widget, QString title); void rm_game_tab(int index); /* doesn't delete widget */ @@ -276,7 +275,6 @@ bool slot_close_widget(int index); void start_page_menu(QPoint); void slot_pick_nation(); - void send_command_to_server(const QString &); void start_new_game(); void start_scenario(); void start_from_save(); @@ -296,6 +294,8 @@ void slot_selection_changed(const QItemSelection&, const QItemSelection&); private: + void chat_message_received(const QString &message, + const struct text_tag_list *tags); void create_main_page(); void create_network_page(); Modified: branches/S2_6/client/gui-qt/listener.h URL: http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/listener.h?rev=33698&r1=33697&r2=33698&view=diff ============================================================================== --- branches/S2_6/client/gui-qt/listener.h (original) +++ branches/S2_6/client/gui-qt/listener.h Wed Aug 24 15:31:03 2016 @@ -98,7 +98,7 @@ function invocation. Compilers should be able to inline calls to invoke(), leaving only the overhead of looping on all instances. - FIXME Implementation is not thread-safe. I don't know if it's needed. + @warning Implementation is not thread-safe. ***************************************************************************/ template<class _type_> class listener @@ -123,6 +123,9 @@ template<class _member_fct_, class _arg1_t_> static void invoke(_member_fct_ function, _arg1_t_ arg); + + template<class _member_fct_, class _arg1_t_, class _arg2_t_> + static void invoke(_member_fct_ function, _arg1_t_ arg1, _arg2_t_ arg2); }; /*************************************************************************** @@ -198,4 +201,26 @@ } } +/*************************************************************************** + Invokes a member function on all instances of an listener type. Template + parameters are meant to be automatically deduced. + + Two-parameters overload. + + @param function The member function to call + @param arg1 The first argument to pass to the function + @param arg2 The second argument to pass to the function +***************************************************************************/ +template<class _type_> +template<class _member_fct_, class _arg1_t_, class _arg2_t_> +void listener<_type_>::invoke(_member_fct_ function, + _arg1_t_ arg1, _arg2_t_ arg2) +{ + typename std::set<type_t *>::iterator it = instances.begin(); + typename std::set<type_t *>::iterator end = instances.end(); + for ( ; it != end; ++it) { + ((*it)->*function)(arg1, arg2); + } +} + #endif // FC__LISTENER_H Modified: branches/S2_6/client/gui-qt/pages.cpp URL: http://svn.gna.org/viewcvs/freeciv/branches/S2_6/client/gui-qt/pages.cpp?rev=33698&r1=33697&r2=33698&view=diff ============================================================================== --- branches/S2_6/client/gui-qt/pages.cpp (original) +++ branches/S2_6/client/gui-qt/pages.cpp Wed Aug 24 15:31:03 2016 @@ -1881,7 +1881,7 @@ menu.addAction(action); } connect(player_menu_mapper, SIGNAL(mapped(const QString &)), - this, SLOT(send_command_to_server(const QString &))); + this, SLOT(send_chat_message(const QString &))); menu.exec(global_pos); return; } @@ -1896,56 +1896,3 @@ { popup_races_dialog(client_player()); } - -/*************************************************************************** - Sends commands to server, but first searches for cutom keys, if it finds - then it makes custom action -***************************************************************************/ -void fc_client::send_command_to_server(const QString &str) -{ - int index; - QString splayer, s; - - /** Key == PICK: used for picking nation, it was put here cause those - * Qt slots are a bit limited ...I'm unable to pass custom player pointer - * or idk how to do that - */ - s = str; - index = str.indexOf("PICK:"); - - if (index != -1) { - s = s.remove("PICK:"); - /* now should be playername left in string */ - players_iterate(pplayer) { - splayer = QString(pplayer->name); - - if (!splayer.compare(s)) { - popup_races_dialog(pplayer); - } - } players_iterate_end; - return; - } - - /** - * If client send commands to take ai, set /away to disable AI - */ - - index = str.indexOf("/take "); - if (index != -1) { - s = s.remove("/take "); - players_iterate(pplayer) { - splayer = QString(pplayer->name); - splayer = "\"" + splayer + "\""; - - if (!splayer.compare(s)) { - if (pplayer->ai_controlled) { - send_chat(str.toLocal8Bit().data()); - send_chat("/away"); - return; - } - } - } players_iterate_end; - } - - send_chat(str.toLocal8Bit().data()); -} _______________________________________________ Freeciv-commits mailing list Freeciv-commits@gna.org https://mail.gna.org/listinfo/freeciv-commits