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