Explorar el Código

First final C++.

Cixo Develop hace 5 meses
padre
commit
27ada33771

+ 111 - 0
cppGTK/source/color.cpp

@@ -0,0 +1,111 @@
+#include <stdint.h>
+#include <cmath>
+#include <iostream>
+#include <string>
+#include "color.hpp"
+
+namespace cx_light {
+
+color::color() {
+    this->red = 0;
+    this->green = 0;
+    this->blue = 0;
+}
+
+color::~color() {}
+
+color color::from_hex(long hex) {
+    color target;
+
+    target.red = ((hex & 0xFF0000) >> 16);
+    target.green = ((hex & 0x00FF00) >> 8);
+    target.blue = ((hex & 0x0000FF) >> 0);
+
+    return target;
+}
+
+color color::from_rgb(uint8_t red, uint8_t green, uint8_t blue) {
+    color target;
+     
+    target.red = red;
+    target.green = green;
+    target.blue = blue;
+
+    return target;
+}
+
+color color::from_white(long temperature) {
+    color target;
+
+    temperature = temperature / 100;
+
+    if (temperature <= 66) {
+        target.red = 255;
+        target.blue = 0;
+
+        float green = 99.4708025861 * log(temperature) - 161.1195681661;
+        
+        green = green > 255 ? 255 : green;
+        green = green < 0 ? 0 : green;
+        target.green = green;
+
+        if (temperature > 19) {
+            float blue = temperature - 10;
+            blue = 138.5177312231 * log(blue) - 305.0447927307;
+        
+            blue = blue > 255 ? 255 : blue;
+            blue = blue < 0 ? 0 : blue;
+            target.blue = blue;
+        }
+        
+        return target;
+    }
+
+    float red = 329.698727446 * pow(temperature - 60, -0.1332047592);
+    red = red > 255 ? 255 : red;
+    red = red < 0 ? 0 : red;
+    target.red = red;
+
+    float green = 288.1221695283 * pow(temperature - 60, -0.0755148492 );
+    green = green > 255 ? 255 : green;
+    green = green < 0 ? 0 : green;
+    target.green = green;
+
+    target.blue = 255;
+
+    return target;
+}
+
+void color::set_source(const Cairo::RefPtr<Cairo::Context> &context) {
+    context->set_source_rgb(
+        this->get_normalized_red(),
+        this->get_normalized_green(),
+        this->get_normalized_blue()
+    );
+}
+
+uint8_t color::get_red() {
+    return this->red;
+}
+
+uint8_t color::get_green() {
+    return this->green;
+}
+
+uint8_t color::get_blue() {
+    return this->blue;
+}         
+
+double color::get_normalized_red() {
+    return ((double) this->red / 255);
+}
+
+double color::get_normalized_green() {
+    return ((double) this->green / 255);
+}
+
+double color::get_normalized_blue() {
+    return ((double) this->blue / 255);
+}
+
+}   

+ 36 - 0
cppGTK/source/color.hpp

@@ -0,0 +1,36 @@
+#ifndef COLOR_HPP_INCLUDED
+#define COLOR_HPP_INCLUDED
+
+#include <stdint.h>
+#include <cairomm/context.h>
+
+namespace cx_light {
+
+class color {
+    public:
+        color();
+        ~color();
+        
+        static color from_hex(long);
+        static color from_rgb(uint8_t, uint8_t, uint8_t);
+        static color from_white(long);
+        
+        uint8_t get_red();
+        uint8_t get_green();
+        uint8_t get_blue();
+
+        double get_normalized_red();
+        double get_normalized_green();
+        double get_normalized_blue();
+    
+        void set_source(const Cairo::RefPtr<Cairo::Context> &);
+
+    private:
+        uint8_t red;
+        uint8_t green;
+        uint8_t blue;
+};
+
+}
+
+#endif

+ 115 - 0
cppGTK/source/control.cpp

@@ -0,0 +1,115 @@
+#include <string>
+#include <gtkmm/grid.h>
+#include <gtkmm/entry.h>
+#include <gtkmm/label.h>
+#include <gtkmm/scale.h>
+#include <gtkmm/button.h>
+#include <gtkmm/orientable.h>
+#include <gtkmm/adjustment.h>
+
+#include "control.hpp"
+#include "config.hpp"
+#include "color.hpp"
+
+namespace cx_light {
+
+control::control(light *target) {
+    this->title = target->get_name();
+    this->address = target->get_address();
+
+    this->set_row_spacing(DEFAULT_MARGIN);
+    this->set_column_spacing(DEFAULT_MARGIN);
+
+    std::string description_text = "<b>Manage ";
+    description_text += this->title;
+    description_text += ".</b>";
+
+    this->description = new Gtk::Label(description_text);
+    this->description->set_use_markup();
+    this->description->set_hexpand();
+    this->description->set_halign(Gtk::Align::START);
+    this->attach(*this->description, 0, 0, 4, 1);
+    
+    this->power_description = new Gtk::Label("Brightness");
+    this->power_description->set_halign(Gtk::Align::START);
+    this->attach(*this->power_description, 0, 1, 1, 1);
+
+    this->power_value = new Glib::RefPtr<Gtk::Adjustment> (
+        Gtk::Adjustment::create(512, 0, 1024, 1)
+    );
+
+    this->power = new Gtk::Scale(
+        *this->power_value, 
+        Gtk::Orientation::HORIZONTAL
+    );
+
+    this->attach(*this->power, 1, 1, 2, 1);
+
+    this->color_description = new Gtk::Label("Color");
+    this->color_description->set_halign(Gtk::Align::START);
+    this->attach(*this->color_description, 0, 2, 1, 1);
+    
+    this->color_value = new Glib::RefPtr<Gtk::Adjustment> (
+        Gtk::Adjustment::create(6500, 1000, 20000, 1)
+    );
+
+    (*this->color_value)->signal_value_changed().connect([&] () {
+        this->drawing->queue_draw();
+    });
+
+    this->color = new Gtk::Scale(
+        *this->color_value,
+        Gtk::Orientation::HORIZONTAL
+    );
+    
+    this->attach(*this->color, 1, 2, 2, 1);
+
+    this->drawing = new Gtk::DrawingArea();
+    this->drawing->set_draw_func(sigc::mem_fun(*this, &control::redraw));
+    this->attach(*this->drawing, 3, 1, 1, 2);
+
+    this->apply = new Gtk::Button("Apply");
+    this->attach(*this->apply, 2, 3, 2, 1);
+
+    this->back = new Gtk::Button("Back");;
+    this->attach(*this->back, 0, 3, 2, 1);
+}
+
+control::~control() {
+    this->remove(*this->description);
+    this->remove(*this->power);
+    this->remove(*this->power_description);
+    this->remove(*this->color);
+    this->remove(*this->color_description);
+    this->remove(*this->apply);
+    this->remove(*this->back);
+    this->remove(*this->drawing);
+
+    delete this->drawing;
+    delete this->description;
+    delete this->power;
+    delete this->power_value;
+    delete this->power_description;
+    delete this->color;
+    delete this->color_value;
+    delete this->color_description;
+    delete this->apply;
+    delete this->back;
+}
+
+void control::redraw(
+    const Cairo::RefPtr<Cairo::Context> &context,
+    int width,
+    int height
+) {
+    context->set_line_width(10.0);
+  
+    color
+    ::from_white(((*this->color_value)->get_value()))
+    .set_source(context);
+  
+    context->arc(width / 2, height / 2, width / 4, 0.0, 2 * M_PI);
+    context->stroke();
+}
+
+}

+ 47 - 0
cppGTK/source/control.hpp

@@ -0,0 +1,47 @@
+#ifndef SOURCE_CONTROL_HPP_INCLUDED
+#define SOURCE_CONTROL_HPP_INCLUDED
+
+#include <string>
+#include <gtkmm/grid.h>
+#include <gtkmm/entry.h>
+#include <gtkmm/label.h>
+#include <gtkmm/scale.h>
+#include <gtkmm/button.h>
+#include <gtkmm/drawingarea.h>
+#include <gtkmm/adjustment.h>
+#include <cairomm/context.h>
+
+#include "light.hpp"
+
+namespace cx_light {
+
+class control: public Gtk::Grid {
+    public:
+        control(light*);
+        ~control() override;
+
+        void redraw(
+            const Cairo::RefPtr<Cairo::Context> &context, 
+            int width, 
+            int height
+        );
+        
+    private:
+        std::string title;
+        std::string address;
+
+        Gtk::Label *description;
+        Gtk::Scale *power;
+        Gtk::Label *power_description;
+        Gtk::Scale *color; 
+        Gtk::Label *color_description;
+        Gtk::DrawingArea *drawing;
+        Gtk::Button *apply;
+        Gtk::Button *back;
+        Glib::RefPtr<Gtk::Adjustment> *color_value;
+        Glib::RefPtr<Gtk::Adjustment> *power_value;
+};
+
+}
+
+#endif

+ 24 - 9
cppGTK/source/core_window.cpp

@@ -12,14 +12,17 @@
 #include "core_window.hpp"
 #include "lights_list.hpp"
 #include "light_adder.hpp"
+#include "welcome.hpp"
+#include "control.hpp"
+#include "editor.hpp"
 
 namespace cx_light {
 
 core_window::core_window() {
     this->current_visible = NULL;
-
+    
     this->set_title(__(WINDOW_TITLE));
-    this->set_default_size(400, 200);
+    this->set_default_size(600, 200);
 
     this->init_default();
 }
@@ -28,7 +31,11 @@ core_window::~core_window() {
     delete this->center;
     delete this->current;
     delete this->lights;
-    delete this->adder;
+    delete this->current_frame;
+
+    if (this->current_visible != NULL) {
+        delete this->current_visible;
+    }
 }
 
 void core_window::init_default() {
@@ -46,22 +53,30 @@ void core_window::init_default() {
     this->create_lights_list();
     this->center->append(*this->current_frame);
 
-    this->create_light_adder();
+    this->set_view(new welcome());
 }
 
 void core_window::create_lights_list() {
     this->lights = new lights_list(); 
     this->center->append(*this->lights);
-}
-
-void core_window::create_light_adder() {
-    this->adder = new light_adder();
-    this->set_view(this->adder);
+    
+    this->lights->set_add_light_callback([&] () {
+        this->set_view(new light_adder());
+    });
+
+    this->lights->set_edit_light_callback([&] (light *target) {
+        this->set_view(new editor(target));
+    });
+
+    this->lights->set_show_light_callback([&] (light *target) {
+        this->set_view(new control(target));
+    });
 }
 
 void core_window::set_view(Gtk::Widget *target) {
     if (this->current_visible != NULL) {
         this->current->remove(*this->current_visible);
+        delete this->current_visible;
     }
 
     this->current_visible = target;

+ 2 - 2
cppGTK/source/core_window.hpp

@@ -11,6 +11,7 @@
 #include "strings.hpp"
 #include "lights_list.hpp"
 #include "light_adder.hpp"
+#include "welcome.hpp"
 
 namespace cx_light {
 
@@ -23,7 +24,7 @@ class core_window: public Gtk::Window {
 
     private:
         lights_list *lights;
-        light_adder *adder;
+
         Gtk::Box *center;
         Gtk::Frame *current_frame;
         Gtk::Widget *current_visible;
@@ -31,7 +32,6 @@ class core_window: public Gtk::Window {
 
         void init_default();
         void create_lights_list();
-        void create_light_adder();
 };
 
 }

+ 89 - 0
cppGTK/source/editor.cpp

@@ -0,0 +1,89 @@
+#include <string>
+#include <gtkmm/grid.h>
+#include <gtkmm/entry.h>
+#include <gtkmm/label.h>
+#include <gtkmm/button.h>
+
+#include "editor.hpp"
+#include "light.hpp"
+#include "config.hpp"
+
+namespace cx_light {
+
+editor::editor(light *target) {
+    this->target = target->copy();
+
+    this->set_row_spacing(DEFAULT_MARGIN);
+    this->set_column_spacing(DEFAULT_MARGIN);
+
+    std::string description_text = "<b>Edit \"";
+    description_text += target->get_name();
+    description_text += "\" light.</b>";
+
+    this->description = new Gtk::Label(description_text);
+    this->description->set_use_markup();
+    this->description->set_hexpand();
+    this->description->set_halign(Gtk::Align::START);
+    this->attach(*this->description, 0, 0, 4, 1);
+
+    this->title_label = new Gtk::Label("Title");
+    this->title_label->set_halign(Gtk::Align::START);
+    this->attach(*this->title_label, 0, 1, 1, 1);
+
+    this->title = new Gtk::Entry();
+    this->attach(*this->title, 1, 1, 3, 1);
+    
+    this->address_label = new Gtk::Label("Address");
+    this->address_label->set_halign(Gtk::Align::START);
+    this->attach(*this->address_label, 0, 2, 1, 1);
+    
+    this->address = new Gtk::Entry();
+    this->attach(*this->address, 1, 2, 3, 1);
+
+    this->back = new Gtk::Button("Back");
+    this->attach(*this->back, 0, 3, 2, 1);
+
+    this->save = new Gtk::Button("Save");
+    this->attach(*this->save, 2, 3, 2, 1);
+}
+
+editor::~editor() { 
+    this->remove(*this->save);
+    this->remove(*this->back);
+    this->remove(*this->title);
+    this->remove(*this->address);
+    this->remove(*this->description);
+    this->remove(*this->title_label);
+    this->remove(*this->address_label);
+
+    delete this->target;
+    delete this->save;
+    delete this->back;
+    delete this->title;
+    delete this->address;
+    delete this->description;
+    delete this->title_label;
+    delete this->address_label;
+}
+
+std::string editor::get_title() {
+    std::string current = this->title->get_buffer()->get_text();
+
+    if (current.empty()) {
+        return this->target->get_name();
+    }
+
+    return current;
+}
+
+std::string editor::get_address() {
+    std::string current = this->address->get_buffer()->get_text();
+
+    if (current.empty()) {
+        return this->target->get_address();
+    }
+
+    return current;
+}
+
+}

+ 38 - 0
cppGTK/source/editor.hpp

@@ -0,0 +1,38 @@
+#ifndef SOURCE_EDITOR_HPP_INCLUDED
+#define SOURCE_EDITOR_HPP_INCLUDED
+
+#include <string>
+#include <gtkmm/grid.h>
+#include <gtkmm/entry.h>
+#include <gtkmm/label.h>
+#include <gtkmm/button.h>
+
+#include "light.hpp"
+
+namespace cx_light {
+
+class editor: public Gtk::Grid {
+    public:
+        editor(light*);
+        ~editor() override;
+        
+        std::string get_title();
+        std::string get_address();
+    
+    private:
+        light *target;
+
+        Gtk::Entry *title;
+        Gtk::Entry *address;
+       
+        Gtk::Label *description;
+        Gtk::Label *title_label;
+        Gtk::Label *address_label;
+
+        Gtk::Button *save;
+        Gtk::Button *back;
+};
+
+}
+
+#endif

+ 22 - 1
cppGTK/source/light_on_list.cpp

@@ -1,9 +1,11 @@
+#include <string>
+#include <functional>
+
 #include <gtkmm/box.h>
 #include <gtkmm/label.h>
 #include <gtkmm/frame.h>
 #include <gtkmm/button.h>
 #include <gtkmm/orientable.h>
-#include <string>
 
 #include "light_on_list.hpp"
 
@@ -11,11 +13,14 @@ namespace cx_light {
 
 light_on_list::light_on_list(light target) {
     this->target = target.copy();
+    this->show_callback = [] (light *target) {};
+    this->edit_callback = [] (light *target) {};
 
     this->center = new Gtk::Box();
     this->center->set_margin(5);
     this->center->set_spacing(10);
     this->center->set_orientation(Gtk::Orientation::HORIZONTAL);
+    this->center->set_homogeneous();
     
     this->set_margin_bottom(5);
     this->set_child(*this->center);
@@ -26,8 +31,16 @@ light_on_list::light_on_list(light target) {
     this->show = new Gtk::Button("Show");
     this->center->append(*this->show);
 
+    this->show->signal_clicked().connect([&] () {
+        this->show_callback(this->target);
+    });
+
     this->edit = new Gtk::Button("Edit");
     this->center->append(*this->edit);
+
+    this->edit->signal_clicked().connect([&] () {
+        this->edit_callback(this->target);
+    });
 }
 
 light_on_list::~light_on_list() {
@@ -46,4 +59,12 @@ int light_on_list::height() {
     return 40;
 }
 
+void light_on_list::set_edit_callback(std::function<void(light *)> target) {
+    this->edit_callback = target;
+}
+
+void light_on_list::set_show_callback(std::function<void(light *)> target) {
+    this->show_callback = target;
+}   
+
 }

+ 7 - 0
cppGTK/source/light_on_list.hpp

@@ -7,6 +7,7 @@
 #include <gtkmm/button.h>
 #include <gtkmm/orientable.h>
 #include <string>
+#include <functional>
 
 #include "light.hpp"
 
@@ -19,13 +20,19 @@ class light_on_list: public Gtk::Frame {
          
         static int width();
         static int height();
+        void set_show_callback(std::function<void(light *)>);
+        void set_edit_callback(std::function<void(light *)>);
 
     private:
         light *target;
+        
         Gtk::Box *center;
         Gtk::Label *title;
         Gtk::Button *show;
         Gtk::Button *edit;
+
+        std::function<void(light *)> show_callback;
+        std::function<void(light *)> edit_callback;
 };
 
 }

+ 35 - 1
cppGTK/source/lights_list.cpp

@@ -1,4 +1,6 @@
 #include <vector>
+#include <functional>
+
 #include <gtkmm/box.h>
 #include <gtkmm/button.h>
 #include <gtkmm/orientable.h>
@@ -12,9 +14,13 @@
 namespace cx_light {
 
 lights_list::lights_list() {
+    this->add_light_callback = [] () {};
+    this->show_light_callback = [] (light *target) {};
+    this->edit_light_callback = [] (light *target) {};
+
     this->lights = new std::vector<light_on_list *>;
     this->lights->clear();
-   
+
     this->content = new Gtk::Box();
     this->content->set_size_request(light_on_list::width(), -1);
     this->content->set_orientation(Gtk::Orientation::VERTICAL);
@@ -53,13 +59,41 @@ void lights_list::add_lights_container() {
 void lights_list::add_create_light_button() {
     this->create_light = new Gtk::Button(__(ADD_LIGHT));
     this->content->append(*this->create_light);
+    
+    this->create_light->signal_clicked().connect([&] () {
+        this->add_light_callback();
+    });
 }
 
 void lights_list::add_light(light target) {
     light_on_list *current = new light_on_list(target);
     
+    current->set_edit_callback([&] (light *target) {
+        this->edit_light_callback(target);
+    });
+
+    current->set_show_callback([&] (light *target) {
+        this->show_light_callback(target);
+    });
+    
     this->lights_container->append(*current);
     this->lights->push_back(current);
 }
 
+void lights_list::set_add_light_callback(std::function<void()> target) {
+    this->add_light_callback = target;
+}
+
+void lights_list::set_edit_light_callback(
+    std::function<void(light *)> target
+) {
+    this->edit_light_callback =  target;
+}
+
+void lights_list::set_show_light_callback(
+    std::function<void(light *)> target
+) {
+    this->show_light_callback =  target;
+}
+
 }

+ 9 - 0
cppGTK/source/lights_list.hpp

@@ -2,6 +2,8 @@
 #define LIGHTS_LIST_HPP_INCLUDED
 
 #include <vector>
+#include <functional>
+
 #include <gtkmm/box.h>
 #include <gtkmm/frame.h>
 #include <gtkmm/button.h>
@@ -17,11 +19,18 @@ class lights_list: public Gtk::Frame {
         ~lights_list() override;
 
         void add_light(light);
+        void set_add_light_callback(std::function<void()>);
+        void set_edit_light_callback(std::function<void (light *)>);
+        void set_show_light_callback(std::function<void (light *)>);
 
     private:
         Gtk::Box *content;
         Gtk::Box *lights_container; 
         Gtk::Button *create_light;
+        
+        std::function<void()> add_light_callback;
+        std::function<void(light *)> edit_light_callback;
+        std::function<void(light *)> show_light_callback;
         std::vector<light_on_list *> *lights;
 
         void add_create_light_button();

+ 5 - 1
cppGTK/source/strings.cpp

@@ -7,7 +7,11 @@ namespace cx_light {
 const char* strings::all_texts[] = {
     "cx-light",
     "Your lights",
-    "Add light"
+    "Add light",
+"<b>Welcome in cx-light!</b>\n\n\
+Select light from list on the left, or create it, if it not already \
+exists in Your system. Remember to use same network, Your computer \
+must could ping Your light!\n"
 };
 
 const char* strings::get(unsigned int target) {

+ 2 - 1
cppGTK/source/strings.hpp

@@ -8,7 +8,8 @@ class strings {
         enum selector {
             WINDOW_TITLE,
             LIGHTS_LIST,
-            ADD_LIGHT
+            ADD_LIGHT,
+            WELCOME_TEXT
         };
     
         const char* get(unsigned int);

+ 27 - 0
cppGTK/source/welcome.cpp

@@ -0,0 +1,27 @@
+#include <gtkmm/box.h>
+#include <gtkmm/label.h>
+#include <gtkmm/orientable.h>
+
+#include "welcome.hpp"
+#include "strings.hpp"
+
+namespace cx_light {
+
+welcome::welcome() {
+    this->set_orientation(Gtk::Orientation::VERTICAL);
+
+    this->info = new Gtk::Label(__(WELCOME_TEXT));
+    this->info->set_max_width_chars(1);
+    this->info->set_wrap();
+    this->info->set_hexpand();
+    this->info->set_use_markup();
+    this->info->set_justify(Gtk::Justification::FILL);
+    this->append(*this->info);
+}
+
+welcome::~welcome() {
+    this->remove(*this->info);
+    delete this->info;
+}
+
+}

+ 20 - 0
cppGTK/source/welcome.hpp

@@ -0,0 +1,20 @@
+#ifndef SOURCE_WELCOME_HPP_INCLUDED
+#define SOURCE_WELCOME_HPP_INCLUDED
+
+#include <gtkmm/box.h>
+#include <gtkmm/label.h>
+
+namespace cx_light {
+
+class welcome: public Gtk::Box {
+    public:
+        welcome();
+        ~welcome() override;
+
+    private:
+        Gtk::Label *info;
+};
+
+}
+
+#endif