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: YQLogView.cc 00020 00021 Author: Stefan Hundhammer <sh@suse.de> 00022 00023 /-*/ 00024 00025 00026 #include <qlabel.h> 00027 #include <qstyle.h> 00028 #include <QVBoxLayout> 00029 #include <QScrollBar> 00030 #define YUILogComponent "qt-ui" 00031 #include <yui/YUILog.h> 00032 00033 using std::max; 00034 00035 #include "utf8.h" 00036 #include "YQUI.h" 00037 #include "YQLogView.h" 00038 #include "YQWidgetCaption.h" 00039 00040 00041 YQLogView::YQLogView( YWidget * parent, 00042 const std::string & label, 00043 int visibleLines, 00044 int maxLines ) 00045 : QFrame( (QWidget *) parent->widgetRep() ) 00046 , YLogView( parent, label, visibleLines, maxLines ) 00047 { 00048 setWidgetRep( this ); 00049 QVBoxLayout* layout = new QVBoxLayout( this ); 00050 setLayout( layout ); 00051 00052 layout->setSpacing( YQWidgetSpacing ); 00053 layout->setMargin( YQWidgetMargin ); 00054 00055 _caption = new YQWidgetCaption( this, label ); 00056 YUI_CHECK_NEW( _caption ); 00057 layout->addWidget( _caption ); 00058 00059 _qt_text = new MyTextEdit( this ); 00060 YUI_CHECK_NEW( _qt_text ); 00061 layout->addWidget( _qt_text ); 00062 00063 _qt_text->setReadOnly( true ); 00064 _qt_text->setAcceptRichText( false ); 00065 _qt_text->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) ); 00066 00067 _caption->setBuddy( _qt_text ); 00068 00069 connect (_qt_text, SIGNAL(resized()), this, SLOT(slotResize())); 00070 00071 } 00072 00073 00074 YQLogView::~YQLogView() 00075 { 00076 // NOP 00077 } 00078 00079 00080 void 00081 YQLogView::displayLogText( const std::string & text ) 00082 { 00083 QScrollBar *sb = _qt_text->verticalScrollBar(); 00084 QString newString = fromUTF8( text ); 00085 00086 bool atEnd = sb->value() == sb->maximum(); 00087 00088 if (newString.startsWith(_lastText) && !_lastText.isEmpty() ) 00089 { 00090 int position = _lastText.length(); 00091 00092 // prevent double line break caused by QTextEdit::append() 00093 if ( newString.mid( _lastText.length(), 1 ) == QString('\n') ) 00094 position++; 00095 00096 _qt_text->append( newString.mid( position) ); 00097 } 00098 else 00099 { 00100 _qt_text->setPlainText( newString ); 00101 } 00102 00103 00104 if (atEnd) 00105 { 00106 _qt_text->moveCursor( QTextCursor::End ); 00107 _qt_text->ensureCursorVisible(); 00108 sb->setValue( sb->maximum() ); 00109 } 00110 00111 _lastText = newString; 00112 } 00113 00114 00115 void 00116 YQLogView::setLabel( const std::string & label ) 00117 { 00118 _caption->setText( label ); 00119 YLogView::setLabel( label ); 00120 } 00121 00122 00123 00124 void 00125 YQLogView::setEnabled( bool enabled ) 00126 { 00127 _caption->setEnabled( enabled ); 00128 _qt_text->setEnabled( enabled ); 00129 YWidget::setEnabled( enabled ); 00130 } 00131 00132 00133 int 00134 YQLogView::preferredWidth() 00135 { 00136 return max( 50, sizeHint().width() ); 00137 } 00138 00139 00140 int 00141 YQLogView::preferredHeight() 00142 { 00143 int hintHeight = visibleLines() * _qt_text->fontMetrics().lineSpacing(); 00144 hintHeight += _qt_text->style()->pixelMetric( QStyle::PM_ScrollBarExtent ); 00145 hintHeight += _qt_text->frameWidth() * 2; 00146 00147 if ( !_caption->isHidden() ) 00148 hintHeight += _caption->sizeHint().height(); 00149 00150 return max( 80, hintHeight ); 00151 } 00152 00153 void 00154 YQLogView::slotResize() 00155 { 00156 QScrollBar *sb = _qt_text->verticalScrollBar(); 00157 00158 bool atEnd = sb->value() == sb->maximum(); 00159 00160 if (atEnd) 00161 { 00162 _qt_text->moveCursor( QTextCursor::End ); 00163 _qt_text->ensureCursorVisible(); 00164 sb->setValue( sb->maximum() ); 00165 } 00166 } 00167 00168 void 00169 YQLogView::setSize( int newWidth, int newHeight ) 00170 { 00171 resize( newWidth, newHeight ); 00172 } 00173 00174 00175 bool 00176 YQLogView::setKeyboardFocus() 00177 { 00178 _qt_text->setFocus(); 00179 00180 return true; 00181 } 00182 00183 00184 00185 #include "YQLogView.moc" 00186