why glibmm is not showing correct time intervals?

40 Views Asked by At

I am getting very large values on the interface and where I am using Glib::DateTime::difference(), that function produces negative numbers

initializer of controller class:

UI_Controller::UI_Controller(Gtk::Builder * refference, Gtk::Application * app)
{
    view_limit_time = new Gtk::Calendar();
    view_limit_time->set_hexpand(true);
    deffine_application (app);
    this->refference = refference;
    refference->get_widget("main_window",this->content_relations);
    widgets = refference->get_objects();
    Glib::ustring widget_names = "";
    //dynamic_cast<Gtk::Button*>(button_access->get_child_at(1,0))
    for (int i=0; i < widgets.size(); i++){
        widget_names = widget_names+dynamic_cast<Gtk::Widget*>(widgets.at(i).get())->get_name()+"\n";
        if (dynamic_cast<Gtk::Buildable*>(widgets.at(i).get())->get_name() == (Glib::ustring) "timer_grid"){
            Gtk::Grid * button_access = dynamic_cast<Gtk::Grid*>(widgets.at(i).get());
            grid_counter = i;
            dynamic_cast<Gtk::Button*>(button_access->get_child_at(0,2))->signal_clicked().connect(sigc::bind<Gtk::Label*>(sigc::mem_fun(*this,&UI_Controller::start_timer),dynamic_cast<Gtk::Label*>(button_access->get_child_at(0,1)),i ) );
            dynamic_cast<Gtk::Button*>(button_access->get_child_at(1,2))->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,&UI_Controller::stop_timer),i ) );
            dynamic_cast<Gtk::Button*>(button_access->get_child_at(2,2))->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,&UI_Controller::restart_timer),i ) );
            dynamic_cast<Gtk::Button*>(button_access->get_child_at(3,0))->signal_clicked().connect(sigc::bind<Gtk::Widget*>(sigc::mem_fun(*this,&UI_Controller::add_timer),button_access ) );
            button_access->set_size_request(700,100);
            //dynamic_cast<Gtk::ScrolledWindow*>((button_access->get_ancestor (GTK_TYPE_SCROLLED_WINDOW)))->set_min_content_height(300);
        }
        if (dynamic_cast<Gtk::Buildable*>(widgets.at(i).get())->get_name() == (Glib::ustring) "counter_grid"){
            Gtk::Grid * button_access = dynamic_cast<Gtk::Grid*>(widgets.at(i).get());
            button_access->attach(*(dynamic_cast<Gtk::Widget*>(view_limit_time)),0,1,2,1);
            //view_limit_time->signal_day_selected().connect(sigc::bind<Gtk::Widget*>(sigc::mem_fun(*this,&UI_Controller::start_counter),(button_access->get_child_at(2,1)),i,(button_access->get_child_at(0,1))) );
            grid_counter = i;
            dynamic_cast<Gtk::Button*>(button_access->get_child_at(0,2))->signal_clicked().connect(sigc::bind<Gtk::Widget*>(sigc::mem_fun(*this,&UI_Controller::start_counter),(button_access->get_child_at(2,1)),i,(button_access->get_child_at(0,1))) );
            dynamic_cast<Gtk::Button*>(button_access->get_child_at(1,2))->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,&UI_Controller::stop_counter),i ) );
            dynamic_cast<Gtk::Button*>(button_access->get_child_at(2,2))->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,&UI_Controller::restart_counter),i ) );
            dynamic_cast<Gtk::Button*>(button_access->get_child_at(3,0))->signal_clicked().connect(sigc::bind<Gtk::Widget*>(sigc::mem_fun(*this,&UI_Controller::add_counter),button_access ) );
            button_access->set_size_request(700,300);
            //dynamic_cast<Gtk::ScrolledWindow*>((button_access->get_ancestor (GTK_TYPE_SCROLLED_WINDOW)))->set_min_content_height(300);
        }
    }
    app->run();
}

header from the controller:

#ifndef _U_I_CONTROLLER_H_
#define _U_I_CONTROLLER_H_

#include <unistd.h>
#include <gtkmm.h>
#include <unordered_map>
#include <glibmm/datetime.h>
#include <time-keeper.h>
#include <iostream>

#define TIMER_GRID_UI "src/n_time.ui"
#define COUNTER_GRID_UI "src/n_counter.ui"

namespace std{
    template <>
    struct hash<Time_Keeper>
    {
        size_t operator()( Time_Keeper& t) const
        {
            return t.hasheable().hash();
        }
    };
}

class UI_Controller
{
public:
    UI_Controller(Gtk::Builder* refference,Gtk::Application * app);
    void deffine_application(Gtk::Application * app);
    void add_window_to_application(Gtk::Window * window);
protected:

private:
    int grid_counter;
    bool timer_started=false;
    Gtk::Builder * refference;
    Gtk::ApplicationWindow * content_relations;
    Gtk::Application * app;
    Gtk::Calendar * view_limit_time;
    std::vector<Glib::RefPtr<Glib::Object>> widgets;
    std::unordered_map<int,Time_Keeper> bind_time;
    void show_window(Gtk::Window *window);
    void start_timer(Gtk::Label * selected, int position);
    void stop_timer(int i) { (bind_time[i]).stop_timer (); };
    void restart_timer(int i) { (bind_time[i]).reset_timer ();};
    void add_timer(Gtk::Widget * selected);
    bool timeout_timer(Gtk::Label * display,int position);
    bool timeout_counter(Gtk::Label * display,int position, Glib::DateTime when);
    int get_index(Glib::RefPtr<Glib::Object> target);
    void start_counter (Gtk::Widget * selected, int position, Gtk::Widget * set_when);
    void stop_counter (int i){ (bind_time[i]).stop_counter();};
    void restart_counter(int i){ return ;};
    void add_counter(Gtk::Widget * selected);
    /*sigc::connection Glib::SignalTimeout::connect(const sigc::slot<bool()>& slot,
                                    unsigned int interval, int priority = Glib::PRIORITY_DEFAULT);*/
};

#endif // _U_I_CONTROLLER_H_

the actionable function related to the counter:

void UI_Controller::start_counter(Gtk::Widget * selected, int position, Gtk::Widget * set_when){
    guint selected_year, selected_month, selected_day;
    if (bind_time.find(position) == bind_time.end() ){
        bind_time [position] = *(new Time_Keeper());
    }
    //Glib::DateTime when = ( Glib::DateTime::create_from_iso8601(( (dynamic_cast<Gtk::Entry*>(set_when))->get_text())) );
    (dynamic_cast<Gtk::Calendar*>(set_when))->get_date(selected_year,selected_month,selected_day);
    Glib::DateTime when = (Glib::DateTime::create_utc(selected_year,selected_month,selected_day,1,1,1));
    //refference->get_widget("timer_display",display);
    //if (!timer_started){
        sigc::slot<bool()> my_slot = sigc::bind(sigc::mem_fun(*this,
                  &UI_Controller::timeout_counter), (dynamic_cast<Gtk::Label*>(selected)), position,when);
        auto conn = Glib::signal_timeout().connect(my_slot, 1000);
        timer_started = true;
    //}
}

the function calculating the time difference on the Time_Keeper class:

Glib::ustring Time_Keeper::display_counter(Glib::DateTime when){
    counter_active = true;
    Glib::DateTime now = Glib::DateTime::create_now_utc();
    size_t seconds = ( ((size_t)(when.difference(now))) /1000000);
    std::cout << now.get_year() << std::endl;

    size_t hours = (seconds/3600);
    seconds%=3600;
    size_t minutes=seconds/60;
    seconds%=60;
    
    return ((Glib::ustring) ( std::to_string(hours)+":" \
        +std::to_string(minutes)+":" \
        +std::to_string(seconds) ));
}

ui file:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
  <requires lib="gtk+" version="3.24"/>
  <object class="GtkApplicationWindow" id="main_window">
    <property name="can-focus">False</property>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can-focus">False</property>
        <property name="orientation">vertical</property>
        <property name="spacing">13</property>
        <property name="homogeneous">True</property>
        <child>
          <object class="GtkScrolledWindow">
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="shadow-type">in</property>
            <child>
              <object class="GtkViewport">
                <property name="visible">True</property>
                <property name="can-focus">False</property>
                <child>
                  <object class="GtkBox">
                    <property name="visible">True</property>
                    <property name="can-focus">False</property>
                    <property name="orientation">vertical</property>
                    <child>
                      <object class="GtkLabel">
                        <property name="visible">True</property>
                        <property name="can-focus">False</property>
                        <property name="label" translatable="yes">current activity time</property>
                      </object>
                      <packing>
                        <property name="expand">False</property>
                        <property name="fill">True</property>
                        <property name="position">0</property>
                      </packing>
                    </child>
                    <child>
                      <!-- n-columns=4 n-rows=3 -->
                      <object class="GtkGrid" id="timer_grid">
                        <property name="visible">True</property>
                        <property name="can-focus">False</property>
                        <property name="hexpand">True</property>
                        <property name="vexpand">True</property>
                        <child>
                          <object class="GtkEntry">
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="hexpand">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">0</property>
                            <property name="top-attach">0</property>
                            <property name="width">3</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkButton" id="start_timer">
                            <property name="label" translatable="yes">Start</property>
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="receives-default">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">0</property>
                            <property name="top-attach">2</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkButton" id="stop_timer">
                            <property name="label" translatable="yes">Stop</property>
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="receives-default">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">1</property>
                            <property name="top-attach">2</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkButton" id="restart_timer">
                            <property name="label" translatable="yes">Restart</property>
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="receives-default">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">2</property>
                            <property name="top-attach">2</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkButton" id="add_timer">
                            <property name="label" translatable="yes">+</property>
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="receives-default">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">3</property>
                            <property name="top-attach">0</property>
                            <property name="height">3</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkLabel" id="timer_display">
                            <property name="visible">True</property>
                            <property name="can-focus">False</property>
                            <property name="hexpand">True</property>
                            <property name="vexpand">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">0</property>
                            <property name="top-attach">1</property>
                            <property name="width">3</property>
                          </packing>
                        </child>
                      </object>
                      <packing>
                        <property name="expand">False</property>
                        <property name="fill">True</property>
                        <property name="position">1</property>
                      </packing>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkScrolledWindow">
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="shadow-type">in</property>
            <child>
              <object class="GtkViewport">
                <property name="visible">True</property>
                <property name="can-focus">False</property>
                <child>
                  <object class="GtkBox">
                    <property name="visible">True</property>
                    <property name="can-focus">False</property>
                    <property name="orientation">vertical</property>
                    <child>
                      <object class="GtkLabel">
                        <property name="visible">True</property>
                        <property name="can-focus">False</property>
                        <property name="label" translatable="yes">activity limit</property>
                      </object>
                      <packing>
                        <property name="expand">False</property>
                        <property name="fill">True</property>
                        <property name="position">0</property>
                      </packing>
                    </child>
                    <child>
                      <!-- n-columns=4 n-rows=3 -->
                      <object class="GtkGrid" id="counter_grid">
                        <property name="visible">True</property>
                        <property name="can-focus">False</property>
                        <property name="hexpand">True</property>
                        <property name="vexpand">True</property>
                        <child>
                          <object class="GtkEntry">
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="hexpand">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">0</property>
                            <property name="top-attach">0</property>
                            <property name="width">3</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkButton">
                            <property name="label" translatable="yes">Start</property>
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="receives-default">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">0</property>
                            <property name="top-attach">2</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkButton">
                            <property name="label" translatable="yes">Stop</property>
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="receives-default">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">1</property>
                            <property name="top-attach">2</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkButton">
                            <property name="label" translatable="yes">Restart</property>
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="receives-default">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">2</property>
                            <property name="top-attach">2</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkButton">
                            <property name="label" translatable="yes">+</property>
                            <property name="visible">True</property>
                            <property name="can-focus">True</property>
                            <property name="receives-default">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">3</property>
                            <property name="top-attach">0</property>
                            <property name="height">3</property>
                          </packing>
                        </child>
                        <child>
                          <object class="GtkLabel">
                            <property name="visible">True</property>
                            <property name="can-focus">False</property>
                            <property name="hexpand">True</property>
                            <property name="vexpand">True</property>
                          </object>
                          <packing>
                            <property name="left-attach">2</property>
                            <property name="top-attach">1</property>
                          </packing>
                        </child>
                        <child>
                          <placeholder/>
                        </child>
                      </object>
                      <packing>
                        <property name="expand">False</property>
                        <property name="fill">True</property>
                        <property name="position">1</property>
                      </packing>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>
1

There are 1 best solutions below

0
jeferson lemos On

on the line (dynamic_cast<Gtk::Calendar*>(set_when))->get_date(selected_year,selected_month,selected_day); the month was being set to 1 unity less and that was confusing the calculation of the time difference. I have found it when outputting the difference of the function get_day_of_year from the Glib::DateTime objects "now" and "when" and seeing that it was showing high values