mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-19 23:20:52 +01:00
Replacement for KColorCombo
This commit is contained in:
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>());
|
||||
}
|
||||
Reference in New Issue
Block a user