26 #define YUILogComponent "qt-ui"
27 #include <yui/YUILog.h>
33 #include <QVBoxLayout>
35 #include <yui/YApplication.h>
36 #include <yui/YEvent.h>
38 #include "QY2Styler.h"
41 #include "YQRichText.h"
46 static const char *colors[] = {
"red",
"blue",
"green", 0};
52 : QFrame( (QWidget *) parent->widgetRep() )
53 , YRichText( parent, text, plainTextMode )
54 , _colors_specified( 0 )
56 QVBoxLayout* layout =
new QVBoxLayout(
this );
57 layout->setSpacing( 0 );
62 layout->setMargin( YQWidgetMargin );
65 YUI_CHECK_NEW( _textBrowser );
66 layout->addWidget( _textBrowser );
68 _textBrowser->installEventFilter(
this );
72 _textBrowser->setWordWrapMode( QTextOption::NoWrap );
76 QString style =
"\n" + QY2Styler::styler()->textStyle();
77 size_t ccolors =
sizeof( colors ) /
sizeof(
char* ) - 1;
78 _colors_specified =
new bool[ccolors];
79 for (
size_t i = 0; i < ccolors; ++i )
81 _colors_specified[i] =
false;
83 sprintf( buffer,
"\n.%s ", colors[i] );
84 if ( style.contains( buffer ) )
85 _colors_specified[i] =
true;
87 _textBrowser->document()->setDefaultStyleSheet( style );
94 connect( _textBrowser, &pclass(_textBrowser)::anchorClicked,
107 if ( _textBrowser->horizontalScrollBar() )
108 _textBrowser->horizontalScrollBar()->setValue( _textBrowser->horizontalScrollBar()->minimum() );
110 if ( ! autoScrollDown() && _textBrowser->verticalScrollBar() )
111 _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->minimum() );
113 QString text = fromUTF8( newText );
115 if ( ! plainTextMode() )
117 for (
int counter = 0; colors[counter]; counter++ )
119 if ( !_colors_specified[counter] )
continue;
120 text.replace( QString(
"color=%1" ).arg( colors[counter] ), QString(
"class=\"%1\"" ).arg( colors[counter] ) );
121 text.replace( QString(
"color=\"%1\"" ).arg( colors[counter] ), QString(
"class=\"%1\"" ).arg( colors[counter] ));
123 text.replace(
"&product;", fromUTF8( YUI::app()->productName() ) );
124 _textBrowser->setHtml( text );
128 _textBrowser->setPlainText( text );
130 YRichText::setValue( newText );
132 if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
133 _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maximum() );
139 YRichText::setPlainTextMode( newPlainTextMode );
141 if ( plainTextMode() )
143 _textBrowser->setWordWrapMode( QTextOption::NoWrap );
150 YRichText::setAutoScrollDown( newAutoScrollDown );
152 if ( autoScrollDown() && _textBrowser->verticalScrollBar() )
153 _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maximum() );
172 if ( ev->type() == QEvent::KeyPress )
174 QKeyEvent *
event = ( QKeyEvent * ) ev;
176 if ( ( event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter ) &&
177 (
event->modifiers() & Qt::NoModifier ||
event->modifiers() & Qt::KeypadModifier ) &&
190 return QWidget::eventFilter( obj, ev );
196 if ( plainTextMode() )
199 return ( _textBrowser->document()->toPlainText().contains( QRegExp(
"<a\\s+href\\s*=", Qt::CaseInsensitive ) ) > 0 );
205 return shrinkable() ? 10 : 100;
211 return shrinkable() ? 10 : 100;
217 resize( newWidth, newHeight );
223 _textBrowser->setEnabled( enabled );
224 YWidget::setEnabled( enabled );
230 _textBrowser->setFocus();
238 return scrollValue( _textBrowser->verticalScrollBar() );
244 setScrollValue( _textBrowser->verticalScrollBar(), newValue );
250 return scrollValue( _textBrowser->horizontalScrollBar() );
256 setScrollValue( _textBrowser->horizontalScrollBar(), newValue );
260 string YQRichText::scrollValue( QScrollBar* scrollBar )
const
266 tmp.setNum( scrollBar->value() );
267 return tmp.toStdString();
271 void YQRichText::setScrollValue( QScrollBar* scrollBar,
const string & newValue )
273 if ( !scrollBar || newValue.empty() )
276 if ( newValue ==
"minimum" )
277 scrollBar->setValue( scrollBar->minimum() );
278 else if ( newValue ==
"maximum" )
279 scrollBar->setValue( scrollBar->maximum() );
282 QString tmp = QString::fromStdString( newValue );
283 scrollBar->setValue( tmp.toInt() );
293 if ( name.toString().startsWith(
"#") )
294 scrollToAnchor( name.toString().mid(1) );
bool activateDefaultButton(bool warn=true)
Activate (i.e.
virtual ~YQRichText()
Destructor.
virtual void activateLink(const std::string &url)
Derived classes should implement this, method is used to trigger event like user has clicked link in ...
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
virtual int preferredWidth()
Preferred width of the widget.
bool haveHyperLinks()
Returns 'true' if the current text of this RichText widget contains hyperlinks.
virtual void setAutoScrollDown(bool on=true)
Set this RichText widget's "auto scroll down" mode on or off.
virtual bool eventFilter(QObject *obj, QEvent *ev)
Event filter.
virtual void setHScrollValue(const std::string &newValue) override
Set the horizontal scrollbar position.
virtual int preferredHeight()
Preferred height of the widget.
virtual std::string hScrollValue() const override
Get the horizontal scrollbar position.
virtual std::string vScrollValue() const override
Get the vertical scrollbar position.
YQRichText(YWidget *parent, const std::string &text, bool plainTextMode=false)
Constructor.
virtual bool setKeyboardFocus()
Accept the keyboard focus.
virtual void setEnabled(bool enabled)
Set enabled/disabled state.
virtual void setVScrollValue(const std::string &newValue) override
Set the vertical scrollbar position.
virtual void setValue(const std::string &newValue) override
Change the text content of the RichText widget.
virtual void setPlainTextMode(bool on=true)
Set this RichText widget's "plain text" mode on or off.
void linkClicked(const QUrl &url)
Notification that a hyperlink is clicked.
Helper class - needed to have the benefits of both QVBox as the base class for YQRichText so uniform ...
virtual void setSource(const QUrl &name)
Get the document pointed to by a hyperlink.
static YQUI * ui()
Access the global Qt-UI.
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...