guck

Vim-like QtWebEngine-powered browser, written in C++
git clone git://git.deurzen.net/guck
Log | Files | Refs | LICENSE

elidedlabel.cc (1337B)


      1 #include "elidedlabel.hh"
      2 
      3 #include <QPainter>
      4 #include <QTextLayout>
      5 
      6 
      7 void
      8 elidedlabel_t::setText(const QString& text)
      9 {
     10     m_content = text;
     11     update();
     12 }
     13 
     14 void
     15 elidedlabel_t::paintEvent(QPaintEvent* event)
     16 {
     17     QFrame::paintEvent(event);
     18 
     19     QPainter painter(this);
     20     QFontMetrics metrics = painter.fontMetrics();
     21 
     22     bool has_elided = false;
     23     int linespacing = metrics.lineSpacing();
     24     int y = 0;
     25 
     26     QTextLayout textLayout(m_content, painter.font());
     27     textLayout.beginLayout();
     28     forever {
     29         QTextLine line = textLayout.createLine();
     30 
     31         if (!line.isValid())
     32             break;
     33 
     34         line.setLineWidth(width());
     35         int nextline_y = y + linespacing;
     36 
     37         if (height() >= nextline_y + linespacing) {
     38             line.draw(&painter, QPoint(0, y));
     39             y = nextline_y;
     40         } else {
     41             QString lastline = m_content.mid(line.textStart());
     42             QString lastline_elided = metrics.elidedText(lastline, Qt::ElideRight, width());
     43             painter.drawText(QPoint(0, y + metrics.ascent()), lastline_elided);
     44             line = textLayout.createLine();
     45             has_elided = line.isValid();
     46             break;
     47         }
     48     }
     49     textLayout.endLayout();
     50     if (has_elided != m_elided) {
     51         m_elided = has_elided;
     52         emit elisionChanged(has_elided);
     53     }
     54 }