25 #define YUILogComponent "qt-ui"
26 #include <yui/YUILog.h>
32 #include <QVBoxLayout>
34 #include <yui/YApplication.h>
35 #include <yui/YEvent.h>
37 #include "QY2Styler.h"
40 #include "YQRichText.h"
42 static const char *colors[] = {
"red",
"blue",
"green", 0};
45 : QFrame( (QWidget *) parent->widgetRep() )
46 , YRichText( parent, text, plainTextMode )
47 , _colors_specified( 0 )
49 QVBoxLayout* layout =
new QVBoxLayout(
this );
50 layout->setSpacing( 0 );
55 layout->setMargin( YQWidgetMargin );
58 YUI_CHECK_NEW( _textBrowser );
59 layout->addWidget( _textBrowser );
61 _textBrowser->installEventFilter(
this );
65 _textBrowser->setWordWrapMode( QTextOption::NoWrap );
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 )
74 _colors_specified[i] =
false;
76 sprintf( buffer,
"\n.%s ", colors[i] );
77 if ( style.contains( buffer ) )
78 _colors_specified[i] =
true;
80 _textBrowser->document()->setDefaultStyleSheet( style );
87 connect( _textBrowser, &pclass(_textBrowser)::anchorClicked,
100 if ( _textBrowser->horizontalScrollBar() )
101 _textBrowser->horizontalScrollBar()->setValue(0);
103 if ( ! autoScrollDown() && _textBrowser->verticalScrollBar() )
104 _textBrowser->verticalScrollBar()->setValue(0);
106 QString text = fromUTF8( newText );
108 if ( ! plainTextMode() )
110 for (
int counter = 0; colors[counter]; counter++ )
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] ));
116 text.replace(
"&product;", fromUTF8( YUI::app()->productName() ) );
117 _textBrowser->setHtml( text );
121 _textBrowser->setPlainText( text );
123 YRichText::setValue( newText );
125 if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
126 _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maximum() );
132 YRichText::setPlainTextMode( newPlainTextMode );
134 if ( plainTextMode() )
136 _textBrowser->setWordWrapMode( QTextOption::NoWrap );
143 YRichText::setAutoScrollDown( newAutoScrollDown );
145 if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
146 _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maximum() );
159 if ( ev->type() == QEvent::KeyPress )
161 QKeyEvent *
event = ( QKeyEvent * ) ev;
163 if ( ( event->key() == Qt::Key_Return ||
event->key() == Qt::Key_Enter ) &&
164 ( event->modifiers() & Qt::NoModifier ||
event->modifiers() & Qt::KeypadModifier ) &&
177 return QWidget::eventFilter( obj, ev );
183 if ( plainTextMode() )
186 return ( _textBrowser->document()->toPlainText().contains( QRegExp(
"<a\\s+href\\s*=", Qt::CaseInsensitive ) ) > 0 );
192 return shrinkable() ? 10 : 100;
198 return shrinkable() ? 10 : 100;
204 resize( newWidth, newHeight );
210 _textBrowser->setEnabled( enabled );
211 YWidget::setEnabled( enabled );
217 _textBrowser->setFocus();
227 if ( name.toString().startsWith(
"#") )
228 scrollToAnchor( name.toString().mid(1) );
234 #include "YQRichText.moc"
virtual bool setKeyboardFocus()
Accept the keyboard focus.
YQRichText(YWidget *parent, const std::string &text, bool plainTextMode=false)
Constructor.
virtual bool eventFilter(QObject *obj, QEvent *ev)
Event filter.
bool haveHyperLinks()
Returns 'true' if the current text of this RichText widget contains hyperlinks.
virtual ~YQRichText()
Destructor.
Helper class - needed to have the benefits of both QVBox as the base class for YQRichText so uniform ...
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
virtual int preferredWidth()
Preferred width of the widget.
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...
virtual void setPlainTextMode(bool on=true)
Set this RichText widget's "plain text" mode on or off.
virtual void setSource(const QUrl &name)
Get the document pointed to by a hyperlink.
virtual void setAutoScrollDown(bool on=true)
Set this RichText widget's "auto scroll down" mode on or off.
virtual void setValue(const std::string &newValue)
Change the text content of the RichText widget.
bool activateDefaultButton(bool warn=true)
Activate (i.e.
virtual int preferredHeight()
Preferred height of the widget.
virtual void setEnabled(bool enabled)
Set enabled/disabled state.
void linkClicked(const QUrl &url)
Notification that a hyperlink is clicked.
static YQUI * ui()
Access the global Qt-UI.