]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQRichText.cc
343f29612a0d8a13791645d6a8912f393f45597e
[duncan/yast2-qt4.git] / src / YQRichText.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQRichText.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17 /-*/
18
19
20 #define y2log_component "qt-ui"
21 #include <ycp/y2log.h>
22
23 #include "qregexp.h"
24 #include "YEvent.h"
25 #include "utf8.h"
26 #include "YQUI.h"
27 #include "YQDialog.h"
28 #include "YQRichText.h"
29
30
31 YQRichText::YQRichText( YWidget * parent, const string & text, bool plainTextMode )
32     : QVBox( (QWidget *) parent->widgetRep() )
33     , YRichText( parent, text, plainTextMode )
34 {
35     setWidgetRep( this );
36
37     setMargin( YQWidgetMargin );
38
39     _textBrowser = new YQTextBrowser( this );
40     YUI_CHECK_NEW( _textBrowser );
41     
42     _textBrowser->setMimeSourceFactory( 0 );
43     _textBrowser->installEventFilter( this );
44
45     if ( plainTextMode )
46     {
47         _textBrowser->setTextFormat( Qt::PlainText );
48         _textBrowser->setWordWrap( QTextEdit::NoWrap );
49     }
50     else
51     {
52         _textBrowser->setTextFormat( Qt::RichText );
53     }
54
55     setValue( text );
56
57
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 :-(
62
63     QPalette pal( _textBrowser->palette() );
64     QColorGroup normalColors( pal.normal() );
65     normalColors.setColor( QColorGroup::Text, black );
66     pal.setNormal( normalColors );
67     _textBrowser->setPalette( pal );
68
69     // Set the text background to a light grey
70
71     _textBrowser->setPaper( QColor( 234, 234, 234 ) );
72
73
74     // Propagate clicks on hyperlinks
75
76     connect( _textBrowser, SIGNAL( linkClicked( const QString & ) ),
77              this,         SLOT  ( linkClicked( const QString & ) ) );
78 }
79
80
81 YQRichText::~YQRichText()
82 {
83     // NOP
84 }
85
86
87 void YQRichText::setValue( const string & newText )
88 {
89     if ( _textBrowser->horizontalScrollBar() )
90         _textBrowser->horizontalScrollBar()->setValue(0);
91
92     if ( ! autoScrollDown() && _textBrowser->verticalScrollBar() )
93         _textBrowser->verticalScrollBar()->setValue(0);
94
95     QString text = fromUTF8( newText );
96
97     if ( ! plainTextMode() )
98         text.replace( "&product;", YQUI::ui()->productName() );
99
100     YRichText::setValue( newText );
101     _textBrowser->setText( text );
102
103     if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
104         _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maxValue() );
105 }
106
107
108 void YQRichText::setPlainTextMode( bool newPlainTextMode )
109 {
110     YRichText::setPlainTextMode( newPlainTextMode );
111
112     if ( plainTextMode() )
113     {
114         _textBrowser->setTextFormat( Qt::PlainText );
115         _textBrowser->setWordWrap( QTextEdit::NoWrap );
116     }
117     else
118     {
119         _textBrowser->setTextFormat( Qt::RichText );
120     }
121 }
122
123
124 void YQRichText::setAutoScrollDown( bool newAutoScrollDown )
125 {
126     YRichText::setAutoScrollDown( newAutoScrollDown );
127
128     if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
129         _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maxValue() );
130 }
131
132
133 void YQRichText::linkClicked( const QString & url )
134 {
135     // y2debug( "Selected hyperlink \"%s\"", (const char *) url );
136     YQUI::ui()->sendEvent( new YMenuEvent( YCPString( (const char *) url ) ) );
137 }
138
139
140 bool YQRichText::eventFilter( QObject * obj, QEvent * ev )
141 {
142     if ( ev->type() == QEvent::KeyPress )
143     {
144         QKeyEvent * event = ( QKeyEvent * ) ev;
145
146         if ( ( event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter ) &&
147              ( event->state() == 0 || event->state() == Qt::Keypad ) &&
148              ! haveHyperLinks() )
149         {
150             YQDialog * dia = (YQDialog *) findDialog();
151
152             if ( dia )
153             {
154                 ( void ) dia->activateDefaultButton();
155                 return true;
156             }
157         }
158     }
159
160     return QWidget::eventFilter( obj, ev );
161 }
162
163
164 bool YQRichText::haveHyperLinks()
165 {
166     if ( plainTextMode() )
167         return false;
168     
169     return ( _textBrowser->text().contains( QRegExp( "<a\\s+href\\s*=", false ) ) > 0 );
170 }
171
172
173 int YQRichText::preferredWidth()
174 {
175     return shrinkable() ? 10 : 100;
176 }
177
178
179 int YQRichText::preferredHeight()
180 {
181     return shrinkable() ? 10 : 100;
182 }
183
184
185 void YQRichText::setSize( int newWidth, int newHeight )
186 {
187     resize( newWidth, newHeight );
188 }
189
190
191 void YQRichText::setEnabled( bool enabled )
192 {
193     _textBrowser->setEnabled( enabled );
194     YWidget::setEnabled( enabled );
195 }
196
197
198 bool YQRichText::setKeyboardFocus()
199 {
200     _textBrowser->setFocus();
201
202     return true;
203 }
204
205
206
207 #include "YQRichText.moc"