QString UpdateItem::elidedChangelog() const { const QString text = QString(clearHTMLTags(m_info.m_changelog)).replace("\n", ""); const QFontMetrics fm(m_appChangelog->font()); const QRect rect(0, 0, 200, fontMetrics().height() * 2); const int textFlag = Qt::AlignTop | Qt::AlignLeft | Qt::TextWordWrap; if (rect.contains(fm.boundingRect(rect, textFlag, text))) return text; QString str(text + "..."); while (true) { if (str.size() < 4) break; QRect boundingRect = fm.boundingRect(rect, textFlag, str); if (rect.contains(boundingRect)) break; str.remove(str.size() - 4, 1); } return str; }
virtual void resizeEvent( QResizeEvent * ) { QFontMetrics fm = fontMetrics(); if(valueText.isEmpty()) { if(longHeadingWidth < width()) setText(longHeadingText); else setText(shortHeadingText); return; } QString value = valueText.first(); int textWidth = fm.boundingRect(value).width(); if(textWidth + longHeadingWidth < width()) setBothText(longHeadingText, value); else if(textWidth + shortHeadingWidth < width()) setBothText(shortHeadingText, value); else { int valueTextCount = valueText.count(); int i; for(i = 1; i < valueTextCount; ++i) { textWidth = fm.boundingRect(valueText.at(i)).width(); if(textWidth + shortHeadingWidth <= width()) { break; } } if(i < valueTextCount) setBothText(shortHeadingText, valueText.at(i)); else setText(noHeadingText + valueText.last()); //This just sets the color of the text } }
QSize AgentTypeWidgetDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const { if ( !index.isValid() ) { return QSize( 0, 0 ); } const QString name = index.model()->data( index, Qt::DisplayRole ).toString(); const QString comment = index.model()->data( index, AgentTypeModel::DescriptionRole ).toString(); QFontMetrics fm = option.fontMetrics; int hn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).height(); int wn = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, name ).width(); int hc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).height(); int wc = fm.boundingRect( 0, 0, 0, 0, Qt::AlignLeft, comment ).width(); int width = 0; int height = 0; if ( !name.isEmpty() ) { height += hn; width = qMax( width, wn ); } if ( !comment.isEmpty() ) { height += hc; width = qMax( width, wc ); } height = qMax( height, 64 ) + 10; width += 64 + 15; return QSize( width, height ); }
//------------------------------------------------------------------------------ // void XxCopyLabel::resizeEvent( QResizeEvent* event ) { setText( _fulltext ); QString tex = text(); QFontMetrics fm = fontMetrics(); QRect br = fm.boundingRect( tex ); while ( br.width() + SAFETY_OFFSET > width() ) { // Remove beginning part // // Note: also check for '\' in case we ever port to Windoze. int pos = -1; if ( tex.indexOf( "[...]/", 0 ) == 0 ) { pos = tex.indexOf( '/', 6 ); } else { pos = tex.indexOf( '/', 0 ); } if ( pos == -1 ) { break; } tex.replace( 0, pos + 1, "[...]/" ); br = fm.boundingRect( tex ); } QLabel::setText( tex ); QLabel::resizeEvent( event ); }
QTime getMaxTime(const QFontMetrics &metrics, const QString &format) { int maxMinSec = 0; for (int width=0, i=0; i<60; ++i) { int w = metrics.boundingRect(QString("%1").arg(i, 2, 10, QChar('0'))).width(); if (w > width) { maxMinSec = i; width = w; } } QTime res; QDateTime dt(QDate(1, 1, 1), QTime(0, maxMinSec, maxMinSec)); int maxWidth = 0; while (dt.date().day() == 1) { int w = metrics.boundingRect(dt.toString(format)).width(); //qDebug() << "*" << dt.toString(format) << w; if (w > maxWidth) { res = dt.time(); maxWidth = w; } dt = dt.addSecs(3600); } //qDebug() << "Max time:" << res.toString(); return res; }
QSize BatteryStatus::sizeHint() const { QString text = statusText(); QString text2 = statusTextIpaq(); QFontMetrics fm = fontMetrics(); QRect r=fm.boundingRect( 10, 0, width(), height(), AlignVCenter, text ); QRect r2=fm.boundingRect( 10, 0, width(), height(), AlignVCenter, text2 ); if ( bat2 ) { return QSize( QMAX( QMIN( 200, qApp->desktop()->width() ), r.width()+2*10 ), 2 * 10 + 100 + r.height() + r2.height() ); } return QSize( QMAX( QMIN( 200, qApp->desktop()->width() ), r.width()+2*10 ), 2 * 10 + 40 + r.height() ); }
void LinearLabel::paintEvent(QPaintEvent *e) { QPainter painter(this); QRect r = rect(); if(m_backgroundPixmap.isNull()) { painter.fillRect(r, m_backgroundColor); } else { painter.drawPixmap(r, m_backgroundPixmap); } QFontMetrics fm = painter.fontMetrics(); QString elidedText = fm.elidedText(m_text, Qt::ElideRight , r.width() - 4 ); if(m_linearGradient) { QRectF boundingRect = fm.boundingRect(r, m_flags,elidedText); m_linearGradient->setStart(boundingRect.topLeft()); m_linearGradient->setFinalStop(boundingRect.bottomLeft()); QPen linearPen(QBrush(*m_linearGradient), m_penWidth); painter.setPen(linearPen); painter.drawText(boundingRect, m_flags, elidedText); } else { QPen pen(m_textColor, m_penWidth); painter.setPen(pen); painter.drawText(r, m_flags, elidedText); } }
void printerStream::printText(const QString &txt, bool newLine) { if (fwbdebug) { qDebug("printText -------"); qDebug("pageBody.height(): %d", pageBody.height()); qDebug("yPos: %d", yPos); } if (txt.isEmpty()) return; if (printer->printerState() == QPrinter::Aborted) return; pr.setFont( bodyFont ); QFontMetrics fm = pr.fontMetrics(); QRect br = fm.boundingRect(txt); if (getYSpace()<br.height()) { flushPage(); beginPage(); // resets yPos } if (pageNo>=fromPage && pageNo<=toPage) { pr.setPen(Qt::black); pr.drawText( xmargin, yPos, printer->width()-2*xmargin, br.height(), Qt::TextExpandTabs | Qt::TextDontClip, txt ); } int nlines=1; int i=-1; while ( (i=txt.indexOf("\n",i+1))>=0 ) nlines++; if (newLine) yPos = yPos + nlines*fm.lineSpacing(); }
//----------------------------------------------------------------------------- //! //----------------------------------------------------------------------------- void tWindPlotGraph::DrawTimeLabelAndGridLine(QPainter& painter, QPen& gridPen, int timeIncrement) { // Work out the Y position for the label and grid line int samplePosition; // Calculate the samplePosition samplePosition = timeIncrement * 60000; samplePosition /= tDigitalTimePlotDataManager::Instance()->GetSamplePeriod(m_TimePlotRange); qreal y = m_GraphConfig.originY + (samplePosition * m_GraphConfig.pixPerTime); QFontMetrics fm = QFontMetrics(painter.font()); // Draw the label QString label = QString("%1").arg(timeIncrement); QRect boundingRect = fm.boundingRect( label ); int w = contentsRect().width(); QRectF textRect = QRectF((w - m_GraphConfig.centreMarginWidth) / 2, y - (boundingRect.height() / 2), m_GraphConfig.centreMarginWidth, boundingRect.height() ); painter.drawText(textRect, Qt::AlignCenter, label ); QPen savedPen = painter.pen(); // draw grid lines if ( (samplePosition != 0) && (samplePosition != m_TimePlotData[0].GetMaxNumberOfSamples()) ) { painter.setPen( gridPen ); painter.drawLine( m_GraphConfig.originX[0], static_cast<int>(y), m_GraphConfig.originX[0] + m_GraphConfig.extentX[0], static_cast<int>(y) ); painter.drawLine( m_GraphConfig.originX[1], static_cast<int>(y), m_GraphConfig.originX[1] + m_GraphConfig.extentX[1], static_cast<int>(y) ); } painter.setPen(savedPen); }
void Vruler::drawNumber(QString num, int starty, QPainter *p) { int textY = starty; for (int a = 0; a < num.length(); ++a) { QString txt = num.mid(a, 1); #ifndef Q_WS_MAC p->drawText(1, textY, txt); #else static const int SCALE = 16; QFontMetrics fm = p->fontMetrics(); QRect bbox = fm.boundingRect(txt); static QPixmap pix; if (pix.width() < bbox.width()*SCALE || pix.height() < bbox.height()*SCALE) pix = QPixmap(bbox.width()*SCALE, bbox.height()*SCALE); QFont fnt = p->font(); QPainter p2; pix.fill(); p2.begin( &pix ); if (fnt.pointSize() > 0) fnt.setPointSize(SCALE*fnt.pointSize()-SCALE/2); else if (fnt.pixelSize() > 0) fnt.setPixelSize(SCALE*fnt.pixelSize()-SCALE/2); else fnt.setPixelSize(SCALE); p2.setFont(fnt); p2.drawText(-bbox.x()*SCALE, -bbox.y()*SCALE, txt); p2.end(); p->scale(1.0/SCALE,1.0/SCALE); p->drawPixmap(1*SCALE, (textY+bbox.top())*SCALE, pix, 0, 0, bbox.width()*SCALE, bbox.height()*SCALE); p->scale(SCALE,SCALE); #endif textY += 11; } }
// --- // paint() - draw stuff here // --- void PlayerItem::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*) { // get our width and height from our icon const int w = size.width(); const int h = size.height(); if (defaultImage != nullptr) { painter->drawImage(QRectF(QPointF(-w/2, -h/2), QSizeF(w, h)), *defaultImage); } else painter->drawEllipse(0, 0, 30, 30); // draw the player's name QFontMetrics fm = painter->fontMetrics(); QPen pen(Qt::white, 2.0f); QRectF fontRect = fm.boundingRect(name + "X"); painter->save(); double hdg = rotation(); painter->rotate(-hdg); if (hdg < 270 && hdg > 90) { painter->translate(-fontRect.width()/2, -size.height() + fontRect.height() - 5); } else { painter->translate(-fontRect.width()/2, size.height() + 5); } QColor sideColor(qRgba(0, 0, 0, 255)); sideColor.setAlpha(150); painter->setBrush(sideColor); painter->setPen(pen); painter->matrix().mapRect(fontRect); painter->drawRoundedRect(fontRect, 5, 5); painter->drawText(fontRect, Qt::AlignCenter, name); painter->restore(); }
QSize IPLineEdit::sizeHint() const { QFontMetrics fm = fontMetrics(); QSize s; s.setHeight(QLineEdit::sizeHint().height()); s.setWidth(fm.boundingRect("888.888.888.888XX").width()); return s; }
void QZuesLabel::paintEvent(QPaintEvent *event) { switch (m_type) { case LABEL_TYPE_NORMAL: QLabel::paintEvent(event); break; case LABEL_TYPE_AUTO_CUT: { QSize newSize = contentsRect().size(); QFontMetrics metrics = fontMetrics(); int contentLength = metrics.boundingRect(m_contetnt).width(); if (contentLength > newSize.width()) { for (int j = m_contetnt.count(); j > 0; --j) { contentLength = metrics.boundingRect(m_contetnt.mid(0, j) + m_placeholdText).width(); if (contentLength <= newSize.width()) { QLabel::setText(m_contetnt.mid(0, j - 1) + m_placeholdText); break; } } } else { QLabel::setText(m_contetnt); } QLabel::paintEvent(event); } break; case LABEL_TYPE_MARQUEE: { QLabel::paintEvent(event); QPainter painter(this); painter.drawText(m_contentsRect, m_contetnt); } break; default: break; } }
void MiniView::setTitle(const QString & title) { m_title = title; QFont font; font.setFamily(FontFamily); font.setWeight(QFont::Bold); font.setPixelSize(FontPixelSize); setFont(font); QFontMetrics metrics = fontMetrics(); QRect br = metrics.boundingRect(m_title); parentWidget()->setMinimumWidth(br.width()); }
void LoginPage::redrawCountryCode() { QFontMetrics fm = country_code_->fontMetrics(); int w = fm.boundingRect(country_code_->text()).width() + 5; QRect content = phone_widget_->contentsRect(); country_code_->resize(w, country_code_->height()); phone_->resize(content.width() - w, phone_->height()); country_code_->move(content.x(), content.y()); phone_->move(content.x() + w, content.y()); }
void DPageListViewDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const { if ( !index.isValid() ) return; painter->setRenderHint( QPainter::Antialiasing ); const QString text = index.model()->data( index, Qt::DisplayRole ).toString(); const QIcon icon = index.model()->data( index, Qt::DecorationRole ).value<QIcon>(); const QPixmap pixmap = icon.pixmap( 32, 32 ); QFontMetrics fm = painter->fontMetrics(); int ht = fm.boundingRect( 0, 0, 0, 0, Qt::AlignCenter, text ).height(); int wt = fm.boundingRect( 0, 0, 0, 0, Qt::AlignCenter, text ).width(); int wp = pixmap.width(); int hp = pixmap.height(); QPen pen = painter->pen(); QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled; if (cg == QPalette::Normal && !(option.state & QStyle::State_Active)) cg = QPalette::Inactive; if (option.state & QStyle::State_Selected) { painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight)); painter->setPen(option.palette.color(cg, QPalette::HighlightedText)); } else { painter->setPen(option.palette.color(cg, QPalette::Text)); } QFont font = painter->font(); painter->setFont(option.font); painter->drawPixmap( option.rect.x() + (option.rect.width()/2)-(wp/2), option.rect.y() + 5, pixmap ); if ( !text.isEmpty() ) painter->drawText( option.rect.x() + (option.rect.width()/2)-(wt/2), option.rect.y() + hp+7, wt, ht, Qt::AlignCenter, text ); painter->setFont(font); painter->setPen(pen); drawFocus( painter, option, option.rect ); }
QSize MailTreeDelegate::sizeHint (const QStyleOptionViewItem& option, const QModelIndex& index) const { const auto& subjFontInfo = GetSubjectFont (index, option); const QFontMetrics plainFM { option.font }; const auto width = View_->viewport ()->width (); const auto height = 2 * Padding + subjFontInfo.second.boundingRect (GetString (index, MailModel::Column::Subject)).height () + plainFM.boundingRect (GetString (index, MailModel::Column::From)).height (); return { width, height }; }
void GxsIdChooser::myCurrentIndexChanged(int index) { Q_UNUSED(index); QFontMetrics fm = QFontMetrics(font()); QString text = currentText(); if (width() < fm.boundingRect(text).width()) { setToolTip(text); } else { setToolTip(""); } }
int toResultViewItem::realWidth(const QFontMetrics &fm, const toTreeWidget *top, int column, const QString &txt) const { if (!MaxColDisp) { MaxColDisp = toConfigurationSingle::Instance().maxColDisp(); Gridlines = toConfigurationSingle::Instance().displayGridlines(); } QString t = text(column); if (t.isNull()) t = txt; QRect bounds = fm.boundingRect(t); return std::min(bounds.width(), MaxColDisp) + top->itemMargin() * 2 - fm.minLeftBearing() - fm.minRightBearing() + 1; }
void set_widget_width_for_text( QWidget* widget, const QString& text, const int margin = 0, const int min_width = 0) { const QFontMetrics metrics(widget->font()); const int text_width = metrics.boundingRect(text).width(); const int final_width = max(text_width + margin, min_width); widget->setMinimumWidth(final_width); widget->setMaximumWidth(final_width); }
int RegSexyDisplay::maxContentHeight() const { int max = 0; QFontMetrics metrics = fontMetrics(); for(size_t i = 0; i < m_reg.GetReg().field.size(); i++) { QString s = QString::fromStdString(m_reg.GetReg().field[i].name); // add extra spaces arounds s = " " + s + " "; max = qMax(max, metrics.boundingRect(s).width()); } return 2 * marginSize() + max; }
QSize TimeTrackingView::sizeHint() const { if ( ! m_cachedSizeHint.isValid() ) { // the sizeHint is like the minimum size hint, only it allows // for more text in the task name column const QFontMetrics narrowFontMetrics = QFontMetrics( m_narrowFont ); const QRect textRect = narrowFontMetrics.boundingRect( tr( "moremoremoremoremore" ) ); const int widthHint = minimumSizeHint().width() + textRect.width(); m_cachedSizeHint = QSize( widthHint, minimumSizeHint().height() ); } return m_cachedSizeHint; }
QRectF TaskUIElement::boundingRect() const { Board *parent = (Board *) parentItem(); QFontMetrics fontMetrics = QFontMetrics(font); QRect rect = fontMetrics.boundingRect(0, 0, parent->getTaskWidth(), minimumHeight, Qt::TextWordWrap, text); return (QRectF) rect; }
//----------------------------------------------------------------------------- //! Update the graphic configuration for the current data //----------------------------------------------------------------------------- void tHeelCorrectionGraphicWidget::UpdateConfig() { // Save enough space to the left of the graph to write the boat speed values on the axis QFontMetrics fm = QFontMetrics(m_AxisLabelFont); QRect boundingRect = fm.boundingRect( QString( tr("Correction") ) ); m_LeftMarginWidth = boundingRect.width() + 10; m_TopMarginHeight = fm.lineSpacing() / 2; m_BottomMarginHeight = fm.lineSpacing() / 2; m_GraphicHeight = this->rect().height() - m_TopMarginHeight - m_BottomMarginHeight; m_GraphicWidth = this->rect().width() - m_LeftMarginWidth - m_cRightMarginWidth; }
void QZuesLabel::timerEvent(QTimerEvent *event) { if (m_maqueeTimerId != -1 && event->timerId() == m_maqueeTimerId) { m_contentsRect.setX(m_contentsRect.x() - m_stepLength); m_contentsRect.setWidth(m_contentsRect.width() + m_stepLength); QFontMetrics metrics = fontMetrics(); int contentLength = metrics.boundingRect(m_contetnt).width(); if (m_contentsRect.width() >= contentLength) { m_contentsRect = contentsRect(); } update(); } }
QSize DPageListViewDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const { if ( !index.isValid() ) return QSize( 0, 0 ); const QString text = index.model()->data( index, Qt::DisplayRole ).toString(); const QIcon icon = index.model()->data( index, Qt::DecorationRole ).value<QIcon>(); const QPixmap pixmap = icon.pixmap( 32, 32 ); QFontMetrics fm = option.fontMetrics; int ht = fm.boundingRect( 0, 0, 0, 0, Qt::AlignCenter, text ).height(); int wt = fm.boundingRect( 0, 0, 0, 0, Qt::AlignCenter, text ).width() + 10; int wp = pixmap.width() + 10; int width, height; if ( text.isEmpty() ) height = pixmap.height(); else height = pixmap.height() + ht + 10; width = qMax( wt, wp ); return QSize( width, height ); }
std::list<int> toLegendChart::sizeHint(int &height, int &items) { QFontMetrics fm = fontMetrics(); int count = 0; { for (std::list<QString>::iterator i = Labels.begin(); i != Labels.end(); i++) if (!(*i).isEmpty() && *i != " ") count++; } items = (count + Columns - 1) / Columns; height = 0; int width = 0; int cheight = 0; int cur = 0; std::list<int> ret; for (std::list<QString>::iterator i = Labels.begin(); i != Labels.end(); i++) { if (!(*i).isEmpty() && *i != " ") { if (cur == items) { ret.insert(ret.end(), width); if (cheight > height) height = cheight; cheight = 0; width = 0; cur = 0; } QRect bounds = fm.boundingRect(0, 0, 10000, 10000, FONT_ALIGN, *i); if (width < bounds.width()) width = bounds.width(); cheight += bounds.height(); cur++; } } if (width > 0) { ret.insert(ret.end(), width); } if (cheight > height) height = cheight; return ret; }
static int TextWidth(const QFontMetrics &fm, const QString &str) { int lpos = 0; int pos = 0; int maxWidth = 0; do { pos = str.indexOf("\n", lpos); QRect bounds = fm.boundingRect(str.mid(lpos, pos - lpos)); if (bounds.width() > maxWidth) maxWidth = bounds.width(); lpos = pos + 1; } while (pos >= 0); return maxWidth; }
static void drawHorizCmRuler(QPainter &painter, int x1, int x2, int y) { painter.drawLine(x1, y, x2, y); const int dpI = painter.device()->logicalDpiX(); const int dpCm = qRound(double(dpI) / 2.54); const int h = dpCm / 2; const QFontMetrics fm(painter.font()); for (int cm = 0, x = x1; x < x2; x += dpCm, ++cm) { painter.drawLine(x, y, x, y - h); if (cm) { const QString n = QString::number(cm); const QRect br = fm.boundingRect(n); painter.drawText(x - br.width() / 2, y - h - 10, n); } } }
static void drawVertCmRuler(QPainter &painter, int x, int y1, int y2) { painter.drawLine(x, y1, x, y2); const int dpI = painter.device()->logicalDpiY(); const int dpCm = qRound(double(dpI) / 2.54); const int h = dpCm / 2; const QFontMetrics fm(painter.font()); for (int cm = 0, y = y1; y < y2; y += dpCm, ++cm) { painter.drawLine(x, y, x + h, y); if (cm) { const QString n = QString::number(cm); const QRect br = fm.boundingRect(n); painter.drawText(x + h + 10, y + br.height() / 2, n); } } }