void ChatDialog::appendChatMessage(const QString& nick, const QString& text, time_t timestamp) { QTextCharFormat nickFormat; nickFormat.setForeground(Qt::darkGreen); nickFormat.setFontWeight(QFont::Bold); nickFormat.setFontUnderline(true); nickFormat.setUnderlineColor(Qt::gray); // Print who & when QTextCursor cursor(ui->textEdit->textCursor()); cursor.movePosition(QTextCursor::End); QTextTableFormat tableFormat; tableFormat.setBorder(0); QTextTable *table = cursor.insertTable(1, 2, tableFormat); QString from = QString("%1 ").arg(nick); QTextTableCell fromCell = table->cellAt(0, 0); fromCell.setFormat(nickFormat); fromCell.firstCursorPosition().insertText(from); printTimeInCell(table, timestamp); // Print what QTextCursor nextCursor(ui->textEdit->textCursor()); nextCursor.movePosition(QTextCursor::End); table = nextCursor.insertTable(1, 1, tableFormat); table->cellAt(0, 0).firstCursorPosition().insertText(text); // Popup notification showMessage(from, text); QScrollBar *bar = ui->textEdit->verticalScrollBar(); bar->setValue(bar->maximum()); }
void DeckListModel::printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node) { static const int totalColumns = 3; if (node->height() == 1) { QTextBlockFormat blockFormat; QTextCharFormat charFormat; charFormat.setFontPointSize(11); charFormat.setFontWeight(QFont::Bold); cursor->insertBlock(blockFormat, charFormat); cursor->insertText(QString("%1: %2").arg(node->getVisibleName()).arg(node->recursiveCount(true))); QTextTableFormat tableFormat; tableFormat.setCellPadding(0); tableFormat.setCellSpacing(0); tableFormat.setBorder(0); QTextTable *table = cursor->insertTable(node->size() + 1, 2, tableFormat); for (int i = 0; i < node->size(); i++) { AbstractDecklistCardNode *card = dynamic_cast<AbstractDecklistCardNode *>(node->at(i)); QTextCharFormat cellCharFormat; cellCharFormat.setFontPointSize(9); QTextTableCell cell = table->cellAt(i, 0); cell.setFormat(cellCharFormat); QTextCursor cellCursor = cell.firstCursorPosition(); cellCursor.insertText(QString("%1 ").arg(card->getNumber())); cell = table->cellAt(i, 1); cell.setFormat(cellCharFormat); cellCursor = cell.firstCursorPosition(); cellCursor.insertText(card->getName()); } } else if (node->height() == 2) { QTextBlockFormat blockFormat; QTextCharFormat charFormat; charFormat.setFontPointSize(14); charFormat.setFontWeight(QFont::Bold); cursor->insertBlock(blockFormat, charFormat); cursor->insertText(QString("%1: %2").arg(node->getVisibleName()).arg(node->recursiveCount(true))); QTextTableFormat tableFormat; tableFormat.setCellPadding(10); tableFormat.setCellSpacing(0); tableFormat.setBorder(0); QVector<QTextLength> constraints; for (int i = 0; i < totalColumns; i++) constraints << QTextLength(QTextLength::PercentageLength, 100.0 / totalColumns); tableFormat.setColumnWidthConstraints(constraints); QTextTable *table = cursor->insertTable(1, totalColumns, tableFormat); for (int i = 0; i < node->size(); i++) { QTextCursor cellCursor = table->cellAt(0, (i * totalColumns) / node->size()).lastCursorPosition(); printDeckListNode(&cellCursor, dynamic_cast<InnerDecklistNode *>(node->at(i))); } } cursor->movePosition(QTextCursor::End); }
FrameIterator::FrameIterator(const QTextTableCell &cell) { Q_ASSERT(cell.isValid()); it = cell.begin(); currentTableIterator = 0; currentSubFrameIterator = 0; lineTextStart = -1; endNoteIndex = 0; }
void FillCellHelper::fill( QTextTable* textTable, KDReports::ReportBuilder& builder, QTextDocument& textDoc, QTextTableCell& cell ) { cellCursor = cell.firstCursorPosition(); QTextCharFormat cellFormat = cell.format(); if ( background.isValid() ) { cellFormat.setBackground( background ); } cellFormat.setVerticalAlignment( toVerticalAlignment( alignment ) ); cell.setFormat( cellFormat ); QTextBlockFormat blockFormat = cellCursor.blockFormat(); blockFormat.setAlignment( alignment ); blockFormat.setNonBreakableLines( nonBreakableLines ); builder.setupBlockFormat( blockFormat ); cellCursor.setBlockFormat( blockFormat ); const bool hasIcon = !cellDecoration.isNull(); const bool iconAfterText = decorationAlignment.isValid() && ( decorationAlignment.toInt() & Qt::AlignRight ); if ( hasIcon && !iconAfterText ) { insertDecoration( builder, textDoc ); } QTextCharFormat charFormat = cellCursor.charFormat(); if ( cellFont.isValid() ) { QFont cellQFont = qvariant_cast<QFont>( cellFont ); #if QT_VERSION >= QT_VERSION_CHECK(5, 3, 0) charFormat.setFont( cellQFont, QTextCharFormat::FontPropertiesSpecifiedOnly ); #else charFormat.setFont( cellQFont ); #endif } else { charFormat.setFont( builder.defaultFont() ); } if ( foreground.isValid() ) charFormat.setForeground( foreground ); cellCursor.setCharFormat( charFormat ); if ( hasIcon && !iconAfterText ) { cellCursor.insertText( QChar::fromLatin1( ' ' ) ); // spacing between icon and text } //qDebug() << cellText; if (cellText.startsWith(QLatin1String("<qt>")) || cellText.startsWith(QLatin1String("<html>"))) cellCursor.insertHtml( cellText ); else cellCursor.insertText( cellText ); if ( hasIcon && iconAfterText ) { cellCursor.insertText( QChar::fromLatin1( ' ' ) ); // spacing between icon and text insertDecoration( builder, textDoc ); } if ( span.width() > 1 || span.height() > 1 ) textTable->mergeCells( cell.row(), cell.column(), span.height(), span.width() ); }
void MessageAction::featureUpdate() { QTextTableCell cell = textTable->cellAt(0,3); QTextTableCellFormat format; if (!isProcessed) format.setBackground(QColor(Qt::red)); else format.setBackground(QColor(Qt::white)); cell.setFormat(format); }
void PrintDialog::fillCategoryTableLine( QTextTable * table, const unsigned int & row, const QString & position, const QString & lastname, const QString & firstname, const QString & time) { int realRow = row + 1; table->insertRows ( realRow, 1 ); QTextCharFormat cellFormat; cellFormat.setFontPointSize(10.0); if(row%2) cellFormat.setBackground(QBrush(QColor(240,240,240))); QTextTableCell cell = table->cellAt(realRow, 0); QTextCursor cursor = cell.firstCursorPosition(); cell.setFormat(cellFormat); cursor.insertText(position); cell = table->cellAt(realRow, 1); cursor = cell.firstCursorPosition(); cell.setFormat(cellFormat); cursor.insertText(lastname); cell = table->cellAt(realRow, 2); cursor = cell.firstCursorPosition(); cell.setFormat(cellFormat); cursor.insertText(firstname); cell = table->cellAt(realRow, 3); cursor = cell.firstCursorPosition(); cell.setFormat(cellFormat); cursor.insertText(time); }
int KTextDocumentLayout::hitTestIterated(QTextFrame::iterator begin, QTextFrame::iterator end, const QPointF &point, Qt::HitTestAccuracy accuracy) const { int position = -1; QTextFrame::iterator it = begin; for (it = begin; it != end; ++it) { QTextBlock block = it.currentBlock(); QTextTable *table = qobject_cast<QTextTable*>(it.currentFrame()); QTextFrame *subFrame = it.currentFrame(); if (table) { QTextTableCell cell = m_state->hitTestTable(table, point); if (cell.isValid()) { position = hitTestIterated(cell.begin(), cell.end(), point, accuracy); if (position == -1) position = cell.lastPosition(); return position; } continue; } else if (subFrame) { position = hitTestIterated(subFrame->begin(), subFrame->end(), point, accuracy); if (position != -1) return position; continue; } else { if (!block.isValid()) continue; } // kDebug(32500) <<"hitTest[" << point.x() <<"," << point.y() <<"]"; QTextLayout *layout = block.layout(); if (point.y() > layout->boundingRect().bottom()) { // just skip this block. position = block.position() + block.length() - 1; continue; } for (int i = 0; i < layout->lineCount(); i++) { QTextLine line = layout->lineAt(i); // kDebug(32500) <<" + line[" << line.textStart() <<"]:" << line.y() <<"-" << line.height(); if (point.y() > line.y() + line.height()) { position = line.textStart() + line.textLength(); continue; } if (accuracy == Qt::ExactHit && point.y() < line.y()) // between lines return -1; if (accuracy == Qt::ExactHit && // left or right of line (point.x() < line.x() || point.x() > line.x() + line.width())) return -1; if (point.x() > line.width() && layout->textOption().textDirection() == Qt::RightToLeft) { // totally right of RTL text means the position is the start of the text. return block.position() + line.textStart(); } return block.position() + line.xToCursor(point.x()); } } return -1; }
void ChatDialog::printTimeInCell(QTextTable *table, time_t timestamp) { QTextCharFormat timeFormat; timeFormat.setForeground(Qt::gray); timeFormat.setFontUnderline(true); timeFormat.setUnderlineColor(Qt::gray); QTextTableCell timeCell = table->cellAt(0, 1); timeCell.setFormat(timeFormat); timeCell.firstCursorPosition().insertText(formatTime(timestamp)); }
void TextEditWidget::sl_InsertColumnAction_Triggered() { QTextTable* table = textField->textCursor().currentTable(); if (!table) { WARNING("Wrong button state"); return; } QTextTableCell currentCell = table->cellAt(textField->textCursor()); table->insertColumns(currentCell.column() + 1, 1); QTextTableCell cell = table->cellAt(0, currentCell.column() + 1); textField->setTextCursor(cell.firstCursorPosition()); }
/*! \fn QTextCursor QTextTable::rowStart(const QTextCursor &cursor) const Returns a cursor pointing to the start of the row that contains the given \a cursor. \sa rowEnd() */ QTextCursor QTextTable::rowStart(const QTextCursor &c) const { Q_D(const QTextTable); QTextTableCell cell = cellAt(c); if (!cell.isValid()) return QTextCursor(); int row = cell.row(); QTextDocumentPrivate *p = d->pieceTable; QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), d->grid[row*d->nCols]); return QTextCursor(p, it.position()); }
void testFindingTables() { // How to find all the tables in a QTextDocument? QTextDocument textDoc; QTextCursor c( &textDoc ); QTextTable* firstTable = c.insertTable( 2, 2 ); QTextTableCell bottomRight = firstTable->cellAt( 1, 1 ); QTextTable* secondTable = bottomRight.firstCursorPosition().insertTable( 3, 3 ); // a nested table c.movePosition( QTextCursor::End ); QTextTable* thirdTable = c.insertTable( 1, 1 ); thirdTable->firstCursorPosition().insertText( "in table" ); c.insertText( "Foo" ); QList<QTextTable *> origTables; origTables << firstTable << secondTable << thirdTable; // A generic and slow solution is // curs.currentTable() && !tablesFound.contains(curs.currentTable()) // for each cursor position. Surely there's better. // We could jump to currentFrame().lastCursorPosition() but then it would skip // nested tables. QTextDocument* clonedDoc = textDoc.clone(); QSet<QTextTable *> tablesFound; { QTextCursor curs(clonedDoc); while (!curs.atEnd()) { QTextTable* currentTable = curs.currentTable(); if ( currentTable && !tablesFound.contains(currentTable) ) { tablesFound.insert( currentTable ); } curs.movePosition( QTextCursor::NextCharacter ); } QCOMPARE( tablesFound.size(), 3 ); } // Let's do something else then, let's find them by cursor position QList<QTextTable *> tablesByPos; { // first test const int firstPos = firstTable->firstCursorPosition().position(); QTextCursor curs( clonedDoc ); curs.setPosition( firstPos ); QVERIFY( curs.currentTable() ); // generic loop. works this approach is in TextDocument::breakTables now. Q_FOREACH( QTextTable* origTable, origTables ) { QTextCursor curs( clonedDoc ); curs.setPosition( origTable->firstCursorPosition().position() ); tablesByPos.append( curs.currentTable() ); } QCOMPARE( tablesByPos.size(), 3 ); QCOMPARE( tablesByPos.toSet(), tablesFound ); }
void TextDocumentModel::fillTable(QTextTable *table, QStandardItem *parent) { for (int row = 0; row < table->rows(); ++row) { for (int col = 0; col < table->columns(); ++col) { QTextTableCell cell = table->cellAt(row, col); auto *item = new QStandardItem; item->setText(tr("Cell %1x%2").arg(row).arg(col)); appendRow(parent, item, cell.format()); for (auto it = cell.begin(); it != cell.end(); ++it) fillFrameIterator(it, item); } } }
/*! \fn QTextCursor QTextTable::rowEnd(const QTextCursor &cursor) const Returns a cursor pointing to the end of the row that contains the given \a cursor. \sa rowStart() */ QTextCursor QTextTable::rowEnd(const QTextCursor &c) const { Q_D(const QTextTable); QTextTableCell cell = cellAt(c); if (!cell.isValid()) return QTextCursor(); int row = cell.row() + 1; int fragment = row < d->nRows ? d->grid[row*d->nCols] : d->fragment_end; QTextDocumentPrivate *p = d->pieceTable; QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), fragment); return QTextCursor(p, it.position() - 1); }
QRectF KoTextLayoutTableArea::selectionBoundingBox(QTextCursor &cursor) const { int lastRow = d->endOfArea->row; if (d->lastRowHasSomething == false) { --lastRow; } if (lastRow < d->startOfArea->row) { return QRectF(); // empty } int firstRow = qMax(d->startOfArea->row, d->headerRows); QTextTableCell startTableCell = d->table->cellAt(cursor.selectionStart()); QTextTableCell endTableCell = d->table->cellAt(cursor.selectionEnd()); if (startTableCell == endTableCell) { if (startTableCell.row() < d->startOfArea->row || startTableCell.row() > lastRow) { return QRectF(); // cell is not in this area } KoTextLayoutArea *area = d->cellAreas[startTableCell.row()][startTableCell.column()]; Q_ASSERT(area); return area->selectionBoundingBox(cursor); } else { int selectionRow; int selectionColumn; int selectionRowSpan; int selectionColumnSpan; cursor.selectedTableCells(&selectionRow, &selectionRowSpan, &selectionColumn, &selectionColumnSpan); qreal top, bottom; if (selectionRow < d->headerRows) { top = d->headerRowPositions[selectionRow] + d->headerOffsetY; } else { top = d->rowPositions[qMin(qMax(firstRow, selectionRow), lastRow)]; } if (selectionRow + selectionRowSpan < d->headerRows) { bottom = d->headerRowPositions[selectionRow + selectionRowSpan] + d->headerOffsetY; } else { bottom = d->rowPositions[d->headerRows] + d->headerOffsetY; if (selectionRow + selectionRowSpan >= firstRow) { bottom = d->rowPositions[qMin(selectionRow + selectionRowSpan, lastRow + 1)]; } } return QRectF(d->columnPositions[selectionColumn], top, d->columnPositions[selectionColumn + selectionColumnSpan] - d->columnPositions[selectionColumn], bottom - top); } }
void XmlWriter::processTableCell(QDomElement &parent, const QTextTableCell &cell) { QDomElement element = document->createElement("cell"); element.setAttribute("row", cell.row()); element.setAttribute("column", cell.column()); QTextFrame::iterator it; for (it = cell.begin(); !(it.atEnd()); ++it) { QTextFrame *childFrame = it.currentFrame(); QTextBlock childBlock = it.currentBlock(); if (childFrame) processFrame(element, childFrame); else if (childBlock.isValid()) processBlock(element, childBlock); } parent.appendChild(element); }
void ChatView::appendMessage(const QString &sender, const QString &message) { QTextCursor cellCursor = table->cellAt(table->rows() - 1, 0).lastCursorPosition(); cellCursor.insertText(QDateTime::currentDateTime().toString("[hh:mm]")); QTextTableCell senderCell = table->cellAt(table->rows() - 1, 1); QTextCharFormat senderFormat; if (sender == ownName) { senderFormat.setFontWeight(QFont::Bold); senderFormat.setForeground(Qt::red); } else senderFormat.setForeground(Qt::blue); senderCell.setFormat(senderFormat); cellCursor = senderCell.lastCursorPosition(); cellCursor.insertText(sender); QTextTableCell messageCell = table->cellAt(table->rows() - 1, 2); QTextCharFormat messageFormat; if (sender.isEmpty()) messageFormat.setForeground(Qt::darkGreen); messageCell.setFormat(messageFormat); cellCursor = messageCell.lastCursorPosition(); cellCursor.insertText(message); table->appendRows(1); verticalScrollBar()->setValue(verticalScrollBar()->maximum()); }
void InsertTableColumnCommand::redo() { KoTableColumnAndRowStyleManager carsManager = KoTableColumnAndRowStyleManager::getManager(m_table); if (!m_first) { carsManager.insertColumns(m_column, 1, m_style); KUndo2Command::redo(); } else { m_first = false; QTextTableCell cell = m_table->cellAt(*m_textEditor->cursor()); m_column = cell.column() + (m_right ? 1 : 0); m_style = carsManager.columnStyle(cell.column()); m_table->insertColumns(m_column, 1); carsManager.insertColumns(m_column, 1, m_style); if (m_right && m_column == m_table->columns()-1) { // Copy the cell style. for the bottomright cell which Qt doesn't QTextTableCell cell = m_table->cellAt(m_table->rows()-1, m_column - 1); QTextCharFormat format = cell.format(); cell = m_table->cellAt(m_table->rows()-1, m_column); cell.setFormat(format); } if (m_changeId) { for (int i=0; i < m_table->rows(); i++) { QTextTableCellFormat cellFormat = m_table->cellAt(i, m_column).format().toTableCellFormat(); cellFormat.setProperty(KoCharacterStyle::ChangeTrackerId, m_changeId); m_table->cellAt(i, m_column).setFormat(cellFormat); } } } }
static PyObject *meth_QTextTableCell_setFormat(PyObject *sipSelf, PyObject *sipArgs) { PyObject *sipParseErr = NULL; { const QTextCharFormat* a0; QTextTableCell *sipCpp; if (sipParseArgs(&sipParseErr, sipArgs, "BJ9", &sipSelf, sipType_QTextTableCell, &sipCpp, sipType_QTextCharFormat, &a0)) { sipCpp->setFormat(*a0); Py_INCREF(Py_None); return Py_None; } } /* Raise an exception if the arguments couldn't be parsed. */ sipNoMethod(sipParseErr, sipName_QTextTableCell, sipName_setFormat, doc_QTextTableCell_setFormat); return NULL; }
void MainWindow::showTable() { QTextCursor cursor = editor->textCursor(); QTextTable *table = cursor.currentTable(); if (!table) return; QTableWidget *tableWidget = new QTableWidget(table->rows(), table->columns()); //! [9] for (int row = 0; row < table->rows(); ++row) { for (int column = 0; column < table->columns(); ++column) { QTextTableCell tableCell = table->cellAt(row, column); //! [9] QTextFrame::iterator it; QString text; for (it = tableCell.begin(); !(it.atEnd()); ++it) { QTextBlock childBlock = it.currentBlock(); if (childBlock.isValid()) text += childBlock.text(); } QTableWidgetItem *newItem = new QTableWidgetItem(text); tableWidget->setItem(row, column, newItem); /* //! [10] processTableCell(tableCell); //! [10] */ //! [11] } //! [11] //! [12] } //! [12] tableWidget->setWindowTitle(tr("Table Contents")); tableWidget->show(); }
void ChatDialog::appendControlMessage(const QString& nick, const QString& action, time_t timestamp) { QTextCharFormat nickFormat; nickFormat.setForeground(Qt::gray); nickFormat.setFontWeight(QFont::Bold); nickFormat.setFontUnderline(true); nickFormat.setUnderlineColor(Qt::gray); QTextCursor cursor(ui->textEdit->textCursor()); cursor.movePosition(QTextCursor::End); QTextTableFormat tableFormat; tableFormat.setBorder(0); QTextTable *table = cursor.insertTable(1, 2, tableFormat); QString controlMsg = QString("%1 %2 ").arg(nick).arg(action); QTextTableCell fromCell = table->cellAt(0, 0); fromCell.setFormat(nickFormat); fromCell.firstCursorPosition().insertText(controlMsg); printTimeInCell(table, timestamp); }
void TextEditWidget::sl_CreateTableAction_Triggered() { QTextTable* table = textField->textCursor().currentTable(); if (table) { return; } textField->textCursor().beginEditBlock(); table = textField->textCursor().insertTable(2, 2); QTextTableFormat format = table->format(); format.setCellSpacing(0); format.setCellPadding(3); format.setBorderStyle(QTextFrameFormat::BorderStyle_Solid); QTextLength tableWidth(QTextLength::PercentageLength, 50); format.setWidth(tableWidth); table->setFormat(format); textField->textCursor().endEditBlock(); QTextTableCell cell = table->cellAt(0, 0); textField->setTextCursor(cell.firstCursorPosition()); }
void DeleteTableColumnCommand::redo() { KoTableColumnAndRowStyleManager carsManager = KoTableColumnAndRowStyleManager::getManager(m_table); if (!m_first) { if (!m_changeId) { carsManager.removeColumns(m_selectionColumn, m_selectionColumnSpan); } KUndo2Command::redo(); } else { m_first = false; int selectionRow; int selectionRowSpan; if(m_textEditor->hasComplexSelection()) { m_textEditor->cursor()->selectedTableCells(&selectionRow, &selectionRowSpan, &m_selectionColumn, &m_selectionColumnSpan); } else { QTextTableCell cell = m_table->cellAt(*m_textEditor->cursor()); m_selectionColumn = cell.column(); m_selectionColumnSpan = 1; } if (!m_changeId) { m_table->removeColumns(m_selectionColumn, m_selectionColumnSpan); for (int i = m_selectionColumn; i < m_selectionColumn + m_selectionColumnSpan; ++i) { m_deletedStyles.append(carsManager.columnStyle(i)); } carsManager.removeColumns(m_selectionColumn, m_selectionColumnSpan); } else { for (int i=0; i < m_table->rows(); i++) { QTextTableCellFormat cellFormat = m_table->cellAt(i, m_selectionColumn).format().toTableCellFormat(); cellFormat.setProperty(KoCharacterStyle::ChangeTrackerId, m_changeId); m_table->cellAt(i, m_selectionColumn).setFormat(cellFormat); } } } }
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); }
void QTextOdfWriter::writeFrame(QXmlStreamWriter &writer, const QTextFrame *frame) { Q_ASSERT(frame); const QTextTable *table = qobject_cast<const QTextTable*> (frame); if (table) { // Start a table. writer.writeStartElement(tableNS, QString::fromLatin1("table")); writer.writeEmptyElement(tableNS, QString::fromLatin1("table-column")); writer.writeAttribute(tableNS, QString::fromLatin1("number-columns-repeated"), QString::number(table->columns())); } else if (frame->document() && frame->document()->rootFrame() != frame) { // start a section writer.writeStartElement(textNS, QString::fromLatin1("section")); } QTextFrame::iterator iterator = frame->begin(); QTextFrame *child = 0; int tableRow = -1; while (! iterator.atEnd()) { if (iterator.currentFrame() && child != iterator.currentFrame()) writeFrame(writer, iterator.currentFrame()); else { // no frame, its a block QTextBlock block = iterator.currentBlock(); if (table) { QTextTableCell cell = table->cellAt(block.position()); if (tableRow < cell.row()) { if (tableRow >= 0) writer.writeEndElement(); // close table row tableRow = cell.row(); writer.writeStartElement(tableNS, QString::fromLatin1("table-row")); } writer.writeStartElement(tableNS, QString::fromLatin1("table-cell")); if (cell.columnSpan() > 1) writer.writeAttribute(tableNS, QString::fromLatin1("number-columns-spanned"), QString::number(cell.columnSpan())); if (cell.rowSpan() > 1) writer.writeAttribute(tableNS, QString::fromLatin1("number-rows-spanned"), QString::number(cell.rowSpan())); if (cell.format().isTableCellFormat()) { writer.writeAttribute(tableNS, QString::fromLatin1("style-name"), QString::fromLatin1("T%1").arg(cell.tableCellFormatIndex())); } } writeBlock(writer, block); if (table) writer.writeEndElement(); // table-cell } child = iterator.currentFrame(); ++iterator; } if (tableRow >= 0) writer.writeEndElement(); // close table-row if (table || (frame->document() && frame->document()->rootFrame() != frame)) writer.writeEndElement(); // close table or section element }
void PrintView::createPage() const { QTextCursor cursor(ui->textEdit->textCursor()); QTextCharFormat format(cursor.charFormat()); format.setFontFamily("Times"); QTextCharFormat boldFormat = format; boldFormat.setFontWeight(QFont::Bold); unsigned int numOfTasks = numOfOpenTasks(); if(!numOfTasks) { cursor.insertText(tr("There are currently no open tasks."), format); return; } else { cursor.insertText(tr("Open tasks as of %1\n\n").arg(QDateTime::currentDateTime().toString()), format); } // sort tasks ascending according to their due dates QVector<Task*> allTasksCp = *allTasks; qSort(allTasksCp.begin(), allTasksCp.end(), TaskLessThan); QTextTableFormat tableFormat; tableFormat.setCellPadding(10.); tableFormat.setCellSpacing(0.); tableFormat.setHeaderRowCount(1); int rows = numOfTasks + 1/*allTasks.size()+1*/, columns = 6; QTextTable *table = cursor.insertTable(rows, columns, tableFormat); QTextTableCell cell; QTextCursor cellCursor; for (int column=0; column<columns; ++column) { cell = table->cellAt(0, column); cellCursor = cell.firstCursorPosition(); switch(column) { case 0: cellCursor.insertText(tr("Due date"), boldFormat); break; case 1: cellCursor.insertText(tr("Title"), boldFormat); break; case 2: cellCursor.insertText(tr("Description"), boldFormat); break; case 3: cellCursor.insertText(tr("Category"), boldFormat); break; case 4: cellCursor.insertText(tr("Location"), boldFormat); break; case 5: cellCursor.insertText(tr("Done"), boldFormat); break; } } int actualRow = 0; // as radio buttons they are exclusive, therefore we don't need a flag for the third 'all' option bool showWeek = ui->radioButton_week->isChecked(); bool showMonth = ui->radioButton_month->isChecked(); for (int row=0; row<allTasksCp.size(); ++row) { if(allTasksCp.at(row)->done()) { continue; } if(showWeek) { if(allTasksCp.at(row)->dueDate()>QDateTime::currentDateTime().addDays(7)) { continue; } } else if(showMonth) { if(allTasksCp.at(row)->dueDate()>QDateTime::currentDateTime().addDays(30)) { continue; } } for (int column=0; column<columns; ++column) { cell = table->cellAt(actualRow+1, column); cellCursor = cell.firstCursorPosition(); switch(column) { case 0: cellCursor.insertText(allTasksCp.at(row)->dueDate().toString(), format); break; case 1: cellCursor.insertText(allTasksCp.at(row)->title(), format); break; case 2: cellCursor.insertText(allTasksCp.at(row)->description(), format); break; case 3: cellCursor.insertText(allTasksCp.at(row)->category(), format); break; case 4: cellCursor.insertText(allTasksCp.at(row)->location(), format); break; } } ++actualRow; } }
//! [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 KoTextLoader::loadBody(const KoXmlElement &bodyElem, QTextCursor &cursor) { const QTextBlockFormat defaultBlockFormat = cursor.blockFormat(); const QTextCharFormat defaultCharFormat = cursor.charFormat(); const QTextDocument *document = cursor.block().document(); d->styleManager = KoTextDocument(document).styleManager(); Q_ASSERT(d->styleManager); d->changeTracker = KoTextDocument(document).changeTracker(); // if (!d->changeTracker) // d->changeTracker = dynamic_cast<KoChangeTracker *>(d->context.dataCenterMap().value("ChangeTracker")); // Q_ASSERT(d->changeTracker); kDebug(32500) << "text-style:" << KoTextDebug::textAttributes( cursor.blockCharFormat() ); #if 0 if ((document->isEmpty()) && (d->styleManager)) { QTextBlock block = cursor.block(); d->styleManager->defaultParagraphStyle()->applyStyle(block); } #endif startBody(KoXml::childNodesCount(bodyElem)); KoXmlElement tag; bool usedParagraph = false; // set to true if we found a tag that used the paragraph, indicating that the next round needs to start a new one. forEachElement(tag, bodyElem) { if (! tag.isNull()) { const QString localName = tag.localName(); if (tag.namespaceURI() == KoXmlNS::text) { if (usedParagraph) cursor.insertBlock(defaultBlockFormat, defaultCharFormat); usedParagraph = true; if (d->changeTracker && localName == "tracked-changes") { d->changeTracker->loadOdfChanges(tag); usedParagraph = false; } else if (d->changeTracker && localName == "change-start") { loadChangedRegion(tag, cursor); usedParagraph = false; } else if (d->changeTracker && localName == "change-end") { d->currentChangeId = 0; usedParagraph = false; } else if (localName == "p") { // text paragraph loadParagraph(tag, cursor); } else if (localName == "h") { // heading loadHeading(tag, cursor); } else if (localName == "unordered-list" || localName == "ordered-list" // OOo-1.1 || localName == "list" || localName == "numbered-paragraph") { // OASIS loadList(tag, cursor); } else if (localName == "section") { // Temporary support (###TODO) loadSection(tag, cursor); } else { KoVariable *var = KoVariableRegistry::instance()->createFromOdf(tag, d->context); if (var) { KoTextDocumentLayout *layout = dynamic_cast<KoTextDocumentLayout*>(cursor.block().document()->documentLayout()); if (layout) { KoInlineTextObjectManager *textObjectManager = layout->inlineTextObjectManager(); if (textObjectManager) { KoVariableManager *varManager = textObjectManager->variableManager(); if (varManager) { textObjectManager->insertInlineObject(cursor, var); } } } } else { usedParagraph = false; kWarning(32500) << "unhandled text:" << localName; } } } else if (tag.namespaceURI() == KoXmlNS::draw) { loadShape(tag, cursor); } else if (tag.namespaceURI() == KoXmlNS::table) { if (localName == "table") { loadTable(tag, cursor); } else { kWarning(32500) << "unhandled table:" << localName; } #if 0 // TODO commented out for now if (localName == "table") { cursor.insertText("\n"); cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, 1); QTextTable *tbl = cursor.insertTable(1, 1); int rows = 0; int columns = 0; kDebug(32500) << "Table inserted"; KoXmlElement tblTag; forEachElement(tblTag, tag) { if (! tblTag.isNull()) { const QString tblLocalName = tblTag.localName(); if (tblTag.namespaceURI() == KoXmlNS::table) { if (tblLocalName == "table-column") { // Do some parsing with the column, see §8.2.1, ODF 1.1 spec int repeatColumn = tblTag.attributeNS(KoXmlNS::table, "number-columns-repeated", "1").toInt(); columns = columns + repeatColumn; if (rows > 0) tbl->resize(rows, columns); else tbl->resize(1, columns); } else if (tblLocalName == "table-row") { // Lot of work to do here... rows++; if (columns > 0) tbl->resize(rows, columns); else tbl->resize(rows, 1); // Added a row int currentCell = 0; KoXmlElement rowTag; forEachElement(rowTag, tblTag) { if (!rowTag.isNull()) { const QString rowLocalName = rowTag.localName(); if (rowTag.namespaceURI() == KoXmlNS::table) { if (rowLocalName == "table-cell") { // Ok, it's a cell... const int currentRow = tbl->rows() - 1; QTextTableCell cell = tbl->cellAt(currentRow, currentCell); if (cell.isValid()) { cursor = cell.firstCursorPosition(); loadBody(context, rowTag, cursor); } else kDebug(32500) << "Invalid table-cell row=" << currentRow << " column=" << currentCell; currentCell++; } } } } } } } } cursor = tbl->lastCursorPosition(); cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 1); } else { kWarning(32500) << "KoTextLoader::loadBody unhandled table::" << localName; } #endif }
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 KDReports::AutoTableElement::build( ReportBuilder& builder ) const { if( !d->m_tableModel ) { return; } QTextDocument& textDoc = builder.currentDocument(); QTextCursor& textDocCursor = builder.cursor(); textDocCursor.beginEditBlock(); QTextTableFormat tableFormat; const int headerRowCount = d->m_horizontalHeaderVisible ? 1 : 0; const int headerColumnCount = d->m_verticalHeaderVisible ? 1 : 0; tableFormat.setHeaderRowCount( headerRowCount ); tableFormat.setProperty( KDReports::HeaderColumnsProperty, headerColumnCount ); tableFormat.setAlignment( textDocCursor.blockFormat().alignment() ); fillTableFormat( tableFormat, textDocCursor ); while (d->m_tableModel->canFetchMore(QModelIndex())) d->m_tableModel->fetchMore(QModelIndex()); const int rows = d->m_tableModel->rowCount(); const int columns = d->m_tableModel->columnCount(); QTextTable* textTable = textDocCursor.insertTable( rows + headerRowCount, columns + headerColumnCount, tableFormat ); QTextCharFormat tableHeaderFormat; tableHeaderFormat.setBackground( d->m_headerBackground ); //qDebug( "rows = %d, columns = %d", textTable->rows(), textTable->columns() ); if( d->m_horizontalHeaderVisible ) { for( int column = 0; column < columns; column++ ) { QTextTableCell cell = textTable->cellAt( 0, column + headerColumnCount ); Q_ASSERT( cell.isValid() ); cell.setFormat( tableHeaderFormat ); d->fillCellFromHeaderData( column, Qt::Horizontal, cell, textDoc, textTable, builder ); } } if( d->m_verticalHeaderVisible ) { for( int row = 0; row < rows; row++ ) { QTextTableCell cell = textTable->cellAt( row + headerRowCount, 0 ); Q_ASSERT( cell.isValid() ); cell.setFormat( tableHeaderFormat ); d->fillCellFromHeaderData( row, Qt::Vertical, cell, textDoc, textTable, builder ); } } QVector<QBitArray> coveredCells; coveredCells.resize(rows); for( int row = 0; row < rows; row++ ) coveredCells[row].resize(columns); // The normal data for( int row = 0; row < rows; row++ ) { for( int column = 0; column < columns; column++ ) { if (coveredCells[row].testBit(column)) continue; QTextTableCell cell = textTable->cellAt( row + headerRowCount, column + headerColumnCount ); Q_ASSERT( cell.isValid() ); const QSize span = d->fillTableCell( row, column, cell, textDoc, textTable, builder ); if (span.isValid()) { for (int r = row; r < row + span.height() && r < rows; ++r) { for (int c = column; c < column + span.width() && c < columns; ++c) { coveredCells[r].setBit(c); } } } } } textDocCursor.movePosition( QTextCursor::End ); textDocCursor.endEditBlock(); builder.currentDocumentData().registerAutoTable( textTable, this ); }
void QTextCopyHelper::copy() { if (cursor.hasComplexSelection()) { QTextTable *table = cursor.currentTable(); int row_start, col_start, num_rows, num_cols; cursor.selectedTableCells(&row_start, &num_rows, &col_start, &num_cols); QTextTableFormat tableFormat = table->format(); tableFormat.setColumns(num_cols); tableFormat.clearColumnWidthConstraints(); const int objectIndex = dst->formatCollection()->createObjectIndex(tableFormat); Q_ASSERT(row_start != -1); for (int r = row_start; r < row_start + num_rows; ++r) { for (int c = col_start; c < col_start + num_cols; ++c) { QTextTableCell cell = table->cellAt(r, c); const int rspan = cell.rowSpan(); const int cspan = cell.columnSpan(); if (rspan != 1) { int cr = cell.row(); if (cr != r) continue; } if (cspan != 1) { int cc = cell.column(); if (cc != c) continue; } // add the QTextBeginningOfFrame QTextCharFormat cellFormat = cell.format(); if (r + rspan >= row_start + num_rows) { cellFormat.setTableCellRowSpan(row_start + num_rows - r); } if (c + cspan >= col_start + num_cols) { cellFormat.setTableCellColumnSpan(col_start + num_cols - c); } const int charFormatIndex = convertFormatIndex(cellFormat, objectIndex); int blockIdx = -2; const int cellPos = cell.firstPosition(); QTextBlock block = src->blocksFind(cellPos); if (block.position() == cellPos) { blockIdx = convertFormatIndex(block.blockFormat()); } dst->insertBlock(QTextBeginningOfFrame, insertPos, blockIdx, charFormatIndex); ++insertPos; // nothing to add for empty cells if (cell.lastPosition() > cellPos) { // add the contents appendFragments(cellPos, cell.lastPosition()); } } } // add end of table int end = table->lastPosition(); appendFragment(end, end+1, objectIndex); } else { appendFragments(cursor.selectionStart(), cursor.selectionEnd()); } }