]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQRichText.cc
picking up branches/tmp/sh/qt4-port/, merging it with trunk
[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 <qtextbrowser.h>
24 #include <qscrollbar.h>
25 #include <qregexp.h>
26 #include <qevent.h>
27 #include <QVBoxLayout>
28
29 #include "YEvent.h"
30 #include "utf8.h"
31 #include "YQUI.h"
32 #include "YQDialog.h"
33 #include "YQRichText.h"
34
35
36 YQRichText::YQRichText( YWidget * parent, const string & text, bool plainTextMode )
37     : QFrame( (QWidget *) parent->widgetRep() )
38     , YRichText( parent, text, plainTextMode )
39 {
40     QVBoxLayout* layout = new QVBoxLayout( this );
41     layout->setSpacing( 0 );
42     setLayout( layout );
43
44     setWidgetRep( this );
45
46     layout->setMargin( YQWidgetMargin );
47
48     _textBrowser = new YQTextBrowser( this );
49     YUI_CHECK_NEW( _textBrowser );
50     layout->addWidget( _textBrowser );
51
52     // FIXME _textBrowser->setMimeSourceFactory( 0 );
53     _textBrowser->installEventFilter( this );
54
55     if ( plainTextMode )
56     {
57         _textBrowser->setTextFormat( Qt::PlainText );
58         _textBrowser->setWordWrapMode( QTextOption::NoWrap );
59     }
60     else
61     {
62         _textBrowser->setTextFormat( Qt::RichText );
63     }
64
65     setValue( text );
66
67
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 :-(
72
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 );
78
79
80 #ifdef FIXME
81     // Set the text background to a light grey
82
83     _textBrowser->setPaper( QColor( 234, 234, 234 ) );
84 #endif
85
86
87     // Propagate clicks on hyperlinks
88
89     connect( _textBrowser, SIGNAL( anchorClicked( const QUrl & ) ),
90              this,         SLOT  ( linkClicked  ( const QUrl & ) ) );
91 }
92
93
94 YQRichText::~YQRichText()
95 {
96     // NOP
97 }
98
99
100 void YQRichText::setValue( const string & newText )
101 {
102     if ( _textBrowser->horizontalScrollBar() )
103         _textBrowser->horizontalScrollBar()->setValue(0);
104
105     if ( ! autoScrollDown() && _textBrowser->verticalScrollBar() )
106         _textBrowser->verticalScrollBar()->setValue(0);
107
108     QString text = fromUTF8( newText );
109
110     if ( ! plainTextMode() )
111         text.replace( "&product;", YQUI::ui()->productName() );
112
113     YRichText::setValue( newText );
114     _textBrowser->setText( text );
115
116     if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
117         _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maxValue() );
118 }
119
120
121 void YQRichText::setPlainTextMode( bool newPlainTextMode )
122 {
123     YRichText::setPlainTextMode( newPlainTextMode );
124
125     if ( plainTextMode() )
126     {
127         _textBrowser->setTextFormat( Qt::PlainText );
128         _textBrowser->setWordWrapMode( QTextOption::NoWrap );
129     }
130     else
131     {
132         _textBrowser->setTextFormat( Qt::RichText );
133     }
134 }
135
136
137 void YQRichText::setAutoScrollDown( bool newAutoScrollDown )
138 {
139     YRichText::setAutoScrollDown( newAutoScrollDown );
140
141     if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
142         _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maxValue() );
143 }
144
145
146 void YQRichText::linkClicked( const QUrl & url )
147 {
148     // y2debug( "Selected hyperlink \"%s\"", (const char *) url.toString() );
149     YQUI::ui()->sendEvent( new YMenuEvent( YCPString( (const char *) url.toString() ) ) );
150 }
151
152
153 bool YQRichText::eventFilter( QObject * obj, QEvent * ev )
154 {
155     if ( ev->type() == QEvent::KeyPress )
156     {
157         QKeyEvent * event = ( QKeyEvent * ) ev;
158
159         if ( ( event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter ) &&
160              ( event->state() == 0 || event->state() == Qt::Keypad ) &&
161              ! haveHyperLinks() )
162         {
163             YQDialog * dia = (YQDialog *) findDialog();
164
165             if ( dia )
166             {
167                 ( void ) dia->activateDefaultButton();
168                 return true;
169             }
170         }
171     }
172
173     return QWidget::eventFilter( obj, ev );
174 }
175
176
177 bool YQRichText::haveHyperLinks()
178 {
179     if ( plainTextMode() )
180         return false;
181
182     return ( _textBrowser->text().contains( QRegExp( "<a\\s+href\\s*=", false ) ) > 0 );
183 }
184
185
186 int YQRichText::preferredWidth()
187 {
188     return shrinkable() ? 10 : 100;
189 }
190
191
192 int YQRichText::preferredHeight()
193 {
194     return shrinkable() ? 10 : 100;
195 }
196
197
198 void YQRichText::setSize( int newWidth, int newHeight )
199 {
200     resize( newWidth, newHeight );
201 }
202
203
204 void YQRichText::setEnabled( bool enabled )
205 {
206     _textBrowser->setEnabled( enabled );
207     YWidget::setEnabled( enabled );
208 }
209
210
211 bool YQRichText::setKeyboardFocus()
212 {
213     _textBrowser->setFocus();
214
215     return true;
216 }
217
218
219
220 #include "YQRichText.moc"