Skip to content

Instantly share code, notes, and snippets.

@Riateche
Created June 15, 2013 11:15
Show Gist options
  • Save Riateche/5787779 to your computer and use it in GitHub Desktop.
Save Riateche/5787779 to your computer and use it in GitHub Desktop.
#include "window.h"
#include <QKeyEvent>
#include <QtCore/QRegExp>
#include <QtGui/QMessageBox>
#include <QGraphicsTextItem>
#include <QFont>
#include <QTransform>
#include <QTimeLine>
#include <QGraphicsItemAnimation>
#include <QDebug>
#include <QPainter>
#include <qmath.h>
QPointF my_find_ellipse_coords(const QRectF &r, qreal angle) {
QPainterPath path;
path.arcMoveTo(r, angle);
return path.currentPosition();
}
class Circle: public QWidget {
public:
int steps, value, maxValue;
QPen pen;
Circle(QWidget *parent): QWidget(parent)
{
}
void setSteps(int i)
{
this->steps = i;
}
void setValue(int i)
{
this->value = i;
repaint();
}
void setMaxValue(int i)
{
this->maxValue = i;
}
void paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(this->pen);
int stepSize = 360/this->steps;
float devideValue = ((100.0/this->maxValue)*this->value)/100.0;
int roundedSize = this->steps*devideValue;
qDebug() << "steps: " << steps;
qDebug() << "stepSize: " << stepSize;
qDebug() << "devideValue: " << devideValue;
qDebug() << "roundedSize: " << roundedSize;
qDebug() << "stepSize*roundedSize: " << (stepSize*roundedSize);
//qDebug() << "angel: " << angle;
double angle = -1.0*(stepSize*roundedSize);
QPainterPath path;
QRectF outer_rect(0, 0, width(), height());
QRectF inner_rect(pen.width(), pen.width(), width() - pen.width() * 2, height() - pen.width() * 2);
path.arcMoveTo(outer_rect, 0);
path.arcTo(outer_rect, 0, angle);
path.lineTo(my_find_ellipse_coords(inner_rect, angle));
path.arcTo(inner_rect, angle, -angle);
path.lineTo(my_find_ellipse_coords(outer_rect, 0));
path.closeSubpath();
painter.fillPath(path, QBrush(pen.color()));
}
void setPen(QPen pen)
{
this->pen = pen;
}
};
Window::Window(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
//QRect mainWidgetGeo = geometry();
int w = 450; //mainWidgetGeo.width();
int h = 450; // mainWidgetGeo.height();
QPen secondPen(Qt::yellow);
secondPen.setWidth(50);
secondPen.setCapStyle(Qt::FlatCap);
Circle* circleSeconds = new Circle(this->centralWidget());
circleSeconds->setMaxValue(60);
circleSeconds->setValue(55);
circleSeconds->setSteps(60);
circleSeconds->setMouseTracking(true);
circleSeconds->setPen(secondPen);
circleSeconds->setGeometry(QRect(0, 0, w, h));
QPen minutePen(Qt::red);
minutePen.setWidth(100);
minutePen.setCapStyle(Qt::FlatCap);
Circle* circleMinutes = new Circle(this->centralWidget());
circleMinutes->setMaxValue(60);
circleMinutes->setValue(50);
circleMinutes->setSteps(60);
circleMinutes->setMouseTracking(true);
circleMinutes->setPen(minutePen);
circleMinutes->setGeometry(QRect(50, 50, w-100, h-100));
QPen hourPen(Qt::green);
hourPen.setWidth(50);
hourPen.setCapStyle(Qt::FlatCap);
Circle* circleHours = new Circle(this->centralWidget());
circleHours->setMaxValue(12);
circleHours->setValue(45);
circleHours->setSteps(12);
circleHours->setMouseTracking(true);
circleHours->setPen(hourPen);
circleHours->setGeometry(QRect(150, 150, w-300, h-300));
resize(300, 300);
}
Window::~Window() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment