diff --git a/cmake/qet_compilation_vars.cmake b/cmake/qet_compilation_vars.cmake index e16b058ee..1783dce0e 100644 --- a/cmake/qet_compilation_vars.cmake +++ b/cmake/qet_compilation_vars.cmake @@ -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 diff --git a/sources/TerminalStrip/ui/terminalstripeditor.ui b/sources/TerminalStrip/ui/terminalstripeditor.ui index 29f6854e3..d02079f23 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.ui +++ b/sources/TerminalStrip/ui/terminalstripeditor.ui @@ -69,7 +69,7 @@ - + @@ -323,9 +323,9 @@ - KColorCombo + ColorComboBox QComboBox -
kcolorcombo.h
+
../../colorcombobox.h
diff --git a/sources/colorcombobox.cpp b/sources/colorcombobox.cpp new file mode 100644 index 000000000..c556598cd --- /dev/null +++ b/sources/colorcombobox.cpp @@ -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 . +*/ +#include "colorcombobox.h" + +#include "colorcomboboxdelegate.h" + +#include +#include +#include +#include + + +/** + @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 &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(), 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()); + } +} + +/** + @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()); +} diff --git a/sources/colorcombobox.h b/sources/colorcombobox.h new file mode 100644 index 000000000..c13a2ecc1 --- /dev/null +++ b/sources/colorcombobox.h @@ -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 . +*/ +#ifndef COLORCOMBOBOX_H +#define COLORCOMBOBOX_H + +#include +#include +#include +#include +#include + +/** + @brief The ColorComboBox class +*/ +class ColorComboBox : public QComboBox +{ + Q_OBJECT + + public: + ColorComboBox(QWidget * = nullptr); + + void setColors(const QVector &); + + public slots: + void colorSelected(int); + + protected: + void paintEvent(QPaintEvent *) override; + + signals: + void activated(const QColor &); +}; + +#endif diff --git a/sources/colorcomboboxdelegate.cpp b/sources/colorcomboboxdelegate.cpp new file mode 100644 index 000000000..a81783168 --- /dev/null +++ b/sources/colorcomboboxdelegate.cpp @@ -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 . +*/ +#include "colorcomboboxdelegate.h" + +#include +#include +#include +#include + +/** + @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(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(); +} diff --git a/sources/colorcomboboxdelegate.h b/sources/colorcomboboxdelegate.h new file mode 100644 index 000000000..1580060d3 --- /dev/null +++ b/sources/colorcomboboxdelegate.h @@ -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 . +*/ +#ifndef COLORCOMBOBOXDELEGATE_H +#define COLORCOMBOBOXDELEGATE_H + +#include + +/** + @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