How to rotate text for drawText? - c++

I would like to rotate the text 45 degrees?
QFont font;
font.setPixelSize(12);
//grid
for(int i = 0; i < 10; i++){
painter->drawLine(100, 100 + i * 800/9, 900, 100 + i * 800/9);
str = QString::number((double)9 - i, 'd', 1);
painter->setFont(font);
painter->drawText(75, 100 + i * 800/9 - 6, 40, 40, 1, str);
}

Insert painter->rotate(45); before painter->drawText(75, 100 + i * 800/9 - 6, 40, 40, 1, str); and painter->rotate(-45); after (to restore the rotation angle of the coordinate system):
painter->rotate(45);
painter->drawText(75, 100 + i * 800/9 - 6, 40, 40, 1, str);
painter->rotate(-45);
Depending on if you mean 45 degrees clockwise or anti-clockwise you may need to negate the rotation angles.
After you rotate the coordinate system, everything you paint will be painted rotated until you restore the painter. A convenient way of saving and restoring the state of the painter is using QPainter::save() and QPainter::restore().

painter->save(); // saves current painter state
// painter->rotate(45); clockwise rotation
// painter->rotate(-45); counter clockwise rotation
painter->restore(); // restores painter state

In order to rotate your text (and any other drawable object) drawn by painter just call
painter->rotate(yourAngle);
before
painter->drawText();
If you wish to return to previous state call rotate again.
painter->rotate(-yourAngle);

Why making such a simple task so complicated?!!!
void CustomLabel::paintEvent(QPaintEvent* e)
{
QPainter painter(this);
painter.translate(m_rect.center());
painter.rotate(m_rotation);
painter.translate(-m_rect.center());
painter.drawText(m_rect, Qt::AlignHCenter | Qt::AlignVCenter, m_text);
QWidget::paintEvent(e);
}
any time the container of CustomLabel changes it size you can set the m_rect or use the this->rect() itself.

Related

QT Rotating Pie of a rectangle without shifting center

I would like to make an application which includes rotation widget inside a circle. I started this with the AnalogClock example in qt. I wouldd like to rotate a quarter pie (quarter circle) instead of gauge. My problem is I cannot locate the pie in the center of my circle.
Below picture, I barely locate the pi in the center of circle but I would like to make the pi bigger than below picture.
enter image description here
I realized that all paintings locate inside of rectangle like below.
enter image description here
When I change the drawPie function parameters rectangle's location change and rotates based on on edge point. I can make the ractange bigger but this time drawPie center changes too like the last visual.
enter image description here
You can check the code below. It is modifed from the AnalogClock example. I would like to draw a quarter pie and make it the rotate endlessly. Any help will be appreciated. If you have a better opinion, I would like hear that too.
AnalogClock::AnalogClock(QWidget *parent) :
QWidget(parent),
ui(new Ui::AnalogClock)
{
ui->setupUi(this);
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, QOverload<>::of(&AnalogClock::update));
timer->start(1000);
setWindowTitle(tr("Analog Clock"));
resize(200, 200);
}
void AnalogClock::paintEvent(QPaintEvent *)
{
QColor minuteColor(0, 127, 127, 191);
int side = qMin(width(), height());
QTime time = QTime::currentTime();
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(width() / 2, height() / 2);
painter.scale(side / 200.0, side / 200.0);
painter.setPen(Qt::NoPen);
qreal radius=10;
qreal startAngle=0;
qreal span=60;
for (int i = 0; i < 12; ++i) {
painter.drawLine(88, 0, 96, 0);
painter.rotate(30.0);
}
painter.setBrush(minuteColor);
painter.save();
painter.rotate(6.0 * time.second() );
//painter.drawConvexPolygon(minuteHand, 3);
QRect rect( -radius, -radius, radius*10, radius*10);
painter.drawPie( rect, startAngle*16, span*16 );
// painter.fillRect(rect,QBrush(Qt::green));
painter.restore();
painter.setPen(minuteColor);
for (int j = 0; j < 60; ++j) {
painter.drawLine(92, 0, 96, 0);
painter.rotate(6.0);
}
}
The drawPie() function takes a rectangle that defines the hypothetical full circle in which the pie (slice) will be painted. In this example, we want that circle to be the same as the full clock circle - same size, same center.
Now the code has normalized the coordinate system for us, so that the clock center point is at (0, 0), and the radius is 100. So we need a rectangle that has its center point in (0,0) and sides that are 2*radius long. That is:
QRect rect(-100, -100, 200, 200);
Changing the rect in the code sample to that fixes the basic issue.

Problem with drawing a clock with Direct2D

I am trying to draw a clock with Direct2D. The program correctly gets the current time, however the error is when Direct2D draws the clock, since it seems that the clock is out of date with the current time. To get the current time, use the GetLocalTime() function and then map the values to transform them to an angle, so that they can be drawn. How could I fix it?
...
renderTarget->BeginDraw();
renderTarget->Clear(ColorF(ColorF::Black));
GetLocalTime(&sysTime);
wstring text = L"Hour: " + to_wstring(sysTime.wHour)
+ L"\nMinute: " + to_wstring(sysTime.wMinute)
+ L"\nSecond: " + to_wstring(sysTime.wSecond);
brush->SetColor(ColorF(ColorF::White));
renderTarget->DrawTextW(text.c_str(), text.length(), textFormat, textRect, brush);
D2D1_POINT_2F centerPoint = Point2F(320, 240);
FLOAT hourAngle = map(sysTime.wHour%12, 0, 12, 0, 360);
FLOAT minuteAngle = map(sysTime.wMinute, 0, 60, 0, 360);
FLOAT secondAngle = map(sysTime.wSecond, 0, 60, 0, 360);
brush->SetColor(ColorF(ColorF::DeepPink));
renderTarget->DrawEllipse(D2D1::Ellipse(centerPoint, 150, 150), brush, 5);
renderTarget->SetTransform(Matrix3x2F::Rotation(secondAngle, centerPoint));
brush->SetColor(ColorF(ColorF::Blue));
renderTarget->DrawLine(centerPoint,Point2F(centerPoint.x,centerPoint.y + 150*0.9), brush, 10,lineStrokeStyle);
renderTarget->SetTransform(Matrix3x2F::Rotation(minuteAngle, centerPoint));
brush->SetColor(ColorF(ColorF::White));
renderTarget->DrawLine(centerPoint,Point2F(centerPoint.x,centerPoint.y + 150*0.7), brush, 10, lineStrokeStyle);
renderTarget->SetTransform(Matrix3x2F::Rotation(hourAngle, centerPoint));
brush->SetColor(ColorF(ColorF::GreenYellow));
renderTarget->DrawLine(centerPoint, Point2F(centerPoint.x,centerPoint.y + 150*0.5), brush, 10,lineStrokeStyle);
renderTarget->SetTransform(Matrix3x2F::Identity());
HRESULT hrErr = renderTarget->EndDraw();
if (hrErr != S_OK) {
MessageBox(hWnd, L"Direct2D Error", L"Direct2D Error", MB_OK | MB_ICONERROR);
SafeRelease(&brush);
SafeRelease(&renderTarget);
SafeRelease(&factory);
}
...
Yes, you can use GetLocalTime to get the current local date and time. The problem may be in mapping the time to an angle.
But these can be solved, because there are very detailed examples in MSDN.
Instead of calculating the coordinates for the lines, we can calculate the angle and then apply a rotation transform. The following code shows a function that draws one clock hand. The fAngle parameter gives the angle of the hand, in degrees.
void Scene::DrawClockHand(float fHandLength, float fAngle, float fStrokeWidth)
{
m_pRenderTarget->SetTransform(
D2D1::Matrix3x2F::Rotation(fAngle, m_ellipse.point)
);
// endPoint defines one end of the hand.
D2D_POINT_2F endPoint = D2D1::Point2F(
m_ellipse.point.x,
m_ellipse.point.y - (m_ellipse.radiusY * fHandLength)
);
// Draw a line from the center of the ellipse to endPoint.
m_pRenderTarget->DrawLine(
m_ellipse.point, endPoint, m_pStroke, fStrokeWidth);
}
This code draws a vertical line, starting from the center of the clock face and ending at the point endPoint. The line is rotated around the center of the ellipse by applying a rotation transform. The center point for the rotation is the center of ellipse that forms the clock face.
void Scene::RenderScene()
{
m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::SkyBlue));
m_pRenderTarget->FillEllipse(m_ellipse, m_pFill);
m_pRenderTarget->DrawEllipse(m_ellipse, m_pStroke);
// Draw hands
SYSTEMTIME time;
GetLocalTime(&time);
// 60 minutes = 30 degrees, 1 minute = 0.5 degree
const float fHourAngle = (360.0f / 12) * (time.wHour) + (time.wMinute * 0.5f);
const float fMinuteAngle =(360.0f / 60) * (time.wMinute);
DrawClockHand(0.6f, fHourAngle, 6);
DrawClockHand(0.85f, fMinuteAngle, 4);
// Restore the identity transformation.
m_pRenderTarget->SetTransform( D2D1::Matrix3x2F::Identity() );
}
Refer: Drawing Clock Hands
Debug:
The whole code sample:
Direct2D Clock Sample

Draw using textured QBrush without translating texture

I have made a simple texture of an outlined box and have the following snippet of code which draws a checkerboard pattern:
scene.setSceneRect(0, 0, 1000, 1000);
ui->g_view->setScene(&scene);
QPixmap texture("block.png");
QBrush brush(texture);
int count = 0;
for(int x=0; x<1000; x += 50) {
for(int y=0; y<1000; y += 50) {
if (count % 2 == 0) {
scene.addRect(x, y, 50, 50, Qt::NoPen, brush);
}
count++;
}
// Offset rows by 1
count++;
}
This works fine:
However, when I modify the code so that the boxes are drawn "off grid":
scene.addRect(x + 5, y + 5, 50, 50, Qt::NoPen, brush);
The following output is produced:
What I expected to happen was that each call to addRect would draw the texture starting from the top corner each time.
However, for some reason qt translates the texture using the location that it is being drawn too, almost like the texture is infinitely tiled in the background and addRect is just cutting away a window.
How can I make drawRect behave as I expected, i.e. no matter what the values of x and y are the texture is always drawn from the top left corner.
Edit: block.png
I solved this issue by instead using the addPixmap method.
//scene.addRect(x + 5, y + 5, 50, 50, Qt::NoPen, brush);
QGraphicsPixmapItem *pix_map = scene.addPixmap(texture);
pix_map->setPos(x + 5, y + 5);

SFML Drawing centered text

I'm trying to center some text in SFML. It doesen't really work though as you can see in the picture below.
Maybe something is wrong with my math?
void TextRenderer::renderCentered(sf::RenderWindow& window, std::string string, sf::Vector2f position, int size, sf::Color color) {
sf::Text text;
text.setFont(TextRenderer::kavoon);
text.setString(string);
float width = text.getLocalBounds().width;
text.setPosition(position.x - width / 2, position.y);
text.setCharacterSize(size);
text.setColor(color);
window.draw(text);
}
in render method:
TextRenderer::renderCentered(*window, pickuptext.str(), sf::Vector2f(player->sprite.getPosition().x, player->sprite.getPosition().y - 48), 28, sf::Color(255, 145, 61));
TextRenderer::renderCentered(*window, pickuptextdesc.str(), sf::Vector2f(player->sprite.getPosition().x, player->sprite.getPosition().y - 16), 18, sf::Color(255, 145, 61));
The formula for centering text horizontally within a bounding box is:
text_start = bounding_box_width / 2 - text_width / 2;
Looks like you may not be supplying the bounding box horizontal center point.

QT QGraphicsItemAnimation

I've got a class: mySquare which inherits from QGraphicsRectItem
added only my constructor, painter and an animation:
ANIMATION:
void mySquare::animation(mySquare *k)
{
QTimeLine *timeLine = new QTimeLine();
timeLine->setLoopCount(1);
QGraphicsItemAnimation *animation = new QGraphicsItemAnimation();
animation->setItem(k);
animation->setTimeLine(timeLine);
int value = 30;
animation->setTranslationAt(0.3, value, value);
timeLine->start();
// (*)
// x += 30;
// y += 30;
}
PAINTER:
void Klocek::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *widget)
{
bokKwadratu = (min(widget->width(), widget->height()))/5;
setRect(x * 30, y * 30, 30 - 3, 30 - 3);
QRectF rect = boundingRect();
painter->setBrush(brush);
painter->setPen(pen);
QFont font;
font.setPixelSize(bokKwadratu/3);
painter->setFont(font);
painter->drawRect(rect);
painter->drawText(rect,Qt::AlignCenter, QString::number(wartosc));
}
CONSTRUCTOR:
mySquare::mySquare(qreal x, qreal y) : QGraphicsRectItem(x * 10, y * 10, 10, 10)
{
setAcceptHoverEvents(true);
this->x = x;
this->y = y;
pen.setColor(Qt::red);
pen.setWidth(2);
brush.setColor(Qt::blue);
brush.setStyle(Qt::SolidPattern);
}
after performing animation (translation) I need to change the object coordinates so they are compatible which the situation on the screen. In other words after the translation (30, 30) I want the coordinates of the rectangle to be change (x += 30, y += 30)
my problem is that when i try to do this ( (*) fragment in the code) the triangle is put far away from its position (just as if the translation was performed twice)
My question is how to translate it and change the coordinates without such complications.
To begin with, I think you're misunderstanding the use of the function setTranslationAt in the QGraphicsItem Animation.
An animation has a normalised value over time, so can start at 0.0 and end at 1.0 (or the reverse). Therefore, by calling
animation->setTranslationAt(0.3, value, value);
You've stated that at the point when the normalised value reaches 0.3, you want the x and y position to be set to 'value'. That's fine, but you also need to set other values for the animation to occur (especially at the val of 1.0!). If you use a for loop, you can iterate through values from 0.0 to 1.0 and set where you want the position of your item to be. Take a look at the example code in the Qt help files for QGraphicsItemAnimation. The QGraphicsItemAnimation uses interpolation to work out the position of your object between the known points that you've given it. If you're interested: -
http://en.wikipedia.org/wiki/Linear_interpolation
Secondly, the item's rect is the definition of the item in its local coordinate space. So if you wanted a rect with its axis in the centre, you'd define it with x,y,w,h of (-w/2, -h/2, w, h). As these are local coordinates, they then get mapped into world coordinates in the GraphicsScene, which is where you set its actual position in the world.
Once you've setup your QGraphicsItemRect's local coordinates and world position, you can then simply paint it with drawRect and should not be setting positions in the paint function.