kgantt

xQGanttBarViewPort_Events.cpp
1 //
2 // file : xQGanttBarViewPort_Events.C
3 // date : 11 nov 2000
4 // changed : 27 dec 2000
5 // author : jh
6 //
7 
8 
9 #include "xQGanttBarViewPort.h"
10 #include "xQGanttBarView.h"
11 
12 #include <math.h>
13 
14 KGanttItem* xQGanttBarViewPort::_currentItem;
15 
16 
17 static int _currentMButton;
18 static bool _Mousemoved;
19 static bool _selectItem;
20 
21 static int _timediff;
22 
23 static bool _changeEnd, _changeStart;
24 static int oldw = -1, oldx = -1;
25 
26 TQDateTime _tmpStartDateTime, _tmpEndDateTime;
27 
28 
29 void
30 xQGanttBarViewPort::mousePressEvent(TQMouseEvent* e)
31 {
32  // set _currentItem to pushed mousebutton
33  _currentMButton = e->button();
34  _Mousemoved = false;
35 
36  _startPoint->setX( e->x() );
37  _startPoint->setY( e->y() );
38 
39  _endPoint->setX( e->x() );
40  _endPoint->setY( e->y() );
41 
42  _itemInfo->hide();
43  _itemTextEdit->hide();
44 
45  // right mousebutton & control -> popup menu
46  if(e->button() == TQt::RightButton && e->state() == ControlButton ) {
47  _menu->popup(e->globalPos());
48  return;
49  }
50 
51 
52  /*
53  * get clicked item and position
54  */
55  _currentItem = NULL;
56  Position pos = check(&_currentItem, e->x(), e->y());
57 
58  if(!_currentItem) {
59  unselectAll();
60  return;
61  }
62 
63 
64  /*
65  * edit text
66  */
67  if(e->button() == TQt::MidButton && _mode == Select) {
68 
69  xTQTaskPosition* tp = _gItemList.find(_currentItem);
70  TQPainter p(this);
71 
72  TQRect rect = p.boundingRect(tp->_textPosX,
73  tp->_textPosY, 200,
74  tp->_screenH, AlignLeft, _currentItem->getText() );
75 
76  _itemTextEdit->setText(_currentItem->getText());
77  _itemTextEdit->move(tp->_textPosX, tp->_screenY + _margin + 1);
78  _itemTextEdit->setFixedWidth(rect.width()+40);
79  _itemTextEdit->setFixedHeight(tp->_screenH - 2 * _margin - 2);
80  _itemTextEdit->setFocus();
81 
82  // if item is not editable, _itemTextEdit should be not editable
83  // as well
84  _itemTextEdit->setReadOnly(!_currentItem->isEditable());
85 
86  _itemTextEdit->show();
87 
88  }
89 
90 
91  /*
92  * open/close item, move start, end, item
93  */
94  if(e->button() == TQt::LeftButton && _mode == Select) {
95 
96  _timediff = 0;
97 
98  switch(pos) {
99 
100  case Handle:
101 
102  _currentItem->open( !_currentItem->isOpen() );
103  break;
104 
105  case Center:
106 
107  _changeEnd = true;
108  _changeStart = true;
109 
110  if(e->state() == ShiftButton) {
111 
112  TQString tmp; tmp.sprintf("%s\n", _currentItem->getText().latin1() );
113 
114  tmp += _currentItem->getStart().toString();
115  tmp += " - ";
116  tmp += _currentItem->getEnd().toString();
117 
118  _itemInfo->setText( tmp );
119  _itemInfo->adjustSize();
120 
121  _itemInfo->move(e->x() + 25, _gItemList.find(_currentItem)->_screenY - 50 );
122  _itemInfo->show();
123  }
124  else
125  _selectItem = true;
126 
127  break;
128 
129 
130  case East:
131 
132  _changeEnd = true;
133  _changeStart = false;
134  break;
135 
136 
137  case West:
138 
139  _changeStart = true;
140  _changeEnd = false;
141  break;
142 
143  default :
144  break;
145 
146  }
147 
148 
149 
150  } // if(e->button() == LeftButton && _mode == Select)
151 
152 }
153 
154 
155 
156 void
157 xQGanttBarViewPort::mouseReleaseEvent(TQMouseEvent* e)
158 {
159  switch(_mode) {
160 
161  case Select: {
162 
163  if(_Mousemoved == true) {
164 
165  _itemInfo->hide();
166 
167  if(_changeStart == true || _changeEnd == true) {
168 
169  if(_changeStart == true) {
170  _currentItem->setStart( _tmpStartDateTime );
171  }
172  if(_changeEnd == true) {
173  _currentItem->setEnd( _tmpEndDateTime );
174  }
175 
176  oldx = -1; oldw = -1;
177 
178  recalc();
179  TQWidget::update();
180 
181  }
182  }
183  else {
184  if(_currentItem && _selectItem) {
185 
186 
187  if(e->state() & ControlButton) {
188  _currentItem->select( !_currentItem->isSelected() );
189  }
190  else {
191  bool state = _currentItem->isSelected();
192  unselectAll();
193  _currentItem->select( !state );
194  }
195 
196  TQWidget::update();
197  _selectItem = false;
198 
199  }
200  }
201 
202  _changeEnd = false;
203  _changeStart = false;
204 
205  }
206  break;
207 
208 
209  case Zoom:
210 
211  if(!_Mousemoved) {
212 
213  if(e->button() == TQt::LeftButton)
214  zoom(1.4, e->x(), e->y() );
215 
216 
217  if(e->button() == TQt::RightButton)
218  zoom(0.7, e->x(), e->y() );
219 
220 
221  if(e->button() == TQt::MidButton)
222  zoomAll();
223 
224  }
225  else {
226 
227  if(_currentMButton == TQt::LeftButton) {
228 
229  TQPainter p(this);
230  TQPen pen(DashLine);
231  pen.setColor(red);
232  p.setRasterOp(XorROP);
233  p.setPen( pen );
234 
235  p.drawRect(_startPoint->x(),
236  _startPoint->y(),
237  _endPoint->x()-_startPoint->x(),
238  _endPoint->y() - _startPoint->y());
239 
240  double x1 = _startPoint->x();
241  double y1 = _startPoint->y();
242 
243  double x2 = _endPoint->x();
244  double y2 = _endPoint->y();
245 
246  double sys_width = fabs(x2 - x1);
247 
248  double mass = (_parent->visibleWidth()/ sys_width);
249 
250  zoom(mass, (int) (x1+(x2-x1)/2), (int) (y1+(y2-y1)/2) );
251 
252 
253  }
254  }
255 
256  break;
257 
258 
259  default:
260  break;
261 
262  }
263 
264  _Mousemoved = false;
265  _currentMButton = 0;
266 
267 }
268 
269 
270 
271 void
272 xQGanttBarViewPort::mouseMoveEvent(TQMouseEvent* e)
273 {
274  if(fabs((float)(_startPoint->x() - e->x())) < 2 &&
275  fabs((float)(_startPoint->y() - e->y())) < 2 )
276  return;
277 
278  static TQPen _dashPen(TQColor(255,0,0),DashLine);
279  static TQPen _solidPen(TQColor(200,200,200));
280 
281  _Mousemoved = true;
282 
283  switch(_mode) {
284 
285  case Select: {
286 
287  if(_currentMButton == TQt::LeftButton && _currentItem) {
288 
289  TQPainter p(this);
290  p.setRasterOp(XorROP);
291  // TQPen pen(DashLine);
292  // pen.setColor(red);
293  p.setPen( _dashPen );
294 
295  TQString stmp;
296  stmp.sprintf("%s\n", _currentItem->getText().latin1() );
297 
298  int pixeldiff = e->x() - _startPoint->x();
299  _timediff = (int) ((double) pixeldiff / _scaleX + 0.5 );
300 
301  xTQTaskPosition* tpos = _gItemList[_currentItem];
302 
303  int x = tpos->_screenX; int w = tpos->_screenW;
304 
305  if(_changeStart && _changeEnd) {
306  double tmp = (double) _timediff/(double) _snapgrid;
307  _timediff = ((int) (tmp + sgn(tmp) * 0.5)) * _snapgrid;
308  stmp += _currentItem->getStart().addSecs(_timediff*60).toString();
309  stmp += " - ";
310  stmp += _currentItem->getEnd().addSecs(_timediff*60).toString();
311  x += (int) (_timediff * _scaleX);
312 
313  _tmpStartDateTime = _currentItem->getStart().addSecs(_timediff*60);
314  _tmpEndDateTime = _currentItem->getEnd().addSecs(_timediff*60);
315 
316  }
317  else {
318 
319  if(_changeStart) {
320 
321  TQDateTime movedStart( _currentItem->getStart().addSecs(_timediff*60) );
322 
323  _tmpStartDateTime.setDate( movedStart.date() );
324  _tmpStartDateTime.setTime(TQTime(0,0,0,0));
325 
326  double diff = _tmpStartDateTime.secsTo(movedStart)/60;
327 
328  double tmp = diff/(double) _snapgrid;
329  _timediff = ((int) (tmp + sgn(tmp) * 0.5)) * _snapgrid;
330 
331  _tmpStartDateTime = _tmpStartDateTime.addSecs(_timediff*60);
332  _timediff = _currentItem->getStart().secsTo(_tmpStartDateTime)/60;
333 
334  stmp += _tmpStartDateTime.toString().latin1();
335  stmp += " - ";
336  stmp += _currentItem->getEnd().toString();
337 
338  x += (int) (_timediff * _scaleX);
339  w -= (int) (_timediff * _scaleX);
340  }
341 
342  if(_changeEnd) {
343 
344  TQDateTime movedEnd( _currentItem->getEnd().addSecs(_timediff*60) );
345 
346  _tmpEndDateTime.setDate( movedEnd.date() );
347  _tmpEndDateTime.setTime(TQTime(0,0,0,0));
348 
349  double diff = _tmpEndDateTime.secsTo(movedEnd)/60;
350 
351  double tmp = diff/(double) _snapgrid;
352  _timediff = ((int) (tmp + sgn(tmp) * 0.5)) * _snapgrid;
353 
354  _tmpEndDateTime = _tmpEndDateTime.addSecs(_timediff*60);
355  _timediff = _currentItem->getEnd().secsTo(_tmpEndDateTime)/60;
356 
357  stmp += _currentItem->getStart().toString();
358  stmp += " - ";
359  stmp += _tmpEndDateTime.toString().latin1();
360 
361  w += (int) (_timediff * _scaleX);
362 
363  }
364 
365  }
366 
367  _itemInfo->setText( stmp );
368  _itemInfo->adjustSize();
369  _itemInfo->move(e->x() + 25, _gItemList.find(_currentItem)->_screenY - 50);
370  _itemInfo->show();
371 
372  if(oldx > 0) {
373  p.fillRect(oldx, _gItemList.find(_currentItem)->_screenY,
374  oldw, _gItemList.find(_currentItem)->_screenH,
375  TQBrush(TQColor(50,50,50), Dense4Pattern));
376  p.drawRect(oldx, _gItemList.find(_currentItem)->_screenY,
377  oldw, _gItemList.find(_currentItem)->_screenH);
378 
379  p.setPen(_solidPen);
380  if(_changeStart)
381  p.drawLine(oldx, 0, oldx, height());
382  if(oldw > 2)
383  if(_changeEnd)
384  p.drawLine(oldx + oldw, 0, oldx + oldw, height());
385 
386  }
387 
388  p.setPen(_dashPen);
389  p.fillRect(x, _gItemList.find(_currentItem)->_screenY,
390  w, _gItemList.find(_currentItem)->_screenH,
391  TQBrush(TQColor(50,50,50), Dense4Pattern) );
392  p.drawRect(x, _gItemList.find(_currentItem)->_screenY,
393  w, _gItemList.find(_currentItem)->_screenH);
394 
395  p.setPen(_solidPen);
396  if(_changeStart)
397  p.drawLine(x, 0, x, height());
398 
399  if(w>2)
400  if(_changeEnd)
401  p.drawLine(x + w, 0, x + w, height());
402 
403  oldx = x; oldw = w;
404 
405  }
406  else {
407 
408  static Position _pos = Outside;
409 
410  KGanttItem* item = NULL;
411 
412  Position pos = check(&item, e->x(), e->y());
413 
414  if(_pos != pos) {
415 
416  _pos = pos;
417 
418  if(pos == West || pos == East) {
419  setCursor( splitHCursor );
420  break;
421  }
422  if(pos == North || pos == South) {
423  setCursor( splitVCursor );
424  break;
425  }
426  if(pos == Center) {
427  setCursor( upArrowCursor);
428  break;
429  }
430  if(pos == Handle) {
431  setCursor(pointingHandCursor);
432  break;
433  }
434 
435  setCursor(arrowCursor);
436 
437  }
438  }
439  }
440  break;
441 
442 
443  case Zoom: {
444 
445  if(_currentMButton == TQt::LeftButton) {
446 
447  static TQString strpos;
448 
449  strpos = "";
450 
451  int s = worldX(_startPoint->x());
452  TQDateTime d1 = _toplevelitem->getStart().addSecs(s*60);
453 
454  s = worldX(e->x());
455  TQDateTime d2 = _toplevelitem->getStart().addSecs(s*60);
456 
457  strpos += d1.date().toString();
458  strpos += " - ";
459  strpos += d2.date().toString();
460 
461  emit message(strpos);
462 
463  TQPainter p(this);
464  TQPen pen(DashLine);
465  pen.setColor(red);
466 
467  p.setRasterOp(XorROP);
468 
469  p.setPen( pen );
470 
471  p.drawRect(_startPoint->x(),
472  _startPoint->y(),
473  _endPoint->x()-_startPoint->x(),
474  _endPoint->y() - _startPoint->y());
475 
476  TQBrush _selectedbrush( TQColor(50,50,50), Dense4Pattern );
477 
478  p.fillRect( _startPoint->x(), _startPoint->y(),
479  _endPoint->x()-_startPoint->x(), _endPoint->y() - _startPoint->y(),
480  _selectedbrush );
481 
482  _endPoint->setX( e->x() );
483  _endPoint->setY( e->y() );
484 
485 
486  p.drawRect(_startPoint->x(), _startPoint->y(),
487  _endPoint->x()-_startPoint->x(), _endPoint->y() - _startPoint->y());
488 
489  p.fillRect( _startPoint->x(), _startPoint->y(),
490  _endPoint->x()-_startPoint->x(), _endPoint->y() - _startPoint->y(),
491  _selectedbrush );
492  }
493 
494  }
495 
496  break;
497 
498  case Move: {
499  emit scroll(_startPoint->x() - e->x(), _startPoint->y() - e->y() );
500  }
501  break;
502 
503 
504  default :
505  break;
506 
507  }
508 }
509 
510 
511 void
512 xQGanttBarViewPort::keyPressEvent(TQKeyEvent* e)
513 {
514 
515  printf("xQGanttBarViewPort::keyPressEvent() key = %d \n", e->key() );
516 
517  int dx = 15;
518 
519  if(e->state() == ControlButton)
520  dx *= 10;
521 
522  switch(e->key()) {
523 
524  case Key_Left:
525 
526  emit scroll(-dx,0);
527  break;
528 
529  case Key_Right:
530 
531  emit scroll(dx,0);
532  break;
533 
534  case Key_Up:
535 
536  emit scroll(0,-dx);
537  break;
538 
539  case Key_Down:
540 
541  emit scroll(0, dx);
542  break;
543 
544  case 43: // +
545 
546  zoom(1.4);
547  break;
548 
549  case 45: // -
550 
551  zoom(0.7);
552  break;
553 
554  case 4103: // del
555 
556  deleteSelectedItems();
557  break;
558 
559  case 4102: // einfg
560 
561  insertIntoSelectedItem();
562  break;
563 
564  case 4119: // bild v
565 
566  emit scroll(0, dx*15);
567  break;
568 
569  case 4118: // bild ^
570 
571  emit scroll(0,-dx*15);
572  break;
573 
574  }
575 
576 }
577 
578 
579 void
580 xQGanttBarViewPort::paintEvent(TQPaintEvent * e)
581 {
583  update(e->rect().left(), e->rect().top(),
584  e->rect().right(), e->rect().bottom() );
585 }
KGanttItem.
Definition: KGanttItem.h:56
TQDateTime getStart()
Get date of starting.
Definition: KGanttItem.cpp:208
void setEnd(const TQDateTime &end)
Set time/date of end.
Definition: KGanttItem.cpp:269
void select(bool f)
Select/unselect item.
Definition: KGanttItem.cpp:450
bool isOpen()
Returns true if item is open (subitems has to be drawn)
Definition: KGanttItem.h:178
TQDateTime getEnd()
Get date of ending.
Definition: KGanttItem.cpp:220
bool isEditable()
Returns if item is editable.
Definition: KGanttItem.h:209
TQString getText()
Get text.
Definition: KGanttItem.h:340
void setStart(const TQDateTime &start)
Set time/date of start.
Definition: KGanttItem.cpp:232
void open(bool f)
Open / Close item.
Definition: KGanttItem.cpp:435
bool isSelected()
Returns true if item is selected.
Definition: KGanttItem.h:219
void update(int x1, int y1, int x2, int y2)
Update widget.