File indexing completed on 2022-11-29 18:26:11

0001 /*
0002 * Tests the KNumInput Widget class
0003 *
0004 * Copyright 1999 by Dirk A. Mueller <dmuell@gmx.net>
0005 *
0006 * Licensed under the GNU General Public License version 2 or later
0007 */
0008 
0009 #include <QApplication>
0010 #include <QLayout>
0011 #include <QGroupBox>
0012 #include <QBoxLayout>
0013 
0014 #include <klocalizedstring.h>
0015 #include <knuminput.h>
0016 
0017 #include "knuminputtest.h"
0018 
0019 #include <kdebug.h>
0020 
0021 void TopLevel::slotPrint(int n)
0022 {
0023     kDebug() << "slotPrint( " << n << " )";
0024 }
0025 void TopLevel::slotPrint(double n)
0026 {
0027     kDebug() << "slotPrint( " << n << " )";
0028 }
0029 
0030 #define conn(x,y) connect( x, SIGNAL(valueChanged(y)), SLOT(slotPrint(y)))
0031 TopLevel::TopLevel(QWidget *parent)
0032     : QWidget(parent)
0033 {
0034     setWindowTitle("KNumInput test application");
0035 
0036     QBoxLayout *l = new QHBoxLayout(this);
0037     l->setMargin(10);
0038 
0039     QGroupBox *b1 = new QGroupBox("KIntNumInput", this);
0040     b1->setLayout(new QVBoxLayout());
0041 
0042     i1 = new KIntNumInput(42, b1, 10 /*"perc_no_slider"*/);
0043     i1->setLabel("percent of usage (no slider)");
0044     i1->setRange(0, 100, 5);
0045     i1->setSliderEnabled(false);
0046     conn(i1, int);
0047     b1->layout()->addWidget(i1);
0048 
0049     i2 = new KIntNumInput(42, b1);
0050     i2->setLabel("percentage of usage (with slider)");
0051     i2->setRange(0, 100, 5);
0052     i2->setSuffix(" %");
0053     i2->setSliderEnabled(true);
0054     conn(i2, int);
0055     b1->layout()->addWidget(i2);
0056 
0057     i3 = new KIntNumInput(0xAF, b1, 16);
0058     i3->setLabel("Hex byte (no slider)");
0059     i3->setRange(0, 255, 1);
0060     i3->setSliderEnabled(false);
0061     i3->setSuffix(" (hex)");
0062     conn(i3, int);
0063     b1->layout()->addWidget(i3);
0064 
0065     i4 = new KIntNumInput(0xfe,  b1, 16);
0066     i4->setLabel("Hex byte (with slider)");
0067     i4->setRange(0, 255, 1);
0068     i4->setSliderEnabled(true);
0069     conn(i4, int);
0070     b1->layout()->addWidget(i4);
0071 
0072     i5 = new KIntNumInput(10, b1, 10);
0073     i5->setLabel("Width (keeps aspect ratio):");
0074     i5->setRange(0, 200);
0075     i5->setSliderEnabled(false);
0076     i5->setReferencePoint(5);
0077     b1->layout()->addWidget(i5);
0078 
0079     i6 = new KIntNumInput(20, b1, 10);
0080     i6->setLabel("Height (should be 2xWidth value):");
0081     i6->setRange(0, 200);
0082     i6->setReferencePoint(10);
0083     i6->setSliderEnabled(false);
0084     b1->layout()->addWidget(i6);
0085 
0086     connect(i5, SIGNAL(relativeValueChanged(double)),
0087             i6, SLOT(setRelativeValue(double)));
0088     connect(i6, SIGNAL(relativeValueChanged(double)),
0089             i5, SLOT(setRelativeValue(double)));
0090 
0091     i7 = new KIntNumInput(0, b1, 10);
0092     i7->setLabel("math test:", Qt::AlignVCenter | Qt::AlignLeft);
0093     i7->setRange(0, 200, 1);
0094     conn(i7, int);
0095     b1->layout()->addWidget(i7);
0096 
0097     i8 = new KIntNumInput(0, b1, 10);
0098     i8->setLabel("plural test:", Qt::AlignVCenter | Qt::AlignLeft);
0099     i8->setRange(0, 100, 1);
0100     i8->setSuffix(ki18np(" suffix", " suffixes"));
0101     b1->layout()->addWidget(i8);
0102 
0103     l->addWidget(b1);
0104 
0105     QGroupBox *b2 = new QGroupBox("KDoubleNumInput", this);
0106     b2->setLayout(new QVBoxLayout());
0107     d1 = new KDoubleNumInput(10, 4.0, 1, b2, 2 /*, "perc_double_no_slider"*/);
0108     d1->setLabel("percent of usage (no slider)", Qt::AlignTop | Qt::AlignRight);
0109     d1->setRange(0.0, 4000.0, 0.01, false);
0110     //d1->setValue(1.00000000000000000001);
0111     conn(d1, double);
0112     b2->layout()->addWidget(d1);
0113 
0114     d2 = new KDoubleNumInput(0, 20, 0.422, b2, 0.1, 3/*, "perc_double_with_slider"*/);
0115     d2->setLabel("percentage of usage (with slider)", Qt::AlignBottom | Qt::AlignLeft);
0116     d2->setRange(0, 0.05, 0.005);
0117     d2->setSuffix("%");
0118     conn(d2, double);
0119     b2->layout()->addWidget(d2);
0120 
0121     d3 = new KDoubleNumInput(0, 20, 16.20, b2);
0122     d3->setLabel("cash: ", Qt::AlignVCenter | Qt::AlignHCenter);
0123     d3->setRange(0.10, 100, 0.1);
0124     d3->setPrefix("p");
0125     d3->setSuffix("$");
0126     conn(d3, double);
0127     b2->layout()->addWidget(d3);
0128 
0129     d4 = new KDoubleNumInput(0, INT_MAX, INT_MAX / 10000.0, b2, 1, 1);
0130 //     d4->setPrecision(3);
0131     d4->setRange(double(INT_MIN + 1) / 1000.0, double(INT_MAX) / 1000.0, 1);
0132     d4->setLabel("math test: ", Qt::AlignVCenter | Qt::AlignLeft);
0133 //    d4->setFormat("%g");
0134     conn(d4, double);
0135     b2->layout()->addWidget(d4);
0136 
0137     d5 = new KDoubleNumInput(double(INT_MIN + 1) / 1e9, double(INT_MAX - 1) / 1e9,
0138                              0.1, b2, 0.001, 9 /*, "d5"*/);
0139     d5->setLabel("math test 2: ", Qt::AlignVCenter | Qt::AlignLeft);
0140     conn(d5, double);
0141     b2->layout()->addWidget(d5);
0142 
0143     d6 = new KDoubleNumInput(-10, 10, 0, b2, 0.001, 3 /*, "d6"*/);
0144     d6->setLabel("aspect ratio test with a negative ratio:");
0145     d6->setReferencePoint(1);
0146     b2->layout()->addWidget(d6);
0147 
0148     d7 = new KDoubleNumInput(-30, 30, 0, b2, 0.001, 3 /*, "d7"*/);
0149     d7->setReferencePoint(-3);
0150     b2->layout()->addWidget(d7);
0151 
0152     connect(d6, SIGNAL(relativeValueChanged(double)),
0153             d7, SLOT(setRelativeValue(double)));
0154     connect(d7, SIGNAL(relativeValueChanged(double)),
0155             d6, SLOT(setRelativeValue(double)));
0156 
0157     l->addWidget(b2);
0158 }
0159 
0160 int main(int argc, char **argv)
0161 {
0162     QApplication::setApplicationName("KNuminputTesT");
0163 
0164     QApplication a(argc, argv);
0165 
0166     TopLevel *toplevel = new TopLevel(nullptr);
0167     toplevel->show();
0168     a.exec();
0169 }
0170