Merge branch 'qt-painter-fixes'
Fixes for measuring text extent and improve/simplify ownership in wxQtGraphicsContext. See https://github.com/wxWidgets/wxWidgets/pull/1160
This commit is contained in:
@@ -19,6 +19,7 @@
|
|||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QDesktopWidget>
|
#include <QDesktopWidget>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
#include <QPicture>
|
||||||
|
|
||||||
#ifndef WX_PRECOMP
|
#ifndef WX_PRECOMP
|
||||||
#include "wx/bitmap.h"
|
#include "wx/bitmap.h"
|
||||||
@@ -30,10 +31,43 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "wx/graphics.h"
|
#include "wx/graphics.h"
|
||||||
|
#include "wx/scopedptr.h"
|
||||||
#include "wx/tokenzr.h"
|
#include "wx/tokenzr.h"
|
||||||
|
|
||||||
#include "wx/private/graphics.h"
|
#include "wx/private/graphics.h"
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
// Ensure that the given painter is active by calling begin() if it isn't. If
|
||||||
|
// it already is, don't do anything.
|
||||||
|
class EnsurePainterIsActive
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit EnsurePainterIsActive(QPainter* painter)
|
||||||
|
: m_painter(painter),
|
||||||
|
m_wasActive(painter->isActive())
|
||||||
|
{
|
||||||
|
if ( !m_wasActive )
|
||||||
|
m_painter->begin(&m_picture);
|
||||||
|
}
|
||||||
|
|
||||||
|
~EnsurePainterIsActive()
|
||||||
|
{
|
||||||
|
if ( !m_wasActive )
|
||||||
|
m_painter->end();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
QPainter* m_painter;
|
||||||
|
QPicture m_picture;
|
||||||
|
bool m_wasActive;
|
||||||
|
|
||||||
|
wxDECLARE_NO_COPY_CLASS(EnsurePainterIsActive);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // anonymous namespace
|
||||||
|
|
||||||
class WXDLLIMPEXP_CORE wxQtBrushData : public wxGraphicsObjectRefData
|
class WXDLLIMPEXP_CORE wxQtBrushData : public wxGraphicsObjectRefData
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -607,7 +641,6 @@ class WXDLLIMPEXP_CORE wxQtGraphicsContext : public wxGraphicsContext
|
|||||||
void InitFromDC(const wxDC& dc)
|
void InitFromDC(const wxDC& dc)
|
||||||
{
|
{
|
||||||
m_qtPainter = static_cast<QPainter*>(dc.GetHandle());
|
m_qtPainter = static_cast<QPainter*>(dc.GetHandle());
|
||||||
m_ownsPainter = false;
|
|
||||||
|
|
||||||
const wxSize sz = dc.GetSize();
|
const wxSize sz = dc.GetSize();
|
||||||
m_width = sz.x;
|
m_width = sz.x;
|
||||||
@@ -615,10 +648,19 @@ class WXDLLIMPEXP_CORE wxQtGraphicsContext : public wxGraphicsContext
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
// Use the specified painter and take ownership of it, i.e. it will be
|
||||||
|
// destroyed in this class dtor.
|
||||||
|
void AttachPainter(QPainter* painter)
|
||||||
|
{
|
||||||
|
m_qtPainter = painter;
|
||||||
|
|
||||||
|
// Ensure that it will be destroyed when this object is.
|
||||||
|
m_ownedPainter.reset(m_qtPainter);
|
||||||
|
}
|
||||||
|
|
||||||
wxQtGraphicsContext(wxGraphicsRenderer* renderer)
|
wxQtGraphicsContext(wxGraphicsRenderer* renderer)
|
||||||
: wxGraphicsContext(renderer),
|
: wxGraphicsContext(renderer),
|
||||||
m_qtPainter(NULL),
|
m_qtPainter(NULL)
|
||||||
m_ownsPainter(false)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -626,8 +668,7 @@ public:
|
|||||||
wxQtGraphicsContext(wxGraphicsRenderer* renderer, QPaintDevice* device)
|
wxQtGraphicsContext(wxGraphicsRenderer* renderer, QPaintDevice* device)
|
||||||
: wxGraphicsContext(renderer)
|
: wxGraphicsContext(renderer)
|
||||||
{
|
{
|
||||||
m_qtPainter = new QPainter(device);
|
AttachPainter(new QPainter(device));
|
||||||
m_ownsPainter = true;
|
|
||||||
|
|
||||||
m_width = device->width();
|
m_width = device->width();
|
||||||
m_height = device->height();
|
m_height = device->height();
|
||||||
@@ -658,19 +699,12 @@ public:
|
|||||||
: wxGraphicsContext(renderer)
|
: wxGraphicsContext(renderer)
|
||||||
{
|
{
|
||||||
m_qtPainter = static_cast<QPainter*>(window->QtGetPainter());
|
m_qtPainter = static_cast<QPainter*>(window->QtGetPainter());
|
||||||
m_ownsPainter = false;
|
|
||||||
|
|
||||||
const wxSize sz = window->GetClientSize();
|
const wxSize sz = window->GetClientSize();
|
||||||
m_width = sz.x;
|
m_width = sz.x;
|
||||||
m_height = sz.y;
|
m_height = sz.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~wxQtGraphicsContext()
|
|
||||||
{
|
|
||||||
if ( m_ownsPainter )
|
|
||||||
delete m_qtPainter;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual bool ShouldOffset() const wxOVERRIDE
|
virtual bool ShouldOffset() const wxOVERRIDE
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@@ -928,6 +962,8 @@ public:
|
|||||||
wxCHECK_RET( !m_font.IsNull(),
|
wxCHECK_RET( !m_font.IsNull(),
|
||||||
"wxQtContext::GetTextExtent - no valid font set" );
|
"wxQtContext::GetTextExtent - no valid font set" );
|
||||||
|
|
||||||
|
EnsurePainterIsActive active(m_qtPainter);
|
||||||
|
|
||||||
const wxQtFontData*
|
const wxQtFontData*
|
||||||
fontData = static_cast<wxQtFontData*>(m_font.GetRefData());
|
fontData = static_cast<wxQtFontData*>(m_font.GetRefData());
|
||||||
m_qtPainter->setFont(fontData->GetFont());
|
m_qtPainter->setFont(fontData->GetFont());
|
||||||
@@ -952,6 +988,8 @@ public:
|
|||||||
wxCHECK_RET( !m_font.IsNull(),
|
wxCHECK_RET( !m_font.IsNull(),
|
||||||
"wxQtContext::GetPartialTextExtents - no valid font set" );
|
"wxQtContext::GetPartialTextExtents - no valid font set" );
|
||||||
|
|
||||||
|
EnsurePainterIsActive active(m_qtPainter);
|
||||||
|
|
||||||
const wxQtFontData*
|
const wxQtFontData*
|
||||||
fontData = static_cast<wxQtFontData*>(m_font.GetRefData());
|
fontData = static_cast<wxQtFontData*>(m_font.GetRefData());
|
||||||
m_qtPainter->setFont(fontData->GetFont());
|
m_qtPainter->setFont(fontData->GetFont());
|
||||||
@@ -1010,9 +1048,11 @@ protected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
QPainter* m_qtPainter;
|
QPainter* m_qtPainter;
|
||||||
bool m_ownsPainter;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
// This pointer may be empty if we don't own m_qtPainter.
|
||||||
|
wxScopedPtr<QPainter> m_ownedPainter;
|
||||||
|
|
||||||
wxDECLARE_NO_COPY_CLASS(wxQtGraphicsContext);
|
wxDECLARE_NO_COPY_CLASS(wxQtGraphicsContext);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1020,12 +1060,10 @@ class wxQtMeasuringContext : public wxQtGraphicsContext
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
wxQtMeasuringContext(wxGraphicsRenderer* renderer)
|
wxQtMeasuringContext(wxGraphicsRenderer* renderer)
|
||||||
: wxQtGraphicsContext(renderer, QApplication::desktop())
|
: wxQtGraphicsContext(renderer)
|
||||||
{
|
{
|
||||||
|
AttachPainter(new QPainter());
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
QPainter painter;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class wxQtImageContext : public wxQtGraphicsContext
|
class wxQtImageContext : public wxQtGraphicsContext
|
||||||
@@ -1037,15 +1075,13 @@ public:
|
|||||||
{
|
{
|
||||||
const wxBitmap wxbitmap(image);
|
const wxBitmap wxbitmap(image);
|
||||||
m_pixmap = *wxbitmap.GetHandle();
|
m_pixmap = *wxbitmap.GetHandle();
|
||||||
m_qtPainter = new QPainter(&m_pixmap);
|
AttachPainter(new QPainter(&m_pixmap));
|
||||||
m_ownsPainter = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
~wxQtImageContext()
|
~wxQtImageContext()
|
||||||
{
|
{
|
||||||
wxQtBitmapData bitmap(GetRenderer(), &m_pixmap);
|
wxQtBitmapData bitmap(GetRenderer(), &m_pixmap);
|
||||||
m_image = bitmap.DoConvertToImage();
|
m_image = bitmap.DoConvertToImage();
|
||||||
delete m_qtPainter;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Reference in New Issue
Block a user