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;
77 QTextEdit *
const textEdit;
78 TableActionMenu *
const q;
79 bool richTextMode =
false;
82void TableActionMenuPrivate::_k_slotRemoveCellContents()
85 QTextTable *table = textEdit->textCursor().currentTable();
86 const QTextTableCell cell = table->
cellAt(textEdit->textCursor());
90 QTextCursor cursor = textEdit->textCursor();
100void TableActionMenuPrivate::_k_slotRemoveRowBelow()
103 QTextTable *table = textEdit->textCursor().currentTable();
105 const QTextTableCell cell = table->
cellAt(textEdit->textCursor());
106 if (cell.
row() < table->
rows() - 1) {
113void TableActionMenuPrivate::_k_slotRemoveRowAbove()
116 QTextTable *table = textEdit->textCursor().currentTable();
118 const QTextTableCell cell = table->
cellAt(textEdit->textCursor());
119 if (cell.
row() >= 1) {
126void TableActionMenuPrivate::_k_slotRemoveColumnBefore()
129 QTextTable *table = textEdit->textCursor().currentTable();
131 const QTextTableCell cell = table->
cellAt(textEdit->textCursor());
139void TableActionMenuPrivate::_k_slotRemoveColumnAfter()
142 QTextTable *table = textEdit->textCursor().currentTable();
144 const QTextTableCell cell = table->
cellAt(textEdit->textCursor());
152void TableActionMenuPrivate::_k_slotInsertRowBelow()
155 QTextTable *table = textEdit->textCursor().currentTable();
157 const QTextTableCell cell = table->
cellAt(textEdit->textCursor());
158 if (cell.
row() < table->
rows()) {
167void TableActionMenuPrivate::_k_slotInsertRowAbove()
170 QTextTable *table = textEdit->textCursor().currentTable();
172 const QTextTableCell cell = table->
cellAt(textEdit->textCursor());
178void TableActionMenuPrivate::_k_slotInsertColumnBefore()
181 QTextTable *table = textEdit->textCursor().currentTable();
183 const QTextTableCell cell = table->
cellAt(textEdit->textCursor());
189void TableActionMenuPrivate::_k_slotInsertColumnAfter()
192 QTextTable *table = textEdit->textCursor().currentTable();
194 const QTextTableCell cell = table->
cellAt(textEdit->textCursor());
204void TableActionMenuPrivate::_k_slotInsertTable()
207 QPointer<InsertTableDialog> dialog =
new InsertTableDialog(textEdit);
208 if (dialog->exec()) {
209 QTextCursor cursor = textEdit->textCursor();
210 QTextTableFormat tableFormat;
212 const int numberOfColumns(dialog->columns());
213 QList<QTextLength> constrains;
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);
224 QTextTable *table = cursor.
insertTable(dialog->rows(), numberOfColumns);
231void TableActionMenuPrivate::_k_slotMergeCell()
234 QTextTable *table = textEdit->textCursor().currentTable();
236 const QTextTableCell cell = table->
cellAt(textEdit->textCursor());
242void TableActionMenuPrivate::_k_slotMergeSelectedCells()
245 QTextTable *table = textEdit->textCursor().currentTable();
252void TableActionMenuPrivate::_k_slotTableFormat()
255 QTextTable *table = textEdit->textCursor().currentTable();
257 QPointer<TableFormatDialog> dialog =
new TableFormatDialog(textEdit);
258 const int numberOfColumn(table->
columns());
259 const int numberOfRow(table->
rows());
260 dialog->setColumns(numberOfColumn);
261 dialog->setRows(numberOfRow);
262 QTextTableFormat tableFormat = table->
format();
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);
286 QList<QTextLength> constrainsText;
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()
314 QTextTable *table = textEdit->textCursor().currentTable();
316 QTextTableCell cell = table->
cellAt(textEdit->textCursor());
317 QPointer<TableCellFormatDialog> dialog =
new TableCellFormatDialog(textEdit);
323 if (dialog->exec()) {
324 if (dialog->useBackgroundColor()) {
325 const QColor tableCellColor = dialog->tableCellBackgroundColor();
326 if (tableCellColor.
isValid()) {
340void TableActionMenuPrivate::_k_slotSplitCell()
343 QTextTable *table = textEdit->textCursor().currentTable();
345 const QTextTableCell cell = table->
cellAt(textEdit->textCursor());
354void TableActionMenuPrivate::_k_updateActions(
bool forceUpdate)
356 if ((richTextMode) || forceUpdate) {
357 QTextTable *table = textEdit->textCursor().currentTable();
358 const bool isTable = (table !=
nullptr);
359 actionInsertRowBelow->setEnabled(isTable);
360 actionInsertRowAbove->setEnabled(isTable);
362 actionInsertColumnBefore->setEnabled(isTable);
363 actionInsertColumnAfter->setEnabled(isTable);
365 actionRemoveRowBelow->setEnabled(isTable);
366 actionRemoveRowAbove->setEnabled(isTable);
368 actionRemoveColumnBefore->setEnabled(isTable);
369 actionRemoveColumnAfter->setEnabled(isTable);
372 const QTextTableCell cell = table->
cellAt(textEdit->textCursor());
376 int firstColumn = -1;
378 textEdit->textCursor().selectedTableCells(&firstRow, &numRows, &firstColumn, &numColumns);
379 const bool hasSelectedTableCell = (firstRow != -1) && (numRows != -1) && (firstColumn != -1) && (numColumns != -1);
381 actionMergeCell->setEnabled(
false);
383 actionMergeCell->setEnabled(
true);
386 actionSplitCell->setEnabled(
true);
388 actionSplitCell->setEnabled(
false);
390 actionTableCellFormat->setEnabled(
true);
391 actionMergeSelectedCells->setEnabled(hasSelectedTableCell);
393 actionSplitCell->setEnabled(
false);
394 actionMergeCell->setEnabled(
false);
395 actionMergeSelectedCells->setEnabled(
false);
397 actionTableFormat->setEnabled(isTable);
398 actionTableCellFormat->setEnabled(isTable);
399 actionRemoveCellContents->setEnabled(isTable);
403TableActionMenu::TableActionMenu(QTextEdit *textEdit)
404 : KActionMenu(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 * insertTable(int rows, int columns)
bool movePosition(MoveOperation operation, MoveMode mode, int n)
int position() const const
void removeSelectedText()
void setPosition(int pos, MoveMode m)
void cursorPositionChanged()
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)