21 #include "tableactionmenu.h"
23 #include "inserttabledialog.h"
24 #include "tableformatdialog.h"
26 #include <KActionCollection>
33 namespace KPIMTextEdit {
35 class TableActionMenuPrivate
38 TableActionMenuPrivate(KActionCollection *ac, TextEdit *edit,TableActionMenu *qq)
39 : actionCollection( ac ),
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_slotMergeCell();
57 void _k_slotTableFormat();
58 void _k_slotSplitCell();
59 void _k_updateActions(
bool forceUpdate =
false);
61 KAction *actionInsertTable;
63 KAction *actionInsertRowBelow;
64 KAction *actionInsertRowAbove;
66 KAction *actionInsertColumnBefore;
67 KAction *actionInsertColumnAfter;
69 KAction *actionRemoveRowBelow;
70 KAction *actionRemoveRowAbove;
72 KAction *actionRemoveColumnBefore;
73 KAction *actionRemoveColumnAfter;
75 KAction *actionMergeCell;
76 KAction *actionSplitCell;
78 KAction *actionTableFormat;
80 KActionCollection *actionCollection;
85 void TableActionMenuPrivate::_k_slotRemoveRowBelow()
87 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
88 QTextTable *table = textEdit->textCursor().currentTable();
90 const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
91 if ( cell.row()<table->rows() - 1 ) {
92 table->removeRows( cell.row(), 1 );
98 void TableActionMenuPrivate::_k_slotRemoveRowAbove()
100 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
101 QTextTable *table = textEdit->textCursor().currentTable();
103 const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
104 if ( cell.row() >= 1 ) {
105 table->removeRows( cell.row() - 1, 1 );
111 void TableActionMenuPrivate::_k_slotRemoveColumnBefore()
113 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
114 QTextTable *table = textEdit->textCursor().currentTable();
116 const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
117 if ( cell.column() > 0 ) {
118 table->removeColumns( cell.column() - 1, 1 );
124 void TableActionMenuPrivate::_k_slotRemoveColumnAfter()
126 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
127 QTextTable *table = textEdit->textCursor().currentTable();
129 const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
130 if ( cell.column()<table->columns() - 1 ) {
131 table->removeColumns( cell.column(), 1 );
137 void TableActionMenuPrivate::_k_slotInsertRowBelow()
139 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
140 QTextTable *table = textEdit->textCursor().currentTable();
142 const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
143 if ( cell.row()<table->rows() ) {
144 table->insertRows( cell.row() + 1, 1 );
146 table->appendRows( 1 );
152 void TableActionMenuPrivate::_k_slotInsertRowAbove()
154 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
155 QTextTable *table = textEdit->textCursor().currentTable();
157 const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
158 table->insertRows( cell.row(), 1 );
163 void TableActionMenuPrivate::_k_slotInsertColumnBefore()
165 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
166 QTextTable *table = textEdit->textCursor().currentTable();
168 const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
169 table->insertColumns( cell.column(), 1 );
174 void TableActionMenuPrivate::_k_slotInsertColumnAfter()
176 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
177 QTextTable *table = textEdit->textCursor().currentTable();
179 const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
180 if ( cell.column()<table->columns() ) {
181 table->insertColumns( cell.column() + 1, 1 );
183 table->appendColumns( 1 );
190 void TableActionMenuPrivate::_k_slotInsertTable()
192 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
193 QPointer<InsertTableDialog> dialog =
new InsertTableDialog( textEdit );
194 if ( dialog->exec() ) {
195 QTextCursor cursor = textEdit->textCursor();
196 QTextTableFormat tableFormat;
197 tableFormat.setBorder( dialog->border() );
198 const int numberOfColumns( dialog->columns() );
199 QVector<QTextLength> contrains;
200 const QTextLength::Type type = dialog->typeOfLength();
201 const int length = dialog->length();
203 for (
int i = 0; i < numberOfColumns; ++i ) {
204 QTextLength textlength( type, length / numberOfColumns );
205 contrains.append( textlength );
207 tableFormat.setColumnWidthConstraints( contrains );
208 tableFormat.setAlignment(Qt::AlignLeft);
209 QTextTable *table = cursor.insertTable( dialog->rows(), numberOfColumns);
210 table->setFormat(tableFormat);
216 void TableActionMenuPrivate::_k_slotMergeCell()
218 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
219 QTextTable *table = textEdit->textCursor().currentTable();
221 const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
222 table->mergeCells( cell.row(), cell.column(), 1, cell.columnSpan() +1 );
227 void TableActionMenuPrivate::_k_slotTableFormat()
229 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
230 QTextTable *table = textEdit->textCursor().currentTable();
232 QPointer<TableFormatDialog> dialog =
new TableFormatDialog( textEdit );
233 const int numberOfColumn( table->columns() );
234 const int numberOfRow( table->rows() );
235 dialog->setColumns( numberOfColumn );
236 dialog->setRows( numberOfRow );
237 QTextTableFormat tableFormat = table->format();
238 dialog->setBorder( tableFormat.border() );
239 dialog->setSpacing( tableFormat.cellSpacing() );
240 dialog->setPadding( tableFormat.cellPadding() );
241 dialog->setAlignment(tableFormat.alignment());
243 QVector<QTextLength> contrains = tableFormat.columnWidthConstraints();
244 if(!contrains.isEmpty()) {
245 dialog->setTypeOfLength(contrains.at(0).type());
246 dialog->setLength(contrains.at(0).rawValue()*numberOfColumn);
249 if ( dialog->exec() ) {
250 const int newNumberOfColumns(dialog->columns());
251 if ( ( newNumberOfColumns != numberOfColumn ) || ( dialog->rows() != numberOfRow ) ) {
252 table->resize( dialog->rows(), newNumberOfColumns );
254 tableFormat.setBorder( dialog->border() );
255 tableFormat.setCellPadding( dialog->padding() );
256 tableFormat.setCellSpacing( dialog->spacing() );
257 tableFormat.setAlignment( dialog->alignment() );
259 QVector<QTextLength> contrains;
260 const QTextLength::Type type = dialog->typeOfLength();
261 const int length = dialog->length();
263 for (
int i = 0; i < newNumberOfColumns; ++i ) {
264 QTextLength textlength( type, length / newNumberOfColumns );
265 contrains.append( textlength );
267 tableFormat.setColumnWidthConstraints( contrains );
269 table->setFormat( tableFormat );
276 void TableActionMenuPrivate::_k_slotSplitCell()
278 if ( textEdit->textMode() == KRichTextEdit::Rich ) {
279 QTextTable *table = textEdit->textCursor().currentTable();
281 const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
282 if ( cell.columnSpan() > 1 ) {
283 table->splitCell( cell.row(), cell.column(), 1, cell.columnSpan()-1 );
289 void TableActionMenuPrivate::_k_updateActions(
bool forceUpdate)
291 if ( ( textEdit->textMode() == KRichTextEdit::Rich ) || forceUpdate ) {
292 QTextTable *table = textEdit->textCursor().currentTable();
293 const bool isTable = ( table != 0 );
294 actionInsertRowBelow->setEnabled( isTable );
295 actionInsertRowAbove->setEnabled( isTable );
297 actionInsertColumnBefore->setEnabled( isTable );
298 actionInsertColumnAfter->setEnabled( isTable );
300 actionRemoveRowBelow->setEnabled( isTable );
301 actionRemoveRowAbove->setEnabled( isTable );
303 actionRemoveColumnBefore->setEnabled( isTable );
304 actionRemoveColumnAfter->setEnabled( isTable );
307 const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
308 if ( cell.column()>table->columns() - 2 ) {
309 actionMergeCell->setEnabled(
false );
311 actionMergeCell->setEnabled(
true );
313 if ( cell.columnSpan() > 1 ) {
314 actionSplitCell->setEnabled(
true );
316 actionSplitCell->setEnabled(
false );
319 actionSplitCell->setEnabled(
false );
320 actionMergeCell->setEnabled(
false );
322 actionTableFormat->setEnabled( isTable );
326 TableActionMenu::TableActionMenu(KActionCollection *ac, TextEdit *textEdit)
327 : KActionMenu( textEdit ), d( new TableActionMenuPrivate( ac, textEdit, this ) )
329 KActionMenu *insertMenu =
new KActionMenu( i18n(
"Insert" ),
this );
330 addAction( insertMenu );
332 d->actionInsertTable =
new KAction( KIcon(QLatin1String(
"table")), i18n(
"Table..." ),
this );
333 insertMenu->addAction( d->actionInsertTable );
334 ac->addAction( QLatin1String(
"insert_new_table" ), d->actionInsertTable );
335 connect( d->actionInsertTable, SIGNAL(triggered(
bool)), SLOT(_k_slotInsertTable()) );
337 insertMenu->addSeparator();
338 d->actionInsertRowBelow =
new KAction( KIcon(QLatin1String(
"edit-table-insert-row-below")), i18n(
"Row Below" ),
this );
339 insertMenu->addAction( d->actionInsertRowBelow );
340 ac->addAction( QLatin1String(
"insert_row_below" ), d->actionInsertRowBelow );
341 connect( d->actionInsertRowBelow, SIGNAL(triggered(
bool)), SLOT(_k_slotInsertRowBelow()) );
343 d->actionInsertRowAbove =
new KAction( KIcon(QLatin1String(
"edit-table-insert-row-above")), i18n(
"Row Above" ),
this );
344 insertMenu->addAction( d->actionInsertRowAbove );
345 ac->addAction( QLatin1String(
"insert_row_above" ), d->actionInsertRowAbove );
346 connect( d->actionInsertRowAbove, SIGNAL(triggered(
bool)), SLOT(_k_slotInsertRowAbove()) );
348 insertMenu->addSeparator();
349 d->actionInsertColumnBefore =
new KAction( KIcon(QLatin1String(
"edit-table-insert-column-left")), i18n(
"Column Before" ),
this );
350 insertMenu->addAction( d->actionInsertColumnBefore );
351 ac->addAction( QLatin1String(
"insert_column_before" ), d->actionInsertColumnBefore );
353 connect( d->actionInsertColumnBefore, SIGNAL(triggered(
bool)), SLOT(_k_slotInsertColumnBefore()) );
355 d->actionInsertColumnAfter =
new KAction( KIcon(QLatin1String(
"edit-table-insert-column-right")), i18n(
"Column After" ),
this );
356 insertMenu->addAction( d->actionInsertColumnAfter );
357 ac->addAction( QLatin1String(
"insert_column_after" ), d->actionInsertColumnAfter );
358 connect( d->actionInsertColumnAfter, SIGNAL(triggered(
bool)), SLOT(_k_slotInsertColumnAfter()) );
360 KActionMenu *removeMenu =
new KActionMenu( i18n(
"Delete" ),
this );
361 addAction( removeMenu );
363 d->actionRemoveRowBelow =
new KAction( i18n(
"Row Below" ),
this );
364 removeMenu->addAction( d->actionRemoveRowBelow );
365 ac->addAction( QLatin1String(
"remove_row_below" ), d->actionRemoveRowBelow );
366 connect( d->actionRemoveRowBelow, SIGNAL(triggered(
bool)), SLOT(_k_slotRemoveRowBelow()) );
368 d->actionRemoveRowAbove =
new KAction( i18n(
"Row Above" ),
this );
369 removeMenu->addAction( d->actionRemoveRowAbove );
370 ac->addAction( QLatin1String(
"remove_row_above" ), d->actionRemoveRowAbove );
371 connect( d->actionRemoveRowAbove, SIGNAL(triggered(
bool)), SLOT(_k_slotRemoveRowAbove()) );
373 removeMenu->addSeparator();
374 d->actionRemoveColumnBefore =
new KAction( i18n(
"Column Before" ),
this );
375 removeMenu->addAction( d->actionRemoveColumnBefore );
376 ac->addAction( QLatin1String(
"remove_column_before" ), d->actionRemoveColumnBefore );
378 connect( d->actionRemoveColumnBefore, SIGNAL(triggered(
bool)), SLOT(_k_slotRemoveColumnBefore()) );
380 d->actionRemoveColumnAfter =
new KAction( i18n(
"Column After" ),
this );
381 removeMenu->addAction( d->actionRemoveColumnAfter );
382 ac->addAction( QLatin1String(
"remove_column_after" ), d->actionRemoveColumnAfter );
383 connect( d->actionRemoveColumnAfter, SIGNAL(triggered(
bool)), SLOT(_k_slotRemoveColumnAfter()) );
386 d->actionMergeCell =
new KAction( KIcon(QLatin1String(
"edit-table-cell-merge")), i18n(
"Join With Cell to the Right" ),
this );
387 ac->addAction( QLatin1String(
"join_cell_to_the_right" ), d->actionMergeCell );
388 connect( d->actionMergeCell, SIGNAL(triggered(
bool)), SLOT(_k_slotMergeCell()) );
389 addAction( d->actionMergeCell );
392 d->actionSplitCell =
new KAction( KIcon(QLatin1String(
"edit-table-cell-split")), i18n(
"Split cells" ),
this );
393 ac->addAction( QLatin1String(
"split_cells" ), d->actionSplitCell );
394 connect( d->actionSplitCell, SIGNAL(triggered(
bool)), SLOT(_k_slotSplitCell()) );
395 addAction( d->actionSplitCell );
397 d->actionTableFormat =
new KAction( i18n(
"Table Format..." ),
this );
398 ac->addAction( QLatin1String(
"table_format" ), d->actionTableFormat );
399 connect( d->actionTableFormat, SIGNAL(triggered(
bool)), SLOT(_k_slotTableFormat()) );
400 addAction( d->actionTableFormat );
402 connect( textEdit, SIGNAL(cursorPositionChanged()),
this, SLOT(_k_updateActions()) );
403 d->_k_updateActions(
true );
407 TableActionMenu::~TableActionMenu()
413 #include "moc_tableactionmenu.cpp"