1 /**
2 * File name: painter.cpp
3 * Project: Redkite (A small GUI toolkit)
4 *
5 * Copyright (C) 2019 Iurie Nistor <http://quamplex.com>
6 *
7 * This file is part of Redkite.
8 *
9 * Redkite is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #include "RkMain.h"
25 #include "RkWidget.h"
26 #include "RkPainter.h"
27 #include "RkPoint.h"
28 #include "RkLog.h"
29 #include "RkEvent.h"
30 #include "RkEventQueue.h"
31 #include "RkLabel.h"
32 #include "RkTimer.h"
33 #include "RkProgressBar.h"
34
35 class Button: public RkWidget {
36 public:
Button(RkWidget * parent=nullptr)37 Button(RkWidget *parent = nullptr)
38 : RkWidget(parent)
39 , isToggled{false} {}
40
41 RK_DECL_ACT(toggled, toggled(bool b), RK_ARG_TYPE(bool), RK_ARG_VAL(b));
42
43 protected:
mouseButtonPressEvent(RkMouseEvent * event)44 void mouseButtonPressEvent(RkMouseEvent *event) final
45 {
46 isToggled = !isToggled;
47 action toggled(isToggled);
48 }
49
50 private:
51 bool isToggled;
52 };
53
54 class PainterExample: public RkWidget {
55 public:
PainterExample(RkMain * app)56 PainterExample(RkMain *app)
57 : RkWidget(app)
58 , startDraw{false}
59 , timerLabel{nullptr}
60 , myTime{0}
61 {
62 setSize(350, 350);
63 auto button = new Button(this);
64 button->setPosition(30, 30);
65 button->setSize({30, 30});
66 button->setBackgroundColor(255, 30, 100);
67 RK_ACT_BIND(button, toggled, RK_ACT_ARGS(bool b), this, drawCircle(b));
68 button->show();
69
70 progressBar = new RkProgressBar(this);
71 progressBar->setRange(0, 23);
72 progressBar->setSize(width() - 10, 5);
73 progressBar->setPosition(5, 150);
74 progressBar->setProgressColor({0, 200, 0});
75
76 timerLabel = new RkLabel(this, "Timer");
77 timerLabel->setBackgroundColor(80, 80, 80);
78 timerLabel->setSize(100, 25);
79 timerLabel->setPosition(30, 80);
80 timerLabel->show();
81
82 timer = new RkTimer(this, 1000);
83 RK_ACT_BIND(timer, timeout, RK_ACT_ARGS(), this, onShowTime());
84 timer->start();
85 }
86
87 protected:
paintEvent(RkPaintEvent * event)88 void paintEvent(RkPaintEvent *event) final
89 {
90 RK_UNUSED(event);
91 RkPainter painter(this);
92 painter.fillRect(rect(), RkColor(background()));
93 RkPen pen(RkColor(255, 0, 0));
94 pen.setWidth(1);
95 painter.setPen(pen);
96 if (startDraw)
97 painter.drawCircle(100, 150, 20);
98 }
99
drawCircle(bool b)100 void drawCircle(bool b)
101 {
102 startDraw = b;
103 update();
104 }
105
onShowTime()106 void onShowTime()
107 {
108 timerLabel->setText(std::to_string(myTime) + "s");
109 progressBar->setValue(myTime++);
110 }
111
112 private:
113 bool startDraw;
114 RkLabel *timerLabel;
115 int myTime;
116 RkTimer* timer;
117 RkProgressBar *progressBar;
118 };
119
main(int arc,char ** argv)120 int main(int arc, char **argv)
121 {
122 RkMain app(arc, argv);
123
124 auto widget = new PainterExample(&app);
125 widget->setTitle("Painter Example");
126 widget->show();
127
128 return app.exec();
129 }
130