Qt 4.8
Public Functions | Friends | List of all members
QTextTable Class Reference

The QTextTable class represents a table in a QTextDocument. More...

#include <qtexttable.h>

Inheritance diagram for QTextTable:
QTextFrame QTextObject QObject

Public Functions

void appendColumns (int count)
 Appends count columns at the right side of the table. More...
 
void appendRows (int count)
 Appends count rows at the bottom of the table. More...
 
QTextTableCell cellAt (int row, int col) const
 Returns the table cell at the given row and column in the table. More...
 
QTextTableCell cellAt (int position) const
 Returns the table cell that contains the character at the given position in the document. More...
 
QTextTableCell cellAt (const QTextCursor &c) const
 Returns the table cell containing the given cursor. More...
 
int columns () const
 Returns the number of columns in the table. More...
 
QTextTableFormat format () const
 Returns the table's format. More...
 
void insertColumns (int pos, int num)
 Inserts a number of columns before the column with the specified index. More...
 
void insertRows (int pos, int num)
 Inserts a number of rows before the row with the specified index. More...
 
void mergeCells (int row, int col, int numRows, int numCols)
 Merges the cell at the specified row and column with the adjacent cells into one cell. More...
 
void mergeCells (const QTextCursor &cursor)
 Merges the cells selected by the provided cursor. More...
 
 QTextTable (QTextDocument *doc)
 
void removeColumns (int pos, int num)
 Removes a number of columns starting with the column at the specified index. More...
 
void removeRows (int pos, int num)
 Removes a number of rows starting with the row at the specified index. More...
 
void resize (int rows, int cols)
 Resizes the table to contain the required number of rows and columns. More...
 
QTextCursor rowEnd (const QTextCursor &c) const
 Returns a cursor pointing to the end of the row that contains the given cursor. More...
 
int rows () const
 Returns the number of rows in the table. More...
 
QTextCursor rowStart (const QTextCursor &c) const
 Returns a cursor pointing to the start of the row that contains the given cursor. More...
 
void setFormat (const QTextTableFormat &format)
 Sets the table's format. More...
 
void splitCell (int row, int col, int numRows, int numCols)
 Splits the specified cell at row and column into an array of multiple cells with dimensions specified by numRows and numCols. More...
 
 ~QTextTable ()
 
- Public Functions inherited from QTextFrame
iterator begin () const
 Returns an iterator pointing to the first document element inside the frame. More...
 
QList< QTextFrame * > childFrames () const
 Returns a (possibly empty) list of the frame's child frames. More...
 
iterator end () const
 Returns an iterator pointing to the position past the last document element inside the frame. More...
 
QTextCursor firstCursorPosition () const
 Returns the first cursor position inside the frame. More...
 
int firstPosition () const
 Returns the first document position inside the frame. More...
 
QTextFrameFormat frameFormat () const
 Returns the frame's format. More...
 
QTextCursor lastCursorPosition () const
 Returns the last cursor position inside the frame. More...
 
int lastPosition () const
 Returns the last document position inside the frame. More...
 
QTextFrameLayoutDatalayoutData () const
 
QTextFrameparentFrame () const
 Returns the frame's parent frame. More...
 
 QTextFrame (QTextDocument *doc)
 Creates a new empty frame for the text document. More...
 
void setFrameFormat (const QTextFrameFormat &format)
 Sets the frame's format. More...
 
void setLayoutData (QTextFrameLayoutData *data)
 
 ~QTextFrame ()
 Destroys the frame, and removes it from the document's layout. More...
 
- Public Functions inherited from QTextObject
QTextDocumentPrivatedocHandle () const
 
QTextDocumentdocument () const
 Returns the document this object belongs to. More...
 
QTextFormat format () const
 Returns the text object's format. More...
 
int formatIndex () const
 Returns the index of the object's format in the document's internal list of formats. More...
 
int objectIndex () const
 Returns the object index of this object. More...
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Friends

class QTextTableCell
 

Additional Inherited Members

- Public Types inherited from QTextFrame
typedef iterator Iterator
 Qt-style synonym for QTextFrame::iterator. More...
 
- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Functions inherited from QTextFrame
 QTextFrame (QTextFramePrivate &p, QTextDocument *doc)
 
- Protected Functions inherited from QTextObject
 QTextObject (QTextDocument *doc)
 Creates a new QTextObject for the given document. More...
 
 QTextObject (QTextObjectPrivate &p, QTextDocument *doc)
 
void setFormat (const QTextFormat &format)
 Sets the text object's format. More...
 
 ~QTextObject ()
 Destroys the text object. More...
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

The QTextTable class represents a table in a QTextDocument.

Note
This class or function is reentrant.

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:

QTextCursor cursor(editor->textCursor());
QTextTable *table = cursor.insertTable(rows, columns, tableFormat);

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.

texttable-split.png
Original Table
Suppose we have a 2x3 table of names and addresses. To merge both columns in the first row we invoke mergeCells() with row = 0, column = 0, numRows = 1 and numColumns = 2.
table->mergeCells(0, 0, 1, 2);
texttable-merge.png
This gives us the following table. To split the first row of the table back into two cells, we invoke the splitCell() function with numRows and numCols = 1.
table->splitCell(0, 0, 1, 1);
texttable-split.png
Split Table
This results in the original table.
See also
QTextTableFormat

Definition at line 103 of file qtexttable.h.

Constructors and Destructors

◆ QTextTable()

QTextTable::QTextTable ( QTextDocument doc)
explicit
Warning
This function is not part of the public interface.

Definition at line 606 of file qtexttable.cpp.

607  : QTextFrame(*new QTextTablePrivate(doc), doc)
608 {
609 }
QTextFrame(QTextDocument *doc)
Creates a new empty frame for the text document.

◆ ~QTextTable()

QTextTable::~QTextTable ( )
Warning
This function is not part of the public interface.

Destroys the table.

Definition at line 615 of file qtexttable.cpp.

616 {
617 }

Functions

◆ appendColumns()

void QTextTable::appendColumns ( int  count)

Appends count columns at the right side of the table.

Since
4.5
See also
insertColumns() insertRows() resize() removeRows() removeColumns() appendRows()

Definition at line 915 of file qtexttable.cpp.

916 {
917  insertColumns(columns(), count);
918 }
int columns() const
Returns the number of columns in the table.
void insertColumns(int pos, int num)
Inserts a number of columns before the column with the specified index.
Definition: qtexttable.cpp:797

◆ appendRows()

void QTextTable::appendRows ( int  count)

Appends count rows at the bottom of the table.

Since
4.5
See also
insertColumns() insertRows() resize() removeRows() removeColumns() appendColumns()

Definition at line 901 of file qtexttable.cpp.

902 {
903  insertRows(rows(), count);
904 }
void insertRows(int pos, int num)
Inserts a number of rows before the row with the specified index.
Definition: qtexttable.cpp:732
int rows() const
Returns the number of rows in the table.

◆ cellAt() [1/3]

QTextTableCell QTextTable::cellAt ( int  row,
int  column 
) const

◆ cellAt() [2/3]

QTextTableCell QTextTable::cellAt ( int  position) const

Returns the table cell that contains the character at the given position in the document.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 651 of file qtexttable.cpp.

652 {
653  Q_D(const QTextTable);
654  if (d->dirty)
655  d->update();
656 
657  uint pos = (uint)position;
658  const QTextDocumentPrivate::FragmentMap &map = d->pieceTable->fragmentMap();
659  if (position < 0 || map.position(d->fragment_start) >= pos || map.position(d->fragment_end) < pos)
660  return QTextTableCell();
661 
662  QFragmentFindHelper helper(position, map);
663  QList<int>::ConstIterator it = qLowerBound(d->cells.begin(), d->cells.end(), helper);
664  if (it != d->cells.begin())
665  --it;
666 
667  return QTextTableCell(this, *it);
668 }
double d
Definition: qnumeric_p.h:62
#define it(className, varName)
static qreal position(QGraphicsObject *item, QDeclarativeAnchorLine::AnchorLine anchorLine)
#define Q_D(Class)
Definition: qglobal.h:2482
unsigned int uint
Definition: qglobal.h:996
uint position(uint node, uint field=0) const
The QTextTable class represents a table in a QTextDocument.
Definition: qtexttable.h:103
Q_OUTOFLINE_TEMPLATE RandomAccessIterator qLowerBound(RandomAccessIterator begin, RandomAccessIterator end, const T &value)
Definition: qalgorithms.h:227
friend class QTextTableCell
Definition: qtexttable.h:138
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ cellAt() [3/3]

QTextTableCell QTextTable::cellAt ( const QTextCursor cursor) const

Returns the table cell containing the given cursor.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 680 of file qtexttable.cpp.

681 {
682  return cellAt(c.position());
683 }
unsigned char c[8]
Definition: qnumeric_p.h:62
QTextTableCell cellAt(int row, int col) const
Returns the table cell at the given row and column in the table.
Definition: qtexttable.cpp:630

◆ columns()

int QTextTable::columns ( ) const

Returns the number of columns in the table.

See also
rows()

Definition at line 1340 of file qtexttable.cpp.

Referenced by QTextCursorPrivate::aboutToRemoveCell(), appendColumns(), QTextDocumentLayoutPrivate::drawFrame(), QTextDocumentLayoutPrivate::drawTableCell(), QTextHtmlExporter::emitTable(), QTextControlPrivate::gotoNextTableCell(), QTextControlPrivate::gotoPreviousTableCell(), QTextDocumentLayoutPrivate::layoutTable(), mergeCells(), QTextCursorPrivate::movePosition(), resize(), setFormat(), and QTextOdfWriter::writeFrame().

1341 {
1342  Q_D(const QTextTable);
1343  if (d->dirty)
1344  d->update();
1345 
1346  return d->nCols;
1347 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QTextTable class represents a table in a QTextDocument.
Definition: qtexttable.h:103

◆ format()

QTextTableFormat QTextTable::format ( ) const
inline

Returns the table's format.

See also
setFormat()

Definition at line 133 of file qtexttable.h.

Referenced by QTextCopyHelper::copy(), QTextDocumentLayoutPrivate::drawFrame(), QTextDocumentLayoutPrivate::drawTableCell(), QTextHtmlExporter::emitTable(), insertColumns(), QTextDocumentLayoutPrivate::layoutFlow(), QTextDocumentLayoutPrivate::layoutTable(), removeColumns(), and setFormat().

133 { return QTextObject::format().toTableFormat(); }
QTextTableFormat toTableFormat() const
Returns this format as a table format.
QTextFormat format() const
Returns the text object&#39;s format.

◆ insertColumns()

void QTextTable::insertColumns ( int  index,
int  columns 
)

Inserts a number of columns before the column with the specified index.

See also
insertRows() resize() removeRows() removeColumns() appendRows() appendColumns()

Definition at line 797 of file qtexttable.cpp.

Referenced by appendColumns(), and resize().

798 {
799  Q_D(QTextTable);
800  if (num <= 0)
801  return;
802 
803  if (d->dirty)
804  d->update();
805 
806  if (pos > d->nCols || pos < 0)
807  pos = d->nCols;
808 
809 // qDebug() << "-------- insertCols" << pos << num;
810  QTextDocumentPrivate *p = d->pieceTable;
812  p->beginEditBlock();
813 
814  QList<int> extendedSpans;
815  for (int i = 0; i < d->nRows; ++i) {
816  int cell;
817  if (i == d->nRows - 1 && pos == d->nCols) {
818  cell = d->fragment_end;
819  } else {
820  int logicalGridIndexBeforePosition = pos > 0
821  ? d->findCellIndex(d->grid[i*d->nCols + pos - 1])
822  : -1;
823 
824  // Search for the logical insertion point by skipping past cells which are not the first
825  // cell in a rowspan. This means any cell for which the logical grid index is
826  // less than the logical cell index of the cell before the insertion.
827  int logicalGridIndex;
828  int gridArrayOffset = i*d->nCols + pos;
829  do {
830  cell = d->grid[gridArrayOffset];
831  logicalGridIndex = d->findCellIndex(cell);
832  gridArrayOffset++;
833  } while (logicalGridIndex < logicalGridIndexBeforePosition
834  && gridArrayOffset < d->nRows*d->nCols);
835 
836  if (logicalGridIndex < logicalGridIndexBeforePosition
837  && gridArrayOffset == d->nRows*d->nCols)
838  cell = d->fragment_end;
839  }
840 
841  if (pos > 0 && pos < d->nCols && cell == d->grid[i*d->nCols + pos - 1]) {
842  // cell spans the insertion place, extend it
843  if (!extendedSpans.contains(cell)) {
845  QTextCharFormat fmt = c->charFormat(it->format);
846  fmt.setTableCellColumnSpan(fmt.tableCellColumnSpan() + num);
847  p->setCharFormat(it.position(), 1, fmt);
848  d->dirty = true;
849  extendedSpans << cell;
850  }
851  } else {
852  /* If the next cell is spanned from the row above, we need to find the right position
853  to insert to */
854  if (i > 0 && pos < d->nCols && cell == d->grid[(i-1) * d->nCols + pos]) {
855  int gridIndex = i*d->nCols + pos;
856  const int gridEnd = d->nRows * d->nCols - 1;
857  while (gridIndex < gridEnd && cell == d->grid[gridIndex]) {
858  ++gridIndex;
859  }
860  if (gridIndex == gridEnd)
861  cell = d->fragment_end;
862  else
863  cell = d->grid[gridIndex];
864  }
866  QTextCharFormat fmt = c->charFormat(it->format);
867  fmt.setTableCellRowSpan(1);
868  fmt.setTableCellColumnSpan(1);
869  Q_ASSERT(fmt.objectIndex() == objectIndex());
870  int position = it.position();
871  int cfmt = p->formatCollection()->indexForFormat(fmt);
873  for (int i = 0; i < num; ++i)
875  }
876  }
877 
878  QTextTableFormat tfmt = format();
879  tfmt.setColumns(tfmt.columns()+num);
880  QVector<QTextLength> columnWidths = tfmt.columnWidthConstraints();
881  if (! columnWidths.isEmpty()) {
882  for (int i = num; i > 0; --i)
883  columnWidths.insert(pos, columnWidths[qMax(0, pos-1)]);
884  }
885  tfmt.setColumnWidthConstraints (columnWidths);
887 
888 // qDebug() << "-------- end insertCols" << pos << num;
889  p->endEditBlock();
890 }
double d
Definition: qnumeric_p.h:62
QTextCharFormat charFormat(int index) const
Definition: qtextformat_p.h:85
The QTextCharFormat class provides formatting information for characters in a QTextDocument.
Definition: qtextformat.h:372
unsigned char c[8]
Definition: qnumeric_p.h:62
int columns() const
Returns the number of columns specified by the table format.
Definition: qtextformat.h:849
#define it(className, varName)
static qreal position(QGraphicsObject *item, QDeclarativeAnchorLine::AnchorLine anchorLine)
#define QTextBeginningOfFrame
void setTableCellRowSpan(int tableCellRowSpan)
If this character format is applied to characters in a table cell, the cell will span tableCellRowSpa...
Definition: qtextformat.h:529
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
#define Q_D(Class)
Definition: qglobal.h:2482
int insertBlock(int pos, int blockFormat, int charFormat, QTextUndoCommand::Operation=QTextUndoCommand::MoveCursor)
FragmentMap::ConstIterator FragmentIterator
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
void setColumnWidthConstraints(const QVector< QTextLength > &constraints)
Sets the column width constraints for the table.
Definition: qtextformat.h:853
void setTableCellColumnSpan(int tableCellColumnSpan)
If this character format is applied to characters in a table cell, the cell will span tableCellColumn...
Definition: qtextformat.h:537
QBool contains(const T &t) const
Returns true if the list contains an occurrence of value; otherwise returns false.
Definition: qlist.h:880
QTextTableFormat format() const
Returns the table&#39;s format.
Definition: qtexttable.h:133
int objectIndex() const
Returns the object index of this object.
QTextFormatCollection * formatCollection()
The QTextTable class represents a table in a QTextDocument.
Definition: qtexttable.h:103
void setColumns(int columns)
Sets the number of columns required by the table format.
Definition: qtextformat.h:885
void insert(int i, const T &t)
Inserts value at index position i in the vector.
Definition: qvector.h:362
void setFormat(const QTextFormat &format)
Sets the text object&#39;s format.
The QTextBlockFormat class provides formatting information for blocks of text in a QTextDocument...
Definition: qtextformat.h:545
The QTextTableFormat class provides formatting information for tables in a QTextDocument.
Definition: qtextformat.h:842
bool isEmpty() const
Returns true if the vector has size 0; otherwise returns false.
Definition: qvector.h:139
void setCharFormat(int pos, int length, const QTextCharFormat &newFormat, FormatChangeMode mode=SetFormat)
const FragmentMap & fragmentMap() const
int indexForFormat(const QTextFormat &f)
QVector< QTextLength > columnWidthConstraints() const
Returns a list of constraints used by this table format to control the appearance of columns in a tab...
Definition: qtextformat.h:856

◆ insertRows()

void QTextTable::insertRows ( int  index,
int  rows 
)

Inserts a number of rows before the row with the specified index.

See also
resize() insertColumns() removeRows() removeColumns() appendRows() appendColumns()

Definition at line 732 of file qtexttable.cpp.

Referenced by appendRows(), QTextControlPrivate::gotoNextTableCell(), and resize().

733 {
734  Q_D(QTextTable);
735  if (num <= 0)
736  return;
737 
738  if (d->dirty)
739  d->update();
740 
741  if (pos > d->nRows || pos < 0)
742  pos = d->nRows;
743 
744 // qDebug() << "-------- insertRows" << pos << num;
745  QTextDocumentPrivate *p = d->pieceTable;
747  p->beginEditBlock();
748 
749  int extended = 0;
750  int insert_before = 0;
751  if (pos > 0 && pos < d->nRows) {
752  for (int i = 0; i < d->nCols; ++i) {
753  int cell = d->grid[pos*d->nCols + i];
754  if (cell == d->grid[(pos-1)*d->nCols+i]) {
755  // cell spans the insertion place, extend it
757  QTextCharFormat fmt = c->charFormat(it->format);
758  fmt.setTableCellRowSpan(fmt.tableCellRowSpan() + num);
759  p->setCharFormat(it.position(), 1, fmt);
760  extended++;
761  } else if (!insert_before) {
762  insert_before = cell;
763  }
764  }
765  } else {
766  insert_before = (pos == 0 ? d->grid[0] : d->fragment_end);
767  }
768  if (extended < d->nCols) {
769  Q_ASSERT(insert_before);
771  QTextCharFormat fmt = c->charFormat(it->format);
772  fmt.setTableCellRowSpan(1);
773  fmt.setTableCellColumnSpan(1);
774  Q_ASSERT(fmt.objectIndex() == objectIndex());
775  int pos = it.position();
776  int cfmt = p->formatCollection()->indexForFormat(fmt);
778 // qDebug("inserting %d cells, nCols=%d extended=%d", num*(d->nCols-extended), d->nCols, extended);
779  for (int i = 0; i < num*(d->nCols-extended); ++i)
781  }
782 
783 // qDebug() << "-------- end insertRows" << pos << num;
784  p->endEditBlock();
785 }
double d
Definition: qnumeric_p.h:62
QTextCharFormat charFormat(int index) const
Definition: qtextformat_p.h:85
The QTextCharFormat class provides formatting information for characters in a QTextDocument.
Definition: qtextformat.h:372
unsigned char c[8]
Definition: qnumeric_p.h:62
#define it(className, varName)
#define QTextBeginningOfFrame
void setTableCellRowSpan(int tableCellRowSpan)
If this character format is applied to characters in a table cell, the cell will span tableCellRowSpa...
Definition: qtextformat.h:529
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_D(Class)
Definition: qglobal.h:2482
int insertBlock(int pos, int blockFormat, int charFormat, QTextUndoCommand::Operation=QTextUndoCommand::MoveCursor)
FragmentMap::ConstIterator FragmentIterator
int objectIndex() const
Returns the object index of this object.
QTextFormatCollection * formatCollection()
The QTextTable class represents a table in a QTextDocument.
Definition: qtexttable.h:103
The QTextBlockFormat class provides formatting information for blocks of text in a QTextDocument...
Definition: qtextformat.h:545
void setCharFormat(int pos, int length, const QTextCharFormat &newFormat, FormatChangeMode mode=SetFormat)
const FragmentMap & fragmentMap() const
int indexForFormat(const QTextFormat &f)

◆ mergeCells() [1/2]

void QTextTable::mergeCells ( int  row,
int  column,
int  numRows,
int  numCols 
)

Merges the cell at the specified row and column with the adjacent cells into one cell.

Since
4.1

The new cell will span numRows rows and numCols columns. If numRows or numCols is less than the current number of rows or columns the cell spans then this method does nothing.

See also
splitCell()

Definition at line 1072 of file qtexttable.cpp.

Referenced by columns(), mergeCells(), and QTextHtmlImporter::scanTable().

1073 {
1074  Q_D(QTextTable);
1075 
1076  if (d->dirty)
1077  d->update();
1078 
1079  QTextDocumentPrivate *p = d->pieceTable;
1081 
1082  const QTextTableCell cell = cellAt(row, column);
1083  if (!cell.isValid() || row != cell.row() || column != cell.column())
1084  return;
1085 
1086  QTextCharFormat fmt = cell.format();
1087  const int rowSpan = fmt.tableCellRowSpan();
1088  const int colSpan = fmt.tableCellColumnSpan();
1089 
1090  numRows = qMin(numRows, rows() - cell.row());
1091  numCols = qMin(numCols, columns() - cell.column());
1092 
1093  // nothing to merge?
1094  if (numRows < rowSpan || numCols < colSpan)
1095  return;
1096 
1097  // check the edges of the merge rect to make sure no cell spans the edge
1098  for (int r = row; r < row + numRows; ++r) {
1099  if (cellAt(r, column) == cellAt(r, column - 1))
1100  return;
1101  if (cellAt(r, column + numCols) == cellAt(r, column + numCols - 1))
1102  return;
1103  }
1104 
1105  for (int c = column; c < column + numCols; ++c) {
1106  if (cellAt(row, c) == cellAt(row - 1, c))
1107  return;
1108  if (cellAt(row + numRows, c) == cellAt(row + numRows - 1, c))
1109  return;
1110  }
1111 
1112  p->beginEditBlock();
1113 
1114  const int origCellPosition = cell.firstPosition() - 1;
1115 
1116  const int cellFragment = d->grid[row * d->nCols + column];
1117 
1118  // find the position at which to insert the contents of the merged cells
1119  QFragmentFindHelper helper(origCellPosition, p->fragmentMap());
1120  QList<int>::Iterator it = qBinaryFind(d->cells.begin(), d->cells.end(), helper);
1121  Q_ASSERT(it != d->cells.end());
1122  Q_ASSERT(*it == cellFragment);
1123  const int insertCellIndex = it - d->cells.begin();
1124  int insertFragment = d->cells.value(insertCellIndex + 1, d->fragment_end);
1125  uint insertPos = p->fragmentMap().position(insertFragment);
1126 
1127  d->blockFragmentUpdates = true;
1128 
1129  bool rowHasText = cell.firstCursorPosition().block().length();
1130  bool needsParagraph = rowHasText && colSpan == numCols;
1131 
1132  // find all cells that will be erased by the merge
1133  for (int r = row; r < row + numRows; ++r) {
1134  int firstColumn = r < row + rowSpan ? column + colSpan : column;
1135 
1136  // don't recompute the cell index for the first row
1137  int firstCellIndex = r == row ? insertCellIndex + 1 : -1;
1138  int cellIndex = firstCellIndex;
1139 
1140  for (int c = firstColumn; c < column + numCols; ++c) {
1141  const int fragment = d->grid[r * d->nCols + c];
1142 
1143  // already handled?
1144  if (fragment == cellFragment)
1145  continue;
1146 
1148  uint pos = it.position();
1149 
1150  if (firstCellIndex == -1) {
1151  QFragmentFindHelper helper(pos, p->fragmentMap());
1152  QList<int>::Iterator it = qBinaryFind(d->cells.begin(), d->cells.end(), helper);
1153  Q_ASSERT(it != d->cells.end());
1154  Q_ASSERT(*it == fragment);
1155  firstCellIndex = cellIndex = it - d->cells.begin();
1156  }
1157 
1158  ++cellIndex;
1159 
1160  QTextCharFormat fmt = fc->charFormat(it->format);
1161 
1162  const int cellRowSpan = fmt.tableCellRowSpan();
1163  const int cellColSpan = fmt.tableCellColumnSpan();
1164 
1165  // update the grid for this cell
1166  for (int i = r; i < r + cellRowSpan; ++i)
1167  for (int j = c; j < c + cellColSpan; ++j)
1168  d->grid[i * d->nCols + j] = cellFragment;
1169 
1170  // erase the cell marker
1171  p->remove(pos, 1);
1172 
1173  const int nextFragment = d->cells.value(cellIndex, d->fragment_end);
1174  const uint nextPos = p->fragmentMap().position(nextFragment);
1175 
1176  Q_ASSERT(nextPos >= pos);
1177 
1178  // merge the contents of the cell (if not empty)
1179  if (nextPos > pos) {
1180  if (needsParagraph) {
1181  needsParagraph = false;
1182  QTextCursor(p, insertPos++).insertBlock();
1183  p->move(pos + 1, insertPos, nextPos - pos);
1184  } else if (rowHasText) {
1185  QTextCursor(p, insertPos++).insertText(QLatin1String(" "));
1186  p->move(pos + 1, insertPos, nextPos - pos);
1187  } else {
1188  p->move(pos, insertPos, nextPos - pos);
1189  }
1190 
1191  insertPos += nextPos - pos;
1192  rowHasText = true;
1193  }
1194  }
1195 
1196  if (rowHasText) {
1197  needsParagraph = true;
1198  rowHasText = false;
1199  }
1200 
1201  // erase cells from last row
1202  if (firstCellIndex >= 0) {
1203  d->cellIndices.remove(firstCellIndex, cellIndex - firstCellIndex);
1204  d->cells.erase(d->cells.begin() + firstCellIndex, d->cells.begin() + cellIndex);
1205  }
1206  }
1207 
1208  d->fragment_start = d->cells.first();
1209 
1210  fmt.setTableCellRowSpan(numRows);
1211  fmt.setTableCellColumnSpan(numCols);
1212  p->setCharFormat(origCellPosition, 1, fmt);
1213 
1214  d->blockFragmentUpdates = false;
1215  d->dirty = false;
1216 
1217  p->endEditBlock();
1218 }
int columns() const
Returns the number of columns in the table.
double d
Definition: qnumeric_p.h:62
QTextCharFormat charFormat(int index) const
Definition: qtextformat_p.h:85
The QTextCharFormat class provides formatting information for characters in a QTextDocument.
Definition: qtextformat.h:372
unsigned char c[8]
Definition: qnumeric_p.h:62
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
void remove(int pos, int length, QTextUndoCommand::Operation=QTextUndoCommand::MoveCursor)
#define it(className, varName)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
Q_OUTOFLINE_TEMPLATE RandomAccessIterator qBinaryFind(RandomAccessIterator begin, RandomAccessIterator end, const T &value)
Definition: qalgorithms.h:295
void setTableCellRowSpan(int tableCellRowSpan)
If this character format is applied to characters in a table cell, the cell will span tableCellRowSpa...
Definition: qtextformat.h:529
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_D(Class)
Definition: qglobal.h:2482
void insertText(const QString &text)
Inserts text at the current position, using the current character format.
FragmentMap::ConstIterator FragmentIterator
void setTableCellColumnSpan(int tableCellColumnSpan)
If this character format is applied to characters in a table cell, the cell will span tableCellColumn...
Definition: qtextformat.h:537
QTextTableCell cellAt(int row, int col) const
Returns the table cell at the given row and column in the table.
Definition: qtexttable.cpp:630
QTextCharFormat format() const
Returns the cell&#39;s character format.
Definition: qtexttable.cpp:153
int column() const
Returns the number of the column in the table that contains this cell.
Definition: qtexttable.cpp:201
The QTextCursor class offers an API to access and modify QTextDocuments.
Definition: qtextcursor.h:70
QTextFormatCollection * formatCollection()
unsigned int uint
Definition: qglobal.h:996
uint position(uint node, uint field=0) const
int row() const
Returns the number of the row in the table that contains this cell.
Definition: qtexttable.cpp:184
int length() const
Returns the length of the block in characters.
The QTextTable class represents a table in a QTextDocument.
Definition: qtexttable.h:103
QTextBlock block() const
Returns the block that contains the cursor.
int tableCellRowSpan() const
If this character format is applied to characters in a table cell, this function returns the number o...
Definition: qtextformat.h:518
void insertBlock()
Inserts a new empty block at the cursor position() with the current blockFormat() and charFormat()...
int firstPosition() const
Returns the first valid position in the document occupied by this cell.
Definition: qtexttable.cpp:273
int rows() const
Returns the number of rows in the table.
int tableCellColumnSpan() const
If this character format is applied to characters in a table cell, this function returns the number o...
Definition: qtextformat.h:521
The QTextTableCell class represents the properties of a cell in a QTextTable.
Definition: qtexttable.h:59
void move(int from, int to, int length, QTextUndoCommand::Operation=QTextUndoCommand::MoveCursor)
QTextCursor firstCursorPosition() const
Returns the first valid cursor position in this cell.
Definition: qtexttable.cpp:249
bool isValid() const
Returns true if this is a valid table cell; otherwise returns false.
Definition: qtexttable.h:77
void setCharFormat(int pos, int length, const QTextCharFormat &newFormat, FormatChangeMode mode=SetFormat)
const FragmentMap & fragmentMap() const
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ mergeCells() [2/2]

void QTextTable::mergeCells ( const QTextCursor cursor)

Merges the cells selected by the provided cursor.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
4.1
See also
splitCell()

Definition at line 1231 of file qtexttable.cpp.

1232 {
1233  if (!cursor.hasComplexSelection())
1234  return;
1235 
1236  int firstRow, numRows, firstColumn, numColumns;
1237  cursor.selectedTableCells(&firstRow, &numRows, &firstColumn, &numColumns);
1238  mergeCells(firstRow, firstColumn, numRows, numColumns);
1239 }
void mergeCells(int row, int col, int numRows, int numCols)
Merges the cell at the specified row and column with the adjacent cells into one cell.
void selectedTableCells(int *firstRow, int *numRows, int *firstColumn, int *numColumns) const
If the selection spans over table cells, firstRow is populated with the number of the first row in th...
bool hasComplexSelection() const
Returns true if the cursor contains a selection that is not simply a range from selectionStart() to s...

◆ removeColumns()

void QTextTable::removeColumns ( int  index,
int  columns 
)

Removes a number of columns starting with the column at the specified index.

See also
insertRows() insertColumns() removeRows() resize() appendRows() appendColumns()

Definition at line 995 of file qtexttable.cpp.

Referenced by resize().

996 {
997  Q_D(QTextTable);
998 // qDebug() << "-------- removeCols" << pos << num;
999 
1000  if (num <= 0 || pos < 0)
1001  return;
1002  if (d->dirty)
1003  d->update();
1004  if (pos >= d->nCols)
1005  return;
1006  if (pos + num > d->nCols)
1007  pos = d->nCols - num;
1008 
1009  QTextDocumentPrivate *p = d->pieceTable;
1010  QTextFormatCollection *collection = p->formatCollection();
1011  p->beginEditBlock();
1012 
1013  // delete whole table?
1014  if (pos == 0 && num == d->nCols) {
1015  const int pos = p->fragmentMap().position(d->fragment_start);
1016  p->remove(pos, p->fragmentMap().position(d->fragment_end) - pos + 1);
1017  p->endEditBlock();
1018  return;
1019  }
1020 
1021  p->aboutToRemoveCell(cellAt(0, pos).firstPosition(), cellAt(d->nRows - 1, pos + num - 1).lastPosition());
1022 
1023  QList<int> touchedCells;
1024  for (int r = 0; r < d->nRows; ++r) {
1025  for (int c = pos; c < pos + num; ++c) {
1026  int cell = d->grid[r*d->nCols + c];
1028  QTextCharFormat fmt = collection->charFormat(it->format);
1029  int span = fmt.tableCellColumnSpan();
1030  if (touchedCells.contains(cell) && span <= 1)
1031  continue;
1032  touchedCells << cell;
1033 
1034  if (span > 1) {
1035  fmt.setTableCellColumnSpan(span - 1);
1036  p->setCharFormat(it.position(), 1, fmt);
1037  } else {
1038  // remove cell
1039  int index = d->cells.indexOf(cell) + 1;
1040  int f_end = index < d->cells.size() ? d->cells.at(index) : d->fragment_end;
1041  p->remove(it.position(), p->fragmentMap().position(f_end) - it.position());
1042  }
1043  }
1044  }
1045 
1046  QTextTableFormat tfmt = format();
1047  tfmt.setColumns(tfmt.columns()-num);
1048  QVector<QTextLength> columnWidths = tfmt.columnWidthConstraints();
1049  if (columnWidths.count() > pos) {
1050  columnWidths.remove(pos, num);
1051  tfmt.setColumnWidthConstraints (columnWidths);
1052  }
1053  QTextObject::setFormat(tfmt);
1054 
1055  p->endEditBlock();
1056 // qDebug() << "-------- end removeCols" << pos << num;
1057 }
double d
Definition: qnumeric_p.h:62
QTextCharFormat charFormat(int index) const
Definition: qtextformat_p.h:85
The QTextCharFormat class provides formatting information for characters in a QTextDocument.
Definition: qtextformat.h:372
unsigned char c[8]
Definition: qnumeric_p.h:62
int columns() const
Returns the number of columns specified by the table format.
Definition: qtextformat.h:849
void remove(int i)
Removes the element at index position i.
Definition: qvector.h:374
void remove(int pos, int length, QTextUndoCommand::Operation=QTextUndoCommand::MoveCursor)
#define it(className, varName)
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
int firstPosition() const
Returns the first document position inside the frame.
#define Q_D(Class)
Definition: qglobal.h:2482
FragmentMap::ConstIterator FragmentIterator
void setColumnWidthConstraints(const QVector< QTextLength > &constraints)
Sets the column width constraints for the table.
Definition: qtextformat.h:853
QTextTableCell cellAt(int row, int col) const
Returns the table cell at the given row and column in the table.
Definition: qtexttable.cpp:630
QBool contains(const T &t) const
Returns true if the list contains an occurrence of value; otherwise returns false.
Definition: qlist.h:880
QTextTableFormat format() const
Returns the table&#39;s format.
Definition: qtexttable.h:133
QTextFormatCollection * formatCollection()
uint position(uint node, uint field=0) const
The QTextTable class represents a table in a QTextDocument.
Definition: qtexttable.h:103
void setColumns(int columns)
Sets the number of columns required by the table format.
Definition: qtextformat.h:885
void setFormat(const QTextFormat &format)
Sets the text object&#39;s format.
int tableCellColumnSpan() const
If this character format is applied to characters in a table cell, this function returns the number o...
Definition: qtextformat.h:521
int lastPosition() const
Returns the last valid position in the document occupied by this cell.
Definition: qtexttable.cpp:287
quint16 index
The QTextTableFormat class provides formatting information for tables in a QTextDocument.
Definition: qtextformat.h:842
void setCharFormat(int pos, int length, const QTextCharFormat &newFormat, FormatChangeMode mode=SetFormat)
const FragmentMap & fragmentMap() const
void aboutToRemoveCell(int cursorFrom, int cursorEnd)
This method is called from QTextTable when it is about to remove a table-cell to allow cursors to upd...
QVector< QTextLength > columnWidthConstraints() const
Returns a list of constraints used by this table format to control the appearance of columns in a tab...
Definition: qtextformat.h:856

◆ removeRows()

void QTextTable::removeRows ( int  index,
int  rows 
)

Removes a number of rows starting with the row at the specified index.

See also
insertRows(), insertColumns(), resize(), removeColumns() appendRows() appendColumns()

Definition at line 930 of file qtexttable.cpp.

Referenced by resize().

931 {
932  Q_D(QTextTable);
933 // qDebug() << "-------- removeRows" << pos << num;
934 
935  if (num <= 0 || pos < 0)
936  return;
937  if (d->dirty)
938  d->update();
939  if (pos >= d->nRows)
940  return;
941  if (pos+num > d->nRows)
942  num = d->nRows - pos;
943 
944  QTextDocumentPrivate *p = d->pieceTable;
945  QTextFormatCollection *collection = p->formatCollection();
946  p->beginEditBlock();
947 
948  // delete whole table?
949  if (pos == 0 && num == d->nRows) {
950  const int pos = p->fragmentMap().position(d->fragment_start);
951  p->remove(pos, p->fragmentMap().position(d->fragment_end) - pos + 1);
952  p->endEditBlock();
953  return;
954  }
955 
956  p->aboutToRemoveCell(cellAt(pos, 0).firstPosition(), cellAt(pos + num - 1, d->nCols - 1).lastPosition());
957 
958  QList<int> touchedCells;
959  for (int r = pos; r < pos + num; ++r) {
960  for (int c = 0; c < d->nCols; ++c) {
961  int cell = d->grid[r*d->nCols + c];
962  if (touchedCells.contains(cell))
963  continue;
964  touchedCells << cell;
966  QTextCharFormat fmt = collection->charFormat(it->format);
967  int span = fmt.tableCellRowSpan();
968  if (span > 1) {
969  fmt.setTableCellRowSpan(span - 1);
970  p->setCharFormat(it.position(), 1, fmt);
971  } else {
972  // remove cell
973  int index = d->cells.indexOf(cell) + 1;
974  int f_end = index < d->cells.size() ? d->cells.at(index) : d->fragment_end;
975  p->remove(it.position(), p->fragmentMap().position(f_end) - it.position());
976  }
977  }
978  }
979 
980  p->endEditBlock();
981 // qDebug() << "-------- end removeRows" << pos << num;
982 }
double d
Definition: qnumeric_p.h:62
QTextCharFormat charFormat(int index) const
Definition: qtextformat_p.h:85
The QTextCharFormat class provides formatting information for characters in a QTextDocument.
Definition: qtextformat.h:372
unsigned char c[8]
Definition: qnumeric_p.h:62
void remove(int pos, int length, QTextUndoCommand::Operation=QTextUndoCommand::MoveCursor)
#define it(className, varName)
int firstPosition() const
Returns the first document position inside the frame.
#define Q_D(Class)
Definition: qglobal.h:2482
FragmentMap::ConstIterator FragmentIterator
QTextTableCell cellAt(int row, int col) const
Returns the table cell at the given row and column in the table.
Definition: qtexttable.cpp:630
QBool contains(const T &t) const
Returns true if the list contains an occurrence of value; otherwise returns false.
Definition: qlist.h:880
QTextFormatCollection * formatCollection()
uint position(uint node, uint field=0) const
The QTextTable class represents a table in a QTextDocument.
Definition: qtexttable.h:103
int tableCellRowSpan() const
If this character format is applied to characters in a table cell, this function returns the number o...
Definition: qtextformat.h:518
int lastPosition() const
Returns the last valid position in the document occupied by this cell.
Definition: qtexttable.cpp:287
quint16 index
void setCharFormat(int pos, int length, const QTextCharFormat &newFormat, FormatChangeMode mode=SetFormat)
const FragmentMap & fragmentMap() const
void aboutToRemoveCell(int cursorFrom, int cursorEnd)
This method is called from QTextTable when it is about to remove a table-cell to allow cursors to upd...

◆ resize()

void QTextTable::resize ( int  rows,
int  columns 
)

Resizes the table to contain the required number of rows and columns.

See also
insertRows() insertColumns() removeRows() removeColumns()

Definition at line 695 of file qtexttable.cpp.

696 {
697  Q_D(QTextTable);
698  if (d->dirty)
699  d->update();
700 
701  int nRows = this->rows();
702  int nCols = this->columns();
703 
704  if (rows == nRows && cols == nCols)
705  return;
706 
707  d->pieceTable->beginEditBlock();
708 
709  if (nCols < cols)
710  insertColumns(nCols, cols - nCols);
711  else if (nCols > cols)
712  removeColumns(cols, nCols - cols);
713 
714  if (nRows < rows)
715  insertRows(nRows, rows-nRows);
716  else if (nRows > rows)
717  removeRows(rows, nRows-rows);
718 
719  d->pieceTable->endEditBlock();
720 }
int columns() const
Returns the number of columns in the table.
double d
Definition: qnumeric_p.h:62
void insertRows(int pos, int num)
Inserts a number of rows before the row with the specified index.
Definition: qtexttable.cpp:732
#define Q_D(Class)
Definition: qglobal.h:2482
void removeRows(int pos, int num)
Removes a number of rows starting with the row at the specified index.
Definition: qtexttable.cpp:930
void insertColumns(int pos, int num)
Inserts a number of columns before the column with the specified index.
Definition: qtexttable.cpp:797
void removeColumns(int pos, int num)
Removes a number of columns starting with the column at the specified index.
Definition: qtexttable.cpp:995
The QTextTable class represents a table in a QTextDocument.
Definition: qtexttable.h:103
int rows() const
Returns the number of rows in the table.

◆ rowEnd()

QTextCursor QTextTable::rowEnd ( const QTextCursor cursor) const

Returns a cursor pointing to the end of the row that contains the given cursor.

See also
rowStart()

Definition at line 1390 of file qtexttable.cpp.

1391 {
1392  Q_D(const QTextTable);
1393  QTextTableCell cell = cellAt(c);
1394  if (!cell.isValid())
1395  return QTextCursor();
1396 
1397  int row = cell.row() + 1;
1398  int fragment = row < d->nRows ? d->grid[row*d->nCols] : d->fragment_end;
1399  QTextDocumentPrivate *p = d->pieceTable;
1401  return QTextCursor(p, it.position() - 1);
1402 }
double d
Definition: qnumeric_p.h:62
unsigned char c[8]
Definition: qnumeric_p.h:62
#define it(className, varName)
#define Q_D(Class)
Definition: qglobal.h:2482
FragmentMap::ConstIterator FragmentIterator
QTextTableCell cellAt(int row, int col) const
Returns the table cell at the given row and column in the table.
Definition: qtexttable.cpp:630
The QTextCursor class offers an API to access and modify QTextDocuments.
Definition: qtextcursor.h:70
int row() const
Returns the number of the row in the table that contains this cell.
Definition: qtexttable.cpp:184
The QTextTable class represents a table in a QTextDocument.
Definition: qtexttable.h:103
The QTextTableCell class represents the properties of a cell in a QTextTable.
Definition: qtexttable.h:59
bool isValid() const
Returns true if this is a valid table cell; otherwise returns false.
Definition: qtexttable.h:77
const FragmentMap & fragmentMap() const

◆ rows()

int QTextTable::rows ( ) const

Returns the number of rows in the table.

See also
columns()

Definition at line 1326 of file qtexttable.cpp.

Referenced by QTextCursorPrivate::aboutToRemoveCell(), appendRows(), QTextDocumentLayoutPrivate::drawFrame(), QTextDocumentLayoutPrivate::drawTableCell(), QTextHtmlExporter::emitTable(), QTextControlPrivate::gotoNextTableCell(), QTextDocumentLayoutPrivate::layoutCell(), QTextDocumentLayoutPrivate::layoutTable(), mergeCells(), and resize().

1327 {
1328  Q_D(const QTextTable);
1329  if (d->dirty)
1330  d->update();
1331 
1332  return d->nRows;
1333 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QTextTable class represents a table in a QTextDocument.
Definition: qtexttable.h:103

◆ rowStart()

QTextCursor QTextTable::rowStart ( const QTextCursor cursor) const

Returns a cursor pointing to the start of the row that contains the given cursor.

See also
rowEnd()

Definition at line 1366 of file qtexttable.cpp.

1367 {
1368  Q_D(const QTextTable);
1369  QTextTableCell cell = cellAt(c);
1370  if (!cell.isValid())
1371  return QTextCursor();
1372 
1373  int row = cell.row();
1374  QTextDocumentPrivate *p = d->pieceTable;
1375  QTextDocumentPrivate::FragmentIterator it(&p->fragmentMap(), d->grid[row*d->nCols]);
1376  return QTextCursor(p, it.position());
1377 }
double d
Definition: qnumeric_p.h:62
unsigned char c[8]
Definition: qnumeric_p.h:62
#define it(className, varName)
#define Q_D(Class)
Definition: qglobal.h:2482
FragmentMap::ConstIterator FragmentIterator
QTextTableCell cellAt(int row, int col) const
Returns the table cell at the given row and column in the table.
Definition: qtexttable.cpp:630
The QTextCursor class offers an API to access and modify QTextDocuments.
Definition: qtextcursor.h:70
int row() const
Returns the number of the row in the table that contains this cell.
Definition: qtexttable.cpp:184
The QTextTable class represents a table in a QTextDocument.
Definition: qtexttable.h:103
The QTextTableCell class represents the properties of a cell in a QTextTable.
Definition: qtexttable.h:59
bool isValid() const
Returns true if this is a valid table cell; otherwise returns false.
Definition: qtexttable.h:77
const FragmentMap & fragmentMap() const

◆ setFormat()

void QTextTable::setFormat ( const QTextTableFormat format)

Sets the table's format.

See also
format()

Definition at line 1414 of file qtexttable.cpp.

1415 {
1416  QTextTableFormat fmt = format;
1417  // don't try to change the number of table columns from here
1418  fmt.setColumns(columns());
1420 }
int columns() const
Returns the number of columns in the table.
QTextTableFormat format() const
Returns the table&#39;s format.
Definition: qtexttable.h:133
void setColumns(int columns)
Sets the number of columns required by the table format.
Definition: qtextformat.h:885
void setFormat(const QTextFormat &format)
Sets the text object&#39;s format.
The QTextTableFormat class provides formatting information for tables in a QTextDocument.
Definition: qtextformat.h:842

◆ splitCell()

void QTextTable::splitCell ( int  row,
int  column,
int  numRows,
int  numCols 
)

Splits the specified cell at row and column into an array of multiple cells with dimensions specified by numRows and numCols.

Since
4.1
Note
It is only possible to split cells that span multiple rows or columns, such as rows that have been merged using mergeCells().
See also
mergeCells()

Definition at line 1255 of file qtexttable.cpp.

1256 {
1257  Q_D(QTextTable);
1258 
1259  if (d->dirty)
1260  d->update();
1261 
1262  QTextDocumentPrivate *p = d->pieceTable;
1264 
1265  const QTextTableCell cell = cellAt(row, column);
1266  if (!cell.isValid())
1267  return;
1268  row = cell.row();
1269  column = cell.column();
1270 
1271  QTextCharFormat fmt = cell.format();
1272  const int rowSpan = fmt.tableCellRowSpan();
1273  const int colSpan = fmt.tableCellColumnSpan();
1274 
1275  // nothing to split?
1276  if (numRows > rowSpan || numCols > colSpan)
1277  return;
1278 
1279  p->beginEditBlock();
1280 
1281  const int origCellPosition = cell.firstPosition() - 1;
1282 
1283  QVarLengthArray<int> rowPositions(rowSpan);
1284 
1285  rowPositions[0] = cell.lastPosition();
1286 
1287  for (int r = row + 1; r < row + rowSpan; ++r) {
1288  // find the cell before which to insert the new cell markers
1289  int gridIndex = r * d->nCols + column;
1290  QVector<int>::iterator it = qUpperBound(d->cellIndices.begin(), d->cellIndices.end(), gridIndex);
1291  int cellIndex = it - d->cellIndices.begin();
1292  int fragment = d->cells.value(cellIndex, d->fragment_end);
1293  rowPositions[r - row] = p->fragmentMap().position(fragment);
1294  }
1295 
1296  fmt.setTableCellColumnSpan(1);
1297  fmt.setTableCellRowSpan(1);
1298  const int fmtIndex = c->indexForFormat(fmt);
1299  const int blockIndex = p->blockMap().find(cell.lastPosition())->format;
1300 
1301  int insertAdjustement = 0;
1302  for (int i = 0; i < numRows; ++i) {
1303  for (int c = 0; c < colSpan - numCols; ++c)
1304  p->insertBlock(QTextBeginningOfFrame, rowPositions[i] + insertAdjustement + c, blockIndex, fmtIndex);
1305  insertAdjustement += colSpan - numCols;
1306  }
1307 
1308  for (int i = numRows; i < rowSpan; ++i) {
1309  for (int c = 0; c < colSpan; ++c)
1310  p->insertBlock(QTextBeginningOfFrame, rowPositions[i] + insertAdjustement + c, blockIndex, fmtIndex);
1311  insertAdjustement += colSpan;
1312  }
1313 
1314  fmt.setTableCellRowSpan(numRows);
1315  fmt.setTableCellColumnSpan(numCols);
1316  p->setCharFormat(origCellPosition, 1, fmt);
1317 
1318  p->endEditBlock();
1319 }
double d
Definition: qnumeric_p.h:62
Q_OUTOFLINE_TEMPLATE RandomAccessIterator qUpperBound(RandomAccessIterator begin, RandomAccessIterator end, const T &value)
Definition: qalgorithms.h:262
The QTextCharFormat class provides formatting information for characters in a QTextDocument.
Definition: qtextformat.h:372
unsigned char c[8]
Definition: qnumeric_p.h:62
#define it(className, varName)
#define QTextBeginningOfFrame
void setTableCellRowSpan(int tableCellRowSpan)
If this character format is applied to characters in a table cell, the cell will span tableCellRowSpa...
Definition: qtextformat.h:529
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
#define Q_D(Class)
Definition: qglobal.h:2482
int insertBlock(int pos, int blockFormat, int charFormat, QTextUndoCommand::Operation=QTextUndoCommand::MoveCursor)
void setTableCellColumnSpan(int tableCellColumnSpan)
If this character format is applied to characters in a table cell, the cell will span tableCellColumn...
Definition: qtextformat.h:537
QTextTableCell cellAt(int row, int col) const
Returns the table cell at the given row and column in the table.
Definition: qtexttable.cpp:630
QTextCharFormat format() const
Returns the cell&#39;s character format.
Definition: qtexttable.cpp:153
int column() const
Returns the number of the column in the table that contains this cell.
Definition: qtexttable.cpp:201
QTextFormatCollection * formatCollection()
uint position(uint node, uint field=0) const
int row() const
Returns the number of the row in the table that contains this cell.
Definition: qtexttable.cpp:184
The QTextTable class represents a table in a QTextDocument.
Definition: qtexttable.h:103
const BlockMap & blockMap() const
int tableCellRowSpan() const
If this character format is applied to characters in a table cell, this function returns the number o...
Definition: qtextformat.h:518
Iterator find(int k, uint field=0)
int firstPosition() const
Returns the first valid position in the document occupied by this cell.
Definition: qtexttable.cpp:273
int tableCellColumnSpan() const
If this character format is applied to characters in a table cell, this function returns the number o...
Definition: qtextformat.h:521
The QTextTableCell class represents the properties of a cell in a QTextTable.
Definition: qtexttable.h:59
int lastPosition() const
Returns the last valid position in the document occupied by this cell.
Definition: qtexttable.cpp:287
bool isValid() const
Returns true if this is a valid table cell; otherwise returns false.
Definition: qtexttable.h:77
void setCharFormat(int pos, int length, const QTextCharFormat &newFormat, FormatChangeMode mode=SetFormat)
const FragmentMap & fragmentMap() const
int indexForFormat(const QTextFormat &f)

Friends and Related Functions

◆ QTextTableCell

friend class QTextTableCell
friend

Definition at line 138 of file qtexttable.h.

Referenced by cellAt().


The documentation for this class was generated from the following files: