KGantt

kganttpenstylecombobox.cpp
1 /*
2  * SPDX-FileCopyrightText: 2020 Dag Andersen <[email protected]>
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 
22 using namespace KGantt;
23 
24 PenStyleComboBoxListView::PenStyleComboBoxListView(QComboBox *cmb)
25  : combo(cmb)
26 {}
27 
28 void PenStyleComboBoxListView::resizeEvent(QResizeEvent *event)
29 {
30  resizeContents(viewport()->width(), contentsSize().height());
32 }
33 
34 void 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
42  QStyleOptionMenuItem menuOpt;
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  menuOpt.tabWidth = 0;
50  QPainter p(viewport());
51  combo->style()->drawControl(QStyle::CE_MenuEmptyArea, &menuOpt, &p, this);
52  }
53  }
55  QPen pen;
56  pen.setColor(Qt::black);
57  pen.setWidth(2);
58  QRect rect = e->rect();
59  QModelIndex idx = indexAt(rect.topLeft());
60  while (idx.isValid()) {
61  QRect itemRect = rectForIndex(idx);
62  pen.setStyle(static_cast<Qt::PenStyle>(idx.data(Qt::UserRole).toInt()));
63  QPainter painter(viewport());
64  painter.setPen(pen);
65  painter.drawLine(itemRect.left()+1, itemRect.center().y(), itemRect.right()-1, itemRect.center().y());
66  rect.adjust(0, itemRect.height(), 0, 0);
67  idx = indexAt(rect.topLeft());
68  }
69 }
70 
71 PenStyleComboBox::PenStyleComboBox(QWidget *parent)
72  : QComboBox(parent)
73 {
74  PenStyleComboBoxListView *v = new PenStyleComboBoxListView(this);
75  v->setViewMode(QListView::ListMode);
76  v->setModel(new QStandardItemModel(this));
77  setView(v);
78 
79  QPen pen;
80  pen.setWidth(2);
81  pen.setColor(Qt::black);
82  QMetaEnum styles = QMetaEnum::fromType<Qt::PenStyle>();
83  for (int i = 0; i < styles.keyCount(); ++i) {
84  int value = styles.value(i);
85  if (value == Qt::NoPen) {
86  continue;
87  }
88  addItem(QString(), value);
89  }
90 }
91 
92 void PenStyleComboBox::setCurrentStyle(Qt::PenStyle style)
93 {
94  setCurrentIndex(findData(static_cast<int>(style)));
95 }
96 
97 Qt::PenStyle PenStyleComboBox::currentStyle() const
98 {
99  return static_cast<Qt::PenStyle>(currentData().toInt());
100 }
101 
102 void PenStyleComboBox::paintEvent(QPaintEvent *pe)
103 {
105 
106  QStyleOptionComboBox option;
107  option.initFrom(this);
108  option.frame = hasFrame();
109  QRect r = style()->subControlRect(QStyle::CC_ComboBox, &option, QStyle::SC_ComboBoxEditField, this);
110  if (!option.frame) { // frameless combo boxes have smaller margins but styles do not take this into account
111  r.adjust(-14, 0, 14, 1);
112  }
113  QPen pen;
114  pen.setStyle(currentStyle());
115  pen.setColor(Qt::black);
116  pen.setWidth(2);
117 
118  QPainter painter(this);
119  painter.setPen(pen);
120  painter.drawLine(r.left(), r.center().y(), r.right(), r.center().y());
121 }
void setStyle(Qt::PenStyle style)
CE_MenuEmptyArea
int right() const const
int keyCount() const const
SH_ComboBox_Popup
int height() const const
int y() const const
void initFrom(const QWidget *widget)
const QRect & rect() const const
bool isValid() const const
int toInt(bool *ok) const const
int left() const const
UserRole
int value(int index) const const
QPoint center() const const
Global namespace.
void setColor(const QColor &color)
virtual void paintEvent(QPaintEvent *e) override
virtual void resizeEvent(QResizeEvent *e) override
QVariant data(int role) const const
void setWidth(int width)
virtual void paintEvent(QPaintEvent *e) override
void adjust(int dx1, int dy1, int dx2, int dy2)
QPoint topLeft() const const
SC_ComboBoxEditField
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Dec 5 2021 22:32:42 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.