00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include "levelwidget.h"
00043 #include "qeditor.h"
00044 #include "paragdata.h"
00045
00046 #include <private/qrichtext_p.h>
00047 #include <kdebug.h>
00048
00049 using namespace std;
00050
00051 const char * plus_xpm[] = {
00052 "12 12 3 1",
00053 " c None",
00054 ". c #000000",
00055 "+ c #FFFFFF",
00056 " ......... ",
00057 " .+++++++. ",
00058 " .+++++++. ",
00059 " .+++++++. ",
00060 " .+++.+++. ",
00061 " .+++.+++. ",
00062 " .+.....+. ",
00063 " .+++.+++. ",
00064 " .+++.+++. ",
00065 " .+++++++. ",
00066 " .+++++++. ",
00067 " ......... " };
00068
00069 const char * minus_xpm[] = {
00070 "12 12 3 1",
00071 " c None",
00072 ". c #000000",
00073 "+ c #FFFFFF",
00074 " ......... ",
00075 " .+++++++. ",
00076 " .+++++++. ",
00077 " .+++++++. ",
00078 " .+++++++. ",
00079 " .+++++++. ",
00080 " .+.....+. ",
00081 " .+++++++. ",
00082 " .+++++++. ",
00083 " .+++++++. ",
00084 " .+++++++. ",
00085 " ......... "};
00086
00087 static QPixmap *plusPixmap = 0;
00088 static QPixmap *minusPixmap = 0;
00089
00090
00091 LevelWidget::LevelWidget( QEditor* editor, QWidget* parent, const char* name )
00092 : QWidget( parent, name, WRepaintNoErase | WStaticContents | WResizeNoErase ),
00093 m_editor( editor )
00094 {
00095 if( !plusPixmap ){
00096 plusPixmap = new QPixmap( plus_xpm );
00097 minusPixmap = new QPixmap( minus_xpm );
00098 }
00099
00100 setFixedWidth( 16 );
00101
00102 connect( m_editor->verticalScrollBar(), SIGNAL( valueChanged( int ) ),
00103 this, SLOT( doRepaint() ) );
00104 connect( m_editor, SIGNAL( textChanged() ),
00105 this, SLOT( doRepaint() ) );
00106 connect( m_editor, SIGNAL( parsed() ),
00107 this, SLOT( doRepaint() ) );
00108 doRepaint();
00109 }
00110
00111 LevelWidget::~LevelWidget()
00112 {
00113 }
00114
00115 void LevelWidget::paintEvent( QPaintEvent* )
00116 {
00117 buffer.fill( );
00118
00119 QTextParagraph *p = m_editor->document()->firstParagraph();
00120 QPainter painter( &buffer );
00121 int yOffset = m_editor->contentsY();
00122 while ( p ) {
00123 if ( !p->isVisible() ) {
00124 p = p->next();
00125 continue;
00126 }
00127 if ( p->rect().y() + p->rect().height() - yOffset < 0 ) {
00128 p = p->next();
00129 continue;
00130 }
00131 if ( p->rect().y() - yOffset > height() )
00132 break;
00133
00134 ParagData* data = (ParagData*) p->extraData();
00135 if( data ){
00136
00137 int prevLevel = 0;
00138 if( p->prev() ){
00139 prevLevel = ((ParagData*) p->prev()->extraData())->level();
00140 }
00141
00142 if( data->isBlockStart() ){
00143 if( data->isOpen() ){
00144 painter.drawLine( minusPixmap->width() / 2 + 2,
00145 p->rect().y() + p->rect().height() - yOffset,
00146 minusPixmap->width() / 2 - 2,
00147 p->rect().y() + p->rect().height() - yOffset );
00148
00149 painter.drawPixmap( 0, p->rect().y() +
00150 ( p->rect().height() - minusPixmap->height() ) / 2 -
00151 yOffset, *minusPixmap );
00152 } else {
00153 painter.drawPixmap( 0, p->rect().y() +
00154 ( p->rect().height() - plusPixmap->height() ) / 2 -
00155 yOffset, *plusPixmap );
00156 }
00157 } else if( data->level() < prevLevel ){
00158 painter.drawLine( plusPixmap->width() / 2, p->rect().y() - yOffset,
00159 plusPixmap->width() / 2, p->rect().y() + p->rect().height() - yOffset );
00160
00161 painter.drawLine( plusPixmap->width() / 2 + 2,
00162 p->rect().y() + p->rect().height() - yOffset,
00163 plusPixmap->width() / 2 - 2,
00164 p->rect().y() + p->rect().height() - yOffset );
00165 } else if( data->level() != 0 ){
00166 painter.drawLine( plusPixmap->width() / 2, p->rect().y() - yOffset,
00167 plusPixmap->width() / 2, p->rect().y() + p->rect().height() - yOffset );
00168 }
00169 }
00170 p = p->next();
00171 }
00172
00173 painter.end();
00174 bitBlt( this, 0, 0, &buffer );
00175 }
00176
00177 void LevelWidget::resizeEvent( QResizeEvent *e )
00178 {
00179 buffer.resize( e->size() );
00180 QWidget::resizeEvent( e );
00181 }
00182
00183 void LevelWidget::mousePressEvent( QMouseEvent* e )
00184 {
00185 QTextParagraph *p = m_editor->document()->firstParagraph();
00186 int yOffset = m_editor->contentsY();
00187 while ( p ) {
00188 if ( e->y() >= p->rect().y() - yOffset && e->y() <= p->rect().y() + p->rect().height() - yOffset ) {
00189 ParagData *data = (ParagData*) p->extraData();
00190 if( data && data->isBlockStart() ){
00191
00192 if( data->isOpen() ){
00193 emit collapseBlock( p );
00194 } else {
00195 emit expandBlock( p );
00196 }
00197 }
00198 break;
00199 }
00200 p = p->next();
00201 }
00202
00203 doRepaint();
00204 }
00205 #include "levelwidget.moc"