예제 #1
0
int TextOption::alignment( lua_State * L ) // const Qt::Alignment 
{
	QTextOption* lhs = ValueInstaller2<QTextOption>::check( L, 1 );
	Qt::Alignment alignment=(Qt::Alignment) lhs->alignment();
	Util::push( L, alignment);
	return 1;
}
예제 #2
0
void QDeclarativeTextEditPrivate::updateDefaultTextOption()
{
    QTextOption opt = document->defaultTextOption();
    int oldAlignment = opt.alignment();
    opt.setAlignment((Qt::Alignment)(int)(hAlign | vAlign));

    QTextOption::WrapMode oldWrapMode = opt.wrapMode();

    if (wrap)
        opt.setWrapMode(QTextOption::WordWrap);
    else
        opt.setWrapMode(QTextOption::NoWrap);

    if (oldWrapMode == opt.wrapMode() && oldAlignment == opt.alignment())
        return;
    document->setDefaultTextOption(opt);
}
예제 #3
0
void ExpandingDelegate::drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &_rect, const QString &text) const
{
    QRect rect(_rect);

    adjustRect(rect);

    QTextLayout layout(text, option.font, painter->device());

    QList<QTextLayout::FormatRange> additionalFormats;

    int missingFormats = text.length();

    for (int i = 0; i < m_cachedHighlights.count(); ++i) {
        if (m_cachedHighlights[i].start + m_cachedHighlights[i].length <= m_currentColumnStart) {
            continue;
        }

        if (additionalFormats.isEmpty())
            if (i != 0 && m_cachedHighlights[i - 1].start + m_cachedHighlights[i - 1].length > m_currentColumnStart) {
                QTextLayout::FormatRange before;
                before.start = 0;
                before.length = m_cachedHighlights[i - 1].start + m_cachedHighlights[i - 1].length - m_currentColumnStart;
                before.format = m_cachedHighlights[i - 1].format;
                additionalFormats.append(before);
            }

        QTextLayout::FormatRange format;
        format.start = m_cachedHighlights[i].start - m_currentColumnStart;
        format.length = m_cachedHighlights[i].length;
        format.format = m_cachedHighlights[i].format;

        additionalFormats.append(format);
    }
    if (!additionalFormats.isEmpty()) {
        missingFormats = text.length() - (additionalFormats.back().length + additionalFormats.back().start);
    }

    if (missingFormats > 0) {
        QTextLayout::FormatRange format;
        format.start = text.length() - missingFormats;
        format.length = missingFormats;
        QTextCharFormat fm;
        fm.setForeground(option.palette.text());
        format.format = fm;
        additionalFormats.append(format);
    }

    if (m_backgroundColor.isValid()) {
        QColor background = m_backgroundColor;
//     qCDebug(LOG_KTE) << text << "background:" << background.name();
        //Now go through the formats, and make sure the contrast background/foreground is readable
        for (int a = 0; a < additionalFormats.size(); ++a) {
            QColor currentBackground = background;
            if (additionalFormats[a].format.hasProperty(QTextFormat::BackgroundBrush)) {
                currentBackground = additionalFormats[a].format.background().color();
            }

            QColor currentColor = additionalFormats[a].format.foreground().color();

            double currentContrast = readabilityContrast(currentColor, currentBackground);
            QColor invertedColor(0xffffffff - additionalFormats[a].format.foreground().color().rgb());
            double invertedContrast = readabilityContrast(invertedColor, currentBackground);

//       qCDebug(LOG_KTE) << "values:" << invertedContrast << currentContrast << invertedColor.name() << currentColor.name();

            if (invertedContrast > currentContrast) {
//         qCDebug(LOG_KTE) << text << additionalFormats[a].length << "switching from" << currentColor.name() << "to" << invertedColor.name();
                QBrush b(additionalFormats[a].format.foreground());
                b.setColor(invertedColor);
                additionalFormats[a].format.setForeground(b);
            }
        }
    }

    for (int a = additionalFormats.size() - 1; a >= 0; --a) {
        if (additionalFormats[a].length == 0) {
            additionalFormats.removeAt(a);
        } else {
            ///For some reason the text-formats seem to be invalid in some way, sometimes
            ///@todo Fix this properly, it sucks not copying everything over
            QTextCharFormat fm;
            fm.setForeground(QBrush(additionalFormats[a].format.foreground().color()));
            fm.setBackground(additionalFormats[a].format.background());
            fm.setUnderlineStyle(additionalFormats[a].format.underlineStyle());
            fm.setUnderlineColor(additionalFormats[a].format.underlineColor());
            fm.setFontWeight(additionalFormats[a].format.fontWeight());
            additionalFormats[a].format = fm;
        }
    }

//   qCDebug(LOG_KTE) << "Highlights for text [" << text << "] col start " << m_currentColumnStart << ":";
//   foreach (const QTextLayout::FormatRange& fr, additionalFormats)
//     qCDebug(LOG_KTE) << fr.start << " len " << fr.length << "foreground" << fr.format.foreground() << "background" << fr.format.background();

    layout.setAdditionalFormats(additionalFormats);

    QTextOption to;

    to.setAlignment( static_cast<Qt::Alignment>(m_cachedAlignment | option.displayAlignment) );

    to.setWrapMode(QTextOption::WrapAnywhere);
    layout.setTextOption(to);

    layout.beginLayout();
    QTextLine line = layout.createLine();
    // Leave some extra space when the text is right-aligned
    line.setLineWidth(rect.width() - (option.displayAlignment == Qt::AlignRight ? 8 : 0));
    layout.endLayout();

    //We need to do some hand layouting here
    if (to.alignment() & Qt::AlignBottom) {
        layout.draw(painter, QPoint(rect.left(), rect.bottom() - (int)line.height()));
    } else {
        layout.draw(painter, rect.topLeft());
    }

    return;

    //if (painter->fontMetrics().width(text) > textRect.width() && !text.contains(QLatin1Char('\n')))
    //str = elidedText(option.fontMetrics, textRect.width(), option.textElideMode, text);
    //qt_format_text(option.font, textRect, option.displayAlignment, str, 0, 0, 0, 0, painter);
}