1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qpixmap.h>
43 #include <qapplication.h>
44 #include <qwidget.h>
45 #include <qdesktopwidget.h>
46 #include <qscreen_qws.h>
47 #include <qwsdisplay_qws.h>
48 #include <private/qdrawhelper_p.h>
49 #include <private/qpixmap_raster_p.h>
50
51
52 QT_BEGIN_NAMESPACE
53
grabWindow(WId window,int x,int y,int w,int h)54 QPixmap QPixmap::grabWindow(WId window, int x, int y, int w, int h)
55 {
56 QWidget *widget = QWidget::find(window);
57 if (!widget)
58 return QPixmap();
59
60 QRect grabRect = widget->frameGeometry();
61 if (!widget->isWindow())
62 grabRect.translate(widget->parentWidget()->mapToGlobal(QPoint()));
63 if (w < 0)
64 w = widget->width() - x;
65 if (h < 0)
66 h = widget->height() - y;
67 grabRect &= QRect(x, y, w, h).translated(widget->mapToGlobal(QPoint()));
68
69 QScreen *screen = qt_screen;
70 QDesktopWidget *desktop = QApplication::desktop();
71 if (!desktop)
72 return QPixmap();
73 if (desktop->numScreens() > 1) {
74 const int screenNo = desktop->screenNumber(widget);
75 if (screenNo != -1)
76 screen = qt_screen->subScreens().at(screenNo);
77 grabRect = grabRect.translated(-screen->region().boundingRect().topLeft());
78 }
79
80 if (screen->pixelFormat() == QImage::Format_Invalid) {
81 qWarning("QPixmap::grabWindow(): Unable to copy pixels from framebuffer");
82 return QPixmap();
83 }
84
85 if (screen->isTransformed()) {
86 const QSize screenSize(screen->width(), screen->height());
87 grabRect = screen->mapToDevice(grabRect, screenSize);
88 }
89
90 QWSDisplay::grab(false);
91 QPixmap pixmap;
92 QImage img(screen->base(),
93 screen->deviceWidth(), screen->deviceHeight(),
94 screen->linestep(), screen->pixelFormat());
95 img = img.copy(grabRect);
96 QWSDisplay::ungrab();
97
98 if (screen->isTransformed()) {
99 QMatrix matrix;
100 switch (screen->transformOrientation()) {
101 case 1: matrix.rotate(90); break;
102 case 2: matrix.rotate(180); break;
103 case 3: matrix.rotate(270); break;
104 default: break;
105 }
106 img = img.transformed(matrix);
107 }
108
109 if (screen->pixelType() == QScreen::BGRPixel)
110 img = img.rgbSwapped();
111
112 return QPixmap::fromImage(img);
113 }
114
clut() const115 QRgb* QPixmap::clut() const
116 {
117 if (data && data->classId() == QPixmapData::RasterClass) {
118 const QRasterPixmapData *d = static_cast<const QRasterPixmapData*>(data.data());
119 return d->image.colorTable().data();
120 }
121
122 return 0;
123 }
124
numCols() const125 int QPixmap::numCols() const
126 {
127 return colorCount();
128 }
129
colorCount() const130 int QPixmap::colorCount() const
131 {
132 if (data && data->classId() == QPixmapData::RasterClass) {
133 const QRasterPixmapData *d = static_cast<const QRasterPixmapData*>(data.data());
134 return d->image.colorCount();
135 }
136
137 return 0;
138 }
139
qwsBits() const140 const uchar* QPixmap::qwsBits() const
141 {
142 if (data && data->classId() == QPixmapData::RasterClass) {
143 const QRasterPixmapData *d = static_cast<const QRasterPixmapData*>(data.data());
144 return d->image.bits();
145 }
146
147 return 0;
148 }
149
qwsBytesPerLine() const150 int QPixmap::qwsBytesPerLine() const
151 {
152 if (data && data->classId() == QPixmapData::RasterClass) {
153 const QRasterPixmapData *d = static_cast<const QRasterPixmapData*>(data.data());
154 return d->image.bytesPerLine();
155 }
156
157 return 0;
158 }
159
160 QT_END_NAMESPACE
161