• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

KPIMTextedit Library

  • kpimtextedit
tableactionmenu.cpp
1 /*
2  Copyright (c) 2012 Montel Laurent <montel@kde.org>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 
19 */
20 
21 #include "tableactionmenu.h"
22 #include "textedit.h"
23 #include "inserttabledialog.h"
24 #include "tableformatdialog.h"
25 
26 #include <KActionCollection>
27 #include <KLocale>
28 
29 #include <QTextTable>
30 #include <QPointer>
31 #include <QDebug>
32 
33 namespace KPIMTextEdit {
34 
35 class TableActionMenuPrivate
36 {
37 public:
38  TableActionMenuPrivate(KActionCollection *ac, TextEdit *edit,TableActionMenu *qq)
39  : actionCollection( ac ),
40  textEdit( edit ),
41  q( qq )
42  {
43  }
44 
45  void _k_slotInsertRowBelow();
46  void _k_slotInsertRowAbove();
47  void _k_slotInsertColumnBefore();
48  void _k_slotInsertColumnAfter();
49 
50  void _k_slotInsertTable();
51 
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);
60 
61  KAction *actionInsertTable;
62 
63  KAction *actionInsertRowBelow;
64  KAction *actionInsertRowAbove;
65 
66  KAction *actionInsertColumnBefore;
67  KAction *actionInsertColumnAfter;
68 
69  KAction *actionRemoveRowBelow;
70  KAction *actionRemoveRowAbove;
71 
72  KAction *actionRemoveColumnBefore;
73  KAction *actionRemoveColumnAfter;
74 
75  KAction *actionMergeCell;
76  KAction *actionSplitCell;
77 
78  KAction *actionTableFormat;
79 
80  KActionCollection *actionCollection;
81  TextEdit *textEdit;
82  TableActionMenu *q;
83 };
84 
85 void TableActionMenuPrivate::_k_slotRemoveRowBelow()
86 {
87  if ( textEdit->textMode() == KRichTextEdit::Rich ) {
88  QTextTable *table = textEdit->textCursor().currentTable();
89  if ( table ) {
90  const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
91  if ( cell.row()<table->rows() - 1 ) {
92  table->removeRows( cell.row(), 1 );
93  }
94  }
95  }
96 }
97 
98 void TableActionMenuPrivate::_k_slotRemoveRowAbove()
99 {
100  if ( textEdit->textMode() == KRichTextEdit::Rich ) {
101  QTextTable *table = textEdit->textCursor().currentTable();
102  if ( table ) {
103  const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
104  if ( cell.row() >= 1 ) {
105  table->removeRows( cell.row() - 1, 1 );
106  }
107  }
108  }
109 }
110 
111 void TableActionMenuPrivate::_k_slotRemoveColumnBefore()
112 {
113  if ( textEdit->textMode() == KRichTextEdit::Rich ) {
114  QTextTable *table = textEdit->textCursor().currentTable();
115  if ( table ) {
116  const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
117  if ( cell.column() > 0 ) {
118  table->removeColumns( cell.column() - 1, 1 );
119  }
120  }
121  }
122 }
123 
124 void TableActionMenuPrivate::_k_slotRemoveColumnAfter()
125 {
126  if ( textEdit->textMode() == KRichTextEdit::Rich ) {
127  QTextTable *table = textEdit->textCursor().currentTable();
128  if ( table ) {
129  const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
130  if ( cell.column()<table->columns() - 1 ) {
131  table->removeColumns( cell.column(), 1 );
132  }
133  }
134  }
135 }
136 
137 void TableActionMenuPrivate::_k_slotInsertRowBelow()
138 {
139  if ( textEdit->textMode() == KRichTextEdit::Rich ) {
140  QTextTable *table = textEdit->textCursor().currentTable();
141  if ( table ) {
142  const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
143  if ( cell.row()<table->rows() ) {
144  table->insertRows( cell.row() + 1, 1 );
145  } else {
146  table->appendRows( 1 );
147  }
148  }
149  }
150 }
151 
152 void TableActionMenuPrivate::_k_slotInsertRowAbove()
153 {
154  if ( textEdit->textMode() == KRichTextEdit::Rich ) {
155  QTextTable *table = textEdit->textCursor().currentTable();
156  if ( table ) {
157  const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
158  table->insertRows( cell.row(), 1 );
159  }
160  }
161 }
162 
163 void TableActionMenuPrivate::_k_slotInsertColumnBefore()
164 {
165  if ( textEdit->textMode() == KRichTextEdit::Rich ) {
166  QTextTable *table = textEdit->textCursor().currentTable();
167  if ( table ) {
168  const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
169  table->insertColumns( cell.column(), 1 );
170  }
171  }
172 }
173 
174 void TableActionMenuPrivate::_k_slotInsertColumnAfter()
175 {
176  if ( textEdit->textMode() == KRichTextEdit::Rich ) {
177  QTextTable *table = textEdit->textCursor().currentTable();
178  if ( table ) {
179  const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
180  if ( cell.column()<table->columns() ) {
181  table->insertColumns( cell.column() + 1, 1 );
182  } else {
183  table->appendColumns( 1 );
184  }
185  }
186  }
187 }
188 
189 
190 void TableActionMenuPrivate::_k_slotInsertTable()
191 {
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();
202 
203  for ( int i = 0; i < numberOfColumns; ++i ) {
204  QTextLength textlength( type, length / numberOfColumns );
205  contrains.append( textlength );
206  }
207  tableFormat.setColumnWidthConstraints( contrains );
208  tableFormat.setAlignment(Qt::AlignLeft);
209  QTextTable *table = cursor.insertTable( dialog->rows(), numberOfColumns);
210  table->setFormat(tableFormat);
211  }
212  delete dialog;
213  }
214 }
215 
216 void TableActionMenuPrivate::_k_slotMergeCell()
217 {
218  if ( textEdit->textMode() == KRichTextEdit::Rich ) {
219  QTextTable *table = textEdit->textCursor().currentTable();
220  if ( table ) {
221  const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
222  table->mergeCells( cell.row(), cell.column(), 1, cell.columnSpan() +1 );
223  }
224  }
225 }
226 
227 void TableActionMenuPrivate::_k_slotTableFormat()
228 {
229  if ( textEdit->textMode() == KRichTextEdit::Rich ) {
230  QTextTable *table = textEdit->textCursor().currentTable();
231  if ( table ) {
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());
242 
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);
247  }
248 
249  if ( dialog->exec() ) {
250  const int newNumberOfColumns(dialog->columns());
251  if ( ( newNumberOfColumns != numberOfColumn ) || ( dialog->rows() != numberOfRow ) ) {
252  table->resize( dialog->rows(), newNumberOfColumns );
253  }
254  tableFormat.setBorder( dialog->border() );
255  tableFormat.setCellPadding( dialog->padding() );
256  tableFormat.setCellSpacing( dialog->spacing() );
257  tableFormat.setAlignment( dialog->alignment() );
258 
259  QVector<QTextLength> contrains;
260  const QTextLength::Type type = dialog->typeOfLength();
261  const int length = dialog->length();
262 
263  for ( int i = 0; i < newNumberOfColumns; ++i ) {
264  QTextLength textlength( type, length / newNumberOfColumns );
265  contrains.append( textlength );
266  }
267  tableFormat.setColumnWidthConstraints( contrains );
268 
269  table->setFormat( tableFormat );
270  }
271  delete dialog;
272  }
273  }
274 }
275 
276 void TableActionMenuPrivate::_k_slotSplitCell()
277 {
278  if ( textEdit->textMode() == KRichTextEdit::Rich ) {
279  QTextTable *table = textEdit->textCursor().currentTable();
280  if ( table ) {
281  const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
282  if ( cell.columnSpan() > 1 ) {
283  table->splitCell( cell.row(), cell.column(), 1, cell.columnSpan()-1 );
284  }
285  }
286  }
287 }
288 
289 void TableActionMenuPrivate::_k_updateActions(bool forceUpdate)
290 {
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 );
296 
297  actionInsertColumnBefore->setEnabled( isTable );
298  actionInsertColumnAfter->setEnabled( isTable );
299 
300  actionRemoveRowBelow->setEnabled( isTable );
301  actionRemoveRowAbove->setEnabled( isTable );
302 
303  actionRemoveColumnBefore->setEnabled( isTable );
304  actionRemoveColumnAfter->setEnabled( isTable );
305 
306  if ( table ) {
307  const QTextTableCell cell = table->cellAt( textEdit->textCursor() );
308  if ( cell.column()>table->columns() - 2 ) {
309  actionMergeCell->setEnabled( false );
310  } else {
311  actionMergeCell->setEnabled( true );
312  }
313  if ( cell.columnSpan() > 1 ) {
314  actionSplitCell->setEnabled( true );
315  } else {
316  actionSplitCell->setEnabled( false );
317  }
318  } else {
319  actionSplitCell->setEnabled( false );
320  actionMergeCell->setEnabled( false );
321  }
322  actionTableFormat->setEnabled( isTable );
323  }
324 }
325 
326 TableActionMenu::TableActionMenu(KActionCollection *ac, TextEdit *textEdit)
327  : KActionMenu( textEdit ), d( new TableActionMenuPrivate( ac, textEdit, this ) )
328 {
329  KActionMenu *insertMenu = new KActionMenu( i18n( "Insert" ), this );
330  addAction( insertMenu );
331 
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()) );
336 
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()) );
342 
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()) );
347 
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 );
352 
353  connect( d->actionInsertColumnBefore, SIGNAL(triggered(bool)), SLOT(_k_slotInsertColumnBefore()) );
354 
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()) );
359 
360  KActionMenu *removeMenu = new KActionMenu( i18n( "Delete" ), this );
361  addAction( removeMenu );
362 
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()) );
367 
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()) );
372 
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 );
377 
378  connect( d->actionRemoveColumnBefore, SIGNAL(triggered(bool)), SLOT(_k_slotRemoveColumnBefore()) );
379 
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()) );
384  addSeparator();
385 
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 );
390  addSeparator();
391 
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 );
396 
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 );
401 
402  connect( textEdit, SIGNAL(cursorPositionChanged()), this, SLOT(_k_updateActions()) );
403  d->_k_updateActions( true );
404 
405 }
406 
407 TableActionMenu::~TableActionMenu()
408 {
409  delete d;
410 }
411 }
412 
413 #include "moc_tableactionmenu.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:26:01 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KPIMTextedit Library

Skip menu "KPIMTextedit Library"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal