Exemplo n.º 1
2
QPixmap QzTools::createPixmapForSite(const QIcon &icon, const QString &title, const QString &url)
{
    const QFontMetrics fontMetrics = QApplication::fontMetrics();
    const int padding = 4;
    const int maxWidth = fontMetrics.width(title.length() > url.length() ? title : url) + 3 * padding + 16;

    const int width = qMin(maxWidth, 150);
    const int height = fontMetrics.height() * 2 + fontMetrics.leading() + 2 * padding;

    QPixmap pixmap(width, height);
    QPainter painter(&pixmap);

    // Draw background
    QPen pen(Qt::black);
    pen.setWidth(1);
    painter.setPen(pen);

    painter.fillRect(QRect(0, 0, width, height), Qt::white);
    painter.drawRect(0, 0, width - 1, height - 1);

    // Draw icon
    QRect iconRect(0, 0, 16 + 2 * padding, height);
    icon.paint(&painter, iconRect);

    // Draw title
    QRect titleRect(iconRect.width(), padding, width - padding - iconRect.width(), fontMetrics.height());
    painter.drawText(titleRect, fontMetrics.elidedText(title, Qt::ElideRight, titleRect.width()));

    // Draw url
    QRect urlRect(titleRect.x(), titleRect.bottom() + fontMetrics.leading(), titleRect.width(), titleRect.height());
    painter.setPen(QApplication::palette().color(QPalette::Link));
    painter.drawText(urlRect, fontMetrics.elidedText(url, Qt::ElideRight, urlRect.width()));

    return pixmap;
}
Exemplo n.º 2
1
void EPGItem::paint( QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*)
{
    // Draw in view's coordinates
    painter->setWorldMatrixEnabled( false );

    // Draw high-quality items
    //painter->setRenderHint( QPainter::Antialiasing );

    // Get the transformations required to map the text on the viewport
    QTransform viewPortTransform = m_view->viewportTransform();
    QRectF mapped = deviceTransform( viewPortTransform ).mapRect( boundingRect() );

    if ( m_current )
    {
        painter->setBrush( QBrush( QColor( 244, 102, 146 ) ) );
        painter->setPen( QPen( QColor( 244, 102, 146 ) ) );
    }
    else
    {
        painter->setBrush( QBrush( QColor( 201, 217, 242 ) ) );
        painter->setPen( QPen( QColor( 201, 217, 242 ) ) );
    }

    mapped.adjust( 1, 2, -1, -2 );
    painter->drawRoundedRect( mapped, 10, 10 );

    /* Draw text */

    // Setup the font
    QFont f = painter->font();

    // Get the font metrics
    QFontMetrics fm = painter->fontMetrics();

    // Adjust the drawing rect
    mapped.adjust( 6, 6, -6, -6 );

    painter->setPen( Qt::black );
    /* Draw the title. */
    painter->drawText( mapped, Qt::AlignTop | Qt::AlignLeft, fm.elidedText( m_name, Qt::ElideRight, mapped.width() ) );

    mapped.adjust( 0, 20, 0, 0 );
    QDateTime m_end = m_start.addSecs( m_duration );
    f.setPixelSize( 10 );
    f.setItalic( true );
    painter->setFont( f );

    /* Draw the hours. */
    painter->drawText( mapped, Qt::AlignTop | Qt::AlignLeft,
                       fm.elidedText( m_start.toString( "hh:mm" ) + " - " +
                                      m_end.toString( "hh:mm" ),
                                      Qt::ElideRight, mapped.width() ) );
}
Exemplo n.º 3
0
void EvButton::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::HighQualityAntialiasing,true);


    QPainterPath backgroundPath = this->backgroundPath();

    if(isDown() || isChecked()){
        painter.fillPath(backgroundPath,m_pressedColor);
    }
    else{
        painter.fillPath(backgroundPath,m_backgroundColor);
    }

    if(m_hover){       
        painter.fillPath(backgroundPath,m_hoverColor);
    }

    switch (m_paintMode) {
    case TextOnly:{
        const QFontMetrics fm = fontMetrics();
        QRect textRect  = this->textRect();
        painter.setFont(font());
        painter.setPen(m_textColor);
        painter.drawText(textRect,Qt::AlignCenter ,fm.elidedText(text(), Qt::ElideRight, textRect.width()));
        break;
    }
    case IconOnly:{
        QRect iconRect = this->iconRect();
        painter.drawPixmap(iconRect,icon().pixmap(iconRect.size()));
        break;
    }
    case IconAndText:{
        const QFontMetrics fm = fontMetrics();
        QRect textRect  = this->textRect();
        QRect iconRect = this->iconRect();

        painter.drawPixmap(iconRect,icon().pixmap(iconRect.size()));

        painter.setFont(font());

        painter.setPen(m_textColor);
        painter.drawText(textRect,Qt::AlignCenter ,fm.elidedText(text(), Qt::ElideRight, textRect.width()));

        break;
    }
    default:
        break;
    }

}
Exemplo n.º 4
0
QString pathElidedText(const QFontMetrics &fm, const QString &text, int width, int flags)
{
	const QString filename = "/" + QFileInfo(text).fileName();
	const QString path = QFileInfo(text).absolutePath();

	int fwidth = fm.width(filename);

	if (fwidth > width)
		// we're SOL, just do the default
		return fm.elidedText(text, Qt::ElideMiddle, width, flags);

	return fm.elidedText(path, Qt::ElideMiddle, width - fwidth, flags) + filename;
}
Exemplo n.º 5
0
void ElidedLabel::paintEvent(QPaintEvent* e)
{
  QFrame::paintEvent(e);
  if(rect().width() <= 0 || rect().height() <= 0)
    return;
  QPainter painter(this);

  const QRect r = rect();
  const QRect ar = r.adjusted(1, 1, -1, -1);

  ItemBackgroundPainter ibp;
  ibp.drawBackground(&painter, r, palette(), 1, 1, !hasOffMode() || !isOff() ? r : QRect());

  if (hasFocus())
        {
        if (_hovered)
              painter.setPen(QPen(QColor(239,239,239)));
        else
              painter.setPen(QPen(Qt::white));
        }
  else if (_hovered)
        painter.setPen(QPen(QColor(48,48,48)));
  else
        painter.setPen(QPen(Qt::black));

  painter.setRenderHint(QPainter::Antialiasing);
  painter.setFont(_curFont);
  QFontMetrics fm = painter.fontMetrics();
  QString elidedText = fm.elidedText(_text, _elideMode, r.width());
//   painter.drawText(QPoint(0, fm.ascent()), elidedText);

  painter.drawText(ar, _alignment, elidedText);
}
Exemplo n.º 6
0
QString QzTools::alignTextToWidth(const QString &string, const QString &text, const QFontMetrics &metrics, int width)
{
    int pos = 0;
    QString returnString;

    while (pos <= string.size()) {
        QString part = string.mid(pos);
        QString elidedLine = metrics.elidedText(part, Qt::ElideRight, width);

        if (elidedLine.isEmpty()) {
            break;
        }

        if (elidedLine.size() != part.size()) {
            elidedLine = elidedLine.left(elidedLine.size() - 3);
        }

        if (!returnString.isEmpty()) {
            returnString += text;
        }

        returnString += elidedLine;
        pos += elidedLine.size();
    }

    return returnString;
}
Exemplo n.º 7
0
void TitleWidget::updateTitle()
{
    qreal maxStopNameWidth = contentsRect().width() - m_icon->boundingRect().right() - 10;
    if ( m_filterWidget ) {
        maxStopNameWidth -= m_filterWidget->boundingRect().width();
    }
    if ( m_journeysWidget ) {
        maxStopNameWidth -= m_journeysWidget->boundingRect().width();
    }

    if ( !m_titleText.contains(QRegExp("<\\/?[^>]+>")) ) {
        const QFontMetrics fm( m_title->font() );
        QString _title = m_titleText;
        const int width = fm.width( _title );
        if ( width > maxStopNameWidth ) {
            // Try to break into two lines
            const int wrapPos = _title.lastIndexOf( ' ', _title.length() / 2 );
            QStringList lines;
            if ( wrapPos == -1 ) {
                lines << _title;
            } else {
                lines << _title.left( wrapPos ) << _title.mid( wrapPos + 1);
            }

            // Elide each text line and construct new title
            _title.clear();
            foreach ( const QString &line, lines ) {
                if ( !_title.isEmpty() ) {
                    _title.append( "<br />" );
                }
                _title.append( fm.elidedText(line, Qt::ElideRight, maxStopNameWidth) );
            }
        }
        m_title->setText( _title );
    } else {
Exemplo n.º 8
0
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);
    }
}
Exemplo n.º 9
0
void LineEdit::paintEvent(QPaintEvent *event)
{
    if (hasFocus() && !isReadOnly() && isEnabled())
    {
        QLineEdit::paintEvent(event);
    }
    else
    {
        QPainter p(this);
        QRect r = rect();
        QPalette pal = palette();

        QStyleOptionFrameV2 panel;
        initStyleOption(&panel);
        style()->drawPrimitive(QStyle::PE_PanelButtonBevel, &panel, &p, this);
        r = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);

        QPen oldpen = p.pen();
        p.setPen(QColor(Qt::darkBlue));

        QFontMetrics fm = fontMetrics();

        Q_D(LineEdit);
        QString elidedText = fm.elidedText(d->labelText,
                                           Qt::ElideRight, r.width());
        p.drawText(r, Qt::AlignLeft | Qt::AlignVCenter, elidedText);
        p.setPen(oldpen);
    }
}
Exemplo n.º 10
0
void IconLabel::resizeEvent(QResizeEvent *) {
  if(m_elided) {
    QFontMetrics fm = QFontMetrics(m_label->font());
    int maxWidth = m_label->width();
    QString elidedString = fm.elidedText(m_text, Qt::ElideRight, maxWidth);
    m_label->setText(elidedString);
  }
}
Exemplo n.º 11
0
void FilesDialog::setSelectedFile(const QString &file)
{
	//ROS_INFO("Selected file: %s", file.toStdString().c_str());

	//I should do something when the path is empty, but, the way the signals are emmitted,
	//that sould not be an problem
	if (!_path.isEmpty()) {
		_file = file;
		if (_file.isEmpty()) {
			_ui.deleteFolderButton->setEnabled(true);
			_ui.createFolderButton->setEnabled(true);
			_ui.deleteFileButton->setEnabled(false);

			_ui.currentFileLabel->setText("No file selected.");

			//And now... for the elipsis
			QFontMetrics metrics = _ui.currentFolderLabel->fontMetrics();
			if (metrics.width(_file) > _ui.currentFolderLabel->width()) {
				_ui.currentFolderLabel->setText(metrics.elidedText(_file, Qt::ElideLeft, _ui.currentFolderLabel->width()));
			} else {
				_ui.currentFolderLabel->setText(_file);
			}
		} else {
			_ui.deleteFolderButton->setEnabled(true);
			_ui.createFolderButton->setEnabled(true);
			_ui.deleteFileButton->setEnabled(true);

			//And now... for the elipsis
			QFontMetrics metrics = _ui.currentFolderLabel->fontMetrics();
			if (metrics.width(_path) > _ui.currentFolderLabel->width()) {
				_ui.currentFolderLabel->setText(metrics.elidedText(_path, Qt::ElideLeft, _ui.currentFolderLabel->width()));
			} else {
				_ui.currentFolderLabel->setText(_path);
			}

			metrics = _ui.currentFileLabel->fontMetrics();
			if (metrics.width(_file) > _ui.currentFileLabel->width()) {
				_ui.currentFileLabel->setText(metrics.elidedText(_file, Qt::ElideLeft, _ui.currentFileLabel->width()));
			} else {
				_ui.currentFileLabel->setText(_file);
			}
		}
	}
}
Exemplo n.º 12
0
QString CustomLabel::elidedText() const
{
    QString elided = text();
    QStringList srcLines;
    QStringList dstLines;
    QRectF lr = contentsRect();
    int textWidth = lr.width();
    // eliding text
    // TODO: move to text change / resize event handler, make elided a member
    if (elideMode() != Qt::ElideNone)
    {
        QFontMetrics fm = fontMetrics();
        if (!wordWrap())
        {
            // single line elide
            elided = fm.elidedText(text(), elideMode(), textWidth);
        }
        else if (elideMode() == Qt::ElideRight)
        {
            // multiline elide
            srcLines = elided.split("\n");
            int pxPerLine = fm.lineSpacing();
            int lines = lr.height() / pxPerLine + 1;

            foreach (QString srcLine, srcLines)
            {
                int w = fm.width(srcLine);
                if (w >= textWidth)
                {
                    QStringList tmpList = srcLine.split(' ');
                    QString s;
                    int i = 0;
                    while (i < tmpList.count())
                    {
                        if (fm.width(s + " " + tmpList.at(i)) >= textWidth)
                        {
                            if (!s.isEmpty())
                            {
                                dstLines += s;
                                s = QString::null;
                            }
                        }
                        if (!s.isEmpty())
                        {
                            s += " ";
                        }
                        s += tmpList.at(i);
                        i++;
                    }
                    dstLines += s;
                }
                else
                {
                    dstLines += srcLine;
                }
            }
Exemplo n.º 13
0
void IconLabel::setText(const QString& text) {
  if (el_mode_ == Qt::ElideNone) {
    text_->setText(text);
    return;
  }

  QFontMetrics metrics = text_->fontMetrics();
  QString elidedText = metrics.elidedText(text, el_mode_, text_->width());
  text_->setText(elidedText);
}
Exemplo n.º 14
0
static QVector<QString> splitInTwoLines(const QString &text, const QFontMetrics &fontMetrics,
                                        qreal availableWidth)
{
    // split in two lines.
    // this looks if full words can be split off at the end of the string,
    // to put them in the second line. First line is drawn with ellipsis,
    // second line gets ellipsis if it couldn't split off full words.
    QVector<QString> splitLines(2);
    QRegExp rx(QLatin1String("\\s+"));
    int splitPos = -1;
    int nextSplitPos = text.length();
    do {
        nextSplitPos = rx.lastIndexIn(text,
                                      nextSplitPos - text.length() - 1);
        if (nextSplitPos != -1) {
            int splitCandidate = nextSplitPos + rx.matchedLength();
            if (fontMetrics.width(text.mid(splitCandidate)) <= availableWidth) {
                splitPos = splitCandidate;
            } else {
                break;
            }
        }
    } while (nextSplitPos > 0 && fontMetrics.width(text.left(nextSplitPos)) > availableWidth);
    // check if we could split at white space at all
    if (splitPos < 0) {
        splitLines[0] = fontMetrics.elidedText(text, Qt::ElideRight,
                                                       availableWidth);
        QString common = Manhattan::commonPrefix(QStringList()
                                             << splitLines[0] << text);
        splitLines[1] = text.mid(common.length());
        // elide the second line even if it fits, since it is cut off in mid-word
        while (fontMetrics.width(QChar(0x2026) /*'...'*/ + splitLines[1]) > availableWidth
               && splitLines[1].length() > 3
               /*keep at least three original characters (should not happen)*/) {
            splitLines[1].remove(0, 1);
        }
        splitLines[1] = QChar(0x2026) /*'...'*/ + splitLines[1];
    } else {
        splitLines[0] = fontMetrics.elidedText(text.left(splitPos).trimmed(), Qt::ElideRight, availableWidth);
        splitLines[1] = text.mid(splitPos);
    }
    return splitLines;
}
Exemplo n.º 15
0
QString elideText(const QString &text, QWidget *widget, int width)
{
	if (width < 0)
	{
		width = (QApplication::desktop()->screenGeometry().width() / 4);
	}

	const QFontMetrics fontMetrics = (widget ? widget->fontMetrics() : QApplication::fontMetrics());

	return fontMetrics.elidedText(text, (QGuiApplication::isLeftToRight() ? Qt::ElideRight : Qt::ElideLeft), qMax(100, width));
}
Exemplo n.º 16
0
void ElidedLabel::paintEvent(QPaintEvent* e)
{
  QFrame::paintEvent(e);
  if(rect().width() <= 0 || rect().height() <= 0)
    return;
  QPainter painter(this);
  painter.setFont(_curFont);
  QFontMetrics fm = painter.fontMetrics();
  QString elidedText = fm.elidedText(_text, _elideMode, width());
//   painter.drawText(QPoint(0, fm.ascent()), elidedText);
  painter.drawText(rect(), Qt::AlignLeft | Qt::AlignVCenter, elidedText);
}
Exemplo n.º 17
0
void SqueezeLabel::paintEvent(QPaintEvent *event) {
  if (m_squeezedTextCache != text()) {
    m_squeezedTextCache = text();
    QFontMetrics fm = fontMetrics();

    if (fm.width(m_squeezedTextCache) > contentsRect().width()) {
      setText(fm.elidedText(text(), Qt::ElideMiddle, width()));
    }
  }

  QLabel::paintEvent(event);
}
Exemplo n.º 18
0
void Text::regenerate()
{
    if (!doc)
    {
        doc = DocumentCache::getInstance().pop();
        dirty = true;
    }

    if (dirty)
    {
        doc->setDefaultFont(defFont);

        if (elide)
        {
            QFontMetrics metrics = QFontMetrics(defFont);
            QString elidedText = metrics.elidedText(text, Qt::ElideRight, qRound(width));

            doc->setPlainText(elidedText);
        }
        else
        {
            doc->setDefaultStyleSheet(defStyleSheet);
            doc->setHtml(text);
        }

        // wrap mode
        QTextOption opt;
        opt.setWrapMode(elide ? QTextOption::NoWrap : QTextOption::WrapAtWordBoundaryOrAnywhere);
        doc->setDefaultTextOption(opt);

        // width
        doc->setTextWidth(width);
        doc->documentLayout()->update();

        // update ascent
        if (doc->firstBlock().layout()->lineCount() > 0)
            ascent = doc->firstBlock().layout()->lineAt(0).ascent();

        // let the scene know about our change in size
        if (size != idealSize())
            prepareGeometryChange();

        // get the new width and height
        size = idealSize();

        dirty = false;
    }

    // if we are not visible -> free mem
    if (!keepInMemory)
        freeResources();
}
Exemplo n.º 19
0
void FilesDialog::resizeEvent(QResizeEvent *event)
{
	QDialog::resizeEvent(event);

	if (!_path.isEmpty()) {
		QFontMetrics metrics = _ui.currentFolderLabel->fontMetrics();
		if (metrics.width(_path) > _ui.currentFolderLabel->width()) {
			_ui.currentFolderLabel->setText(metrics.elidedText(_path, Qt::ElideLeft, _ui.currentFolderLabel->width()));
		} else {
			_ui.currentFolderLabel->setText(_path);
		}
	}

	if (!_file.isEmpty()) {
		QFontMetrics metrics = _ui.currentFileLabel->fontMetrics();
		if (metrics.width(_file) > _ui.currentFileLabel->width()) {
			_ui.currentFileLabel->setText(metrics.elidedText(_file, Qt::ElideLeft, _ui.currentFileLabel->width()));
		} else {
			_ui.currentFileLabel->setText(_file);
		}
	}
}
Exemplo n.º 20
0
void SqueezeLabel::paintEvent(QPaintEvent *event)
{
    QFontMetrics fm = fontMetrics();
    if (fm.width(text()) > contentsRect().width()) {
        QString elided = fm.elidedText(text(), Qt::ElideMiddle, width());
        QString oldText = text();
        setText(elided);
        QLabel::paintEvent(event);
        setText(oldText);
    } else {
        QLabel::paintEvent(event);
    }
}
Exemplo n.º 21
0
void PropertiesEditorItem::setButtonUrl(QPushButton* button, const QUrl& url)
{
    if (url.isValid()) {
        QFontMetrics metrics = button->fontMetrics();
        QString elidedText = metrics.elidedText(url.toString(), Qt::ElideLeft, button->width());

        button->setText(elidedText);
        button->setToolTip(url.toString());
    } else {
        button->setText(tr("Open File..."));
        button->setToolTip(QString());
    }
}
Exemplo n.º 22
0
void PlaylistDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    if ((option.state) & (QStyle::State_Selected)) {
        painter->drawImage(option.rect, QImage("/etc/hildon/theme/images/TouchListBackgroundPressed.png"));
    }
    else {
        painter->drawImage(option.rect, QImage("/etc/hildon/theme/images/TouchListBackgroundNormal.png"));
    }
   
    QImage image = m_cache->image(index.data(m_thumbnailRole).toString(), QSize(64, 64));
    
    if (image.isNull()) {
        image = QImage("/usr/share/icons/hicolor/64x64/hildon/mediaplayer_default_album.png");
    }
    
    QRect imageRect = option.rect;
    imageRect.setLeft(imageRect.left() + 8);
    imageRect.setWidth(imageRect.height());
    
    drawCenteredImage(painter, imageRect, image);
    
    QRect textRect = option.rect;
    textRect.setLeft(imageRect.right() + 8);
    textRect.setRight(textRect.right() - 8);
    textRect.setTop(textRect.top() + 8);
    textRect.setBottom(textRect.bottom() - 8);
    
    QFontMetrics fm = painter->fontMetrics();
    
    QString trackCount = tr("%1 tracks").arg(index.data(m_trackCountRole).toInt());
    QString title = fm.elidedText(index.data(m_titleRole).toString(), Qt::ElideRight,
                                  textRect.width() - fm.width(trackCount) - 8);
    
    painter->drawText(textRect, Qt::AlignLeft | Qt::AlignTop, title);
    painter->drawText(textRect, Qt::AlignRight | Qt::AlignTop, trackCount);
        
    QFont font;
    font.setPointSize(13);
    
    QString artist = index.data(m_artistRole).toString();
    
    if (artist.isEmpty()) {
        artist = tr("Unknown artist");
    }
        
    painter->save();
    painter->setFont(font);
    painter->setPen(QApplication::palette().color(QPalette::Mid));
    painter->drawText(textRect, Qt::AlignLeft | Qt::AlignBottom,
                      QFontMetrics(font).elidedText(artist, Qt::ElideRight, textRect.width()));
    painter->restore();
}
Exemplo n.º 23
0
void MoodLineEdit::paintEvent(QPaintEvent *e)
{
    QPainter p( this );
    QFontMetrics fm = p.fontMetrics();
    if ( underMouse() )
    {
        p.save();
        p.setPen( QPen( Qt::lightGray ));
        p.drawRect( this->rect().adjusted( 1, 1, -1, -1 ) ) ;
        p.restore();
    }
    p.drawText( this->rect().adjusted( 2, 1, -1, -1 ),
                Qt::AlignVCenter,
                fm.elidedText( lineEdit->text().isEmpty() ? strMood : lineEdit->text(),
                               Qt::ElideRight, this->rect().width() ));
}
Exemplo n.º 24
0
//! [2]
void ElidedLabel::paintEvent(QPaintEvent *event)
{
    QFrame::paintEvent(event);

    QPainter painter(this);
    QFontMetrics fontMetrics = painter.fontMetrics();

    bool didElide = false;
    int lineSpacing = fontMetrics.lineSpacing();
    int y = 0;

    QTextLayout textLayout(content, painter.font());
    textLayout.beginLayout();
    forever {
        QTextLine line = textLayout.createLine();

        if (!line.isValid())
            break;

        line.setLineWidth(width());
        int nextLineY = y + lineSpacing;

        if (height() >= nextLineY + lineSpacing) {
            line.draw(&painter, QPoint(0, y));
            y = nextLineY;
            //! [2]
            //! [3]
        } else {
            QString lastLine = content.mid(line.textStart());
            QString elidedLastLine = fontMetrics.elidedText(lastLine, Qt::ElideRight, width());
            painter.drawText(QPoint(0, y + fontMetrics.ascent()), elidedLastLine);
            line = textLayout.createLine();
            didElide = line.isValid();
            break;
        }
    }
    textLayout.endLayout();
    //! [3]

    //! [4]
    if (didElide != elided) {
        elided = didElide;
        emit elisionChanged(didElide);
    }
}
Exemplo n.º 25
0
void ElidingLabel::paintEvent(QPaintEvent *)
{
    const int m = margin();
    QRect contents = contentsRect().adjusted(m, m, -m, -m);
    QFontMetrics fm = fontMetrics();
    QString txt = text();
    if (txt.length() > 4 && fm.width(txt) > contents.width()) {
        setToolTip(txt);
        txt = fm.elidedText(txt, m_elideMode, contents.width());
    } else {
        setToolTip(QString());
    }
    int flags = QStyle::visualAlignment(layoutDirection(), alignment()) | Qt::TextSingleLine;

    QPainter painter(this);
    drawFrame(&painter);
    painter.drawText(contents, flags, txt);
}
Exemplo n.º 26
0
void ElidedLabel::paintEvent(QPaintEvent *event)
{
	QLabel::paintEvent(event);
	
	QPainter painter(this);
	QFontMetrics fontMetrics = painter.fontMetrics();
	QRect cr = contentsRect();
	cr.adjust(margin(), margin(), -margin(), -margin());
	
	bool didElide = false;
	int lineSpacing = fontMetrics.lineSpacing();
	int x, y = x =cr.top()+(cr.height()-lineSpacing)/2;
	
	QTextLayout textLayout(mContent, painter.font());
	textLayout.beginLayout();
	forever {
		QTextLine line = textLayout.createLine();
		
		if (!line.isValid())
			break;
		
		line.setLineWidth(cr.width()+2*x);
		int nextLineY = y + lineSpacing;
		
		if (cr.height() >= nextLineY + lineSpacing) {
			line.draw(&painter, QPoint(x, y));
			y = nextLineY;
		} else {
			QString lastLine = mContent.mid(line.textStart());
			QString elidedLastLine = fontMetrics.elidedText(lastLine, Qt::ElideRight, cr.width());
			painter.drawText(QPoint(x, y + fontMetrics.ascent()), elidedLastLine);
			line = textLayout.createLine();
			didElide = line.isValid();
			break;
		}
	}
	textLayout.endLayout();
	
	if (didElide != mElided) {
		mElided = didElide;
		emit elisionChanged(didElide);
	}
}
Exemplo n.º 27
0
/*!
 * Показ меню истории отправленных сообщений.
 *
 * \todo Добавить кнопку очистки истории.
 */
void SendButton::showMenu()
{
  if (!m_history->isEmpty())
    return;

  const QStringList history = SendWidget::i()->input()->history();
  if (!history.isEmpty()) {
    const QFontMetrics fm = fontMetrics();
    QAction *action = 0;

    for (int i = history.size() - 1; i >= 0; --i) {
      action = m_history->addAction(fm.elidedText(PlainTextFilter::filter(history.at(i)), Qt::ElideMiddle, 150));
      action->setData(i);
      connect(action, SIGNAL(triggered()), SLOT(showItem()));
    }
  }

  if (m_history->isEmpty())
    m_history->addAction(tr("Empty"))->setEnabled(false);
}
Exemplo n.º 28
0
void FilesDialog::setSelectedFolder(const QString &path)
{
	//ROS_INFO("Selected folder: %s", path.toStdString().c_str());
	_path = path;
	_file = QString();
	if (path.isEmpty()) {
		/* If path is empty, it means that the folder tree was restarted...
		 * So, all the buttons should be disabled
		 * (this should be like an reseted state)
		 */
		_ui.createFolderButton->setEnabled(false);
		_ui.deleteFileButton->setEnabled(false);
		_ui.deleteFolderButton->setEnabled(false);

		_ui.currentFileLabel->setText("No file selected.");
		_ui.currentFolderLabel->setText("No folder selected.");
	} else {
		/* But, when the folder is selected, theorically, the file is still not selected...
		 * So, we cannot allow yet the user to delete a file
		 */
		_file = QString();
		_ui.deleteFolderButton->setEnabled(true);
		_ui.createFolderButton->setEnabled(true);
		_ui.deleteFileButton->setEnabled(false);

		_ui.currentFileLabel->setText("No file selected.");

		//And now... for the elipsis
		QFontMetrics metrics = _ui.currentFolderLabel->fontMetrics();
		if (metrics.width(_path) > _ui.currentFolderLabel->width()) {
			_ui.currentFolderLabel->setText(metrics.elidedText(_path, Qt::ElideLeft, _ui.currentFolderLabel->width()));
		} else {
			_ui.currentFolderLabel->setText(_path);
		}
	}
}
Exemplo n.º 29
0
void QStatusButton::paintEvent(QPaintEvent *e)
{
    QToolButton::paintEvent(e);
    const QFontMetrics fm=fontMetrics();
    const int baseLine=(height()-fm.height()+1)/2+fm.ascent();
    const int numberWidth=fm.width(m_number);

    QPainter p(this);
    if(m_flashTimer->state()==QTimeLine::Running)
    {
        p.setPen(Qt::transparent);
        p.fillRect(rect().adjusted(19,1,-1,-1),QBrush(QColor(255,0,0,m_flashTimer->currentFrame())));
    }
    p.setFont(font());
    p.setPen(Qt::white);
    p.drawText((20-numberWidth)/2,baseLine,m_number);
    if(!isChecked())
    {
        p.setPen(Qt::black);
    }
    int leftPart=22;
    int labelWidth=m_label->isVisible()?m_label->width()+3:0;
    p.drawText(leftPart,baseLine,fm.elidedText(m_text,Qt::ElideRight,width()-leftPart-1-labelWidth));
}
void OrthogonalRenderer::drawMapObject(QPainter *painter,
                                       const MapObject *object,
                                       const QColor &color) const
{
    painter->save();

    const QRectF &bounds = object->bounds();
    QRectF rect(tileToPixelCoords(bounds.topLeft()),
                tileToPixelCoords(bounds.bottomRight()));

    painter->translate(rect.topLeft());
    rect.moveTopLeft(QPointF(0, 0));

    if (!object->cell().isEmpty()) {
        const Cell &cell = object->cell();

        CellRenderer(painter).render(cell, QPointF(),
                                     CellRenderer::BottomLeft);

        if (testFlag(ShowTileObjectOutlines)) {
            const QRect rect = cell.tile->image().rect();
            QPen pen(Qt::SolidLine);
            pen.setWidth(0);
            painter->setPen(pen);
            painter->drawRect(rect);
            pen.setStyle(Qt::DotLine);
            pen.setColor(color);
            painter->setPen(pen);
            painter->drawRect(rect);
        }
    } else {
        const QPen linePen(color, 2);
        const QPen shadowPen(Qt::black, 2);

        QColor brushColor = color;
        brushColor.setAlpha(50);
        const QBrush fillBrush(brushColor);

        painter->setRenderHint(QPainter::Antialiasing);

        switch (object->shape()) {
        case MapObject::Rectangle: {
            if (rect.isNull())
                rect = QRectF(QPointF(-10, -10), QSizeF(20, 20));

            const QFontMetrics fm = painter->fontMetrics();
            QString name = fm.elidedText(object->name(), Qt::ElideRight,
                                         rect.width() + 2);

            // Draw the shadow
            painter->setPen(shadowPen);
            painter->drawRect(rect.translated(QPointF(1, 1)));
            if (!name.isEmpty())
                painter->drawText(QPoint(1, -5 + 1), name);

            painter->setPen(linePen);
            painter->setBrush(fillBrush);
            painter->drawRect(rect);
            if (!name.isEmpty())
                painter->drawText(QPoint(0, -5), name);

            break;
        }

        case MapObject::Polyline: {
            QPolygonF screenPolygon = tileToPixelCoords(object->polygon());

            painter->setPen(shadowPen);
            painter->drawPolyline(screenPolygon.translated(1, 1));

            painter->setPen(linePen);
            painter->setBrush(fillBrush);
            painter->drawPolyline(screenPolygon);
            break;
        }

        case MapObject::Polygon: {
            QPolygonF screenPolygon = tileToPixelCoords(object->polygon());

            painter->setPen(shadowPen);
            painter->drawPolygon(screenPolygon.translated(1, 1));

            painter->setPen(linePen);
            painter->setBrush(fillBrush);
            painter->drawPolygon(screenPolygon);
            break;
        }

        case MapObject::Ellipse: {
            if (rect.isNull())
                rect = QRectF(QPointF(-10, -10), QSizeF(20, 20));

            const QFontMetrics fm = painter->fontMetrics();
            QString name = fm.elidedText(object->name(), Qt::ElideRight,
                                         rect.width() + 2);

            // Draw the shadow
            painter->setPen(shadowPen);
            painter->drawEllipse(rect.translated(QPointF(1, 1)));
            if (!name.isEmpty())
                painter->drawText(QPoint(1, -5 + 1), name);

            painter->setPen(linePen);
            painter->setBrush(fillBrush);
            painter->drawEllipse(rect);
            if (!name.isEmpty())
                painter->drawText(QPoint(0, -5), name);

            break;
        }
        }
    }

    painter->restore();
}