File indexing completed on 2024-05-12 15:54:28
0001 /* 0002 * Copyright (C) 2020 Dag Andersen <danders@get2net.dk> 0003 * 0004 * This file is part of the KGantt library. 0005 * 0006 * This program is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU General Public License as 0008 * published by the Free Software Foundation; either version 2 of 0009 * the License, or (at your option) any later version. 0010 * 0011 * This program is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 * GNU General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU General Public License 0017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 0018 */ 0019 0020 0021 #include "kganttpenstylecombobox.h" 0022 #include "kganttpenstylecombobox_p.h" 0023 0024 #include <QStandardItemModel> 0025 #include <QStyleOptionComboBox> 0026 #include <QPainter> 0027 #include <QPaintEvent> 0028 #include <QPen> 0029 #include <QMetaEnum> 0030 #include <QPixmap> 0031 #include <QDebug> 0032 0033 using namespace KGantt; 0034 0035 PenStyleComboBoxListView::PenStyleComboBoxListView(QComboBox *cmb) 0036 : combo(cmb) 0037 {} 0038 0039 void PenStyleComboBoxListView::resizeEvent(QResizeEvent *event) 0040 { 0041 resizeContents(viewport()->width(), contentsSize().height()); 0042 QListView::resizeEvent(event); 0043 } 0044 0045 void PenStyleComboBoxListView::paintEvent(QPaintEvent *e) 0046 { 0047 if (combo) { 0048 QStyleOptionComboBox opt; 0049 opt.initFrom(combo); 0050 opt.editable = combo->isEditable(); 0051 if (combo->style()->styleHint(QStyle::SH_ComboBox_Popup, &opt, combo)) { 0052 //we paint the empty menu area to avoid having blank space that can happen when scrolling 0053 QStyleOptionMenuItem menuOpt; 0054 menuOpt.initFrom(this); 0055 menuOpt.palette = palette(); 0056 menuOpt.state = QStyle::State_None; 0057 menuOpt.checkType = QStyleOptionMenuItem::NotCheckable; 0058 menuOpt.menuRect = e->rect(); 0059 menuOpt.maxIconWidth = 0; 0060 menuOpt.tabWidth = 0; 0061 QPainter p(viewport()); 0062 combo->style()->drawControl(QStyle::CE_MenuEmptyArea, &menuOpt, &p, this); 0063 } 0064 } 0065 QListView::paintEvent(e); 0066 QPen pen; 0067 pen.setColor(Qt::black); 0068 pen.setWidth(2); 0069 QRect rect = e->rect(); 0070 QModelIndex idx = indexAt(rect.topLeft()); 0071 while (idx.isValid()) { 0072 QRect itemRect = rectForIndex(idx); 0073 pen.setStyle(static_cast<Qt::PenStyle>(idx.data(Qt::UserRole).toInt())); 0074 QPainter painter(viewport()); 0075 painter.setPen(pen); 0076 painter.drawLine(itemRect.left()+1, itemRect.center().y(), itemRect.right()-1, itemRect.center().y()); 0077 rect.adjust(0, itemRect.height(), 0, 0); 0078 idx = indexAt(rect.topLeft()); 0079 } 0080 } 0081 0082 PenStyleComboBox::PenStyleComboBox(QWidget *parent) 0083 : QComboBox(parent) 0084 { 0085 PenStyleComboBoxListView *v = new PenStyleComboBoxListView(this); 0086 v->setViewMode(QListView::ListMode); 0087 v->setModel(new QStandardItemModel(this)); 0088 setView(v); 0089 0090 QPen pen; 0091 pen.setWidth(2); 0092 pen.setColor(Qt::black); 0093 QMetaEnum styles = QMetaEnum::fromType<Qt::PenStyle>(); 0094 for (int i = 0; i < styles.keyCount(); ++i) { 0095 int value = styles.value(i); 0096 if (value == Qt::NoPen) { 0097 continue; 0098 } 0099 addItem(QString(), value); 0100 } 0101 } 0102 0103 void PenStyleComboBox::setCurrentStyle(Qt::PenStyle style) 0104 { 0105 setCurrentIndex(findData(static_cast<int>(style))); 0106 } 0107 0108 Qt::PenStyle PenStyleComboBox::currentStyle() const 0109 { 0110 return static_cast<Qt::PenStyle>(currentData().toInt()); 0111 } 0112 0113 void PenStyleComboBox::paintEvent(QPaintEvent *pe) 0114 { 0115 QComboBox::paintEvent(pe); 0116 0117 QStyleOptionComboBox option; 0118 option.initFrom(this); 0119 option.frame = hasFrame(); 0120 QRect r = style()->subControlRect(QStyle::CC_ComboBox, &option, QStyle::SC_ComboBoxEditField, this); 0121 if (!option.frame) { // frameless combo boxes have smaller margins but styles do not take this into account 0122 r.adjust(-14, 0, 14, 1); 0123 } 0124 QPen pen; 0125 pen.setStyle(currentStyle()); 0126 pen.setColor(Qt::black); 0127 pen.setWidth(2); 0128 0129 QPainter painter(this); 0130 painter.setPen(pen); 0131 painter.drawLine(r.left(), r.center().y(), r.right(), r.center().y()); 0132 }