diff --git a/src/qdialogtext.cpp b/src/qdialogtext.cpp index 23a8ed1..c8986f4 100644 --- a/src/qdialogtext.cpp +++ b/src/qdialogtext.cpp @@ -22,8 +22,8 @@ #include "ui_qdialogtext.h" #include "qcdscene.h" #include "qdialogsettings.h" +#include "qlighttextitem.h" -#include #include #include @@ -55,19 +55,20 @@ void QDialogText::changeEvent(QEvent *e) bool QDialogText::exec( QGraphicsItem *graphicsItem ) { - QGraphicsSimpleTextItem *item = dynamic_cast< QGraphicsSimpleTextItem * >( graphicsItem ); + QLightTextItem *item = dynamic_cast< QLightTextItem * >( graphicsItem ); if( !item ) return false; QCDScene scene; m_ui->cdView->setScene( &scene ); - m_item = new QGraphicsSimpleTextItem; + m_item = new QLightTextItem; m_item->setPos( item->pos() ); m_item->setText( item->text() ); m_item->setFont( item->font() ); - m_item->setBrush( item->brush() ); + m_item->setColor( item->color() ); m_item->setTransform( item->transform() ); + m_item->setAlignment( item->alignment() ); scene.addItem( m_item, true ); @@ -75,19 +76,27 @@ bool QDialogText::exec( QGraphicsItem *graphicsItem ) m_ui->spinY->setValue( m_item->pos().y() ); m_ui->textEdit->setPlainText( m_item->text() ); + int index = 0; + if( m_item->alignment() == Qt::AlignRight ) index = 2; + else if( m_item->alignment() == Qt::AlignCenter ) index = 1; + + m_ui->comboAlignment->setCurrentIndex( index ); + colorChanged(); fontChanged(); connect( m_ui->textEdit, SIGNAL(textChanged()), this, SLOT(textChanged()) ); connect( m_ui->spinX, SIGNAL(valueChanged(double)), this, SLOT(posChanged()) ); connect( m_ui->spinY, SIGNAL(valueChanged(double)), this, SLOT(posChanged()) ); + connect( m_ui->comboAlignment, SIGNAL(currentIndexChanged(int)),this, SLOT(alignChanged()) ); if( QDialog::exec() == Rejected ) return false; item->setPos( m_item->pos() ); item->setText( m_item->text() ); item->setFont( m_item->font() ); - item->setBrush( m_item->brush() ); + item->setColor( m_item->color() ); + item->setAlignment( m_item->alignment() ); return true; } @@ -105,11 +114,11 @@ void QDialogText::onFont() void QDialogText::onColor() { - QColor color = QColorDialog::getColor( m_item->brush().color(), this ); + QColor color = QColorDialog::getColor( m_item->color(), this ); if( color.isValid() ){ - m_item->setBrush( color ); + m_item->setColor( color ); colorChanged(); - } + } } void QDialogText::fontChanged() @@ -120,7 +129,7 @@ void QDialogText::fontChanged() void QDialogText::colorChanged() { QPixmap pixmap( m_ui->colorWidget->size() ); - pixmap.fill( m_item->brush().color() ); + pixmap.fill( m_item->color() ); m_ui->colorWidget->setPixmap( pixmap ); } @@ -134,3 +143,9 @@ void QDialogText::posChanged() m_item->setPos( m_ui->spinX->value(), m_ui->spinY->value() ); } +void QDialogText::alignChanged() +{ + Qt::Alignment align[] = { Qt::AlignLeft, Qt::AlignCenter, Qt::AlignRight }; + m_item->setAlignment( align[ m_ui->comboAlignment->currentIndex() ] ); +} + diff --git a/src/qdialogtext.h b/src/qdialogtext.h index e0620d4..ad41eaa 100644 --- a/src/qdialogtext.h +++ b/src/qdialogtext.h @@ -16,7 +16,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - $Id:$ */ + $Id$ */ #ifndef QDIALOGTEXT_H #define QDIALOGTEXT_H @@ -27,7 +27,7 @@ namespace Ui { class QDialogText; } -class QGraphicsSimpleTextItem; +class QLightTextItem; class QDialogText : public QItemDialog { Q_OBJECT @@ -48,10 +48,11 @@ private slots: void fontChanged(); void colorChanged(); void posChanged(); + void alignChanged(); private: - Ui::QDialogText *m_ui; - QGraphicsSimpleTextItem *m_item; + Ui::QDialogText *m_ui; + QLightTextItem *m_item; }; #endif // QDIALOGTEXT_H diff --git a/src/qdialogtext.ui b/src/qdialogtext.ui index 04b7690..3743a37 100644 --- a/src/qdialogtext.ui +++ b/src/qdialogtext.ui @@ -1,10 +1,11 @@ - + + QDialogText - - + + true - + 0 0 @@ -12,70 +13,70 @@ 627 - + Text properties - + - + - - + + QFrame::StyledPanel - + QFrame::Raised - + 3 - + - - + + QLayout::SetMaximumSize - + 0 - - - - + + + + X: - - - + + + 1 - + -61.000000000000000 - + 61.000000000000000 - - - + + + Y: - - - + + + 1 - + -61.000000000000000 - + 61.000000000000000 @@ -83,11 +84,11 @@ - - + + Qt::Horizontal - + 13 20 @@ -96,70 +97,104 @@ - - - - + + + QFormLayout::ExpandingFieldsGrow + + + + Font: - - - + + + + false + + + false + + + + + + Color: - - - - false - - - false - - - - - - + + + 20 20 - + 20 20 - + 0 0 - + QFrame::Box - - + + + - + Qt::PlainText + + + + 0 + + + + Left + + + + + Center + + + + + Right + + + + + + + + + Alignment: + + + - - + + Qt::Horizontal - + 17 20 @@ -168,20 +203,20 @@ - - + + QFormLayout::ExpandingFieldsGrow - - - + + + Color... - - - + + + Font... @@ -191,26 +226,26 @@ - + - - + + Text - + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop - - - + + + 0 0 - + 16777215 50 @@ -221,20 +256,20 @@ - - - + + + 0 2 - + 0 0 - + false @@ -243,11 +278,11 @@ - - + + Qt::Horizontal - + QDialogButtonBox::Cancel|QDialogButtonBox::Ok @@ -278,11 +313,11 @@ QDialogText accept() - + 258 582 - + 157 274 @@ -294,11 +329,11 @@ QDialogText reject() - + 326 582 - + 286 274 diff --git a/src/qlighttextitem.cpp b/src/qlighttextitem.cpp index 4d38c48..a9c6042 100644 --- a/src/qlighttextitem.cpp +++ b/src/qlighttextitem.cpp @@ -25,6 +25,7 @@ #include #include +#include QLightTextItem::QLightTextItem() @@ -39,6 +40,58 @@ QVariant QLightTextItem::itemChange( GraphicsItemChange change, const QVariant & return value; } +QString QLightTextItem::text() const +{ + return QGraphicsSimpleTextItem::text(); +} + +QColor QLightTextItem::color() const +{ + return this->brush().color(); +} + +Qt::Alignment QLightTextItem::alignment() const +{ + return m_alignment; +} + +void QLightTextItem::setText( const QString &txt ) +{ + QGraphicsSimpleTextItem::setText( txt ); + updateTrans(); +} + +void QLightTextItem::setFont( const QFont &font ) +{ + QGraphicsSimpleTextItem::setFont( font ); + updateTrans(); +} + + +void QLightTextItem::setColor( const QColor &color ) +{ + this->setBrush( color ); +} + + +void QLightTextItem::setAlignment( Qt::Alignment align ) +{ + m_alignment = align; + updateTrans(); +} + +void QLightTextItem::updateTrans() +{ + double size = this->boundingRect().width(); + + QTransform trans; + trans.scale( 0.5, 0.5 ); + switch( m_alignment ) { + case Qt::AlignRight : trans.translate( -size, 0 ); break; + case Qt::AlignCenter : trans.translate( -size / 2.0, 0 ); break; + } + this->setTransform( trans ); +} RegisterController< QShapeControllerText > regControllerText; @@ -69,20 +122,21 @@ QItemDialog *QShapeControllerText::createDialog( QWidget *parent ) const void QShapeControllerText::writeData( QXmlStreamWriter &writer, const QGraphicsItem *item ) const { - const QGraphicsSimpleTextItem *textItem = static_cast< const QGraphicsSimpleTextItem * >( item ); + const QLightTextItem *textItem = static_cast< const QLightTextItem * >( item ); writer.writeEmptyElement( "pos" ); writer.writeAttribute( QXmlStreamAttribute( "x", QString::number( textItem->pos().x() ) ) ); writer.writeAttribute( QXmlStreamAttribute( "y", QString::number( textItem->pos().y() ) ) ); writer.writeAttribute( QXmlStreamAttribute( "z", QString::number( textItem->zValue() ) ) ); writer.writeTextElement( "font", textItem->font().toString() ); - writer.writeTextElement( "color", textItem->brush().color().name() ); + writer.writeTextElement( "color", textItem->color().name() ); writer.writeTextElement( "text", textItem->text() ); + writer.writeTextElement( "alignment", QString::number( textItem->alignment() ) ); } void QShapeControllerText::replace( QGraphicsItem *item, const QString &from, const QString &to ) const { - QGraphicsSimpleTextItem *textItem = static_cast< QGraphicsSimpleTextItem * >( item ); + QLightTextItem *textItem = static_cast< QLightTextItem * >( item ); bool placeHolder = textItem->text().left( 1 ) == "?" && textItem->text().right( 1 ) == "?"; @@ -102,7 +156,7 @@ void QShapeControllerText::readData( const QString &element, const QString &data, QGraphicsItem *item ) const { - QGraphicsSimpleTextItem *textItem = static_cast< QGraphicsSimpleTextItem * >( item ); + QLightTextItem *textItem = static_cast< QLightTextItem * >( item ); if( element == "pos" ) { textItem->setPos( attrs.value( "x" ).toString().toDouble(), @@ -119,7 +173,7 @@ void QShapeControllerText::readData( const QString &element, } if( element == "color" ) { - textItem->setBrush( QColor( data ) ); + textItem->setColor( QColor( data ) ); return; } @@ -127,4 +181,9 @@ void QShapeControllerText::readData( const QString &element, textItem->setText( data ); return; } + + if( element == "alignment" ) { + textItem->setAlignment( Qt::Alignment( data.toInt() ) ); + return; + } } diff --git a/src/qlighttextitem.h b/src/qlighttextitem.h index 4f84074..ff0cb95 100644 --- a/src/qlighttextitem.h +++ b/src/qlighttextitem.h @@ -30,9 +30,22 @@ class QLightTextItem : public QGraphicsSimpleTextItem public: QLightTextItem(); + QString text() const; + Qt::Alignment alignment() const; + QColor color() const; + + void setText( const QString &txt ); + void setColor( const QColor &color ); + void setFont( const QFont &font ); + void setAlignment( Qt::Alignment align ); + protected: virtual QVariant itemChange ( GraphicsItemChange change, const QVariant & value ); +private: + void updateTrans(); + + Qt::Alignment m_alignment; }; class QShapeControllerText : public QShapeController {