MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // 获取文档对象 QTextDocument *document = ui->textEdit->document(); // 获取根框架 QTextFrame *rootFrame = document->rootFrame(); // 创建框架格式 QTextFrameFormat format; // 边界颜色 format.setBorderBrush(Qt::red); // 边界宽度 format.setBorder(3); // 框架使用格式 rootFrame->setFrameFormat(format); QTextFrameFormat frameFormat; // 设置背景颜色 frameFormat.setBackground(Qt::lightGray); // 设置边距 frameFormat.setMargin(10); // 设置填衬 frameFormat.setPadding(5); frameFormat.setBorder(2); frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Dotted); //设置边框样式 // 获取光标 QTextCursor cursor = ui->textEdit->textCursor(); // 在光标处插入框架 cursor.insertFrame(frameFormat); //以下是5.2.2节内容 QAction *action_textFrame = new QAction(tr("框架"),this); connect(action_textFrame,SIGNAL(triggered()),this,SLOT(showTextFrame())); // 在工具栏添加动作 ui->mainToolBar->addAction(action_textFrame); QAction *action_textBlock = new QAction(tr("文本块"),this); connect(action_textBlock,SIGNAL(triggered()),this,SLOT(showTextBlock())); ui->mainToolBar->addAction(action_textBlock); QAction *action_font = new QAction(tr("字体"),this); // 设置动作可以被选中 action_font->setCheckable(true); connect(action_font,SIGNAL(toggled(bool)),this,SLOT(setTextFont(bool))); ui->mainToolBar->addAction(action_font); }
QwtRichTextDocument( const QString &text, int flags, const QFont &font ) { setUndoRedoEnabled( false ); setDefaultFont( font ); setHtml( text ); // make sure we have a document layout ( void )documentLayout(); QTextOption option = defaultTextOption(); if ( flags & Qt::TextWordWrap ) option.setWrapMode( QTextOption::WordWrap ); else option.setWrapMode( QTextOption::NoWrap ); option.setAlignment( ( Qt::Alignment ) flags ); setDefaultTextOption( option ); QTextFrame *root = rootFrame(); QTextFrameFormat fm = root->frameFormat(); fm.setBorder( 0 ); fm.setMargin( 0 ); fm.setPadding( 0 ); fm.setBottomMargin( 0 ); fm.setLeftMargin( 0 ); root->setFrameFormat( fm ); adjustSize(); }
/*! Append a new message to the current session and scroll to the end of the message protocol and returns true if the action was successful. The \a messageColor defines the color of the message box and should be provided as a full-color (no dimming required) color, as it is automatically adjusted for the border and background. */ bool QwcPrivateMessager::appendMessageToCurrentSession(QTextDocument *document, const QString message, const QColor messageColor) { if (!document) { return false; } QTextCursor cursor = document->rootFrame()->lastCursorPosition(); cursor.movePosition(QTextCursor::StartOfBlock); QTextFrameFormat frameFormat; frameFormat.setPadding(4); frameFormat.setBackground(messageColor.lighter(190)); frameFormat.setMargin(0); frameFormat.setBorder(2); frameFormat.setBorderBrush(messageColor.lighter(150)); frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Outset); // Title QTextCharFormat backupCharFormat = cursor.charFormat(); QTextCharFormat newCharFormat; newCharFormat.setFontPointSize(9); QTextBlockFormat headerFormat; headerFormat.setAlignment(Qt::AlignHCenter); cursor.insertBlock(headerFormat); cursor.setCharFormat(newCharFormat); cursor.insertText(QDateTime::currentDateTime().toString()); QTextFrame *frame = cursor.insertFrame(frameFormat); cursor.setCharFormat(backupCharFormat); frame->firstCursorPosition().insertText(message); return true; }
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 ); }
TextLayer::TextLayer(const int layer_id , QGraphicsItem *parent , QGraphicsScene *scene ) : QGraphicsItem(parent,scene),evesum(0),modus(Show),border(1.),currentprintrender(false), hi(Metric("30px")),wi(Metric("110px")),bgcolor(QColor(Qt::white)),SwapLockBreak(false), bordercolor(QColor(Qt::red)),Rotate(0),check_view_area_time(0),ActionHover(false), format(DIV_ABSOLUTE),mount(new TextController) { mount->q = this; setAcceptsHoverEvents(true); wisub_border = wi + border; history.clear(); id = layer_id; setAcceptDrops(true); setFlag(QGraphicsItem::ItemIsSelectable,true); setFlag(QGraphicsItem::ItemIsFocusable,true); setFlag(QGraphicsItem::ItemIsMovable,false); setSelected(false); _doc = new QTextDocument(); _doc->setHtml(tr("<p>Write your text<p>")); QTextFrame *Tframe = _doc->rootFrame(); QTextFrameFormat rootformats = Tframe->frameFormat(); rootformats.setWidth(wi); rootformats.setBorder(0); Tframe->setFrameFormat(rootformats); _doc->setPageSize(QSizeF(wi,hi)); DLayout = _doc->documentLayout(); setDocument(_doc); mount->txtControl()->document()->toHtml().size(); /* connect all */ setZValue(1.99999); RestoreMoveAction(); init(); }
QTextFrameFormat DefaultFrameFormat() { QTextFrameFormat base; base.setBorderBrush ( Qt::blue ); base.setBorderStyle ( QTextFrameFormat::BorderStyle_Solid ); base.setBorder ( 0.6 ); return base; }
void KDReports::Frame::build( ReportBuilder& builder ) const { // prepare the frame QTextFrameFormat format; if ( d->m_width ) { if ( d->m_widthUnit == Millimeters ) { format.setWidth( QTextLength( QTextLength::FixedLength, mmToPixels( d->m_width ) ) ); } else { format.setWidth( QTextLength( QTextLength::PercentageLength, d->m_width ) ); } } if ( d->m_height ) { if ( d->m_heightUnit == Millimeters ) { format.setHeight( QTextLength( QTextLength::FixedLength, mmToPixels( d->m_height ) ) ); } else { format.setHeight( QTextLength( QTextLength::PercentageLength, d->m_height ) ); } } format.setPadding( mmToPixels( padding() ) ); format.setBorder( d->m_border ); // TODO borderBrush like in AbstractTableElement format.setPosition( QTextFrameFormat::InFlow ); QTextCursor& textDocCursor = builder.cursor(); QTextFrame *frame = textDocCursor.insertFrame(format); QTextCursor contentsCursor = frame->firstCursorPosition(); ReportBuilder contentsBuilder( builder.currentDocumentData(), contentsCursor, builder.report() ); contentsBuilder.copyStateFrom( builder ); foreach( const KDReports::ElementData& ed, d->m_elements ) { switch ( ed.m_type ) { case KDReports::ElementData::Inline: contentsBuilder.addInlineElement( *ed.m_element ); break; case KDReports::ElementData::Block: contentsBuilder.addBlockElement( *ed.m_element, ed.m_align ); break; case KDReports::ElementData::Variable: contentsBuilder.addVariable( ed.m_variableType ); break; } } textDocCursor.movePosition( QTextCursor::End ); }
/*! * \class TextCursorChangeBorder * \author Anders Fernström * \date 2005-11-03 * \date 2005-11-07 (update) * * \brief Command for changing border * * 2005-11-07 AF, implemented the function */ void TextCursorChangeBorder::execute() { QTextEdit *editor = document()->getCursor()->currentCell()->textEdit(); if( editor ) { QTextFrameFormat format = editor->document()->rootFrame()->frameFormat(); format.setBorder( border_ ); editor->document()->rootFrame()->setFrameFormat( format ); // create a rule for the border QString ruleValue; ruleValue.setNum( border_ ); Rule *rule = new Rule( "OMNotebook_Border", ruleValue ); document()->getCursor()->currentCell()->addRule( rule ); // update the cells style document()->getCursor()->currentCell()->style()->textFrameFormat()->setBorder( border_ ); } }
//! [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())); }
MainWindow::MainWindow() { QMenu *fileMenu = new QMenu(tr("&File")); QAction *saveAction = fileMenu->addAction(tr("&Save...")); saveAction->setShortcut(tr("Ctrl+S")); QAction *quitAction = fileMenu->addAction(tr("E&xit")); quitAction->setShortcut(tr("Ctrl+Q")); menuBar()->addMenu(fileMenu); editor = new QTextEdit(); QTextCursor cursor(editor->textCursor()); cursor.movePosition(QTextCursor::Start); QTextFrame *mainFrame = cursor.currentFrame(); QTextCharFormat plainCharFormat; QTextCharFormat boldCharFormat; boldCharFormat.setFontWeight(QFont::Bold); /* main frame //! [0] QTextFrame *mainFrame = cursor.currentFrame(); cursor.insertText(...); //! [0] */ cursor.insertText("Text documents are represented by the " "QTextDocument class, rather than by QString objects. " "Each QTextDocument object contains information about " "the document's internal representation, its structure, " "and keeps track of modifications to provide undo/redo " "facilities. This approach allows features such as the " "layout management to be delegated to specialized " "classes, but also provides a focus for the framework.", plainCharFormat); //! [1] QTextFrameFormat frameFormat; frameFormat.setMargin(32); frameFormat.setPadding(8); frameFormat.setBorder(4); //! [1] cursor.insertFrame(frameFormat); /* insert frame //! [2] cursor.insertFrame(frameFormat); cursor.insertText(...); //! [2] */ cursor.insertText("Documents are either converted from external sources " "or created from scratch using Qt. The creation process " "can done by an editor widget, such as QTextEdit, or by " "explicit calls to the Scribe API.", boldCharFormat); cursor = mainFrame->lastCursorPosition(); /* last cursor //! [3] cursor = mainFrame->lastCursorPosition(); cursor.insertText(...); //! [3] */ cursor.insertText("There are two complementary ways to visualize the " "contents of a document: as a linear buffer that is " "used by editors to modify the contents, and as an " "object hierarchy containing structural information " "that is useful to layout engines. In the hierarchical " "model, the objects generally correspond to visual " "elements such as frames, tables, and lists. At a lower " "level, these elements describe properties such as the " "style of text used and its alignment. The linear " "representation of the document is used for editing and " "manipulation of the document's contents.", plainCharFormat); connect(saveAction, SIGNAL(triggered()), this, SLOT(saveFile())); connect(quitAction, SIGNAL(triggered()), this, SLOT(close())); setCentralWidget(editor); setWindowTitle(tr("Text Document Frames")); }
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 DTipDialog::setupGUI() { setWindowTitle(tr("Tip of day")); int h,s,v; QColor baseColor = palette().base().color(); baseColor.getHsv(&h,&s,&v); baseColor.setHsv(h, int(s*(71/76.0)), int(v*(67/93.0))); // baseColor.setHsv(h, int(s*(10/6.0)), int(v*(93/99.0))); QVBoxLayout *layout = new QVBoxLayout(this); m_textArea = new QTextBrowser; QTextFrameFormat format = m_textArea->document()->rootFrame()->frameFormat(); format.setMargin(15); format.setBorder(5); m_textArea->document()->rootFrame()->setFrameFormat(format); m_textArea->setWordWrapMode( QTextOption::WrapAtWordBoundaryOrAnywhere ); m_textArea->setFrameStyle(QFrame::NoFrame | QFrame::Plain); m_textArea->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); QPalette pal = m_textArea->palette(); pal.setBrush(QPalette::Base, baseColor); m_textArea->setPalette(pal); layout->addWidget(m_textArea); layout->addWidget(new KSeparator); QHBoxLayout *buttonLayout = new QHBoxLayout; m_showOnStart = new QCheckBox(tr("show on start")); buttonLayout->addWidget(m_showOnStart); connect(m_showOnStart, SIGNAL(clicked()), this, SLOT(setShowOnStart())); buttonLayout->addStretch(1); QPushButton *prevTip = new QPushButton(tr("Previous tip")); buttonLayout->addWidget(prevTip); connect(prevTip, SIGNAL(clicked()), this, SLOT(showPrevTip())); QPushButton *nextTip = new QPushButton(tr("Next tip")); buttonLayout->addWidget(nextTip); connect(nextTip, SIGNAL(clicked()), this, SLOT(showNextTip())); QPushButton *close = new QPushButton(tr("close")); buttonLayout->addWidget(close); connect(close, SIGNAL(clicked()), this, SLOT(accept())); layout->addLayout(buttonLayout); setAttribute(Qt::WA_DeleteOnClose, true); DCONFIG->beginGroup("TipOfDay"); m_showOnStart->setChecked(qvariant_cast<bool>(DCONFIG->value("ShowOnStart", true ) )); showNextTip(); }