int DateTimeEditor::getNearestEditablePosition(const QPoint& pos) const { int minDist = -1; int bestPosition = -1; for (const CharacterInfo& c : m_characters) { if (c.isEditable()) { const QRect r = getCharacterRect(c.m_position); int dist = -1; if (r.contains(pos)) { dist = 0; } else { if (pos.x() <= r.left()) { const int d = r.left() - pos.x(); if (dist < 0 || d < dist) dist = d; } else if (pos.x() >= r.right()) { const int d = pos.x() - r.right(); if (dist < 0 || d < dist) dist = d; } if (pos.y() <= r.top()) { const int d = r.top() - pos.y(); if (dist < 0 || d < dist) dist = d; } else if (pos.y() >= r.bottom()) { const int d = pos.y() - r.bottom(); if (dist < 0 || d < dist) dist = d; } } if (minDist < 0 || dist < minDist) { minDist = dist; bestPosition = c.m_position; } } } return bestPosition; }
// blockText.left(ex.matchedLength()), // formats, // leadingSpace, // dummy, dummy, dummy); // qDebug() << "leading space is: " << leadingSpace; // } // } QFont font(blockFont.top()); // drawing with a 1pt font will freak out Windows: font.setPointSizeF(100.0); font.setBold(blockBold.top()); font.setItalic(blockItalic.top()); // bounding boxes for 1.0 height font: QRectF boxA = getCharacterRect(font, 'A'); QRectF boxG = getCharacterRect(font, 'g'); double heightA = boxA.height() * 100.0; // scale to scale from 100.0pt font to a font size where an 'A' is // exactly 1 unit tall: double ttfScale = 1.0 / heightA; descent = (boxA.bottom()*100 - boxG.bottom()*100) * ttfScale; QFontMetricsF fm(font); ascent = fm.ascent() * ttfScale; // empty text, e.g. line feed only: if (blockText=="") { horizontalAdvance = 0.0; return QList<RPainterPath>();
void DateTimeEditor::paintEvent(QPaintEvent* /*event*/) { QPainter painter(this); QStyleOptionFrame panel; initStyleOption(&panel); style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, &painter, this); QRect r = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this); const int margin = 2; r.setX(r.x() + margin); r.setY(r.y() + margin); r.setRight(r.right() - margin); r.setBottom(r.bottom() - margin); painter.setClipRect(r); QPalette pal = style()->standardPalette(); if (m_selectedPosition >= 0) { const GroupInfo& g = m_groups[m_selectedGroup]; for (auto p: g.positions) { const QRect r = getCharacterRect(p); painter.fillRect(r, pal.alternateBase()); } const QRect r = getCharacterRect(m_selectedPosition); if (g.invalid) { painter.fillRect(r, Qt::red); } else { painter.fillRect(r, pal.highlight()); } } painter.setRenderHint(QPainter::Antialiasing); for (const CharacterInfo& c : m_characters) { const QRect r = getCharacterRect(c.m_position); if (c.m_position == m_selectedPosition) { painter.setPen(pal.highlightedText().color()); } else { const GroupInfo& g = m_groups[c.m_group]; if (g.invalid) { painter.setPen(Qt::red); } else { painter.setPen(pal.text().color()); } } painter.drawText(r, Qt::AlignCenter, c.m_character); } }