1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
15 Author: Stefan Hundhammer <sh@suse.de>
21 #define y2log_component "qt-ui"
22 #include <ycp/y2log.h>
24 #include <QColorGroup>
25 #include <qtextbrowser.h>
26 #include <qscrollbar.h>
29 #include <QVBoxLayout>
35 #include "YQRichText.h"
38 YQRichText::YQRichText( YWidget * parent, const string & text, bool plainTextMode )
39 : QFrame( (QWidget *) parent->widgetRep() )
40 , YRichText( parent, text, plainTextMode )
42 QVBoxLayout* layout = new QVBoxLayout( this );
43 layout->setSpacing( 0 );
48 layout->setMargin( YQWidgetMargin );
50 _textBrowser = new YQTextBrowser( this );
51 YUI_CHECK_NEW( _textBrowser );
52 layout->addWidget( _textBrowser );
54 _textBrowser->installEventFilter( this );
58 _textBrowser->setTextFormat( Qt::PlainText );
59 _textBrowser->setWordWrapMode( QTextOption::NoWrap );
63 _textBrowser->setTextFormat( Qt::RichText );
69 // Set the text foreground color to black, regardless of its current
70 // settings - it might be changed if this widget resides in a
71 // warnColor dialog - which we cannot find right now out since our
72 // parent is not set yet :-(
74 QPalette pal( _textBrowser->palette() );
75 QColorGroup normalColors( pal.normal() );
76 normalColors.setColor( QColorGroup::Text, Qt::black );
77 pal.setNormal( normalColors );
78 _textBrowser->setPalette( pal );
82 // Set the text background to a light grey
84 _textBrowser->setPaper( QColor( 234, 234, 234 ) );
88 // Propagate clicks on hyperlinks
90 connect( _textBrowser, SIGNAL( anchorClicked( const QUrl & ) ),
91 this, SLOT ( linkClicked ( const QUrl & ) ) );
95 YQRichText::~YQRichText()
101 void YQRichText::setValue( const string & newText )
103 if ( _textBrowser->horizontalScrollBar() )
104 _textBrowser->horizontalScrollBar()->setValue(0);
106 if ( ! autoScrollDown() && _textBrowser->verticalScrollBar() )
107 _textBrowser->verticalScrollBar()->setValue(0);
109 QString text = fromUTF8( newText );
111 if ( ! plainTextMode() )
112 text.replace( "&product;", YQUI::ui()->productName() );
114 YRichText::setValue( newText );
115 _textBrowser->setText( text );
117 if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
118 _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maxValue() );
122 void YQRichText::setPlainTextMode( bool newPlainTextMode )
124 YRichText::setPlainTextMode( newPlainTextMode );
126 if ( plainTextMode() )
128 _textBrowser->setTextFormat( Qt::PlainText );
129 _textBrowser->setWordWrapMode( QTextOption::NoWrap );
133 _textBrowser->setTextFormat( Qt::RichText );
138 void YQRichText::setAutoScrollDown( bool newAutoScrollDown )
140 YRichText::setAutoScrollDown( newAutoScrollDown );
142 if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
143 _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maxValue() );
147 void YQRichText::linkClicked( const QUrl & url )
149 // y2debug( "Selected hyperlink \"%s\"", (const char *) url );
150 YQUI::ui()->sendEvent( new YMenuEvent( toUTF8( url ) ) );
154 bool YQRichText::eventFilter( QObject * obj, QEvent * ev )
156 if ( ev->type() == QEvent::KeyPress )
158 QKeyEvent * event = ( QKeyEvent * ) ev;
160 if ( ( event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter ) &&
161 ( event->state() == 0 || event->state() == Qt::Keypad ) &&
164 YQDialog * dia = (YQDialog *) findDialog();
168 ( void ) dia->activateDefaultButton();
174 return QWidget::eventFilter( obj, ev );
178 bool YQRichText::haveHyperLinks()
180 if ( plainTextMode() )
183 return ( _textBrowser->text().contains( QRegExp( "<a\\s+href\\s*=", false ) ) > 0 );
187 int YQRichText::preferredWidth()
189 return shrinkable() ? 10 : 100;
193 int YQRichText::preferredHeight()
195 return shrinkable() ? 10 : 100;
199 void YQRichText::setSize( int newWidth, int newHeight )
201 resize( newWidth, newHeight );
205 void YQRichText::setEnabled( bool enabled )
207 _textBrowser->setEnabled( enabled );
208 YWidget::setEnabled( enabled );
212 bool YQRichText::setKeyboardFocus()
214 _textBrowser->setFocus();
221 #include "YQRichText.moc"