26 #include <QStyleOptionGraphicsItem>
27 #include <QGraphicsSceneMouseEvent>
33 :
Meter(k, x, y, w, h),
34 m_selectionColor(128, 128, 128),
37 m_cursorVisible(true),
42 setFlags(QGraphicsItem::ItemIsFocusable);
44 setCursor(QCursor(Qt::IBeamCursor));
46 connect(&m_cursorTimer, SIGNAL(timeout()), (
QObject*)
this, SLOT(blinkCursor()));
47 m_cursorTimer.start(1000);
57 void Input::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
63 painter->setPen(m_fgColor);
64 QBrush oldBrush = painter->brush();
65 painter->setBrush(m_bgColor);
67 painter->setBrush(oldBrush);
69 QTextLine line = m_textLayout.lineAt(0);
71 int widthUsed = qRound(line.naturalTextWidth()) + 1 + 4;
73 QFontMetrics fm(m_font);
76 painter->setClipRect(innerRect);
78 QPointF topLeft = innerRect.topLeft();
80 double curPos = line.cursorToX(m_cursorPos);
81 if (4 + widthUsed <= innerRect.width()) {
83 }
else if (curPos - m_hscroll >= innerRect.width()) {
84 m_hscroll = curPos - innerRect.width() + 1;
85 }
else if (curPos - m_hscroll < 0) {
87 }
else if (widthUsed - m_hscroll < innerRect.width()) {
88 m_hscroll = widthUsed - innerRect.width() + 1;
90 topLeft.rx() -= m_hscroll;
91 topLeft.ry() += (
boundingRect().height() - line.height()) / 2;
93 painter->setPen(m_fontColor);
94 m_textLayout.draw(painter, topLeft, m_selection, innerRect);
96 if (hasFocus() && m_cursorVisible)
97 m_textLayout.drawCursor(painter, topLeft, m_cursorPos);
103 if (QGraphicsSceneMouseEvent *event = dynamic_cast<QGraphicsSceneMouseEvent*>(e)) {
105 }
else if (QGraphicsSceneWheelEvent *event = dynamic_cast<QGraphicsSceneWheelEvent*>(e)) {
109 QTextLine line = m_textLayout.lineAt(0);
111 QPoint mappedPos = mapFromParent(pos).toPoint();
112 m_cursorPos = line.xToCursor(mappedPos.x() - 2 + m_hscroll);
113 m_cursorVisible =
true;
115 m_selStart = m_cursorPos;
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;
129 m_cursorPos = selEnd;
131 m_selLength = selEnd - m_selStart;
132 m_cursorPos = m_selStart + m_selLength;
134 m_mouseMoved =
false;
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;
161 m_cursorTimer.stop();
165 void Input::blinkCursor()
167 m_cursorVisible = !m_cursorVisible;
174 bool newSelection =
false;
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);
185 m_text.remove(m_selStart, m_selLength);
186 m_cursorPos = m_selStart;
195 if (m_selLength == 0) {
196 if (m_cursorPos >= 0) {
197 m_text.remove(m_cursorPos, 1);
200 m_text.remove(m_selStart, m_selLength);
201 m_cursorPos = m_selStart;
211 if (event->modifiers() == Qt::ShiftModifier) {
212 if (m_cursorPos != -1) {
213 if (m_selLength > 0) {
214 if (m_cursorPos + 1 == m_selStart) {
221 m_selStart = m_cursorPos;
227 clearSelection =
true;
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) {
244 m_selStart = m_cursorPos-1;
250 clearSelection =
true;
257 int oldCursorPos = m_cursorPos;
259 if (event->modifiers() == Qt::ShiftModifier) {
261 m_selLength = oldCursorPos;
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;
283 clearSelection =
true;
289 if (m_selLength == 0) {
290 m_text.insert(m_cursorPos, event->text());
291 m_cursorPos +=
event->text().length();
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;
302 if (clearSelection) {
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;
320 if (m_cursorPos > m_text.length())
321 m_cursorPos = m_text.length();
323 m_cursorVisible =
true;
407 return m_font.family();
412 m_fontColor = fontColor;
423 m_selectionColor = selectionColor;
429 return m_selectionColor;
434 m_selectedTextColor = selectedTextColor;
439 return m_selectedTextColor;
444 m_font.setPixelSize(size);
451 return m_font.pixelSize();
478 void Input::layoutText()
480 m_textLayout.setText(m_text);
481 m_textLayout.setFont(m_font);
483 m_textLayout.beginLayout();
484 QTextLine line = m_textLayout.createLine();
485 line.setPosition(QPointF(0, 0));
486 m_textLayout.endLayout();
493 QTextLine line = m_textLayout.lineAt(0);
494 if (line.isValid()) {
495 return static_cast<int>(line.naturalTextWidth());
503 m_selLength = length;
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;
523 QTextLayout::FormatRange selection;
524 selection.start = m_selStart;
525 selection.length = m_selLength;
virtual QRectF boundingRect() const
virtual void setSize(int x, int y, int width, int height)
virtual void setHeight(int)
QColor getBGColor() const
virtual void setWidth(int)