// Handle size changes. Set the contained controls extents. Called by framework void CMessageListBox::SizeChanged() { innerRect = Border().InnerRect(Rect()); // Application Title rect. Fills the controls top area TSize titleSize(innerRect.Width(), iAppTitle->MinimumSize().iHeight); TRect titleRect(innerRect.iTl, titleSize); iAppTitle->SetRect(titleRect); // List box size and rect. Fills the remaining area TSize listBoxSize(innerRect.Width(), innerRect.Height() - iAppTitle->MinimumSize().iHeight); TRect listBoxRect(TPoint(titleRect.iTl.iX, titleRect.iBr.iY+1), listBoxSize); iListbox->SetRect(listBoxRect); }
void LegendItem::paint(QPainter *painter) { if (!isVisible()) { return; } RelationList legendItems; if (_auto) { legendItems = plot()->renderItem(PlotRenderItem::Cartesian)->relationList(); } else { legendItems = _relations; } int count = legendItems.count(); if (count <= 0) { // no legend or box if there are no legend items return; } QFont font(_font); font.setPointSizeF(view()->scaledFontSize(_fontScale, *painter->device())); painter->setFont(font); // generate string list of relation names QStringList names; bool allAuto = true; bool sameX = true; bool sameYUnits = true; LabelInfo label_info = legendItems.at(0)->xLabelInfo(); QString yUnits = legendItems.at(0)->yLabelInfo().units; for (int i = 0; i<count; i++) { RelationPtr relation = legendItems.at(i); if (relation->descriptiveNameIsManual()) { allAuto = false; } if (relation->xLabelInfo() != label_info) { sameX = false; } // sameYUnits is false if any non empty units are defined differently. if (yUnits.isEmpty()) { yUnits = relation->yLabelInfo().units; } else if (relation->yLabelInfo().units != yUnits) { if (!relation->yLabelInfo().units.isEmpty()) { sameYUnits = false; } } } if (!allAuto) { for (int i = 0; i<count; i++) { names.append(legendItems.at(i)->descriptiveName()); } } else { for (int i = 0; i<count; i++) { RelationPtr relation = legendItems.at(i); QString label = relation->titleInfo().singleRenderItemLabel(); if (label.isEmpty()) { label_info = relation->yLabelInfo(); QString y_label = label_info.name; if (!sameYUnits) { if (!label_info.units.isEmpty()) { y_label = tr("%1 \\[%2\\]", "axis labels. %1 is quantity, %2 is units. eg Time [s]. '[' must be escaped.").arg(y_label).arg(label_info.units); } } if (!y_label.isEmpty()) { LabelInfo xlabel_info = relation->xLabelInfo(); if (!sameX) { label = tr("%1 vs %2", "describes a plot. %1 is X axis. %2 is Y axis").arg(y_label).arg(xlabel_info.name); } else if (xlabel_info.quantity.isEmpty()) { label = y_label; } else if (xlabel_info.quantity != xlabel_info.name) { label = tr("%1 vs %2", "describes a plot. %1 is X axis. %2 is Y axis").arg(y_label).arg(xlabel_info.name); } else { label = y_label; } } else { label = relation->descriptiveName(); } } int i_dup = names.indexOf(label); if (i_dup<0) { names.append(label); } else { RelationPtr dup_relation = legendItems.at(i_dup); if (!dup_relation->yLabelInfo().file.isEmpty()) { names.replace(i_dup, label + " (" + dup_relation->yLabelInfo().escapedFile() + ')'); } if (!relation->yLabelInfo().file.isEmpty()) { names.append(label + " (" + relation->yLabelInfo().escapedFile() + ')'); } } } } QSize legendSize(0, 0); QSize titleSize(0,0); Label::Parsed *parsed = Label::parse(_title); int pad = painter->fontMetrics().ascent()/4; Label::RenderContext rc(painter->font(), painter); Label::renderLabel(rc, parsed->chunk, false, false); if (!_title.isEmpty()) { titleSize.setWidth(rc.x+3*pad); titleSize.setHeight(painter->fontMetrics().height()+pad); } QList<QSize> sizes; int max_w = 0; int max_h = 0; for (int i = 0; i<count; i++) { RelationPtr relation = legendItems.at(i); QSize size; painter->save(); size = paintRelation(names.at(i), relation, painter, false); painter->restore(); sizes.append(size); max_w = qMax(max_w, size.width()); max_h = qMax(max_h, size.height()); } // determine number of rows and number of columns int n_rows = 0; int n_cols = 0; if (_verticalDisplay) { int h=titleSize.height(); for (int i = 0; i<count; i++) { h+=sizes.at(i).height(); } int max_legend_height = _plotItem->plotRect().height()*0.6+1; n_cols = qMin(count, h / max_legend_height + 1); n_rows = count / n_cols; while (n_rows*n_cols<count) { n_rows++; } } else { int w = 0; for (int i = 0; i<count; i++) { w+=sizes.at(i).width(); } int max_legend_width = _plotItem->plotRect().width()*0.8+1; n_rows = qMin(count, w / max_legend_width+1); n_cols = count/n_rows; while (n_rows*n_cols<count) { n_cols++; } } // determine the dimensions of each column QList<QSize> col_sizes; for (int i=0; i<n_cols; i++) { col_sizes.append(QSize(0,0)); } for (int i = 0; i<count; i++) { int col = i/n_rows; col_sizes[col].rheight()+= sizes.at(i).height(); col_sizes[col].setWidth(qMax(sizes.at(i).width(), col_sizes.at(col).width())); } // determine the dimensions of the legend int w = 0; int h = 0; for (int col = 0; col < n_cols; col++) { w += col_sizes.at(col).width(); h = qMax(h, col_sizes.at(col).height()); } legendSize.setHeight(h + titleSize.height()); legendSize.setWidth(qMax(titleSize.width(), w)); setViewRect(rect().x(), rect().y(), legendSize.width()+pad, legendSize.height()+pad); // Now paint everything painter->drawRect(rect()); int x=rect().x(); int y=rect().y(); if (!_title.isEmpty()) { rc.y = rect().y() + titleSize.height()-pad; rc.x = qMax(rect().x()+pad, rect().x() + legendSize.width()/2 - titleSize.width()/2); Label::renderLabel(rc, parsed->chunk, false, true); y+= titleSize.height(); } legendSize.setWidth(0); legendSize.setHeight(0); for (int i = 0; i<count; i++) { RelationPtr relation = legendItems.at(i); painter->save(); painter->translate(x,y); paintRelation(names.at(i), relation, painter, true); painter->restore(); int col = i/n_rows; int row = i%n_rows; if (row == n_rows-1) { // end of a column x += col_sizes.at(col).width(); y = rect().y() + titleSize.height(); } else { y += sizes.at(i).height(); } } delete parsed; }
void KonqComboItemDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const { QIcon icon = qvariant_cast<QIcon>( index.data( Qt::DecorationRole ) ); QString url = index.data( Qt::DisplayRole ).toString(); QString title = index.data( Qt::UserRole ).toString(); QIcon::Mode mode = option.state & QStyle::State_Enabled ? QIcon::Normal : QIcon::Disabled; const QSize size = icon.actualSize( option.decorationSize, mode ); QPixmap pixmap = icon.pixmap( size, mode ); QStyleOptionViewItemV3 opt( option ); painter->save(); // Draw the item background // ### When Qt 4.4 is released we need to change this code to draw the background // by calling QStyle::drawPrimitive() with PE_PanelItemViewRow. if ( opt.state & QStyle::State_Selected ) { painter->fillRect( option.rect, option.palette.brush( QPalette::Highlight ) ); painter->setPen( QPen( option.palette.brush( QPalette::HighlightedText ), 0 ) ); } int hMargin = QApplication::style()->pixelMetric( QStyle::PM_FocusFrameHMargin ); int vMargin = QApplication::style()->pixelMetric( QStyle::PM_FocusFrameVMargin ); const QRect bounding = option.rect.adjusted( hMargin, vMargin, -hMargin, -vMargin ); const QSize textSize( bounding.width() - pixmap.width() - 2, bounding.height() ); const QRect pixmapRect = QStyle::alignedRect( option.direction, Qt::AlignLeft | Qt::AlignVCenter, pixmap.size(), bounding ); const QRect textRect = QStyle::alignedRect( option.direction, Qt::AlignRight, textSize, bounding ); if ( !pixmap.isNull() ) painter->drawPixmap( pixmapRect.topLeft(), pixmap ); QSize titleSize( ( bounding.width() / 3 ) - 1, textRect.height() ); if (title.isEmpty()) { // Don't truncate the urls when there is no title to show (e.g. local files) // Ideally we would do this globally for all items - reserve space for a title for all, or for none titleSize = QSize(); } const QSize urlSize( textRect.width() - titleSize.width() - 2, textRect.height() ); const QRect titleRect = QStyle::alignedRect( option.direction, Qt::AlignRight, titleSize, textRect ); const QRect urlRect = QStyle::alignedRect( option.direction, Qt::AlignLeft, urlSize, textRect ); if ( !url.isEmpty() ) { QString squeezedText = option.fontMetrics.elidedText( url, Qt::ElideMiddle, urlRect.width() ); painter->drawText( urlRect, Qt::AlignLeft | Qt::AlignVCenter, squeezedText ); } if ( !title.isEmpty() ) { QString squeezedText = option.fontMetrics.elidedText( title, Qt::ElideRight, titleRect.width() ); QFont font = painter->font(); font.setItalic( true ); painter->setFont( font ); QColor color = painter->pen().color(); color.setAlphaF(.75); painter->setPen(color); painter->drawText( titleRect, Qt::AlignLeft | Qt::AlignVCenter, squeezedText ); } painter->restore(); }