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>
23 #include <qtextbrowser.h>
24 #include <qscrollbar.h>
27 #include <QVBoxLayout>
33 #include "YQRichText.h"
36 YQRichText::YQRichText( YWidget * parent, const string & text, bool plainTextMode )
37 : QFrame( (QWidget *) parent->widgetRep() )
38 , YRichText( parent, text, plainTextMode )
40 QVBoxLayout* layout = new QVBoxLayout( this );
41 layout->setSpacing( 0 );
46 layout->setMargin( YQWidgetMargin );
48 _textBrowser = new YQTextBrowser( this );
49 YUI_CHECK_NEW( _textBrowser );
50 layout->addWidget( _textBrowser );
52 // FIXME _textBrowser->setMimeSourceFactory( 0 );
53 _textBrowser->installEventFilter( this );
57 _textBrowser->setTextFormat( Qt::PlainText );
58 _textBrowser->setWordWrapMode( QTextOption::NoWrap );
62 _textBrowser->setTextFormat( Qt::RichText );
68 // Set the text foreground color to black, regardless of its current
69 // settings - it might be changed if this widget resides in a
70 // warnColor dialog - which we cannot find right now out since our
71 // parent is not set yet :-(
73 QPalette pal( _textBrowser->palette() );
74 QColorGroup normalColors( pal.normal() );
75 normalColors.setColor( QColorGroup::Text, Qt::black );
76 pal.setNormal( normalColors );
77 _textBrowser->setPalette( pal );
81 // Set the text background to a light grey
83 _textBrowser->setPaper( QColor( 234, 234, 234 ) );
87 // Propagate clicks on hyperlinks
89 connect( _textBrowser, SIGNAL( anchorClicked( const QUrl & ) ),
90 this, SLOT ( linkClicked ( const QUrl & ) ) );
94 YQRichText::~YQRichText()
100 void YQRichText::setValue( const string & newText )
102 if ( _textBrowser->horizontalScrollBar() )
103 _textBrowser->horizontalScrollBar()->setValue(0);
105 if ( ! autoScrollDown() && _textBrowser->verticalScrollBar() )
106 _textBrowser->verticalScrollBar()->setValue(0);
108 QString text = fromUTF8( newText );
110 if ( ! plainTextMode() )
111 text.replace( "&product;", YQUI::ui()->productName() );
113 YRichText::setValue( newText );
114 _textBrowser->setText( text );
116 if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
117 _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maxValue() );
121 void YQRichText::setPlainTextMode( bool newPlainTextMode )
123 YRichText::setPlainTextMode( newPlainTextMode );
125 if ( plainTextMode() )
127 _textBrowser->setTextFormat( Qt::PlainText );
128 _textBrowser->setWordWrapMode( QTextOption::NoWrap );
132 _textBrowser->setTextFormat( Qt::RichText );
137 void YQRichText::setAutoScrollDown( bool newAutoScrollDown )
139 YRichText::setAutoScrollDown( newAutoScrollDown );
141 if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
142 _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maxValue() );
146 void YQRichText::linkClicked( const QUrl & url )
148 // y2debug( "Selected hyperlink \"%s\"", (const char *) url.toString() );
149 YQUI::ui()->sendEvent( new YMenuEvent( YCPString( (const char *) url.toString() ) ) );
153 bool YQRichText::eventFilter( QObject * obj, QEvent * ev )
155 if ( ev->type() == QEvent::KeyPress )
157 QKeyEvent * event = ( QKeyEvent * ) ev;
159 if ( ( event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter ) &&
160 ( event->state() == 0 || event->state() == Qt::Keypad ) &&
163 YQDialog * dia = (YQDialog *) findDialog();
167 ( void ) dia->activateDefaultButton();
173 return QWidget::eventFilter( obj, ev );
177 bool YQRichText::haveHyperLinks()
179 if ( plainTextMode() )
182 return ( _textBrowser->text().contains( QRegExp( "<a\\s+href\\s*=", false ) ) > 0 );
186 int YQRichText::preferredWidth()
188 return shrinkable() ? 10 : 100;
192 int YQRichText::preferredHeight()
194 return shrinkable() ? 10 : 100;
198 void YQRichText::setSize( int newWidth, int newHeight )
200 resize( newWidth, newHeight );
204 void YQRichText::setEnabled( bool enabled )
206 _textBrowser->setEnabled( enabled );
207 YWidget::setEnabled( enabled );
211 bool YQRichText::setKeyboardFocus()
213 _textBrowser->setFocus();
220 #include "YQRichText.moc"