1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-22 08:04:49 -05:00
sdrangel/sdrgui/gui/frequencydelegate.cpp

149 lines
5.0 KiB
C++
Raw Permalink Normal View History

2023-05-15 11:40:43 -04:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
// written by Christian Daniel //
// Copyright (C) 2015-2020 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
// Copyright (C) 2021, 2023 Jon Beniston, M7RCE <jon@beniston.com> //
2023-05-15 11:40:43 -04:00
// //
// 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/>. //
///////////////////////////////////////////////////////////////////////////////////
2023-09-28 11:45:35 -04:00
#include <QLineEdit>
2023-05-15 11:40:43 -04:00
#include "frequencydelegate.h"
#include "int64validator.h"
2023-05-15 11:40:43 -04:00
2024-04-09 20:33:42 -04:00
FrequencyDelegate::FrequencyDelegate(const QString& units, int precision, bool group, QObject *parent) :
QStyledItemDelegate(parent),
2023-05-15 11:40:43 -04:00
m_units(units),
m_precision(precision),
m_group(group)
{
}
QString FrequencyDelegate::displayText(const QVariant &value, const QLocale &locale) const
{
bool ok;
qlonglong v = value.toLongLong(&ok);
if (ok)
{
QLocale l(locale);
if (m_group) {
l.setNumberOptions(l.numberOptions() & ~QLocale::OmitGroupSeparator);
2023-09-28 11:45:35 -04:00
}
else {
2023-05-15 11:40:43 -04:00
l.setNumberOptions(l.numberOptions() | QLocale::OmitGroupSeparator);
}
2023-09-28 11:45:35 -04:00
if (m_units == "Auto")
{
if (v == 0)
{
return "0 Hz";
}
else
{
QString s = QString::number(v);
int scale = 1;
while (s.endsWith("000"))
{
s.chop(3);
scale *= 1000;
}
v /= scale;
double d = v;
if ((abs(v) >= 1000) && (m_precision >= 3))
{
scale *= 1000;
d /= 1000.0;
}
QString units;
if (scale == 1) {
units = "Hz";
} else if (scale == 1000) {
units = "kHz";
} else if (scale == 1000000) {
units = "MHz";
} else if (scale == 1000000000) {
units = "GHz";
}
if (scale == 1) {
s = l.toString(d, 'f', 0);
} else {
s = l.toString(d, 'f', m_precision);
}
return QString("%1 %2").arg(s).arg(units);
}
}
else
{
double d;
if (m_units == "GHz") {
d = v / 1000000000.0;
}
else if (m_units == "MHz") {
d = v / 1000000.0;
}
else if (m_units == "kHz") {
d = v / 1000.0;
}
else {
d = v;
}
QString s = l.toString(d, 'f', m_precision);
return QString("%1 %2").arg(s).arg(m_units);
}
2023-05-15 11:40:43 -04:00
}
else
{
return value.toString();
}
}
2023-09-28 11:45:35 -04:00
2023-09-28 11:45:35 -04:00
QWidget* FrequencyDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
2023-10-22 05:25:08 -04:00
(void) option;
(void) index;
2023-09-28 11:45:35 -04:00
QLineEdit* editor = new QLineEdit(parent);
Int64Validator* validator = new Int64Validator();
2023-09-28 11:45:35 -04:00
validator->setBottom(0);
editor->setValidator(validator);
return editor;
}
void FrequencyDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
QString value = index.model()->data(index, Qt::EditRole).toString();
QLineEdit* line = static_cast<QLineEdit*>(editor);
line->setText(value);
}
void FrequencyDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
QLineEdit* line = static_cast<QLineEdit*>(editor);
QString value = line->text();
model->setData(index, value);
}
void FrequencyDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
2023-10-22 05:25:08 -04:00
(void) index;
2023-09-28 11:45:35 -04:00
editor->setGeometry(option.rect);
}