OpenASIP 2.2
Loading...
Searching...
No Matches
OperationDAGDialog.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 OperationDAGEditor.cc
26 *
27 * Declaration of OperationDAGEditor class.
28 *
29 * @author Tero Ryynänen 2008 (tero.ryynanen-no.spam-tut.fi)
30 * @note rating: red
31 */
32
33#include <wx/valgen.h>
34#include <boost/format.hpp>
35#include <fstream>
36#include <iostream>
37#include <string>
38
39#include "OperationDAGDialog.hh"
40#include "OperationContainer.hh"
41#include "WxConversion.hh"
42#include "WidgetTools.hh"
43#include "InputOperandDialog.hh"
45#include "OSEdConstants.hh"
46#include "Application.hh"
47#include "GUITextGenerator.hh"
48#include "OSEdTextGenerator.hh"
49#include "ErrorDialog.hh"
50#include "ConfirmDialog.hh"
51#include "CommandThread.hh"
52#include "DialogPosition.hh"
53#include "OSEd.hh"
54#include "StringTools.hh"
55#include "OperationIndex.hh"
56#include "Operation.hh"
57#include "Operand.hh"
58#include "OperationModule.hh"
59#include "ObjectState.hh"
60#include "WarningDialog.hh"
61#include "OperationDAG.hh"
62#include "TCEString.hh"
63#include "FileSystem.hh"
64
65using std::string;
66using std::vector;
67using std::ifstream;
68using boost::format;
69
70BEGIN_EVENT_TABLE(OperationDAGDialog, wxDialog)
71
72 EVT_BUTTON(ID_SAVE_DAG_BUTTON, OperationDAGDialog::onSaveDAG)
73 EVT_BUTTON(ID_UNDO_DAG_BUTTON, OperationDAGDialog::onUndoDAG)
74 EVT_BUTTON(ID_DELETE_DAG_BUTTON, OperationDAGDialog::onDeleteDAG)
78 EVT_TEXT(ID_EDIT_DAG, OperationDAGDialog::onDAGChange)
79 EVT_CHOICE(ID_INDEX_DAG, OperationDAGDialog::onComboBoxChange)
80
82
83/**
84 * Constructor.
85 *
86 * @param parent The parent window.
87 * @param op Operation to be created or modified.
88 * @param module Module in which operation belongs to.
89 * @param path Path in which module belongs to.
90 */
92 wxWindow* parent,
93 Operation* op ):
94 wxDialog(
95 parent, -1, _T(""),
96 DialogPosition::getPosition(DialogPosition::DIALOG_PROPERTIES),
97 wxDefaultSize, wxRESIZE_BORDER),
98 operation_(op),
99 dotInstalled_(false) {
100
101 createContents(this, true, true);
102
103
104 dagEdit_ = dynamic_cast<wxTextCtrl*>(FindWindow(ID_EDIT_DAG));
105
106 dagIndex_ =
107 dynamic_cast<wxChoice*>(FindWindow(ID_INDEX_DAG));
108
109 //FindWindow(ID_NAME)->SetValidator(
110 // wxTextValidator(wxFILTER_ASCII, &name_));
111
112 // set OK button as default choice
113 // FindWindow(ID_OK_BUTTON)->SetFocus();
114
115 orig_ = operation_->saveState(); // save original operation's state
116
117 setTexts();
118
119 FindWindow(ID_SAVE_DAG_BUTTON)->Disable();
120 FindWindow(ID_UNDO_DAG_BUTTON)->Disable();
121 FindWindow(ID_DELETE_DAG_BUTTON)->Disable();
122 FindWindow(ID_NEW_DAG_BUTTON)->Disable();
123
124 updateIndex();
125
126 dotInstalled_ = hasDot();
127
128 updateDAG();
129}
130
131/**
132 * Destructor.
133 */
135
136 // set dialog position
137 int x, y;
138 GetPosition(&x, &y);
139 wxPoint point(x, y);
141}
142
143/**
144 * Returns operation object of dialog.
145 *
146 * @return Operation object of dialog.
147 */
148Operation*
150 return operation_;
151}
152
153/**
154 * Set texts to all widgets.
155 */
156void
180
181/**
182 * Transfers data to window.
183 *
184 * @return True if transfer is successful.
185 */
186bool
188 return wxWindow::TransferDataToWindow();
189}
190
191/**
192 * Updates the DAG list.
193 */
194void
196
197 dagIndex_->Clear();
198
199 if (operation_ == NULL) {
200 return;
201 }
202
203 if (operation_->dagCount() > 0) {
204 for (int i = 0; i < operation_->dagCount(); i++) {
205 wxString oper = WxConversion::toWxString(i+1);
206 dagIndex_->Append(oper);
207 }
208 }
209
210 // last item is "New Dag"-option
211 wxString oper = wxT("New DAG");
212 dagIndex_->Append(oper);
213 dagIndex_->SetSelection(0);
214
215}
216
217/**
218 * Tests whether user has program dot is installed.
219 *
220 * @return True if dot is installed.
221 */
222bool
224
225 if (system("which dot > /dev/null 2>&1") != 0) {
226 wxString caption(wxString::FromAscii("Warning"));
227 wxString message(
228 wxString::FromAscii(
229 "You must have dot installed to display DAGs."));
230 wxMessageBox(message, caption);
231 dagImageStaticBoxSizer_->Show(false);
232 Layout();
233 return false; // cannot display DAG without dot
234 } else {
235 return true;
236 }
237}
238
239/**
240 * Updates TextCtrl for DAG
241 */
242void
244
245 dagEdit_->Clear();
246
247 // get selected item from ComboBox
248 int index = dagIndex_->GetSelection();
249
250 // check that index is legal
251 if (operation_ != NULL && index < operation_->dagCount()) {
252 std::string code = operation_->dagCode(index);
253 std::ostream dagCode(dagEdit_);
254 dagCode << code;
255 dagCode.flush();
256
257 FindWindow(ID_UNDO_DAG_BUTTON)->Disable();
258 FindWindow(ID_SAVE_DAG_BUTTON)->Disable();
260 FindWindow(ID_NEW_DAG_BUTTON)->Enable();
261
262 OperationDAG& currentDAG = operation_->dag(index);
263 if (operation_->dagError(index) != "") {
264 wxString message(
265 wxString::FromAscii(
266 operation_->dagError(index).c_str()));
267 wxString caption(wxString::FromAscii("Warning"));
268 wxMessageBox(message, caption);
269 dagImageStaticBoxSizer_->Show(false);
270 Layout();
271 }
272
273 std::string temp = FileSystem::createTempDirectory();
274 std::string pngDag = temp + "/dag.png";
275 std::string dotDag = temp + "/dag.dot";
276
277 if( !dotInstalled_ )
278 {
279 // cannot show DAG without dot
280 dagImageStaticBoxSizer_->Show(false);
281 Layout();
282 return;
283 }
284
285 if (currentDAG.isNull() == true) { // compile error
286
287 std::string errText = operation_->dagError(index);
288
289 // strip carriage returns from errText
290 while (true) {
291 const int pos = errText.find('\n');
292 if (pos == -1) {
293 break;
294 }
295 errText.replace(pos, 1,"\\n");
296 }
297
298 // command to generate error message
299 std::string dotCmd =
300 std::string("echo 'digraph G {n140545368 [label=\"") + errText +
301 "\", shape=plaintext, fontsize=12]; }' | dot -Tpng > " + pngDag;
302
303 if (system(dotCmd.c_str()) != 0) {
304 debugLog("Error executing dot.");
305 }
306
307 delete dotImage_;
308 dotImage_ = new wxBitmap(100,100);
309 wxString wxTemp(wxString::FromAscii(pngDag.c_str()));
310 dotImage_->LoadFile(wxTemp, wxBITMAP_TYPE_PNG);
311 dagStaticBitmap_->SetBitmap(*dotImage_);
312
313 unsigned int width = dotImage_->GetWidth();
314 unsigned int height = dotImage_->GetHeight();
315
316 // calculate virtual area and rounding the result up
317 width = static_cast<unsigned int>(float(width) / 20 + 0.5);
318 height = static_cast<unsigned int>(float(height) / 20 + 0.5);
319
320 dagWindow_->SetSize(-1, -1, width, height);
321 dagWindow_->SetScrollbars(20, 20, -1, -1);
322 dagImageStaticBoxSizer_->Show(true);
323
324 } else {
325
326 // generate dot
327 currentDAG.writeToDotFile(dotDag);
328
329 // generate png from dot
330 std::string dotCmd = "dot -Tpng " + dotDag + " > " + pngDag;
331 if (system(dotCmd.c_str()) != 0) {
332 debugLog("Error executing dot.");
333 }
334
335 delete dotImage_;
336 dotImage_ = new wxBitmap(300,100);
337 wxString wxTemp(wxString::FromAscii(pngDag.c_str()));
338 dotImage_->LoadFile(wxTemp, wxBITMAP_TYPE_PNG);
339 dagStaticBitmap_->SetBitmap(*dotImage_);
340 dagImageStaticBoxSizer_->Show(true);
341
342 unsigned int width = dotImage_->GetWidth();
343 unsigned int height = dotImage_->GetHeight();
344
345 // calculate virtual area and rounding the result up
346 width = static_cast<unsigned int>(float(width) / 20 + 0.5);
347 height = static_cast<unsigned int>(float(height) / 20 + 0.5);
348
349 dagWindow_->SetSize(50, 50);
350 dagWindow_->SetScrollbars(20, 20, width, height);
351 }
353
354 } else {
355
356 // new dag => do not update, just disable the buttons
357 FindWindow(ID_UNDO_DAG_BUTTON)->Disable();
358 FindWindow(ID_SAVE_DAG_BUTTON)->Disable();
360 FindWindow(ID_NEW_DAG_BUTTON)->Disable();
361
362 dagImageStaticBoxSizer_->Show(false);
363 }
364 Layout();
365}
366
367/**
368 * Handles the event when OK button is pushed.
369 */
370void
371OperationDAGDialog::onOK(wxCommandEvent&) {
372 for (int i = 0; i < operation_->dagCount(); i++) {
373 doSaveDAG(i);
374 }
375 EndModal(wxID_OK);
376}
377
378
379/**
380 * Handles the event when Cancel button is pushed.
381 */
382void
385 EndModal(wxID_CANCEL);
386}
387
388void
390 std::string code("");
391 wxString wxTemp;
392
393 for (int i = 0; i < dagEdit_->GetNumberOfLines(); ++i) {
394 wxTemp = dagEdit_->GetLineText(i);
395
396#if wxCHECK_VERSION(2, 6, 0)
397 std::string stdTemp(wxTemp.mb_str());
398#else
399 std::string stdTemp(wxTemp.c_str());
400#endif
401
402 code += stdTemp;
403 code += '\n';
404 }
405
406 if (operation_->dagCount() == index) {
407 operation_->addDag(code);
408 } else {
409 operation_->setDagCode(index, code.c_str());
410 }
411
412 updateIndex();
413 dagIndex_->SetSelection(index);
414 updateDAG();
415}
416
417/**
418 * Handles the event when Save button is pushed.
419 */
420void
422 if(operation_ == NULL) {
423 return;
424 }
425
426 int index = dagIndex_->GetSelection();
427
428 doSaveDAG(index);
429
430 FindWindow(ID_SAVE_DAG_BUTTON)->Disable();
431 FindWindow(ID_UNDO_DAG_BUTTON)->Disable();
433}
434
435/**
436 * Handles the event when Undo button is pushed.
437 */
438void
440 std::iostream dagCode(dagEdit_);
441
442 int index = dagIndex_->GetSelection();
443 if (operation_->dagCount() == index) { // new operation
444 dagEdit_->Clear();
446 } else {
447 updateDAG();
449 }
450 FindWindow(ID_SAVE_DAG_BUTTON)->Disable();
451 FindWindow(ID_UNDO_DAG_BUTTON)->Disable();
452}
453
454/**
455 * Handles the event when Delete button is pushed.
456 */
457void
459 int index = dagIndex_->GetSelection();
460 if (operation_->dagCount() > index) {
461 dagEdit_->Clear();
462 operation_->removeDag(index);
463 }
464
465 updateIndex();
466
467 if (index > 0) {
468 index--;
469 }
470
471 dagIndex_->SetSelection(index);
472 updateDAG();
473}
474
475/**
476 * Handles the event when New button is pushed.
477 */
478void
480 int index = operation_->dagCount();
481 dagEdit_->Clear();
482 dagIndex_->SetSelection(index);
483 FindWindow(ID_SAVE_DAG_BUTTON)->Disable();
484 FindWindow(ID_UNDO_DAG_BUTTON)->Disable();
486 FindWindow(ID_NEW_DAG_BUTTON)->Disable();
487 dagImageStaticBoxSizer_->Show(false);
488}
489
490/**
491 * Handles the event when DAG is changed
492 */
493void
497}
498
499/**
500 * Handles the event when ComboBox is changed
501 */
502void
504 updateDAG();
505}
506
507/**
508 * Creates the contents of the dialog.
509 *
510 * @param parent Parent window.
511 * @param call_fit If true, fits the contents inside the dialog.
512 * @param set_sizer If true, sets the main sizer as dialog contents.
513 * @return The created sizer.
514 */
515wxSizer*
517 wxWindow* parent,
518 bool call_fit,
519 bool set_sizer) {
520
521 // Window for scrolling DAG image
522 wxSize size(300, 400);
523 dagWindow_ = new wxScrolledWindow(parent, -1, wxDefaultPosition, size);
524
525 // Window sizers
526 wxBoxSizer *item0 = new wxBoxSizer(wxVERTICAL);
527
528 // Static box for DAG code
529 wxStaticBox *dagStaticBox = new wxStaticBox(parent, -1, wxT("Code"));
530 wxStaticBoxSizer *dagStaticBoxSizer =
531 new wxStaticBoxSizer(dagStaticBox, wxVERTICAL);
532
533 // DAG editor
534 wxTextCtrl* editDAG =
535 new wxTextCtrl(
536 parent, ID_EDIT_DAG, wxT(""), wxDefaultPosition,
537 wxSize(250,-1), wxTE_MULTILINE);
538
539 // Static box for DAG image
540 wxStaticBox *dagImageStaticBox = new wxStaticBox(parent, -1, wxT("DAG"));
542 new wxStaticBoxSizer(dagImageStaticBox, wxVERTICAL);
543
544 dagImageStaticBoxSizer_->Add(dagWindow_, 1, wxALL|wxGROW, 5);
545 dagImageStaticBoxSizer_->SetDimension(-1, -1, 550, 550);
546
547 // DAG image
548 dotImage_ = new wxBitmap(300,100);
550 new wxStaticBitmap(dagWindow_, ID_DAG_IMAGE, *dotImage_);
551
552 dagWindow_->SetSize(-1, -1, 50, 50);
553 dagWindow_->SetScrollbars(20, 20, 20, 20);
554 dagImageStaticBoxSizer_->Show(true);
555
556 // Add DAG editor to DAG code sizer
557 dagStaticBoxSizer->Add(editDAG, 1, wxALL|wxGROW, 5);
558
559 // Add DAG code to page sizer
560 wxBoxSizer *pageSizer = new wxBoxSizer(wxHORIZONTAL);
561 pageSizer->Add(dagStaticBoxSizer, 1, wxGROW|wxTOP, 10);
562 pageSizer->Add(dagImageStaticBoxSizer_, 2, wxGROW|wxTOP, 10);
563
564 // Add page sizer to window sizer
565 item0->Add(pageSizer, 1, wxEXPAND|wxALL, 5);
566
567 wxString strs9[] =
568 {
569 wxT("id: 1")
570 };
571
572
573 // ComboBox for choosing DAG
574 wxChoice *indexDAG =
575 new wxChoice(
576 parent, ID_INDEX_DAG, wxDefaultPosition, wxSize(150,-1), 1,
577 strs9);
578 wxButton *saveDAG =
579 new wxButton(
580 parent, ID_SAVE_DAG_BUTTON, wxT("Save"), wxDefaultPosition,
581 wxDefaultSize, 0);
582 wxButton *undoDAG =
583 new wxButton(
584 parent, ID_UNDO_DAG_BUTTON, wxT("Undo"), wxDefaultPosition,
585 wxDefaultSize, 0);
586 wxButton *deleteDAG =
587 new wxButton(
588 parent, ID_DELETE_DAG_BUTTON, wxT("Delete"), wxDefaultPosition,
589 wxDefaultSize, 0);
590 wxButton *newDAG =
591 new wxButton(
592 parent, ID_NEW_DAG_BUTTON, wxT("New"), wxDefaultPosition,
593 wxDefaultSize, 0);
594
595 wxBoxSizer *dagToolsSizer = new wxBoxSizer(wxHORIZONTAL);
596 dagToolsSizer->Add(indexDAG, 0, wxALIGN_CENTER|wxALL, 5);
597
598 wxGridSizer *dagButtonSizer = new wxGridSizer(2, 0, 0);
599
600 dagButtonSizer->Add(saveDAG, 0, wxALIGN_CENTER|wxALL, 5);
601 dagButtonSizer->Add(undoDAG, 0, wxALIGN_CENTER|wxALL, 5);
602 dagButtonSizer->Add(deleteDAG, 0, wxALIGN_CENTER|wxALL, 5);
603 dagButtonSizer->Add(newDAG, 0, wxALIGN_CENTER|wxALL, 5);
604
605 dagToolsSizer->Add(dagButtonSizer, 0, wxALIGN_CENTER|wxALL, 5);
606 dagStaticBoxSizer->Add(dagToolsSizer, 0, wxALL, 5);
607
608 wxBoxSizer *controlButtonSizer = new wxBoxSizer(wxHORIZONTAL);
609 wxButton *OK =
610 new wxButton(
611 parent, ID_OK_BUTTON, wxT("Ok"), wxDefaultPosition,
612 wxDefaultSize, 0);
613 wxButton *Cancel =
614 new wxButton(
615 parent, wxID_CANCEL, wxT("Cancel"), wxDefaultPosition,
616 wxDefaultSize, 0);
617
618 controlButtonSizer->Add(OK, 0, wxALL, 5);
619 controlButtonSizer->Add(Cancel, 0, wxALL, 5);
620 item0->Add(
621 controlButtonSizer, 0,
622 wxALL, 5);
623
624 if (set_sizer) {
625 parent->SetSizer(item0);
626 if (call_fit) {
627 item0->SetSizeHints(parent);
628 }
629 }
630
631
632 return item0;
633}
#define debugLog(text)
END_EVENT_TABLE() using namespace IDF
EVT_BUTTON(ID_EDIT_ARCH_PORT, FUImplementationDialog::onEditArchitecturePort) EVT_BUTTON(ID_ADD_EXTERNAL_PORT
find Finds info of the inner loops in the false
static void setPosition(Dialogs dialog, wxPoint point)
@ DIALOG_PROPERTIES
Operation properties dialog.
static bool removeFileOrDirectory(const std::string &path)
static std::string createTempDirectory(const std::string &path="/tmp", const std::string &tempDirPrefix="tmp_tce_")
virtual void writeToDotFile(const TCEString &fileName) const
static OSEdTextGenerator & instance()
@ TXT_PROPERTY_DIALOG_TITLE
Property dialog title.
@ TXT_BUTTON_SAVE
Save button label.
@ TXT_BUTTON_NEW
New button label.
@ TXT_BUTTON_UNDO
Undo button label.
@ TXT_BUTTON_DELETE
Delete button label.
virtual bool TransferDataToWindow()
wxStaticBoxSizer * dagImageStaticBoxSizer_
wxStaticBitmap * dagStaticBitmap_
void onComboBoxChange(wxCommandEvent &event)
wxSizer * createContents(wxWindow *parent, bool call_fit, bool set_sizer)
void onNewDAG(wxCommandEvent &event)
Operation * operation_
Operation to be created or to be modified.
void onDAGChange(wxCommandEvent &event)
void onDeleteDAG(wxCommandEvent &event)
void doSaveDAG(int index)
ObjectState * orig_
Original operation's ObjectState tree.
void onOK(wxCommandEvent &event)
void onUndoDAG(wxCommandEvent &event)
wxScrolledWindow * dagWindow_
Operation * operation() const
void onCancel(wxCommandEvent &event)
void onSaveDAG(wxCommandEvent &event)
bool isNull() const
virtual OperationDAG & dag(int index) const
Definition Operation.cc:148
virtual TCEString dagError(int index) const
Definition Operation.cc:182
virtual TCEString dagCode(int index) const
Definition Operation.cc:159
virtual void addDag(const TCEString &code)
Definition Operation.cc:113
virtual void setDagCode(int index, const TCEString &code)
Definition Operation.cc:171
virtual void removeDag(int index)
Definition Operation.cc:123
virtual int dagCount() const
Definition Operation.cc:134
virtual void loadState(const ObjectState *state)
Definition Operation.cc:480
static unsigned replace(std::string &str, const std::string &oldPattern, const std::string &newPattern)
Definition TCEString.cc:251
virtual boost::format text(int textId)
static void setLabel(Texts::TextGenerator *generator, wxWindow *widget, int textID)
static wxString toWxString(const std::string &source)