1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
15 Author: Stefan Hundhammer <sh@suse.de>
20 #define y2log_component "qt-ui"
21 #include <ycp/y2log.h>
28 #include "YQRichText.h"
31 YQRichText::YQRichText( YWidget * parent, const string & text, bool plainTextMode )
32 : QVBox( (QWidget *) parent->widgetRep() )
33 , YRichText( parent, text, plainTextMode )
37 setMargin( YQWidgetMargin );
39 _textBrowser = new YQTextBrowser( this );
40 YUI_CHECK_NEW( _textBrowser );
42 _textBrowser->setMimeSourceFactory( 0 );
43 _textBrowser->installEventFilter( this );
47 _textBrowser->setTextFormat( Qt::PlainText );
48 _textBrowser->setWordWrap( QTextEdit::NoWrap );
52 _textBrowser->setTextFormat( Qt::RichText );
58 // Set the text foreground color to black, regardless of its current
59 // settings - it might be changed if this widget resides in a
60 // warnColor dialog - which we cannot find right now out since our
61 // parent is not set yet :-(
63 QPalette pal( _textBrowser->palette() );
64 QColorGroup normalColors( pal.normal() );
65 normalColors.setColor( QColorGroup::Text, black );
66 pal.setNormal( normalColors );
67 _textBrowser->setPalette( pal );
69 // Set the text background to a light grey
71 _textBrowser->setPaper( QColor( 234, 234, 234 ) );
74 // Propagate clicks on hyperlinks
76 connect( _textBrowser, SIGNAL( linkClicked( const QString & ) ),
77 this, SLOT ( linkClicked( const QString & ) ) );
81 YQRichText::~YQRichText()
87 void YQRichText::setValue( const string & newText )
89 if ( _textBrowser->horizontalScrollBar() )
90 _textBrowser->horizontalScrollBar()->setValue(0);
92 if ( ! autoScrollDown() && _textBrowser->verticalScrollBar() )
93 _textBrowser->verticalScrollBar()->setValue(0);
95 QString text = fromUTF8( newText );
97 if ( ! plainTextMode() )
98 text.replace( "&product;", YQUI::ui()->productName() );
100 YRichText::setValue( newText );
101 _textBrowser->setText( text );
103 if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
104 _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maxValue() );
108 void YQRichText::setPlainTextMode( bool newPlainTextMode )
110 YRichText::setPlainTextMode( newPlainTextMode );
112 if ( plainTextMode() )
114 _textBrowser->setTextFormat( Qt::PlainText );
115 _textBrowser->setWordWrap( QTextEdit::NoWrap );
119 _textBrowser->setTextFormat( Qt::RichText );
124 void YQRichText::setAutoScrollDown( bool newAutoScrollDown )
126 YRichText::setAutoScrollDown( newAutoScrollDown );
128 if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
129 _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maxValue() );
133 void YQRichText::linkClicked( const QString & url )
135 // y2debug( "Selected hyperlink \"%s\"", (const char *) url );
136 YQUI::ui()->sendEvent( new YMenuEvent( YCPString( (const char *) url ) ) );
140 bool YQRichText::eventFilter( QObject * obj, QEvent * ev )
142 if ( ev->type() == QEvent::KeyPress )
144 QKeyEvent * event = ( QKeyEvent * ) ev;
146 if ( ( event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter ) &&
147 ( event->state() == 0 || event->state() == Qt::Keypad ) &&
150 YQDialog * dia = (YQDialog *) findDialog();
154 ( void ) dia->activateDefaultButton();
160 return QWidget::eventFilter( obj, ev );
164 bool YQRichText::haveHyperLinks()
166 if ( plainTextMode() )
169 return ( _textBrowser->text().contains( QRegExp( "<a\\s+href\\s*=", false ) ) > 0 );
173 int YQRichText::preferredWidth()
175 return shrinkable() ? 10 : 100;
179 int YQRichText::preferredHeight()
181 return shrinkable() ? 10 : 100;
185 void YQRichText::setSize( int newWidth, int newHeight )
187 resize( newWidth, newHeight );
191 void YQRichText::setEnabled( bool enabled )
193 _textBrowser->setEnabled( enabled );
194 YWidget::setEnabled( enabled );
198 bool YQRichText::setKeyboardFocus()
200 _textBrowser->setFocus();
207 #include "YQRichText.moc"