Blame view

Modif/epsilon-master/apps/statistics/histogram_parameter_controller.cpp 4.11 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
  #include "histogram_parameter_controller.h"
  #include "app.h"
  #include <assert.h>
  #include <cmath>
  
  using namespace Shared;
  
  namespace Statistics {
  
  HistogramParameterController::HistogramParameterController(Responder * parentResponder, Store * store) :
    FloatParameterController(parentResponder),
    m_cells{},
    m_store(store)
  {
  }
  
  const char * HistogramParameterController::title() {
    return I18n::translate(I18n::Message::HistogramSet);
  }
  
  void HistogramParameterController::willDisplayCellForIndex(HighlightCell * cell, int index) {
    if (index == numberOfRows()-1) {
      return;
    }
    MessageTableCellWithEditableText * myCell = (MessageTableCellWithEditableText *)cell;
    I18n::Message labels[k_numberOfCells] = {I18n::Message::RectangleWidth, I18n::Message::BarStart};
    myCell->setMessage(labels[index]);
    FloatParameterController::willDisplayCellForIndex(cell, index);
  }
  
  double HistogramParameterController::parameterAtIndex(int index) {
    assert(index >= 0 && index < k_numberOfCells);
    return index == 0 ? m_store->barWidth() : m_store->firstDrawnBarAbscissa();
  }
  
  bool HistogramParameterController::setParameterAtIndex(int parameterIndex, double f) {
    assert(parameterIndex >= 0 && parameterIndex < k_numberOfCells);
    if (parameterIndex == 0) {
      // The bar width cannot be negative
      if (f <= 0.0f) {
        app()->displayWarning(I18n::Message::ForbiddenValue);
        return false;
      }
  
      // There should be at least one value in the drawn bin
      for (int i = 0; i < DoublePairStore::k_numberOfSeries; i++) {
        if (m_store->firstDrawnBarAbscissa() <= m_store->maxValue(i)+f) {
          break;
        } else if (i == DoublePairStore::k_numberOfSeries - 1) {
          app()->displayWarning(I18n::Message::ForbiddenValue);
          return false;
        }
      }
  
      // The number of bars cannot be above the max
      assert(DoublePairStore::k_numberOfSeries > 0);
      double maxNewNumberOfBars = std::ceil((m_store->maxValue(0) - m_store->minValue(0))/f);
      for (int i = 1; i < DoublePairStore::k_numberOfSeries; i++) {
        double numberOfBars = std::ceil((m_store->maxValue(i) - m_store->minValue(i))/f);
        if (maxNewNumberOfBars < numberOfBars) {
          maxNewNumberOfBars = numberOfBars;
        }
      }
      if (maxNewNumberOfBars > Store::k_maxNumberOfBars) {
        app()->displayWarning(I18n::Message::ForbiddenValue);
        return false;
      }
  
      // Set the bar width
      m_store->setBarWidth(f);
    } else {
      // The number of bars cannot be above the max
      assert(DoublePairStore::k_numberOfSeries > 0);
      double maxNewNumberOfBars = ceilf((m_store->maxValue(0) - f)/m_store->barWidth());
      for (int i = 1; i < DoublePairStore::k_numberOfSeries; i++) {
        double numberOfBars = ceilf((m_store->maxValue(i) - f)/m_store->barWidth());
        if (maxNewNumberOfBars < numberOfBars) {
          maxNewNumberOfBars = numberOfBars;
        }
      }
      if (maxNewNumberOfBars > Store::k_maxNumberOfBars) {
        app()->displayWarning(I18n::Message::ForbiddenValue);
        return false;
      }
      // There should be at least one value in the drawn bin
      for (int i = 0; i < DoublePairStore::k_numberOfSeries; i++) {
        if (f <= m_store->maxValue(i)+m_store->barWidth()) {
          break;
        } else if (i == DoublePairStore::k_numberOfSeries - 1) {
          app()->displayWarning(I18n::Message::ForbiddenValue);
          return false;
        }
      }
      // Set the first drawn bar abscissa
      m_store->setFirstDrawnBarAbscissa(f);
    }
    return true;
  }
  
  HighlightCell * HistogramParameterController::reusableParameterCell(int index, int type) {
    assert(index >= 0 && index < k_numberOfCells);
    return m_cells[index];
  }
  
  View * HistogramParameterController::loadView() {
    SelectableTableView * tableView = (SelectableTableView *)FloatParameterController::loadView();
    for (int i = 0; i < k_numberOfCells; i++) {
      m_cells[i] = new MessageTableCellWithEditableText(tableView, this, m_draftTextBuffer, I18n::Message::Default);
    }
    return tableView;
  }
  
  void HistogramParameterController::unloadView(View * view) {
    for (int i = 0; i < k_numberOfCells; i++) {
      delete m_cells[i];
      m_cells[i] = nullptr;
    }
    FloatParameterController::unloadView(view);
  }
  
  }