⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 layout.cpp

📁 Linux下的基于X11的图形开发环境。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
bool Grid::isWidgetStartRow( int r ) const{    int c;    for ( c = 0; c < ncols; c++ ) {	if ( cell( r, c ) && ( (r==0) || (cell( r, c) != cell( r-1, c) )) )	    return TRUE;    }    return FALSE;}bool Grid::isWidgetEndRow( int r ) const{    int c;    for ( c = 0; c < ncols; c++ ) {	if ( cell( r, c ) && ((r == nrows-1) || (cell( r, c) != cell( r+1, c) )) )	    return TRUE;    }    return FALSE;}bool Grid::isWidgetTopLeft( int r, int c ) const{    QWidget* w = cell( r, c );    if ( !w )	return FALSE;    return ( !r || cell( r-1, c) != w ) && (!c || cell( r, c-1) != w);}void Grid::extendLeft(){    int r,c,i;    for ( c = 1; c < ncols; c++ ) {	for ( r = 0; r < nrows; r++ ) {	    QWidget* w = cell( r, c );	    if ( !w )		continue;	    int cc = countCol( r, c);	    int stretch = 0;	    for ( i = c-1; i >= 0; i-- ) {		if ( cell( r, i ) )		    break;		if ( countCol( r, i ) < cc )		    break;		if ( isWidgetEndCol( i ) )		    break;		if ( isWidgetStartCol( i ) ) {		    stretch = c - i;		    break;		}	    }	    if ( stretch ) {		for ( i = 0; i < stretch; i++ )		    setCol( r, c-i-1, w, cc );	    }	}    }}void Grid::extendRight(){    int r,c,i;    for ( c = ncols - 2; c >= 0; c-- ) {	for ( r = 0; r < nrows; r++ ) {	    QWidget* w = cell( r, c );	    if ( !w )		continue;	    int cc = countCol( r, c);	    int stretch = 0;	    for ( i = c+1; i < ncols; i++ ) {		if ( cell( r, i ) )		    break;		if ( countCol( r, i ) < cc )		    break;		if ( isWidgetStartCol( i ) )		    break;		if ( isWidgetEndCol( i ) ) {		    stretch = i - c;		    break;		}	    }	    if ( stretch ) {		for ( i = 0; i < stretch; i++ )		    setCol( r, c+i+1, w, cc );	    }	}    }}void Grid::extendUp(){    int r,c,i;    for ( r = 1; r < nrows; r++ ) {	for ( c = 0; c < ncols; c++ ) {	    QWidget* w = cell( r, c );	    if ( !w )		continue;	    int cr = countRow( r, c);	    int stretch = 0;	    for ( i = r-1; i >= 0; i-- ) {		if ( cell( i, c ) )		    break;		if ( countRow( i, c ) < cr )		    break;		if ( isWidgetEndRow( i ) )		    break;		if ( isWidgetStartRow( i ) ) {		    stretch = r - i;		    break;		}	    }	    if ( stretch ) {		for ( i = 0; i < stretch; i++ )		    setRow( r-i-1, c, w, cr );	    }	}    }}void Grid::extendDown(){    int r,c,i;    for ( r = nrows - 2; r >= 0; r-- ) {	for ( c = 0; c < ncols; c++ ) {	    QWidget* w = cell( r, c );	    if ( !w )		continue;	    int cr = countRow( r, c);	    int stretch = 0;	    for ( i = r+1; i < nrows; i++ ) {		if ( cell( i, c ) )		    break;		if ( countRow( i, c ) < cr )		    break;		if ( isWidgetStartRow( i ) )		    break;		if ( isWidgetEndRow( i ) ) {		    stretch = i - r;		    break;		}	    }	    if ( stretch ) {		for ( i = 0; i < stretch; i++ )		    setRow( r+i+1, c, w, cr );	    }	}    }}void Grid::simplify(){    extendLeft();    extendRight();    extendUp();    extendDown();    merge();}void Grid::merge(){    int r,c;    for ( c = 0; c < ncols; c++ )	cols[c] = FALSE;    for ( r = 0; r < nrows; r++ )	rows[r] = FALSE;    for ( c = 0; c < ncols; c++ ) {	for ( r = 0; r < nrows; r++ ) {	    if ( isWidgetTopLeft( r, c ) ) {		rows[r] = TRUE;		cols[c] = TRUE;	    }	}    }}bool Grid::locateWidget( QWidget* w, int& row, int& col, int& rowspan, int & colspan ){    int r,c, r2, c2;    for ( c = 0; c < ncols; c++ ) {	for ( r = 0; r < nrows; r++ ) {	    if ( cell( r, c ) == w  ) {		row = 0;		for ( r2 = 1; r2 <= r; r2++ ) {		    if ( rows[ r2-1 ] )			row++;		}		col = 0;		for ( c2 = 1; c2 <= c; c2++ ) {		    if ( cols[ c2-1 ] )			col++;		}		rowspan = 0;		for ( r2 = r ; r2 < nrows && cell( r2, c) == w; r2++ ) {		    if ( rows[ r2 ] )			rowspan++;		}		colspan = 0;		for ( c2 = c; c2 < ncols && cell( r, c2) == w; c2++ ) {		    if ( cols[ c2] )			colspan++;		}		return TRUE;	    }	}    }    return FALSE;}GridLayout::GridLayout( const QWidgetList &wl, QWidget *p, FormWindow *fw, QWidget *lb, const QSize &res, bool doSetup )    : Layout( wl, p, fw, lb, doSetup ), resolution( res ){    grid = 0;    if ( doSetup )	setup();}GridLayout::~GridLayout(){    delete grid;}void GridLayout::doLayout(){    bool needMove, needReparent;    if ( !prepareLayout( needMove, needReparent ) )	return;    QDesignerGridLayout *layout = (QDesignerGridLayout*)WidgetFactory::createLayout( layoutBase, 0, WidgetFactory::Grid );    if ( !grid )	buildGrid();    QWidget* w;    int r, c, rs, cs;    for ( w = widgets.first(); w; w = widgets.next() ) {	if ( grid->locateWidget( w, r, c, rs, cs) ) {	    if ( needReparent && w->parent() != layoutBase )		w->reparent( layoutBase, 0, QPoint( 0, 0 ), FALSE );	    if ( rs * cs == 1 ) {		layout->addWidget( w, r, c, w->inherits( "Spacer" ) ? ( (Spacer*)w )->alignment() : 0 );	    } else {		layout->addMultiCellWidget( w, r, r+rs-1, c, c+cs-1, w->inherits( "Spacer" ) ? ( (Spacer*)w )->alignment() : 0 );	    }	    if ( w->inherits( "QLayoutWidget" ) )		( (QLayoutWidget*)w )->updateSizePolicy();	    w->show();	} else {	    qWarning("ooops, widget '%s' does not fit in layout", w->name() );	}    }    finishLayout( needMove, layout );}void GridLayout::setup(){    Layout::setup();    buildGrid();}void GridLayout::buildGrid(){    if ( !widgets.count() )	return;    // Pixel to cell conversion:    // By keeping a list of start'n'stop values (x & y) for each widget,    // it is possible to create a very small grid of cells to represent    // the widget layout.    // -----------------------------------------------------------------    // We need a list of both start and stop values for x- & y-axis    QValueVector<int> x( widgets.count()*2 );    QValueVector<int> y( widgets.count()*2 );    // Using push_back would look nicer, but operator[] is much faster    int index  = 0;    QWidget* w = 0;    for ( w = widgets.first(); w; w = widgets.next() ) {	QRect widgetPos = w->geometry();	x[index]   = widgetPos.left();	x[index+1] = widgetPos.right();	y[index]   = widgetPos.top();	y[index+1] = widgetPos.bottom();	index += 2;    }    qHeapSort(x);    qHeapSort(y);    // Remove duplicate x enteries (Remove next, if equal to current)    if ( !x.empty() ) {	for (QValueVector<int>::iterator current = x.begin() ;	     (current != x.end()) && ((current+1) != x.end()) ; )	    if ( (*current == *(current+1)) )		x.erase(current+1);	    else		current++;    }    // Remove duplicate y enteries (Remove next, if equal to current)    if ( !y.empty() ) {	for (QValueVector<int>::iterator current = y.begin() ;	     (current != y.end()) && ((current+1) != y.end()) ; )	    if ( (*current == *(current+1)) )		y.erase(current+1);	    else		current++;    }    // Create the smallest grid possible to represent the current layout    // Since no widget will be placed in the last row and column, we'll    // skip them to increase speed even further    delete grid;    grid = new Grid( y.size()-1, x.size()-1 );    // Mark the cells in the grid that contains a widget    for ( w = widgets.first(); w; w = widgets.next() ) {	QRect c(0,0,0,0), widgetPos = w->geometry();	// From left til right (not including)	for (uint cw=0; cw<x.size(); cw++) {	    if ( x[cw] == widgetPos.left() )		c.setLeft(cw);	    if ( x[cw] <  widgetPos.right())		c.setRight(cw);	}	// From top til bottom (not including)	for (uint ch=0; ch<y.size(); ch++) {	    if ( y[ch] == widgetPos.top()    )		c.setTop(ch);	    if ( y[ch] <  widgetPos.bottom() )		c.setBottom(ch);	}	grid->setCells(c, w); // Mark cellblock    }    grid->simplify();}Spacer::Spacer( QWidget *parent, const char *name )    : QWidget( parent, name, WMouseNoMask ),      orient( Vertical ), interactive(TRUE), sh( QSize(20,20) ){    setSizeType( Expanding );    setAutoMask( TRUE );}void Spacer::paintEvent( QPaintEvent * ){    QPainter p( this );    p.setPen( Qt::blue );    if ( orient == Horizontal ) {	const int dist = 3;	const int amplitude = QMIN( 3, height() / 3 );	const int base = height() / 2;	int i = 0;	p.setPen( white );	for ( i = 0; i < width() / 3 +2; ++i )	    p.drawLine( i * dist, base - amplitude, i * dist + dist / 2, base + amplitude );	p.setPen( blue );	for ( i = 0; i < width() / 3 +2; ++i )	    p.drawLine( i * dist + dist / 2, base + amplitude, i * dist + dist, base - amplitude );	p.drawLine( 0, 0, 0, height() );	p.drawLine( width() - 1, 0, width() - 1, height());    } else {	const int dist = 3;	const int amplitude = QMIN( 3, width() / 3 );	const int base = width() / 2;	int i = 0;	p.setPen( white );	for ( i = 0; i < height() / 3 +2; ++i )	    p.drawLine( base - amplitude, i * dist, base + amplitude,i * dist + dist / 2 );	p.setPen( blue );	for ( i = 0; i < height() / 3 +2; ++i )	    p.drawLine( base + amplitude, i * dist + dist / 2, base - amplitude, i * dist + dist );	p.drawLine( 0, 0, width(), 0 );	p.drawLine( 0, height() - 1, width(), height() - 1 );    }}void Spacer::resizeEvent( QResizeEvent* e){    QWidget::resizeEvent( e );    if ( !parentWidget() || WidgetFactory::layoutType( parentWidget() ) == WidgetFactory::NoLayout )	sh = size();}void Spacer::updateMask(){    QRegion r( rect() );    if ( orient == Horizontal ) {	const int amplitude = QMIN( 3, height() / 3 );	const int base = height() / 2;	r = r.subtract( QRect(1, 0, width() - 2, base - amplitude ) );	r = r.subtract( QRect(1, base + amplitude, width() - 2, height() - base - amplitude ) );    } else {	const int amplitude = QMIN( 3, width() / 3 );	const int base = width() / 2;	r = r.subtract( QRect(0, 1, base - amplitude, height() - 2 ) );	r = r.subtract( QRect( base + amplitude, 1, width() - base - amplitude, height() - 2 ) );    }    setMask( r );}void Spacer::setSizeType( SizeType t ){    QSizePolicy sizeP;    if ( orient == Vertical )	sizeP = QSizePolicy( QSizePolicy::Minimum, (QSizePolicy::SizeType)t );    else	sizeP = QSizePolicy( (QSizePolicy::SizeType)t, QSizePolicy::Minimum );    setSizePolicy( sizeP );}Spacer::SizeType Spacer::sizeType() const{    if ( orient == Vertical )	return (SizeType)sizePolicy().verData();    return (SizeType)sizePolicy().horData();}int Spacer::alignment() const{    if ( orient == Vertical )	return AlignHCenter;    return AlignVCenter;}QSize Spacer::minimumSize() const{    QSize s = QSize( 20,20 );    if ( sizeType() == Expanding )	if ( orient == Vertical )	    s.rheight() = 0;	else	    s.rwidth() = 0;    return s;}QSize Spacer::sizeHint() const{    return sh;}void Spacer::setSizeHint( const QSize &s ){    sh = s;    if ( !parentWidget() || WidgetFactory::layoutType( parentWidget() ) == WidgetFactory::NoLayout )	resize( sizeHint() );    updateGeometry();}Qt::Orientation Spacer::orientation() const{    return orient;}void Spacer::setOrientation( Qt::Orientation o ){    if ( orient == o )	return;    SizeType st = sizeType();    orient = o;    setSizeType( st );    if ( interactive ) {	sh = QSize( sh.height(), sh.width() );	if (!parentWidget() || WidgetFactory::layoutType( parentWidget() ) == WidgetFactory::NoLayout )	    resize( height(), width() );    }    updateMask();    update();    updateGeometry();}void QDesignerGridLayout::addWidget( QWidget *w, int row, int col, int align_ ){    items.insert( w, Item(row, col, 1, 1) );    QGridLayout::addWidget( w, row, col, align_ );}void QDesignerGridLayout::addMultiCellWidget( QWidget *w, int fromRow, int toRow,					      int fromCol, int toCol, int align_ ){    items.insert( w, Item(fromRow, fromCol, toRow - fromRow + 1, toCol - fromCol +1) );    QGridLayout::addMultiCellWidget( w, fromRow, toRow, fromCol, toCol, align_ );}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -