Blame view

build2/epsilon-master/escher/src/toolbox.cpp 6.38 KB
6663b6c9   adorian   projet complet av...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
  #include <escher/toolbox.h>
  #include <escher/metric.h>
  #include <assert.h>
  #include <string.h>
  
  /* State */
  
  Toolbox::Stack::State::State(int selectedRow, KDCoordinate verticalScroll) :
    m_selectedRow(selectedRow),
    m_verticalScroll(verticalScroll)
  {
  }
  
  bool Toolbox::Stack::State::isNull(){
    if (m_selectedRow == -1) {
      return true;
    }
    return false;
  }
  
  /* Stack */
  
  void Toolbox::Stack::push(int selectedRow, KDCoordinate verticalScroll) {
    int i = 0;
    while (!m_statesStack[i].isNull() && i < k_maxModelTreeDepth) {
      i++;
    }
    assert(m_statesStack[i].isNull());
    m_statesStack[i] = State(selectedRow, verticalScroll);
  }
  
  Toolbox::Stack::State * Toolbox::Stack::stateAtIndex(int index) {
    return &m_statesStack[index];
  }
  
  int Toolbox::Stack::depth() {
    int depth = 0;
    for (int i = 0; i < k_maxModelTreeDepth; i++) {
      depth += (!m_statesStack[i].isNull());
    }
    return depth;
  }
  
  void Toolbox::Stack::pop() {
    int stackDepth = depth();
    if (stackDepth == 0) {
      return;
    }
    m_statesStack[stackDepth-1] = State();
  }
  
  void Toolbox::Stack::resetStack() {
    for (int i = 0; i < k_maxModelTreeDepth; i++) {
      m_statesStack[i] = State();
    }
  }
  
  /* List Controller */
  
  Toolbox::ListController::ListController(Responder * parentResponder, SelectableTableView * tableView, const char * title) :
    ViewController(parentResponder),
    m_selectableTableView(tableView),
    m_firstSelectedRow(0),
    m_title(title)
  {
  }
  
  const char * Toolbox::ListController::title() {
    return m_title;
  }
  
  View * Toolbox::ListController::view() {
    return m_selectableTableView;
  }
  
  void Toolbox::ListController::didBecomeFirstResponder() {
    m_selectableTableView->reloadData();
    m_selectableTableView->selectCellAtLocation(0, m_firstSelectedRow);
    app()->setFirstResponder(m_selectableTableView);
  }
  
  void Toolbox::ListController::setFirstSelectedRow(int firstSelectedRow) {
    m_firstSelectedRow = firstSelectedRow;
  }
  
  /* Toolbox */
  
  Toolbox::Toolbox(Responder * parentResponder, const char * title) :
    StackViewController(parentResponder, &m_listController, KDColorWhite, Palette::PurpleBright, Palette::PurpleDark),
    m_selectableTableView(&m_listController, this, this),
    m_listController(this, &m_selectableTableView, title),
    m_messageTreeModel(nullptr),
    m_sender(nullptr)
  {
    m_selectableTableView.setMargins(0);
    m_selectableTableView.setShowsIndicators(false);
  }
  
  bool Toolbox::handleEvent(Ion::Events::Event event) {
    return handleEventForRow(event, selectedRow());
  }
  
  void Toolbox::didBecomeFirstResponder() {
    app()->setFirstResponder(&m_listController);
  }
  
  void Toolbox::viewWillAppear() {
    StackViewController::viewWillAppear();
    m_messageTreeModel = (ToolboxMessageTree *)rootModel();
    m_selectableTableView.reloadData();
    m_stack.resetStack();
    m_listController.setFirstSelectedRow(0);
  }
  
  void Toolbox::viewDidDisappear() {
    StackViewController::viewDidDisappear();
    m_selectableTableView.deselectTable();
  }
  
  KDCoordinate Toolbox::rowHeight(int j) {
    return Metric::ToolboxRowHeight;
  }
  
  int Toolbox::numberOfRows() {
    if (m_messageTreeModel == nullptr) {
      m_messageTreeModel = (ToolboxMessageTree *)rootModel();
    }
    return m_messageTreeModel->numberOfChildren();
  }
  
  HighlightCell * Toolbox::reusableCell(int index, int type) {
    assert(type < 2);
    assert(index >= 0);
    assert(index < maxNumberOfDisplayedRows());
    if (type == LeafCellType) {
      return leafCellAtIndex(index);
    }
    return nodeCellAtIndex(index);
  }
  
  int Toolbox::reusableCellCount(int type) {
    return maxNumberOfDisplayedRows();
  }
  
  void Toolbox::willDisplayCellForIndex(HighlightCell * cell, int index) {
    ToolboxMessageTree * messageTree = (ToolboxMessageTree *)m_messageTreeModel->children(index);
    if (messageTree->numberOfChildren() == 0) {
      MessageTableCellWithMessage * myCell = (MessageTableCellWithMessage *)cell;
      myCell->setMessage(messageTree->label());
      myCell->setAccessoryMessage(messageTree->text());
      myCell->setAccessoryTextColor(Palette::GreyDark);
      return;
    }
    MessageTableCell * myCell = (MessageTableCell *)cell;
    myCell->setMessage(messageTree->label());
  }
  
  int Toolbox::typeAtLocation(int i, int j) {
    MessageTree * messageTree = (MessageTree *)m_messageTreeModel->children(j);
    if (messageTree->numberOfChildren() == 0) {
      return LeafCellType;
    }
    return NodeCellType;
  }
  
  int Toolbox::stackDepth() {
    return m_stack.depth();
  }
  
  bool Toolbox::handleEventForRow(Ion::Events::Event event, int selectedRow) {
    int depth = m_stack.depth();
    if ((event == Ion::Events::Back || event == Ion::Events::Left) && depth > 0) {
      return returnToPreviousMenu();
    }
    ToolboxMessageTree * selectedMessageTree = (ToolboxMessageTree *)m_messageTreeModel->children(selectedRow);
    if ((event == Ion::Events::OK || event == Ion::Events::EXE || event == Ion::Events::Right) && selectedMessageTree->numberOfChildren() > 0) {
      return selectSubMenu(selectedMessageTree);
    }
    if ((event == Ion::Events::OK || event == Ion::Events::EXE) && selectedMessageTree->numberOfChildren() == 0) {
      return selectLeaf(selectedMessageTree);
    }
    return false;
  }
  
  bool Toolbox::selectSubMenu(ToolboxMessageTree * selectedMessageTree) {
    m_stack.push(selectedRow(), m_selectableTableView.contentOffset().y());
    m_selectableTableView.deselectTable();
    m_messageTreeModel = selectedMessageTree;
    m_listController.setFirstSelectedRow(0);
    app()->setFirstResponder(&m_listController);
    return true;
  }
  
  bool Toolbox::returnToPreviousMenu() {
    int currentDepth = m_stack.depth();
    int index = 0;
    // We want to get the current ToolboxMessageTree's parent ToolboxMessageTree,
    // but there is no ToolboxMessageTree::getParent() method. We thus have to
    // start from the root ToolboxMessageTree and sequentially get the selected
    // child until it has the wanted depth.
    ToolboxMessageTree * parentMessageTree = (ToolboxMessageTree *)rootModel();
    Stack::State * previousState = m_stack.stateAtIndex(index++);;
    while (currentDepth-- > 1) {
      parentMessageTree = (ToolboxMessageTree *)parentMessageTree->children(previousState->selectedRow());
      previousState = m_stack.stateAtIndex(index++);
    }
    m_selectableTableView.deselectTable();
    m_messageTreeModel = parentMessageTree;
    m_listController.setFirstSelectedRow(previousState->selectedRow());
    KDPoint scroll = m_selectableTableView.contentOffset();
    m_selectableTableView.setContentOffset(KDPoint(scroll.x(), previousState->verticalScroll()));
    m_stack.pop();
    app()->setFirstResponder(&m_listController);
    return true;
  }