21 #include "tableactionmenu.h"
23 #include "inserttabledialog.h"
24 #include "tableformatdialog.h"
25 #include "tablecellformatdialog.h"
27 #include <KActionCollection>
30 #include <KLocalizedString>
35 namespace KPIMTextEdit {
37 class TableActionMenuPrivate
40 TableActionMenuPrivate( KActionCollection *ac, TextEdit *edit, TableActionMenu *qq )
41 : actionCollection( ac ), textEdit( edit ), q( qq )
45 void _k_slotInsertRowBelow();
46 void _k_slotInsertRowAbove();
47 void _k_slotInsertColumnBefore();
48 void _k_slotInsertColumnAfter();
50 void _k_slotInsertTable();
52 void _k_slotRemoveRowBelow();
53 void _k_slotRemoveRowAbove();
54 void _k_slotRemoveColumnBefore();
55 void _k_slotRemoveColumnAfter();
56 void _k_slotRemoveCellContents();
58 void _k_slotMergeCell();
59 void _k_slotMergeSelectedCells();
60 void _k_slotTableFormat();
61 void _k_slotTableCellFormat();
62 void _k_slotSplitCell();
63 void _k_updateActions(
bool forceUpdate =
false );
65 KAction *actionInsertTable;
67 KAction *actionInsertRowBelow;
68 KAction *actionInsertRowAbove;
70 KAction *actionInsertColumnBefore;
71 KAction *actionInsertColumnAfter;
73 KAction *actionRemoveRowBelow;
74 KAction *actionRemoveRowAbove;
76 KAction *actionRemoveColumnBefore;
77 KAction *actionRemoveColumnAfter;
79 KAction *actionMergeCell;
80 KAction *actionMergeSelectedCells;
81 KAction *actionSplitCell;
83 KAction *actionTableFormat;
84 KAction *actionTableCellFormat;
86 KAction *actionRemoveCellContents;
88 KActionCollection *actionCollection;
93 void TableActionMenuPrivate::_k_slotRemoveCellContents()
95 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
96 QTextTable *table = textEdit->textCursor().currentTable();
104 cursor.
movePosition( QTextCursor::NextCharacter, QTextCursor::KeepAnchor,
112 void TableActionMenuPrivate::_k_slotRemoveRowBelow()
114 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
115 QTextTable *table = textEdit->textCursor().currentTable();
118 if ( cell.
row()<table->
rows() - 1 ) {
125 void TableActionMenuPrivate::_k_slotRemoveRowAbove()
127 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
128 QTextTable *table = textEdit->textCursor().currentTable();
131 if ( cell.
row() >= 1 ) {
138 void TableActionMenuPrivate::_k_slotRemoveColumnBefore()
140 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
141 QTextTable *table = textEdit->textCursor().currentTable();
144 if ( cell.
column() > 0 ) {
151 void TableActionMenuPrivate::_k_slotRemoveColumnAfter()
153 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
154 QTextTable *table = textEdit->textCursor().currentTable();
164 void TableActionMenuPrivate::_k_slotInsertRowBelow()
166 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
167 QTextTable *table = textEdit->textCursor().currentTable();
170 if ( cell.
row()<table->
rows() ) {
179 void TableActionMenuPrivate::_k_slotInsertRowAbove()
181 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
182 QTextTable *table = textEdit->textCursor().currentTable();
190 void TableActionMenuPrivate::_k_slotInsertColumnBefore()
192 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
193 QTextTable *table = textEdit->textCursor().currentTable();
201 void TableActionMenuPrivate::_k_slotInsertColumnAfter()
203 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
204 QTextTable *table = textEdit->textCursor().currentTable();
216 void TableActionMenuPrivate::_k_slotInsertTable()
218 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
220 if ( dialog->exec() ) {
223 tableFormat.
setBorder( dialog->border() );
224 const int numberOfColumns( dialog->columns() );
226 const QTextLength::Type type = dialog->typeOfLength();
227 const int length = dialog->length();
229 const QTextLength textlength( type, length / numberOfColumns );
230 for (
int i = 0; i < numberOfColumns; ++i ) {
231 contrains.append( textlength );
242 void TableActionMenuPrivate::_k_slotMergeCell()
244 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
245 QTextTable *table = textEdit->textCursor().currentTable();
253 void TableActionMenuPrivate::_k_slotMergeSelectedCells()
255 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
256 QTextTable *table = textEdit->textCursor().currentTable();
263 void TableActionMenuPrivate::_k_slotTableFormat()
265 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
266 QTextTable *table = textEdit->textCursor().currentTable();
269 const int numberOfColumn( table->
columns() );
270 const int numberOfRow( table->
rows() );
271 dialog->setColumns( numberOfColumn );
272 dialog->setRows( numberOfRow );
274 dialog->setBorder( tableFormat.
border() );
277 dialog->setAlignment( tableFormat.
alignment() );
278 if ( tableFormat.
hasProperty( QTextFormat::BackgroundBrush ) ) {
283 dialog->setTypeOfLength( contrains.
at( 0 ).type() );
284 dialog->setLength( contrains.
at( 0 ).rawValue() * numberOfColumn );
287 if ( dialog->exec() ) {
288 const int newNumberOfColumns( dialog->columns() );
289 if ( ( newNumberOfColumns != numberOfColumn ) ||
290 ( dialog->rows() != numberOfRow ) ) {
291 table->
resize( dialog->rows(), newNumberOfColumns );
293 tableFormat.
setBorder( dialog->border() );
299 const QTextLength::Type type = dialog->typeOfLength();
300 const int length = dialog->length();
302 const QTextLength textlength( type, length / newNumberOfColumns );
303 for (
int i = 0; i < newNumberOfColumns; ++i ) {
304 contrains.
append( textlength );
307 const QColor tableBackgroundColor = dialog->tableBackgroundColor();
308 if ( dialog->useBackgroundColor() ) {
309 if ( tableBackgroundColor.
isValid() ) {
322 void TableActionMenuPrivate::_k_slotTableCellFormat()
324 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
325 QTextTable *table = textEdit->textCursor().currentTable();
330 if ( format.
hasProperty( QTextFormat::BackgroundBrush ) ) {
334 if ( dialog->exec() ) {
335 if ( dialog->useBackgroundColor() ) {
336 const QColor tableCellColor = dialog->tableCellBackgroundColor();
337 if ( tableCellColor.
isValid() ) {
351 void TableActionMenuPrivate::_k_slotSplitCell()
353 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
354 QTextTable *table = textEdit->textCursor().currentTable();
367 void TableActionMenuPrivate::_k_updateActions(
bool forceUpdate )
369 if ( ( textEdit->textMode() == KRichTextEdit::Rich ) || forceUpdate ) {
370 QTextTable *table = textEdit->textCursor().currentTable();
371 const bool isTable = ( table != 0 );
372 actionInsertRowBelow->setEnabled( isTable );
373 actionInsertRowAbove->setEnabled( isTable );
375 actionInsertColumnBefore->setEnabled( isTable );
376 actionInsertColumnAfter->setEnabled( isTable );
378 actionRemoveRowBelow->setEnabled( isTable );
379 actionRemoveRowAbove->setEnabled( isTable );
381 actionRemoveColumnBefore->setEnabled( isTable );
382 actionRemoveColumnAfter->setEnabled( isTable );
389 int firstColumn = -1;
391 textEdit->textCursor().selectedTableCells ( &firstRow, &numRows, &firstColumn, &numColumns );
392 const bool hasSelectedTableCell =
393 ( firstRow != -1 ) && ( numRows != -1 ) &&
394 ( firstColumn != -1 ) && ( numColumns != -1 );
396 actionMergeCell->setEnabled(
false );
398 actionMergeCell->setEnabled(
true );
401 actionSplitCell->setEnabled(
true );
403 actionSplitCell->setEnabled(
false );
405 actionTableCellFormat->setEnabled(
true );
406 actionMergeSelectedCells->setEnabled( hasSelectedTableCell );
408 actionSplitCell->setEnabled(
false );
409 actionMergeCell->setEnabled(
false );
410 actionMergeSelectedCells->setEnabled(
false );
412 actionTableFormat->setEnabled( isTable );
413 actionTableCellFormat->setEnabled( isTable );
414 actionRemoveCellContents->setEnabled( isTable );
418 TableActionMenu::TableActionMenu( KActionCollection *ac, TextEdit *textEdit )
419 : KActionMenu( textEdit ), d( new TableActionMenuPrivate( ac, textEdit, this ) )
421 KActionMenu *insertMenu =
new KActionMenu( i18n(
"Insert" ),
this );
422 addAction( insertMenu );
424 d->actionInsertTable =
new KAction( KIcon(
QLatin1String(
"insert-table" ) ), i18n(
"Table..." ),
this );
425 insertMenu->addAction( d->actionInsertTable );
426 ac->addAction(
QLatin1String(
"insert_new_table" ), d->actionInsertTable );
427 connect( d->actionInsertTable, SIGNAL(triggered(
bool)),
428 SLOT(_k_slotInsertTable()) );
430 insertMenu->addSeparator();
431 d->actionInsertRowBelow =
432 new KAction( KIcon(
QLatin1String(
"edit-table-insert-row-below" ) ),
433 i18n(
"Row Below" ),
this );
434 insertMenu->addAction( d->actionInsertRowBelow );
435 ac->addAction(
QLatin1String(
"insert_row_below" ), d->actionInsertRowBelow );
436 connect( d->actionInsertRowBelow, SIGNAL(triggered(
bool)),
437 SLOT(_k_slotInsertRowBelow()) );
439 d->actionInsertRowAbove =
440 new KAction( KIcon(
QLatin1String(
"edit-table-insert-row-above" ) ),
441 i18n(
"Row Above" ),
this );
442 insertMenu->addAction( d->actionInsertRowAbove );
443 ac->addAction(
QLatin1String(
"insert_row_above" ), d->actionInsertRowAbove );
444 connect( d->actionInsertRowAbove, SIGNAL(triggered(
bool)),
445 SLOT(_k_slotInsertRowAbove()) );
447 insertMenu->addSeparator();
448 d->actionInsertColumnBefore =
449 new KAction( KIcon(
QLatin1String(
"edit-table-insert-column-left" ) ),
450 i18n(
"Column Before" ),
this );
451 insertMenu->addAction( d->actionInsertColumnBefore );
452 ac->addAction(
QLatin1String(
"insert_column_before" ), d->actionInsertColumnBefore );
454 connect( d->actionInsertColumnBefore, SIGNAL(triggered(
bool)),
455 SLOT(_k_slotInsertColumnBefore()) );
457 d->actionInsertColumnAfter =
458 new KAction( KIcon(
QLatin1String(
"edit-table-insert-column-right" ) ),
459 i18n(
"Column After" ),
this );
460 insertMenu->addAction( d->actionInsertColumnAfter );
461 ac->addAction(
QLatin1String(
"insert_column_after" ), d->actionInsertColumnAfter );
462 connect( d->actionInsertColumnAfter, SIGNAL(triggered(
bool)),
463 SLOT(_k_slotInsertColumnAfter()) );
465 KActionMenu *removeMenu =
new KActionMenu( i18n(
"Delete" ),
this );
466 addAction( removeMenu );
468 d->actionRemoveRowBelow =
new KAction( i18n(
"Row Below" ),
this );
469 removeMenu->addAction( d->actionRemoveRowBelow );
470 ac->addAction(
QLatin1String(
"remove_row_below" ), d->actionRemoveRowBelow );
471 connect( d->actionRemoveRowBelow, SIGNAL(triggered(
bool)),
472 SLOT(_k_slotRemoveRowBelow()) );
474 d->actionRemoveRowAbove =
new KAction( i18n(
"Row Above" ),
this );
475 removeMenu->addAction( d->actionRemoveRowAbove );
476 ac->addAction(
QLatin1String(
"remove_row_above" ), d->actionRemoveRowAbove );
477 connect( d->actionRemoveRowAbove, SIGNAL(triggered(
bool)),
478 SLOT(_k_slotRemoveRowAbove()) );
480 removeMenu->addSeparator();
481 d->actionRemoveColumnBefore =
new KAction( i18n(
"Column Before" ),
this );
482 removeMenu->addAction( d->actionRemoveColumnBefore );
483 ac->addAction(
QLatin1String(
"remove_column_before" ), d->actionRemoveColumnBefore );
485 connect( d->actionRemoveColumnBefore, SIGNAL(triggered(
bool)),
486 SLOT(_k_slotRemoveColumnBefore()) );
488 d->actionRemoveColumnAfter =
new KAction( i18n(
"Column After" ),
this );
489 removeMenu->addAction( d->actionRemoveColumnAfter );
490 ac->addAction(
QLatin1String(
"remove_column_after" ), d->actionRemoveColumnAfter );
491 connect( d->actionRemoveColumnAfter, SIGNAL(triggered(
bool)),
492 SLOT(_k_slotRemoveColumnAfter()) );
494 removeMenu->addSeparator();
495 d->actionRemoveCellContents =
new KAction( i18n(
"Cell Contents" ),
this );
496 removeMenu->addAction( d->actionRemoveCellContents );
497 ac->addAction(
QLatin1String(
"remove_cell_contents" ), d->actionRemoveCellContents );
498 connect( d->actionRemoveCellContents, SIGNAL(triggered(
bool)),
499 SLOT(_k_slotRemoveCellContents()) );
504 new KAction( KIcon(
QLatin1String(
"edit-table-cell-merge" ) ),
505 i18n(
"Join With Cell to the Right" ),
this );
506 ac->addAction(
QLatin1String(
"join_cell_to_the_right" ), d->actionMergeCell );
507 connect( d->actionMergeCell, SIGNAL(triggered(
bool)),
508 SLOT(_k_slotMergeCell()) );
509 addAction( d->actionMergeCell );
511 d->actionMergeSelectedCells =
new KAction( i18n(
"Join Selected Cells" ),
this );
512 ac->addAction(
QLatin1String(
"join_cell_selected_cells" ), d->actionMergeSelectedCells );
513 connect( d->actionMergeSelectedCells, SIGNAL(triggered(
bool)),
514 SLOT(_k_slotMergeSelectedCells()) );
515 addAction( d->actionMergeSelectedCells );
518 new KAction( KIcon(
QLatin1String(
"edit-table-cell-split" ) ),
519 i18n(
"Split cells" ),
this );
520 ac->addAction(
QLatin1String(
"split_cells" ), d->actionSplitCell );
521 connect( d->actionSplitCell, SIGNAL(triggered(
bool)),
522 SLOT(_k_slotSplitCell()) );
523 addAction( d->actionSplitCell );
527 d->actionTableFormat =
new KAction( i18n(
"Table Format..." ),
this );
528 ac->addAction(
QLatin1String(
"table_format" ), d->actionTableFormat );
529 connect( d->actionTableFormat, SIGNAL(triggered(
bool)),
530 SLOT(_k_slotTableFormat()) );
531 addAction( d->actionTableFormat );
533 d->actionTableCellFormat =
new KAction( i18n(
"Table Cell Format..." ),
this );
534 ac->addAction(
QLatin1String(
"table_cell_format" ), d->actionTableCellFormat );
535 connect( d->actionTableCellFormat, SIGNAL(triggered(
bool)),
536 SLOT(_k_slotTableCellFormat()) );
537 addAction( d->actionTableCellFormat );
539 connect( textEdit, SIGNAL(cursorPositionChanged()),
540 this, SLOT(_k_updateActions()) );
541 d->_k_updateActions(
true );
544 TableActionMenu::~TableActionMenu()
551 #include "moc_tableactionmenu.cpp"
void resize(int rows, int columns)
QTextCursor firstCursorPosition() const
void removeSelectedText()
void append(const T &value)
void setCellSpacing(qreal spacing)
void setFormat(const QTextCharFormat &format)
void removeColumns(int index, int columns)
void insertColumns(int index, int columns)
void setColumnWidthConstraints(const QVector< QTextLength > &constraints)
bool movePosition(MoveOperation operation, MoveMode mode, int n)
QTextCursor lastCursorPosition() const
void appendRows(int count)
QBrush background() const
const QColor & color() const
bool hasProperty(int propertyId) const
QTextTableFormat format() const
void setVerticalAlignment(VerticalAlignment alignment)
void setBorder(qreal width)
qreal cellSpacing() const
void appendColumns(int count)
void setBackground(const QBrush &brush)
void setAlignment(QFlags< Qt::AlignmentFlag > alignment)
void setFormat(const QTextTableFormat &format)
void removeRows(int index, int rows)
void mergeCells(int row, int column, int numRows, int numCols)
QTextCharFormat format() const
QTextTableCellFormat toTableCellFormat() const
QTextTable * insertTable(int rows, int columns, const QTextTableFormat &format)
const T & at(int i) const
QTextTableCell cellAt(int row, int column) const
QVector< QTextLength > columnWidthConstraints() const
void insertRows(int index, int rows)
VerticalAlignment verticalAlignment() const
qreal cellPadding() const
void setCellPadding(qreal padding)
void splitCell(int row, int column, int numRows, int numCols)
Qt::Alignment alignment() const
void setPosition(int pos, MoveMode m)