Example #1
0
void DiagramTextItem::serialize(QDataStream& out) const
{
	out << textWidth();
	out << font();
	out << defaultTextColor();
	out << toPlainText();
	QPointF point = pos();
	out << point;
	out << *this;
}
Example #2
0
/**
 * This is a utility method to copy all the attributes of this
 * TextItem to \a other TextItem.
 */
void TextItem::copyAttributesTo(TextItem *other) const
{
    other->setDefaultTextColor(defaultTextColor());
    other->setHoverBrush(hoverBrush());
    other->setAcceptHoverEvents(acceptHoverEvents());
    other->setAlignment(alignment());
    other->setFont(font());
    other->setBold(bold());
    other->setItalic(italic());
    other->setUnderline(underline());
    other->setBackgroundBrush(backgroundBrush());
}
Example #3
0
void AATextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    if(hasFocus()){
        QGraphicsTextItem::paint(painter, option, widget);
        return;
    }

    painter->setRenderHint(QPainter::Antialiasing);

    QPainterPath path;
    QFontMetrics fm(font());
    path.addText(document()->documentMargin(), fm.height(), font(), toPlainText());
    painter->fillPath(path, defaultTextColor());
}
Example #4
0
void AATextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    if (hasFocus()){
        QGraphicsTextItem::paint(painter, option, widget);
        return;
    }

    painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    QPainterPath path;
    QString s = toPlainText();
    QTextDocument *doc = document();
    qreal margin = doc->documentMargin();
    QStringList lines = s.split("\n", QString::SkipEmptyParts);

    QFont f = font();
    f.setStyleHint(QFont::AnyStyle, QFont::PreferAntialias);
    QFontMetrics fm(f);
    QFont f_bold = f;
    f_bold.setBold(true);
    QFontMetrics fm_bold(f_bold);

    int line = 0;
    for (int i = 0; i < lines.length(); i++){
        QString thisline = lines[i];
        QStringList s1 = thisline.split(']', QString::SkipEmptyParts);
        QStringList s_non_bold, s_bold;
        foreach(QString s, s1){
            if (!s.contains('[')){
                s_non_bold << s;
                s_bold << QString();
            }
            else if (!s.startsWith('[')){
                QStringList s2 = s.split('[', QString::SkipEmptyParts);
                s_non_bold << s2.first();
                s_bold << s2.last();
            }
            else {
                s = s.mid(1);
                s_non_bold << QString();
                s_bold << s;
            }
        }

        int width = 0;
        for (int j = 0; j < s_non_bold.length(); j++){
            QString non_bold = s_non_bold[j];
            for (int k = 0; k < non_bold.length(); k++){
                QChar c = non_bold[k];
                QString str = c;
                int width_c = fm.width(str);
                if (width + width_c > doc->size().width()){
                    ++line;
                    width = 0;
                }
                path.addText(margin + width, fm.height() * (line + 1), f, str);
                width += width_c;
            }
            QString bold = s_bold[j];
            for (int k = 0; k < bold.length(); k++){
                QChar c = bold[k];
                QString str = c;
                int width_c = fm_bold.width(str);
                if (width + width_c > doc->size().width()){
                    ++line;
                    width = 0;
                }
                path.addText(margin + width, fm.height() * (line + 1), f_bold, str); //use fm.height not fm_bold.height, for sometimes the bold characters are not as high as the non-bold ones
                width += width_c;
            }
        }
        ++line;
    }
    painter->fillPath(path, defaultTextColor());
}