From b890c32f13062147eaa52253d5173eb86335d8e4 Mon Sep 17 00:00:00 2001 From: srcejon Date: Thu, 28 Mar 2024 15:24:39 +0000 Subject: [PATCH] Add GUI ColorDialog. --- sdrgui/CMakeLists.txt | 4 ++ sdrgui/gui/colordialog.cpp | 72 ++++++++++++++++++++++++++++ sdrgui/gui/colordialog.h | 51 ++++++++++++++++++++ sdrgui/gui/tablecolorchooser.cpp | 81 ++++++++++++++++++++++++++++++++ sdrgui/gui/tablecolorchooser.h | 48 +++++++++++++++++++ 5 files changed, 256 insertions(+) create mode 100644 sdrgui/gui/colordialog.cpp create mode 100644 sdrgui/gui/colordialog.h create mode 100644 sdrgui/gui/tablecolorchooser.cpp create mode 100644 sdrgui/gui/tablecolorchooser.h diff --git a/sdrgui/CMakeLists.txt b/sdrgui/CMakeLists.txt index 39bec49ac..f1089b92a 100644 --- a/sdrgui/CMakeLists.txt +++ b/sdrgui/CMakeLists.txt @@ -17,6 +17,7 @@ set(sdrgui_SOURCES gui/buttonswitch.cpp gui/channeladddialog.cpp gui/clickablelabel.cpp + gui/colordialog.cpp gui/colormapper.cpp gui/commanditem.cpp gui/commandsdialog.cpp @@ -88,6 +89,7 @@ set(sdrgui_SOURCES gui/spectrummarkersdialog.cpp gui/spectrummeasurementsdialog.cpp gui/spectrummeasurements.cpp + gui/tablecolorchooser.cpp gui/tabletapandhold.cpp gui/tickedslider.cpp gui/timedelegate.cpp @@ -142,6 +144,7 @@ set(sdrgui_HEADERS gui/basicfeaturesettingsdialog.h gui/buttonswitch.h gui/channeladddialog.h + gui/colordialog.h gui/colormapper.h gui/commanditem.h gui/commandsdialog.h @@ -217,6 +220,7 @@ set(sdrgui_HEADERS gui/spectrummarkersdialog.h gui/spectrummeasurementsdialog.h gui/spectrummeasurements.h + gui/tablecolorchooser.h gui/tabletapandhold.h gui/tickedslider.h gui/timedelegate.h diff --git a/sdrgui/gui/colordialog.cpp b/sdrgui/gui/colordialog.cpp new file mode 100644 index 000000000..8918eb63a --- /dev/null +++ b/sdrgui/gui/colordialog.cpp @@ -0,0 +1,72 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2022 Jon Beniston, M7RCE // +// // +// This program 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 as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program 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 V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include +#include + +#include "colordialog.h" + +ColorDialog::ColorDialog(const QColor &initial, QWidget *parent) : + QDialog(parent) +{ + m_colorDialog = new QColorDialog(); + m_colorDialog->setWindowFlags(Qt::Widget); + m_colorDialog->setOptions(QColorDialog::ShowAlphaChannel | QColorDialog::NoButtons | QColorDialog::DontUseNativeDialog); + m_colorDialog->setCurrentColor(initial); // Needs to be set after setOptions on Linux, which seems to overwrite QColorDialog(initial) + QVBoxLayout *v = new QVBoxLayout(this); + v->addWidget(m_colorDialog); + QHBoxLayout *h = new QHBoxLayout(); + m_noColorButton = new QPushButton("No Color"); + m_cancelButton = new QPushButton("Cancel"); + m_okButton = new QPushButton("OK"); + h->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding)); + h->addWidget(m_noColorButton); + h->addWidget(m_cancelButton); + h->addWidget(m_okButton); + v->addLayout(h); + + connect(m_noColorButton, &QPushButton::clicked, this, &ColorDialog::noColorClicked); + connect(m_cancelButton, &QPushButton::clicked, this, &QDialog::reject); + connect(m_okButton, &QPushButton::clicked, this, &QDialog::accept); + + m_noColorSelected = false; +} + +QColor ColorDialog::selectedColor() const +{ + return m_colorDialog->selectedColor(); +} + +bool ColorDialog::noColorSelected() const +{ + return m_noColorSelected; +} + +void ColorDialog::accept() +{ + m_colorDialog->accept(); + QDialog::accept(); +} + +void ColorDialog::noColorClicked() +{ + m_noColorSelected = true; + accept(); +} diff --git a/sdrgui/gui/colordialog.h b/sdrgui/gui/colordialog.h new file mode 100644 index 000000000..c4dc3874b --- /dev/null +++ b/sdrgui/gui/colordialog.h @@ -0,0 +1,51 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2023-2024 Jon Beniston, M7RCE // +// // +// This program 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 as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program 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 V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#ifndef INCLUDE_GUI_COLORDIALOG_H +#define INCLUDE_GUI_COLORDIALOG_H + +#include +#include + +#include "export.h" + +class QColorDialog; +class QPushButton; + +class SDRGUI_API ColorDialog : public QDialog { + Q_OBJECT + +public: + explicit ColorDialog(const QColor &initial, QWidget *parent = nullptr); + QColor selectedColor() const; + bool noColorSelected() const; + +public slots: + virtual void accept() override; + void noColorClicked(); + +private: + + QColorDialog *m_colorDialog; + QPushButton *m_noColorButton; + QPushButton *m_cancelButton; + QPushButton *m_okButton; + + bool m_noColorSelected; +}; + +#endif // INCLUDE_GUI_COLORDIALOG_H diff --git a/sdrgui/gui/tablecolorchooser.cpp b/sdrgui/gui/tablecolorchooser.cpp new file mode 100644 index 000000000..ada6ddaea --- /dev/null +++ b/sdrgui/gui/tablecolorchooser.cpp @@ -0,0 +1,81 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2023-2024 Jon Beniston, M7RCE // +// // +// This program 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 as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program 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 V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include +#include + +#include "tablecolorchooser.h" +#include "colordialog.h" + +static QString rgbToColor(quint32 rgb) +{ + QColor color = QColor::fromRgba(rgb); + return QString("%1,%2,%3").arg(color.red()).arg(color.green()).arg(color.blue()); +} + +static QString backgroundCSS(quint32 rgb) +{ + // Must specify a border, otherwise we end up with a gradient instead of solid background + return QString("QToolButton { background-color: rgb(%1); border: none; }").arg(rgbToColor(rgb)); +} + +static QString noColorCSS() +{ + return "QToolButton { background-color: black; border: none; }"; +} + +TableColorChooser::TableColorChooser(QTableWidget *table, int row, int col, bool noColor, quint32 color) : + m_noColor(noColor), + m_color(color) +{ + m_colorButton = new QToolButton(table); + m_colorButton->setFixedSize(22, 22); + if (!m_noColor) + { + m_colorButton->setStyleSheet(backgroundCSS(m_color)); + } + else + { + m_colorButton->setStyleSheet(noColorCSS()); + m_colorButton->setText("-"); + } + table->setCellWidget(row, col, m_colorButton); + connect(m_colorButton, &QToolButton::clicked, this, &TableColorChooser::on_color_clicked); +} + +void TableColorChooser::on_color_clicked() +{ + ColorDialog dialog(QColor::fromRgba(m_color), m_colorButton); + if (dialog.exec() == QDialog::Accepted) + { + m_noColor = dialog.noColorSelected(); + if (!m_noColor) + { + m_colorButton->setText(""); + m_color = dialog.selectedColor().rgba(); + m_colorButton->setStyleSheet(backgroundCSS(m_color)); + } + else + { + m_colorButton->setText("-"); + m_colorButton->setStyleSheet(noColorCSS()); + } + } +} diff --git a/sdrgui/gui/tablecolorchooser.h b/sdrgui/gui/tablecolorchooser.h new file mode 100644 index 000000000..aea6cfceb --- /dev/null +++ b/sdrgui/gui/tablecolorchooser.h @@ -0,0 +1,48 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2023-2024 Jon Beniston, M7RCE // +// // +// This program 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 as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program 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 V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#ifndef INCLUDE_GUI_TABLECOLORCHOOSER_H +#define INCLUDE_GUI_TABLECOLORCHOOSER_H + +#include + +#include "export.h" + +class QTableWidget; +class QToolButton; + +// An widget for use in tables, that displays a color, and when clicked, opens a ColorDialog, allowing the user to select a color +class SDRGUI_API TableColorChooser : public QObject { + Q_OBJECT +public: + + TableColorChooser(QTableWidget *table, int row, int col, bool noColor, quint32 color); + +public slots: + void on_color_clicked(); + +private: + QToolButton *m_colorButton; + +public: + // Have copies of settings, so we don't change unless main dialog is accepted + bool m_noColor; + quint32 m_color; + +}; + +#endif // INCLUDE_GUI_TABLECOLORDIALOG_H