void CreateBlogMsg::toggleCode()
{
    static QString preFontFamily;

    QTextCharFormat charFormat = ui.msgEdit->currentCharFormat();
    QTextCharFormat f;
    
    if ( charFormat.hasProperty( TextFormat::HasCodeStyle ) &&
         charFormat.boolProperty( TextFormat::HasCodeStyle ) ) {
        f.setProperty( TextFormat::HasCodeStyle, QVariant( false ) );
        f.setBackground( defaultCharFormat.background() );
        f.setFontFamily( preFontFamily );
        ui.msgEdit->textCursor().mergeCharFormat( f );

    } else {
        preFontFamily = ui.msgEdit->fontFamily();
        f.setProperty( TextFormat::HasCodeStyle, QVariant( true ) );
        f.setBackground( codeBackground );
        f.setFontFamily( "Dejavu Sans Mono" );
        ui.msgEdit->textCursor().mergeCharFormat( f );
    }
    ui.msgEdit->setFocus( Qt::OtherFocusReason );
}
Exemple #2
0
QList<HtmlExporter::tag> HtmlExporter::emitCharFormatStyle( const QTextCharFormat 
                         &charFormat, const QTextBlockFormat &blockFormat  )
{
//     kDebug() << "html" << html;

    QList<HtmlExporter::tag> tags;
    bool attributesEmitted = false;
    QLatin1String styleTag( "<span style=\"" );

    const QString family = charFormat.fontFamily();

    //if (!family.isEmpty() && family != defaultCharFormat.fontFamily()) {
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( charFormat.hasProperty( BilboTextFormat::HasCodeStyle ) && 
         charFormat.boolProperty( BilboTextFormat::HasCodeStyle ) ) {
        tags << code;

    } else if ( !family.isEmpty() && family != mDefaultCharFormat.fontFamily() ) {
//         if ( family.right( 7 ) == "courier" ) {
//             tags << code;
//         } else {
            if ( ! attributesEmitted ) {
                html += styleTag;
            }
            html += QLatin1String( " font-family:'" );
            html += family;
            html += QLatin1String( "';" );
            attributesEmitted = true;
//         }
    }


//     if (format.hasProperty(QTextFormat::FontPointSize)
//             && format.fontPointSize() != defaultCharFormat.fontPointSize()) {
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( charFormat.hasProperty( QTextFormat::FontPointSize )
         && charFormat.fontPointSize() != mDefaultCharFormat.fontPointSize() ) {
        if ( ! attributesEmitted ) {
            html += styleTag;
        }
        html += QLatin1String( " font-size:" );
        html += QString::number( charFormat.fontPointSize() );
        html += QLatin1String( "pt;" );
        attributesEmitted = true;
    } else if ( charFormat.hasProperty( QTextFormat::FontSizeAdjustment ) && 
               !( blockFormat.hasProperty( BilboTextFormat::HtmlHeading ) &&
                  blockFormat.intProperty( BilboTextFormat::HtmlHeading ) ) ) {

        ///To use <h1-5> tags for font size
//         const int idx = format.intProperty(QTextFormat::FontSizeAdjustment) + 1;
//
//         switch (idx) {
//         case 0: tags << h5; break;
//             //case 1: tags << h4; break; //NOTE h4 will be the normal text!
//         case 2: tags << h3; break;
//         case 3: tags << h2; break;
//         case 4: tags << h1; break;
//         }

        ///To use <span> tags for font size
        static const char * const sizeNames[] = {
            "small", "medium", "large", "x-large", "xx-large"
        };
        const char *name = 0;
        const int idx = charFormat.intProperty( QTextFormat::FontSizeAdjustment ) + 1;
        if ( idx == 1 ) {
            // assume default to not bloat the html too much
        } else if ( idx >= 0 && idx <= 4 ) {
            name = sizeNames[idx];
        }
        if ( name ) {
            if ( ! attributesEmitted ) {
                html += styleTag;
            }
            html += QLatin1String( " font-size:" );
            html += QLatin1String( name );
            html += QLatin1Char( ';' );
            attributesEmitted = true;
        }
    }


//    if (format.fontWeight() > defaultCharFormat.fontWeight()) {
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( charFormat.fontWeight() > mDefaultCharFormat.fontWeight() && 
        !( blockFormat.hasProperty( BilboTextFormat::HtmlHeading ) &&
           blockFormat.intProperty( BilboTextFormat::HtmlHeading ) ) ) {
        tags << strong;
        /*if (! attributesEmitted ) html += styleTag;
        html += QLatin1String(" font-weight:");
        html += QString::number(format.fontWeight() * 8);
        html += QLatin1Char(';');
        attributesEmitted = true;*/
    }

//    if (format.fontItalic() != defaultCharFormat.fontItalic()) {
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( charFormat.fontItalic() != mDefaultCharFormat.fontItalic() ) {
        tags << em;
        /*
        if (! attributesEmitted ) html += styleTag;
        html += QLatin1String(" font-style:");
        html += (format.fontItalic() ? QLatin1String("italic") : QLatin1String("normal"));
        html += QLatin1Char(';');
        attributesEmitted = true;*/
    }

//    if (format.fontUnderline() != defaultCharFormat.fontUnderline()) {
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( charFormat.fontUnderline() != mDefaultCharFormat.fontUnderline() ) {
        tags << u;
    }


//    if (format.fontStrikeOut() != defaultCharFormat.fontStrikeOut()) {
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( charFormat.fontStrikeOut() != mDefaultCharFormat.fontStrikeOut() ) {
        tags << s;
    }

//    if (format.fontOverline() != defaultCharFormat.fontOverline()) {
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( charFormat.fontOverline() != mDefaultCharFormat.fontOverline() ) {
        if ( charFormat.fontOverline() ) {
            if ( ! attributesEmitted ) {
                html += styleTag;
            }
            html += QLatin1String( " text-decoration: overline;" );
            attributesEmitted = true;
        }
    }

    /*
    bool hasDecoration = false;
    bool atLeastOneDecorationSet = false;
    QLatin1String decorationTag(" text-decoration:");

    if (format.fontUnderline() != defaultCharFormat.fontUnderline() ||
    format.fontOverline() != defaultCharFormat.fontOverline() ||
    format.fontStrikeOut() != defaultCharFormat.fontStrikeOut() )
    {
      if (! attributesEmitted ) html += styleTag;
      html += decorationTag;
    }

    if (format.fontUnderline() != defaultCharFormat.fontUnderline()) {
        hasDecoration = true;
        if (format.fontUnderline()) {
            html += QLatin1String(" underline");
            atLeastOneDecorationSet = true;
        }
    }

    if (format.fontOverline() != defaultCharFormat.fontOverline()) {
        hasDecoration = true;
        if (format.fontOverline()) {
            html += QLatin1String(" overline");
            atLeastOneDecorationSet = true;
        }
    }

    if (format.fontStrikeOut() != defaultCharFormat.fontStrikeOut()) {
        hasDecoration = true;
        if (format.fontStrikeOut()) {
            html += QLatin1String(" line-through");
            atLeastOneDecorationSet = true;
        }
    }

    if (hasDecoration) {
        if (!atLeastOneDecorationSet)
            html += QLatin1String("none");
        html += QLatin1Char(';');
        attributesEmitted = true;
    }*/
    /* else {
        html.chop(qstrlen(decorationTag.latin1()));
    }*/

//     QBrush linkColor = KColorScheme(QPalette::Active, KColorScheme::Window).foreground(KColorScheme::LinkText);

//    if ( format.foreground() != defaultCharFormat.foreground() &&
//            format.foreground().style() != Qt::NoBrush) {
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( charFormat.foreground() != mDefaultCharFormat.foreground() &&
            charFormat.foreground().style() != Qt::NoBrush && !charFormat.isAnchor() ) {
        //         if ( format.foreground() != linkColor ) {
//    if ( format.anchorHref().isNull() ) {
        if ( ! attributesEmitted ) {
            html += styleTag;
            attributesEmitted = true;
        }
//    } else {
//     html += QLatin1String(" style=\"");
//    }
        html += QLatin1String( " color:" );
        html += charFormat.foreground().color().name();
        html += QLatin1Char( ';' );
//    if ( !format.anchorHref().isNull() ) {
//     html += QLatin1String("\"");
//    }
//    attributesEmitted = true;
        //         }
    }

//    if (format.background() != defaultCharFormat.background()
//            && format.background().style() != Qt::NoBrush) {
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( !( charFormat.hasProperty( BilboTextFormat::HasCodeStyle ) && 
         charFormat.boolProperty( BilboTextFormat::HasCodeStyle ) ) ) {
        if ( charFormat.background() != mDefaultCharFormat.background()
                && charFormat.background().style() != Qt::NoBrush ) {
            if ( ! attributesEmitted ) {
                html += styleTag;
            }
            html += QLatin1String( " background-color:" );
            html += charFormat.background().color().name();
            html += QLatin1Char( ';' );
            attributesEmitted = true;
        }
    }
//    if (format.verticalAlignment() != defaultCharFormat.verticalAlignment()) { //TODO
    // NOTE the above line replaced with the bottom line to use default charFormat, which can be set from outside.
    if ( charFormat.verticalAlignment() != mDefaultCharFormat.verticalAlignment() ) { //TODO
        if ( ! attributesEmitted ) {
            html += styleTag;
        }
        html += QLatin1String( " vertical-align:" );

        QTextCharFormat::VerticalAlignment valign = charFormat.verticalAlignment();
        if ( valign == QTextCharFormat::AlignSubScript ) {
            html += QLatin1String( "sub" );
        } else if ( valign == QTextCharFormat::AlignSuperScript ) {
            html += QLatin1String( "super" );
        }

        html += QLatin1Char( ';' );
        attributesEmitted = true;
    }

    //Append close span Tag
    if ( attributesEmitted ) {
        html += QLatin1String( "\">" );
        tags.prepend( span );
    }

//     kDebug() << "html=>" << html << tags;
    return tags;
}