Example #1
0
QPixmap QPixmap::grabWindow(WId window, int x, int y, int w, int h)
{
    QWidget *widget = QWidget::find(window);
    if (!widget)
        return QPixmap();

    QRect grabRect = widget->frameGeometry();
    if (!widget->isWindow())
        grabRect.translate(widget->parentWidget()->mapToGlobal(QPoint()));
    if (w < 0)
        w = widget->width() - x;
    if (h < 0)
        h = widget->height() - y;
    grabRect &= QRect(x, y, w, h).translated(widget->mapToGlobal(QPoint()));

    QScreen *screen = qt_screen;
    QDesktopWidget *desktop = QApplication::desktop();
    if (!desktop)
        return QPixmap();
    if (desktop->numScreens() > 1) {
        const int screenNo = desktop->screenNumber(widget);
        if (screenNo != -1)
            screen = qt_screen->subScreens().at(screenNo);
        grabRect = grabRect.translated(-screen->region().boundingRect().topLeft());
    }

    if (screen->pixelFormat() == QImage::Format_Invalid) {
        qWarning("QPixmap::grabWindow(): Unable to copy pixels from framebuffer");
        return QPixmap();
    }

    if (screen->isTransformed()) {
        const QSize screenSize(screen->width(), screen->height());
        grabRect = screen->mapToDevice(grabRect, screenSize);
    }

    QWSDisplay::grab(false);
    QPixmap pixmap;
    QImage img(screen->base(),
               screen->deviceWidth(), screen->deviceHeight(),
               screen->linestep(), screen->pixelFormat());
    img = img.copy(grabRect);
    QWSDisplay::ungrab();

    if (screen->isTransformed()) {
        QMatrix matrix;
        switch (screen->transformOrientation()) {
        case 1: matrix.rotate(90); break;
        case 2: matrix.rotate(180); break;
        case 3: matrix.rotate(270); break;
        default: break;
        }
        img = img.transformed(matrix);
    }

    if (screen->pixelType() == QScreen::BGRPixel)
        img = img.rgbSwapped();

    return QPixmap::fromImage(img);
}
Example #2
0
static inline QScreen *getPrimaryScreen()
{
    QScreen *screen = QScreen::instance();
    if (!screen->base()) {
        QList<QScreen*> subScreens = screen->subScreens();
        if (subScreens.size() < 1)
            return 0;
        screen = subScreens.at(0);
    }
    return screen;
}
QT_BEGIN_NAMESPACE

#ifdef Q_WS_QWS
#ifndef QT_NO_DIRECTPAINTER

/*!
    \class QDirectPainter
    \ingroup painting
    \ingroup qws

    \brief The QDirectPainter class provides direct access to the
    underlying hardware in Qt for Embedded Linux.

    Note that this class is only available in \l{Qt for Embedded Linux}.

    QDirectPainter allows a client application to reserve a region of
    the framebuffer and render directly onto the screen. There are two
    ways of using the QDirectPainter class: You can either reserve a
    region using the provided static functions, or you can instantiate
    an object and make use of its more dynamic API.

    \tableofcontents

    \section1 Dynamic Allocation

    By instantiating a QDirectPainter object using the default
    QDirectPainter::NonReserved surface flag, the client application
    only gets some control over the reserved region, i.e., it can
    still render directly onto the screen but the allocated region may
    change (for example, if a window with a higher focus requests
    parts of the same region). The currently allocated region can be
    retrieved using the allocatedRegion() function, while the
    requestedRegion() function returns the originally reserved
    region.


    \section1 Static Allocation


    Using the static approach, the client application gets complete
    control over the reserved region, i.e., the affected region will
    never be modified by the screen driver.

    To create a static region, pass the QDirectPainter::Reserved
    surface flag to the constructor. After the reserved region is
    reported through regionChanged(), the allocated region will not
    change, unless setRegion() is called.

    If QDirectPainter::ReservedSynchronous is passed to the
    constructor, calls to setRegion() will block until the region is
    reserved, meaning that allocatedRegion() will be available immediately.
    Note that in the current version setRegion() will cause the application
    event loop to be entered, potentially causing reentrancy issues.

    \section1 Rendering

    To draw on a given region, the application must first get hold of
    a pointer to the framebuffer. In most cases, this pointer can be
    retrieved using the QDirectPainter::frameBuffer() function. But
    note that if the current screen has subscreens, you must query the
    screen driver instead to identify the correct subscreen. A pointer
    to the current screen driver can always be retrieved using the
    static QScreen::instance() function. Then use QScreen's \l
    {QScreen::}{subScreenIndexAt()} and \l {QScreen::}{subScreens()}
    functions to access the correct subscreen, and the subscreen's \l
    {QScreen::}{base()} function to retrieve a pointer to the
    framebuffer.

    Depending on the hardware, it might be necessary to lock the
    framebuffer for exclusive use while writing to it. This is
    possible using the lock() and unlock() functions. Note that
    calling lock() will prevent all other applications from working
    until unlock() is called.

    In addition, QDirectPainter provides several functions returning
    information about the framebuffer: the linestep() function returns
    the length (in bytes) of each scanline of the framebuffer while
    the screenDepth(), screenWidth() and screenHeight() function
    return the screen metrics.

    \sa QScreen, QWSEmbedWidget, {Qt for Embedded Linux Architecture}
*/

/*!
    \enum QDirectPainter::SurfaceFlag

    This enum describes the behavior of the region reserved by this
    QDirectPainter object.

    \value NonReserved The allocated region may change, e.g., if a
    window with a higher focus requests parts of the same region. See
    also \l {Dynamic Allocation}.

    \value Reserved The allocated region will never change. See also
    \l {Static Allocation}.

    \value ReservedSynchronous The allocated region will never change and
    each function that changes the allocated region will be blocking.

    \sa allocatedRegion()
*/

/*!
    \fn QRegion QDirectPainter::region()
    \obsolete

    Use QDirectPainter::allocatedRegion() instead.
*/

static inline QScreen *getPrimaryScreen()
{
    QScreen *screen = QScreen::instance();
    if (!screen->base()) {
        QList<QScreen*> subScreens = screen->subScreens();
        if (subScreens.size() < 1)
            return 0;
        screen = subScreens.at(0);
    }
    return screen;
}
/*!
    Returns a pointer to the beginning of the display memory.

    Note that it is the application's responsibility to limit itself
    to modifying only the reserved region.

    Do not use this pointer if the current screen has subscreens,
    query the screen driver instead: A pointer to the current screen
    driver can always be retrieved using the static
    QScreen::instance() function. Then use QScreen's \l
    {QScreen::}{subScreenIndexAt()} and \l {QScreen::}{subScreens()}
    functions to access the correct subscreen, and the subscreen's \l
    {QScreen::}{base()} function to retrieve a pointer to the
    framebuffer.

    \sa requestedRegion(), allocatedRegion(), linestep()
*/
uchar* QDirectPainter::frameBuffer()
{
    QScreen *screen = getPrimaryScreen();
    if (!screen)
        return 0;
    return screen->base();
}