1 /*
2 * Copyright (C) 2020 Damir Porobic <damir.porobic@gmx.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include <QtGui/QPainter>
21 #include "ListMenuItem.h"
22
23 namespace kImageAnnotator {
24
ListMenuItem(const QIcon & icon,const QString & text,const QVariant & data,QWidget * parent)25 ListMenuItem::ListMenuItem(const QIcon &icon, const QString &text, const QVariant &data, QWidget *parent) :
26 QWidget(parent),
27 mIcon(icon),
28 mText(text),
29 mData(data),
30 mIsChecked(false),
31 mSpacer(6),
32 mMargin(5)
33 {
34 setMouseTracking(true);
35
36 mIconSize = ScaledSizeProvider::menuItemIconSize();
37 auto offsetForTextWidth = 1.15; // Width not correct calculated on windows #117
38 mTextSize = QSize(fontMetrics().width(mText) * offsetForTextWidth, fontMetrics().height());
39 mIconOffset = QPoint(mMargin, mMargin);
40 mTextOffset = QPoint(mIconSize.width() + mIconOffset.x() + mSpacer, (mIconSize.height() - mTextSize.height()) / 2 + mIconOffset.y());
41 mSize = QSize(mIconSize.width() + mTextSize.width() + mMargin * 2 + mSpacer, mIconSize.height() + mMargin * 2);
42 }
43
data() const44 QVariant ListMenuItem::data() const
45 {
46 return mData;
47 }
48
icon() const49 QIcon ListMenuItem::icon() const
50 {
51 return mIcon;
52 }
53
text() const54 QString ListMenuItem::text() const
55 {
56 return mText;
57 }
58
minimumSizeHint() const59 QSize ListMenuItem::minimumSizeHint() const
60 {
61 return mSize;
62 }
63
isChecked() const64 bool ListMenuItem::isChecked() const
65 {
66 return mIsChecked;
67 }
68
setIsChecked(bool isChecked)69 void ListMenuItem::setIsChecked(bool isChecked)
70 {
71 mIsChecked = isChecked;
72 emit toggled(isChecked);
73 update();
74 }
75
paintEvent(QPaintEvent * event)76 void ListMenuItem::paintEvent(QPaintEvent *event)
77 {
78 QPainter painter(this);
79 QStyleOption styleOption;
80 styleOption.initFrom(this);
81 auto rect = event->rect().adjusted(0,0,-1,-1);
82
83 if(styleOption.state & QStyle::State_MouseOver)
84 {
85 auto defaultBrush = painter.brush();
86 auto defaultPen = painter.pen();
87 painter.setBrush(Constants::HoverColor);
88 painter.setPen(Constants::HoverColor);
89 painter.drawRect(rect);
90 painter.setBrush(defaultBrush);
91 painter.setPen(defaultPen);
92 }
93
94 painter.drawPixmap(rect.topLeft() + mIconOffset, mIcon.pixmap(mIconSize));
95 painter.drawText(QRect(rect.topLeft() + mTextOffset, mTextSize), Qt::AlignLeft | Qt::AlignVCenter, mText);
96
97 if(mIsChecked) {
98 painter.drawRect(rect);
99 }
100 }
101
mouseMoveEvent(QMouseEvent * event)102 void ListMenuItem::mouseMoveEvent(QMouseEvent *event)
103 {
104 update();
105 QWidget::mouseMoveEvent(event);
106 }
107
leaveEvent(QEvent * event)108 void ListMenuItem::leaveEvent(QEvent *event)
109 {
110 update();
111 QWidget::leaveEvent(event);
112 }
113
mouseReleaseEvent(QMouseEvent * event)114 void ListMenuItem::mouseReleaseEvent(QMouseEvent* event)
115 {
116 QWidget::mouseReleaseEvent(event);
117
118 QRect itemRect(QPoint(0, 0), mSize);
119 if (isEnabled() && itemRect.contains(mapFromGlobal(QCursor::pos()))) {
120 mIsChecked = true;
121 emit toggled(mIsChecked);
122 }
123 }
124
125 } // namespace kImageAnnotator