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 _textBrowser->installEventFilter( this );
56 _textBrowser->setTextFormat( Qt::PlainText );
57 _textBrowser->setWordWrapMode( QTextOption::NoWrap );
61 _textBrowser->setTextFormat( Qt::RichText );
67 // Set the text foreground color to black, regardless of its current
68 // settings - it might be changed if this widget resides in a
69 // warnColor dialog - which we cannot find right now out since our
70 // parent is not set yet :-(
72 QPalette pal( _textBrowser->palette() );
73 QColorGroup normalColors( pal.normal() );
74 normalColors.setColor( QColorGroup::Text, Qt::black );
75 pal.setNormal( normalColors );
76 _textBrowser->setPalette( pal );
80 // Set the text background to a light grey
82 _textBrowser->setPaper( QColor( 234, 234, 234 ) );
86 // Propagate clicks on hyperlinks
88 connect( _textBrowser, SIGNAL( anchorClicked( const QUrl & ) ),
89 this, SLOT ( linkClicked ( const QUrl & ) ) );
93 YQRichText::~YQRichText()
99 void YQRichText::setValue( const string & newText )
101 if ( _textBrowser->horizontalScrollBar() )
102 _textBrowser->horizontalScrollBar()->setValue(0);
104 if ( ! autoScrollDown() && _textBrowser->verticalScrollBar() )
105 _textBrowser->verticalScrollBar()->setValue(0);
107 QString text = fromUTF8( newText );
109 if ( ! plainTextMode() )
110 text.replace( "&product;", YQUI::ui()->productName() );
112 YRichText::setValue( newText );
113 _textBrowser->setText( text );
115 if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
116 _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maxValue() );
120 void YQRichText::setPlainTextMode( bool newPlainTextMode )
122 YRichText::setPlainTextMode( newPlainTextMode );
124 if ( plainTextMode() )
126 _textBrowser->setTextFormat( Qt::PlainText );
127 _textBrowser->setWordWrapMode( QTextOption::NoWrap );
131 _textBrowser->setTextFormat( Qt::RichText );
136 void YQRichText::setAutoScrollDown( bool newAutoScrollDown )
138 YRichText::setAutoScrollDown( newAutoScrollDown );
140 if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
141 _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maxValue() );
145 void YQRichText::linkClicked( const QUrl & url )
147 // y2debug( "Selected hyperlink \"%s\"", (const char *) url );
148 YQUI::ui()->sendEvent( new YMenuEvent( toUTF8( url ) ) );
152 bool YQRichText::eventFilter( QObject * obj, QEvent * ev )
154 if ( ev->type() == QEvent::KeyPress )
156 QKeyEvent * event = ( QKeyEvent * ) ev;
158 if ( ( event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter ) &&
159 ( event->state() == 0 || event->state() == Qt::Keypad ) &&
162 YQDialog * dia = (YQDialog *) findDialog();
166 ( void ) dia->activateDefaultButton();
172 return QWidget::eventFilter( obj, ev );
176 bool YQRichText::haveHyperLinks()
178 if ( plainTextMode() )
181 return ( _textBrowser->text().contains( QRegExp( "<a\\s+href\\s*=", false ) ) > 0 );
185 int YQRichText::preferredWidth()
187 return shrinkable() ? 10 : 100;
191 int YQRichText::preferredHeight()
193 return shrinkable() ? 10 : 100;
197 void YQRichText::setSize( int newWidth, int newHeight )
199 resize( newWidth, newHeight );
203 void YQRichText::setEnabled( bool enabled )
205 _textBrowser->setEnabled( enabled );
206 YWidget::setEnabled( enabled );
210 bool YQRichText::setKeyboardFocus()
212 _textBrowser->setFocus();
219 #include "YQRichText.moc"