1 #include "schedulepoint.h"\r
2 #include "schedulescreen.h"\r
3 #include "settingscreen.h"\r
4 #include "globalsettings.h"\r
5 #include "utilities.h"\r
6 \r
7 #include <QtGui>\r
8 \r
9 \r
10 #define MAXTEMP 100\r
11 #define MINTEMP 50\r
12 \r
13 SchedulePoint::SchedulePoint(int seqNumber)\r
14 {\r
15 m_globalSettings = GlobalSettings::getInstance();\r
16 \r
17 // create schedule point with necessary information\r
18 ID = seqNumber;\r
19 temp = 72;\r
20 pen.setColor((Qt::white));\r
21 setFlags(ItemIsSelectable);\r
22 location=0;\r
23 m_pressed = false;\r
24 m_disabled = false;\r
25 m_selected = false;\r
26 updateUnit();\r
27 }\r
28 \r
29 SchedulePoint::~SchedulePoint()\r
30 {\r
31 // destructor\r
32 }\r
33 \r
34 //calculation to make color vary linearly from defined MINTEMP to MAXTEMP\r
35 //based on passed temp\r
36 \r
37 QColor SchedulePoint::tempToColor(int temp)\r
38 {\r
39 float pct = ((float)temp-MINTEMP)/100.0 *((float)MAXTEMP/MINTEMP);\r
40 return QColor(200*pct, 20, 200*(1-pct));\r
41 }\r
42 \r
43 void SchedulePoint::setText(const QString &text)\r
44 {\r
45 // add new text\r
46 prepareGeometryChange();\r
47 myText = text;\r
48 update();\r
49 }\r
50 \r
51 void SchedulePoint::setTimeBlockWidth(float timeBlockWidth)\r
52 {\r
53 m_timeBlockWidth = timeBlockWidth;\r
54 }\r
55 \r
56 void SchedulePoint::setWeekHeight(int weekHeight)\r
57 {\r
58 m_weekHeight = weekHeight;\r
59 }\r
60 \r
61 void SchedulePoint::setPointArea(QRectF pointArea)\r
62 {\r
63 m_pointArea = pointArea;\r
64 }\r
65 \r
66 QRectF SchedulePoint::outlineRect() const\r
67 {\r
68 // define outline rectangle\r
69 QRectF rect;\r
70 //choose either to go smallest size to fit the text or 90% of the row Height\r
71 rect.setHeight(qMax(rect.height(), (qreal)(m_weekHeight*.90)));\r
72 //adjust to make wider and look normal\r
73 rect.adjust(-m_timeBlockWidth*2.5, 0, m_timeBlockWidth*2.5, 0);\r
74 //make it so the coords represent the center\r
75 rect.translate(-rect.center());\r
76 return rect;\r
77 }\r
78 \r
79 QRectF SchedulePoint::boundingRect() const\r
80 {\r
81 // provide size of each point for collision detection\r
82 const int margin = 1;\r
83 return outlineRect().adjusted(-margin, -margin, +margin, +margin);\r
84 }\r
85 \r
86 QPainterPath SchedulePoint::shape() const\r
87 {\r
88 // provide shape of each point\r
89 QRectF rect = outlineRect();\r
90 QPainterPath path;\r
91 path.addRoundRect(rect,roundness(rect.width()), roundness(rect.height()));\r
92 return path;\r
93 }\r
94 \r
95 void SchedulePoint::paint(QPainter *painter,\r
96 const QStyleOptionGraphicsItem *option, QWidget * /*widget*/)\r
97 {\r
98 // draw point itself\r
99 \r
100 if(option->state & QStyle::State_Selected) {\r
101 \r
102 }\r
103 \r
104 \r
105 \r
106 if(m_disabled)\r
107 {\r
108 painter->setBrush(QColor(150,150,150));\r
109 painter->setPen(QColor(110,110,110));\r
110 }\r
111 \r
112 else if(m_selected)\r
113 {\r
114 pen.setWidth(3);\r
115 pen.setColor(QColor(255, 189, 43));\r
116 painter->setPen(pen);\r
117 myBackgroundColor = tempToColor(temp);\r
118 painter->setBrush(myBackgroundColor);\r
119 }\r
120 else\r
121 {\r
122 myBackgroundColor = tempToColor(temp);\r
123 pen.setColor(Qt::white);\r
124 pen.setWidth(1);\r
125 painter->setPen(pen);\r
126 painter->setBrush(myBackgroundColor);\r
127 }\r
128 \r
129 QRectF rect = outlineRect();\r
130 painter->drawRoundRect(rect, roundness(rect.width()), roundness(rect.height()));\r
131 painter->setPen(Qt::white);\r
132 painter->drawText(rect, Qt::AlignCenter, myText);\r
133 }\r
134 \r
135 void SchedulePoint::mousePressEvent(QGraphicsSceneMouseEvent *e)\r
136 {\r
137 if(disabled())\r
138 return;\r
139 \r
140 // provide event handler for mouse click\r
141 emit clicked(this);\r
142 m_pressed = true;\r
143 pen.setColor(Qt::black);\r
144 setZValue(10);\r
145 m_xClickPos = e->pos().x();\r
146 m_yClickPos = e->pos().y();\r
147 }\r
148 \r
149 void SchedulePoint::mouseMoveEvent(QGraphicsSceneMouseEvent *e)\r
150 {\r
151 if(disabled())\r
152 return;\r
153 int oldTemp = temp;\r
154 qreal oldX = pos().x();\r
155 // provide event handler for mouse move\r
156 emit clicked(this);\r
157 if(m_pressed == true)\r
158 {\r
159 if(m_yClickPos - m_weekHeight/4 > e->pos().y())\r
160 {\r
161 m_yClickPos = e->pos().y();\r
162 increaseTemp();\r
163 }\r
164 else if(m_yClickPos + m_weekHeight/4 < e->pos().y())\r
165 {\r
166 m_yClickPos = e->pos().y();\r
167 decreaseTemp();\r
168 }\r
169 \r
170 if(m_xClickPos - m_timeBlockWidth > e->pos().x())\r
171 {\r
172 //m_xClickPos = e->pos().x();\r
173 shiftLeft();\r
174 }\r
175 else if(m_xClickPos + m_timeBlockWidth < e->pos().x())\r
176 {\r
177 //m_xClickPos = e->pos().x();\r
178 shiftRight();\r
179 }\r
180 }\r
181 \r
182 emit(shareAdjustment(temp-oldTemp, pos().x()-oldX));\r
183 \r
184 e->accept();\r
185 }\r
186 \r
187 void SchedulePoint::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)\r
188 {\r
189 if(disabled())\r
190 return;\r
191 setZValue(0);\r
192 pen.setColor(Qt::white);\r
193 e->accept();\r
194 }\r
195 \r
196 int SchedulePoint::roundness(double size) const\r
197 {\r
198 // rounded edges for rectangle\r
199 const int diameter = 12;\r
200 return 100 * diameter / int(size);\r
201 }\r
202 \r
203 \r
204 void SchedulePoint::shiftLeft()\r
205 {\r
206 if(positionToTimeBlocks()<1)\r
207 return;\r
208 // provide a slot to allow point shifting to the left\r
209 location--;\r
210 this->moveBy(-m_timeBlockWidth, 0.0);\r
211 update();\r
212 }\r
213 \r
214 void SchedulePoint::shiftRight()\r
215 {\r
216 if(positionToTimeBlocks()>94)\r
217 return;\r
218 // provide a slot to allow point shifting to the right\r
219 location++;\r
220 this->moveBy(+m_timeBlockWidth, 0.0);\r
221 update();\r
222 }\r
223 \r
224 void SchedulePoint::increaseTemp()\r
225 {\r
226 // provide a slot to increase point temperature\r
227 temp++;\r
228 if(temp>MAXTEMP)temp=MAXTEMP;\r
229 updateUnit();\r
230 }\r
231 \r
232 void SchedulePoint::decreaseTemp()\r
233 {\r
234 // provide a slot to decrease temperature\r
235 temp--;\r
236 if(temp<MINTEMP)temp=MINTEMP;\r
237 updateUnit();\r
238 }\r
239 \r
240 int SchedulePoint::getID()\r
241 {\r
242 // provide point's unique ID number\r
243 return ID;\r
244 }\r
245 \r
246 void SchedulePoint::setID(int seqNumber)\r
247 {\r
248 // give point a unique ID number\r
249 ID = seqNumber;\r
250 }\r
251 \r
252 void SchedulePoint::setLocation(int num)\r
253 {\r
254 // track point's horizontal movement from starting position\r
255 location=num;\r
256 }\r
257 \r
258 int SchedulePoint::getLocation()\r
259 {\r
260 // return how much point has moved from start\r
261 return location;\r
262 }\r
263 \r
264 QString SchedulePoint::time()\r
265 {\r
266 // output the current time represnted by this point's location\r
267 int timeBlockCount = positionToTimeBlocks();\r
268 \r
269 int hours = (timeBlockCount/4.0f);\r
270 int minutes = (timeBlockCount%4) *15;\r
271 \r
272 QTime time(hours%24, minutes);\r
273 return formatTimeString(time, m_globalSettings->timeFormat());\r
274 }\r
275 \r
276 int SchedulePoint::positionToTimeBlocks()\r
277 {\r
278 return qRound((-3*m_timeBlockWidth + pos().x() -m_pointArea.left())/m_timeBlockWidth);\r
279 }\r
280 \r
281 void SchedulePoint::updateUnit()\r
282 {\r
283 myText = formatTemperatureString(temp, m_globalSettings->temperatureFormat());\r
284 update();\r
285 }\r
286 \r
287 void SchedulePoint::setDisabled(bool disabled)\r
288 {\r
289 m_disabled = disabled;\r
290 update();\r
291 }\r
292 \r
293 bool SchedulePoint::disabled()\r
294 {\r
295 return m_disabled;\r
296 }\r
297 \r
298 void SchedulePoint::setSelected(bool selected)\r
299 {\r
300 m_selected = selected;\r
301 if(selected)\r
302 setZValue(10);\r
303 else\r
304 setZValue(0);\r
305 update();\r
306 }\r
307 \r
308 bool SchedulePoint::selected()\r
309 {\r
310 return m_selected;\r
311 }\r
312 \r
313 void SchedulePoint::adjust(int tempAdjust, qreal xPos)\r
314 {\r
315 if(tempAdjust>0)\r
316 increaseTemp();\r
317 if(tempAdjust<0)\r
318 decreaseTemp();\r
319 if(xPos<0)\r
320 shiftLeft();\r
321 if(xPos>0)\r
322 shiftRight();\r
323 }\r