1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
15 Author: Stefan Hundhammer <sh@suse.de>
19 #define y2log_component "qt-ui"
20 #include <ycp/y2log.h>
22 #include <QColorGroup>
26 #include <QVBoxLayout>
32 #include "YQRichText.h"
35 YQRichText::YQRichText( YWidget * parent, const string & text, bool plainTextMode )
36 : QFrame( (QWidget *) parent->widgetRep() )
37 , YRichText( parent, text, plainTextMode )
39 QVBoxLayout* layout = new QVBoxLayout( this );
40 layout->setSpacing( 0 );
45 layout->setMargin( YQWidgetMargin );
47 _textBrowser = new YQTextBrowser( this );
48 YUI_CHECK_NEW( _textBrowser );
49 layout->addWidget( _textBrowser );
51 _textBrowser->installEventFilter( this );
55 _textBrowser->setPlainText(QString::fromUtf8(text.c_str()));
56 _textBrowser->setWordWrapMode( QTextOption::NoWrap );
60 _textBrowser->setHtml(QString::fromUtf8(text.c_str()));
63 // Set the text foreground color to black, regardless of its current
64 // settings - it might be changed if this widget resides in a
65 // warnColor dialog - which we cannot find right now out since our
66 // parent is not set yet :-(
68 QPalette pal( _textBrowser->palette() );
69 pal.setColor( QPalette::Text, Qt::black );
70 pal.setCurrentColorGroup( QPalette::Normal );
71 _textBrowser->setPalette( pal );
75 // Set the text background to a light grey
77 _textBrowser->setPaper( QColor( 234, 234, 234 ) );
81 // Propagate clicks on hyperlinks
83 connect( _textBrowser, SIGNAL( anchorClicked( const QUrl & ) ),
84 this, SLOT ( linkClicked ( const QUrl & ) ) );
88 YQRichText::~YQRichText()
94 void YQRichText::setValue( const string & newText )
96 if ( _textBrowser->horizontalScrollBar() )
97 _textBrowser->horizontalScrollBar()->setValue(0);
99 if ( ! autoScrollDown() && _textBrowser->verticalScrollBar() )
100 _textBrowser->verticalScrollBar()->setValue(0);
102 QString text = fromUTF8( newText );
104 if ( ! plainTextMode() )
106 text.replace( "&product;", YQUI::ui()->productName() );
107 _textBrowser->setHtml( text );
111 _textBrowser->setPlainText( text );
113 YRichText::setValue( newText );
115 if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
116 _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maximum() );
120 void YQRichText::setPlainTextMode( bool newPlainTextMode )
122 YRichText::setPlainTextMode( newPlainTextMode );
124 if ( plainTextMode() )
126 _textBrowser->setWordWrapMode( QTextOption::NoWrap );
131 void YQRichText::setAutoScrollDown( bool newAutoScrollDown )
133 YRichText::setAutoScrollDown( newAutoScrollDown );
135 if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
136 _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maximum() );
140 void YQRichText::linkClicked( const QUrl & url )
142 // y2debug( "Selected hyperlink \"%s\"", (const char *) url );
143 YQUI::ui()->sendEvent( new YMenuEvent(url.toString().toUtf8()) );
147 bool YQRichText::eventFilter( QObject * obj, QEvent * ev )
149 if ( ev->type() == QEvent::KeyPress )
151 QKeyEvent * event = ( QKeyEvent * ) ev;
153 if ( ( event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter ) &&
154 ( event->modifiers() & Qt::NoModifier || event->modifiers() & Qt::KeypadModifier ) &&
157 YQDialog * dia = (YQDialog *) findDialog();
161 ( void ) dia->activateDefaultButton();
167 return QWidget::eventFilter( obj, ev );
171 bool YQRichText::haveHyperLinks()
173 if ( plainTextMode() )
176 return ( _textBrowser->document()->toPlainText().contains( QRegExp( "<a\\s+href\\s*=", Qt::CaseInsensitive ) ) > 0 );
180 int YQRichText::preferredWidth()
182 return shrinkable() ? 10 : 100;
186 int YQRichText::preferredHeight()
188 return shrinkable() ? 10 : 100;
192 void YQRichText::setSize( int newWidth, int newHeight )
194 resize( newWidth, newHeight );
198 void YQRichText::setEnabled( bool enabled )
200 _textBrowser->setEnabled( enabled );
201 YWidget::setEnabled( enabled );
205 bool YQRichText::setKeyboardFocus()
207 _textBrowser->setFocus();
214 #include "YQRichText.moc"