void GraphicTextDialog::textStyle(int styleIndex) { QTextCursor cursor = textEdit->textCursor(); if(styleIndex != 0) { QTextListFormat::Style style = QTextListFormat::ListDisc; switch (styleIndex) { default: case 1: style = QTextListFormat::ListDisc; break; case 2: style = QTextListFormat::ListCircle; break; case 3: style = QTextListFormat::ListSquare; break; case 4: style = QTextListFormat::ListDecimal; break; case 5: style = QTextListFormat::ListLowerAlpha; break; case 6: style = QTextListFormat::ListUpperAlpha; break; } cursor.beginEditBlock(); QTextBlockFormat blockFmt = cursor.blockFormat(); QTextListFormat listFmt; if(cursor.currentList()) { listFmt = cursor.currentList()->format(); } else { listFmt.setIndent(blockFmt.indent() + 1); blockFmt.setIndent(0); cursor.setBlockFormat(blockFmt); } listFmt.setStyle(style); cursor.createList(listFmt); cursor.endEditBlock(); } else { // #### QTextBlockFormat bfmt; bfmt.setObjectIndex(-1); cursor.mergeBlockFormat(bfmt); } }
void MenuMainScreen::setCurrentOption(QGraphicsSimpleTextItem *option) { QBrush whiteBrush(Qt::white); QBrush yellowBrush(Qt::yellow); if (!!_currentOption) _currentOption->setBrush(whiteBrush); _currentOption = option; _currentOption->setBrush(yellowBrush); if (option->text() == QString("Character")) _textField->setPlainText("View more information about the selected character."); else if (option->text() == QString("Equipment")) _textField->setPlainText("View or change the equipment the selected character is wearing."); else if (option->text() == QString("Abilities")) _textField->setPlainText("View the abilities of the selected character."); else if (option->text() == QString("Inventory")) _textField->setPlainText("View the items collected by the party."); else if (option->text() == QString("Exit")) _textField->setPlainText("Exit the menu."); QFont font("Times", 12, QFont::Bold); _textField->setFont(font); QTextBlockFormat format; format.setAlignment(Qt::AlignLeft); QTextCursor cursor = _textField->textCursor(); cursor.select(QTextCursor::Document); cursor.mergeBlockFormat(format); cursor.clearSelection(); _textField->setTextCursor(cursor); }
void TextRoom::alignCenter() { QTextBlockFormat fmt; QTextCursor tc = textEdit->textCursor(); fmt.setAlignment(Qt::AlignHCenter); tc.mergeBlockFormat(fmt); }
//段落标号、编号 void MyChild::setStyle(int style) { QTextCursor cursor = this->textCursor(); if (style != 0) { QTextListFormat::Style stylename = QTextListFormat::ListDisc; switch (style) { default: case 1: stylename = QTextListFormat::ListDisc; break; case 2: stylename = QTextListFormat::ListCircle; break; case 3: stylename = QTextListFormat::ListSquare; break; case 4: stylename = QTextListFormat::ListDecimal; break; case 5: stylename = QTextListFormat::ListLowerAlpha; break; case 6: stylename = QTextListFormat::ListUpperAlpha; break; case 7: stylename = QTextListFormat::ListLowerRoman; break; case 8: stylename = QTextListFormat::ListUpperRoman; break; } cursor.beginEditBlock(); QTextBlockFormat blockFmt = cursor.blockFormat(); QTextListFormat listFmt; if (cursor.currentList()) { listFmt = cursor.currentList()->format(); } else { listFmt.setIndent(blockFmt.indent() + 1); blockFmt.setIndent(0); cursor.setBlockFormat(blockFmt); } listFmt.setStyle(stylename); cursor.createList(listFmt); cursor.endEditBlock(); } else { QTextBlockFormat bfmt; bfmt.setObjectIndex(-1); cursor.mergeBlockFormat(bfmt); } }
void DocumentHandler::setAlignment(Qt::Alignment a) { QTextBlockFormat fmt; fmt.setAlignment((Qt::Alignment) a); QTextCursor cursor = QTextCursor(m_doc); cursor.setPosition(m_selectionStart, QTextCursor::MoveAnchor); cursor.setPosition(m_selectionEnd, QTextCursor::KeepAnchor); cursor.mergeBlockFormat(fmt); emit alignmentChanged(); }
void CreateBlogMsg::changeFormatType(int styleIndex ) { ui.msgEdit->setFocus( Qt::OtherFocusReason ); QTextCursor cursor = ui.msgEdit->textCursor(); //QTextBlockFormat bformat = cursor.blockFormat(); QTextBlockFormat bformat; QTextCharFormat cformat; switch (styleIndex) { default: case 0: bformat.setProperty( TextFormat::HtmlHeading, QVariant( 0 ) ); cformat.setFontWeight( QFont::Normal ); cformat.setProperty( QTextFormat::FontSizeAdjustment, QVariant( 0 ) ); break; case 1: bformat.setProperty( TextFormat::HtmlHeading, QVariant( 1 ) ); cformat.setFontWeight( QFont::Bold ); cformat.setProperty( QTextFormat::FontSizeAdjustment, QVariant( 3 ) ); break; case 2: bformat.setProperty( TextFormat::HtmlHeading, QVariant( 2 ) ); cformat.setFontWeight( QFont::Bold ); cformat.setProperty( QTextFormat::FontSizeAdjustment, QVariant( 2 ) ); break; case 3: bformat.setProperty( TextFormat::HtmlHeading, QVariant( 3 ) ); cformat.setFontWeight( QFont::Bold ); cformat.setProperty( QTextFormat::FontSizeAdjustment, QVariant( 1 ) ); break; case 4: bformat.setProperty( TextFormat::HtmlHeading, QVariant( 4 ) ); cformat.setFontWeight( QFont::Bold ); cformat.setProperty( QTextFormat::FontSizeAdjustment, QVariant( 0 ) ); break; case 5: bformat.setProperty( TextFormat::HtmlHeading, QVariant( 5 ) ); cformat.setFontWeight( QFont::Bold ); cformat.setProperty( QTextFormat::FontSizeAdjustment, QVariant( -1 ) ); break; case 6: bformat.setProperty( TextFormat::HtmlHeading, QVariant( 6 ) ); cformat.setFontWeight( QFont::Bold ); cformat.setProperty( QTextFormat::FontSizeAdjustment, QVariant( -2 ) ); break; } //cformat.clearProperty( TextFormat::HasCodeStyle ); cursor.beginEditBlock(); cursor.mergeBlockFormat( bformat ); cursor.select( QTextCursor::BlockUnderCursor ); cursor.mergeCharFormat( cformat ); cursor.endEditBlock(); }
QTextDocument *Exporter::prepareTextDoc(QTextDocument *textDoc) { QTextDocument *textDocument = textDoc->clone(this); // textDocument->setDefaultStyleSheet("p, li { white-space: pre-wrap; } p{line-height: 2em; font-family:'Liberation Serif'; font-size:19pt;margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:72px;}"); //cut blank spaces at the begining and end : QTextCursor *tCursor = new QTextCursor(textDocument); tCursor->movePosition(QTextCursor::Start, QTextCursor::MoveAnchor,1); tCursor->movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor,1); while(tCursor->selectedText() == " "){ tCursor->deleteChar(); tCursor->movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor,1); } tCursor->movePosition(QTextCursor::End, QTextCursor::MoveAnchor,1); tCursor->movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor,1); while(tCursor->selectedText() == " "){ tCursor->deleteChar(); tCursor->movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor,1); } //set text config : QTextBlockFormat blockFormat; // blockFormat.setBottomMargin(0); // blockFormat.setTopMargin(0); // blockFormat.setTextIndent(72); blockFormat.setLineHeight(200, QTextBlockFormat::ProportionalHeight); blockFormat.setAlignment(Qt::AlignJustify); // QTextCharFormat charFormat; // charFormat.setFontPointSize(12); // charFormat.setFontFamily("Courrier"); tCursor->select(QTextCursor::Document); tCursor->mergeBlockFormat(blockFormat); // tCursor->mergeBlockCharFormat(charFormat); QRegExp reg("-qt-paragraph-type:.*;|margin-top:.*;|margin-bottom:.*;|margin-left:.*;|margin-right:.*;|-qt-block-indent:.*;|text-indent:.*;|font-family:.*;|font-size:.*;"); reg.setMinimal(true); textDocument->setHtml(textDocument->toHtml().remove(reg)); return textDocument; }
void center(QGraphicsTextItem *item) { QTextBlockFormat format; format.setAlignment(Qt::AlignCenter); QTextCursor cursor = item->textCursor(); cursor.select(QTextCursor::Document); cursor.mergeBlockFormat(format); cursor.clearSelection(); item->setTextCursor(cursor); }
void Stack::setTextDirectionRTL() { if (m_current_document) { m_current_document->setRichText(true); QTextCursor cursor = m_current_document->text()->textCursor(); QTextBlockFormat format = cursor.blockFormat(); format.setLayoutDirection(Qt::RightToLeft); format.setAlignment(Qt::AlignRight | Qt::AlignAbsolute); cursor.mergeBlockFormat(format); emit updateFormatAlignmentActions(); } }
void MyTextItem::setAlignment(Qt::Alignment alignment) { m_alignment = alignment; QTextBlockFormat format; format.setAlignment(alignment); QTextCursor cursor = textCursor(); // save cursor position int position = textCursor().position(); cursor.select(QTextCursor::Document); cursor.mergeBlockFormat(format); cursor.clearSelection(); cursor.setPosition(position); // restore cursor position setTextCursor(cursor); }
/*! * \brief Changes alignment of the text to the one specified by action \a a */ void QwwRichTextEdit::changeAlignment(QAction *a){ QTextCursor cur = textCursor(); QTextBlockFormat fmt; if(a==al) fmt.setAlignment(Qt::AlignLeft); else if(a==ar) fmt.setAlignment(Qt::AlignRight); else if(a==ac) fmt.setAlignment(Qt::AlignCenter); else if(a==aj) fmt.setAlignment(Qt::AlignJustify); cur.mergeBlockFormat(fmt); setTextCursor(cur); }
void TextZone::changeTextStyleSlot(int styleIndex) { QTextBlockFormat blockFormat; blockFormat.setBottomMargin(textStyles->blockBottomMarginAt(styleIndex)); blockFormat.setTextIndent(textStyles->blockFirstLineIndentAt(styleIndex)); blockFormat.setLeftMargin(textStyles->blockLeftMarginAt(styleIndex)); blockFormat.setAlignment(textStyles->blockAlignmentTrueNameAt(styleIndex)); blockFormat.setTopMargin(0); blockFormat.setRightMargin(0); QTextCharFormat charFormat; charFormat.setFontPointSize(textStyles->fontSizeAt(styleIndex)); charFormat.setFontFamily(textStyles->fontFamilyAt(styleIndex)); // charFormat.setFontItalic(textStyles->fontItalicAt(styleIndex)); // if (textStyles->fontBoldAt(styleIndex) == true) // charFormat.setFontWeight(75); // else // charFormat.setFontWeight(50); // charFormat.setFontUnderline(textStyles->fontUnderlineAt(styleIndex)); // charFormat.setFontStrikeOut(textStyles->fontStrikeOutAt(styleIndex)); // charFormat.clearForeground(); QTextCursor tCursor = this->textCursor(); // select all of the blocks selected : QTextCursor tStartCursor = this->textCursor(); tStartCursor.setPosition(tCursor.selectionStart()); tStartCursor.movePosition(QTextCursor::StartOfBlock); int startFirstBlock = tStartCursor.position(); QTextCursor tEndCursor = this->textCursor(); tEndCursor.setPosition(tCursor.selectionEnd()); tEndCursor.movePosition(QTextCursor::EndOfBlock); int endLastBlock = tEndCursor.position(); tCursor.setPosition(startFirstBlock); tCursor.setPosition(endLastBlock, QTextCursor::KeepAnchor); // merge : tCursor.mergeBlockFormat(blockFormat); tCursor.mergeCharFormat(charFormat); this->mergeCurrentCharFormat(charFormat); }
QTextDocument *Exporter::prepareNoteDoc(QTextDocument *noteDoc) { QTextDocument *textDocument = noteDoc->clone(this); //cut blank spaces at the begining and end : QTextCursor *tCursor = new QTextCursor(textDocument); tCursor->movePosition(QTextCursor::Start, QTextCursor::MoveAnchor,1); tCursor->movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor,1); while(tCursor->selectedText() == " "){ tCursor->deleteChar(); tCursor->movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor,1); } tCursor->movePosition(QTextCursor::End, QTextCursor::MoveAnchor,1); tCursor->movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor,1); while(tCursor->selectedText() == " "){ tCursor->deleteChar(); tCursor->movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor,1); } //set text config : QTextBlockFormat blockFormat; blockFormat.setBottomMargin(0); blockFormat.setTopMargin(0); blockFormat.setTextIndent(72); blockFormat.setLineHeight(200, QTextBlockFormat::ProportionalHeight); blockFormat.setAlignment(Qt::AlignJustify); QTextCharFormat charFormat; charFormat.setFontPointSize(12); charFormat.setFontFamily("Courrier"); tCursor->select(QTextCursor::Document); tCursor->mergeBlockCharFormat(charFormat); tCursor->mergeBlockFormat(blockFormat); return textDocument; }
void BattleCharacterStatus::valueChanged(QString name) { BattleCharacterItem *character = 0; QGraphicsTextItem *infoItem = 0; for (int i = 0; i < _characters.size(); i++) { BattleCharacterItem *it = _characters.at(i); if (!!it && (it->getName() == name)) { character = it; infoItem = _info.at(i); break; } } if (!!character) { QString infoString; infoString += character->getCharacter()->getName() + QString(":\n"); infoString += QString("Health: ") + QString::number(character->getHealth().second) + QString(" / ") + QString::number(character->getMaxHealth().second) + QString("\t"); infoString += QString("Energy: ") + QString::number(character->getEnergy().second) + QString(" / ") + QString::number(character->getMaxEnergy().second); double percentage = (double)character->getHealth().second / (double)character->getMaxHealth().second * 100; if (percentage > 60) infoItem->setDefaultTextColor(Qt::green); else if ((percentage <= 60) && (percentage > 25)) infoItem->setDefaultTextColor(Qt::yellow); else if ((percentage <= 25) && (percentage > 0)) infoItem->setDefaultTextColor(Qt::red); else infoItem->setDefaultTextColor(Qt::gray); infoItem->setPlainText(infoString); QFont font("Times", 12, QFont::Bold); infoItem->setFont(font); QTextBlockFormat format; format.setAlignment(Qt::AlignLeft); QTextCursor cursor = infoItem->textCursor(); cursor.select(QTextCursor::Document); cursor.mergeBlockFormat(format); cursor.clearSelection(); infoItem->setTextCursor(cursor); } }
void BattleCharacterStatus::setCharacters(const QVector<BattleCharacterItem*> &characters) { for (int i = 0; i < _info.size(); i++) { delete _info.at(i); } _info.clear(); _characters = characters; int y = 10; for (int i = 0; i < _characters.size(); i++) { BattleCharacterItem *character = _characters.at(i); connect (character, SIGNAL(valueChanged(QString)), this, SLOT(valueChanged(QString))); QString infoString; infoString += character->getCharacter()->getName() + QString(":\n"); infoString += QString("Health: ") + QString::number(character->getHealth().second) + QString(" / ") + QString::number(character->getMaxHealth().second) + QString("\t"); infoString += QString("Energy: ") + QString::number(character->getEnergy().second) + QString(" / ") + QString::number(character->getMaxEnergy().second); QGraphicsTextItem *infoItem = new QGraphicsTextItem(this); _info.append(infoItem); infoItem->setTextWidth(boundingRect().width()); infoItem->setDefaultTextColor(Qt::green); infoItem->setPlainText(infoString); QFont font("Times", 12, QFont::Bold); infoItem->setFont(font); QTextBlockFormat format; format.setAlignment(Qt::AlignLeft); QTextCursor cursor = infoItem->textCursor(); cursor.select(QTextCursor::Document); cursor.mergeBlockFormat(format); cursor.clearSelection(); infoItem->setTextCursor(cursor); infoItem->setPos(10, y); y += 50; } }
TextEffect::TextEffect(MainGraphicsView *view, const QString &text, int duration_ms, const QColor &color) : QGraphicsTextItem(text), time_expire(0), view(view) { this->setDefaultTextColor(color); this->time_expire = game_g->getGameTimeTotalMS() + duration_ms; //this->setFont(game_g->getFontStd()); //this->setFont(game_g->getFontSmall()); this->setFont(game_g->getFontScene()); { // centre alignment - see http://www.cesarbs.org/blog/2011/05/30/aligning-text-in-qgraphicstextitem/ this->setTextWidth(this->boundingRect().width()); QTextBlockFormat format; format.setAlignment(Qt::AlignCenter); QTextCursor cursor = this->textCursor(); cursor.select(QTextCursor::Document); cursor.mergeBlockFormat(format); cursor.clearSelection(); this->setTextCursor(cursor); } this->setCacheMode(QGraphicsItem::DeviceCoordinateCache); }
void TextZone::insertFromMimeData (const QMimeData *source ) { QTextCursor cursor = this->textCursor(); int bottMargin; int textIndent; int leftMargin; Qt::Alignment textAlignment; int textHeight; QString fontFamily; if(cursor.atStart() == true || cursor.position() == 1 || cursor.position() == 0){ int defaultIndex = textStyles->defaultStyleIndex(); bottMargin = textStyles->blockBottomMarginAt(defaultIndex); textIndent = textStyles->blockFirstLineIndentAt(defaultIndex); leftMargin = textStyles->blockLeftMarginAt(defaultIndex); textAlignment = textStyles->blockAlignmentTrueNameAt(defaultIndex); textHeight = textStyles->fontSizeAt(defaultIndex); fontFamily = textStyles->fontFamilyAt(defaultIndex); } else{ bottMargin = cursor.blockFormat().bottomMargin(); textIndent = cursor.blockFormat().textIndent(); leftMargin = cursor.blockFormat().leftMargin(); textAlignment = cursor.blockFormat().alignment(); textHeight = cursor.charFormat().fontPointSize(); fontFamily = cursor.charFormat().fontFamily(); } if(source->hasHtml() && !forceCopyWithoutFormatting){ QByteArray richtext; if (source->hasFormat(QLatin1String("text/rtf"))) { richtext = source->data(QLatin1String("text/rtf")); } else if (source->hasHtml()) { richtext = mimeToRtf(source); } QTextEdit *textEdit = new QTextEdit(0); RTF::Reader reader; QBuffer buffer(&richtext); buffer.open(QIODevice::ReadOnly); reader.read(&buffer, textEdit->textCursor()); buffer.close(); QString sourceString = textEdit->toHtml(); sourceString.remove(QChar::ReplacementCharacter); sourceString.remove(QChar::ObjectReplacementCharacter); sourceString.remove(QChar::Null); //htmlText QTextDocument *document = new QTextDocument; document->setHtml(Utils::parseHtmlText(sourceString)); QTextBlockFormat blockFormat; blockFormat.setBottomMargin(bottMargin); blockFormat.setTextIndent(textIndent); blockFormat.setLeftMargin(leftMargin); blockFormat.setAlignment(textAlignment); blockFormat.setRightMargin(0); QTextCharFormat charFormat; charFormat.setFontPointSize(textHeight); charFormat.setFontFamily(fontFamily); charFormat.setBackground(QBrush(Qt::NoBrush)); charFormat.setForeground(QBrush(Qt::NoBrush)); charFormat.setAnchor(false); charFormat.setUnderlineStyle(QTextCharFormat::NoUnderline); charFormat.setFontStrikeOut(false); QTextCursor *tCursor = new QTextCursor(document); tCursor->movePosition(QTextCursor::Start, QTextCursor::MoveAnchor,1); tCursor->movePosition(QTextCursor::End, QTextCursor::KeepAnchor,1); tCursor->mergeCharFormat(charFormat); tCursor->mergeBlockFormat(blockFormat); QTextCursor cursor = this->textCursor(); cursor.insertHtml(document->toHtml("utf-8")); qDebug() << "insertFromMimeData Html"; } else if(source->hasText() || forceCopyWithoutFormatting){ QTextDocument *document = new QTextDocument; document->setPlainText(qvariant_cast<QString>(source->text())); QTextBlockFormat blockFormat; blockFormat.setBottomMargin(bottMargin); blockFormat.setTextIndent(textIndent); blockFormat.setLeftMargin(leftMargin); blockFormat.setAlignment(textAlignment); blockFormat.setRightMargin(0); QTextCharFormat charFormat; charFormat.setFontPointSize(textHeight); charFormat.setFontFamily(fontFamily); charFormat.clearForeground(); charFormat.setBackground(QBrush(Qt::NoBrush)); charFormat.setForeground(QBrush(Qt::NoBrush)); charFormat.setAnchor(false); charFormat.setUnderlineStyle(QTextCharFormat::NoUnderline); charFormat.setFontStrikeOut(false); QTextCursor *tCursor = new QTextCursor(document); tCursor->movePosition(QTextCursor::Start, QTextCursor::MoveAnchor,1); tCursor->movePosition(QTextCursor::End, QTextCursor::KeepAnchor,1); tCursor->mergeCharFormat(charFormat); tCursor->mergeBlockFormat(blockFormat); QTextCursor cursor = this->textCursor(); cursor.insertHtml(document->toHtml("utf-8")); qDebug() << "insertFromMimeData plainText"; } }
QTextDocument * Exporter::buildFinalDoc() { //search for checked items : QDomDocument domDoc = hub->project()->mainTreeDomDoc(); QDomElement root = domDoc.documentElement(); QList<QDomElement> itemList = searchForCheckedItems(root); if(itemList.size() == 0) return new QTextDocument(); // set up the progress bar : QWidget *progressWidget = new QWidget(this, Qt::Dialog | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); QHBoxLayout *progressLayout = new QHBoxLayout(progressWidget); QProgressBar *progressBar = new QProgressBar(progressWidget); int progressValue = 0; progressLayout->addWidget(progressBar); progressWidget->setLayout(progressLayout); progressBar->setMaximum(itemList.size()); progressBar->setValue(progressValue); progressWidget->show(); // QString debug; // qDebug() << "itemList" << debug.setNum(itemList->size()); QTextDocument *textDocument = new QTextDocument(this); QTextEdit *edit = new QTextEdit(this); textDocument->setDefaultStyleSheet("p, li { white-space: pre-wrap; } p{line-height: 2em; font-family:'Liberation Serif'; font-size:12pt;margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:72px;}"); for(int i = 0; i < itemList.size(); ++i){ QDomElement element = itemList.at(i); QTextCursor *tCursor = new QTextCursor(textDocument); QTextBlockFormat blockFormatLeft; blockFormatLeft.setBottomMargin(0); blockFormatLeft.setTopMargin(0); blockFormatLeft.setTextIndent(72); blockFormatLeft.setLineHeight(200, QTextBlockFormat::ProportionalHeight); blockFormatLeft.setAlignment(Qt::AlignJustify); QTextCharFormat charFormatLeft; charFormatLeft.setFontPointSize(12); charFormatLeft.setFontFamily("Courrier"); QTextBlockFormat blockFormatCenter; blockFormatCenter.setAlignment(Qt::AlignCenter); if(element.tagName() != "separator"){ qDebug() << "element name : "+ element.attribute("name"); MainTextDocument *textDoc = hub->project()->findChild<MainTextDocument *>("textDoc_" + element.attribute("number")); MainTextDocument *synDoc = hub->project()->findChild<MainTextDocument *>("synDoc_" + element.attribute("number")); MainTextDocument *noteDoc = hub->project()->findChild<MainTextDocument *>("noteDoc_" + element.attribute("number")); QTextDocumentFragment textFrag(prepareTextDoc(textDoc)); QTextDocumentFragment synFrag(prepareSynDoc(synDoc)); QTextDocumentFragment noteFrag(prepareNoteDoc(noteDoc)); edit->setDocument(textDocument); if(element.tagName() == "book"){ textDocument->setMetaInformation(QTextDocument::DocumentTitle,element.attribute("name", "")); edit->append("<h1>" + element.attribute("name", "") + "</h1>"); tCursor->movePosition(QTextCursor::End, QTextCursor::MoveAnchor,1); tCursor->mergeBlockFormat(blockFormatCenter); edit->append("<h4>" + QDateTime::currentDateTime().toString() + "</h4>"); tCursor->movePosition(QTextCursor::End, QTextCursor::MoveAnchor,1); tCursor->mergeBlockFormat(blockFormatCenter); edit->append("<br>"); edit->append("<br>"); } if(element.tagName() == "act"){ edit->append("<br>"); edit->append("<br>"); edit->append("<h2>" + element.attribute("name", "") + "</h2>"); tCursor->movePosition(QTextCursor::End, QTextCursor::MoveAnchor,1); tCursor->mergeBlockFormat(blockFormatCenter); edit->append("<br>"); } if(element.tagName() == "chapter"){ edit->append("<br>"); edit->append("<br>"); edit->append("<h2>" + element.attribute("name", "") + "</h2>"); tCursor->movePosition(QTextCursor::End, QTextCursor::MoveAnchor,1); tCursor->mergeBlockFormat(blockFormatCenter); edit->append("<br>"); } if(element.tagName() == "scene" && ui->setSceneTitlesComboBox->currentIndex() != 0){ QString sceneTitle; switch (ui->setSceneTitlesComboBox->currentIndex()){ case 1: sceneTitle = element.attribute("name", ""); break; case 2: sceneTitle = "###"; break; case 3: sceneTitle = "***"; break; default: sceneTitle = element.attribute("name", ""); break; } edit->append("<br>"); edit->append("<h3>" + sceneTitle + "</h3>"); tCursor->movePosition(QTextCursor::End, QTextCursor::MoveAnchor,1); tCursor->mergeBlockFormat(blockFormatCenter); edit->append("<br>"); } if(ui->synopsisCheckBox->isChecked() && !synFrag.isEmpty()){ edit->append("<br>"); edit->append("<h4>" + tr("Synopsis") + "</h4>"); tCursor->movePosition(QTextCursor::End, QTextCursor::MoveAnchor,1); tCursor->mergeBlockFormat(blockFormatCenter); edit->append("<br>"); tCursor->movePosition(QTextCursor::End, QTextCursor::MoveAnchor,1); tCursor->insertBlock(blockFormatLeft, charFormatLeft); tCursor->insertFragment(synFrag); } if(ui->notesCheckBox->isChecked() && !noteFrag.isEmpty()){ edit->append("<br>"); edit->append("<h4>" + tr("Note") + "</h4>"); tCursor->movePosition(QTextCursor::End, QTextCursor::MoveAnchor,1); tCursor->mergeBlockFormat(blockFormatCenter); edit->append("<br>"); tCursor->movePosition(QTextCursor::End, QTextCursor::MoveAnchor,1); tCursor->insertBlock(blockFormatLeft, charFormatLeft); tCursor->insertFragment(noteFrag); } if(ui->storyCheckBox->isChecked()){ if((ui->synopsisCheckBox->isChecked() || ui->notesCheckBox->isChecked()) && !textFrag.isEmpty()){ tCursor->insertBlock(); tCursor->insertHtml("<h4>" + tr("Story") + "</h4>"); tCursor->mergeBlockFormat(blockFormatCenter); tCursor->insertBlock(); } tCursor->insertHtml("<br>"); // tCursor->movePosition(QTextCursor::End, QTextCursor::MoveAnchor,1); tCursor->insertBlock(blockFormatLeft, charFormatLeft); tCursor->insertFragment(textFrag); // edit->append(textFrag->toHtml()); } } else if(element.tagName() == "separator"){ edit->append("<br>"); edit->append("<h3>#</h3>"); tCursor->movePosition(QTextCursor::End, QTextCursor::MoveAnchor,1); tCursor->mergeBlockFormat(blockFormatCenter); edit->append("<br>"); tCursor->movePosition(QTextCursor::End, QTextCursor::MoveAnchor,1); tCursor->mergeBlockFormat(blockFormatLeft); } progressValue += 1; progressBar->setValue(progressValue); } QRegExp reg("-qt-paragraph-type:.*;|margin-top:.*;|margin-bottom:.*;|margin-left:.*;|margin-right:.*;|-qt-block-indent:.*;|text-indent:.*;|font-family:.*;|font-size:.*;"); reg.setMinimal(true); textDocument->setHtml(textDocument->toHtml().remove(reg)); //find and change final page css style : //textDocument->setDefaultStyleSheet("p, li { white-space: pre-wrap; } p{line-height: 2em; font-family:'Liberation Serif'; font-size:14pt;margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:72px;}"); // <style type="text/css"> // p, li { white-space: pre-wrap; } // </style> // tCursor // qDebug() << textDocument->toHtml(); progressWidget->close(); return textDocument; }
void KWQTableView::createPages(QPrinter *printer, QTextDocument *textDoc, bool sendToPrinter) { printer->setFullPage(true); int myDpi = printer->logicalDpiY(); if (Prefs::printStyle() == Prefs::EnumPrintStyle::Flashcard) { printer->setOrientation(QPrinter::Landscape); int cardWidth = qRound(5 * qreal(myDpi)); int cardHeight = qRound(3 * qreal(myDpi)); QTextTable *table = textDoc->rootFrame()->lastCursorPosition().insertTable(model()->rowCount(), 2); QTextTableFormat tableFormat = table->format(); tableFormat.setHeaderRowCount(0); tableFormat.setBorderStyle(QTextFrameFormat::BorderStyle_None); tableFormat.setCellSpacing(0); tableFormat.setCellPadding(0); QVector<QTextLength> constraints; constraints.append(QTextLength(QTextLength::FixedLength, cardWidth)); constraints.append(QTextLength(QTextLength::FixedLength, cardWidth)); tableFormat.setColumnWidthConstraints(constraints); table->setFormat(tableFormat); QTextBlockFormat headerFormat; headerFormat.setAlignment(Qt::AlignLeft); QTextCharFormat headerCharFormat; headerCharFormat.setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont)); QTextBlockFormat cellFormat; cellFormat.setAlignment(Qt::AlignCenter); QTextCharFormat cellCharFormat; cellCharFormat.setFont(Prefs::editorFont()); QTextFrameFormat cardFormat; cardFormat.setBorder(1); cardFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Solid); cardFormat.setBorderBrush(QBrush(Qt::black)); cardFormat.setWidth(QTextLength(QTextLength::FixedLength, cardWidth)); cardFormat.setHeight(QTextLength(QTextLength::FixedLength, cardHeight)); cardFormat.setPadding(qRound(0.25 * myDpi)); QTextFrameFormat lineFormat; lineFormat.setBorder(1); lineFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Solid); lineFormat.setBorderBrush(QBrush(Qt::black)); lineFormat.setWidth(QTextLength(QTextLength::FixedLength, qRound(4.5 * myDpi))); lineFormat.setHeight(1.1); //1 is drawn as a box whereas this is drawn as a line. Strange... lineFormat.setPadding(0); QTextFrame *card; for (int i = 0; i < model()->rowCount(); i++) { for (int j = 0; j < model()->columnCount(); j++) { cardFormat.setPosition(QTextFrameFormat::FloatLeft); card = table->cellAt(i, j).firstCursorPosition().insertFrame(cardFormat); card->lastCursorPosition().insertText(model()->headerData(j, Qt::Horizontal, Qt::DisplayRole).toString(), headerCharFormat); card->lastCursorPosition().insertFrame(lineFormat); card->lastCursorPosition().insertBlock(); card->lastCursorPosition().insertBlock(); card->lastCursorPosition().insertBlock(cellFormat, cellCharFormat); card->lastCursorPosition().insertText(model()->data(model()->index(i, j)).toString(), cellCharFormat); } } } else { textDoc->rootFrame()->lastCursorPosition().insertText(QStringLiteral("kwordquiz %1").arg(KWQ_VERSION)); if (Prefs::printStyle() == Prefs::EnumPrintStyle::Exam) textDoc->rootFrame()->lastCursorPosition().insertText(' ' + i18n("Name:_____________________________ Date:__________")); QTextTable* table; if (Prefs::printStyle() == Prefs::EnumPrintStyle::Exam) table = textDoc->rootFrame()->lastCursorPosition().insertTable(model()->rowCount() + 1, model()->columnCount() + 2); else table = textDoc->rootFrame()->lastCursorPosition().insertTable(model()->rowCount() + 1, model()->columnCount() + 1); QTextTableFormat tableFormat = table->format(); tableFormat.setHeaderRowCount(1); tableFormat.setBorder(1); tableFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Solid); tableFormat.setCellSpacing(0); tableFormat.setBorderBrush(QBrush(Qt::black)); tableFormat.setCellPadding(2); QVector<QTextLength> constraints; constraints.append(QTextLength(QTextLength::FixedLength, verticalHeader()->width())); constraints.append(QTextLength(QTextLength::FixedLength, columnWidth(0))); constraints.append(QTextLength(QTextLength::FixedLength, columnWidth(1))); if (Prefs::printStyle() == Prefs::EnumPrintStyle::Exam) constraints.append(QTextLength(QTextLength::FixedLength, 50)); tableFormat.setColumnWidthConstraints(constraints); table->setFormat(tableFormat); QTextBlockFormat headerFormat; headerFormat.setAlignment(Qt::AlignHCenter); QTextCharFormat headerCharFormat; headerCharFormat.setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont)); QTextCursor cellCursor; cellCursor = table->cellAt(0, 1).firstCursorPosition(); cellCursor.mergeBlockFormat(headerFormat); cellCursor.mergeCharFormat(headerCharFormat); cellCursor.insertText(model()->headerData(0, Qt::Horizontal, Qt::DisplayRole).toString()); cellCursor = table->cellAt(0, 2).firstCursorPosition(); cellCursor.mergeBlockFormat(headerFormat); cellCursor.mergeCharFormat(headerCharFormat); cellCursor.insertText(model()->headerData(1, Qt::Horizontal, Qt::DisplayRole).toString()); if (Prefs::printStyle() == Prefs::EnumPrintStyle::Exam) { cellCursor = table->cellAt(0, 3).firstCursorPosition(); cellCursor.mergeBlockFormat(headerFormat); cellCursor.mergeCharFormat(headerCharFormat); cellCursor.insertText(i18n("Score")); } headerCharFormat = cellCursor.charFormat(); QTextCharFormat cellCharFormat = cellCursor.charFormat(); cellCharFormat.setFont(Prefs::editorFont()); for (int i = 0; i < model()->rowCount(); i++) { table->cellAt(i + 1, 0).firstCursorPosition().insertText(model()->headerData(i, Qt::Vertical, Qt::DisplayRole).toString(), headerCharFormat); table->cellAt(i + 1, 1).firstCursorPosition().insertText(model()->data(model()->index(i, 0)).toString(), cellCharFormat); if (Prefs::printStyle() == Prefs::EnumPrintStyle::List) table->cellAt(i + 1, 2).firstCursorPosition().insertText(model()->data(model()->index(i, 1)).toString(), cellCharFormat); } } if (sendToPrinter) textDoc->print(printer); }
void Format::parseFrame( QTextCursor &cursor, const QDomElement &element ) { QTextBlock extraBlock; bool hasExtraBlock = false; QDomNode n; for( n = element.firstChild(); !n.isNull(); n = n.nextSibling() ) { QDomElement e = n.toElement(); if ( e.tagName() == "block" ) { if ( e.hasAttribute( "liststyle" ) ) { cursor.setCharFormat( TextFormats::normalCharFormat() ); QTextListFormat f; f.setIndent( e.attribute( "listindent" ).toInt() ); if ( e.attribute( "liststyle" ) == "decimal" ) { f.setStyle( QTextListFormat::ListDecimal ); } else { f.setStyle( QTextListFormat::ListDisc ); } cursor.mergeBlockFormat( TextFormats::normalBlockFormat() ); cursor.insertList( f ); } else if ( n != element.firstChild() ) { QTextBlockFormat f; if ( e.hasAttribute( "blockindent" ) ) { f.setIndent( e.attribute( "blockindent" ).toInt() ); } else { f.setIndent( 0 ); } if ( hasExtraBlock ) { QTextCursor c( extraBlock ); c.setBlockFormat( f ); hasExtraBlock = false; } else { cursor.insertBlock( f ); } } if ( e.hasAttribute( "lastmodified" ) ) { QString str = e.attribute( "lastmodified" ); QDateTime dt = QDateTime::fromString( str, Qt::ISODate ); TextFormats::setLastModified( cursor, dt ); } parseBlock( cursor, e ); if ( e.hasAttribute( "titlestyle" ) ) { if ( e.attribute( "titlestyle" ) == "title" ) { cursor.mergeBlockFormat( TextFormats::titleBlockFormat() ); } else if ( e.attribute( "titlestyle" ) == "subtitle" ) { cursor.mergeBlockFormat( TextFormats::subTitleBlockFormat() ); } } else { cursor.mergeBlockFormat( TextFormats::normalBlockFormat() ); } } else if ( e.tagName() == "frame" ) { QTextFrame *parentFrame = cursor.currentFrame(); QTextFrame *frame = cursor.insertFrame( TextFormats::codeFrameFormat() ); parseFrame( cursor, e ); if ( e.attribute( "type" ) == "code" ) { TextFormats::setCodeFrameFormats( frame ); } cursor = parentFrame->lastCursorPosition(); extraBlock = cursor.block(); hasExtraBlock = true; } } }