KGantt

kganttpenstylecombobox.cpp
1/*
2 * SPDX-FileCopyrightText: 2020 Dag Andersen <danders@get2net.dk>
3 *
4 * This file is part of the KGantt library.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9
10#include "kganttpenstylecombobox.h"
11#include "kganttpenstylecombobox_p.h"
12
13#include <QStandardItemModel>
14#include <QStyleOptionComboBox>
15#include <QPainter>
16#include <QPaintEvent>
17#include <QPen>
18#include <QMetaEnum>
19#include <QPixmap>
20#include <QDebug>
21
22using namespace KGantt;
23
24PenStyleComboBoxListView::PenStyleComboBoxListView(QComboBox *cmb)
25 : combo(cmb)
26{}
27
28void PenStyleComboBoxListView::resizeEvent(QResizeEvent *event)
29{
30 resizeContents(viewport()->width(), contentsSize().height());
32}
33
34void PenStyleComboBoxListView::paintEvent(QPaintEvent *e)
35{
36 if (combo) {
38 opt.initFrom(combo);
39 opt.editable = combo->isEditable();
40 if (combo->style()->styleHint(QStyle::SH_ComboBox_Popup, &opt, combo)) {
41 //we paint the empty menu area to avoid having blank space that can happen when scrolling
43 menuOpt.initFrom(this);
44 menuOpt.palette = palette();
45 menuOpt.state = QStyle::State_None;
46 menuOpt.checkType = QStyleOptionMenuItem::NotCheckable;
47 menuOpt.menuRect = e->rect();
48 menuOpt.maxIconWidth = 0;
49 QPainter p(viewport());
50 combo->style()->drawControl(QStyle::CE_MenuEmptyArea, &menuOpt, &p, this);
51 }
52 }
54 QPen pen;
56 pen.setWidth(2);
57 QRect rect = e->rect();
58 QModelIndex idx = indexAt(rect.topLeft());
59 while (idx.isValid()) {
60 QRect itemRect = rectForIndex(idx);
61 pen.setStyle(static_cast<Qt::PenStyle>(idx.data(Qt::UserRole).toInt()));
62 QPainter painter(viewport());
63 painter.setPen(pen);
64 painter.drawLine(itemRect.left()+1, itemRect.center().y(), itemRect.right()-1, itemRect.center().y());
65 rect.adjust(0, itemRect.height(), 0, 0);
66 idx = indexAt(rect.topLeft());
67 }
68}
69
70PenStyleComboBox::PenStyleComboBox(QWidget *parent)
71 : QComboBox(parent)
72{
73 PenStyleComboBoxListView *v = new PenStyleComboBoxListView(this);
74 v->setViewMode(QListView::ListMode);
75 v->setModel(new QStandardItemModel(this));
76 setView(v);
77
78 QPen pen;
79 pen.setWidth(2);
82 for (int i = 0; i < styles.keyCount(); ++i) {
83 int value = styles.value(i);
84 if (value == Qt::NoPen) {
85 continue;
86 }
87 addItem(QString(), value);
88 }
89}
90
91void PenStyleComboBox::setCurrentStyle(Qt::PenStyle style)
92{
93 setCurrentIndex(findData(static_cast<int>(style)));
94}
95
96Qt::PenStyle PenStyleComboBox::currentStyle() const
97{
98 return static_cast<Qt::PenStyle>(currentData().toInt());
99}
100
101void PenStyleComboBox::paintEvent(QPaintEvent *pe)
102{
104
106 option.initFrom(this);
107 option.frame = hasFrame();
109 if (!option.frame) { // frameless combo boxes have smaller margins but styles do not take this into account
110 r.adjust(-14, 0, 14, 1);
111 }
112 QPen pen;
113 pen.setStyle(currentStyle());
114 pen.setColor(Qt::black);
115 pen.setWidth(2);
116
117 QPainter painter(this);
118 painter.setPen(pen);
119 painter.drawLine(r.left(), r.center().y(), r.right(), r.center().y());
120}
Global namespace.
void setCurrentIndex(int index)
int findData(const QVariant &data, int role, Qt::MatchFlags flags) const const
bool hasFrame() const const
virtual void paintEvent(QPaintEvent *e) override
virtual void paintEvent(QPaintEvent *e) override
virtual void resizeEvent(QResizeEvent *e) override
QMetaEnum fromType()
int keyCount() const const
int value(int index) const const
QVariant data(int role) const const
bool isValid() const const
const QRect & rect() const const
void setColor(const QColor &color)
void setStyle(Qt::PenStyle style)
void setWidth(int width)
int y() const const
void adjust(int dx1, int dy1, int dx2, int dy2)
QPoint center() const const
int height() const const
int left() const const
int right() const const
QPoint topLeft() const const
CE_MenuEmptyArea
SH_ComboBox_Popup
SC_ComboBoxEditField
virtual QRect subControlRect(ComplexControl control, const QStyleOptionComplex *option, SubControl subControl, const QWidget *widget) const const=0
void initFrom(const QWidget *widget)
UserRole
PenStyle
int toInt(bool *ok) const const
QStyle * style() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.