File indexing completed on 2024-04-28 11:21:04
0001 /* 0002 SPDX-License-Identifier: GPL-2.0-or-later 0003 SPDX-FileCopyrightText: 2020 Sirgienko Nikita <warquark@gmail.com> 0004 */ 0005 0006 #include "worksheetcontrolitem.h" 0007 #include "worksheet.h" 0008 #include "worksheetentry.h" 0009 0010 #include <QApplication> 0011 #include <QGraphicsSceneMouseEvent> 0012 #include <QPainter> 0013 0014 #include <KColorScheme> 0015 0016 WorksheetControlItem::WorksheetControlItem(Worksheet* worksheet, WorksheetEntry* parent) : QGraphicsRectItem(parent), 0017 m_worksheet(worksheet) 0018 { 0019 setAcceptDrops(true); 0020 setAcceptHoverEvents(true); 0021 setFlags(flags() | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsFocusable); 0022 } 0023 0024 void WorksheetControlItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) 0025 { 0026 Q_UNUSED(option); 0027 Q_UNUSED(widget); 0028 0029 if (m_worksheet->isPrinting()) 0030 return; 0031 0032 painter->setViewTransformEnabled(true); 0033 0034 if (m_isHovered) 0035 painter->setPen(QPen(QApplication::palette().color(QPalette::Text), 2)); 0036 else 0037 painter->setPen(QPen(QApplication::palette().color(QPalette::Text), 1)); 0038 0039 qreal x = rect().x(); 0040 qreal y = rect().y(); 0041 qreal w = rect().width(); 0042 qreal h = rect().height(); 0043 0044 painter->drawLine(x, y, x+w, y); 0045 painter->drawLine(x+w, y, x+w, y+h); 0046 painter->drawLine(x, y+h, x+w, y+h); 0047 0048 //For collabsable entries draw "collapsing triangle" (form will depends from collapse's state) 0049 if (isCollapsable) 0050 { 0051 if (isCollapsed) 0052 { 0053 QBrush brush = painter->brush(); 0054 brush.setStyle(Qt::SolidPattern); 0055 brush.setColor(QApplication::palette().color(QPalette::Text)); 0056 painter->setBrush(brush); 0057 0058 QPolygon triangle; 0059 triangle << QPoint(x, y) << QPoint(x+w, y) << QPoint(x+w, y+w); 0060 0061 painter->drawPolygon(triangle); 0062 } 0063 else 0064 painter->drawLine(x, y, x+w, y+w); 0065 } 0066 0067 if (isSelected) 0068 { 0069 //Use theme colour for selection, but with transparent 0070 QColor color = QApplication::palette().color(QPalette::Highlight); 0071 color.setAlpha(192); 0072 0073 painter->fillRect(rect(), color); 0074 } 0075 } 0076 0077 void WorksheetControlItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) 0078 { 0079 emit doubleClick(); 0080 QGraphicsItem::mouseDoubleClickEvent(event); 0081 } 0082 0083 void WorksheetControlItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event) 0084 { 0085 if (event->buttons() != Qt::LeftButton) 0086 return; 0087 0088 const QPointF buttonDownPos = event->buttonDownPos(Qt::LeftButton); 0089 if (contains(buttonDownPos) && (event->pos() - buttonDownPos).manhattanLength() >= QApplication::startDragDistance()) 0090 { 0091 ungrabMouse(); 0092 emit drag(mapToParent(buttonDownPos), mapToParent(event->pos())); 0093 event->accept(); 0094 } 0095 } 0096 0097 void WorksheetControlItem::hoverEnterEvent(QGraphicsSceneHoverEvent* event) 0098 { 0099 Q_UNUSED(event); 0100 m_isHovered = true; 0101 update(); 0102 } 0103 0104 void WorksheetControlItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event) 0105 { 0106 Q_UNUSED(event); 0107 m_isHovered = false; 0108 update(); 0109 }