24 #include "gobchartstextitem.h"
25 #include "utils/globalincludes.h"
27 #include <QtCore/qmath.h>
28 #include <QFontMetrics>
29 #include <QTextDocument>
30 #include <QTextOption>
31 #include <QDomDocument>
32 #include <QMessageBox>
33 #include <QTextCursor>
40 qreal PERCENTAGE_DEAD_SPACE = 0.05;
44 qreal PERCENTAGE_WIDTH_MARGIN = 0.05;
49 bool WiderThanNew(
const QRectF &memberRect,
const QRectF &newTextRect )
51 return ( qFloor( memberRect.width() ) > qFloor( newTextRect.width() ) );
56 bool HigherThanNew(
const QRectF &memberRect,
const QRectF &newTextRect )
58 return ( qFloor( memberRect.height() ) > qFloor( newTextRect.height() ) );
63 bool HasSameHeight(
const QRectF &memberRect,
const QRectF &newTextRect )
65 return ( qFloor( memberRect.height() ) == qFloor( newTextRect.height() ) );
70 bool HasSameWidth(
const QRectF &memberRect,
const QRectF &newTextRect )
72 return ( qFloor( memberRect.width() ) == qFloor( newTextRect.width() ) );
77 bool LargerThanNew(
const QRectF &memberRect,
const QRectF &newTextRect )
79 return !( ( ( WiderThanNew ( memberRect, newTextRect ) || HasSameWidth ( memberRect, newTextRect ) ) && HigherThanNew( memberRect, newTextRect ) ) ||
80 ( ( HigherThanNew( memberRect, newTextRect ) || HasSameHeight( memberRect, newTextRect ) ) && WiderThanNew ( memberRect, newTextRect ) ) );
87 QGraphicsTextItem( parent ),
88 m_orientation ( orientation ),
89 m_alignment ( Qt::AlignHCenter ),
90 m_textDocument ( NULLPOINTER ),
91 m_identity ( uniqueID ),
93 m_busyResizing ( false ),
96 setTextInteractionFlags( Qt::TextEditable | Qt::TextEditorInteraction );
100 if( m_orientation == Qt::Vertical )
106 m_textDocument =
new QTextDocument(
this );
109 connect( m_textDocument, SIGNAL( contentsChanged() ),
this, SLOT( resize() ) );
112 QTextOption textOption( m_alignment );
113 m_textDocument->setDefaultTextOption( textOption );
114 setDocument( m_textDocument );
121 qreal widthMargin = rect.width() * PERCENTAGE_WIDTH_MARGIN;
122 qreal heightMargin = rect.height() * PERCENTAGE_DEAD_SPACE;
126 if( m_orientation == Qt::Horizontal )
129 m_rectF.setWidth ( rect.width() - widthMargin );
130 m_rectF.setHeight( rect.height() - heightMargin );
132 pos = rect.topLeft();
133 pos.ry() += ( heightMargin / 2 );
136 m_rectF.setHeight( rect.width() - widthMargin );
137 m_rectF.setWidth ( rect.height() - heightMargin );
139 pos = rect.bottomLeft();
140 pos.ry() -= ( heightMargin / 2 );
143 pos.rx() += ( widthMargin / 2 );
146 setTextWidth( m_rectF.width() );
154 m_maxFontSize = font.pointSize();
162 QGraphicsTextItem::setFont( font );
170 QGraphicsTextItem::setPlainText( text );
172 QTextCursor cursor( textCursor() );
173 cursor.movePosition( QTextCursor::EndOfLine );
174 setTextCursor( cursor );
184 QTextOption textOption( m_alignment );
185 m_textDocument->setDefaultTextOption( textOption );
197 void GobChartsTextItem::resize()
200 if( !m_busyResizing && !m_rectF.isNull() )
202 m_busyResizing =
true;
208 QGraphicsTextItem tempItem( toPlainText() );
209 tempItem.setTextWidth( textWidth() );
211 QFont newFont( font() );
212 tempItem.setFont( newFont );
214 QRectF newRect( tempItem.boundingRect() );
215 bool fontTooSmall( WiderThanNew ( m_rectF, newRect ) || HigherThanNew( m_rectF, newRect ) );
218 if( fontTooSmall && ( newFont.pointSize() < m_maxFontSize ) )
221 while( fontTooSmall && !LargerThanNew( m_rectF, newRect ) )
223 newFont.setPointSize( newFont.pointSize() + 1 );
224 tempItem.setFont( newFont );
225 newRect = tempItem.boundingRect();
226 fontTooSmall = ( WiderThanNew ( m_rectF, newRect ) || HigherThanNew( m_rectF, newRect ) );
231 if( LargerThanNew( m_rectF, newRect ) && newFont.pointSize() > 1 )
233 newFont.setPointSize( newFont.pointSize() - 1 );
234 tempItem.setFont( newFont );
235 newRect = tempItem.boundingRect();
239 while( ( LargerThanNew( m_rectF, newRect ) || newFont.pointSize() > m_maxFontSize ) &&
240 newFont.pointSize() > 1 )
242 newFont.setPointSize( newFont.pointSize() - 1 );
243 tempItem.setFont( newFont );
244 newRect = tempItem.boundingRect();
249 m_busyResizing =
false;
257 keyPressEvent( event );
265 QDomElement root = doc.createElement(
"LabelDetails" );
266 doc.appendChild( root );
268 QDomElement fontElement = doc.createElement(
"Font" );
269 fontElement.setAttribute(
"value", font().toString() );
270 root.appendChild( fontElement );
272 QDomElement fontColour = doc.createElement(
"FontColour" );
273 fontColour.setAttribute(
"red", defaultTextColor().red() );
274 fontColour.setAttribute(
"green", defaultTextColor().green() );
275 fontColour.setAttribute(
"blue", defaultTextColor().blue() );
276 root.appendChild( fontColour );
278 QDomElement maxFont = doc.createElement(
"MaxFontSize" );
279 maxFont.setAttribute(
"value", m_maxFontSize );
280 root.appendChild( maxFont );
282 QDomElement text = doc.createElement(
"Text" );
283 text.setAttribute(
"value", toPlainText() );
284 root.appendChild( text );
286 QDomElement align = doc.createElement(
"Alignment" );
288 if( m_alignment == Qt::AlignLeft )
290 align.setAttribute(
"value",
"left" );
292 else if( m_alignment == Qt::AlignRight )
294 align.setAttribute(
"value",
"right" );
298 align.setAttribute(
"value",
"centre" );
301 root.appendChild( align );
303 return doc.toString( 2 );
312 QDomNode labelDetails = node.firstChildElement(
"LabelDetails" );
314 m_maxFontSize = labelDetails.firstChildElement(
"MaxFontSize" ).attribute(
"value" ).toInt();
317 tempFont.fromString( labelDetails.firstChildElement(
"Font" ).attribute(
"value" ) );
320 setDefaultTextColor( QColor( labelDetails.firstChildElement(
"FontColour" ).attribute(
"red" ).toInt(),
321 labelDetails.firstChildElement(
"FontColour" ).attribute(
"green" ).toInt(),
322 labelDetails.firstChildElement(
"FontColour" ).attribute(
"blue" ).toInt() ) );
324 setPlainText( labelDetails.firstChildElement(
"Text" ).attribute(
"value" ) );
326 QString alignMent = labelDetails.firstChildElement(
"Alignment" ).attribute(
"value" );
328 if( alignMent ==
"left" )
330 m_alignment = Qt::AlignLeft;
332 else if( alignMent ==
"right" )
334 m_alignment = Qt::AlignRight;
338 m_alignment = Qt::AlignCenter;
351 QGraphicsTextItem::mousePressEvent( event );