diff --git a/src/blackgui/elidedpushbutton.cpp b/src/blackgui/elidedpushbutton.cpp new file mode 100644 index 000000000..237b880ad --- /dev/null +++ b/src/blackgui/elidedpushbutton.cpp @@ -0,0 +1,44 @@ +/* Copyright (C) 2019 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "elidedpushbutton.h" + +#include +#include +#include + +namespace BlackGui +{ + CElidedPushButton::CElidedPushButton(QWidget *parent) : QPushButton(parent) + { } + + CElidedPushButton::CElidedPushButton(const QString &text, QWidget *parent) + : QPushButton(parent), m_elided(false), m_content(text) + { + this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); + } + + void CElidedPushButton::setText(const QString &newText) + { + m_content = newText; + this->update(); + } + + void CElidedPushButton::paintEvent(QPaintEvent *event) + { + QPainter painter(this); + const QFontMetrics fontMetrics = painter.fontMetrics(); + const int usableWidth = qRound(0.9 * this->width()); + + const QString elidedText = fontMetrics.elidedText(m_content, Qt::ElideRight, usableWidth); + m_elided = (elidedText != m_content); + QPushButton::setText(elidedText); + QPushButton::paintEvent(event); + } +} // ns diff --git a/src/blackgui/elidedpushbutton.h b/src/blackgui/elidedpushbutton.h new file mode 100644 index 000000000..6fc23b172 --- /dev/null +++ b/src/blackgui/elidedpushbutton.h @@ -0,0 +1,53 @@ +/* Copyright (C) 2019 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKGUI_ELIDEDPUSHBUTTON_H +#define BLACKGUI_ELIDEDPUSHBUTTON_H + +#include + +namespace BlackGui +{ + /** + * Push button with elided text + * \remark based on http://doc.qt.io/qt-5/qtwidgets-widgets-elidedlabel-example.html + */ + class CElidedPushButton : public QPushButton + { + Q_OBJECT + + public: + //! Constructor + explicit CElidedPushButton(QWidget *parent = nullptr); + + //! Constructor + explicit CElidedPushButton(const QString &text, QWidget *parent = nullptr); + + //! Text + void setText(const QString &text); + + //! Text + const QString &text() const { return m_content; } + + //! Elided? + bool isElided() const { return m_elided; } + + protected: + //! \copydoc QPushButton::paintEvent + void paintEvent(QPaintEvent *event) override; + + private: + bool m_elided = false; + QString m_content; + }; +} // ns + +#endif // guard