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