summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Qt3DStudio/Controls/WinRenderer.cpp
blob: 1fd98734c79199c0f402d652dbb3ab6a22889320 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/****************************************************************************
**
** Copyright (C) 2016 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "Qt3DSCommonPrecompile.h"

#include "WinRenderer.h"
#include "MasterP.h"
#include "CoreUtils.h"

//=============================================================================
/**
 * Leaves the Renderer in an invalid state, only for subclasses.
 * In order for this renderer to be valid, you must do two things: (1) you must
 * create a CDC and set m_DC equal to it, and (2) you must call PushClippingRect.
 */
CWinRenderer::CWinRenderer()
    : m_painter(nullptr)
{
}

//=============================================================================
/**
 * Leaves the Renderer in an invalid state, only for subclasses.
 * PushClippingRect must be called in order for this to become valid.
 */
CWinRenderer::CWinRenderer(QPainter *inDC)
{
    m_painter = inDC;
}

CWinRenderer::CWinRenderer(QPainter *inDC, const QRect &inClippingRect)
{
    m_painter = inDC;

    PushClippingRect(inClippingRect);
}

CWinRenderer::~CWinRenderer()
{
    m_Pens.clear();
}

//=============================================================================
/**
 * Draws a rectangle and fills it with inColor.
 * The rectangle has no border and is solid.
 * The coordinates are converted to global space using the current translation.
 * @param inCoordinates the coordinates of the rectangle.
 * @param inColor the color of the rectangle to draw.
 */
void CWinRenderer::FillSolidRect(const QRect &inCoordinates, const QColor &inColor)
{
    QRect theRect(inCoordinates.topLeft() + m_Translation,
                  inCoordinates.size());

     m_painter->fillRect(theRect, inColor);
}

//=============================================================================
/**
 * Draws a rounded rectangle (which actually is just a line) and fills it with inColor.
 * The coordinates are converted to global space using the current translation.
 * @param inCoordinates the coordinates of the rectangle.
 * @param inColor the color of the rectangle to draw.
 */
void CWinRenderer::FillRoundedRect(const QRect &inCoordinates, const QColor &inColor,
                                   bool vertical)
{
    QPen previousPen = m_painter->pen();
    QRect theRect(inCoordinates.topLeft() + m_Translation, inCoordinates.size());
    QPointF startPoint = (theRect.bottomLeft() + theRect.topLeft()) / 2.0;
    QPointF endPoint = (theRect.bottomRight() + theRect.topRight()) / 2.0;
    qreal lineWidth = theRect.bottom() - theRect.top() + 1.0;

    if (vertical) {
        lineWidth = theRect.right() - theRect.left() + 1.0;
        startPoint = (theRect.topRight() + theRect.topLeft()) / 2.0;
        endPoint = (theRect.bottomRight() + theRect.bottomLeft()) / 2.0;
    }

    m_painter->setPen(QPen(inColor, lineWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    m_painter->drawLine(startPoint, endPoint);

    // Restore previous pen
    m_painter->setPen(previousPen);
}

//=============================================================================
/**
 * Move the pen to the position.
 * This will put the pen at inPoint but will not draw a line there from the
 * current location.
 * @param inPoint the location to move to, in local coordinates.
 */
void CWinRenderer::MoveTo(const QPoint &inPoint)
{
    m_currentPos = inPoint + m_Translation;
}

//=============================================================================
/**
 * Move the pen to the position.
 * This will put the pen to the point but will not draw a line there from the
 * current location.
 * @param inX the X location to move to, in local coordinates.
 * @param inY the Y location to move to, in local coordinates.
 */
void CWinRenderer::MoveTo(long inX, long inY)
{
    MoveTo(QPoint(inX, inY));
}

//=============================================================================
/**
 * Draw a line from the current pen location to inPoint.
 * This will draw a line to inPoint and move the current location of the pen
 * to inPoint.
 * @param inPoint the location to draw to, in local coordinates.
 */
void CWinRenderer::LineTo(const QPoint &inPoint)
{
    const QPoint point = inPoint + m_Translation;
    m_painter->drawLine(m_currentPos, point);
    m_painter->drawLine(point, QPoint(point.x(), point.y() + 1));
    m_currentPos = point;
}

//=============================================================================
/**
 * Draw a line from the current pen location to the point.
 * This will draw a line to the point and move the current location of the pen
 * to inPoint.
 * @param inX the X coordinate of the point to draw to, in local coordinates.
 * @param inY the Y coordinate of the point to draw to, in local coordinates.
 */
void CWinRenderer::LineTo(long inX, long inY)
{
    LineTo(QPoint(inX, inY));
}

//=============================================================================
/**
 * Change the active pen color.
 * This acts as a stack so that it does not interfere with previous components.
 * Once the color is done being used PopPen should be called.
 * @param inColor the color to make the current pen.
 * @param inWidth the pen width, in pixels.
 */
void CWinRenderer::PushPen(const QColor &inColor, int inWidth)
{
    QPen thePen = GetPen(inColor, inWidth, Qt::SolidLine);

    QPen theCurrentPen = m_painter->pen();
    m_painter->setPen(thePen);
    m_PenList.push_back(theCurrentPen);
}

void CWinRenderer::PopPen()
{
    QPen thePen = m_PenList.back();
    m_PenList.pop_back();
    m_painter->setPen(thePen);
}

//=============================================================================
/**
 * Copy the bits from another renderer into this one.
 * This will copy the rect from inRenderer into this renderer.
 * inRect will be offset by this renderer's current translation. inXSrc and inYSrc
 * will be offset by inRenderer's current translation.
 * @param inRect the position and size of the area to be copied into this renderer.
 * @param inPixmap the renderer to copy from.
 * @param xSrc the x location of the location to copy from in inRenderer.
 * @param ySrc the y location of the location to copy from in inRenderer.
 */
void CWinRenderer::BitBltFrom(const QRect &inRect, CRenderer *inRenderer, long inXSrc, long inYSrc)
{
    const auto inTranslation = inRenderer->GetTranslation();
    inXSrc += inTranslation.x();
    inYSrc += inTranslation.y();

    auto srcRect = inRect;
    auto destRect = inRect;
    destRect.translate(m_Translation);
    srcRect.moveTo(inXSrc, inYSrc);
    m_painter->save();
    m_painter->setCompositionMode(QPainter::CompositionMode_DestinationOver);
    m_painter->drawPixmap(destRect, inRenderer->pixmap(), srcRect);
    m_painter->restore();
}

//=============================================================================
/**
 * Draws text out without clipping it.
 * @param inPoint the point at which to draw the text. (upper left corner)
 * @param inText the text to draw.
 */
void CWinRenderer::DrawText(float inX, float inY, const QString &inText)
{
    inX += m_Translation.x();
    inY += m_Translation.y();
    m_painter->drawText(QPointF(inX, inY), inText);
}

//=============================================================================
/**
 * Draws text out to a clipped rectangle.
 * If any text occurs outside the bounding box then it will be clipped.
 * @param inPoint the point at which to draw the text. (upper left corner)
 * @param inText the text to draw.
 * @param inBoundingBox the bounding box used to clip the text.
 * @param inColor color to draw the text in
 */
void CWinRenderer::DrawText(float inX, float inY, const QString &inText,
                            const QRect &inBoundingBox, const QColor &inColor)
{
    inX += m_Translation.x();
    inY += m_Translation.y();

    QRectF rect(inBoundingBox);
    rect.translate(inX, inY);
    m_painter->save();
    QPen pen(inColor);
    m_painter->setPen(pen);
    m_painter->drawText(rect, Qt::AlignLeft | Qt::AlignVCenter, inText);
    m_painter->restore();
}

//=============================================================================
/**
 * Draws BOLD text out to a clipped rectangle.
 * If any text occurs outside the bounding box then it will be clipped.
 * @param inPoint the point at which to draw the text. (upper left corner)
 * @param inText the text to draw.
 * @param inBoundingBox the bounding box used to clip the text.
 * @param inColor color to draw the text in
 */
void CWinRenderer::DrawBoldText(float inX, float inY, const QString &inText,
                                const QRect &inBoundingBox, const QColor &inColor)
{
    inX += m_Translation.x();
    inY += m_Translation.y();

    QRectF rect(inBoundingBox);
    rect.translate(inX, inY);
    m_painter->save();
    QPen pen(inColor);
    m_painter->setPen(pen);
    QFont font = m_painter->font();
    font.setBold(true);
    m_painter->setFont(font);
    m_painter->drawText(rect, Qt::AlignLeft | Qt::AlignVCenter, inText);
    m_painter->restore();
}

//=============================================================================
/**
 * Gets the dimensions of the text string as it would be written out to the
 * screen.
 * @param inText the text to check the length on.
 * @return the length of the text in pixels.
 */
QSize CWinRenderer::GetTextSize(const QString &inText)
{
    QFontMetrics fm = m_painter->fontMetrics();
    return fm.size(Qt::TextSingleLine, inText);
}

//=============================================================================
/**
 * Draws a a three-dimensional rectangle with the top and left sides in the
 * color specified by inTopLeftColor and the bottom and right sides in the color
 * specified by inBottomRightColor.
 *
 * @param inRect The rectangle to draw
 * @param inTopLeftColor Color for the top and left sides of the rect
 * @param inBottomRightColor Color for the bottom and right sides of the rect
 */
void CWinRenderer::Draw3dRect(const QRect &inRect, const QColor &inTopLeftColor,
                              const QColor &inBottomRightColor)
{
    auto rect = inRect;
    rect.translate(m_Translation);
    m_painter->drawRect(rect);
    m_painter->save();
    m_painter->setPen(inTopLeftColor);
    m_painter->drawLine(rect.topLeft(), rect.bottomLeft());
    m_painter->drawLine(rect.topLeft(), rect.topRight());
    m_painter->setPen(inBottomRightColor);
    m_painter->drawLine(rect.bottomLeft(), rect.bottomRight());
    m_painter->drawLine(rect.bottomRight(), rect.topRight());
    m_painter->restore();
}

//==============================================================================
/**
 *	DrawBitmap
 *
 *	Draw a bitmap given a device context, position and HBITMAP handle.
 *
 *	@param	inDC		Device context for drawing
 *	@param	inPos		CPoint position for drawing
 *	@param	inBitmap	Handle of the bitmap to draw
 */
//==============================================================================
void CWinRenderer::DrawBitmap(const QPoint &inPos, const QPixmap &inImage)
{
    m_painter->save();
    m_painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
    m_painter->drawPixmap(inPos + m_Translation, inImage);
    m_painter->restore();;
}

void CWinRenderer::PushTranslation(const QPoint &inTranslation)
{
    m_Translation += inTranslation;

    m_Translations.push_back(inTranslation);
}

void CWinRenderer::PopTranslation()
{
    QPoint thePreviousTranslation = m_Translations.back();
    m_Translation -= thePreviousTranslation;

    m_Translations.pop_back();
}

QPoint CWinRenderer::GetTranslation()
{
    return m_Translation;
}

QPainter* CWinRenderer::GetPainter()
{
    return m_painter;
}

//==============================================================================
/**
 * Get the current clipping rect.
 * The clipping rect is the boundary of pixels that will be drawn to the DC.
 * This can be used to not draw non-visible objects.
 * @return the clipping rect in local coordinates.
 */
QRect CWinRenderer::GetClippingRect()
{
    QRect theClippingRect =  m_painter->clipBoundingRect().toRect();
    theClippingRect.translate(-m_Translation);

    return theClippingRect;
}

//==============================================================================
/**
 * Push a clipping rect onto the stack of clipping rects.
 * This will cause any drawing outside of the clipping rect to be ignored. The
 * Control class also uses this to not draw objects outside of this rect.
 * @param inClippingRect the new clipping rect, in local coordinates.
 */
void CWinRenderer::PushClippingRect(const QRect &inClippingRect)
{
    QRect clippingRect(inClippingRect);
    clippingRect.translate(m_Translation);

    const QRegion currentRegion = m_painter->clipRegion();
    m_painter->setClipRect(clippingRect);

    m_ClippingRegions.push_back(currentRegion);
}

//==============================================================================
/**
 * Pop the current clipping rect.
 * This will change the clipping rect to use the one previous to the current
 * one.
 */
void CWinRenderer::PopClippingRect()
{
    if (m_ClippingRegions.size() > 1) {
        QRegion thePreviousRegion = m_ClippingRegions.back();
        m_painter->setClipRegion(thePreviousRegion);
        m_ClippingRegions.pop_back();
    }
}

//==============================================================================
/**
 *	DrawGradiantBitmap
 *
 *	Draws a gradiant based on the begin color
 *
 *	@param	inRct		Rct tof the control
 *	@param	inBeginColor color to start with
 *	@param	inInverted	true if this is inverted
 */
void CWinRenderer::DrawGradientBitmap(const QRect &inRct, const QColor &inBeginColor, bool inInverted,
                                      double inScalingFactor)
{
    QRect rect(inRct);
    QRect theClippingRect = GetClippingRect();
    rect &= theClippingRect;
    rect.translate(m_Translation);

    long theHeight = rect.height();
    long theWidth = rect.width();

    QImage theCompatibleBitmap(1, theHeight,  QImage::Format_RGB32);

    int theR = inBeginColor.red();
    int theG = inBeginColor.green();
    int theB = inBeginColor.blue();

    double theExtraRModifier = inScalingFactor * (1.0 - (theR / 255.0));
    double theExtraGModifier = inScalingFactor * (1.0 - (theG / 255.0));
    double theExtraBModifier = inScalingFactor * (1.0 - (theB / 255.0));

    for (long thePixel = 0; thePixel < theHeight; ++thePixel) {
        double theNormPixel = (double)thePixel / (theHeight * 4.8);
        double theCos = 1.0 / (theNormPixel * theNormPixel + 1.0);
        double theRValue = (double)theR * (theCos + theExtraRModifier);
        double theGValue = (double)theG * (theCos + theExtraGModifier);
        double theBValue = (double)theB * (theCos + theExtraBModifier);

        QColor theTempColor(::dtoi(theRValue), ::dtoi(theGValue), ::dtoi(theBValue));
        if (inInverted) {
            theCompatibleBitmap.setPixelColor(0, qMax(0l, theHeight - thePixel - 2), theTempColor);
        } else {
            theCompatibleBitmap.setPixelColor(0, thePixel, theTempColor);
        }
        theExtraRModifier *= 0.3;
        theExtraGModifier *= 0.3;
        theExtraBModifier *= 0.3;
    }

    m_painter->save();
    m_painter->drawImage(QRect(rect.x(), rect.y(), theWidth, theHeight), theCompatibleBitmap);
    m_painter->restore();
}

//=============================================================================
/**
 * Draw a gradient over inRect.
 * This will blend horizontally across the rect from inBeginColor into inEndColor.
 * This does linear interpolation.
 * @param inRect the rect to draw on.
 * @param inBeginColor the start (left most) color.
 * @param inEndColor the final (right most) color.
 */
void CWinRenderer::DrawGradient(const QRect &inRect, const QColor &inBeginColor, const QColor &inEndColor)
{
    const QRect rect = inRect.translated(m_Translation);
    QLinearGradient gradient(rect.topLeft(), rect.topRight());
    gradient.setColorAt(0, inBeginColor);
    gradient.setColorAt(1, inEndColor);

    QBrush brush(gradient);
    m_painter->fillRect(rect, brush);
}

void CWinRenderer::FillHashed(const QRect &inRect, const QColor &inForegroundColor)
{
    m_painter->save();

    QBrush theBrush(inForegroundColor, Qt::BDiagPattern);
    QPen pen(inForegroundColor);
    m_painter->setPen(pen);
    m_painter->setBrush(theBrush);
    m_painter->drawRect(inRect.translated(m_Translation));

    m_painter->restore();
}

QPen CWinRenderer::GetPen(const QColor &inColor, int inWidth, Qt::PenStyle inStyle)
{
    QPen thePen;
    SPenInfo theInfo = { inColor, inWidth, inStyle };
    TPenMap::iterator thePos = m_Pens.find(theInfo);
    if (thePos != m_Pens.end()) {
        thePen = thePos->second;
    } else {
        thePen.setColor(inColor);
        thePen.setWidth(inWidth);
        m_Pens[theInfo] = thePen;
    }
    return thePen;
}

QPixmap CWinRenderer::pixmap() const
{
    return {};
}