SoDaRadio-5.0.3-master:8901fb5
soda_list_spinbox.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2017 Matthew H. Reilly (kb1vc)
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8 
9  Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in
13  the documentation and/or other materials provided with the
14  distribution.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef SODA_LIST_SPINBOX_H
30 #define SODA_LIST_SPINBOX_H
31 
32 #include <QWidget>
33 #include <QAbstractSpinBox>
34 #include <QSpinBox>
35 #include <QKeyEvent>
36 
37 #include <iostream>
38 #include <boost/format.hpp>
39 
40 #include <cmath>
41 #include <vector>
42 
43 namespace GUISoDa {
44  class NoEditSpinbox : public QSpinBox {
45  public:
46  NoEditSpinbox(QWidget * parent = 0) : QSpinBox(parent) { }
47 
48  protected:
49 
50  void keyPressEvent(QKeyEvent * event) {
51  event->ignore();
52  }
53  };
54 
55  class ListSpinbox : public QSpinBox
56  {
57  public:
58  ListSpinbox(QWidget * parent = 0) : QSpinBox(parent) {
59  init();
60  }
61 
62  ListSpinbox(std::vector<int> vals) {
63  init();
64  setValues(vals);
65  }
66 
67  void init() {
68  cur_index = 0;
69  setAccelerated(false);
70  setReadOnly(true);
71  }
72 
73  void setValues(std::vector<int> vals) {
74  values = vals;
75  setValue(values[cur_index]);
76  setMinimum(values[0]);
77  setMaximum(values[values.size() - 1]);
78  cur_index = values.size() - 1;
79  }
80 
81  void stepBy(int steps) {
82  cur_index += steps;
83  std::cerr << boost::format("stepBy(%d) -> cur_index = %d\n") % steps % cur_index;
84  if(cur_index < 0) cur_index = 0;
85  if(cur_index >= ((int) values.size())) cur_index = values.size() - 1;
86  std::cerr << boost::format("\tvalues[%d] = %d\n") % cur_index % values[cur_index];
87  setValue(values[cur_index]);
88  std::cerr << boost::format("\tnew value = %d\n") % value();
89  }
90 
91  protected:
92  StepEnabled stepEnabled() const {
93  if(values.empty()) return StepEnabled(StepNone);
94  StepEnabled ret;
95  if(cur_index > 0) ret |= StepDownEnabled;
96  if((cur_index + 1) < ((int) values.size())) ret |= StepUpEnabled;
97  return ret;
98  }
99 
100  std::vector<int> values;
101  int cur_index;
102  };
103 }
104 #endif
ListSpinbox(std::vector< int > vals)
StepEnabled stepEnabled() const
std::vector< int > values
void keyPressEvent(QKeyEvent *event)
ListSpinbox(QWidget *parent=0)
NoEditSpinbox(QWidget *parent=0)
void setValues(std::vector< int > vals)