1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #include <sal/config.h>
11 #include <sal/log.hxx>
12 #include <salbmp.hxx>
13
14 #include <config_cairo_canvas.h>
15
16 #include <Qt5Data.hxx>
17 #include <Qt5Frame.hxx>
18 #include <Qt5Graphics_Controls.hxx>
19 #include <Qt5SvpGraphics.hxx>
20 #include <Qt5SvpSurface.hxx>
21 #include <Qt5Tools.hxx>
22
23 #include <QtGui/QScreen>
24 #include <QtGui/QWindow>
25 #include <QtWidgets/QWidget>
26
Qt5SvpGraphics(Qt5Frame * pFrame)27 Qt5SvpGraphics::Qt5SvpGraphics(Qt5Frame* pFrame)
28 : SvpSalGraphics()
29 , m_pFrame(pFrame)
30 {
31 if (!Qt5Data::noNativeControls())
32 m_pWidgetDraw.reset(new Qt5Graphics_Controls());
33 }
34
~Qt5SvpGraphics()35 Qt5SvpGraphics::~Qt5SvpGraphics() {}
36
updateQWidget() const37 void Qt5SvpGraphics::updateQWidget() const
38 {
39 if (!m_pFrame)
40 return;
41 QWidget* pQWidget = m_pFrame->GetQWidget();
42 if (pQWidget)
43 pQWidget->update(pQWidget->rect());
44 }
45
46 #if ENABLE_CAIRO_CANVAS
47
SupportsCairo() const48 bool Qt5SvpGraphics::SupportsCairo() const { return true; }
49
50 cairo::SurfaceSharedPtr
CreateSurface(const cairo::CairoSurfaceSharedPtr & rSurface) const51 Qt5SvpGraphics::CreateSurface(const cairo::CairoSurfaceSharedPtr& rSurface) const
52 {
53 return cairo::SurfaceSharedPtr(new cairo::Qt5SvpSurface(rSurface));
54 }
55
CreateSurface(const OutputDevice &,int x,int y,int width,int height) const56 cairo::SurfaceSharedPtr Qt5SvpGraphics::CreateSurface(const OutputDevice& /*rRefDevice*/, int x,
57 int y, int width, int height) const
58 {
59 return cairo::SurfaceSharedPtr(new cairo::Qt5SvpSurface(this, x, y, width, height));
60 }
61
62 #endif
63
QImage2BitmapBuffer(QImage & rImg,BitmapBuffer & rBuf)64 static void QImage2BitmapBuffer(QImage& rImg, BitmapBuffer& rBuf)
65 {
66 assert(rImg.width());
67 assert(rImg.height());
68
69 rBuf.mnWidth = rImg.width();
70 rBuf.mnHeight = rImg.height();
71 rBuf.mnBitCount = getFormatBits(rImg.format());
72 rBuf.mpBits = rImg.bits();
73 rBuf.mnScanlineSize = rImg.bytesPerLine();
74 }
75
handleDamage(const tools::Rectangle & rDamagedRegion)76 void Qt5SvpGraphics::handleDamage(const tools::Rectangle& rDamagedRegion)
77 {
78 assert(m_pWidgetDraw);
79 assert(dynamic_cast<Qt5Graphics_Controls*>(m_pWidgetDraw.get()));
80 assert(!rDamagedRegion.IsEmpty());
81
82 QImage* pImage = static_cast<Qt5Graphics_Controls*>(m_pWidgetDraw.get())->getImage();
83 assert(pImage);
84 if (pImage->width() == 0 || pImage->height() == 0)
85 return;
86
87 BitmapBuffer aBuffer;
88 QImage2BitmapBuffer(*pImage, aBuffer);
89 SalTwoRect aTR(0, 0, pImage->width(), pImage->height(), rDamagedRegion.getX(),
90 rDamagedRegion.getY(), rDamagedRegion.GetWidth(), rDamagedRegion.GetHeight());
91 drawBitmap(aTR, &aBuffer, CAIRO_OPERATOR_OVER);
92 }
93
GetResolution(sal_Int32 & rDPIX,sal_Int32 & rDPIY)94 void Qt5SvpGraphics::GetResolution(sal_Int32& rDPIX, sal_Int32& rDPIY)
95 {
96 char* pForceDpi;
97 if ((pForceDpi = getenv("SAL_FORCEDPI")))
98 {
99 OString sForceDPI(pForceDpi);
100 rDPIX = rDPIY = sForceDPI.toInt32();
101 return;
102 }
103
104 if (!m_pFrame || !m_pFrame->GetQWidget()->window()->windowHandle())
105 return;
106
107 QScreen* pScreen = m_pFrame->GetQWidget()->window()->windowHandle()->screen();
108 rDPIX = pScreen->logicalDotsPerInchX() * pScreen->devicePixelRatio() + 0.5;
109 rDPIY = pScreen->logicalDotsPerInchY() * pScreen->devicePixelRatio() + 0.5;
110 }
111
112 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
113