📄 htmlclue.cpp
字号:
// get the current ascent not including curr HTMLObject *obj = head; while ( obj != curr ) { ascent += obj->getHeight(); obj = obj->next(); } // remove any aligned objects previously added by the current // object. removeAlignedByParent( curr ); } else { ascent = padding; descent = 0; curr = head; } while ( curr != 0 ) { // Set an initial ypos so that the alignment stuff knows where // the top of this object is curr->setYPos( ascent ); curr->calcSize( this ); if ( curr->getWidth() > width - ( padding << 1) ) width = curr->getWidth() + ( padding << 1 ); ascent += curr->getHeight(); curr->setPos( lmargin, ascent - curr->getDescent() ); curr = curr->next(); } ascent += padding; // remember the last object so that we can start from here next time // we are called. curr = tail; if ((max_width != 0) && (width > max_width)) width = max_width; HTMLObject *obj; if ( halign == HCenter ) { for ( obj = head; obj != 0; obj = obj->next() ) obj->setXPos( lmargin + (width - obj->getWidth()) / 2 ); } else if ( halign == Right ) { for ( obj = head; obj != 0; obj = obj->next() ) obj->setXPos( lmargin + width - obj->getWidth() ); } HTMLClueAligned *clue; for ( clue = alignLeftList; clue != 0; clue = clue->nextClue() ) { if ( clue->getYPos() + clue->parent()->getYPos() - clue->parent()->getAscent() > ascent ) ascent = clue->getYPos() + clue->parent()->getYPos() - clue->parent()->getAscent(); } for ( clue = alignRightList; clue != 0; clue = clue->nextClue() ) { if ( clue->getYPos() + clue->parent()->getYPos() - clue->parent()->getAscent() > ascent ) ascent = clue->getYPos() + clue->parent()->getYPos() - clue->parent()->getAscent(); }}bool HTMLClueV::print( QPainter *_painter, int _x, int _y, int _width, int _height, int _tx, int _ty, bool toPrinter ){ bool rv = HTMLClue::print( _painter, _x, _y, _width, _height, _tx, _ty, toPrinter ); // print aligned objects if ( _y + _height < y - getAscent() || _y > y + descent ) return rv; _tx += x; _ty += y - ascent; HTMLClueAligned *clue; for ( clue = alignLeftList; clue != 0; clue = clue->nextClue() ) { clue->print( _painter, _tx + clue->parent()->getXPos(), _ty + clue->parent()->getYPos() - clue->parent()->getAscent() ); } for ( clue = alignRightList; clue != 0; clue = clue->nextClue() ) { clue->print( _painter, _tx + clue->parent()->getXPos(), _ty + clue->parent()->getYPos() - clue->parent()->getAscent() ); } return rv;}void HTMLClueV::findFreeArea( int _y, int _width, int _height, int _indent, int *_y_pos, int *_lmargin, int *_rmargin)// This method tries to find a free rectangular area of _width x _height// from position _y on. The start of this area is written in *y_pos.// The actual left and right margins of the area are returned in// *lmargin and *rmargin.{ int try_y = _y; int lmargin; int rmargin; HTMLClueAligned *clue; int next_y, top_y, base_y;#ifdef DEBUG_ALIGN// Debug alignment lists //printf("Find w,h = %d,%d from Y-pos %d on\n", _width, _height, _y); //printf("aligLeftList:\n"); for ( clue = alignLeftList; clue != 0; clue = clue->nextClue() ) { base_y = clue->getYPos() + clue->parent()->getYPos() - clue->parent()->getAscent(); top_y = base_y - clue->getAscent(); //printf(" x,y [x,y] / w,h = %d,%d [%d,%d] / %d, %d\n", clue->getXPos()+clue->parent()->getXPos(), top_y, clue->getXPos(), clue->getYPos(), clue->getWidth(), clue->getHeight()); } //printf("aligRightList:\n"); for ( clue = alignRightList; clue != 0; clue = clue->nextClue() ) { base_y = clue->getYPos() + clue->parent()->getYPos() - clue->parent()->getAscent(); top_y = base_y - clue->getAscent(); //printf(" x,y [x,y] / w,h = %d,%d [%d,%d] / %d, %d\n", clue->getXPos()+clue->parent()->getXPos(), top_y, clue->getXPos(), clue->getYPos(), clue->getWidth(), clue->getHeight()); }#endif // Find a suitable position while(1) { // try position try_y lmargin = _indent; rmargin = max_width; next_y = 0; // Calculate left margin for ( clue = alignLeftList; clue != 0; clue = clue->nextClue() ) { base_y = clue->getYPos() + clue->parent()->getYPos() - clue->parent()->getAscent(); top_y = base_y - clue->getAscent(); if ((top_y <= try_y+_height) && (base_y > try_y)) { int lm = clue->getXPos() + clue->getWidth(); if (lm > lmargin) lmargin = lm; if ((next_y == 0) || (base_y < next_y)) { next_y = base_y; } } } // Calculate right margin for ( clue = alignRightList; clue != 0; clue = clue->nextClue() ) { base_y = clue->getYPos() + clue->parent()->getYPos() - clue->parent()->getAscent(); top_y = base_y - clue->getAscent(); if ((top_y <= try_y+_height) && (base_y > try_y)) { int rm = clue->getXPos(); if (rm < rmargin) rmargin = rm; if ((next_y == 0) || (base_y < next_y)) { next_y = base_y; } } } /* no margins here.. just put it in */ if ((lmargin == _indent) && (rmargin == max_width)) break; /* object fits within current margins */ if ((rmargin-lmargin) >= _width) break; /* Object does not fit here.... increase Y */ try_y = next_y; } *_y_pos = try_y; *_rmargin = rmargin; *_lmargin = lmargin;#ifdef DEBUG_ALIGN //printf("Got y=%d, lmargin = %d, rmargin =%d\n", try_y, lmargin, rmargin);#endif}void HTMLClueV::appendLeftAligned( HTMLClueAligned *_clue )// This method adds _clue to the left aligned list.// Its X-position is calculated based on the left margin// at the Y-position of _clue. If _clue does not fit,// its Y-position will be increased until it fits.{ int y_pos; int start_y = 0; int lmargin; int rmargin; HTMLClueAligned *clue; clue = alignLeftList; if (clue) { while ( clue->nextClue() ) { clue = clue->nextClue(); } y_pos = clue->getYPos() + clue->parent()->getYPos() - clue->parent()->getAscent(); if (y_pos > start_y) start_y = y_pos; } y_pos = _clue->getYPos() + _clue->parent()->getYPos() - _clue->parent()->getAscent(); if (y_pos > start_y) start_y = y_pos; // Start looking for space from the position of the last object in // the left-aligned list on, or from the current position of the // object. findFreeArea(start_y - _clue->getAscent(), _clue->getWidth(), _clue->getHeight(), 0, &y_pos, &lmargin, &rmargin); // Set position _clue->setPos(lmargin, y_pos - _clue->parent()->getYPos() + _clue->parent()->getAscent() + _clue->getAscent()); // Insert clue in align list if ( !alignLeftList ) { alignLeftList = _clue; _clue->setNextClue( 0 ); } else { HTMLClueAligned *obj = alignLeftList; while ( obj->nextClue() ) { if ( obj == _clue ) { //printf("%s:%d Clue already in alignLeftList\n", __FILE__, __LINE__); return; } obj = obj->nextClue(); } if ( obj == _clue ) { //printf("%s:%d Clue already in alignLeftList\n", __FILE__, __LINE__); return; } obj->setNextClue( _clue ); _clue->setNextClue( 0 ); }}void HTMLClueV::appendRightAligned( HTMLClueAligned *_clue )// This method adds _clue to the right aligned list.// Its X-position is calculated based on the right margin// at the Y-position of _clue. If _clue does not fit,// its Y-position will be increased until it fits.{ int y_pos; int start_y = 0; int lmargin; int rmargin; HTMLClueAligned *clue; clue = alignRightList; if (clue) { while ( clue->nextClue() ) { clue = clue->nextClue(); } y_pos = clue->getYPos() + clue->parent()->getYPos(); if (y_pos > start_y) start_y = y_pos; } y_pos = _clue->getYPos() + _clue->parent()->getYPos(); if (y_pos > start_y) start_y = y_pos; // Start looking for space from the position of the last object in // the left-aligned list on, or from the current position of the // object. findFreeArea(start_y - _clue->getAscent(), _clue->getWidth(), _clue->getHeight(), 0, &y_pos, &lmargin, &rmargin); // Set position _clue->setPos(rmargin - _clue->getWidth(), y_pos - _clue->parent()->getYPos() + _clue->getAscent()); // Insert clue in align list if ( !alignRightList ) { alignRightList = _clue; _clue->setNextClue( 0 ); } else { HTMLClueAligned *obj = alignRightList; while ( obj->nextClue() ) { if ( obj == _clue ) { //printf("%s:%d Clue already in alignRightList\n", __FILE__, __LINE__); return; } obj = obj->nextClue(); } if ( obj == _clue ) { //printf("%s:%d Clue already in alignRightList\n", __FILE__, __LINE__); return; } obj->setNextClue( _clue ); _clue->setNextClue( 0 ); }}int HTMLClueV::appended( HTMLClueAligned *_clue )// Returns whether _clue is already in the alignList{ HTMLClueAligned *clue; if (_clue->getHAlign() == Left) { clue = alignLeftList; } else { clue = alignRightList; } while ( clue) { if (clue == _clue) return 1; clue = clue->nextClue(); } return 0;}// This is a horrible hack so that the progressive size calculation is// not stuffed up by aligned clues added in a previous pass on the// current clue//void HTMLClueV::removeAlignedByParent( HTMLObject *p ){ HTMLClueAligned *tmp, *obj; tmp = 0; obj = alignLeftList; while ( obj ) { if ( obj->parent() == p ) { if ( tmp ) { tmp->setNextClue( obj->nextClue() ); tmp = obj; } else { alignLeftList = obj->nextClue(); tmp = 0; } } else tmp = obj; obj = obj->nextClue(); } tmp = 0; obj = alignRightList; while ( obj ) { if ( obj->parent() == p ) { if ( tmp ) { tmp->setNextClue( obj->nextClue() ); tmp = obj; } else { alignRightList = obj->nextClue(); tmp = 0; } } else tmp = obj; obj = obj->nextClue(); }}int HTMLClueV::getLeftMargin( int _y ){ int margin = 0; HTMLClueAligned *clue; for ( clue = alignLeftList; clue != 0; clue = clue->nextClue() ) { if ( clue->getYPos() - clue->getAscent() + clue->parent()->getYPos() - clue->parent()->getAscent() <= _y && clue->getYPos() + clue->parent()->getYPos() - clue->parent()->getAscent() > _y ) margin = clue->getXPos() + clue->getWidth(); } return margin;}int HTMLClueV::getRightMargin( int _y ){ int margin = max_width; HTMLClueAligned *clue; for ( clue = alignRightList; clue != 0; clue = clue->nextClue() ) { if ( clue->getYPos()-clue->getAscent()+clue->parent()->getYPos() - clue->parent()->getAscent() <= _y && clue->getYPos() + clue->parent()->getYPos() - clue->parent()->getAscent() > _y ) margin = clue->getXPos(); } return margin;}int HTMLClueV::getLeftClear( int _y ){ HTMLClueAligned *clue; int top_y, base_y; // try position _y for ( clue = alignLeftList; clue != 0; clue = clue->nextClue() ) { base_y = clue->getYPos() + clue->parent()->getYPos() - clue->parent()->getAscent(); top_y = base_y - clue->getAscent(); if ((top_y <= _y) && (base_y > _y)) { _y = base_y; } } return _y;}int HTMLClueV::getRightClear( int _y ){ HTMLClueAligned *clue; int top_y, base_y; // try position _y for ( clue = alignRightList; clue != 0; clue = clue->nextClue() ) { base_y = clue->getYPos() + clue->parent()->getYPos() - clue->parent()->getAscent(); top_y = base_y - clue->getAscent(); if ((top_y <= _y) && (base_y > _y)) { _y = base_y; } } return _y;}//-----------------------------------------------------------------------------HTMLCell::HTMLCell( int _x, int _y, int _max_width, int _percent, const char *_url, const char *_target ) : HTMLClueV( _x, _y, _max_width, _percent ){ url = _url; target = _target; bIsMarked = false; padding = 1; // leave a little room for the selection rectangle.}void HTMLCell::select( KHTMLWidget *_htmlw, HTMLChain *_chain, QRect & _rect, int _tx, int _ty ){ HTMLObject *obj; QRect r( x + _tx, y - ascent + _ty, width, ascent + descent ); _tx += x; _ty += y - ascent; bool sel = false; if ( _rect.contains( r ) ) { sel = true; } else if ( !_rect.intersects( r ) ) { sel = false; } else { QRect isect = _rect.intersect( r ); if ( isect.width() > r.width()/2 && isect.height() > r.height()/2 ) sel = true; } _chain->push( this ); for ( obj = head; obj != 0; obj = obj->next() ) obj->select( _htmlw, _chain, sel, _tx, _ty ); _chain->pop();}bool HTMLCell::print( QPainter *_painter, int _x, int _y, int _width, int _height, int _tx, int _ty, bool toPrinter ){ bool rv = HTMLClueV::print( _painter, _x, _y, _width, _height, _tx, _ty, toPrinter ); // print aligned objects if ( _y + _height < y - getAscent() || _y > y + descent ) return rv; if ( !toPrinter && bIsMarked ) { QPen pen( _painter->pen() ); QPen newPen( black ); _painter->setPen( newPen ); _painter->drawRect( _tx + x, _ty + y - ascent, width, ascent + descent ); newPen.setColor( white ); newPen.setStyle( DotLine ); _painter->setPen( newPen ); _painter->drawRect( _tx + x, _ty + y - ascent, width, ascent + descent ); _painter->setPen( pen ); } return rv;}void HTMLCell::findCells( int _tx, int _ty, QList<HTMLCellInfo> &_list ){ HTMLCellInfo *p = new HTMLCellInfo; p->pCell = this; p->xAbs = _tx + x; p->baseAbs = _ty + y; p->tx = _tx; p->ty = _ty; _list.append( p );}//-----------------------------------------------------------------------------bool HTMLClueH::selectText( KHTMLWidget *_htmlw, HTMLChain *_chain, int _x1, int _y1, int _x2, int _y2, int _tx, int _ty ){ HTMLObject *obj; bool isSel = false; int a = 0, d = 0; if ( !head ) return false; _tx += x; _ty += y - ascent; // get max ascent and descent for ( obj = head; obj != 0; obj = obj->next() ) { if ( obj->getAscent() > a ) a = obj->getAscent(); if ( obj->getDescent() > d ) d = obj->getDescent(); } int rely1 = _y1 - ( y - ascent ); int rely2 = _y2 - ( y - ascent ); int ypos = head->getYPos(); if ( rely1 > ypos - a && rely1 < ypos + d ) rely1 = ypos-1; if ( rely2 > ypos - a && rely2 < ypos + d ) rely2 = ypos; _chain->push( this ); // (de)select objects for ( obj = head; obj != 0; obj = obj->next() ) { if ( obj->getObjectType() == Clue ) isSel = obj->selectText( _htmlw, _chain, _x1 - x, _y1 - (y-ascent), _x2 - x, _y2 - ( y - ascent ), _tx, _ty ) || isSel; else isSel = obj->selectText( _htmlw, _chain, _x1 - x, rely1, _x2 - x, rely2, _tx, _ty ) || isSel; } _chain->pop(); return isSel;}void HTMLClueH::getSelectedText( QString &_str ){ HTMLObject *obj;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -