// TODO: hdr #include "lts_notification_ctl.h" #include #include #include #include #include #include namespace LtsNotification { LtsNotificationController::LtsNotificationController(const QString &identifier) : QObject() , m_count(0) , m_identifier(QStringLiteral("LtsNotification%1").arg(identifier)) {} LtsNotificationController::~LtsNotificationController() { qDeleteAll(m_notiList); } void LtsNotificationController::setIcon(const QIcon& icon) { m_icon = icon; setAllIcons(); } void LtsNotificationController::setCount(const int count) { m_count = count; setAllIcons(); } /*! * \brief LtsNotificationController::setBackground * \param r red * \param g green * \param b yellow * \author Niklas Schmidt * \details If all params(r,g,b) are -1, than reset Stylesheet = non stylesheet */ //TODO: Extra function resetBackground instead of -1 params void LtsNotificationController::setBackground(const int r, const int g, const int b) { for (QToolButton *tb : qAsConst(m_notiList)) { QString stylesheet; if(r == -1 && g == -1 && b == -1) stylesheet = QStringLiteral(""); else stylesheet = QStringLiteral("QToolButton {background-color: rgb(") + QString::number(r) + QStringLiteral(",") + QString::number(g) + QStringLiteral(",") + QString::number(b) + QStringLiteral(");}"); tb->setStyleSheet(stylesheet); } } void LtsNotificationController::setEnabled(bool enable) { m_enable = enable; setAllIcons(); } void LtsNotificationController::setAction(const std::function &action) { m_action = action; } void LtsNotificationController::setToolTip(const QString &toolTip) { m_toolTip = toolTip; for (QToolButton *tb : qAsConst(m_notiList)) { tb->setToolTip(toolTip); tb->setIconSize(QSize(30,30)); } } void LtsNotificationController::setAllIcons() { if (m_icon.isNull()) return; // prepare new Icon (add count), then set to all ToolButtons QIcon icon = modifyIcon(m_icon); for (QToolButton *tb : qAsConst(m_notiList)) { tb->setIcon(icon); tb->setIconSize(QSize(30,30)); tb->setEnabled(m_enable); } } #define SIZE 40 QIcon LtsNotificationController::modifyIcon(const QIcon &icon) // TODO: automatic advanced scope ??! { /* FIXME: it is impossible to check if QIcon is valid, so calling modifyIcon will start * a painter on an invalid QPixmap (hence the QIcon) which produces error messages in * the log file & stdout. */ if (m_count < 1) // do not draw '0' on it.. return icon; QPixmap iconImage(icon.pixmap(SIZE * 8 / 10, SIZE * 8 / 10)); QPainter painter(&iconImage); painter.save(); painter.setRenderHints(QPainter::TextAntialiasing | QPainter::Antialiasing | QPainter::HighQualityAntialiasing | QPainter::SmoothPixmapTransform); QFont f; f.setFamily(QStringLiteral("Monospace")); f.setPointSize(16); f.setBold(true); painter.setFont(f); QColor color(Qt::red); painter.setBrush(color); painter.setPen(QPen(color, 3.1)); painter.drawText(QRect(0, 0, 30, 30), QString::number(m_count)); // TODO: working version of this: , QTextOption(Qt::AlignRight | Qt::AlignBottom)); painter.restore(); return iconImage; } QToolButton *LtsNotificationController::createWidget() { QToolButton *b = new QToolButton(); b->setToolButtonStyle(Qt::ToolButtonIconOnly); b->setObjectName(m_identifier); //qWarning() << "object name" << b->objectName() << "==" << m_identifier; // FIXME: variable sizes !! LtsDragIcon ? b->setMinimumSize(SIZE, SIZE); b->setMaximumSize(SIZE, SIZE); connect(b, &QToolButton::destroyed, this, &LtsNotificationController::slotNotiWidgetDestroyed); connect(b, &QToolButton::clicked, this, [this](){ if (m_action) m_action(); }); // TODO: FIXME: BAD, BAD HACK?! if (!m_icon.isNull()) b->setIcon(modifyIcon(m_icon)); b->setToolTip(m_toolTip); m_notiList.append(b); return b; } void LtsNotificationController::slotNotiWidgetDestroyed(QObject *obj) { m_notiList.removeAll(static_cast(obj)); // no qobject_cast for destroyed objects } } // namespace LtsNotification