libyui-qt  2.43.5
 All Classes Functions Variables
YQRichText.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YQRichText.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #define YUILogComponent "qt-ui"
26 #include <yui/YUILog.h>
27 
28 #include <QColorGroup>
29 #include <QScrollBar>
30 #include <QRegExp>
31 #include <QDebug>
32 #include <QKeyEvent>
33 #include <QVBoxLayout>
34 
35 #include <yui/YApplication.h>
36 #include <yui/YEvent.h>
37 #include "utf8.h"
38 #include "QY2Styler.h"
39 #include "YQUI.h"
40 #include "YQDialog.h"
41 #include "YQRichText.h"
42 
43 static const char *colors[] = { "red", "blue", "green", 0};
44 
45 YQRichText::YQRichText( YWidget * parent, const std::string & text, bool plainTextMode )
46  : QFrame( (QWidget *) parent->widgetRep() )
47  , YRichText( parent, text, plainTextMode )
48  , _colors_specified( 0 )
49 {
50  QVBoxLayout* layout = new QVBoxLayout( this );
51  layout->setSpacing( 0 );
52  setLayout( layout );
53 
54  setWidgetRep( this );
55 
56  layout->setMargin( YQWidgetMargin );
57 
58  _textBrowser = new YQTextBrowser( this );
59  YUI_CHECK_NEW( _textBrowser );
60  layout->addWidget( _textBrowser );
61 
62  _textBrowser->installEventFilter( this );
63 
64  if ( plainTextMode )
65  {
66  _textBrowser->setWordWrapMode( QTextOption::NoWrap );
67  }
68  else
69  {
70  QString style = "\n" + QY2Styler::styler()->textStyle();
71  size_t ccolors = sizeof( colors ) / sizeof( char* ) - 1;
72  _colors_specified = new bool[ccolors];
73  for ( size_t i = 0; i < ccolors; ++i )
74  {
75  _colors_specified[i] = false;
76  char buffer[20];
77  sprintf( buffer, "\n.%s ", colors[i] );
78  if ( style.contains( buffer ) )
79  _colors_specified[i] = true;
80  }
81  _textBrowser->document()->setDefaultStyleSheet( style );
82  }
83 
84  setValue( text );
85 
86  // Propagate clicks on hyperlinks
87 
88  connect( _textBrowser, SIGNAL( anchorClicked( const QUrl & ) ),
89  this, SLOT ( linkClicked ( const QUrl & ) ) );
90 }
91 
92 
94 {
95  // NOP
96 }
97 
98 
99 void YQRichText::setValue( const std::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  {
111  for ( int counter = 0; colors[counter]; counter++ )
112  {
113  if ( !_colors_specified[counter] ) continue;
114  text.replace( QString( "color=%1" ).arg( colors[counter] ), QString( "class=\"%1\"" ).arg( colors[counter] ) );
115  text.replace( QString( "color=\"%1\"" ).arg( colors[counter] ), QString( "class=\"%1\"" ).arg( colors[counter] ));
116  }
117  text.replace( "&product;", fromUTF8( YUI::app()->productName() ) );
118  _textBrowser->setHtml( text );
119  }
120  else
121  {
122  _textBrowser->setPlainText( text );
123  }
124  YRichText::setValue( newText );
125 
126  if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
127  _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maximum() );
128 }
129 
130 
131 void YQRichText::setPlainTextMode( bool newPlainTextMode )
132 {
133  YRichText::setPlainTextMode( newPlainTextMode );
134 
135  if ( plainTextMode() )
136  {
137  _textBrowser->setWordWrapMode( QTextOption::NoWrap );
138  }
139 }
140 
141 
142 void YQRichText::setAutoScrollDown( bool newAutoScrollDown )
143 {
144  YRichText::setAutoScrollDown( newAutoScrollDown );
145 
146  if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
147  _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maximum() );
148 }
149 
150 
151 void YQRichText::linkClicked( const QUrl & url )
152 {
153  // yuiDebug() << "Selected hyperlink \"" << url.toString() << "\" << std::endl;
154  YQUI::ui()->sendEvent( new YMenuEvent( url.toString().toUtf8()) );
155 }
156 
157 
158 bool YQRichText::eventFilter( QObject * obj, QEvent * ev )
159 {
160  if ( ev->type() == QEvent::KeyPress )
161  {
162  QKeyEvent * event = ( QKeyEvent * ) ev;
163 
164  if ( ( event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter ) &&
165  ( event->modifiers() & Qt::NoModifier || event->modifiers() & Qt::KeypadModifier ) &&
166  ! haveHyperLinks() )
167  {
168  YQDialog * dia = (YQDialog *) findDialog();
169 
170  if ( dia )
171  {
172  ( void ) dia->activateDefaultButton();
173  return true;
174  }
175  }
176  }
177 
178  return QWidget::eventFilter( obj, ev );
179 }
180 
181 
183 {
184  if ( plainTextMode() )
185  return false;
186 
187  return ( _textBrowser->document()->toPlainText().contains( QRegExp( "<a\\s+href\\s*=", Qt::CaseInsensitive ) ) > 0 );
188 }
189 
190 
192 {
193  return shrinkable() ? 10 : 100;
194 }
195 
196 
198 {
199  return shrinkable() ? 10 : 100;
200 }
201 
202 
203 void YQRichText::setSize( int newWidth, int newHeight )
204 {
205  resize( newWidth, newHeight );
206 }
207 
208 
209 void YQRichText::setEnabled( bool enabled )
210 {
211  _textBrowser->setEnabled( enabled );
212  YWidget::setEnabled( enabled );
213 }
214 
215 
217 {
218  _textBrowser->setFocus();
219 
220  return true;
221 }
222 
223 void YQTextBrowser::setSource( const QUrl & name )
224 {
225  // scroll to link if it's available in the current document
226  // but prevent loading empty pages
227 
228  if ( name.toString().startsWith("#") )
229  scrollToAnchor( name.toString().mid(1) );
230 
231 }
232 
233 
234 
235 #include "YQRichText.moc"