// TEXTCELL void PrinterVisitor::visitTextCellNodeBefore(TextCell *node) { if( !ignore_ || firstChild_ ) { ++currentTableRow_; table_->insertRows( currentTableRow_, 1 ); // first column QTextTableCell tableCell( table_->cellAt( currentTableRow_, 0 ) ); if( tableCell.isValid() ) { if( !node->ChapterCounterHtml().isNull() ) { QTextCursor cursor( tableCell.firstCursorPosition() ); cursor.insertFragment( QTextDocumentFragment::fromHtml( node->ChapterCounterHtml() )); } } // second column tableCell = table_->cellAt( currentTableRow_, 1 ); if( tableCell.isValid() ) { QTextCursor cursor( tableCell.firstCursorPosition() ); if( node->isViewExpression() ) { //view expression table QTextTableFormat tableFormatExpression; tableFormatExpression.setBorder( 0 ); tableFormatExpression.setColumns( 1 ); tableFormatExpression.setCellPadding( 2 ); // tableFormatExpression.setBackground( QColor(235, 235, 220) ); // 180, 180, 180 tableFormatExpression.setBackground( QColor(235, 0, 0) ); // 180, 180, 180 QVector<QTextLength> constraints; constraints << QTextLength(QTextLength::PercentageLength, 100); tableFormatExpression.setColumnWidthConstraints(constraints); cursor.insertTable( 1, 1, tableFormatExpression ); // QMessageBox::information(0,"uu2", node->text()); QString html = node->textHtml(); cursor.insertFragment( QTextDocumentFragment::fromHtml( html )); } else { QString html = node->textHtml(); html.remove( "file:///" ); printEditor_->document()->setTextWidth(700); cursor.insertFragment(QTextDocumentFragment::fromHtml( html )); // QMessageBox::information(0, "uu3", node->text()); } } if( firstChild_ ) firstChild_ = false; } }
QObject* TextCursor::insertTable(int rows, int columns) { QTextTableFormat format; //format.setColumns(columns); //format.setHeaderRowCount(1); format.setBackground(QColor("#e0e0e0")); //format.setCellPadding(1); format.setCellSpacing(1); //testcase QVector<QTextLength> constraints; constraints << QTextLength(QTextLength::PercentageLength, 16); constraints << QTextLength(QTextLength::PercentageLength, 28); constraints << QTextLength(QTextLength::PercentageLength, 28); constraints << QTextLength(QTextLength::PercentageLength, 28); format.setColumnWidthConstraints(constraints); QTextTable* table = m_cursor.insertTable(rows, columns, format); //QTextTable* t = m_cursor.insertTable(rows, columns); QTextTableCell cell = table->cellAt(0, 0); cell.firstCursorPosition().insertText(tr("aaa") /*, QTextCharFormat::charFormat*/); table->cellAt(0, 1).firstCursorPosition().insertText(tr("bbb")); return new TextTable(this, table); }
//! [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")); QMenu *showMenu = new QMenu(tr("&Show")); QAction *showTableAction = showMenu->addAction(tr("&Table")); menuBar()->addMenu(fileMenu); menuBar()->addMenu(showMenu); editor = new QTextEdit(); //! [0] //! [1] QTextCursor cursor(editor->textCursor()); //! [0] cursor.movePosition(QTextCursor::Start); //! [1] int rows = 11; int columns = 4; //! [2] QTextTableFormat tableFormat; tableFormat.setBackground(QColor("#e0e0e0")); QVector<QTextLength> constraints; constraints << QTextLength(QTextLength::PercentageLength, 16); constraints << QTextLength(QTextLength::PercentageLength, 28); constraints << QTextLength(QTextLength::PercentageLength, 28); constraints << QTextLength(QTextLength::PercentageLength, 28); tableFormat.setColumnWidthConstraints(constraints); //! [3] QTextTable *table = cursor.insertTable(rows, columns, tableFormat); //! [2] //! [3] int column; int row; QTextTableCell cell; QTextCursor cellCursor; QTextCharFormat charFormat; charFormat.setForeground(Qt::black); //! [4] cell = table->cellAt(0, 0); cellCursor = cell.firstCursorPosition(); cellCursor.insertText(tr("Week"), charFormat); //! [4] //! [5] for (column = 1; column < columns; ++column) { cell = table->cellAt(0, column); cellCursor = cell.firstCursorPosition(); cellCursor.insertText(tr("Team %1").arg(column), charFormat); } for (row = 1; row < rows; ++row) { cell = table->cellAt(row, 0); cellCursor = cell.firstCursorPosition(); cellCursor.insertText(tr("%1").arg(row), charFormat); for (column = 1; column < columns; ++column) { if ((row-1) % 3 == column-1) { //! [5] //! [6] cell = table->cellAt(row, column); QTextCursor cellCursor = cell.firstCursorPosition(); cellCursor.insertText(tr("On duty"), charFormat); } //! [6] //! [7] } //! [7] //! [8] } //! [8] connect(saveAction, SIGNAL(triggered()), this, SLOT(saveFile())); connect(quitAction, SIGNAL(triggered()), this, SLOT(close())); connect(showTableAction, SIGNAL(triggered()), this, SLOT(showTable())); setCentralWidget(editor); setWindowTitle(tr("Text Document Tables")); }
void PrinterVisitor::visitLatexCellNodeBefore(LatexCell *node) { if( !ignore_ || firstChild_ ) { ++currentTableRow_; table_->insertRows( currentTableRow_, 1 ); // first column QTextTableCell tableCell( table_->cellAt( currentTableRow_, 0 ) ); if( tableCell.isValid() ) { if( !node->ChapterCounterHtml().isNull() ) { QTextCursor cursor( tableCell.firstCursorPosition() ); cursor.insertFragment( QTextDocumentFragment::fromHtml( node->ChapterCounterHtml() )); } } // second column tableCell = table_->cellAt( currentTableRow_, 1 ); if( tableCell.isValid() ) { QTextCursor cursor( tableCell.firstCursorPosition() ); // input table QTextTableFormat tableFormatInput; tableFormatInput.setBorder( 0 ); tableFormatInput.setMargin( 6 ); tableFormatInput.setColumns( 1 ); tableFormatInput.setCellPadding( 8 ); tableFormatInput.setBackground( QColor(245, 245, 255) ); // 200, 200, 255 QVector<QTextLength> constraints; constraints << QTextLength(QTextLength::PercentageLength, 100); tableFormatInput.setColumnWidthConstraints(constraints); cursor.insertTable( 1, 1, tableFormatInput ); QString html = node->textHtml(); html += "<br>"; if( !node->isEvaluated() || node->isClosed() ) html += "<br>"; cursor.insertFragment( QTextDocumentFragment::fromHtml( html )); if( node->isEvaluated() && !node->isClosed() ) { QTextTableFormat tableFormatOutput; tableFormatOutput.setBorder( 0 ); tableFormatOutput.setMargin( 6 ); tableFormatOutput.setColumns( 1 ); tableFormatOutput.setCellPadding( 8 ); QVector<QTextLength> constraints; constraints << QTextLength(QTextLength::PercentageLength, 100); tableFormatOutput.setColumnWidthConstraints(constraints); cursor = tableCell.lastCursorPosition(); cursor.insertTable( 1, 1, tableFormatOutput ); QString outputHtml( node->textOutputHtml() ); outputHtml += "<br><br>"; outputHtml.remove( "file:///" ); cursor.insertFragment( QTextDocumentFragment::fromHtml( outputHtml )); } } if( firstChild_ ) firstChild_ = false; } }
void PrinterVisitor::visitGraphCellNodeBefore(GraphCell *node) { if( !ignore_ || firstChild_ ) { ++currentTableRow_; table_->insertRows( currentTableRow_, 1 ); // first column QTextTableCell tableCell( table_->cellAt( currentTableRow_, 0 ) ); if( tableCell.isValid() ) { if( !node->ChapterCounterHtml().isNull() ) { QTextCursor cursor( tableCell.firstCursorPosition() ); cursor.insertFragment( QTextDocumentFragment::fromHtml( node->ChapterCounterHtml() )); } } // second column tableCell = table_->cellAt( currentTableRow_, 1 ); if( tableCell.isValid() ) { QTextCursor cursor( tableCell.firstCursorPosition() ); // input table QTextTableFormat tableFormatInput; tableFormatInput.setBorder( 0 ); tableFormatInput.setColumns( 1 ); tableFormatInput.setCellPadding( 2 ); tableFormatInput.setBackground( QColor(245, 245, 255) ); // 200, 200, 255 QVector<QTextLength> constraints; constraints << QTextLength(QTextLength::PercentageLength, 100); tableFormatInput.setColumnWidthConstraints(constraints); cursor.insertTable( 1, 1, tableFormatInput ); QString html = node->textHtml(); if( !node->isEvaluated() || node->isClosed() ) html += "<br />"; cursor.insertFragment( QTextDocumentFragment::fromHtml( html )); if( node->isEvaluated() && !node->isClosed() ) { QTextTableFormat tableFormatOutput; tableFormatOutput.setBorder( 0 ); tableFormatOutput.setColumns( 1 ); tableFormatOutput.setCellPadding( 2 ); QVector<QTextLength> constraints; constraints << QTextLength(QTextLength::PercentageLength, 100); tableFormatOutput.setColumnWidthConstraints(constraints); cursor = tableCell.lastCursorPosition(); cursor.insertTable( 1, 1, tableFormatOutput ); QString outputHtml( node->textOutputHtml() ); outputHtml.remove( "file:///" ); cursor.insertFragment( QTextDocumentFragment::fromHtml( outputHtml )); } } // print the plot if (node->mpPlotWindow && node->mpPlotWindow->isVisible()) { ++currentTableRow_; table_->insertRows( currentTableRow_, 1 ); // first column tableCell = table_->cellAt( currentTableRow_, 1 ); if( tableCell.isValid() ) { QTextCursor cursor( tableCell.firstCursorPosition() ); // input table QTextTableFormat tableFormatInput; tableFormatInput.setBorder( 0 ); tableFormatInput.setColumns( 1 ); tableFormatInput.setCellPadding( 2 ); QVector<QTextLength> constraints; constraints << QTextLength(QTextLength::PercentageLength, 100); tableFormatInput.setColumnWidthConstraints(constraints); cursor.insertTable( 1, 1, tableFormatInput ); OMPlot::Plot *pPlot = node->mpPlotWindow->getPlot(); // calculate height for widht while preserving aspect ratio. int width, height; if (pPlot->size().width() > 600) { width = 600; qreal ratio = (double)pPlot->size().height() / pPlot->size().width(); height = width * qCeil(ratio); } else { width = pPlot->size().width(); height = pPlot->size().height(); } // create a pixmap and render the plot on it QPixmap plotPixmap(width, height); QwtPlotRenderer plotRenderer; plotRenderer.renderTo(pPlot, plotPixmap); // insert the pixmap to table cursor.insertImage(plotPixmap.toImage()); } } if( firstChild_ ) firstChild_ = false; } }