// AlignedTextItem.h
#include <QGraphicsSimpleTextItem>
class AlignedTextItem
{
public:
~AlignedTextItem();
Qt::Alignment alignment() const;
void setAlignment( Qt::Alignment flags );
private:
Qt::Alignment mFlags;
};
// AlignedTextItem.cpp
#include "AlignedTextItem.h"
#include <QPainter>
AlignedTextItem
::AlignedTextItem( const QString & text
/*= QString()*/, Qt
::Alignment flags
/*= Qt::AlignTop | Qt::AlignLeft*/,
QGraphicsItem * pParent
/*= 0 */ ) , mFlags( flags )
{
setFlag( ItemSendsGeometryChanges );
}
AlignedTextItem
::AlignedTextItem( const QString & text
/*= QString()*/,
QGraphicsItem * pParent
/*= 0 */ ) , mFlags( Qt::AlignTop | Qt::AlignLeft )
{
setFlag( ItemSendsGeometryChanges );
}
AlignedTextItem
::AlignedTextItem( QGraphicsItem * pParent
/*= 0 */ ) , mFlags( Qt::AlignTop | Qt::AlignLeft )
{
setFlag( ItemSendsGeometryChanges );
}
AlignedTextItem::~AlignedTextItem()
{
}
Qt::Alignment AlignedTextItem::alignment() const
{
return mFlags;
}
void AlignedTextItem::setAlignment( Qt::Alignment flags )
{
mFlags = flags;
}
QRectF AlignedTextItem
::boundingRect() const {
if ( mFlags.testFlag( Qt::AlignRight ) )
offset.setX( -mBounds.width() );
else if ( mFlags.testFlag( Qt::AlignHCenter ) )
offset.setX( -mBounds.width() / 2.0 );
if ( mFlags.testFlag( Qt::AlignBottom ) )
offset.setY( mBounds.height() );
else if ( mFlags.testFlag( Qt::AlignVCenter ) )
offset.setY( mBounds.height() / 2.0 );
mBounds.translate( offset );
return mBounds;
}
{
pPainter->translate( mBounds.left(), -mBounds.top() );
}
// AlignedTextItem.h
#include <QGraphicsSimpleTextItem>
class AlignedTextItem
: public QGraphicsSimpleTextItem
{
public:
AlignedTextItem( const QString & text, Qt::Alignment flags, QGraphicsItem * pParent = 0 );
AlignedTextItem( const QString & text, QGraphicsItem * pParent = 0 );
AlignedTextItem( QGraphicsItem * pParent = 0 );
~AlignedTextItem();
QRectF boundingRect() const;
void paint( QPainter * pPainter, const QStyleOptionGraphicsItem * pOption, QWidget * pWidget = 0 );
Qt::Alignment alignment() const;
void setAlignment( Qt::Alignment flags );
private:
Qt::Alignment mFlags;
mutable QRectF mBounds;
};
// AlignedTextItem.cpp
#include "AlignedTextItem.h"
#include <QPainter>
AlignedTextItem::AlignedTextItem( const QString & text /*= QString()*/, Qt::Alignment flags /*= Qt::AlignTop | Qt::AlignLeft*/, QGraphicsItem * pParent /*= 0 */ )
: QGraphicsSimpleTextItem( text, pParent )
, mFlags( flags )
{
setFlag( ItemSendsGeometryChanges );
mBounds = QGraphicsSimpleTextItem::boundingRect();
}
AlignedTextItem::AlignedTextItem( const QString & text /*= QString()*/, QGraphicsItem * pParent /*= 0 */ )
: QGraphicsSimpleTextItem( text, pParent )
, mFlags( Qt::AlignTop | Qt::AlignLeft )
{
setFlag( ItemSendsGeometryChanges );
mBounds = QGraphicsSimpleTextItem::boundingRect();
}
AlignedTextItem::AlignedTextItem( QGraphicsItem * pParent /*= 0 */ )
: QGraphicsSimpleTextItem( pParent )
, mFlags( Qt::AlignTop | Qt::AlignLeft )
{
setFlag( ItemSendsGeometryChanges );
mBounds = QGraphicsSimpleTextItem::boundingRect();
}
AlignedTextItem::~AlignedTextItem()
{
}
Qt::Alignment AlignedTextItem::alignment() const
{
return mFlags;
}
void AlignedTextItem::setAlignment( Qt::Alignment flags )
{
mFlags = flags;
}
QRectF AlignedTextItem::boundingRect() const
{
mBounds = QGraphicsSimpleTextItem::boundingRect();
QPointF offset( 0.0, 0.0 );
if ( mFlags.testFlag( Qt::AlignRight ) )
offset.setX( -mBounds.width() );
else if ( mFlags.testFlag( Qt::AlignHCenter ) )
offset.setX( -mBounds.width() / 2.0 );
if ( mFlags.testFlag( Qt::AlignBottom ) )
offset.setY( mBounds.height() );
else if ( mFlags.testFlag( Qt::AlignVCenter ) )
offset.setY( mBounds.height() / 2.0 );
mBounds.translate( offset );
return mBounds;
}
void AlignedTextItem::paint( QPainter * pPainter, const QStyleOptionGraphicsItem * pOption, QWidget * pWidget /*= 0 */ )
{
pPainter->translate( mBounds.left(), -mBounds.top() );
QGraphicsSimpleTextItem::paint( pPainter, pOption, pWidget );
}
To copy to clipboard, switch view to plain text mode
Bookmarks