1
/***************************************************************************
2
* Copyright (C) 2017 by santiago González *
3
* santigoro@gmail.com *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 3 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, see <http://www.gnu.org/licenses/>. *
18
***************************************************************************/
20
#include "textcomponent.h"
23
static const char* TextComponent_properties[] = {
24
QT_TRANSLATE_NOOP("App::Property","Text"),
25
QT_TRANSLATE_NOOP("App::Property","Font"),
26
QT_TRANSLATE_NOOP("App::Property","Font Size"),
27
QT_TRANSLATE_NOOP("App::Property","Fixed Width"),
28
QT_TRANSLATE_NOOP("App::Property","Margin"),
29
QT_TRANSLATE_NOOP("App::Property","Border"),
30
QT_TRANSLATE_NOOP("App::Property","Opacity")
33
Component* TextComponent::construct( QObject* parent, QString type, QString id )
35
return new TextComponent( parent, type, id );
38
LibraryItem* TextComponent::libraryItem()
40
return new LibraryItem(
45
TextComponent::construct );
48
TextComponent::TextComponent( QObject* parent, QString type, QString id )
49
: Component( parent, type, id )
51
Q_UNUSED( TextComponent_properties );
57
m_color = QColor( 255, 255, 220 );
58
m_font = "Helvetica [Cronyx]";
60
QFont sansFont( m_font, 10 );
61
#if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
62
sansFont.setWeight( QFont::Medium );
64
sansFont.setWeight( QFont::Normal);
66
sansFont.setFixedPitch(true);
68
m_text = new QGraphicsTextItem( this );
69
//m_text->setTextInteractionFlags( Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard | Qt::TextEditable);
70
m_text->setTextInteractionFlags( 0 );
71
m_text->setTextWidth( 90 );
72
m_text->setFont( sansFont );
73
m_text->setPlainText("... TEXT ...");
74
m_text->setPos( 0, 0 );
75
m_text->document()->setTextWidth(-1);
76
m_text->setDefaultTextColor( Qt::darkBlue );
77
m_text->setCursor( Qt::OpenHandCursor );
78
m_text->installEventFilter( this );
82
updateGeometry( 0, 0, 0 );
86
connect(m_text->document(), SIGNAL( contentsChange(int, int, int )),
87
this, SLOT( updateGeometry(int, int, int )), Qt::UniqueConnection );
89
TextComponent::~TextComponent(){}
91
void TextComponent::updateGeometry(int, int, int)
93
m_text->document()->setTextWidth(-1);
95
int margin = m_margin;
97
m_area = QRect( -margin, -margin, m_text->boundingRect().width()+margin*2, m_text->boundingRect().height()+margin*2 );
99
Circuit::self()->update();
102
int TextComponent::margin() { return m_margin; }
104
void TextComponent::setMargin( int margin )
106
if( margin < 2 ) margin = 2;
108
updateGeometry( 0, 0, 0 );
111
bool TextComponent::fixedW() { return m_fixedW; }
113
void TextComponent::setFixedW( bool fixedW )
117
QFont font = m_text->font();
118
font.setFixedPitch( fixedW );
119
m_text->setFont( font );
120
updateGeometry( 0, 0, 0 );
123
QString TextComponent::getFont()
128
void TextComponent::setFont( QString font )
130
if( font == "" ) return;
133
QFont Tfont = m_text->font();
134
Tfont.setFamily( font );
135
m_text->setFont( Tfont );
136
updateGeometry( 0, 0, 0 );
139
int TextComponent::fontSize()
144
void TextComponent::setFontSize( int size )
146
if( size < 1 ) return;
149
QFont font = m_text->font();
150
font.setPixelSize( size );
151
m_text->setFont( font );
152
updateGeometry( 0, 0, 0 );
155
int TextComponent::border() { return m_border; }
156
void TextComponent::setBorder( int border ) { m_border = border; }
158
QString TextComponent::getText() { return m_text->toPlainText(); }
159
void TextComponent::setText( QString text ) { m_text->document()->setPlainText( text ); }
161
qreal TextComponent::opac()
166
void TextComponent::setOpac( qreal op )
172
void TextComponent::mouseDoubleClickEvent( QGraphicsSceneMouseEvent* event )
174
if( event->button() == Qt::LeftButton )
176
m_text->setTextInteractionFlags( Qt::TextEditorInteraction | Qt::TextBrowserInteraction ); //( Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard | Qt::TextEditable);
177
m_text->setCursor( Qt::IBeamCursor );
179
setSelected( false );
183
bool TextComponent::eventFilter( QObject* object, QEvent* event )
185
if( event->type() == QEvent::FocusIn )
187
if( object == m_text)
189
Circuit::self()->deselectAll();
195
void TextComponent::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *widget )
197
if( !m_text->hasFocus() )
199
m_text->setTextInteractionFlags( 0 );
200
m_text->setCursor( Qt::OpenHandCursor );
202
p->setOpacity( m_opac );
204
Component::paint( p, option, widget );
206
QPen pen( QColor( 0, 0, 0 ), m_border, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
209
if( m_border >0 ) p->drawRect( m_area );
210
else p->fillRect( m_area, p->brush() );
215
#include "moc_textcomponent.cpp"