1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-25 17:28:50 -05:00

Add GUI ColorDialog.

This commit is contained in:
srcejon 2024-03-28 15:24:39 +00:00
parent 605628567b
commit b890c32f13
5 changed files with 256 additions and 0 deletions

View File

@ -17,6 +17,7 @@ set(sdrgui_SOURCES
gui/buttonswitch.cpp gui/buttonswitch.cpp
gui/channeladddialog.cpp gui/channeladddialog.cpp
gui/clickablelabel.cpp gui/clickablelabel.cpp
gui/colordialog.cpp
gui/colormapper.cpp gui/colormapper.cpp
gui/commanditem.cpp gui/commanditem.cpp
gui/commandsdialog.cpp gui/commandsdialog.cpp
@ -88,6 +89,7 @@ set(sdrgui_SOURCES
gui/spectrummarkersdialog.cpp gui/spectrummarkersdialog.cpp
gui/spectrummeasurementsdialog.cpp gui/spectrummeasurementsdialog.cpp
gui/spectrummeasurements.cpp gui/spectrummeasurements.cpp
gui/tablecolorchooser.cpp
gui/tabletapandhold.cpp gui/tabletapandhold.cpp
gui/tickedslider.cpp gui/tickedslider.cpp
gui/timedelegate.cpp gui/timedelegate.cpp
@ -142,6 +144,7 @@ set(sdrgui_HEADERS
gui/basicfeaturesettingsdialog.h gui/basicfeaturesettingsdialog.h
gui/buttonswitch.h gui/buttonswitch.h
gui/channeladddialog.h gui/channeladddialog.h
gui/colordialog.h
gui/colormapper.h gui/colormapper.h
gui/commanditem.h gui/commanditem.h
gui/commandsdialog.h gui/commandsdialog.h
@ -217,6 +220,7 @@ set(sdrgui_HEADERS
gui/spectrummarkersdialog.h gui/spectrummarkersdialog.h
gui/spectrummeasurementsdialog.h gui/spectrummeasurementsdialog.h
gui/spectrummeasurements.h gui/spectrummeasurements.h
gui/tablecolorchooser.h
gui/tabletapandhold.h gui/tabletapandhold.h
gui/tickedslider.h gui/tickedslider.h
gui/timedelegate.h gui/timedelegate.h

View File

@ -0,0 +1,72 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2022 Jon Beniston, M7RCE <jon@beniston.com> //
// //
// 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 <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QDebug>
#include <QColorDialog>
#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();
}

51
sdrgui/gui/colordialog.h Normal file
View File

@ -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 <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDE_GUI_COLORDIALOG_H
#define INCLUDE_GUI_COLORDIALOG_H
#include <QColor>
#include <QDialog>
#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

View File

@ -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 <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <QColor>
#include <QColor>
#include <QToolButton>
#include <QDialog>
#include <QTableWidget>
#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());
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDE_GUI_TABLECOLORCHOOSER_H
#define INCLUDE_GUI_TABLECOLORCHOOSER_H
#include <QObject>
#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