libyui-qt  2.47.1.1
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 <QScrollBar>
29 #include <QRegExp>
30 #include <QDebug>
31 #include <QKeyEvent>
32 #include <QVBoxLayout>
33 
34 #include <yui/YApplication.h>
35 #include <yui/YEvent.h>
36 #include "utf8.h"
37 #include "QY2Styler.h"
38 #include "YQUI.h"
39 #include "YQDialog.h"
40 #include "YQRichText.h"
41 
42 static const char *colors[] = { "red", "blue", "green", 0};
43 
44 YQRichText::YQRichText( YWidget * parent, const std::string & text, bool plainTextMode )
45  : QFrame( (QWidget *) parent->widgetRep() )
46  , YRichText( parent, text, plainTextMode )
47  , _colors_specified( 0 )
48 {
49  QVBoxLayout* layout = new QVBoxLayout( this );
50  layout->setSpacing( 0 );
51  setLayout( layout );
52 
53  setWidgetRep( this );
54 
55  layout->setMargin( YQWidgetMargin );
56 
57  _textBrowser = new YQTextBrowser( this );
58  YUI_CHECK_NEW( _textBrowser );
59  layout->addWidget( _textBrowser );
60 
61  _textBrowser->installEventFilter( this );
62 
63  if ( plainTextMode )
64  {
65  _textBrowser->setWordWrapMode( QTextOption::NoWrap );
66  }
67  else
68  {
69  QString style = "\n" + QY2Styler::styler()->textStyle();
70  size_t ccolors = sizeof( colors ) / sizeof( char* ) - 1;
71  _colors_specified = new bool[ccolors];
72  for ( size_t i = 0; i < ccolors; ++i )
73  {
74  _colors_specified[i] = false;
75  char buffer[20];
76  sprintf( buffer, "\n.%s ", colors[i] );
77  if ( style.contains( buffer ) )
78  _colors_specified[i] = true;
79  }
80  _textBrowser->document()->setDefaultStyleSheet( style );
81  }
82 
83  setValue( text );
84 
85  // Propagate clicks on hyperlinks
86 
87  connect( _textBrowser, &pclass(_textBrowser)::anchorClicked,
88  this, &pclass(this)::linkClicked );
89 }
90 
91 
93 {
94  // NOP
95 }
96 
97 
98 void YQRichText::setValue( const std::string & newText )
99 {
100  if ( _textBrowser->horizontalScrollBar() )
101  _textBrowser->horizontalScrollBar()->setValue(0);
102 
103  if ( ! autoScrollDown() && _textBrowser->verticalScrollBar() )
104  _textBrowser->verticalScrollBar()->setValue(0);
105 
106  QString text = fromUTF8( newText );
107 
108  if ( ! plainTextMode() )
109  {
110  for ( int counter = 0; colors[counter]; counter++ )
111  {
112  if ( !_colors_specified[counter] ) continue;
113  text.replace( QString( "color=%1" ).arg( colors[counter] ), QString( "class=\"%1\"" ).arg( colors[counter] ) );
114  text.replace( QString( "color=\"%1\"" ).arg( colors[counter] ), QString( "class=\"%1\"" ).arg( colors[counter] ));
115  }
116  text.replace( "&product;", fromUTF8( YUI::app()->productName() ) );
117  _textBrowser->setHtml( text );
118  }
119  else
120  {
121  _textBrowser->setPlainText( text );
122  }
123  YRichText::setValue( newText );
124 
125  if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
126  _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maximum() );
127 }
128 
129 
130 void YQRichText::setPlainTextMode( bool newPlainTextMode )
131 {
132  YRichText::setPlainTextMode( newPlainTextMode );
133 
134  if ( plainTextMode() )
135  {
136  _textBrowser->setWordWrapMode( QTextOption::NoWrap );
137  }
138 }
139 
140 
141 void YQRichText::setAutoScrollDown( bool newAutoScrollDown )
142 {
143  YRichText::setAutoScrollDown( newAutoScrollDown );
144 
145  if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
146  _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maximum() );
147 }
148 
149 
150 void YQRichText::linkClicked( const QUrl & url )
151 {
152  // yuiDebug() << "Selected hyperlink \"" << url.toString() << "\" << std::endl;
153  YQUI::ui()->sendEvent( new YMenuEvent( url.toString().toUtf8()) );
154 }
155 
156 
157 bool YQRichText::eventFilter( QObject * obj, QEvent * ev )
158 {
159  if ( ev->type() == QEvent::KeyPress )
160  {
161  QKeyEvent * event = ( QKeyEvent * ) ev;
162 
163  if ( ( event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter ) &&
164  ( event->modifiers() & Qt::NoModifier || event->modifiers() & Qt::KeypadModifier ) &&
165  ! haveHyperLinks() )
166  {
167  YQDialog * dia = (YQDialog *) findDialog();
168 
169  if ( dia )
170  {
171  ( void ) dia->activateDefaultButton();
172  return true;
173  }
174  }
175  }
176 
177  return QWidget::eventFilter( obj, ev );
178 }
179 
180 
182 {
183  if ( plainTextMode() )
184  return false;
185 
186  return ( _textBrowser->document()->toPlainText().contains( QRegExp( "<a\\s+href\\s*=", Qt::CaseInsensitive ) ) > 0 );
187 }
188 
189 
191 {
192  return shrinkable() ? 10 : 100;
193 }
194 
195 
197 {
198  return shrinkable() ? 10 : 100;
199 }
200 
201 
202 void YQRichText::setSize( int newWidth, int newHeight )
203 {
204  resize( newWidth, newHeight );
205 }
206 
207 
208 void YQRichText::setEnabled( bool enabled )
209 {
210  _textBrowser->setEnabled( enabled );
211  YWidget::setEnabled( enabled );
212 }
213 
214 
216 {
217  _textBrowser->setFocus();
218 
219  return true;
220 }
221 
222 void YQTextBrowser::setSource( const QUrl & name )
223 {
224  // scroll to link if it's available in the current document
225  // but prevent loading empty pages
226 
227  if ( name.toString().startsWith("#") )
228  scrollToAnchor( name.toString().mid(1) );
229 
230 }
231 
232 
233 
234 #include "YQRichText.moc"
virtual bool setKeyboardFocus()
Accept the keyboard focus.
Definition: YQRichText.cc:215
YQRichText(YWidget *parent, const std::string &text, bool plainTextMode=false)
Constructor.
Definition: YQRichText.cc:44
virtual bool eventFilter(QObject *obj, QEvent *ev)
Event filter.
Definition: YQRichText.cc:157
bool haveHyperLinks()
Returns &#39;true&#39; if the current text of this RichText widget contains hyperlinks.
Definition: YQRichText.cc:181
virtual ~YQRichText()
Destructor.
Definition: YQRichText.cc:92
Helper class - needed to have the benefits of both QVBox as the base class for YQRichText so uniform ...
Definition: YQRichText.h:156
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
Definition: YQRichText.cc:202
virtual int preferredWidth()
Preferred width of the widget.
Definition: YQRichText.cc:190
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...
Definition: YQUI.cc:494
virtual void setPlainTextMode(bool on=true)
Set this RichText widget&#39;s "plain text" mode on or off.
Definition: YQRichText.cc:130
virtual void setSource(const QUrl &name)
Get the document pointed to by a hyperlink.
Definition: YQRichText.cc:222
virtual void setAutoScrollDown(bool on=true)
Set this RichText widget&#39;s "auto scroll down" mode on or off.
Definition: YQRichText.cc:141
virtual void setValue(const std::string &newValue)
Change the text content of the RichText widget.
Definition: YQRichText.cc:98
bool activateDefaultButton(bool warn=true)
Activate (i.e.
Definition: YQDialog.cc:535
virtual int preferredHeight()
Preferred height of the widget.
Definition: YQRichText.cc:196
virtual void setEnabled(bool enabled)
Set enabled/disabled state.
Definition: YQRichText.cc:208
void linkClicked(const QUrl &url)
Notification that a hyperlink is clicked.
Definition: YQRichText.cc:150
static YQUI * ui()
Access the global Qt-UI.
Definition: YQUI.h:80