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