LoginSignup
0
0

More than 5 years have passed since last update.

raspberry pi 1でgtkmm その3

Posted at

概要

raspberry pi 1でgtkmmやってみた。
時計やってみた。

写真

2018-12-22-111524_1440x900_scrot.png

サンプルコード

#include <gtkmm/drawingarea.h>
#include <ctime>
#include <cmath>
#include <cairomm/context.h>
#include <glibmm/main.h>
#include <gtkmm/application.h>
#include <gtkmm/window.h>

class Clock: public Gtk::DrawingArea
{
public:
    Clock();
    virtual ~Clock();
protected:
    bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr) override;
    bool on_timeout();
    double m_radius;
    double m_line_width;
};
Clock::Clock(): m_radius(0.42), m_line_width(0.05)
{
    Glib::signal_timeout().connect( sigc::mem_fun(* this, &Clock::on_timeout), 1000);
}
Clock::~Clock()
{
}
bool Clock::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
    Gtk::Allocation allocation = get_allocation();
    const int width = allocation.get_width();
    const int height = allocation.get_height();
    cr->scale(width, height);
    cr->translate(0.5, 0.5);
    cr->set_line_width(m_line_width);
    cr->save();
    cr->set_source_rgba(0.337, 0.612, 0.117, 0.9);
    cr->paint();
    cr->restore();
    cr->arc(0, 0, m_radius, 0, 2 * M_PI);
    cr->save();
    cr->set_source_rgba(1.0, 1.0, 1.0, 0.8);
    cr->fill_preserve();
    cr->restore();
    cr->stroke_preserve();
    cr->clip();
    for (int i = 0; i < 12; i++)
    {
        double inset = 0.05;
        cr->save();
        cr->set_line_cap(Cairo::LINE_CAP_ROUND);
        if (i % 3 != 0)
        {
            inset *= 0.8;
            cr->set_line_width(0.03);
        }
        cr->move_to((m_radius - inset) * cos (i * M_PI / 6), (m_radius - inset) * sin (i * M_PI / 6));
        cr->line_to (m_radius * cos (i * M_PI / 6), m_radius * sin (i * M_PI / 6));
        cr->stroke();
        cr->restore();
    }
    time_t rawtime;
    time(&rawtime);
    struct tm * timeinfo = localtime(&rawtime);
    double minutes = timeinfo->tm_min * M_PI / 30;
    double hours = timeinfo->tm_hour * M_PI / 6;
    double seconds= timeinfo->tm_sec * M_PI / 30;
    cr->save();
    cr->set_line_cap(Cairo::LINE_CAP_ROUND);
    cr->save();
    cr->set_line_width(m_line_width / 3);
    cr->set_source_rgba(0.7, 0.7, 0.7, 0.8);
    cr->move_to(0, 0);
    cr->line_to(sin(seconds) * (m_radius * 0.9), -cos(seconds) * (m_radius * 0.9));
    cr->stroke();
    cr->restore();
    cr->set_source_rgba(0.117, 0.337, 0.612, 0.9);
    cr->move_to(0, 0);
    cr->line_to(sin(minutes + seconds / 60) * (m_radius * 0.8), -cos(minutes + seconds / 60) * (m_radius * 0.8));
    cr->stroke();
    cr->set_source_rgba(0.337, 0.612, 0.117, 0.9);
    cr->move_to(0, 0);
    cr->line_to(sin(hours + minutes / 12.0) * (m_radius * 0.5), -cos(hours + minutes / 12.0) * (m_radius * 0.5));
    cr->stroke();
    cr->restore();
    cr->arc(0, 0, m_line_width / 3.0, 0, 2 * M_PI);
    cr->fill();
    return true;
}
bool Clock::on_timeout()
{
    auto win = get_window();
    if (win)
    {
        Gdk::Rectangle r(0, 0, get_allocation().get_width(), get_allocation().get_height());
        win->invalidate_rect(r, false);
    }
    return true;
}
int main(int argc, char ** argv)
{
     auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
     Gtk::Window win;
     win.set_title("Cairomm Clock");
     Clock c;
     win.add(c);
     c.show();
     return app->run(win);
}



以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0