From kde-commits Sat Mar 05 16:57:04 2016 From: =?utf-8?q?Thomas_L=C3=BCbking?= Date: Sat, 05 Mar 2016 16:57:04 +0000 To: kde-commits Subject: [trojita] src/Gui: make longer address rows expandable Message-Id: X-MARC-Message: https://marc.info/?l=kde-commits&m=145719703800720 Git commit 877a06cfc49142e28b54a1052e7eb7303823f444 by Thomas L=C3=BCbking. Committed on 28/02/2016 at 12:07. Pushed by gerrit into branch 'master'. make longer address rows expandable Change-Id: I40cb378faca4dfab9cdfa2616a7dd6debea2211a M +116 -5 src/Gui/AddressRowWidget.cpp M +23 -1 src/Gui/AddressRowWidget.h http://commits.kde.org/trojita/877a06cfc49142e28b54a1052e7eb7303823f444 diff --git a/src/Gui/AddressRowWidget.cpp b/src/Gui/AddressRowWidget.cpp index eee7409..aa97d68 100644 --- a/src/Gui/AddressRowWidget.cpp +++ b/src/Gui/AddressRowWidget.cpp @@ -23,11 +23,23 @@ #include "Gui/FlowLayout.h" #include "Gui/OneEnvelopeAddress.h" = +#include +#include +#include +#include + namespace Gui { = +static int plainChars(const QLabel *l) +{ + static QTextDocument converter; + converter.setHtml(l->text()); + return converter.toPlainText().length(); +} + AddressRowWidget::AddressRowWidget(QWidget *parent, const QString &headerN= ame, const QList= &addresses, MessageView *messageView): - QWidget(parent) + QWidget(parent), m_expander(0) { FlowLayout *lay =3D new FlowLayout(this, 0, 0, -1); setLayout(lay); @@ -38,13 +50,112 @@ AddressRowWidget::AddressRowWidget(QWidget *parent, co= nst QString &headerName, title->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); title->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::LinksAc= cessibleByMouse); lay->addWidget(title); + int chars =3D 0; + bool collapse =3D false; for (int i =3D 0; i < addresses.size(); ++i) { - QWidget *w =3D new OneEnvelopeAddress(this, addresses[i], messageV= iew, - i =3D=3D addresses.size() - 1 ? - OneEnvelopeAddress::Positi= on::Last : - OneEnvelopeAddress::Positi= on::Middle); + auto *w =3D new OneEnvelopeAddress(this, addresses[i], messageView, + i =3D=3D addresses.size() - 1 ? + OneEnvelopeAddress::Position::Las= t : + OneEnvelopeAddress::Position::Mid= dle); + chars +=3D plainChars(w); lay->addWidget(w); + if (i > 1 && chars > 66) { + w->hide(); + collapse =3D true; + } + } + if (collapse) { + lay->addWidget(m_expander =3D new Expander(this)); + connect(m_expander, &Expander::clicked, this, &AddressRowWidget::t= oggle); + } +} + +void AddressRowWidget::toggle() +{ + Q_ASSERT(m_expander); + if (m_expander->isExpanding()) { + m_expander->setExpanding(false); + for (int i =3D 0; i < layout()->count(); ++i) { + if (QWidget *w =3D layout()->itemAt(i)->widget()) + w->show(); + } + } else { + int chars =3D 0, addresses =3D 0; + int collapse =3D 0; + for (int i =3D 0; i < layout()->count(); ++i) { + if (OneEnvelopeAddress *w =3D + qobject_cast(layout()->itemAt(i)->wid= get())) { + ++addresses; + chars +=3D plainChars(w); + if (addresses > 1 && chars > 66) { + ++collapse; + w->hide(); + } + } + } + m_expander->setExpanding(collapse); + } +} + +Expander::Expander(QWidget *parent, Expander::Direction d) : QLabel(parent) +{ + setCursor(Qt::PointingHandCursor); + setExpanding(d =3D=3D Direction::Expanding); +} + +bool Expander::isExpanding() const { + return m_expanding; +} + +void Expander::setExpanding(const int expanding) +{ + m_expanding =3D expanding; + setToolTip(expanding ? tr("Click to see %1 more items").arg(expanding)= : tr("Click to collapse")); +} + +QSize Expander::sizeHint() const +{ + QSize s =3D QLabel::sizeHint(); + s.setWidth(m_expanding ? 2*s.height() : s.height()); + return s; +} + +void Expander::mouseReleaseEvent(QMouseEvent *me) +{ + if (me->button() =3D=3D Qt::LeftButton) + emit clicked(); +} + +void Expander::paintEvent(QPaintEvent *pe) +{ + QPainter p(this); + p.setRenderHint(QPainter::Antialiasing); + QRectF r(rect()); + r.setWidth(height()); + if (m_expanding) { + // ellipsis + p.setPen(palette().color(foregroundRole())); + p.setBrush(Qt::NoBrush); + p.drawText(r, Qt::AlignCenter, QStringLiteral("\u2026")); + r.moveRight(rect().right()); + } + p.setBrush(palette().color(foregroundRole())); + p.setPen(Qt::NoPen); + p.drawEllipse(r); + p.setBrush(palette().color(backgroundRole())); + float d =3D r.height()/4.0f; + r.adjust(d,d,-d,-d); + // the unicode triangles are not necessarily centered - looks crap + if (m_expanding) { + r.translate(r.width()/8.0f,0); // fix gravity + const QPointF points[3] =3D { r.topLeft(), r.bottomLeft(), QPointF= (r.right(), r.y() + r.height()/2.0) }; + p.drawConvexPolygon(points, 3); + } else { + r.translate(-r.width()/8.0f,0); // fix gravity + const QPointF points[3] =3D { r.topRight(), r.bottomRight(), QPoin= tF(r.left(), r.y() + r.height()/2.0) }; + p.drawConvexPolygon(points, 3); } + p.end(); } = } diff --git a/src/Gui/AddressRowWidget.h b/src/Gui/AddressRowWidget.h index 0e02df9..46fa109 100644 --- a/src/Gui/AddressRowWidget.h +++ b/src/Gui/AddressRowWidget.h @@ -22,7 +22,7 @@ #ifndef GUI_ADDRESSROWWIDGET_H #define GUI_ADDRESSROWWIDGET_H = -#include +#include = namespace Imap { namespace Message { @@ -34,6 +34,25 @@ namespace Gui { = class MessageView; = +/** @short A label with expansion indicating text and "clicked" signal */ +class Expander : public QLabel +{ + Q_OBJECT +public: + enum class Direction { Collapsing =3D 0, Expanding }; + explicit Expander(QWidget *parent, Direction d =3D Direction::Expandin= g); + bool isExpanding() const; + void setExpanding(const int expanding); + QSize sizeHint() const; +signals: + void clicked(); +protected: + void mouseReleaseEvent(QMouseEvent *me); + void paintEvent(QPaintEvent *pe); +private: + int m_expanding; +}; + /** @short Widget displaying the message envelope */ class AddressRowWidget : public QWidget { @@ -41,9 +60,12 @@ class AddressRowWidget : public QWidget public: AddressRowWidget(QWidget *parent, const QString &headerName, const QLi= st &addresses, MessageView *messageView); = +private slots: + void toggle(); private: AddressRowWidget(const AddressRowWidget &) =3D delete; AddressRowWidget &operator=3D(const AddressRowWidget &) =3D delete; + Expander *m_expander; }; = }