Blame view

app/gui/oven_control/multicookview.cpp 6.24 KB
2f6b55128   김태훈   다중 요리 구현
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
  #include "multicookview.h"
  
  #include <QMouseEvent>
  #include <QPainter>
  #include <QDebug>
  
  MultiCookView::MultiCookView(QWidget *parent)
      : QWidget(parent),
        timePosition(0)
  {
      barPixmap.load(":/images/multi/bar.png");
  
      connect(&checkTimer, SIGNAL(timeout()), SLOT(check()));
  }
  
  void MultiCookView::setContainer(MultiCookContainer *container)
  {
      this->container = container;
      rects = calcRects();
      checkTimer.start(33);
  }
  
  void MultiCookView::setTimeBar(MultiCookTimeBar *bar)
  {
      this->bar = bar;
  }
  
  void MultiCookView::showNow()
  {
      timePosition = 0;
      updateView();
  
      bar->showNow();
  }
  
  void MultiCookView::showNext()
  {
      if (timePosition < 0 || !nothingOnRight())
      {
          timePosition++;
          updateView();
  
          bar->showNext();
      }
  }
  
  void MultiCookView::showPrev()
  {
      if (timePosition > 0)
      {
          timePosition--;
          updateView();
  
          bar->showPrev();
      }
  }
  
  void MultiCookView::updateView()
  {
      rects = calcRects();
      update();
  }
  
  void MultiCookView::mousePressEvent(QMouseEvent *event)
  {
      int x = event->x();
      int y = event->y();
  
      pressedSlot = calcSlot(x, y);
  }
  
  void MultiCookView::mouseReleaseEvent(QMouseEvent *event)
  {
      if (pressedSlot == -1)
          return;
  
      int x = event->x();
      int y = event->y();
  
      if (calcSlot(x, y) == pressedSlot)
          emit clicked(pressedSlot);
  }
  
  void MultiCookView::paintEvent(QPaintEvent */*event*/)
  {
      QPainter painter(this);
      paintGrids(painter);
      paintBars(painter);
      paintCurrentTime(painter);
  }
  
  void MultiCookView::resizeEvent(QResizeEvent *event)
  {
      rects = calcRects();
  
      QWidget::resizeEvent(event);
  }
  
  void MultiCookView::check()
  {
      bool needUpdate = false;
  
      QList<QRect> rects = calcRects();
      for (int i = 0; i < 10; i++)
      {
          if (rects.at(i) != this->rects.at(i))
          {
              needUpdate = true;
              break;
          }
      }
  
      if (needUpdate)
      {
          this->rects = rects;
          update();
      }
  }
  
  QList<QRect> MultiCookView::calcRects()
  {
      int w = width();
      int h = height();
      int x = w * (2-timePosition) / 10 + 1;
b3e47756e   김태훈   소스 파일 일괄 변경
125
      int y = (h / 10 - 67) / 2;
2f6b55128   김태훈   다중 요리 구현
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  
      QList<QRect> list;
      for (int i = 0; i < 10; i++)
      {
          MultiCook *cook = container->at(i);
          if (cook == Q_NULLPTR)
          {
              list.append(QRect());
              continue;
          }
  
          int barY = y + h * i / 10;
          int barW = 0;
  
          // bar width = remaining time(ms) / 200s * width of a grid
          int remainingTime = cook->remainingTime();
          if (remainingTime > 0)
              barW = qCeil(remainingTime / 2000000.0 * w);
b3e47756e   김태훈   소스 파일 일괄 변경
144
          list.append(QRect(x, barY, barW, 67));
2f6b55128   김태훈   다중 요리 구현
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
      }
  
      return list;
  }
  
  int MultiCookView::calcSlot(int x, int y)
  {
      for (int i = 0; i < 10; i++)
      {
          const QRect &r = rects.at(i);
          if (r.isNull())
              continue;
  
          if (r.contains(x, y))
              return i;
      }
  
      return -1;
  }
  
  void MultiCookView::paintGrids(QPainter &painter)
  {
b3e47756e   김태훈   소스 파일 일괄 변경
167
      static const int padding = 12;
2f6b55128   김태훈   다중 요리 구현
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
  
      int w = width();
      int h = height();
  
      painter.setBrush(Qt::NoBrush);
      painter.setPen(QColor(70,70,70));
  
      for (int i = 1; i < 10; i++)
          painter.drawLine(0, h * i / 10, w, h * i / 10);
  
      for (int i = 1; i < 10; i++)
          painter.drawLine(w * i / 10, padding, w * i / 10, h - padding);
  }
  
  void MultiCookView::paintBars(QPainter &painter)
  {
      for (int i = 0; i < 10; i++)
      {
          MultiCook *cook = container->at(i);
          if (cook == Q_NULLPTR)
              continue;
  
          QRect r = rects.at(i);
          if (!r.isNull())
              paintBar(painter, r);
b3e47756e   김태훈   소스 파일 일괄 변경
193
          r.translate(12, 0);
2f6b55128   김태훈   다중 요리 구현
194
b3e47756e   김태훈   소스 파일 일괄 변경
195
196
          if (r.left() < 12)
              r.moveLeft(12);
2f6b55128   김태훈   다중 요리 구현
197
198
199
200
201
202
203
204
205
  
          painter.setBrush(Qt::NoBrush);
          painter.setPen(Qt::white);
          painter.drawText(r, Qt::AlignVCenter | Qt::TextDontClip, cook->name());
      }
  }
  
  void MultiCookView::paintCurrentTime(QPainter &painter)
  {
b3e47756e   김태훈   소스 파일 일괄 변경
206
      static const int padding = 12;
2f6b55128   김태훈   다중 요리 구현
207
208
209
210
211
212
  
      int w = width();
      int h = height();
      int x = w * (2-timePosition) / 10;
  
      QPen pen(Qt::yellow);
b3e47756e   김태훈   소스 파일 일괄 변경
213
      pen.setWidth(4);
2f6b55128   김태훈   다중 요리 구현
214
215
216
217
218
219
220
      painter.setPen(pen);
      painter.setBrush(Qt::NoBrush);
      painter.drawLine(x, padding, x, h - padding);
  }
  
  void MultiCookView::paintBar(QPainter &painter, QRect r)
  {
b3e47756e   김태훈   소스 파일 일괄 변경
221
222
223
      static const int fullWidth = 337;
      static const int headWidth = 8;
      static const int tailWidth = fullWidth - 306;
2f6b55128   김태훈   다중 요리 구현
224
      static const int bodyWidth = fullWidth - headWidth - tailWidth;
b3e47756e   김태훈   소스 파일 일괄 변경
225
      static const int height = 67;
2f6b55128   김태훈   다중 요리 구현
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
  
      static const int bodyOffset = headWidth;
      static const int tailOffset = fullWidth - tailWidth;
  
      int w = r.width();
      if (w < tailWidth)
      {
          // Clip tail
          QRect sourceRect(tailWidth - w + tailOffset, 0, w, height);
          painter.drawPixmap(r, barPixmap, sourceRect);
      }
      else if (w < headWidth + tailWidth)
      {
          // Full tail
          QRect sourceRect(tailOffset, 0, tailWidth, height);
          QRect targetRect(r.x() + w - tailWidth, r.y(), tailWidth, height);
          painter.drawPixmap(targetRect, barPixmap, sourceRect);
  
          // Clip head
  //        sourceRect = QRect(0, 0, w - tailWidth, height);
  //        targetRect = QRect(r.x(), r.y(), w - tailWidth, height);
  //        painter.drawPixmap(targetRect, barPixmap, sourceRect);
          sourceRect = QRect(headWidth - (w - tailWidth), 0, w - tailWidth, height);
          targetRect = QRect(r.x(), r.y(), w - tailWidth, height);
          painter.drawPixmap(targetRect, barPixmap, sourceRect);
      }
      else
      {
          // Full tail
          QRect sourceRect(tailOffset, 0, tailWidth, height);
          QRect targetRect(r.x() + w - tailWidth, r.y(), tailWidth, height);
          painter.drawPixmap(targetRect, barPixmap, sourceRect);
  
          // Full head
          sourceRect = QRect(0, 0, headWidth, height);
          targetRect = QRect(r.x(), r.y(), headWidth, height);
          painter.drawPixmap(targetRect, barPixmap, sourceRect);
  
          // Stretch body
          sourceRect = QRect(bodyOffset, 0, bodyWidth, 0);
          targetRect = QRect(r.x() + bodyOffset, r.y(), w - headWidth - tailWidth, height);
          painter.drawPixmap(targetRect, barPixmap, sourceRect);
      }
  }
  
  bool MultiCookView::nothingOnRight()
  {
      int rightMost = width() * 2 / 3;
  
      foreach (QRect r, rects)
          if (r.right() > rightMost)
              return false;
  
      return true;
  }