OpenASIP 2.2
Loading...
Searching...
No Matches
OSEdTreeView.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2009 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/**
25 * @file OSEdTreeView.cc
26 *
27 * Definition of OSEdTreeView class.
28 *
29 * @author Jussi Nykänen 2004 (nykanen-no.spam-cs.tut.fi)
30 * @note rating: red
31 */
32
33#include <boost/format.hpp>
34
35#include "OSEdTreeView.hh"
36#include "WxConversion.hh"
37#include "OperationContainer.hh"
38#include "MapTools.hh"
39#include "OSEdConstants.hh"
40#include "InformationDialog.hh"
41#include "OSEd.hh"
42#include "GUICommand.hh"
43#include "OSEdTextGenerator.hh"
44#include "Environment.hh"
45#include "DropDownMenu.hh"
46#include "OperationModule.hh"
47#include "CommandRegistry.hh"
48#include "Operation.hh"
49#include "OSEdInfoView.hh"
50#include "OperationIndex.hh"
51#include "TCEString.hh"
52#include "OperationBehavior.hh"
53#include "ObjectState.hh"
54
55using std::string;
56using std::vector;
57using boost::format;
58
59BEGIN_EVENT_TABLE(OSEdTreeView, wxTreeCtrl)
62 EVT_RIGHT_DOWN(OSEdTreeView::onDropDownMenu)
64
65/**
66 * Constructor.
67 *
68 * @param parent. Parent window.
69 * @param infoView Info view controlled by tree view.
70 */
71OSEdTreeView::OSEdTreeView(wxWindow* parent, OSEdInfoView* infoView) :
72 wxTreeCtrl(parent, OSEdConstants::CMD_TREE_ITEM), infoView_(infoView) {
73}
74
75/**
76 * Destructor.
77 */
80
81/**
82 * Constructs the tree structure.
83 *
84 * If errors occurs while constructing the tree structure, the error
85 * messages are collected and returned.
86 *
87 * @return Results of erronous modules.
88 */
89vector<string>
91
92 vector<string> results;
94 OperationSerializer& serializer =
96
97 wxTreeItemId root = AddRoot(_T("root"));
98 vector<string> paths = Environment::osalPaths();
99
100 for (size_t i = 0; i < paths.size(); i++) {
101 // add path
102 string pathName = paths[i];
103 wxTreeItemId path =
104 AppendItem(root, WxConversion::toWxString(pathName));
105
106 if (FileSystem::fileExists(pathName)) {
107 SetItemBold(path);
108 }
109
110 paths_[pathName] = path;
111 int modules = 0;
112 try {
113 modules = opIndex.moduleCount(pathName);
114 } catch (const PathNotFound& p) {
115 // no modules in this path
116 continue;
117 }
118 for (int j = 0; j < modules; j++) {
119 // add module
120 OperationModule& mod = opIndex.module(j, pathName);
121 wxTreeItemId module =
122 AppendItem(path, WxConversion::toWxString(mod.name()));
123
124 modules_.insert(std::pair<std::string, wxTreeItemId>(mod.name(), module));
125
126 int operations = 0;
127
128 // test that operation properties can be loaded
129 try {
130 operations = opIndex.operationCount(mod);
131 serializer.setSourceFile(mod.propertiesModule());
132 ObjectState* tree = serializer.readState();
133 if (tree->childCount() > 0) {
135 temp.loadState(tree->child(0));
136 }
137 delete tree;
138 } catch (const Exception& e) {
139 results.push_back(mod.propertiesModule() + ":\n");
140 results.push_back(e.errorMessage() + "\n");
141 results.push_back("\n");
142 }
143
144 for (int k = 0; k < operations; k++) {
145 // add operation
146 string opName = opIndex.operationName(k, mod);
147 wxTreeItemId oper =
148 AppendItem(module, WxConversion::toWxString(opName));
149
150 //operations_[opIndex.operationName(k, mod)] = oper;
151 operations_.insert(std::pair<std::string, wxTreeItemId>(opIndex.operationName(k, mod), oper));
152 }
153 SortChildren(module);
154 }
155 SortChildren(path);
156 }
158 return results;
159}
160
161/**
162 * Handles the event when item is selected on tree.
163 *
164 * If selected item is a path, path view is shown. If selected
165 * item is a module, module view is shown. If selected item is an operation,
166 * operation view is shown.
167 *
168 * @param event The event to be handled.
169 */
170void
171OSEdTreeView::onItemSelected(wxTreeEvent& event) {
172
173 OSEdMainFrame* mainFrame = wxGetApp().mainFrame();
175 wxTreeItemId id = event.GetItem();
176
177 if (isPath(id)) {
178 string path = MapTools::keyForValue<string>(paths_, id);
180 mainFrame->statusBar()->
181 SetStatusText(WxConversion::toWxString(fmt.str()));
182 infoView_->moduleView(path);
183 } else if (isModule(id)) {
184 string moduleName = MapTools::keyForValue<string>(modules_, id);
186 mainFrame->statusBar()->
187 SetStatusText(WxConversion::toWxString(fmt.str()));
188 string pathName = pathOfModule(id);
189 infoView_->operationView(pathName, moduleName);
190 } else if (isOperation(id)) {
191
192 string opName = MapTools::keyForValue<string>(operations_, id);
193 format fmt =
195
196 mainFrame->statusBar()->
197 SetStatusText(WxConversion::toWxString(fmt.str()));
198
199 string modName = moduleOfOperation(id);
200 string pathName = pathOfModule(GetItemParent(id));
201 infoView_->operationPropertyView(opName, modName, pathName);
202 } else {
203 mainFrame->statusBar()->SetStatusText(_T(""));
204 // root is selected
206 }
207 wxGetApp().mainFrame()->updateMenuBar();
208}
209
210/**
211 * Handles the event when item is selected on tree by double-clicking
212 * with mouse.
213 *
214 * Double clicking an operation opens an operation property dialog, where
215 * operation properties can be modified.
216 *
217 * @param event Event to be handled.
218 */
219void
220OSEdTreeView::onItemClicked(wxTreeEvent& event) {
221 wxTreeItemId id = event.GetItem();
222 if (isOperation(id)) {
223 // operation is clicked, lets open the property dialog
224 OSEdMainFrame* mainFrame = wxGetApp().mainFrame();
225 CommandRegistry* registry = mainFrame->registry();
226 GUICommand* command =
228 command->setParentWindow(mainFrame);
229 command->Do();
230 delete command;
231 }
232}
233
234/**
235 * Handles the event when right mouse button is clicked.
236 *
237 * A different drop down menu is showed, depending on where mouse button is
238 * clicked.
239 *
240 * @param event Event to be handled.
241 */
242void
243OSEdTreeView::onDropDownMenu(wxMouseEvent& event) {
244
245 // coordinates of mouse click
247 OSEdMainFrame* mainFrame = wxGetApp().mainFrame();
248 wxPoint pos = event.GetPosition();
249 int flags = wxTREE_HITTEST_ONITEMLABEL;
250 wxTreeItemId id = HitTest(pos, flags);
251
252 // mouse is not clicked on a tree item
253 if (!id.IsOk()) {
254 return;
255 }
256
257 SelectItem(id);
258 DropDownMenu* menu = NULL;
259 if (isPath(id)) {
260
262 mainFrame->statusBar()->
263 SetStatusText(WxConversion::toWxString(fmt.str()));
265 PopupMenu(menu, pos);
266 } else if (isModule(id)) {
267
269 mainFrame->statusBar()->
270 SetStatusText(WxConversion::toWxString(fmt.str()));
272 PopupMenu(menu, pos);
273 } else if (isOperation(id)) {
274
275 format fmt =
277 mainFrame->statusBar()->
278 SetStatusText(WxConversion::toWxString(fmt.str()));
280 PopupMenu(menu, pos);
281 } else {
282 mainFrame->statusBar()->SetStatusText(_T("Cannot open"));
283 }
284 delete menu;
285}
286
287/**
288 * Returns true if tree node with given id is a path.
289 *
290 * @param id Id of the tree node.
291 * @return True if id is a path.
292 */
293bool
294OSEdTreeView::isPath(wxTreeItemId id) const {
296}
297
298/**
299 * Returns true if tree node with given id is a module.
300 *
301 * @param id Id of the tree node.
302 * @return True if id is a module.
303 */
304bool
305OSEdTreeView::isModule(wxTreeItemId id) const {
307}
308
309/**
310 * Return true if tree node with given id is an operation.
311 *
312 * @param id Id of the tree node.
313 * @return True if id is an operation.
314 */
315bool
316OSEdTreeView::isOperation(wxTreeItemId id) const {
318}
319
320/**
321 * Returns the selected operation.
322 *
323 * If no operation is selected, NULL is returned.
324 *
325 * @return The selected operation.
326 */
329
330 Iter it = operations_.begin();
331 string opName = "";
332 string modName = "";
333 string pathName = "";
334
335 // get the name of the selected operation, its module and its path.
336 while(it != operations_.end()) {
337 if (IsSelected((*it).second)) {
338 opName = (*it).first;
339 wxTreeItemId opId = (*it).second;
340
341 wxTreeItemId moduleId = GetItemParent(opId);
342 wxTreeItemId pathId = GetItemParent(moduleId);
343
344 modName = WxConversion::toString(GetItemText(moduleId));
345 pathName = WxConversion::toString(GetItemText(pathId));
346
347 break;
348 }
349 it++;
350 }
351
352 // if no operation was selected in a tree view, let's look from
353 // the info view
354 if (opName == "") {
355 opName = infoView_->selectedOperation();
356 if (opName == "") {
357 return NULL;
358 } else {
359
360 modName = selectedModule();
361 assert(modName != "");
362
363 pathName = pathOfModule(selectedModuleId());
364 assert(pathName != "");
365 }
366 }
367
368 Operation* op = OperationContainer::operation(pathName, modName, opName);
369 return op;
370}
371
372/**
373 * Returns the id of selected operation.
374 *
375 * @throw NotAvailable If no operation is selected.
376 * @return The id of selected operation.
377 */
378wxTreeItemId
380 Iter it = operations_.begin();
381 // first look from the tree view
382 while (it != operations_.end()) {
383 if (IsSelected((*it).second)) {
384 return (*it).second;
385 }
386 it++;
387 }
388
389 string name = infoView_->selectedOperation();
390 it = operations_.begin();
391 while (it != operations_.end()) {
392 if ((*it).first == name) {
393 return (*it).second;
394 }
395 it++;
396 }
397 throw NotAvailable(__FILE__, __LINE__, __func__);
398}
399
400/**
401 * Returns the module of an operation with a given id.
402 *
403 * @param id The id of operation.
404 * @return The name of the module.
405 */
406string
408 wxTreeItemId modId = GetItemParent(id);
409 string moduleName = MapTools::keyForValue<string>(modules_, modId);
410 return moduleName;
411}
412
413/**
414 * Returns the id of the module of the operation with a given id.
415 *
416 * @param id The id of the operation.
417 * @return The id of the module.
418 */
419wxTreeItemId
421 return GetItemParent(id);
422}
423
424/**
425 * Returns the path of the module with given id.
426 *
427 * @param id The id of the module.
428 * @return The name of the path.
429 */
430string
432 wxTreeItemId pathId = GetItemParent(id);
433 std::string path = MapTools::keyForValue<string>(paths_, pathId);
434 return path;
435}
436
437/**
438 * Returns the id of the path in which module with given id belongs to.
439 *
440 * @param id Id of the module.
441 * @return The id of the path.
442 */
443wxTreeItemId
445 return GetItemParent(id);
446}
447
448/**
449 * Returns the selected path.
450 *
451 * If not path is selected, an empty string is returned.
452 *
453 * @return The selected path, or an empty string.
454 */
455string
457
458 Iter it = paths_.begin();
459
460 // first look from the tree view
461 while (it != paths_.end()) {
462 if (IsSelected((*it).second)) {
463 return (*it).first;
464 }
465 it++;
466 }
467
468 // then info view
469 string path = infoView_->selectedPath();
470 return path;
471}
472
473/**
474 * Returns the id of selected path.
475 *
476 * @throw NotAvailable If path is not selected.
477 * @return Id of selected path.
478 */
479wxTreeItemId
481 Iter it = paths_.begin();
482
483 // first look from the tree view
484 while (it != paths_.end()) {
485 if (IsSelected((*it).second)) {
486 return (*it).second;
487 }
488 it++;
489 }
490
491 // then look from the info view
492 string path = infoView_->selectedPath();
493 if (path != "") {
494 try {
495 wxTreeItemId id = paths_[path];
496 return id;
497 } catch (const KeyNotFound& k) {
498 assert(false);
499 }
500 }
501
502 throw NotAvailable(__FILE__, __LINE__, __func__);
503}
504
505/**
506 * Returns the selected module.
507 *
508 * If no module is selected, an empty string is returned.
509 *
510 * @return The selected module or an empty string.
511 */
512string
514
515 IterM it = modules_.begin();
516
517 // first look from the tree view
518 while (it != modules_.end()) {
519 if (IsSelected((*it).second)) {
520 return (*it).first;
521 }
522 it++;
523 }
524
525 // then look from the info view
526 string module = infoView_->selectedModule();
527 return module;
528}
529
530/**
531 * Returns the id of selected module.
532 *
533 * @throw NotAvailable If no module is selected.
534 * @return The id of selected module.
535 */
536wxTreeItemId
538 if (MapTools::containsValue(modules_, GetSelection())) {
539 return GetSelection();
540 }
541
542 IterM it = modules_.begin();
543 // first search the selected module from tree view
544 while (it != modules_.end()) {
545 if (IsSelected((*it).second)) {
546 return (*it).second;
547 }
548 it++;
549 }
550
551 // no module in tree view selected, look from the info view
552 string module = infoView_->selectedModule();
553 if (module != "") {
554 try {
555 it = modules_.find(module);
556 wxTreeItemId id = it->second;
557 return id;
558 } catch (const KeyNotFound& k) {
559 assert(false);
560 }
561 }
562
563 throw NotAvailable(__FILE__, __LINE__, __func__);
564}
565
566/**
567 * Adds an item to the tree.
568 *
569 * @param parent Parent id.
570 * @param item The item to be added.
571 */
572void
573OSEdTreeView::addItem(wxTreeItemId parent, std::string item) {
574 wxTreeItemId id = AppendItem(parent, WxConversion::toWxString(item));
575 if (isPath(parent)) {
576 modules_.insert(std::pair<std::string, wxTreeItemId>(item, id));
577 } else if (isModule(parent)) {
578 operations_.insert(std::pair<std::string, wxTreeItemId>(item, id));
579 //operations_[item] = id;
580 } else {
581 assert(false);
582 }
583}
584
585/**
586 * Changes the text of the item.
587 *
588 * @param id Id of the item to be changed.
589 * @param text New label.
590 */
591void
592OSEdTreeView::changeText(wxTreeItemId id, const std::string& text) {
593
594 if (isOperation(id)) {
596 Iter it = operations_.find(key);
597 operations_.erase(it);
598 operations_.insert(std::pair<std::string, wxTreeItemId>(text, id));
599 //operations_[text] = id;
600 } else if (isModule(id)) {
601 string key = MapTools::keyForValue<string>(modules_, id);
602 IterM it = modules_.find(key);
603 modules_.erase(it);
604 modules_.insert(std::pair<std::string, wxTreeItemId>(text, id));
605 } else if (isPath(id)) {
606 string key = MapTools::keyForValue<string>(paths_, id);
607 Iter it = paths_.find(key);
608 paths_.erase(it);
609 paths_[text] = id;
610 }
611 SetItemText(id, WxConversion::toWxString(text));
612}
613
614/**
615 * Removes an item from the tree view.
616 *
617 * Collapses the parent and set the parent selected.
618 *
619 * @param id The id of the item to be removed.
620 */
621void
622OSEdTreeView::removeItem(wxTreeItemId id) {
623
624 wxTreeItemId parent = GetItemParent(id);
625 Collapse(parent);
626 SelectItem(parent);
627 DeleteChildren(id);
628 Delete(id);
629
630 // remove the item from the inner data structure also
631 if (isOperation(id)) {
633 Iter it = operations_.find(key);
634 operations_.erase(it);
635 } else if (isModule(id)) {
636 string key = MapTools::keyForValue<string>(modules_, id);
637 IterM it = modules_.find(key);
638 modules_.erase(it);
639 Iter ot = operations_.begin();
640 while (ot != operations_.end()) {
641 if (GetItemParent((*ot).second) == id) {
642 Iter next = ot;
643 next++;
644 operations_.erase(ot);
645 ot = next;
646 } else {
647 ot++;
648 }
649 }
650 } else if (isPath(id)) {
651 // paths can not be erased
652 assert(false);
653 }
654}
655
656/**
657 * Returns pointer to info view.
658 *
659 * @return Pointer to info view.
660 */
663 return infoView_;
664}
665
666/**
667 * Updates the treeview.
668 */
669void
671
672 Iter it = paths_.begin();
673 while (it != paths_.end()) {
674 if (IsSelected((*it).second)) {
675 infoView_->moduleView((*it).first);
676 return;
677 }
678 it++;
679 }
680
681 IterM itm = modules_.begin();
682 while (itm != modules_.end()) {
683 if (IsSelected((*itm).second)) {
684 wxTreeItemId pathId = GetItemParent((*itm).second);
685 string pathName = WxConversion::toString(GetItemText(pathId));
686 infoView_->operationView(pathName, (*itm).first);
687 return;
688 }
689 itm++;
690 }
691
692 it = operations_.begin();
693 while (it != operations_.end()) {
694 if (IsSelected((*it).second)) {
695 wxTreeItemId modId = GetItemParent((*it).second);
696 wxTreeItemId pathId = GetItemParent(modId);
697
698 string modName = WxConversion::toString(GetItemText(modId));
699 string pathName = WxConversion::toString(GetItemText(pathId));
700
701 infoView_->operationPropertyView((*it).first, modName, pathName);
702 return;
703 }
704 it++;
705 }
706
708}
709
710/**
711 * Returns true if a path is selected.
712 *
713 * @return True, if a path is selected.
714 */
715bool
717 if (MapTools::containsValue(paths_, GetSelection()) ||
718 infoView_->selectedPath() != "") {
719
720 return true;
721 }
722 return false;
723}
724
725/**
726 * Returns true if a module is selected.
727 *
728 * @return True, if a module is selected.
729 */
730bool
732 if (MapTools::containsValue(modules_, GetSelection()) ||
733 infoView_->selectedModule() != "") {
734 return true;
735 }
736 return false;
737}
738
739/**
740 * Returns true if an operation is selected.
741 *
742 * @return True, if an operation is selected.
743 */
744bool
746 if (MapTools::containsValue(operations_, GetSelection()) ||
747 infoView_->selectedOperation() != "") {
748
749 return true;
750 }
751 return false;
752}
753
#define __func__
#define assert(condition)
END_EVENT_TABLE() using namespace IDF
GUICommand * createCommand(const int id)
@ MENU_MODULE
Module menu.
@ MENU_OPERATION
Operation menu.
@ MENU_PATH
Path menu.
static std::vector< std::string > osalPaths()
std::string errorMessage() const
Definition Exception.cc:123
static bool fileExists(const std::string fileName)
virtual bool Do()=0
void setParentWindow(wxWindow *view)
Definition GUICommand.cc:64
static KeyType keyForValue(const MapType &aMap, const ValueType &aValue)
static bool containsValue(const MapType &aMap, const ValueType &aValue)
static NullOperationBehavior & instance()
@ CMD_TREE_ITEM
Id for tree events.
@ CMD_PROPERTIES
Operation properties command id.
std::string selectedOperation()
void operationView(const std::string &path, const std::string &mod)
void moduleView(const std::string &name)
std::string selectedModule()
void operationPropertyView(const std::string &opName, const std::string &modName, const std::string &pathName)
std::string selectedPath()
CommandRegistry * registry() const
wxStatusBar * statusBar() const
static OSEdTextGenerator & instance()
@ TXT_STATUS_MODULE_SELECTED
Status bar text when module is selected.
@ TXT_STATUS_OPERATION_SELECTED
Status bar text when operation is selected.
@ TXT_STATUS_PATH_SELECTED
Status bar text when path is selected.
bool isModuleSelected() const
wxTreeItemId selectedModuleId()
std::string pathOfModule(wxTreeItemId id)
std::multimap< std::string, wxTreeItemId > operations_
Operations of operation data base.
std::vector< std::string > constructTree()
void changeText(wxTreeItemId id, const std::string &text)
void onItemClicked(wxTreeEvent &event)
bool isOperationSelected() const
wxTreeItemId selectedOperationId()
void addItem(wxTreeItemId parent, std::string item)
virtual ~OSEdTreeView()
Operation * selectedOperation()
OSEdInfoView * infoView() const
bool isPathSelected() const
std::map< std::string, wxTreeItemId > paths_
Paths of the operation data base.
std::string selectedPath()
void onDropDownMenu(wxMouseEvent &event)
std::map< std::string, wxTreeItemId >::iterator Iter
Iterators for the maps.
bool isPath(wxTreeItemId id) const
bool isOperation(wxTreeItemId id) const
wxTreeItemId selectedPathId()
void removeItem(wxTreeItemId id)
bool isModule(wxTreeItemId id) const
OSEdInfoView * infoView_
An info window controlled by tree view.
std::string selectedModule()
wxTreeItemId moduleIdOfOperation(wxTreeItemId id)
void onItemSelected(wxTreeEvent &event)
wxTreeItemId pathIdOfModule(wxTreeItemId id)
std::multimap< std::string, wxTreeItemId > modules_
Modules of the operation data base.
std::multimap< std::string, wxTreeItemId >::iterator IterM
std::string moduleOfOperation(wxTreeItemId id)
ObjectState * child(int index) const
int childCount() const
static OperationSerializer & operationSerializer()
static Operation * operation(const std::string &path, const std::string &module, const std::string &oper)
static OperationIndex & operationIndex()
std::string operationName(int i, const OperationModule &om)
OperationModule & module(int i)
int operationCount(const OperationModule &om)
int moduleCount() const
virtual std::string name() const
virtual std::string propertiesModule() const
void setSourceFile(const std::string &filename)
virtual ObjectState * readState()
virtual void loadState(const ObjectState *state)
Definition Operation.cc:480
virtual boost::format text(int textId)
static wxString toWxString(const std::string &source)
static std::string toString(const wxString &source)