summaryrefslogtreecommitdiffstats
path: root/src/gui/text/qtexttable.cpp
blob: 683ad8ac35260a4f99eaefe6c61c848c4e00b427 (plain)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
// Copyright (C) 2016 The Qt Company Ltd.// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only#include"qtexttable.h"#include"qtextcursor.h"#include"qtextformat.h"#include <qdebug.h>#include"qtextcursor_p.h"#include"qtexttable_p.h"#include"qvarlengtharray.h"#include <algorithm>#include <stdlib.h> QT_BEGIN_NAMESPACE using namespaceQt::StringLiterals;/*! \class QTextTableCell \reentrant \brief The QTextTableCell class represents the properties of a cell in a QTextTable. \inmodule QtGui \ingroup richtext-processing Table cells are pieces of document structure that belong to a table. The table orders cells into particular rows and columns; cells can also span multiple columns and rows. Cells are usually created when a table is inserted into a document with QTextCursor::insertTable(), but they are also created and destroyed when a table is resized. Cells contain information about their location in a table; you can obtain the row() and column() numbers of a cell, and its rowSpan() and columnSpan(). The format() of a cell describes the default character format of its contents. The firstCursorPosition() and lastCursorPosition() functions are used to obtain the extent of the cell in the document. \sa QTextTable, QTextTableFormat*//*! \fn QTextTableCell::QTextTableCell() Constructs an invalid table cell. \sa isValid()*//*! \fn QTextTableCell::QTextTableCell(const QTextTableCell &other) Copy constructor. Creates a new QTextTableCell object based on the \a other cell.*//*! \fn QTextTableCell& QTextTableCell::operator=(const QTextTableCell &other) Assigns the \a other table cell to this table cell.*//*! \since 4.2 Sets the cell's character format to \a format. This can for example be used to change the background color of the entire cell: \code QTextTableCell cell = table->cellAt(2, 3); QTextCharFormat format = cell.format(); format.setBackground(Qt::blue); cell.setFormat(format); \endcode Note that the cell's row or column span cannot be changed through this function. You have to use QTextTable::mergeCells and QTextTable::splitCell instead. \sa format()*/voidQTextTableCell::setFormat(const QTextCharFormat &format){ QTextCharFormat fmt = format; fmt.clearProperty(QTextFormat::ObjectIndex); fmt.setObjectType(QTextFormat::TableCellObject); QTextDocumentPrivate *p =const_cast<QTextDocumentPrivate *>(QTextDocumentPrivate::get(table));QTextDocumentPrivate::FragmentIterator frag(&p->fragmentMap(), fragment); QTextFormatCollection *c = p->formatCollection(); QTextCharFormat oldFormat = c->charFormat(frag->format); fmt.setTableCellRowSpan(oldFormat.tableCellRowSpan()); fmt.setTableCellColumnSpan(oldFormat.tableCellColumnSpan()); p->setCharFormat(frag.position(),1, fmt,QTextDocumentPrivate::SetFormatAndPreserveObjectIndices);}/*! Returns the cell's character format.*/ QTextCharFormat QTextTableCell::format()const{const QTextDocumentPrivate *p =QTextDocumentPrivate::get(table);const QTextFormatCollection *c = p->formatCollection(); QTextCharFormat fmt = c->charFormat(tableCellFormatIndex()); fmt.setObjectType(QTextFormat::TableCellObject);return fmt;}/*! \since 4.5 Returns the index of the tableCell's format in the document's internal list of formats. \sa QTextDocument::allFormats()*/intQTextTableCell::tableCellFormatIndex()const{const QTextDocumentPrivate *p =QTextDocumentPrivate::get(table);returnQTextDocumentPrivate::FragmentIterator(&p->fragmentMap(), fragment)->format;}/*! Returns the number of the row in the table that contains this cell. \sa column()*/intQTextTableCell::row()const{const QTextTablePrivate *tp = table->d_func();if(tp->dirty) tp->update();int idx = tp->findCellIndex(fragment);if(idx == -1)return idx;return tp->cellIndices.at(idx) / tp->nCols;}/*! Returns the number of the column in the table that contains this cell. \sa row()*/intQTextTableCell::column()const{const QTextTablePrivate *tp = table->d_func();if(tp->dirty) tp->update();int idx = tp->findCellIndex(fragment);if(idx == -1)return idx;return tp->cellIndices.at(idx) % tp->nCols;}/*! Returns the number of rows this cell spans. The default is 1. \sa columnSpan()*/intQTextTableCell::rowSpan()const{returnformat().tableCellRowSpan();}/*! Returns the number of columns this cell spans. The default is 1. \sa rowSpan()*/intQTextTableCell::columnSpan()const{returnformat().tableCellColumnSpan();}/*! \fn bool QTextTableCell::isValid() const Returns \c true if this is a valid table cell; otherwise returns false.*//*! Returns the first valid cursor position in this cell. \sa lastCursorPosition()*/ QTextCursor QTextTableCell::firstCursorPosition()const{returnQTextCursorPrivate::fromPosition(table->d_func()->pieceTable,firstPosition());}/*! Returns the last valid cursor position in this cell. \sa firstCursorPosition()*/ QTextCursor QTextTableCell::lastCursorPosition()const{returnQTextCursorPrivate::fromPosition(table->d_func()->pieceTable,lastPosition());}/*! \internal Returns the first valid position in the document occupied by this cell.*/intQTextTableCell::firstPosition()const{const QTextDocumentPrivate *p =QTextDocumentPrivate::get(table);return p->fragmentMap().position(fragment) +1;}/*! \internal Returns the last valid position in the document occupied by this cell.*/intQTextTableCell::lastPosition()const{const QTextDocumentPrivate *p =QTextDocumentPrivate::get(table);const QTextTablePrivate *td = table->d_func();int index = table->d_func()->findCellIndex(fragment);int f;if(index != -1) f = td->cells.value(index +1, td->fragment_end);else f = td->fragment_end;return p->fragmentMap().position(f);}/*! Returns a frame iterator pointing to the beginning of the table's cell. \sa end()*/QTextFrame::iterator QTextTableCell::begin()const{const QTextDocumentPrivate *p =QTextDocumentPrivate::get(table);int b = p->blockMap().findNode(firstPosition());int e = p->blockMap().findNode(lastPosition()+1);returnQTextFrame::iterator(const_cast<QTextTable *>(table), b, b, e);}/*! Returns a frame iterator pointing to the end of the table's cell. \sa begin()*/QTextFrame::iterator QTextTableCell::end()const{const QTextDocumentPrivate *p =QTextDocumentPrivate::get(table);int b = p->blockMap().findNode(firstPosition());int e = p->blockMap().findNode(lastPosition()+1);returnQTextFrame::iterator(const_cast<QTextTable *>(table), e, b, e);}/*! \fn QTextCursor QTextTableCell::operator==(const QTextTableCell &other) const Returns \c true if this cell object and the \a other cell object describe the same cell; otherwise returns \c false.*//*! \fn QTextCursor QTextTableCell::operator!=(const QTextTableCell &other) const Returns \c true if this cell object and the \a other cell object describe different cells; otherwise returns \c false.*//*! \fn QTextTableCell::~QTextTableCell() Destroys the table cell.*/ QTextTable *QTextTablePrivate::createTable(QTextDocumentPrivate *pieceTable,int pos,int rows,int cols,const QTextTableFormat &tableFormat){ QTextTableFormat fmt = tableFormat; fmt.setColumns(cols); QTextTable *table = qobject_cast<QTextTable *>(pieceTable->createObject(fmt));Q_ASSERT(table); pieceTable->beginEditBlock();// qDebug("---> createTable: rows=%d, cols=%d at %d", rows, cols, pos);// add block after table QTextCharFormat charFmt; charFmt.setObjectIndex(table->objectIndex()); charFmt.setObjectType(QTextFormat::TableCellObject);int charIdx = pieceTable->formatCollection()->indexForFormat(charFmt);int cellIdx = pieceTable->formatCollection()->indexForFormat(QTextBlockFormat()); QTextTablePrivate *d = table->d_func(); d->blockFragmentUpdates =true; d->fragment_start = pieceTable->insertBlock(QTextBeginningOfFrame, pos, cellIdx, charIdx); d->cells.append(d->fragment_start);++pos;for(int i =1; i < rows*cols; ++i) { d->cells.append(pieceTable->insertBlock(QTextBeginningOfFrame, pos, cellIdx, charIdx));// qDebug(" addCell at %d", pos);++pos;} d->fragment_end = pieceTable->insertBlock(QTextEndOfFrame, pos, cellIdx, charIdx);// qDebug(" addEOR at %d", pos);++pos; d->blockFragmentUpdates =false; d->dirty =true; pieceTable->endEditBlock();return table;}struct QFragmentFindHelper {inlineQFragmentFindHelper(int _pos,constQTextDocumentPrivate::FragmentMap &map):pos(_pos),fragmentMap(map) {} uint pos;constQTextDocumentPrivate::FragmentMap &fragmentMap;};staticinlinebooloperator<(int fragment,const QFragmentFindHelper &helper){return helper.fragmentMap.position(fragment) < helper.pos;}staticinlinebooloperator<(const QFragmentFindHelper &helper,int fragment){return helper.pos < helper.fragmentMap.position(fragment);}intQTextTablePrivate::findCellIndex(int fragment)const{ QFragmentFindHelper helper(pieceTable->fragmentMap().position(fragment), pieceTable->fragmentMap());constauto it =std::lower_bound(cells.constBegin(), cells.constEnd(), helper);if((it == cells.constEnd()) || (helper < *it))return-1;return it - cells.constBegin();}voidQTextTablePrivate::fragmentAdded(QChar type, uint fragment){ dirty =true;if(blockFragmentUpdates)return;if(type == QTextBeginningOfFrame) {Q_ASSERT(cells.indexOf(int(fragment)) == -1);const uint pos = pieceTable->fragmentMap().position(fragment); QFragmentFindHelper helper(pos, pieceTable->fragmentMap());auto it =std::lower_bound(cells.begin(), cells.end(), helper); cells.insert(it, fragment);if(!fragment_start || pos < pieceTable->fragmentMap().position(fragment_start)) fragment_start = fragment;return;}QTextFramePrivate::fragmentAdded(type, fragment);}voidQTextTablePrivate::fragmentRemoved(QChar type, uint fragment){ dirty =true;if(blockFragmentUpdates)return;if(type == QTextBeginningOfFrame) {Q_ASSERT(cells.indexOf(int(fragment)) != -1); cells.removeAll(int(fragment));if(fragment_start == fragment && cells.size()) { fragment_start = cells.at(0);}if(fragment_start != fragment)return;}QTextFramePrivate::fragmentRemoved(type, fragment);}/*! /fn void QTextTablePrivate::update() const This function is usually called when the table is "dirty". It seems to update all kind of table information.*/voidQTextTablePrivate::update()const{Q_Q(const QTextTable); nCols = q->format().columns(); nRows = (cells.size() + nCols-1)/nCols;// qDebug(">>>> QTextTablePrivate::update, nRows=%d, nCols=%d", nRows, nCols); grid.assign(nRows * nCols,0); QTextDocumentPrivate *p = pieceTable; QTextFormatCollection *c = p->formatCollection(); cellIndices.resize(cells.size());int cell =0;for(int i =0; i < cells.size(); ++i) {int fragment = cells.at(i); QTextCharFormat fmt = c->charFormat(QTextDocumentPrivate::FragmentIterator(&p->fragmentMap(), fragment)->format);int rowspan = fmt.tableCellRowSpan();int colspan = fmt.tableCellColumnSpan();// skip taken cellswhile(cell < nRows*nCols && grid[cell])++cell;int r = cell/nCols;int c = cell%nCols; cellIndices[i] = cell;if(r + rowspan > nRows) { grid.resize((r + rowspan) * nCols,0); nRows = r + rowspan;}Q_ASSERT(c + colspan <= nCols);for(int ii =0; ii < rowspan; ++ii) {for(int jj =0; jj < colspan; ++jj) {Q_ASSERT(grid[(r+ii)*nCols + c+jj] ==0); grid[(r+ii)*nCols + c+jj] = fragment;// qDebug(" setting cell %d span=%d/%d at %d/%d", fragment, rowspan, colspan, r+ii, c+jj);}}}// qDebug("<<<< end: nRows=%d, nCols=%d", nRows, nCols); dirty =false;}/*! \class QTextTable \reentrant \brief The QTextTable class represents a table in a QTextDocument. \inmodule QtGui \ingroup richtext-processing A table is a group of cells ordered into rows and columns. Each table contains at least one row and one column. Each cell contains a block, and is surrounded by a frame. Tables are usually created and inserted into a document with the QTextCursor::insertTable() function. For example, we can insert a table with three rows and two columns at the current cursor position in an editor using the following lines of code: \snippet textdocument-tables/mainwindow.cpp 1 \codeline \snippet textdocument-tables/mainwindow.cpp 3 The table format is either defined when the table is created or changed later with setFormat(). The table currently being edited by the cursor is found with QTextCursor::currentTable(). This allows its format or dimensions to be changed after it has been inserted into a document. A table's size can be changed with resize(), or by using insertRows(), insertColumns(), removeRows(), or removeColumns(). Use cellAt() to retrieve table cells. The starting and ending positions of table rows can be found by moving a cursor within a table, and using the rowStart() and rowEnd() functions to obtain cursors at the start and end of each row. Rows and columns within a QTextTable can be merged and split using the mergeCells() and splitCell() functions. However, only cells that span multiple rows or columns can be split. (Merging or splitting does not increase or decrease the number of rows and columns.) Note that if you have merged multiple columns and rows into one cell, you will not be able to split the merged cell into new cells spanning over more than one row or column. To be able to split cells spanning over several rows and columns you need to do this over several iterations. \table 80% \row \li \inlineimage texttable-split.png Original Table \li Suppose we have a 2x3 table of names and addresses. To merge both columns in the first row we invoke mergeCells() with \a row = 0, \a column = 0, \a numRows = 1 and \a numColumns = 2. \snippet textdocument-texttable/main.cpp 0 \row \li \inlineimage texttable-merge.png \li This gives us the following table. To split the first row of the table back into two cells, we invoke the splitCell() function with \a numRows and \a numCols = 1. \snippet textdocument-texttable/main.cpp 1 \row \li \inlineimage texttable-split.png Split Table \li This results in the original table. \endtable \sa QTextTableFormat*//*! \internal */QTextTable::QTextTable(QTextDocument *doc):QTextFrame(*newQTextTablePrivate(doc), doc){}/*! \internalDestroys the table. */QTextTable::~QTextTable(){}/*! \fn QTextTableCell QTextTable::cellAt(int row, int column) const Returns the table cell at the given \a row and \a column in the table. \sa columns(), rows()*/ QTextTableCell QTextTable::cellAt(int row,int col)const{Q_D(const QTextTable);if(d->dirty) d->update();if(row <0|| row >= d->nRows || col <0|| col >= d->nCols)returnQTextTableCell();returnQTextTableCell(this, d->grid[row*d->nCols + col]);}/*! \overload Returns the table cell that contains the character at the given \a position in the document.*/ QTextTableCell QTextTable::cellAt(int position)const{Q_D(const QTextTable);if(d->dirty) d->update(); uint pos = (uint)position;constQTextDocumentPrivate::FragmentMap &map = d->pieceTable->fragmentMap();if(position <0|| map.position(d->fragment_start) >= pos || map.position(d->fragment_end) < pos)returnQTextTableCell(); QFragmentFindHelper helper(position, map);auto it =std::lower_bound(d->cells.begin(), d->cells.end(), helper);if(it != d->cells.begin())--it;returnQTextTableCell(this, *it);}/*! \fn QTextTableCell QTextTable::cellAt(const QTextCursor &cursor) const \overload Returns the table cell containing the given \a cursor.*/ QTextTableCell QTextTable::cellAt(const QTextCursor &c)const{returncellAt(c.position());}/*! \fn void QTextTable::resize(int rows, int columns) Resizes the table to contain the required number of \a rows and \a columns. \sa insertRows(), insertColumns(), removeRows(), removeColumns()*/voidQTextTable::resize(int rows,int cols){Q_D(QTextTable);if(d->dirty) d->update();int nRows =this->rows();int nCols =this->columns();if(rows == nRows && cols == nCols)return; d->pieceTable->beginEditBlock();if(nCols < cols)insertColumns(nCols, cols - nCols);else if(nCols > cols)removeColumns(cols, nCols - cols);if(nRows < rows)insertRows(nRows, rows-nRows);else if(nRows > rows)removeRows(rows, nRows-rows); d->pieceTable->endEditBlock();}/*! \fn void QTextTable::insertRows(int index, int rows) Inserts a number of \a rows before the row with the specified \a index. \sa resize(), insertColumns(), removeRows(), removeColumns(), appendRows(), appendColumns()*/voidQTextTable::insertRows(int pos,int num){Q_D(QTextTable);if(num <=0)return;if(d->dirty) d->update();if(pos > d->nRows || pos <0) pos = d->nRows;// qDebug() << "-------- insertRows" << pos << num; QTextDocumentPrivate *p = d->pieceTable; QTextFormatCollection *c = p->formatCollection(); p->beginEditBlock();int extended =0;int insert_before =0;if(pos >0&& pos < d->nRows) {int lastCell = -1;for(int i =0; i < d->nCols; ++i) {int cell = d->grid[pos*d->nCols + i];if(cell == d->grid[(pos-1)*d->nCols+i]) {// cell spans the insertion place, extend itif(cell != lastCell) {QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), cell); QTextCharFormat fmt = c->charFormat(it->format); fmt.setTableCellRowSpan(fmt.tableCellRowSpan() + num); p->setCharFormat(it.position(),1, fmt);} extended++;}else if(!insert_before) { insert_before = cell;} lastCell = cell;}}else{ insert_before = (pos ==0? d->grid[0] : d->fragment_end);}if(extended < d->nCols) {Q_ASSERT(insert_before);QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), insert_before); QTextCharFormat fmt = c->charFormat(it->format); fmt.setTableCellRowSpan(1); fmt.setTableCellColumnSpan(1);Q_ASSERT(fmt.objectIndex() ==objectIndex());int pos = it.position();int cfmt = p->formatCollection()->indexForFormat(fmt);int bfmt = p->formatCollection()->indexForFormat(QTextBlockFormat());// qDebug("inserting %d cells, nCols=%d extended=%d", num*(d->nCols-extended), d->nCols, extended);for(int i =0; i < num*(d->nCols-extended); ++i) p->insertBlock(QTextBeginningOfFrame, pos, bfmt, cfmt,QTextUndoCommand::MoveCursor);}// qDebug() << "-------- end insertRows" << pos << num; p->endEditBlock();}/*! \fn void QTextTable::insertColumns(int index, int columns) Inserts a number of \a columns before the column with the specified \a index. \sa insertRows(), resize(), removeRows(), removeColumns(), appendRows(), appendColumns()*/voidQTextTable::insertColumns(int pos,int num){Q_D(QTextTable);if(num <=0)return;if(d->dirty) d->update();if(pos > d->nCols || pos <0) pos = d->nCols;// qDebug() << "-------- insertCols" << pos << num; QTextDocumentPrivate *p = d->pieceTable; QTextFormatCollection *c = p->formatCollection(); p->beginEditBlock(); QList<int> extendedSpans;for(int i =0; i < d->nRows; ++i) {int cell;if(i == d->nRows -1&& pos == d->nCols) { cell = d->fragment_end;}else{int logicalGridIndexBeforePosition = pos >0? d->findCellIndex(d->grid[i*d->nCols + pos -1]): -1;// Search for the logical insertion point by skipping past cells which are not the first// cell in a rowspan. This means any cell for which the logical grid index is// less than the logical cell index of the cell before the insertion.int logicalGridIndex;int gridArrayOffset = i*d->nCols + pos;do{ cell = d->grid[gridArrayOffset]; logicalGridIndex = d->findCellIndex(cell); gridArrayOffset++;}while(logicalGridIndex < logicalGridIndexBeforePosition && gridArrayOffset < d->nRows*d->nCols);if(logicalGridIndex < logicalGridIndexBeforePosition && gridArrayOffset == d->nRows*d->nCols) cell = d->fragment_end;}if(pos >0&& pos < d->nCols && cell == d->grid[i*d->nCols + pos -1]) {// cell spans the insertion place, extend itif(!extendedSpans.contains(cell)) {QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), cell); QTextCharFormat fmt = c->charFormat(it->format); fmt.setTableCellColumnSpan(fmt.tableCellColumnSpan() + num); p->setCharFormat(it.position(),1, fmt); d->dirty =true; extendedSpans << cell;}}else{/* If the next cell is spanned from the row above, we need to find the right position to insert to */if(i >0&& pos < d->nCols && cell == d->grid[(i-1) * d->nCols + pos]) {int gridIndex = i*d->nCols + pos;const int gridEnd = d->nRows * d->nCols -1;while(gridIndex < gridEnd && cell == d->grid[gridIndex]) {++gridIndex;}if(gridIndex == gridEnd) cell = d->fragment_end;else cell = d->grid[gridIndex];}QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), cell); QTextCharFormat fmt = c->charFormat(it->format); fmt.setTableCellRowSpan(1); fmt.setTableCellColumnSpan(1);Q_ASSERT(fmt.objectIndex() ==objectIndex());int position = it.position();int cfmt = p->formatCollection()->indexForFormat(fmt);int bfmt = p->formatCollection()->indexForFormat(QTextBlockFormat());for(int i =0; i < num; ++i) p->insertBlock(QTextBeginningOfFrame, position, bfmt, cfmt,QTextUndoCommand::MoveCursor);}} QTextTableFormat tfmt =format(); tfmt.setColumns(tfmt.columns()+num); QList<QTextLength> columnWidths = tfmt.columnWidthConstraints();if(! columnWidths.isEmpty()) {for(int i = num; i >0; --i) columnWidths.insert(pos, columnWidths.at(qMax(0, pos -1)));} tfmt.setColumnWidthConstraints(columnWidths);QTextObject::setFormat(tfmt);// qDebug() << "-------- end insertCols" << pos << num; p->endEditBlock();}/*! \since 4.5 Appends \a count rows at the bottom of the table. \sa insertColumns(), insertRows(), resize(), removeRows(), removeColumns(), appendColumns()*/voidQTextTable::appendRows(int count){insertRows(rows(), count);}/*! \since 4.5 Appends \a count columns at the right side of the table. \sa insertColumns(), insertRows(), resize(), removeRows(), removeColumns(), appendRows()*/voidQTextTable::appendColumns(int count){insertColumns(columns(), count);}/*! \fn void QTextTable::removeRows(int index, int rows) Removes a number of \a rows starting with the row at the specified \a index. \sa insertRows(), insertColumns(), resize(), removeColumns(), appendRows(), appendColumns()*/voidQTextTable::removeRows(int pos,int num){Q_D(QTextTable);// qDebug() << "-------- removeRows" << pos << num;if(num <=0|| pos <0)return;if(d->dirty) d->update();if(pos >= d->nRows)return;if(pos+num > d->nRows) num = d->nRows - pos; QTextDocumentPrivate *p = d->pieceTable; QTextFormatCollection *collection = p->formatCollection(); p->beginEditBlock();// delete whole table?if(pos ==0&& num == d->nRows) {const int pos = p->fragmentMap().position(d->fragment_start); p->remove(pos, p->fragmentMap().position(d->fragment_end) - pos +1); p->endEditBlock();return;} p->aboutToRemoveCell(cellAt(pos,0).firstPosition(),cellAt(pos + num -1, d->nCols -1).lastPosition()); QList<int> touchedCells;for(int r = pos; r < pos + num; ++r) {for(int c =0; c < d->nCols; ++c) {int cell = d->grid[r*d->nCols + c];if(touchedCells.contains(cell))continue; touchedCells << cell;QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), cell); QTextCharFormat fmt = collection->charFormat(it->format);int span = fmt.tableCellRowSpan();if(span >1) { fmt.setTableCellRowSpan(span -1); p->setCharFormat(it.position(),1, fmt);}else{// remove cellint index = d->cells.indexOf(cell) +1;int f_end = index < d->cells.size() ? d->cells.at(index) : d->fragment_end; p->remove(it.position(), p->fragmentMap().position(f_end) - it.position());}}} p->endEditBlock();// qDebug() << "-------- end removeRows" << pos << num;}/*! \fn void QTextTable::removeColumns(int index, int columns) Removes a number of \a columns starting with the column at the specified \a index. \sa insertRows(), insertColumns(), removeRows(), resize(), appendRows(), appendColumns()*/voidQTextTable::removeColumns(int pos,int num){Q_D(QTextTable);// qDebug() << "-------- removeCols" << pos << num;if(num <=0|| pos <0)return;if(d->dirty) d->update();if(pos >= d->nCols)return;if(pos + num > d->nCols) pos = d->nCols - num; QTextDocumentPrivate *p = d->pieceTable; QTextFormatCollection *collection = p->formatCollection(); p->beginEditBlock();// delete whole table?if(pos ==0&& num == d->nCols) {const int pos = p->fragmentMap().position(d->fragment_start); p->remove(pos, p->fragmentMap().position(d->fragment_end) - pos +1); p->endEditBlock();return;} p->aboutToRemoveCell(cellAt(0, pos).firstPosition(),cellAt(d->nRows -1, pos + num -1).lastPosition()); QList<int> touchedCells;for(int r =0; r < d->nRows; ++r) {for(int c = pos; c < pos + num; ++c) {int cell = d->grid[r*d->nCols + c];QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), cell); QTextCharFormat fmt = collection->charFormat(it->format);int span = fmt.tableCellColumnSpan();if(touchedCells.contains(cell) && span <=1)continue; touchedCells << cell;if(span >1) { fmt.setTableCellColumnSpan(span -1); p->setCharFormat(it.position(),1, fmt);}else{// remove cellint index = d->cells.indexOf(cell) +1;int f_end = index < d->cells.size() ? d->cells.at(index) : d->fragment_end; p->remove(it.position(), p->fragmentMap().position(f_end) - it.position());}}} QTextTableFormat tfmt =format(); tfmt.setColumns(tfmt.columns()-num); QList<QTextLength> columnWidths = tfmt.columnWidthConstraints();if(columnWidths.size() > pos) { columnWidths.remove(pos, num); tfmt.setColumnWidthConstraints(columnWidths);}QTextObject::setFormat(tfmt); p->endEditBlock();// qDebug() << "-------- end removeCols" << pos << num;}/*! \since 4.1 Merges the cell at the specified \a row and \a column with the adjacent cells into one cell. The new cell will span \a numRows rows and \a numCols columns. This method does nothing if \a numRows or \a numCols is less than the current number of rows or columns spanned by the cell. \sa splitCell()*/voidQTextTable::mergeCells(int row,int column,int numRows,int numCols){Q_D(QTextTable);if(d->dirty) d->update(); QTextDocumentPrivate *p = d->pieceTable; QTextFormatCollection *fc = p->formatCollection();const QTextTableCell cell =cellAt(row, column);if(!cell.isValid() || row != cell.row() || column != cell.column())return; QTextCharFormat fmt = cell.format();const int rowSpan = fmt.tableCellRowSpan();const int colSpan = fmt.tableCellColumnSpan(); numRows =qMin(numRows,rows() - cell.row()); numCols =qMin(numCols,columns() - cell.column());// nothing to merge?if(numRows < rowSpan || numCols < colSpan)return;// check the edges of the merge rect to make sure no cell spans the edgefor(int r = row; r < row + numRows; ++r) {if(cellAt(r, column) ==cellAt(r, column -1))return;if(cellAt(r, column + numCols) ==cellAt(r, column + numCols -1))return;}for(int c = column; c < column + numCols; ++c) {if(cellAt(row, c) ==cellAt(row -1, c))return;if(cellAt(row + numRows, c) ==cellAt(row + numRows -1, c))return;} p->beginEditBlock();const int origCellPosition = cell.firstPosition() -1;const int cellFragment = d->grid[row * d->nCols + column];// find the position at which to insert the contents of the merged cells QFragmentFindHelper helper(origCellPosition, p->fragmentMap());constauto begin = d->cells.cbegin();constauto it =std::lower_bound(begin, d->cells.cend(), helper);Q_ASSERT(it != d->cells.cend());Q_ASSERT(!(helper < *it));Q_ASSERT(*it == cellFragment);const int insertCellIndex = it - begin;int insertFragment = d->cells.value(insertCellIndex +1, d->fragment_end); uint insertPos = p->fragmentMap().position(insertFragment); d->blockFragmentUpdates =true;bool rowHasText = cell.firstCursorPosition().block().length();bool needsParagraph = rowHasText && colSpan == numCols;// find all cells that will be erased by the mergefor(int r = row; r < row + numRows; ++r) {int firstColumn = r < row + rowSpan ? column + colSpan : column;// don't recompute the cell index for the first rowint firstCellIndex = r == row ? insertCellIndex +1: -1;int cellIndex = firstCellIndex;for(int c = firstColumn; c < column + numCols; ++c) {const int fragment = d->grid[r * d->nCols + c];// already handled?if(fragment == cellFragment)continue;QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), fragment); uint pos = it.position();if(firstCellIndex == -1) { QFragmentFindHelper helper(pos, p->fragmentMap());constauto begin = d->cells.cbegin();constauto it =std::lower_bound(begin, d->cells.cend(), helper);Q_ASSERT(it != d->cells.cend());Q_ASSERT(!(helper < *it));Q_ASSERT(*it == fragment); firstCellIndex = cellIndex = it - begin;}++cellIndex; QTextCharFormat fmt = fc->charFormat(it->format);const int cellRowSpan = fmt.tableCellRowSpan();const int cellColSpan = fmt.tableCellColumnSpan();// update the grid for this cellfor(int i = r; i < r + cellRowSpan; ++i)for(int j = c; j < c + cellColSpan; ++j) d->grid[i * d->nCols + j] = cellFragment;// erase the cell marker p->remove(pos,1);const int nextFragment = d->cells.value(cellIndex, d->fragment_end);const uint nextPos = p->fragmentMap().position(nextFragment);Q_ASSERT(nextPos >= pos);// merge the contents of the cell (if not empty)if(nextPos > pos) {if(needsParagraph) { needsParagraph =false;QTextCursorPrivate::fromPosition(p, insertPos++).insertBlock(); p->move(pos +1, insertPos, nextPos - pos);}else if(rowHasText) {QTextCursorPrivate::fromPosition(p, insertPos++).insertText(" "_L1); p->move(pos +1, insertPos, nextPos - pos);}else{ p->move(pos, insertPos, nextPos - pos);} insertPos += nextPos - pos; rowHasText =true;}}if(rowHasText) { needsParagraph =true; rowHasText =false;}// erase cells from last rowif(firstCellIndex >=0) { d->cellIndices.remove(firstCellIndex, cellIndex - firstCellIndex); d->cells.erase(d->cells.begin() + firstCellIndex, d->cells.begin() + cellIndex);}} d->fragment_start = d->cells.constFirst(); fmt.setTableCellRowSpan(numRows); fmt.setTableCellColumnSpan(numCols); p->setCharFormat(origCellPosition,1, fmt); d->blockFragmentUpdates =false; d->dirty =false; p->endEditBlock();}/*! \overload \since 4.1 Merges the cells selected by the provided \a cursor. \sa splitCell()*/voidQTextTable::mergeCells(const QTextCursor &cursor){if(!cursor.hasComplexSelection())return;int firstRow, numRows, firstColumn, numColumns; cursor.selectedTableCells(&firstRow, &numRows, &firstColumn, &numColumns);mergeCells(firstRow, firstColumn, numRows, numColumns);}/*! \since 4.1 Splits the specified cell at \a row and \a column into an array of multiple cells with dimensions specified by \a numRows and \a numCols. \note It is only possible to split cells that span multiple rows or columns, such as rows that have been merged using mergeCells(). \sa mergeCells()*/voidQTextTable::splitCell(int row,int column,int numRows,int numCols){Q_D(QTextTable);if(d->dirty) d->update(); QTextDocumentPrivate *p = d->pieceTable; QTextFormatCollection *c = p->formatCollection();const QTextTableCell cell =cellAt(row, column);if(!cell.isValid())return; row = cell.row(); column = cell.column(); QTextCharFormat fmt = cell.format();const int rowSpan = fmt.tableCellRowSpan();const int colSpan = fmt.tableCellColumnSpan();// nothing to split?if(numRows > rowSpan || numCols > colSpan)return; p->beginEditBlock();const int origCellPosition = cell.firstPosition() -1; QVarLengthArray<int>rowPositions(rowSpan); rowPositions[0] = cell.lastPosition();for(int r = row +1; r < row + rowSpan; ++r) {// find the cell before which to insert the new cell markersint gridIndex = r * d->nCols + column;constauto begin = d->cellIndices.cbegin();constauto it =std::upper_bound(begin, d->cellIndices.cend(), gridIndex);int fragment = d->cells.value(it - begin, d->fragment_end); rowPositions[r - row] = p->fragmentMap().position(fragment);} fmt.setTableCellColumnSpan(1); fmt.setTableCellRowSpan(1);const int fmtIndex = c->indexForFormat(fmt);const int blockIndex = p->blockMap().find(cell.lastPosition())->format;int insertAdjustement =0;for(int i =0; i < numRows; ++i) {for(int c =0; c < colSpan - numCols; ++c) p->insertBlock(QTextBeginningOfFrame, rowPositions[i] + insertAdjustement + c, blockIndex, fmtIndex); insertAdjustement += colSpan - numCols;}for(int i = numRows; i < rowSpan; ++i) {for(int c =0; c < colSpan; ++c) p->insertBlock(QTextBeginningOfFrame, rowPositions[i] + insertAdjustement + c, blockIndex, fmtIndex); insertAdjustement += colSpan;} fmt.setTableCellRowSpan(numRows); fmt.setTableCellColumnSpan(numCols); p->setCharFormat(origCellPosition,1, fmt); p->endEditBlock();}/*! Returns the number of rows in the table. \sa columns()*/intQTextTable::rows()const{Q_D(const QTextTable);if(d->dirty) d->update();return d->nRows;}/*! Returns the number of columns in the table. \sa rows()*/intQTextTable::columns()const{Q_D(const QTextTable);if(d->dirty) d->update();return d->nCols;}/*! \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())returnQTextCursor();int row = cell.row(); QTextDocumentPrivate *p = d->pieceTable;QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), d->grid[row*d->nCols]);returnQTextCursorPrivate::fromPosition(p, it.position());}/*! \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())returnQTextCursor();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);returnQTextCursorPrivate::fromPosition(p, it.position() -1);}/*! \fn void QTextTable::setFormat(const QTextTableFormat &format) Sets the table's \a format. \sa format()*/voidQTextTable::setFormat(const QTextTableFormat &format){ QTextTableFormat fmt = format;// don't try to change the number of table columns from here fmt.setColumns(columns());QTextObject::setFormat(fmt);}/*! \fn QTextTableFormat QTextTable::format() const Returns the table's format. \sa setFormat()*/ QT_END_NAMESPACE #include"moc_qtexttable.cpp"
close