mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-19 06:20:53 +01:00
Replacement for KColorCombo
This commit is contained in:
@@ -113,6 +113,10 @@ set(QET_SRC_FILES
|
||||
${QET_DIR}/sources/bordertitleblock.h
|
||||
${QET_DIR}/sources/colorbutton.cpp
|
||||
${QET_DIR}/sources/colorbutton.h
|
||||
${QET_DIR}/sources/colorcombobox.cpp
|
||||
${QET_DIR}/sources/colorcombobox.h
|
||||
${QET_DIR}/sources/colorcomboboxdelegate.cpp
|
||||
${QET_DIR}/sources/colorcomboboxdelegate.h
|
||||
${QET_DIR}/sources/conductorautonumerotation.cpp
|
||||
${QET_DIR}/sources/conductorautonumerotation.h
|
||||
${QET_DIR}/sources/conductornumexport.cpp
|
||||
|
||||
@@ -69,7 +69,7 @@
|
||||
</widget>
|
||||
</item>
|
||||
<item row="12" column="1" colspan="2">
|
||||
<widget class="KColorCombo" name="m_bridge_color_cb"/>
|
||||
<widget class="ColorComboBox" name="m_bridge_color_cb"/>
|
||||
</item>
|
||||
<item row="5" column="0">
|
||||
<widget class="QLabel" name="label_6">
|
||||
@@ -323,9 +323,9 @@
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
<class>KColorCombo</class>
|
||||
<class>ColorComboBox</class>
|
||||
<extends>QComboBox</extends>
|
||||
<header>kcolorcombo.h</header>
|
||||
<header>../../colorcombobox.h</header>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources>
|
||||
|
||||
98
sources/colorcombobox.cpp
Normal file
98
sources/colorcombobox.cpp
Normal file
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
Copyright 2006-2025 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "colorcombobox.h"
|
||||
|
||||
#include "colorcomboboxdelegate.h"
|
||||
|
||||
#include <QColorDialog>
|
||||
#include <QPainter>
|
||||
#include <QStylePainter>
|
||||
#include <Qt>
|
||||
|
||||
|
||||
/**
|
||||
@brief ColorComboBox::ColorComboBox
|
||||
Simple constructor
|
||||
@param parent QObject parent of the ColorComboBox
|
||||
*/
|
||||
ColorComboBox::ColorComboBox(QWidget *parent) : QComboBox(parent)
|
||||
{
|
||||
connect(this, &QComboBox::activated, this, &ColorComboBox::colorSelected);
|
||||
setItemDelegate(new ColorComboBoxDelegate());
|
||||
}
|
||||
|
||||
/**
|
||||
@brief ColorComboBox::setColors
|
||||
Sets the colors of the combo box. An item at the top will be added to allow selection
|
||||
of a custom color.
|
||||
@param colors Vector of colors to add to the combo box
|
||||
*/
|
||||
void ColorComboBox::setColors(const QVector<QColor> &colors)
|
||||
{
|
||||
addItem(tr("[Custom color...]"), QColor(Qt::black));
|
||||
for(auto &color : colors) {
|
||||
addItem(color.name(), color);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@brief ColorComboBox::colorSelected
|
||||
Opens a color selection dialog and lets the user select a color.
|
||||
@param checked Not used
|
||||
*/
|
||||
void ColorComboBox::colorSelected(int index)
|
||||
{
|
||||
if (index == 0) {
|
||||
// Open color selection dialog if custom color is selected
|
||||
auto new_color = QColorDialog::getColor(itemData(index).value<QColor>(), this, tr("Select color"), QColorDialog::DontUseNativeDialog);
|
||||
|
||||
// Validate and emit user input color
|
||||
if (new_color.isValid()) {
|
||||
setItemData(index, new_color);
|
||||
emit activated(new_color);
|
||||
}
|
||||
} else {
|
||||
// Emit color from selected combo box row
|
||||
emit activated(itemData(index).value<QColor>());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@brief ColorComboBox::paintEvent
|
||||
Paints a filled rectangle with the current selected color on the combo box surface.
|
||||
@param e Paint event context
|
||||
*/
|
||||
void ColorComboBox::paintEvent(QPaintEvent *e) {
|
||||
// Padding for the color indicator
|
||||
const int padding_x = 5;
|
||||
const int padding_y = 5;
|
||||
|
||||
// Create painter and draw a vanilla combobox
|
||||
QStylePainter painter(this);
|
||||
QStyleOptionComboBox opt;
|
||||
initStyleOption(&opt);
|
||||
painter.drawComplexControl(QStyle::CC_ComboBox, opt);
|
||||
|
||||
// Get dimensions of the combo box paint surface
|
||||
auto r_width = painter.device()->width();
|
||||
auto r_height = painter.device()->height();
|
||||
|
||||
// Paint a rectangle with a margin
|
||||
auto color_indicator = QRect(padding_x, padding_y, r_width - padding_x * 2, r_height - padding_y * 2);
|
||||
painter.fillRect(color_indicator, itemData(currentIndex()).value<QColor>());
|
||||
}
|
||||
49
sources/colorcombobox.h
Normal file
49
sources/colorcombobox.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
Copyright 2006-2025 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef COLORCOMBOBOX_H
|
||||
#define COLORCOMBOBOX_H
|
||||
|
||||
#include <QColor>
|
||||
#include <QObject>
|
||||
#include <QComboBox>
|
||||
#include <QVector>
|
||||
#include <Qt>
|
||||
|
||||
/**
|
||||
@brief The ColorComboBox class
|
||||
*/
|
||||
class ColorComboBox : public QComboBox
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ColorComboBox(QWidget * = nullptr);
|
||||
|
||||
void setColors(const QVector<QColor> &);
|
||||
|
||||
public slots:
|
||||
void colorSelected(int);
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *) override;
|
||||
|
||||
signals:
|
||||
void activated(const QColor &);
|
||||
};
|
||||
|
||||
#endif
|
||||
58
sources/colorcomboboxdelegate.cpp
Normal file
58
sources/colorcomboboxdelegate.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
Copyright 2006-2025 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "colorcomboboxdelegate.h"
|
||||
|
||||
#include <QColor>
|
||||
#include <QPainter>
|
||||
#include <QPalette>
|
||||
#include <QtDebug>
|
||||
|
||||
/**
|
||||
@brief ColorComboBoxDelegate::paint
|
||||
Paints a filled rectangle on the drop down item with the items color.
|
||||
@param painter Painter context
|
||||
@param option Style options
|
||||
@param index Index of the item to paint
|
||||
|
||||
*/
|
||||
void ColorComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
painter->save();
|
||||
|
||||
if (index.row() > 0)
|
||||
{
|
||||
auto rect = option.rect;
|
||||
|
||||
// Draw mouseover background
|
||||
if (option.state & QStyle::State_MouseOver)
|
||||
{
|
||||
auto pal = option.widget->palette();
|
||||
painter->fillRect(rect, pal.color(QPalette::Highlight));
|
||||
}
|
||||
|
||||
// Draw color indicator rectangle
|
||||
auto color = qvariant_cast<QColor>(index.data());
|
||||
rect.adjust(5, 2, -5, -2);
|
||||
painter->fillRect(rect, color);
|
||||
} else {
|
||||
// Draw a normal drop down item for custom color
|
||||
QStyledItemDelegate::paint(painter, option, index);
|
||||
}
|
||||
|
||||
painter->restore();
|
||||
}
|
||||
35
sources/colorcomboboxdelegate.h
Normal file
35
sources/colorcomboboxdelegate.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
Copyright 2006-2025 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef COLORCOMBOBOXDELEGATE_H
|
||||
#define COLORCOMBOBOXDELEGATE_H
|
||||
|
||||
#include <QStyledItemDelegate>
|
||||
|
||||
/**
|
||||
@brief The ColorComboBoxDelegate class
|
||||
Handles drawing of items in the drop down list of ColorComboBox.
|
||||
*/
|
||||
class ColorComboBoxDelegate : public QStyledItemDelegate
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const override;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user