[Xfce4-commits] [panel-plugins/xfce4-hardware-monitor-plugin] 08/13: Configurable text overlay position

noreply at xfce.org noreply at xfce.org
Wed Aug 12 18:39:12 CEST 2015


This is an automated email from the git hooks/post-receive script.

omegaphil pushed a commit to branch master
in repository panel-plugins/xfce4-hardware-monitor-plugin.

commit 03509c10ac92448582651781f688762f94f3f839
Author: OmegaPhil <OmegaPhil at startmail.com>
Date:   Sat Aug 8 18:19:38 2015 +0100

    Configurable text overlay position
---
 src/applet.cpp             |   29 +++++++++++++++-
 src/applet.hpp             |    5 +++
 src/curve-view.cpp         |   81 +++++++++++++++++++++++++++++++++++++++++---
 src/curve-view.hpp         |   16 +++++++++
 src/preferences-window.cpp |   63 ++++++++++++++++++++++++++++++++++
 src/preferences-window.hpp |   15 +++++++-
 src/ui.glade               |   35 +++++++++++++++++++
 7 files changed, 238 insertions(+), 6 deletions(-)

diff --git a/src/applet.cpp b/src/applet.cpp
index 1b3d254..1bac45b 100644
--- a/src/applet.cpp
+++ b/src/applet.cpp
@@ -169,7 +169,8 @@ Applet::Applet(XfcePanelPlugin *plugin)
   viewer_text_overlay_format_string("%a %m"),
   viewer_text_overlay_separator(" "),
   viewer_text_overlay_font(""),
-  viewer_text_overlay_color(0x00000000)
+  viewer_text_overlay_color(0x00000000),
+  viewer_text_overlay_position(CurveView::bottom_left)
 {
   // Search for settings file
   XfceRc* settings_ro = NULL;
@@ -208,6 +209,12 @@ Applet::Applet(XfcePanelPlugin *plugin)
       "viewer_text_overlay_font", viewer_text_overlay_font.c_str());
     viewer_text_overlay_color = xfce_rc_read_int_entry(settings_ro,
       "viewer_text_overlay_color", viewer_text_overlay_color);
+
+    // Extra care needed for this since enums don't enforce a range...
+    int text_overlay_position = xfce_rc_read_int_entry(settings_ro,
+      "viewer_text_overlay_position", 0);
+    set_viewer_text_overlay_position(
+          static_cast<CurveView::TextOverlayPosition>(text_overlay_position));
   }
   
   // Loading icon
@@ -610,6 +617,26 @@ void Applet::set_viewer_text_overlay_color(const int color)
   viewer_text_overlay_color = color;
 }
 
+const CurveView::TextOverlayPosition Applet::get_viewer_text_overlay_position()
+{
+  return viewer_text_overlay_position;
+}
+
+void Applet::set_viewer_text_overlay_position(CurveView::TextOverlayPosition
+                                      position)
+{
+  // Validating input - an enum does not enforce a range!!
+  if (position < CurveView::top_left ||
+      position >= CurveView::NUM_TEXT_OVERLAY_POSITIONS)
+  {
+    std::cerr << "Applet::set_viewer_text_overlay_position was called with an "
+                 "invalid position: " << position << "!\n";
+    position = CurveView::top_left;
+  }
+
+  viewer_text_overlay_position = position;
+}
+
 void Applet::add_monitor(Monitor *monitor)
 {
   add_sync_for(monitor);
diff --git a/src/applet.hpp b/src/applet.hpp
index cc1e247..cf7607e 100644
--- a/src/applet.hpp
+++ b/src/applet.hpp
@@ -40,6 +40,7 @@ extern "C"
 }
 
 #include "monitor.hpp"
+#include "curve-view.hpp"
 
 class PreferencesWindow; 
 class View;
@@ -81,6 +82,9 @@ public:
   void set_viewer_text_overlay_font(const Glib::ustring font_details);
   const int get_viewer_text_overlay_color() const;
   void set_viewer_text_overlay_color(const int color);
+  const CurveView::TextOverlayPosition get_viewer_text_overlay_position();
+  void set_viewer_text_overlay_position(CurveView::TextOverlayPosition
+                                        position);
   void viewer_type_listener(const Glib::ustring viewer_type);
   void background_color_listener(unsigned int background_color);
   void use_background_color_listener(gboolean use_background_color);
@@ -119,6 +123,7 @@ private:
   Glib::ustring viewer_text_overlay_format_string, viewer_text_overlay_separator,
                 viewer_text_overlay_font;
   int viewer_text_overlay_color;
+  CurveView::TextOverlayPosition viewer_text_overlay_position;
 
   int viewer_size, background_color, next_color;
   gboolean use_background_color;
diff --git a/src/curve-view.cpp b/src/curve-view.cpp
index 16f4cf1..0f0ca5f 100644
--- a/src/curve-view.cpp
+++ b/src/curve-view.cpp
@@ -359,10 +359,6 @@ void CurveView::do_draw_loop()
       text_overlay = new Gnome::Canvas::Text(*canvas->root());
       text_overlay->property_anchor() = Gtk::ANCHOR_NW;
       text_overlay->property_text() = overlay_text;
-
-      // Positioning text at the bottom of the canvas
-      text_overlay->property_y() = applet->get_height() -
-          text_overlay->property_text_height();
     }
 
     // It is - updating if it has changed
@@ -381,5 +377,82 @@ void CurveView::do_draw_loop()
     int color = applet->get_viewer_text_overlay_color();
     if (text_overlay->property_fill_color_rgba() != color)
       text_overlay->property_fill_color_rgba() = color;
+
+    // Positioning text
+    int x, y;
+    text_overlay_calc_position(x, y, applet->get_viewer_text_overlay_position());
+    if (text_overlay->property_x() != x)
+      text_overlay->property_x() = x;
+    if (text_overlay->property_y() != y)
+      text_overlay->property_y() = y;
+  }
+}
+
+const Glib::ustring CurveView::text_overlay_position_to_string(
+      TextOverlayPosition position)
+{
+  switch(position)
+  {
+    case top_left:
+      return _("Top left");
+    case top_center:
+      return _("Top center");
+    case top_right:
+      return _("Top right");
+    case center:
+      return _("Center");
+    case bottom_left:
+      return _("Bottom left");
+    case bottom_center:
+      return _("Bottom center");
+    case bottom_right:
+      return _("Bottom right");
+    default:
+      return _("Top left");
   }
 }
+
+void CurveView::text_overlay_calc_position(int& x, int& y,
+                                           TextOverlayPosition position)
+{
+  switch(position)
+  {
+    case top_left:
+      x = y = 0;
+      break;
+
+    case top_center:
+      x = (applet->get_width() - text_overlay->property_text_width()) / 2;
+      y = 0;
+      break;
+
+    case top_right:
+      x = applet->get_width() - text_overlay->property_text_width();
+      y = 0;
+      break;
+
+    case center:
+      x = (applet->get_width() - text_overlay->property_text_width()) / 2;
+      y = (applet->get_height() - text_overlay->property_text_height()) / 2;
+      break;
+
+    case bottom_left:
+      x = 0;
+      y = applet->get_height() - text_overlay->property_text_height();
+      break;
+
+    case bottom_center:
+      x = (applet->get_width() - text_overlay->property_text_width()) / 2;
+      y = applet->get_height() - text_overlay->property_text_height();
+      break;
+
+    case bottom_right:
+      x = applet->get_width() - text_overlay->property_text_width();
+      y = applet->get_height() - text_overlay->property_text_height();
+      break;
+
+    default:
+      x = y = 0;
+      break;
+   }
+}
diff --git a/src/curve-view.hpp b/src/curve-view.hpp
index 54a91df..f73dd5d 100644
--- a/src/curve-view.hpp
+++ b/src/curve-view.hpp
@@ -43,12 +43,28 @@ public:
   
   static int const pixels_per_sample;
 
+  enum TextOverlayPosition {
+     top_left,
+     top_center,
+     top_right,
+     center,
+     bottom_left,
+     bottom_center,
+     bottom_right,
+     NUM_TEXT_OVERLAY_POSITIONS
+  };
+
+  static const Glib::ustring text_overlay_position_to_string(
+      TextOverlayPosition position);
+
 private:
   virtual void do_update();
   virtual void do_attach(Monitor *monitor);
   virtual void do_detach(Monitor *monitor);
   virtual void do_draw_loop();
 
+  void text_overlay_calc_position(int &x, int &y, TextOverlayPosition position);
+
   // Must be destroyed before the canvas
   typedef std::list<Curve *> curve_sequence;
   typedef curve_sequence::iterator curve_iterator;
diff --git a/src/preferences-window.cpp b/src/preferences-window.cpp
index 7c95a02..1975581 100644
--- a/src/preferences-window.cpp
+++ b/src/preferences-window.cpp
@@ -31,6 +31,7 @@
 #include "gui-helpers.hpp"
 #include "applet.hpp"
 #include "monitor.hpp"
+#include "curve-view.hpp"
 #include "i18n.hpp"
 
 
@@ -134,6 +135,12 @@ PreferencesWindow::PreferencesWindow(Applet &applet_, monitor_seq monitors)
       .connect(sigc::mem_fun(*this,
                &PreferencesWindow::on_text_overlay_colorbutton_set));
 
+  // I tried to get at a ComboboxText but libglade does not support it??
+  ui->get_widget("text_overlay_position_combobox",
+                 text_overlay_position_combobox);
+  text_overlay_position_combobox->signal_changed()
+      .connect(sigc::mem_fun(*this,
+               &PreferencesWindow::on_text_overlay_position_combobox_changed));
 
   ui->get_widget("background_colorbutton", background_colorbutton);
   background_colorbutton->signal_color_set()
@@ -148,6 +155,11 @@ PreferencesWindow::PreferencesWindow(Applet &applet_, monitor_seq monitors)
     .connect(sigc::mem_fun(*this,
           &PreferencesWindow::on_background_color_radiobutton_toggled));
 
+  // Initialising text overlay position combobox
+  static TextOverlayPositionColumns topc;
+  text_overlay_position_store = Gtk::ListStore::create(topc);
+  text_overlay_position_combobox->set_model(text_overlay_position_store);
+  text_overlay_position_combobox->pack_start(topc.position);
 
   // Connect the Monitor tab widgets
   Gtk::Button *add_button;
@@ -224,6 +236,21 @@ PreferencesWindow::PreferencesWindow(Applet &applet_, monitor_seq monitors)
    * between the greyed-out buttons and the treeview */
   monitor_treeview->get_selection()->unselect_all();
 
+  // Populating text overlay position combobox and selecting the correct position
+  CurveView::TextOverlayPosition current_pos, position =
+      applet.get_viewer_text_overlay_position();
+  store_iter i;
+  for (int r = 0; r < CurveView::NUM_TEXT_OVERLAY_POSITIONS; ++r)
+  {
+    current_pos = static_cast<CurveView::TextOverlayPosition>(r);
+
+    i = text_overlay_position_store->append();
+    (*i)[topc.position] = CurveView::text_overlay_position_to_string(current_pos);
+
+    if (position == current_pos)
+      text_overlay_position_combobox->set_active(r);
+  }
+
   // Make sure background colorbutton is grayed out
   background_color_radiobutton->toggled();
   
@@ -812,6 +839,7 @@ void PreferencesWindow::on_text_overlay_checkbutton_toggled()
   text_overlay_font_checkbutton->set_sensitive(active);
   text_overlay_fontbutton->set_sensitive(active);
   text_overlay_colorbutton->set_sensitive(active);
+  text_overlay_position_combobox->set_sensitive(active);
 
   save_text_overlay_enabled(active);
 }
@@ -866,6 +894,41 @@ void PreferencesWindow::on_text_overlay_colorbutton_set()
     get_colorbutton_int(text_overlay_colorbutton));
 }
 
+void PreferencesWindow::on_text_overlay_position_combobox_changed()
+{
+  int position = text_overlay_position_combobox->get_active_row_number();
+
+  applet.set_viewer_text_overlay_position(
+        static_cast<CurveView::TextOverlayPosition>(position));
+
+  // Search for a writeable settings file, create one if it doesnt exist */
+  gchar* file = xfce_panel_plugin_save_location(applet.panel_applet, true);
+
+  if (file)
+  {
+    // Opening setting file
+    XfceRc* settings_w = xfce_rc_simple_open(file, false);
+    g_free(file);
+
+    // Ensuring default group is in focus
+    xfce_rc_set_group(settings_w, NULL);
+
+    // Updating configuration
+    xfce_rc_write_int_entry(settings_w, "viewer_text_overlay_position",
+                            position);
+
+    // Close settings file
+    xfce_rc_close(settings_w);
+  }
+  else
+  {
+    // Unable to obtain writeable config file - informing user and exiting
+    std::cerr << _("Unable to obtain writeable config file path in order to"
+      " save viewer text overlay position in PreferencesWindow::"
+                   "on_text_overlay_position_combobox_changed!\n");
+  }
+}
+
 void PreferencesWindow::on_add_button_clicked()
 {
   Monitor *monitor = run_choose_monitor_window(Glib::ustring());
diff --git a/src/preferences-window.hpp b/src/preferences-window.hpp
index bf8133e..0586647 100644
--- a/src/preferences-window.hpp
+++ b/src/preferences-window.hpp
@@ -31,6 +31,7 @@
 #include <gtkmm/button.h>
 #include <gtkmm/checkbutton.h>
 #include <gtkmm/colorbutton.h>
+#include <gtkmm/combobox.h>
 #include <gtkmm/fontbutton.h>
 #include <gtkmm/label.h>
 #include <gtkmm/liststore.h>
@@ -71,6 +72,7 @@ private:
   Gtk::FontButton *fontbutton, *text_overlay_fontbutton;
   Gtk::Entry *text_overlay_format_string_entry, *text_overlay_separator_entry;
   Gtk::ColorButton *text_overlay_colorbutton;
+  Gtk::ComboBox *text_overlay_position_combobox;
 
   Gtk::Button *remove_button, *change_button;
   Gtk::TreeView *monitor_treeview;
@@ -92,7 +94,17 @@ private:
   
   Glib::RefPtr<Gtk::ListStore> monitor_store;
   typedef Gtk::ListStore::iterator store_iter;
-  
+
+  class TextOverlayPositionColumns: public Gtk::TreeModel::ColumnRecord
+  {
+  public:
+    Gtk::TreeModelColumn<Glib::ustring> position;
+
+    TextOverlayPositionColumns() { add(position); }
+  };
+
+  Glib::RefPtr<Gtk::ListStore> text_overlay_position_store;
+
   // Originally gconf callbacks
   void viewer_type_listener(const Glib::ustring viewer_type, bool enable);
   void background_color_listener(unsigned int background_color);
@@ -129,6 +141,7 @@ private:
   void on_text_overlay_font_checkbutton_toggled();
   void on_text_overlay_fontbutton_set();
   void on_text_overlay_colorbutton_set();
+  void on_text_overlay_position_combobox_changed();
 
   void on_add_button_clicked();
   void on_remove_button_clicked();
diff --git a/src/ui.glade b/src/ui.glade
index 079cd56..630aba7 100644
--- a/src/ui.glade
+++ b/src/ui.glade
@@ -2403,6 +2403,41 @@ individual monitor values</property>
                         <property name="position">5</property>
                       </packing>
                     </child>
+                    <child>
+                      <widget class="GtkHBox" id="text_overlay_position_hbox">
+                        <property name="visible">True</property>
+                        <property name="can_focus">False</property>
+                        <child>
+                          <widget class="GtkLabel" id="text_overlay_position_label">
+                            <property name="visible">True</property>
+                            <property name="can_focus">False</property>
+                            <property name="label" translatable="yes">Text position:</property>
+                          </widget>
+                          <packing>
+                            <property name="expand">False</property>
+                            <property name="fill">True</property>
+                            <property name="position">0</property>
+                          </packing>
+                        </child>
+                        <child>
+                          <widget class="GtkComboBox" id="text_overlay_position_combobox">
+                            <property name="visible">True</property>
+                            <property name="can_focus">False</property>
+                          </widget>
+                          <packing>
+                            <property name="expand">False</property>
+                            <property name="fill">True</property>
+                            <property name="padding">10</property>
+                            <property name="position">1</property>
+                          </packing>
+                        </child>
+                      </widget>
+                      <packing>
+                        <property name="expand">True</property>
+                        <property name="fill">True</property>
+                        <property name="position">6</property>
+                      </packing>
+                    </child>
                   </widget>
                   <packing>
                     <property name="expand">True</property>

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list