File indexing completed on 2024-04-21 14:44:04
0001 /* 0002 SPDX-FileCopyrightText: 2017 Robert Lancaster <rlancaste@gmail.com> 0003 0004 Based on an idea discussed in the QT Centre: http://www.qtcentre.org/threads/47535-QDoubleSpinBox-with-nonlinear-values 0005 0006 SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include "auxiliary/nonlineardoublespinbox.h" 0010 0011 NonLinearDoubleSpinBox::NonLinearDoubleSpinBox(QWidget *parent) : QDoubleSpinBox(parent) 0012 { 0013 m_Values << 0.01 << 0.02 << 0.05 << 0.1 << 0.2 << 0.25 << 0.5 << 1 << 1.5 << 2 << 2.5 << 3 << 5 << 6 << 7 << 8 << 9 << 10 << 20 << 30 << 40 << 50 << 60 << 120 << 180 << 300 << 600 << 900; 0014 setRange(m_Values.first() , m_Values.last()); 0015 0016 //This will update the _idx variable to the index of the new value. It will give -1 if not in the list, and the index if it is in the list. 0017 connect(this, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), 0018 [=](double d){ m_idx = m_Values.indexOf(d); }); 0019 } 0020 0021 void NonLinearDoubleSpinBox::stepBy(int steps) 0022 { 0023 // If the current value is not currently in the list, it will find where the value falls in the list and set it to the 0024 // appropriate one whether going up or down. 0025 if(m_idx == -1) 0026 { 0027 int i = 0; 0028 while(value() > m_Values.at(i)) 0029 i++; 0030 if(steps > 0 ) 0031 m_idx = i; 0032 else 0033 m_idx = i - 1; 0034 } 0035 else 0036 m_idx += steps; 0037 0038 //This makes sure that it doesn't go past the ends of the list. 0039 if(m_idx<0) 0040 m_idx=0; 0041 else if(m_idx >= m_Values.count()) 0042 m_idx = m_Values.count()-1; 0043 0044 //This sets the value to the value at that index in the list. 0045 setValue( m_Values.at(m_idx) ); 0046 } 0047 0048 void NonLinearDoubleSpinBox::setRecommendedValues(QList<double> values) 0049 { 0050 m_Values = values; 0051 updateRecommendedValues(); 0052 } 0053 0054 void NonLinearDoubleSpinBox::addRecommendedValue(double v) 0055 { 0056 m_Values.append(v); //This will be sorted into place in the next command. 0057 updateRecommendedValues(); 0058 } 0059 0060 void NonLinearDoubleSpinBox::updateRecommendedValues() 0061 { 0062 std::sort(m_Values.begin(), m_Values.end()); //This will make sure they are all in order. 0063 m_idx = m_Values.indexOf(value()); //This will update the _idx variable to the index of the new value. It will search for current value in the new list or set it to negative 1 if it isn't in the list. 0064 setRange(m_Values.first() , m_Values.last()); 0065 //This makes sure all the values are in the range. The range is expanded if necessary. 0066 if(m_Values.first() < minimum()) 0067 setMinimum(m_Values.first()); 0068 if(m_Values.last() > maximum()) 0069 setMaximum(m_Values.last()); 0070 } 0071 0072 QList<double> NonLinearDoubleSpinBox::getRecommendedValues() 0073 { 0074 return m_Values; 0075 } 0076 0077 QString NonLinearDoubleSpinBox::getRecommendedValuesString() 0078 { 0079 QString returnString; 0080 for(int i=0; i < m_Values.size() - 1; i++) 0081 returnString += QString::number(m_Values.at(i)) + ", "; 0082 returnString += QString::number(m_Values.last()); 0083 return returnString; 0084 }