1 // Licensed GNU LGPL v3 or later: http://www.gnu.org/licenses/lgpl.html
2 
3 #include "smmorphoperatorview.hh"
4 #include "smrenameopwindow.hh"
5 #include "smoperatorrolemap.hh"
6 
7 using namespace SpectMorph;
8 
9 using std::string;
10 
MorphOperatorView(Widget * parent,MorphOperator * op,MorphPlanWindow * window)11 MorphOperatorView::MorphOperatorView (Widget *parent, MorphOperator *op, MorphPlanWindow *window) :
12   Frame (parent),
13   morph_plan_window (window),
14   m_op (op)
15 {
16   FixedGrid grid;
17 
18   title_label = new MorphOperatorTitle (this, "");
19   title_label->set_align (TextAlign::CENTER);
20   title_label->set_bold (true);
21   grid.add_widget (title_label, 0, 0, 43, 4);
22 
23   connect (title_label->signal_move, this, &MorphOperatorView::on_move);
24   connect (title_label->signal_end_move, this, &MorphOperatorView::on_end_move);
25   connect (title_label->signal_rename, this, &MorphOperatorView::on_rename);
26 
27   fold_button = new ToolButton (this);
28   grid.add_widget (fold_button, 2, 1, 2, 2);
29   connect (fold_button->signal_clicked, this, &MorphOperatorView::on_fold_clicked);
30 
31   close_button = new ToolButton (this, 'x');
32   grid.add_widget (close_button, 39, 1, 2, 2);
33   connect (close_button->signal_clicked, [=]() { m_op->morph_plan()->remove (m_op); });
34 
35   body_widget = new Widget (this);
36 
37   update_body_visible();
38 
39   /* title update */
40   connect (m_op->morph_plan()->signal_plan_changed, this, &MorphOperatorView::on_operators_changed);
41   on_operators_changed();
42 }
43 
44 void
set_role(int role)45 MorphOperatorView::set_role (int role)
46 {
47   if (m_role != role)
48     {
49       m_role = role;
50       set_role_colors();
51     }
52 }
53 
54 void
set_role_colors()55 MorphOperatorView::set_role_colors()
56 {
57   if (m_role == 2) /* directly connected to output */
58     {
59       title_label->set_color (Color (0.3, 0.9, 0.3));
60       set_frame_color (ThemeColor::FRAME);
61     }
62   else if (m_role > 0) /* output or reachable */
63     {
64       title_label->set_color (ThemeColor::TEXT);
65       set_frame_color (ThemeColor::FRAME);
66     }
67   else /* unreachable */
68     {
69       title_label->set_color (Color (0.7, 0.7, 0.7));
70       set_frame_color (Color (0.7, 0.7, 0.7));
71     }
72 }
73 
74 void
on_operators_changed()75 MorphOperatorView::on_operators_changed()
76 {
77   string title = m_op->type_name() + ": " + m_op->name();
78 
79   title_label->set_text (title);
80 }
81 
82 void
on_move(double y)83 MorphOperatorView::on_move (double y)
84 {
85   if (is_output()) // output operator: move not supported
86     return;
87 
88   set_frame_color (ThemeColor::MENU_ITEM);
89 
90   MorphOperator *op_next = morph_plan_window->where (m_op, y);
91 
92   signal_move_indication (op_next, false);
93 }
94 
95 void
on_end_move(double y)96 MorphOperatorView::on_end_move (double y)
97 {
98   if (is_output()) // output operator: move not supported
99     return;
100 
101   set_role_colors();
102 
103   MorphOperator *op_next = morph_plan_window->where (m_op, y);
104 
105   signal_move_indication (op_next, true); // done
106 
107   if (op_next != m_op) // avoid redundant moves
108     {
109       // DELETION can occur here
110       m_op->morph_plan()->move (m_op, op_next);
111     }
112 }
113 
114 void
on_rename()115 MorphOperatorView::on_rename()
116 {
117   RenameOpWindow::create (window(), m_op);
118 }
119