libyui-qt
2.43.5
|
00001 /* 00002 Copyright (C) 2000-2012 Novell, Inc 00003 This library is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU Lesser General Public License as 00005 published by the Free Software Foundation; either version 2.1 of the 00006 License, or (at your option) version 3.0 of the License. This library 00007 is distributed in the hope that it will be useful, but WITHOUT ANY 00008 WARRANTY; without even the implied warranty of MERCHANTABILITY or 00009 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00010 License for more details. You should have received a copy of the GNU 00011 Lesser General Public License along with this library; if not, write 00012 to the Free Software Foundation, Inc., 51 Franklin Street, Fifth 00013 Floor, Boston, MA 02110-1301 USA 00014 */ 00015 00016 00017 /*-/ 00018 00019 File: YQRichText.cc 00020 00021 Author: Stefan Hundhammer <sh@suse.de> 00022 00023 /-*/ 00024 00025 #define YUILogComponent "qt-ui" 00026 #include <yui/YUILog.h> 00027 00028 #include <QColorGroup> 00029 #include <QScrollBar> 00030 #include <QRegExp> 00031 #include <QDebug> 00032 #include <QKeyEvent> 00033 #include <QVBoxLayout> 00034 00035 #include <yui/YApplication.h> 00036 #include <yui/YEvent.h> 00037 #include "utf8.h" 00038 #include "QY2Styler.h" 00039 #include "YQUI.h" 00040 #include "YQDialog.h" 00041 #include "YQRichText.h" 00042 00043 static const char *colors[] = { "red", "blue", "green", 0}; 00044 00045 YQRichText::YQRichText( YWidget * parent, const std::string & text, bool plainTextMode ) 00046 : QFrame( (QWidget *) parent->widgetRep() ) 00047 , YRichText( parent, text, plainTextMode ) 00048 , _colors_specified( 0 ) 00049 { 00050 QVBoxLayout* layout = new QVBoxLayout( this ); 00051 layout->setSpacing( 0 ); 00052 setLayout( layout ); 00053 00054 setWidgetRep( this ); 00055 00056 layout->setMargin( YQWidgetMargin ); 00057 00058 _textBrowser = new YQTextBrowser( this ); 00059 YUI_CHECK_NEW( _textBrowser ); 00060 layout->addWidget( _textBrowser ); 00061 00062 _textBrowser->installEventFilter( this ); 00063 00064 if ( plainTextMode ) 00065 { 00066 _textBrowser->setWordWrapMode( QTextOption::NoWrap ); 00067 } 00068 else 00069 { 00070 QString style = "\n" + QY2Styler::styler()->textStyle(); 00071 size_t ccolors = sizeof( colors ) / sizeof( char* ) - 1; 00072 _colors_specified = new bool[ccolors]; 00073 for ( size_t i = 0; i < ccolors; ++i ) 00074 { 00075 _colors_specified[i] = false; 00076 char buffer[20]; 00077 sprintf( buffer, "\n.%s ", colors[i] ); 00078 if ( style.contains( buffer ) ) 00079 _colors_specified[i] = true; 00080 } 00081 _textBrowser->document()->setDefaultStyleSheet( style ); 00082 } 00083 00084 setValue( text ); 00085 00086 // Propagate clicks on hyperlinks 00087 00088 connect( _textBrowser, SIGNAL( anchorClicked( const QUrl & ) ), 00089 this, SLOT ( linkClicked ( const QUrl & ) ) ); 00090 } 00091 00092 00093 YQRichText::~YQRichText() 00094 { 00095 // NOP 00096 } 00097 00098 00099 void YQRichText::setValue( const std::string & newText ) 00100 { 00101 if ( _textBrowser->horizontalScrollBar() ) 00102 _textBrowser->horizontalScrollBar()->setValue(0); 00103 00104 if ( ! autoScrollDown() && _textBrowser->verticalScrollBar() ) 00105 _textBrowser->verticalScrollBar()->setValue(0); 00106 00107 QString text = fromUTF8( newText ); 00108 00109 if ( ! plainTextMode() ) 00110 { 00111 for ( int counter = 0; colors[counter]; counter++ ) 00112 { 00113 if ( !_colors_specified[counter] ) continue; 00114 text.replace( QString( "color=%1" ).arg( colors[counter] ), QString( "class=\"%1\"" ).arg( colors[counter] ) ); 00115 text.replace( QString( "color=\"%1\"" ).arg( colors[counter] ), QString( "class=\"%1\"" ).arg( colors[counter] )); 00116 } 00117 text.replace( "&product;", fromUTF8( YUI::app()->productName() ) ); 00118 _textBrowser->setHtml( text ); 00119 } 00120 else 00121 { 00122 _textBrowser->setPlainText( text ); 00123 } 00124 YRichText::setValue( newText ); 00125 00126 if ( autoScrollDown() && _textBrowser->verticalScrollBar() ) 00127 _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maximum() ); 00128 } 00129 00130 00131 void YQRichText::setPlainTextMode( bool newPlainTextMode ) 00132 { 00133 YRichText::setPlainTextMode( newPlainTextMode ); 00134 00135 if ( plainTextMode() ) 00136 { 00137 _textBrowser->setWordWrapMode( QTextOption::NoWrap ); 00138 } 00139 } 00140 00141 00142 void YQRichText::setAutoScrollDown( bool newAutoScrollDown ) 00143 { 00144 YRichText::setAutoScrollDown( newAutoScrollDown ); 00145 00146 if ( autoScrollDown() && _textBrowser->verticalScrollBar() ) 00147 _textBrowser->verticalScrollBar()->setValue( _textBrowser->verticalScrollBar()->maximum() ); 00148 } 00149 00150 00151 void YQRichText::linkClicked( const QUrl & url ) 00152 { 00153 // yuiDebug() << "Selected hyperlink \"" << url.toString() << "\" << std::endl; 00154 YQUI::ui()->sendEvent( new YMenuEvent( url.toString().toUtf8()) ); 00155 } 00156 00157 00158 bool YQRichText::eventFilter( QObject * obj, QEvent * ev ) 00159 { 00160 if ( ev->type() == QEvent::KeyPress ) 00161 { 00162 QKeyEvent * event = ( QKeyEvent * ) ev; 00163 00164 if ( ( event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter ) && 00165 ( event->modifiers() & Qt::NoModifier || event->modifiers() & Qt::KeypadModifier ) && 00166 ! haveHyperLinks() ) 00167 { 00168 YQDialog * dia = (YQDialog *) findDialog(); 00169 00170 if ( dia ) 00171 { 00172 ( void ) dia->activateDefaultButton(); 00173 return true; 00174 } 00175 } 00176 } 00177 00178 return QWidget::eventFilter( obj, ev ); 00179 } 00180 00181 00182 bool YQRichText::haveHyperLinks() 00183 { 00184 if ( plainTextMode() ) 00185 return false; 00186 00187 return ( _textBrowser->document()->toPlainText().contains( QRegExp( "<a\\s+href\\s*=", Qt::CaseInsensitive ) ) > 0 ); 00188 } 00189 00190 00191 int YQRichText::preferredWidth() 00192 { 00193 return shrinkable() ? 10 : 100; 00194 } 00195 00196 00197 int YQRichText::preferredHeight() 00198 { 00199 return shrinkable() ? 10 : 100; 00200 } 00201 00202 00203 void YQRichText::setSize( int newWidth, int newHeight ) 00204 { 00205 resize( newWidth, newHeight ); 00206 } 00207 00208 00209 void YQRichText::setEnabled( bool enabled ) 00210 { 00211 _textBrowser->setEnabled( enabled ); 00212 YWidget::setEnabled( enabled ); 00213 } 00214 00215 00216 bool YQRichText::setKeyboardFocus() 00217 { 00218 _textBrowser->setFocus(); 00219 00220 return true; 00221 } 00222 00223 void YQTextBrowser::setSource( const QUrl & name ) 00224 { 00225 // scroll to link if it's available in the current document 00226 // but prevent loading empty pages 00227 00228 if ( name.toString().startsWith("#") ) 00229 scrollToAnchor( name.toString().mid(1) ); 00230 00231 } 00232 00233 00234 00235 #include "YQRichText.moc"