Author: mir3x
Date: Thu Sep  1 18:30:29 2016
New Revision: 33733

URL: http://svn.gna.org/viewcvs/freeciv?rev=33733&view=rev
Log:
Qt client - Add listener for fonts
Patch by Louis Moureaux <louis94>

See patch #7659


Added:
    trunk/client/gui-qt/fonts.cpp
    trunk/client/gui-qt/fonts.h
Modified:
    trunk/client/gui-qt/Makefile.am
    trunk/client/gui-qt/gui_main.cpp

Modified: trunk/client/gui-qt/Makefile.am
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/client/gui-qt/Makefile.am?rev=33733&r1=33732&r2=33733&view=diff
==============================================================================
--- trunk/client/gui-qt/Makefile.am     (original)
+++ trunk/client/gui-qt/Makefile.am     Thu Sep  1 18:30:29 2016
@@ -31,6 +31,7 @@
        meta_optiondlg.cpp      \
        meta_citydlg.cpp        \
        meta_cityrep.cpp        \
+       meta_fonts.cpp          \
        meta_helpdlg.cpp        \
        meta_plrdlg.cpp         \
        meta_diplodlg.cpp       \
@@ -61,6 +62,8 @@
        diplodlg.h      \
        fc_client.cpp   \
        fc_client.h     \
+       fonts.cpp       \
+       fonts.h         \
        finddlg.cpp     \
        finddlg.h       \
        gotodlg.cpp     \

Added: trunk/client/gui-qt/fonts.cpp
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/client/gui-qt/fonts.cpp?rev=33733&view=auto
==============================================================================
--- trunk/client/gui-qt/fonts.cpp       (added)
+++ trunk/client/gui-qt/fonts.cpp       Thu Sep  1 18:30:29 2016
@@ -0,0 +1,123 @@
+/**********************************************************************
+ Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+***********************************************************************/
+
+#include "fonts.h"
+
+// client
+#include "options.h"
+
+// Qt
+#include <QWidget>
+
+FC_CPP_DECLARE_LISTENER(font_options_listener)
+
+QMap<QString, QFont> font_options_listener::font_map =
+    QMap<QString, QFont>();
+
+/***************************************************************************
+  Constructor
+***************************************************************************/
+font_options_listener::font_options_listener()
+{
+  if (font_map.empty()) {
+    init_font_map();
+  }
+}
+
+/***************************************************************************
+  Initializes the font map
+***************************************************************************/
+void font_options_listener::init_font_map()
+{
+  QFont font;
+  QString s;
+
+  options_iterate(client_optset, poption) {
+    if (option_type(poption) == OT_FONT) {
+      s = option_font_get(poption);
+      font.fromString(s);
+      s = option_name(poption);
+      font_map[s] = font;
+    }
+  } options_iterate_end;
+}
+
+/***************************************************************************
+  Called whenever a font changes. Default implementation does nothing.
+***************************************************************************/
+void font_options_listener::update_font(const QString &name,
+                                        const QFont &font)
+{}
+
+/***************************************************************************
+  Returns the font with the given name.
+***************************************************************************/
+QFont font_options_listener::get_font(const QString &name) const
+{
+  return font_map[name];
+}
+
+/***************************************************************************
+  Returns the appropriate QFont for the given client_font
+***************************************************************************/
+QFont font_options_listener::get_font(client_font font)
+{
+  if (font_map.empty()) {
+    init_font_map();
+  }
+  switch (font) {
+  case FONT_CITY_NAME:
+    return font_map[fonts::city_names];
+  case FONT_CITY_PROD:
+    return font_map[fonts::city_productions];
+  case FONT_REQTREE_TEXT:
+    return font_map[fonts::reqtree_text];
+  case FONT_COUNT:
+    break;
+    // Will trigger a warning if not all cases are covered
+  }
+  // Reasonable default
+  return font_map[fonts::default_font];
+}
+
+/***************************************************************************
+  Sets the font with the given name. The configuration is *not* updated.
+***************************************************************************/
+void font_options_listener::set_font(const QString &name,
+                                     const QFont &font)
+{
+  font_map[name] = font;
+  invoke(&font_options_listener::update_font, name, font);
+}
+
+/***************************************************************************
+  Constructor.
+***************************************************************************/
+font_updater::font_updater(QWidget *widget, const QString &font_name) :
+  QObject(widget),
+  font_name(font_name),
+  widget(widget)
+{
+  widget->setFont(get_font(font_name));
+  font_options_listener::listen();
+}
+
+/***************************************************************************
+  Updates the widget's font.
+***************************************************************************/
+void font_updater::update_font(const QString &name, const QFont &font)
+{
+  if (name == font_name) {
+    widget->setFont(font);
+  }
+}

Added: trunk/client/gui-qt/fonts.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/client/gui-qt/fonts.h?rev=33733&view=auto
==============================================================================
--- trunk/client/gui-qt/fonts.h (added)
+++ trunk/client/gui-qt/fonts.h Thu Sep  1 18:30:29 2016
@@ -0,0 +1,97 @@
+/**********************************************************************
+ Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+***********************************************************************/
+
+#ifndef FC__FONTS_H
+#define FC__FONTS_H
+
+// gui-qt
+#include "listener.h"
+
+// client
+#include "canvas_g.h" // client_font
+
+// Qt
+#include <QFont>
+#include <QMap>
+#include <QObject>
+
+class QWidget;
+
+/***************************************************************************
+  The list of font names supported by the font classes. Keep it in sync with
+  the options in client/options.c
+***************************************************************************/
+namespace fonts
+{
+  const char * const city_label       = "gui_qt_font_city_label";
+  const char * const default_font     = "gui_qt_font_default";
+  const char * const notify_label     = "gui_qt_font_notify_label";
+  const char * const spaceship_label  = "gui_qt_font_spaceship_label";
+  const char * const help_label       = "gui_qt_font_help_label";
+  const char * const help_link        = "gui_qt_font_help_link";
+  const char * const help_text        = "gui_qt_font_help_text";
+  const char * const help_title       = "gui_qt_font_help_title";
+  const char * const chatline         = "gui_qt_font_chatline";
+  const char * const beta_label       = "gui_qt_font_beta_label";
+  const char * const small            = "gui_qt_font_small";
+  const char * const comment_label    = "gui_qt_font_comment_label";
+  const char * const city_names       = "gui_qt_font_city_names";
+  const char * const city_productions = "gui_qt_font_city_productions";
+  const char * const reqtree_text     = "gui_qt_font_reqtree_text";
+}
+
+/***************************************************************************
+  Handles font configuration. Font names are those defined in the fonts
+  namespace.
+***************************************************************************/
+class font_options_listener : public listener<font_options_listener>
+{
+  static QMap<QString, QFont> font_map;
+  static void init_font_map();
+
+public:
+  static void set_font(const QString &name, const QFont &value);
+  static QFont get_font(client_font font);
+
+  explicit font_options_listener();
+
+  virtual void update_font(const QString &name, const QFont &font);
+
+  QFont get_font(const QString &name) const;
+};
+
+/***************************************************************************
+  Automatically updates the font of a widget whenever it changes. All you
+  need to do is creating a font_updater:
+
+  ~~~~~{.cpp}
+  new font_updater(the_widget, the_font_name);
+  ~~~~~
+
+  You don't need to care about deleting this object.
+***************************************************************************/
+class font_updater : public QObject, private font_options_listener
+{
+  Q_OBJECT
+
+  QString font_name;
+  QWidget *widget;
+
+public:
+  explicit font_updater(QWidget *widget, const QString &font_name);
+
+private:
+  virtual void update_font(const QString &name, const QFont &font);
+};
+
+#endif // FC__FONTS_H

Modified: trunk/client/gui-qt/gui_main.cpp
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/client/gui-qt/gui_main.cpp?rev=33733&r1=33732&r2=33733&view=diff
==============================================================================
--- trunk/client/gui-qt/gui_main.cpp    (original)
+++ trunk/client/gui-qt/gui_main.cpp    Thu Sep  1 18:30:29 2016
@@ -54,6 +54,7 @@
 
 // gui-qt
 #include "fc_client.h"
+#include "fonts.h"
 #include "gui_main.h"
 #include "helpdlg.h"
 #include "qtg_cxxside.h"
@@ -342,10 +343,15 @@
 ****************************************************************************/
 static void apply_font(struct option *poption)
 {
+  QFont font;
   QFont *f;
   QFont *remove_old;
   QString s;
 
+  s = option_font_get(poption);
+  font.fromString(s);
+  s = option_name(poption);
+  font_options_listener::set_font(s, font);
   if (gui()) {
     f = new QFont;
     s = option_font_get(poption);


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to