void HeaderedTextEdit::_q_resetNewBlockMargin() { // This method shouldn't be used if text block format has LeftMargin set. QTextBlockFormat tbf = textCursor().blockFormat(); if (textCursor().blockNumber() == 0) { tbf.setTextIndent(headerLabel->size().width()); } else { tbf.setTextIndent(0); } textCursor().setBlockFormat(tbf); }
void QTextDocumentExporter::writeParagraphBegin(CEDParagraph& par) { QTextBlockFormat format; switch(par.align()) { case ALIGN_NONE: case ALIGN_LEFT: format.setAlignment(Qt::AlignLeft); break; case ALIGN_RIGHT: format.setAlignment(Qt::AlignRight); break; case ALIGN_CENTER: format.setAlignment(Qt::AlignHCenter); break; case ALIGN_JUSTIFY: format.setAlignment(Qt::AlignJustify); break; } if(par.indent() != 0) format.setTextIndent(par.indent()); cursor_.beginEditBlock(); cursor_.insertBlock(format); cursor_.movePosition(QTextCursor::StartOfBlock); line_num_in_par_ = 0; par_line_count_ = par.lineCount(); }
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 HeaderedTextEdit::updateTextLeftMargin() { QSizeF labelSize = headerLabel->sizeHint(Qt::PreferredSize, QSizeF(-1, -1)); QTextCursor cursor(document()); cursor.setPosition(0); QTextBlockFormat tbf = cursor.blockFormat(); // Will not overwrite label if LeftMargin for // text block format is set. Usefuly for only inputs // such as emails. // tbf.setLeftMargin(labelSize.width()); tbf.setTextIndent(labelSize.width()); Qt::LayoutDirection dir = MLocale::directionForText(headerLabel->text()); setLayoutDirection(dir); updateLabelPosition(geometry()); cursor.setBlockFormat(tbf); }
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; }
int main(int argc, char *argv[]) { QApplication app(argc, argv); //! [0] QTextEdit *editor = new QTextEdit(); QTextCursor cursor(editor->textCursor()); //! [0] cursor.movePosition(QTextCursor::Start); QTextBlockFormat blockFormat = cursor.blockFormat(); blockFormat.setTopMargin(4); blockFormat.setLeftMargin(4); blockFormat.setRightMargin(4); blockFormat.setBottomMargin(4); cursor.setBlockFormat(blockFormat); cursor.insertText(tr("This contains plain text inside a " "text block with margins to keep it separate " "from other parts of the document.")); cursor.insertBlock(); //! [1] QTextBlockFormat backgroundFormat = blockFormat; backgroundFormat.setBackground(QColor("lightGray")); cursor.setBlockFormat(backgroundFormat); //! [1] cursor.insertText(tr("The background color of a text block can be " "changed to highlight text.")); cursor.insertBlock(); QTextBlockFormat rightAlignedFormat = blockFormat; rightAlignedFormat.setAlignment(Qt::AlignRight); cursor.setBlockFormat(rightAlignedFormat); cursor.insertText(tr("The alignment of the text within a block is " "controlled by the alignment properties of " "the block itself. This text block is " "right-aligned.")); cursor.insertBlock(); QTextBlockFormat paragraphFormat = blockFormat; paragraphFormat.setAlignment(Qt::AlignJustify); paragraphFormat.setTextIndent(32); cursor.setBlockFormat(paragraphFormat); cursor.insertText(tr("Text can be formatted so that the first " "line in a paragraph has its own margin. " "This makes the text more readable.")); cursor.insertBlock(); QTextBlockFormat reverseFormat = blockFormat; reverseFormat.setAlignment(Qt::AlignJustify); reverseFormat.setTextIndent(32); cursor.setBlockFormat(reverseFormat); cursor.insertText(tr("The direction of the text can be reversed. " "This is useful for right-to-left " "languages.")); editor->setWindowTitle(tr("Text Block Formats")); editor->resize(480, 480); editor->show(); return app.exec(); }
ScriptFormatter::ScriptFormatter( QQmlEngine* newEngine, QObject *parent ) : QObject(parent) { QFont baseFont; //The base from which all other fonts are derived baseFont.setBold( false ); baseFont.setCapitalization( QFont::MixedCase ); baseFont.setFamily( "Courier" ); baseFont.setItalic( false ); baseFont.setPointSize( 12 ); baseFont.setStyleHint( QFont::Courier ); QTextBlockFormat baseFormat; baseFormat.setAlignment( Qt::AlignLeft ); baseFormat.setIndent( 0 ); baseFormat.setLineHeight( 1, QTextBlockFormat::SingleHeight ); //The first argument should be ignored according to the documentation, since we're setting the LineHeightType (2nd argument) to single height baseFormat.setNonBreakableLines( false ); //baseFormat.setPageBreakPolicy( QTextFormat::PageBreak_Auto ); baseFormat.setTextIndent( 0 ); baseFormat.setTopMargin( 0 ); baseFormat.setBottomMargin( 0 ); baseFormat.setLeftMargin( 0 ); baseFormat.setRightMargin( 0 ); baseFormat.setNonBreakableLines( false ); //Scenes are left-aligned, bold, and all caps sceneFont = QFont( baseFont ); sceneFont.setBold( true ); sceneFont.setCapitalization( QFont::AllUppercase ); sceneBlockFormat = QTextBlockFormat( baseFormat ); sceneBlockFormat.setAlignment( Qt::AlignLeft ); //sceneBlockFormat.setPageBreakPolicy( QTextFormat::PageBreak_AlwaysBefore ); //Actions are left-aligned actionFont = QFont( baseFont ); actionBlockFormat = QTextBlockFormat( baseFormat ); actionBlockFormat.setAlignment( Qt::AlignLeft ); //Character names are centered and all caps characterFont = QFont( baseFont ); characterFont.setCapitalization( QFont::AllUppercase ); characterBlockFormat = QTextBlockFormat( baseFormat ); characterBlockFormat.setAlignment( Qt::AlignHCenter ); //characterBlockFormat.setPageBreakPolicy( QTextFormat::PageBreak_AlwaysBefore ); //Dialog is centered dialogFont = QFont( baseFont ); dialogBlockFormat = QTextBlockFormat( baseFormat ); dialogBlockFormat.setAlignment( Qt::AlignHCenter ); //dialogBlockFormat.setPageBreakPolicy( QTextFormat::PageBreak_AlwaysAfter ); //Parentheticals are centered and italicized parentheticalFont = QFont( baseFont ); parentheticalFont.setItalic( true ); parentheticalBlockFormat = QTextBlockFormat( baseFormat ); parentheticalBlockFormat.setAlignment( Qt::AlignHCenter ); //Transitions are right-aligned and all caps transitionFont = QFont( baseFont ); transitionFont.setCapitalization( QFont::AllUppercase ); transitionBlockFormat = QTextBlockFormat( baseFormat ); transitionBlockFormat.setAlignment( Qt::AlignRight ); //Shots are left-aligned and all caps shotFont = QFont( baseFont ); shotFont.setCapitalization( QFont::AllUppercase ); shotBlockFormat = QTextBlockFormat( baseFormat ); shotBlockFormat.setAlignment( Qt::AlignLeft ); //Act breaks are centered, bold, and underlined actBreakFont = QFont( baseFont ); actBreakFont.setBold( true ); actBreakFont.setUnderline( true ); actBreakBlockFormat = QTextBlockFormat( baseFormat ); actBreakBlockFormat.setAlignment( Qt::AlignHCenter ); }