/** * @RDE_HEADER@ * An Image Editor which shows an Image, and emits a signal when clicked (to edit/swap an Image) * $Id$ * $HeadURL$ * $Author$ * * Created by Mike Wille, SLSE, 2016-09-19 */ #include "lts_image_editor.h" #include #include #include #include #include #include using namespace LtsWidgets; LtsImageEditor::LtsImageEditor(QWidget *parent) : QWidget(parent) , m_isHovered(false) , m_menuEnabled(true) // enabled by default , m_bDrawOuterRect(true) {} void LtsImageEditor::setPixmap(const QPixmap &pixmap) { if (pixmap.isNull()) // invalid / empty pixmap given, trying to replace it with one from lts_resources m_pixmap = QPixmap(QStringLiteral(":style/noimage.png")); // when this fails, the pixmap is still invalid, so no checks needed TODO: add noimage.png ! else m_pixmap = pixmap; update(); emit imageChanged(); } void LtsImageEditor::enterEvent(QEvent *) { //qWarning() << "enter"; m_isHovered = true; update(); } void LtsImageEditor::leaveEvent(QEvent *) { //qWarning() << "leave"; m_isHovered = false; update(); } bool LtsImageEditor::event(QEvent *event) { if (event->type() == QEvent::MouseButtonPress && m_menuEnabled) { emit signalClicked(); event->accept(); m_isHovered = false; // unhovered on click update(); return true; } return QWidget::event(event); // otherwise.. } void LtsImageEditor::paintEvent(QPaintEvent* event) // TODO: colors from palette (style plugin) ? { QPainter p(this); // draw profile picture p.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); QBrush brush = QColor(QStringLiteral("white")); // TODO: get from palette if (!m_pixmap.isNull()) { if (!(m_isHovered && m_menuEnabled)) // draw if NOT hovered or draw when hovered but menu is disabled { const QSize s(size()); const QPixmap pixmap(m_pixmap.scaled(s, Qt::KeepAspectRatio, Qt::SmoothTransformation)); const int x = qRound((s.width() - pixmap.width()) / 2.); const int y = qRound((s.height() - pixmap.height()) / 2.); if (m_bDrawOuterRect) { p.setPen(QPen(QColor("gray"), 2)); p.drawRect(rect()); } p.drawPixmap(x, y, pixmap); event->accept(); return; } } // draw frame p.setPen(QPen(QColor("gray"), 2)); p.setBrush(brush); p.drawRect(rect()); // draw menu icon if (m_isHovered && m_menuEnabled) { p.setPen(QPen(QColor(QStringLiteral("gray")), (size().height() / 48 * 2))); // respect height, width is unnecessary double xc = size().width()/2.012; double yc = size().height()/2.012; p.drawLine(QPointF(xc - (xc/2.01f), yc - (yc/3.01f)), QPointF(xc + (xc/2.02f), yc - (yc/3.02f))); p.drawLine(QPointF(xc - (xc/2.01f), yc - 0.01f), QPointF(xc + (xc/2.02f), yc - 0)); p.drawLine(QPointF(xc - (xc/2.01f), yc + (yc/3.01f)), QPointF(xc + (xc/2.02f), yc + (yc/3.02f))); } event->accept(); }