Bug Summary

File:OMNotebook/OMNotebook/OMNotebookGUI/cellcommands.cpp
Warning:line 209, column 19
Value stored to 'c' during its initialization is never read

Annotated Source Code

[?] Use j/k keys for keyboard navigation

1/*
2 * This file is part of OpenModelica.
3 *
4 * Copyright (c) 1998-2010, Linköpings University,
5 * Department of Computer and Information Science,
6 * SE-58183 Linköping, Sweden.
7 *
8 * All rights reserved.
9 *
10 * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
11 * LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
12 * THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
13 * PUBLIC LICENSE.
14 *
15 * The OpenModelica software and the Open Source Modelica
16 * Consortium (OSMC) Public License (OSMC-PL) are obtained
17 * from Linköpings University, either from the above address,
18 * from the URL: http://www.ida.liu.se/projects/OpenModelica
19 * and in the OpenModelica distribution.
20 *
21 * This program is distributed WITHOUT ANY WARRANTY; without
22 * even the implied warranty of MERCHANTABILITY or FITNESS
23 * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
24 * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
25 * OF OSMC-PL.
26 *
27 * See the full OSMC Public License conditions for more details.
28 *
29 * For more information about the Qt-library visit TrollTech's webpage
30 * regarding the Qt licence: http://www.trolltech.com/products/qt/licensing.html
31 */
32
33/*!
34 * \file cellcommands.h
35 * \author Ingemar Axelsson and Anders Fernström
36 *
37 * \brief Describes different cell commands
38 */
39
40// QT Headers
41#include <QObject>
42#include <QtGui/QTextCursor>
43#include <QtGui/QTextDocumentFragment>
44
45//STD Headers
46#include <exception>
47#include <stdexcept>
48#include <typeinfo>
49
50//IAEX Headers
51#include "cellcommands.h"
52#include "graphcell.h"
53#include "latexcell.h"
54#include "inputcell.h"
55#include "textcell.h"
56#include "cellgroup.h"
57
58
59typedef vector<Rule *> rules_t;
60
61namespace IAEX
62{
63 /*!
64 * \class AddCellCommand
65 * \author Ingemar Axelsson and Anders Fernström
66 *
67 * \brief Command for adding a new cell to the cellstructure.
68 */
69 void AddCellCommand::execute()
70 {
71 try
72 {
73 CellCursor *cursor = document()->getCursor();
74
75 Factory *fac = document()->cellFactory();
76
77 // 2005-10-28 AF, changed style from QString to CellStyle
78 CellStyle style;
79
80 if(cursor->currentCell()->hasChilds())
81 style = *cursor->currentCell()->child()->style();
82 else
83 style = *cursor->currentCell()->style();
84
85
86 //This does not work.
87 if(cursor->currentCell()->isClosed())
88 {
89 if(cursor->currentCell()->hasChilds())
90 {
91 cursor->currentCell()->child()->setReadOnly(true);
92 cursor->currentCell()->child()->setFocus(false);
93 }
94 }
95 else
96 {
97 cursor->currentCell()->setReadOnly(true);
98 cursor->currentCell()->setFocus(false);
99 }
100
101 // 2005-11-21 AF, Added check if the current cell is a
102 // inputcell, set style to 'text' instead.
103 // 2006-02-03 AF, added check if the current cell is a
104 // groupcell
105 if( style.name() == "input" || style.name() == "Input" || style.name() == "ModelicaInput" ||
106 style.name() == "cellgroup" )
107 cursor->addBefore(fac->createCell( "Text" ));
108 else
109 cursor->addBefore(fac->createCell(style.name()));
110
111 if(cursor->currentCell()->isClosed())
112 {
113 if(cursor->currentCell()->hasChilds())
114 {
115 cursor->currentCell()->child()->setReadOnly(false);
116 cursor->currentCell()->child()->setFocus(true);
117 }
118 }
119 else
120 {
121 cursor->currentCell()->setReadOnly(false);
122 cursor->currentCell()->setFocus(true);
123 }
124
125 //2006-01-18 AF, set docuement changed
126 document()->setChanged( true );
127 }
128 catch(exception &e)
129 {
130 // 2006-01-30 AF, add exception
131 string str = string("AddCellCommand(), Exception: \n") + e.what();
132 throw runtime_error( str.c_str() );
133 }
134 }
135
136 /*!
137 * \class CreateNewCellCommand
138 * \author Ingemar Axelsson
139 *
140 * \brief Command for creating a new cell.
141 */
142 void CreateNewCellCommand::execute()
143 {
144 try
145 {
146 CellCursor *cursor = document()->getCursor();
147
148 Factory *fac = document()->cellFactory();
149
150 //This does not work.
151 if(cursor->currentCell()->isClosed())
152 {
153 if(cursor->currentCell()->hasChilds())
154 {
155 cursor->currentCell()->child()->setReadOnly(true);
156 cursor->currentCell()->child()->setFocus(false);
157 }
158 }
159 else
160 {
161 cursor->currentCell()->setReadOnly(true);
162 cursor->currentCell()->setFocus(false);
163 }
164
165 cursor->addBefore(fac->createCell(style_));
166
167 if(cursor->currentCell()->isClosed())
168 {
169 if(cursor->currentCell()->hasChilds())
170 {
171 cursor->currentCell()->child()->setReadOnly(false);
172 cursor->currentCell()->child()->setFocus(true);
173 }
174 }
175 else
176 {
177 cursor->currentCell()->setReadOnly(false);
178 cursor->currentCell()->setFocus(true);
179 }
180
181 //2006-01-18 AF, set docuement changed
182 document()->setChanged( true );
183 }
184 catch(exception &e)
185 {
186 // 2006-01-30 AF, add exception
187 string str = string("CreateNewCommand(), Exception: \n") + e.what();
188 throw runtime_error( str.c_str() );
189 }
190 }
191
192
193 /*!
194 * \class CopySelectedCellsCommand
195 * \author Ingemar Axelsson
196 *
197 * \brief copy a cell. Adds a copy of the cell on the pasteboard.
198 *
199 *
200 * \todo Implement some kind of internal state or copy constructors
201 * for all cells. This to make the copy process more general.(Ingemar Axelsson)
202 *
203 * \todo Implement release from cellgroup. It should be possible to
204 * remove pointers to the cell structure without deleting the
205 * object. That needs reparenting of all subcells.(Ingemar Axelsson)
206 */
207 void CopySelectedCellsCommand::execute()
208 {
209 CellCursor *c = document()->getCursor();
Value stored to 'c' during its initialization is never read
210 vector<Cell *> cells = document()->getSelection();
211
212 if(cells.empty())
213 {
214 return;
215 //Empty pasteboard.
216 //application()->clearPasteboard();
217 //application()->addToPasteboard(c->currentCell());
218 }
219 else
220 {
221 document()->clearSelection(); //Notice
222 application()->clearPasteboard();
223
224 vector<Cell *>::iterator i = cells.begin();
225 for(;i != cells.end();++i)
226 {
227 application()->addToPasteboard((*i));
228 }
229 }
230 }
231
232 /*! What happens if a Groupcell is removed?
233 */
234 void DeleteCurrentCellCommand::execute()
235 {
236 try
237 {
238 CellCursor *c = document()->getCursor();
239 vector<Cell *> cells = document()->getSelection();
240
241 if(cells.empty())
242 {
243 return;
244 //Empty pasteboard.
245 //application()->clearPasteboard();
246 //application()->addToPasteboard(c->currentCell());
247
248 //c->removeCurrentCell();
249 }
250 else
251 {
252 document()->clearSelection(); //Notice
253 application()->clearPasteboard();
254
255 vector<Cell *>::iterator i = cells.begin();
256 for(;i != cells.end();++i)
257 {
258 c->moveAfter((*i));
259
260 //1. Copy cell to pasteboard.
261 application()->addToPasteboard(c->currentCell());
262
263 //2. Delete Cell.
264 c->removeCurrentCell();
265 }
266 }
267
268 //2006-01-18 AF, set docuement changed
269 document()->setChanged( true );
270 }
271 catch(exception &e)
272 {
273 // 2006-01-30 AF, add message box
274 string str = string("DeleteCurrentCellsCommand(), Exception: \n") + e.what();
275 throw runtime_error( str.c_str() );
276 }
277 }
278
279 //Det borde vara möjligt att titta på stylen för att avgöra om en
280 //cell är en grupp cell. Då borde det gå att kopiera in underceller
281 //också.
282 //
283 // Kontrollera på stylen hur cellerna ska kopieras. Speciellt ska
284 // gruppceller specialbehandlas så att deras subceller också
285 // kopieras. Just nu funkar det att kopiera enstaka celler. Men
286 // inte gruppceller.
287 void PasteCellsCommand::execute()
288 {
289 try
290 {
291 vector<Cell *> cells = application()->pasteboard();
292
293 // Insert new cells before this position.
294 if(!cells.empty())
295 {
296 //Reverse iterator!!!!!
297 //vector<Cell *>::reverse_iterator i = cells.rbegin();
298 //for(;i != cells.rend();++i)
299 // AF, Not reverse
300 vector<Cell *>::iterator i = cells.begin();
301 for(;i != cells.end();++i)
302 {
303 try
304 {
305 pasteCell( (*i) );
306 }
307 catch(exception &e)
308 {
309 throw e;
310 }
311 }
312 }
313 else
314 {
315 QMessageBox::warning( 0, QObject::tr("Warning"), QObject::tr("No cells in clipboard. To paste a cell, select the cells and perform copy."), "OK" );
316 }
317
318 //2006-01-18 AF, set docuement changed
319 document()->setChanged( true );
320 }
321 catch(exception &e)
322 {
323 // 2006-01-30 AF, add exception
324 string str = string("PasteCellsCommand(), Exception: \n") + e.what();
325 throw runtime_error( str.c_str() );
326 }
327 }
328
329 // 2006-01-16 AF, move this code to a seperated function
330 // 2006-09-04 AF, redid entire function, so groupcells are created, have there
331 // children added and THEN add to the documnet
332 void PasteCellsCommand::pasteCell( Cell *cell, CellGroup *groupcell )
333 {
334 // get cursor, factory and cell style
335 CellCursor *cursor = document()->getCursor();
336 Factory *factory = document()->cellFactory();
337 CellStyle style = *cell->style();
338
339 // set focus and readonly stuff (from old implementation, IA)
340 if(cursor->currentCell()->isClosed())
341 {
342 if(cursor->currentCell()->hasChilds())
343 {
344 cursor->currentCell()->child()->setReadOnly(true);
345 cursor->currentCell()->child()->setFocus(false);
346 }
347 }
348 else
349 {
350 cursor->currentCell()->setReadOnly(true);
351 cursor->currentCell()->setFocus(false);
352 }
353
354 // create the new cell, if there exists a groupcell add the new cell to
355 // that groupcell.
356 Cell* newCell = factory->createCell( style.name() );
357
358// if( groupcell )
359// groupcell->addChild( newCell );
360
361
362 // set content of cell
363 // *************************************************************************
364
365 // COPY - EVERY CELL TYPE
366 // ----------------------
367 newCell->setCellTag( cell->cellTag() );
368
369 // rules
370 rules_t rules = cell->rules();
371 rules_t::iterator current = rules.begin();
372 while( current != rules.end() )
373 {
374 newCell->addRule( (*current) );
375 ++current;
376 }
377
378 // COPY - SPECIFIC FOR CELL TYPE
379 // -----------------------------
380 if( typeid(CellGroup) == typeid( *newCell ))
381 {
382 CellGroup *newCellGroup = dynamic_cast<CellGroup *>( newCell );
383 newCellGroup->setClosed( cell->isClosed() );
384
385 if( cell->hasChilds() )
386 {
387 Cell* child = cell->child();
388 while( child )
389 {
390 pasteCell( child, newCellGroup );
391 child = child->next();
392 }
393 }
394 }
395 else if( typeid(InputCell) == typeid( *newCell ))
396 {
397 InputCell *newInputCell = dynamic_cast<InputCell *>( newCell );
398 InputCell *oldInputCell = dynamic_cast<InputCell *>( cell );
399
400 newInputCell->setStyle( style );
401 newInputCell->setText( oldInputCell->text() );
402
403 if( oldInputCell->isEvaluated() )
404 {
405 newInputCell->setEvaluated( true );
406 newInputCell->setTextOutput( oldInputCell->textOutput() );
407 }
408 else
409 newInputCell->setEvaluated( false );
410
411 newInputCell->setClosed( oldInputCell->isClosed() );
412 }
413 else if( typeid(GraphCell) == typeid( *newCell ))
414 {
415 GraphCell *newGraphCell = dynamic_cast<GraphCell *>( newCell );
416 GraphCell *oldGraphCell = dynamic_cast<GraphCell *>( cell );
417
418 newGraphCell->setStyle( style );
419 newGraphCell->setText( oldGraphCell->text() );
420
421 if( oldGraphCell->isEvaluated() )
422 {
423 newGraphCell->setEvaluated( true );
424 newGraphCell->setTextOutput( oldGraphCell->textOutput() );
425 }
426 else
427 newGraphCell->setEvaluated( false );
428
429 newGraphCell->setClosed( oldGraphCell->isClosed() );
430 }
431 else if( typeid(LatexCell) == typeid( *newCell ))
432 {
433 LatexCell *newLatexCell = dynamic_cast<LatexCell *>( newCell );
434 LatexCell *oldLatexCell = dynamic_cast<LatexCell *>( cell );
435
436 newLatexCell->setStyle( style );
437 newLatexCell->setTextHtml(oldLatexCell->textHtml());
438 if( oldLatexCell->isEvaluated() )
439 {
440 newLatexCell->setEvaluated(true);
441 //newLatexCell->setTextOutput( oldLatexCell->textOutput() );
442 newLatexCell->setTextOutputHtml(oldLatexCell->textOutputHtml());
443
444 }
445 else
446 {
447 newLatexCell->setEvaluated(false);
448 }
449 newLatexCell->setClosed( oldLatexCell->isClosed() );
450 }
451 else if( typeid(TextCell) == typeid( *newCell ))
452 {
453 newCell->setStyle( style );
454 newCell->setTextHtml( cell->textHtml() );
455 }
456 else
457 {
458 // Error
459 throw runtime_error("pasteCell(): Unknown celltype.");
460 }
461 // *************************************************************************
462
463
464 // Add cell to document
465 if( !groupcell )
466 cursor->addBefore( newCell );
467 else //if there exists a groupcell add the new cell to that groupcell.
468 groupcell->addChild( newCell );
469
470 // set focus and readonly stuff (from old implementation, IA)
471 if(cursor->currentCell()->isClosed())
472 {
473 if(cursor->currentCell()->hasChilds())
474 {
475 cursor->currentCell()->child()->setReadOnly(false);
476 cursor->currentCell()->child()->setFocus(true);
477 }
478 }
479 else
480 {
481 cursor->currentCell()->setReadOnly(false);
482 cursor->currentCell()->setFocus(true);
483 }
484 }
485
486 /*!
487 * \class DeleteSelectedCellsCommand
488 * \author Ingemar Axelsson
489 *
490 * Deletes all selected cells. If no cell is selected the cell that
491 * is before the cursor is deleted.
492 *
493 * What happens when all cells in a cellgroup is empty? Should the
494 * cellgroup be deleted? Or what to do with it?
495 *
496 * Notice that the selection must be cleared.
497 *
498 *
499 * \bug When all cells within a cellgroup is deleted. The cellgroup
500 * should also be deleted then. What if all but one cell is deleted?
501 */
502 void DeleteSelectedCellsCommand::execute()
503 {
504 try
505 {
506 vector<Cell *> cells = document()->getSelection();
507 if(cells.empty())
508 {
509 document()->getCursor()->deleteCurrentCell();
510 }
511 else
512 {
513 document()->clearSelection(); //Notice
514
515 vector<Cell *>::iterator i = cells.begin();
516 for(;i != cells.end();++i)
517 {
518 (document()->getCursor())->moveAfter((*i));
519 (document()->getCursor())->deleteCurrentCell();
520 }
521 }
522
523 //2006-01-18 AF, set docuement changed
524 document()->setChanged( true );
525 }
526 catch(exception &e)
527 {
528 // 2006-01-30 AF, add exception
529 string str = string("DeleteSelectedCellsCommand(), Exception: \n") + e.what();
530 throw runtime_error( str.c_str() );
531 }
532 }
533
534
535
536 /*!
537 * \class ChangeStyleOnSelectedCellsCommand
538 * \author Ingemar Axelsson and Anders Fernström
539 * \date 2005-10-28 (update)
540 *
541 * \brief Changes style on selected cells.
542 *
543 * This does not work on selected Cellgroups. This because I have
544 * not defined what will happen if I change style on a
545 * cellgroup. Probably all its children will get the same style.
546 *
547 * 2005-10-28, updated style_ from a QString object to a CellStyle
548 * object. Needed to do some major change of the class. /AF
549 *
550 */
551 void ChangeStyleOnSelectedCellsCommand::execute()
552 {
553 try
554 {
555 vector<Cell *> cells = document()->getSelection();
556
557 if(cells.empty())
558 {
559 document()->getCursor()->currentCell()->setStyle(style_);
560 }
561 else
562 {;
563 vector<Cell *>::iterator i = cells.begin();
564
565 for(;i != cells.end() ;++i)
566 {
567 //This makes an segfault. Do not now why?
568 (*i)->setStyle(style_);
569 }
570
571 }
572
573 //2006-01-18 AF, set docuement changed
574 document()->setChanged( true );
575 }
576 catch(exception &e)
577 {
578 // 2006-01-30 AF, add exception
579 string str = string("ChangeStyleOnSelectedCellsCommand(), Exception: \n") + e.what();
580 throw runtime_error( str.c_str() );
581 }
582 }
583
584
585 void ChangeStyleOnCurrentCellCommand::execute()
586 {
587 try
588 {
589 document()->getCursor()->currentCell()->setStyle(style_);
590
591 //2006-01-18 AF, set docuement changed
592 document()->setChanged( true );
593 }
594 catch(exception &e)
595 {
596 // 2006-01-30 AF, add exception
597 string str = string("ChangeStyleOnCurrentCellCommand(), Exception: \n") + e.what();
598 throw runtime_error( str.c_str() );
599 }
600 }
601
602
603 void MakeGroupCellCommand::execute()
604 {
605 try
606 {
607 Factory *fac = document()->cellFactory();
608 CellCursor *cursor = document()->getCursor();
609
610 Cell *prev = cursor->currentCell();
611 cursor->currentCell()->parentCell()->removeChild(prev);
612
613 Cell *group = fac->createCell("cellgroup", cursor->parentCell());
614
615 group->addChild(prev);
616 cursor->addBefore(group);
617 cursor->moveToLastChild(group);
618
619 //2006-01-18 AF, set docuement changed
620 document()->setChanged( true );
621 }
622 catch(exception &e)
623 {
624 // 2006-01-30 AF, add exception
625 string str = string("MakeGroupCellCommand(), Exception: \n") + e.what();
626 throw runtime_error( str.c_str() );
627
628 }
629 }
630
631 /*!
632 * \class UngroupCellCommand
633 * \author Anders Fernström
634 * \date 2006-04-26
635 *
636 * \brief Ungroup the cell
637 */
638 void UngroupCellCommand::execute()
639 {
640 try
641 {
642 vector<Cell *> cells = document()->getSelection();
643
644 if( !cells.empty() )
645 {
646 // clear selection before changing cell strucure
647 document()->clearSelection();
648
649 vector<Cell *>::iterator c_iter = cells.begin();
650 for(; c_iter != cells.end() ; ++c_iter )
651 {
652 //check if groupcell
653 if( typeid( *(*c_iter) ) == typeid( CellGroup ))
654 {
655 if( !(*c_iter)->hasChilds() )
656 throw runtime_error( "No children" );
657
658 // get child
659 Cell* child = (*c_iter)->child();
660 Cell* deletedCellsParent = (*c_iter)->parentCell();
661 Cell* deletedCellsPrevious = (*c_iter)->previous();
662 Cell* deletedCellsNext = (*c_iter)->next();
663
664 // if previous is 0 = first in cell
665 child->setPrevious( deletedCellsPrevious );
666 if( !child->hasPrevious() )
667 deletedCellsParent->setChild( child );
668 else
669 deletedCellsPrevious->setNext( child );
670
671 // add all children
672 while( child != 0 )
673 {
674 //child->setParent( (*c_iter)->parent() );
675 child->setParentCell( deletedCellsParent );
676
677 if( child->hasNext() )
678 child = child->next();
679 else
680 {
681 child->setNext( deletedCellsNext );
682 if( !child->hasNext() )
683 {
684 // update last value on all cells
685 Cell* current = child;
686 while( current != 0 )
687 {
688 current->setLast( child );
689 current = current->previous();
690 }
691 }
692 else
693 deletedCellsNext->setPrevious( child );
694
695 break;
696 }
697 }
698
699 (*c_iter)->setChild( 0 );
700 (*c_iter)->hide();
701
702 // must update groupcells parents layout
703 deletedCellsParent->removeCellWidgets();
704 deletedCellsParent->addCellWidgets();
705
706 // delete groupcell
707 //(document()->getCursor())->moveAfter( (*c_iter) );
708 //(document()->getCursor())->deleteCurrentCell();
709 delete (*c_iter);
710
711 // update document
712 document()->setChanged( true );
713 }
714 }
715 }
716 }
717 catch( exception &e )
718 {
719 string str = string("UngroupCellCommand(), Exception: ") + e.what();
720 throw runtime_error( str.c_str() );
721 }
722 }
723
724 /*!
725 * \class SplitCellCommand
726 * \author Anders Fernström
727 * \date 2006-04-26
728 *
729 * \brief Split the cell
730 */
731 void SplitCellCommand::execute()
732 {
733 try
734 {
735 if( document()->getCursor()->currentCell() )
736 {
737 if( typeid( *document()->getCursor()->currentCell() ) == typeid( TextCell ) ||
738 typeid( *document()->getCursor()->currentCell() ) == typeid( InputCell ) )
739 {
740 // extraxt text
741 QTextEdit* editor = document()->getCursor()->currentCell()->textEdit();
742 if( editor )
743 {
744 QTextCursor cursor = editor->textCursor();
745 cursor.movePosition( QTextCursor::End, QTextCursor::KeepAnchor );
746 QTextDocumentFragment text = cursor.selection();
747 cursor.removeSelectedText();
748
749 // add new cell
750 if( typeid( *document()->getCursor()->currentCell() ) == typeid( TextCell ) )
751 {
752 AddCellCommand addcellCommand;
753 addcellCommand.setApplication( application() );
754 addcellCommand.setDocument( document() );
755 addcellCommand.execute();
756 }
757 else
758 {
759 // inputcell
760 CreateNewCellCommand newcellCommand( "Input" );
761 newcellCommand.setApplication( application() );
762 newcellCommand.setDocument( document() );
763 newcellCommand.execute();
764 }
765
766 // add text to new cell
767 QTextEdit* newEditor = document()->getCursor()->currentCell()->textEdit();
768 QTextCursor newCursor = newEditor->textCursor();
769 newCursor.insertFragment( text );
770 newCursor.movePosition( QTextCursor::Start );
771 newEditor->setTextCursor( newCursor );
772
773 // update document
774 document()->setChanged( true );
775 }
776 }
777 }
778 }
779 catch( exception &e )
780 {
781 string str = string("SplitCellCommand(), Exception: ") + e.what();
782 throw runtime_error( str.c_str() );
783 }
784 }
785
786};