• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

superkaramba

  • sources
  • kde-4.12
  • kdeutils
  • superkaramba
  • src
  • meters
meters/input.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright (c) 2005 Alexander Wiedenbruch <mail@wiedenbruch.de>
3  * Copyright (c) 2007 Matt Broadstone <mbroadst@gmail.com>
4  *
5  * This file is part of SuperKaramba.
6  *
7  * SuperKaramba is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * SuperKaramba is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with SuperKaramba; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  ****************************************************************************/
21 
22 #include "input.h"
23 #include "input.moc"
24 
25 #include <QPainter>
26 #include <QStyleOptionGraphicsItem>
27 #include <QGraphicsSceneMouseEvent>
28 #include <QCursor>
29 
30 #include "textfield.h"
31 
32 Input::Input(Karamba* k, int x, int y, int w, int h)
33  : Meter(k, x, y, w, h),
34  m_selectionColor(128, 128, 128),
35  m_hscroll(0),
36  m_cursorPos(0),
37  m_cursorVisible(true),
38  m_mouseMoved(false),
39  m_selStart(-1),
40  m_selLength(0)
41 {
42  setFlags(QGraphicsItem::ItemIsFocusable);
43 
44  setCursor(QCursor(Qt::IBeamCursor));
45 
46  connect(&m_cursorTimer, SIGNAL(timeout()), (QObject*)this, SLOT(blinkCursor()));
47  m_cursorTimer.start(1000);
48 }
49 
50 Input::~Input()
51 {}
52 
53 /*
54  Some code in this method is copied from QLineEdit.
55  Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.
56 */
57 void Input::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
58  QWidget *widget)
59 {
60  Q_UNUSED(option);
61  Q_UNUSED(widget);
62 
63  painter->setPen(m_fgColor);
64  QBrush oldBrush = painter->brush();
65  painter->setBrush(m_bgColor);
66  painter->drawRect(boundingRect());
67  painter->setBrush(oldBrush);
68 
69  QTextLine line = m_textLayout.lineAt(0);
70 
71  int widthUsed = qRound(line.naturalTextWidth()) + 1 + 4;
72 
73  QFontMetrics fm(m_font);
74  QRectF innerRect(boundingRect().x() + 2, boundingRect().y(),
75  boundingRect().width() - 4, boundingRect().height());
76  painter->setClipRect(innerRect);
77 
78  QPointF topLeft = innerRect.topLeft();
79 
80  double curPos = line.cursorToX(m_cursorPos);
81  if (4 + widthUsed <= innerRect.width()) {
82  m_hscroll = 0;
83  } else if (curPos - m_hscroll >= innerRect.width()) {
84  m_hscroll = curPos - innerRect.width() + 1;
85  } else if (curPos - m_hscroll < 0) {
86  m_hscroll = curPos;
87  } else if (widthUsed - m_hscroll < innerRect.width()) {
88  m_hscroll = widthUsed - innerRect.width() + 1;
89  }
90  topLeft.rx() -= m_hscroll;
91  topLeft.ry() += (boundingRect().height() - line.height()) / 2;
92 
93  painter->setPen(m_fontColor);
94  m_textLayout.draw(painter, topLeft, m_selection, innerRect);
95 
96  if (hasFocus() && m_cursorVisible)
97  m_textLayout.drawCursor(painter, topLeft, m_cursorPos);
98 }
99 
100 void Input::mouseEvent(QEvent *e)
101 {
102  QPointF pos;
103  if (QGraphicsSceneMouseEvent *event = dynamic_cast<QGraphicsSceneMouseEvent*>(e)) {
104  pos = event->pos();
105  } else if (QGraphicsSceneWheelEvent *event = dynamic_cast<QGraphicsSceneWheelEvent*>(e)) {
106  pos = event->pos();
107  }
108 
109  QTextLine line = m_textLayout.lineAt(0);
110 
111  QPoint mappedPos = mapFromParent(pos).toPoint();
112  m_cursorPos = line.xToCursor(mappedPos.x() - 2 + m_hscroll);
113  m_cursorVisible = true;
114 
115  m_selStart = m_cursorPos;
116 
117  update();
118 }
119 
120 void Input::mouseEventRelease(QGraphicsSceneMouseEvent *e)
121 {
122  if (m_mouseMoved) {
123  QTextLine line = m_textLayout.lineAt(0);
124  QPoint mappedPos = mapFromParent(e->pos()).toPoint();
125  int selEnd = line.xToCursor(mappedPos.x() - 2 + m_hscroll);
126  if (m_selStart > selEnd) {
127  m_selLength = m_selStart - selEnd;
128  m_selStart = selEnd;
129  m_cursorPos = selEnd;
130  } else {
131  m_selLength = selEnd - m_selStart;
132  m_cursorPos = m_selStart + m_selLength;
133  }
134  m_mouseMoved = false;
135 
136  m_selection.clear();
137  QTextLayout::FormatRange selection;
138  selection.format.setBackground(m_selectionColor);
139  selection.format.setForeground(m_selectedTextColor);
140  selection.start = m_selStart;
141  selection.length = m_selLength;
142  m_selection << selection;
143 
144  update();
145  } else {
146  m_selStart = -1;
147  m_selLength = 0;
148  }
149 }
150 
151 void Input::mouseEventMove(QGraphicsSceneHoverEvent *e)
152 {
153  Q_UNUSED(e);
154  m_mouseMoved = true;
155 }
156 
157 void Input::focusOutEvent(QFocusEvent *event)
158 {
159  Q_UNUSED(event);
160 
161  m_cursorTimer.stop();
162  update();
163 }
164 
165 void Input::blinkCursor()
166 {
167  m_cursorVisible = !m_cursorVisible;
168  update();
169 }
170 
171 void Input::keyPress(QKeyEvent *event)
172 {
173  bool append = true;
174  bool newSelection = false;
175  bool clearSelection = false;
176 
177  switch (event->key()) {
178  case Qt::Key_Backspace:
179  if (m_selLength == 0) {
180  if (m_cursorPos > 0) {
181  m_text.remove(m_cursorPos - 1, 1);
182  m_cursorPos--;
183  }
184  } else {
185  m_text.remove(m_selStart, m_selLength);
186  m_cursorPos = m_selStart;
187  m_selStart = -1;
188  m_selLength = 0;
189  m_selection.clear();
190  }
191  append = false;
192  break;
193 
194  case Qt::Key_Delete:
195  if (m_selLength == 0) {
196  if (m_cursorPos >= 0) {
197  m_text.remove(m_cursorPos, 1);
198  }
199  } else {
200  m_text.remove(m_selStart, m_selLength);
201  m_cursorPos = m_selStart;
202  m_selStart = -1;
203  m_selLength = 0;
204  m_selection.clear();
205  }
206  append = false;
207  break;
208 
209  case Qt::Key_Left:
210  m_cursorPos--;
211  if (event->modifiers() == Qt::ShiftModifier) {
212  if (m_cursorPos != -1) {
213  if (m_selLength > 0) {
214  if (m_cursorPos + 1 == m_selStart) {
215  m_selStart--;
216  m_selLength += 1;
217  } else {
218  m_selLength -= 1;
219  }
220  } else {
221  m_selStart = m_cursorPos;
222  m_selLength += 1;
223  }
224  newSelection = true;
225  }
226  } else {
227  clearSelection = true;
228  }
229  append = false;
230  break;
231 
232  case Qt::Key_Right:
233  m_cursorPos++;
234  if (event->modifiers() == Qt::ShiftModifier) {
235  if (m_cursorPos != m_text.length()+1) {
236  if (m_selLength > 0) {
237  if (m_cursorPos - 1 == m_selStart) {
238  m_selStart++;
239  m_selLength -= 1;
240  } else {
241  m_selLength += 1;
242  }
243  } else {
244  m_selStart = m_cursorPos-1;
245  m_selLength += 1;
246  }
247  newSelection = true;
248  }
249  } else {
250  clearSelection = true;
251  }
252  append = false;
253  break;
254 
255  case Qt::Key_Home:
256  {
257  int oldCursorPos = m_cursorPos;
258  m_cursorPos = 0;
259  if (event->modifiers() == Qt::ShiftModifier) {
260  m_selStart = 0;
261  m_selLength = oldCursorPos;
262  newSelection = true;
263  }
264  append = false;
265  }
266  break;
267 
268  case Qt::Key_End:
269  {
270  int oldCursorPos = m_cursorPos;
271  m_cursorPos = m_text.length();
272  if (event->modifiers() == Qt::ShiftModifier) {
273  m_selStart = oldCursorPos;
274  m_selLength = m_cursorPos - oldCursorPos;
275  newSelection = true;
276  }
277  append = false;
278  }
279  break;
280 
281  case Qt::Key_Enter:
282  case Qt::Key_Return:
283  clearSelection = true;
284  append = false;
285  break;
286  }
287 
288  if (append) {
289  if (m_selLength == 0) {
290  m_text.insert(m_cursorPos, event->text());
291  m_cursorPos += event->text().length();
292  } else {
293  if (event->text().length() > 0) {
294  m_text.remove(m_selStart, m_selLength);
295  m_text.insert(m_selStart, event->text());
296  m_cursorPos = m_selStart + event->text().length();
297  clearSelection = true;
298  }
299  }
300  }
301 
302  if (clearSelection) {
303  m_selection.clear();
304  m_selStart = -1;
305  m_selLength = 0;
306  }
307 
308  if (newSelection) {
309  m_selection.clear();
310  QTextLayout::FormatRange selection;
311  selection.format.setBackground(m_selectionColor);
312  selection.format.setForeground(m_selectedTextColor);
313  selection.start = m_selStart;
314  selection.length = m_selLength;
315  m_selection << selection;
316  }
317 
318  if (m_cursorPos < 0)
319  m_cursorPos = 0;
320  if (m_cursorPos > m_text.length())
321  m_cursorPos = m_text.length();
322 
323  m_cursorVisible = true;
324 
325  layoutText();
326 }
327 
328 
329 void Input::setValue(const QString &text)
330 {
331  m_text = text;
332 
333  layoutText();
334 }
335 
336 QString Input::getStringValue() const
337 {
338  return m_text;
339 }
340 
341 void Input::setBGColor(QColor c)
342 {
343  m_bgColor = c;
344  update();
345 }
346 
347 void Input::setColor(QColor c)
348 {
349  m_fgColor = c;
350  update();
351 }
352 
353 QColor Input::getBGColor() const
354 {
355  return m_bgColor;
356 }
357 
358 QColor Input::getColor() const
359 {
360  return m_fgColor;
361 }
362 
363 void Input::hide()
364 {
365  Meter::hide();
366 }
367 
368 void Input::show()
369 {
370  Meter::show();
371 }
372 
373 void Input::setSize(int ix, int iy, int iw, int ih)
374 {
375  Meter::setSize(ix, iy, iw, ih);
376 }
377 
378 void Input::setX(int ix)
379 {
380  Meter::setX(ix);
381 }
382 
383 void Input::setY(int iy)
384 {
385  Meter::setY(iy);
386 }
387 
388 void Input::setWidth(int iw)
389 {
390  Meter::setWidth(iw);
391 }
392 
393 void Input::setHeight(int ih)
394 {
395  Meter::setHeight(ih);
396 }
397 
398 void Input::setFont(const QString &f)
399 {
400  m_font.setFamily(f);
401 
402  layoutText();
403 }
404 
405 QString Input::getFont() const
406 {
407  return m_font.family();
408 }
409 
410 void Input::setFontColor(QColor fontColor)
411 {
412  m_fontColor = fontColor;
413  update();
414 }
415 
416 QColor Input::getFontColor() const
417 {
418  return m_fontColor;
419 }
420 
421 void Input::setSelectionColor(QColor selectionColor)
422 {
423  m_selectionColor = selectionColor;
424  update();
425 }
426 
427 QColor Input::getSelectionColor() const
428 {
429  return m_selectionColor;
430 }
431 
432 void Input::setSelectedTextColor(QColor selectedTextColor)
433 {
434  m_selectedTextColor = selectedTextColor;
435 }
436 
437 QColor Input::getSelectedTextColor() const
438 {
439  return m_selectedTextColor;
440 }
441 
442 void Input::setFontSize(int size)
443 {
444  m_font.setPixelSize(size);
445 
446  layoutText();
447 }
448 
449 int Input::getFontSize() const
450 {
451  return m_font.pixelSize();
452 }
453 
454 void Input::setTextProps(TextField* t)
455 {
456  if (t) {
457  setFontSize(t->getFontSize());
458  setFont(t->getFont());
459  setColor(t->getColor());
460  setBGColor(t->getBGColor());
461  }
462 
463  layoutText();
464 }
465 
466 void Input::setInputFocus()
467 {
468  setFocus();
469  update();
470 }
471 
472 void Input::clearInputFocus()
473 {
474  clearFocus();
475  update();
476 }
477 
478 void Input::layoutText()
479 {
480  m_textLayout.setText(m_text);
481  m_textLayout.setFont(m_font);
482 
483  m_textLayout.beginLayout();
484  QTextLine line = m_textLayout.createLine();
485  line.setPosition(QPointF(0, 0));
486  m_textLayout.endLayout();
487 
488  update();
489 }
490 
491 int Input::getTextWidth() const
492 {
493  QTextLine line = m_textLayout.lineAt(0);
494  if (line.isValid()) {
495  return static_cast<int>(line.naturalTextWidth());
496  }
497  return -1;
498 }
499 
500 void Input::setSelection(int start, int length)
501 {
502  m_selStart = start;
503  m_selLength = length;
504 
505  m_selection.clear();
506  QTextLayout::FormatRange selection;
507  selection.format.setBackground(m_selectionColor);
508  selection.format.setForeground(m_selectedTextColor);
509  selection.start = m_selStart;
510  selection.length = m_selLength;
511  m_selection << selection;
512 
513  update();
514 }
515 
516 void Input::clearSelection()
517 {
518  setSelection(-1, 0);
519 }
520 
521 QTextLayout::FormatRange Input::getSelection() const
522 {
523  QTextLayout::FormatRange selection;
524  selection.start = m_selStart;
525  selection.length = m_selLength;
526 
527  return selection;
528 }
Input::Input
Input()
input.h
Input::setColor
void setColor(QColor c)
Definition: meters/input.cpp:347
Input::show
void show()
Definition: meters/input.cpp:368
TextField::getFontSize
int getFontSize() const
Definition: textfield.cpp:99
Input::hide
void hide()
Definition: meters/input.cpp:363
Meter::boundingRect
virtual QRectF boundingRect() const
Definition: meters/meter.cpp:193
Input::mouseEventRelease
void mouseEventRelease(QGraphicsSceneMouseEvent *e)
Definition: meters/input.cpp:120
TextField::getFont
QString getFont() const
Definition: textfield.cpp:88
Meter::setSize
virtual void setSize(int x, int y, int width, int height)
Definition: meters/meter.cpp:159
Input::getFont
QString getFont() const
Definition: meters/input.cpp:405
QWidget
Input::keyPress
void keyPress(QKeyEvent *event)
Definition: meters/input.cpp:171
Input::setFontSize
void setFontSize(int size)
Definition: meters/input.cpp:442
Input::setSelectedTextColor
void setSelectedTextColor(QColor selectedTextColor)
Definition: meters/input.cpp:432
Meter::setHeight
virtual void setHeight(int)
Definition: meters/meter.cpp:102
Input::focusOutEvent
void focusOutEvent(QFocusEvent *event)
Definition: meters/input.cpp:157
TextField::getColor
QColor getColor() const
Definition: textfield.cpp:65
Meter::show
virtual void show()
Definition: meters/meter.cpp:179
Input::setFont
void setFont(const QString &f)
Definition: meters/input.cpp:398
Input::getSelectionColor
QColor getSelectionColor() const
Definition: meters/input.cpp:427
Input::getStringValue
QString getStringValue() const
Definition: meters/input.cpp:336
QObject
Input::setFontColor
void setFontColor(QColor fontColor)
Definition: meters/input.cpp:410
Input::setInputFocus
void setInputFocus()
Definition: meters/input.cpp:466
TextField::getBGColor
QColor getBGColor() const
Definition: textfield.cpp:75
Input::getBGColor
QColor getBGColor() const
Definition: meters/input.cpp:353
Input::mouseEventMove
void mouseEventMove(QGraphicsSceneHoverEvent *e)
Definition: meters/input.cpp:151
Input::mouseEvent
void mouseEvent(QEvent *e)
Definition: meters/input.cpp:100
Input::setY
void setY(int iy)
Definition: meters/input.cpp:383
Input::setTextProps
void setTextProps(TextField *)
Definition: meters/input.cpp:454
Input::setWidth
void setWidth(int iw)
Definition: meters/input.cpp:388
Karamba
Definition: karamba.h:52
Input::setValue
void setValue(const QString &text)
Definition: meters/input.cpp:329
Input::getFontSize
int getFontSize() const
Definition: meters/input.cpp:449
Input::setSelectionColor
void setSelectionColor(QColor selectionColor)
Definition: meters/input.cpp:421
Input::clearInputFocus
void clearInputFocus()
Definition: meters/input.cpp:472
textfield.h
Input::getTextWidth
int getTextWidth() const
Definition: meters/input.cpp:491
Meter::setWidth
virtual void setWidth(int)
Definition: meters/meter.cpp:88
Meter::hide
virtual void hide()
Definition: meters/meter.cpp:186
Input::getFontColor
QColor getFontColor() const
Definition: meters/input.cpp:416
Input::setX
void setX(int ix)
Definition: meters/input.cpp:378
Input::getSelectedTextColor
QColor getSelectedTextColor() const
Definition: meters/input.cpp:437
Meter::setX
virtual void setX(int)
Definition: meters/meter.cpp:60
Input::getColor
QColor getColor() const
Definition: meters/input.cpp:358
Input::clearSelection
void clearSelection()
Definition: meters/input.cpp:516
Meter
Definition: meters/meter.h:23
Input::getSelection
QTextLayout::FormatRange getSelection() const
Definition: meters/input.cpp:521
Input::setBGColor
void setBGColor(QColor c)
Definition: meters/input.cpp:341
Input::setHeight
void setHeight(int ih)
Definition: meters/input.cpp:393
Input::setSize
void setSize(int ix, int iy, int iw, int ih)
Definition: meters/input.cpp:373
TextField
Ralph M.
Definition: textfield.h:22
Input::~Input
~Input()
Definition: meters/input.cpp:50
Meter::setY
virtual void setY(int)
Definition: meters/meter.cpp:74
Input::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Definition: meters/input.cpp:57
Input::setSelection
void setSelection(int start, int length)
Definition: meters/input.cpp:500
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:07:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

superkaramba

Skip menu "superkaramba"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal