text_writer( docEdit& e ) : edit( e ) { QTextCursor cursor = edit.textCursor(); cursor.movePosition( QTextCursor::Start ); mainFrame = cursor.currentFrame(); plainFormat = cursor.charFormat(); plainFormat.setFontPointSize( 10 ); paragraphFormat = plainFormat; paragraphFormat.setFontPointSize( 12 ); headingFormat = plainFormat; headingFormat.setFontWeight( QFont::Bold ); headingFormat.setFontPointSize( 16 ); empasisFormat = plainFormat; empasisFormat.setFontItalic( true ); tagFormat = plainFormat; tagFormat.setForeground( QColor( "#990000" ) ); tagFormat.setFontUnderline( true ); underlineFormat = plainFormat; underlineFormat.setFontUnderline( true ); frameFormat.setBorderStyle( QTextFrameFormat::BorderStyle_Inset ); frameFormat.setBorder( 1 ); frameFormat.setMargin( 10 ); frameFormat.setPadding( 4 ); }
void MainWindow::selectFrame() { QTextCursor cursor = editor->textCursor(); QTextFrame *frame = cursor.currentFrame(); cursor.beginEditBlock(); cursor.setPosition(frame->firstPosition()); cursor.setPosition(frame->lastPosition(), QTextCursor::KeepAnchor); cursor.endEditBlock(); editor->setTextCursor(cursor); }
//! [5] void MainWindow::insertCalendar() { editor->clear(); QTextCursor cursor = editor->textCursor(); cursor.beginEditBlock(); QDate date(selectedDate.year(), selectedDate.month(), 1); //! [5] //! [6] QTextTableFormat tableFormat; tableFormat.setAlignment(Qt::AlignHCenter); tableFormat.setBackground(QColor("#e0e0e0")); tableFormat.setCellPadding(2); tableFormat.setCellSpacing(4); //! [6] //! [7] QVector<QTextLength> constraints; constraints << QTextLength(QTextLength::PercentageLength, 14) << QTextLength(QTextLength::PercentageLength, 14) << QTextLength(QTextLength::PercentageLength, 14) << QTextLength(QTextLength::PercentageLength, 14) << QTextLength(QTextLength::PercentageLength, 14) << QTextLength(QTextLength::PercentageLength, 14) << QTextLength(QTextLength::PercentageLength, 14); tableFormat.setColumnWidthConstraints(constraints); //! [7] //! [8] QTextTable *table = cursor.insertTable(1, 7, tableFormat); //! [8] //! [9] QTextFrame *frame = cursor.currentFrame(); QTextFrameFormat frameFormat = frame->frameFormat(); frameFormat.setBorder(1); frame->setFrameFormat(frameFormat); //! [9] //! [10] QTextCharFormat format = cursor.charFormat(); format.setFontPointSize(fontSize); QTextCharFormat boldFormat = format; boldFormat.setFontWeight(QFont::Bold); QTextCharFormat highlightedFormat = boldFormat; highlightedFormat.setBackground(Qt::yellow); //! [10] //! [11] for (int weekDay = 1; weekDay <= 7; ++weekDay) { QTextTableCell cell = table->cellAt(0, weekDay-1); //! [11] //! [12] QTextCursor cellCursor = cell.firstCursorPosition(); cellCursor.insertText(QString("%1").arg(QDate::longDayName(weekDay)), boldFormat); } //! [12] //! [13] table->insertRows(table->rows(), 1); //! [13] while (date.month() == selectedDate.month()) { int weekDay = date.dayOfWeek(); QTextTableCell cell = table->cellAt(table->rows()-1, weekDay-1); QTextCursor cellCursor = cell.firstCursorPosition(); if (date == QDate::currentDate()) cellCursor.insertText(QString("%1").arg(date.day()), highlightedFormat); else cellCursor.insertText(QString("%1").arg(date.day()), format); date = date.addDays(1); if (weekDay == 7 && date.month() == selectedDate.month()) table->insertRows(table->rows(), 1); } cursor.endEditBlock(); //! [14] setWindowTitle(tr("Calendar for %1 %2" ).arg(QDate::longMonthName(selectedDate.month()) ).arg(selectedDate.year())); }
void MainWindow::showList() { QTextCursor cursor = editor->textCursor(); QTextFrame *frame = cursor.currentFrame(); if (!frame) return; QTreeWidget *treeWidget = new QTreeWidget; treeWidget->setColumnCount(1); QStringList headerLabels; headerLabels << tr("Lists"); treeWidget->setHeaderLabels(headerLabels); QTreeWidgetItem *parentItem = 0; QTreeWidgetItem *item; QTreeWidgetItem *lastItem = 0; parentItems.clear(); previousItems.clear(); //! [3] QTextFrame::iterator it; for (it = frame->begin(); !(it.atEnd()); ++it) { QTextBlock block = it.currentBlock(); if (block.isValid()) { QTextList *list = block.textList(); if (list) { int index = list->itemNumber(block); //! [3] if (index == 0) { parentItems.append(parentItem); previousItems.append(lastItem); listStructures.append(list); parentItem = lastItem; lastItem = 0; if (parentItem != 0) item = new QTreeWidgetItem(parentItem, lastItem); else item = new QTreeWidgetItem(treeWidget, lastItem); } else { while (parentItem != 0 && listStructures.last() != list) { listStructures.pop_back(); parentItem = parentItems.takeLast(); lastItem = previousItems.takeLast(); } if (parentItem != 0) item = new QTreeWidgetItem(parentItem, lastItem); else item = new QTreeWidgetItem(treeWidget, lastItem); } item->setText(0, block.text()); lastItem = item; /* //! [4] processListItem(list, index); //! [4] */ //! [5] } //! [5] //! [6] } //! [6] //! [7] } //! [7] treeWidget->setWindowTitle(tr("List Contents")); treeWidget->show(); }
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; } } }