Commit d2d4687e0721510932366fcc9f2685c56a7cc0c8
1 parent
4cdc059ed4
Exists in
master
and in
2 other branches
아날로그 시계 추가
Showing
8 changed files
with
248 additions
and
0 deletions
Show diff stats
app/gui/samples/analogclock/.gitignore
@@ -0,0 +1 @@ | @@ -0,0 +1 @@ | ||
1 | +*.pro.user |
app/gui/samples/analogclock/analogclock.pro
@@ -0,0 +1,22 @@ | @@ -0,0 +1,22 @@ | ||
1 | +#------------------------------------------------- | ||
2 | +# | ||
3 | +# Project created by QtCreator 2017-01-13T15:08:27 | ||
4 | +# | ||
5 | +#------------------------------------------------- | ||
6 | + | ||
7 | +QT += core gui | ||
8 | + | ||
9 | +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets | ||
10 | + | ||
11 | +TARGET = test_clock | ||
12 | +TEMPLATE = app | ||
13 | + | ||
14 | + | ||
15 | +SOURCES += main.cpp\ | ||
16 | + mainwindow.cpp \ | ||
17 | + clock.cpp | ||
18 | + | ||
19 | +HEADERS += mainwindow.h \ | ||
20 | + clock.h | ||
21 | + | ||
22 | +FORMS += mainwindow.ui |
app/gui/samples/analogclock/clock.cpp
@@ -0,0 +1,120 @@ | @@ -0,0 +1,120 @@ | ||
1 | +#include "clock.h" | ||
2 | + | ||
3 | +#include <QTime> | ||
4 | +#include <QPainter> | ||
5 | +#include <QtDebug> | ||
6 | +#include <QDateTime> | ||
7 | +#include <QTimeZone> | ||
8 | +#include <QTimer> | ||
9 | + | ||
10 | +Clock::Clock(QWidget *parent) : QWidget(parent) | ||
11 | +{ | ||
12 | + QTimer *timer = new QTimer(this); | ||
13 | + connect(timer, SIGNAL(timeout()), this, SLOT(update())); | ||
14 | + timer->start(33); | ||
15 | +} | ||
16 | + | ||
17 | +void Clock::paintEvent(QPaintEvent */*event*/) | ||
18 | +{ | ||
19 | + QTime time = QDateTime::currentDateTime().time(); | ||
20 | + | ||
21 | + QPainter painter(this); | ||
22 | + painter.setRenderHint(QPainter::Antialiasing); | ||
23 | + painter.translate(width() / 2, height() / 2); | ||
24 | + | ||
25 | + int side = qMin(width(), height()); | ||
26 | + painter.scale(side / 200.0, side / 200.0); | ||
27 | + | ||
28 | + QPen hourHand(Qt::white, 6, Qt::SolidLine, Qt::RoundCap); | ||
29 | + QPen hourArm(Qt::white, 2); | ||
30 | + QPen hourLine(Qt::white, 3); | ||
31 | + | ||
32 | + QPen minHand(hourHand); | ||
33 | + QPen minArm(hourArm); | ||
34 | + QPen minLine(Qt::white, 1); | ||
35 | + | ||
36 | + QPen secHand(Qt::red, 2); | ||
37 | + | ||
38 | + QPen capPen(Qt::white, 3); | ||
39 | + QBrush capBrush(Qt::black, Qt::SolidPattern); | ||
40 | + | ||
41 | + QFont numberFont; | ||
42 | + numberFont.setPixelSize(20); | ||
43 | + | ||
44 | + QFontMetrics numberFontMetrics(numberFont); | ||
45 | + | ||
46 | + QPen numberPen(Qt::white); | ||
47 | + numberPen.setWidth(2); | ||
48 | + | ||
49 | + // Lines | ||
50 | + painter.setPen(minLine); | ||
51 | + for (int j = 0; j < 60; ++j) { | ||
52 | + if ((j % 5) != 0) | ||
53 | + painter.drawLine(92, 0, 96, 0); | ||
54 | + painter.rotate(6.0); | ||
55 | + } | ||
56 | + | ||
57 | + painter.setPen(hourLine); | ||
58 | + for (int i = 0; i < 12; ++i) { | ||
59 | +// painter.drawLine(88, 0, 96, 0); | ||
60 | + painter.drawLine(92, 0, 96, 0); | ||
61 | + painter.rotate(30.0); | ||
62 | + } | ||
63 | + | ||
64 | + // Numbers | ||
65 | + painter.setPen(numberPen); | ||
66 | + painter.setFont(numberFont); | ||
67 | + | ||
68 | + QTransform transform; | ||
69 | + QPoint targetPoint(0, -75); | ||
70 | + QPoint transformedTarget(targetPoint); | ||
71 | + | ||
72 | + QString strings[] = { "12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11" }; | ||
73 | + for (int i = 0; i < 12; i++) { | ||
74 | + QString string = strings[i]; | ||
75 | + QRect boundingRect = numberFontMetrics.boundingRect(string); | ||
76 | + boundingRect.moveCenter(transformedTarget); | ||
77 | + painter.drawText(boundingRect, Qt::AlignCenter, string); | ||
78 | + | ||
79 | + transform.rotate(30.0); | ||
80 | + transformedTarget = transform.map(targetPoint); | ||
81 | + } | ||
82 | + | ||
83 | + // Needles | ||
84 | + qreal second = time.second() + time.msec() / 1000.0; | ||
85 | + if (/* ticking == */ false) | ||
86 | + second = time.second(); | ||
87 | + | ||
88 | + qreal minute = time.minute() + second / 60.0; | ||
89 | + qreal hour = time.hour() + minute / 60.0; | ||
90 | + | ||
91 | + // Sec | ||
92 | + painter.save(); | ||
93 | + painter.setPen(secHand); | ||
94 | + painter.rotate(6.0 * second); | ||
95 | + painter.drawLine(0, 15, 0, -80); | ||
96 | + painter.restore(); | ||
97 | + | ||
98 | + // Min | ||
99 | + painter.save(); | ||
100 | + painter.rotate(6.0 * minute); | ||
101 | + painter.setPen(minArm); | ||
102 | + painter.drawLine(0, 0, 0, -15); | ||
103 | + painter.setPen(minHand); | ||
104 | + painter.drawLine(0, -15, 0, -75); | ||
105 | + painter.restore(); | ||
106 | + | ||
107 | + // Hour | ||
108 | + painter.save(); | ||
109 | + painter.rotate(30.0 * hour); | ||
110 | + painter.setPen(hourArm); | ||
111 | + painter.drawLine(0, 0, 0, -15); | ||
112 | + painter.setPen(hourHand); | ||
113 | + painter.drawLine(0, -15, 0, -50); | ||
114 | + painter.restore(); | ||
115 | + | ||
116 | + // Cap | ||
117 | + painter.setPen(capPen); | ||
118 | + painter.setBrush(capBrush); | ||
119 | + painter.drawEllipse(-5, -5, 10, 10); | ||
120 | +} |
app/gui/samples/analogclock/clock.h
@@ -0,0 +1,20 @@ | @@ -0,0 +1,20 @@ | ||
1 | +#ifndef CLOCK_H | ||
2 | +#define CLOCK_H | ||
3 | + | ||
4 | +#include <QWidget> | ||
5 | + | ||
6 | +class Clock : public QWidget | ||
7 | +{ | ||
8 | + Q_OBJECT | ||
9 | +public: | ||
10 | + explicit Clock(QWidget *parent = 0); | ||
11 | + | ||
12 | +signals: | ||
13 | + | ||
14 | +public slots: | ||
15 | + | ||
16 | +protected: | ||
17 | + void paintEvent(QPaintEvent *event); | ||
18 | +}; | ||
19 | + | ||
20 | +#endif // CLOCK_H |
app/gui/samples/analogclock/main.cpp
app/gui/samples/analogclock/mainwindow.cpp
app/gui/samples/analogclock/mainwindow.h
@@ -0,0 +1,22 @@ | @@ -0,0 +1,22 @@ | ||
1 | +#ifndef MAINWINDOW_H | ||
2 | +#define MAINWINDOW_H | ||
3 | + | ||
4 | +#include <QMainWindow> | ||
5 | + | ||
6 | +namespace Ui { | ||
7 | +class MainWindow; | ||
8 | +} | ||
9 | + | ||
10 | +class MainWindow : public QMainWindow | ||
11 | +{ | ||
12 | + Q_OBJECT | ||
13 | + | ||
14 | +public: | ||
15 | + explicit MainWindow(QWidget *parent = 0); | ||
16 | + ~MainWindow(); | ||
17 | + | ||
18 | +private: | ||
19 | + Ui::MainWindow *ui; | ||
20 | +}; | ||
21 | + | ||
22 | +#endif // MAINWINDOW_H |
app/gui/samples/analogclock/mainwindow.ui
@@ -0,0 +1,38 @@ | @@ -0,0 +1,38 @@ | ||
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<ui version="4.0"> | ||
3 | + <class>MainWindow</class> | ||
4 | + <widget class="QMainWindow" name="MainWindow"> | ||
5 | + <property name="geometry"> | ||
6 | + <rect> | ||
7 | + <x>0</x> | ||
8 | + <y>0</y> | ||
9 | + <width>370</width> | ||
10 | + <height>357</height> | ||
11 | + </rect> | ||
12 | + </property> | ||
13 | + <property name="windowTitle"> | ||
14 | + <string>MainWindow</string> | ||
15 | + </property> | ||
16 | + <property name="styleSheet"> | ||
17 | + <string notr="true">background-color: black</string> | ||
18 | + </property> | ||
19 | + <widget class="QWidget" name="centralWidget"> | ||
20 | + <layout class="QHBoxLayout" name="horizontalLayout"> | ||
21 | + <item> | ||
22 | + <widget class="Clock" name="widget" native="true"/> | ||
23 | + </item> | ||
24 | + </layout> | ||
25 | + </widget> | ||
26 | + </widget> | ||
27 | + <layoutdefault spacing="6" margin="11"/> | ||
28 | + <customwidgets> | ||
29 | + <customwidget> | ||
30 | + <class>Clock</class> | ||
31 | + <extends>QWidget</extends> | ||
32 | + <header>clock.h</header> | ||
33 | + <container>1</container> | ||
34 | + </customwidget> | ||
35 | + </customwidgets> | ||
36 | + <resources/> | ||
37 | + <connections/> | ||
38 | +</ui> |