8#include "tableactionmenu.h"
11#include "inserttabledialog.h"
12#include "tablecellformatdialog.h"
13#include "tableformatdialog.h"
15#include <KLocalizedString>
25class TableActionMenuPrivate
28 explicit TableActionMenuPrivate(
QTextEdit *edit, TableActionMenu *qq)
34 void _k_slotInsertRowBelow();
35 void _k_slotInsertRowAbove();
36 void _k_slotInsertColumnBefore();
37 void _k_slotInsertColumnAfter();
39 void _k_slotInsertTable();
41 void _k_slotRemoveRowBelow();
42 void _k_slotRemoveRowAbove();
43 void _k_slotRemoveColumnBefore();
44 void _k_slotRemoveColumnAfter();
45 void _k_slotRemoveCellContents();
47 void _k_slotMergeCell();
48 void _k_slotMergeSelectedCells();
49 void _k_slotTableFormat();
50 void _k_slotTableCellFormat();
51 void _k_slotSplitCell();
52 void _k_updateActions(
bool forceUpdate =
false);
54 QAction *actionInsertTable =
nullptr;
56 QAction *actionInsertRowBelow =
nullptr;
57 QAction *actionInsertRowAbove =
nullptr;
59 QAction *actionInsertColumnBefore =
nullptr;
60 QAction *actionInsertColumnAfter =
nullptr;
62 QAction *actionRemoveRowBelow =
nullptr;
63 QAction *actionRemoveRowAbove =
nullptr;
65 QAction *actionRemoveColumnBefore =
nullptr;
66 QAction *actionRemoveColumnAfter =
nullptr;
68 QAction *actionMergeCell =
nullptr;
69 QAction *actionMergeSelectedCells =
nullptr;
70 QAction *actionSplitCell =
nullptr;
72 QAction *actionTableFormat =
nullptr;
73 QAction *actionTableCellFormat =
nullptr;
75 QAction *actionRemoveCellContents =
nullptr;
78 TableActionMenu *
const q;
79 bool richTextMode =
false;
82void TableActionMenuPrivate::_k_slotRemoveCellContents()
100void TableActionMenuPrivate::_k_slotRemoveRowBelow()
106 if (cell.
row() < table->
rows() - 1) {
113void TableActionMenuPrivate::_k_slotRemoveRowAbove()
119 if (cell.
row() >= 1) {
126void TableActionMenuPrivate::_k_slotRemoveColumnBefore()
139void TableActionMenuPrivate::_k_slotRemoveColumnAfter()
152void TableActionMenuPrivate::_k_slotInsertRowBelow()
158 if (cell.
row() < table->
rows()) {
167void TableActionMenuPrivate::_k_slotInsertRowAbove()
178void TableActionMenuPrivate::_k_slotInsertColumnBefore()
189void TableActionMenuPrivate::_k_slotInsertColumnAfter()
204void TableActionMenuPrivate::_k_slotInsertTable()
208 if (dialog->exec()) {
212 const int numberOfColumns(dialog->columns());
214 constrains.
reserve(numberOfColumns);
216 const int length = dialog->length();
218 const QTextLength textlength(type, length / numberOfColumns);
219 for (
int i = 0; i < numberOfColumns; ++i) {
220 constrains.
append(textlength);
231void TableActionMenuPrivate::_k_slotMergeCell()
242void TableActionMenuPrivate::_k_slotMergeSelectedCells()
252void TableActionMenuPrivate::_k_slotTableFormat()
258 const int numberOfColumn(table->
columns());
259 const int numberOfRow(table->
rows());
260 dialog->setColumns(numberOfColumn);
261 dialog->setRows(numberOfRow);
263 dialog->setBorder(tableFormat.
border());
266 dialog->setAlignment(tableFormat.
alignment());
272 dialog->setTypeOfLength(constrains.
at(0).type());
273 dialog->setLength(constrains.
at(0).rawValue() * numberOfColumn);
276 if (dialog->exec()) {
277 const int newNumberOfColumns(dialog->columns());
278 if ((newNumberOfColumns != numberOfColumn) || (dialog->rows() != numberOfRow)) {
279 table->
resize(dialog->rows(), newNumberOfColumns);
287 constrainsText.
reserve(newNumberOfColumns);
289 const int length = dialog->length();
291 const QTextLength textlength(type, length / newNumberOfColumns);
292 for (
int i = 0; i < newNumberOfColumns; ++i) {
293 constrainsText.
append(textlength);
296 const QColor tableBackgroundColor = dialog->tableBackgroundColor();
297 if (dialog->useBackgroundColor()) {
298 if (tableBackgroundColor.
isValid()) {
311void TableActionMenuPrivate::_k_slotTableCellFormat()
323 if (dialog->exec()) {
324 if (dialog->useBackgroundColor()) {
325 const QColor tableCellColor = dialog->tableCellBackgroundColor();
326 if (tableCellColor.
isValid()) {
340void TableActionMenuPrivate::_k_slotSplitCell()
354void TableActionMenuPrivate::_k_updateActions(
bool forceUpdate)
356 if ((richTextMode) || forceUpdate) {
358 const bool isTable = (table !=
nullptr);
362 actionInsertColumnBefore->
setEnabled(isTable);
368 actionRemoveColumnBefore->
setEnabled(isTable);
376 int firstColumn = -1;
379 const bool hasSelectedTableCell = (firstRow != -1) && (numRows != -1) && (firstColumn != -1) && (numColumns != -1);
391 actionMergeSelectedCells->
setEnabled(hasSelectedTableCell);
399 actionRemoveCellContents->
setEnabled(isTable);
403TableActionMenu::TableActionMenu(
QTextEdit *textEdit)
405 , d(new TableActionMenuPrivate(textEdit, this))
408 addAction(insertMenu);
411 d->actionInsertTable->setObjectName(
"insert_new_table"_L1);
412 insertMenu->addAction(d->actionInsertTable);
414 d->_k_slotInsertTable();
417 insertMenu->addSeparator();
419 insertMenu->addAction(d->actionInsertRowBelow);
420 d->actionInsertRowBelow->setObjectName(
"insert_row_below"_L1);
422 d->_k_slotInsertRowBelow();
426 insertMenu->addAction(d->actionInsertRowAbove);
427 d->actionInsertRowAbove->setObjectName(
"insert_row_above"_L1);
429 d->_k_slotInsertRowAbove();
432 insertMenu->addSeparator();
433 d->actionInsertColumnBefore =
new QAction(
QIcon::fromTheme(QStringLiteral(
"edit-table-insert-column-left")),
i18n(
"Column Before"),
this);
434 insertMenu->addAction(d->actionInsertColumnBefore);
435 d->actionInsertColumnBefore->setObjectName(
"insert_column_before"_L1);
438 d->_k_slotInsertColumnBefore();
441 d->actionInsertColumnAfter =
new QAction(
QIcon::fromTheme(QStringLiteral(
"edit-table-insert-column-right")),
i18n(
"Column After"),
this);
442 insertMenu->addAction(d->actionInsertColumnAfter);
443 d->actionInsertColumnAfter->setObjectName(
"insert_column_after"_L1);
445 d->_k_slotInsertColumnAfter();
449 addAction(removeMenu);
451 d->actionRemoveRowBelow =
new QAction(
i18nc(
"@action",
"Row Below"),
this);
452 removeMenu->addAction(d->actionRemoveRowBelow);
453 d->actionRemoveRowBelow->setObjectName(
"remove_row_below"_L1);
455 d->_k_slotRemoveRowBelow();
458 d->actionRemoveRowAbove =
new QAction(
i18nc(
"@action",
"Row Above"),
this);
459 removeMenu->addAction(d->actionRemoveRowAbove);
460 d->actionRemoveRowAbove->setObjectName(
"remove_row_above"_L1);
462 d->_k_slotRemoveRowAbove();
465 removeMenu->addSeparator();
466 d->actionRemoveColumnBefore =
new QAction(
i18nc(
"@action",
"Column Before"),
this);
467 removeMenu->addAction(d->actionRemoveColumnBefore);
468 d->actionRemoveColumnBefore->setObjectName(
"remove_column_before"_L1);
471 d->_k_slotRemoveColumnBefore();
474 d->actionRemoveColumnAfter =
new QAction(
i18nc(
"@action",
"Column After"),
this);
475 removeMenu->addAction(d->actionRemoveColumnAfter);
476 d->actionRemoveColumnAfter->setObjectName(
"remove_column_after"_L1);
478 d->_k_slotRemoveColumnAfter();
481 removeMenu->addSeparator();
482 d->actionRemoveCellContents =
new QAction(
i18nc(
"@action",
"Cell Contents"),
this);
483 removeMenu->addAction(d->actionRemoveCellContents);
484 d->actionRemoveCellContents->setObjectName(
"remove_cell_contents"_L1);
486 d->_k_slotRemoveCellContents();
491 d->actionMergeCell =
new QAction(
QIcon::fromTheme(QStringLiteral(
"edit-table-cell-merge")),
i18n(
"Join With Cell to the Right"),
this);
492 d->actionMergeCell->setObjectName(
"join_cell_to_the_right"_L1);
494 d->_k_slotMergeCell();
496 addAction(d->actionMergeCell);
498 d->actionMergeSelectedCells =
new QAction(
i18nc(
"@action",
"Join Selected Cells"),
this);
499 d->actionMergeSelectedCells->setObjectName(
"join_cell_selected_cells"_L1);
501 d->_k_slotMergeSelectedCells();
503 addAction(d->actionMergeSelectedCells);
506 d->actionSplitCell->setObjectName(
"split_cells"_L1);
508 d->_k_slotSplitCell();
510 addAction(d->actionSplitCell);
514 d->actionTableFormat =
new QAction(
i18nc(
"@action",
"Table Format…"),
this);
515 d->actionTableFormat->setObjectName(
"table_format"_L1);
517 d->_k_slotTableFormat();
519 addAction(d->actionTableFormat);
521 d->actionTableCellFormat =
new QAction(
i18nc(
"@action",
"Table Cell Format…"),
this);
522 d->actionTableCellFormat->setObjectName(
"table_cell_format"_L1);
524 d->_k_slotTableCellFormat();
526 addAction(d->actionTableCellFormat);
529 d->_k_updateActions(
false);
531 d->_k_updateActions(
true);
534TableActionMenu::~TableActionMenu() =
default;
536void TableActionMenu::setRichTextMode(
bool richTextMode)
538 d->richTextMode = richTextMode;
542#include "moc_tableactionmenu.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
void triggered(bool checked)
const QColor & color() const const
bool isValid() const const
QIcon fromTheme(const QString &name)
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
bool isEmpty() const const
void reserve(qsizetype size)
void setVerticalAlignment(VerticalAlignment alignment)
VerticalAlignment verticalAlignment() const const
QTextTable * currentTable() const const
QTextTable * insertTable(int rows, int columns)
bool movePosition(MoveOperation operation, MoveMode mode, int n)
int position() const const
void removeSelectedText()
void selectedTableCells(int *firstRow, int *numRows, int *firstColumn, int *numColumns) const const
void setPosition(int pos, MoveMode m)
void cursorPositionChanged()
QTextCursor textCursor() const const
QBrush background() const const
bool hasProperty(int propertyId) const const
void setBackground(const QBrush &brush)
QTextTableCellFormat toTableCellFormat() const const
qreal border() const const
void setBorder(qreal width)
void appendColumns(int count)
void appendRows(int count)
QTextTableCell cellAt(const QTextCursor &cursor) const const
int columns() const const
QTextTableFormat format() const const
void insertColumns(int index, int columns)
void insertRows(int index, int rows)
void mergeCells(const QTextCursor &cursor)
void removeColumns(int index, int columns)
void removeRows(int index, int rows)
void resize(int rows, int columns)
void setFormat(const QTextTableFormat &format)
void splitCell(int row, int column, int numRows, int numCols)
int columnSpan() const const
QTextCursor firstCursorPosition() const const
QTextCharFormat format() const const
bool isValid() const const
QTextCursor lastCursorPosition() const const
int rowSpan() const const
void setFormat(const QTextCharFormat &format)
Qt::Alignment alignment() const const
qreal cellPadding() const const
qreal cellSpacing() const const
QList< QTextLength > columnWidthConstraints() const const
void setAlignment(Qt::Alignment alignment)
void setCellPadding(qreal padding)
void setCellSpacing(qreal spacing)
void setColumnWidthConstraints(const QList< QTextLength > &constraints)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)