| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#ifndef GUI_SPINBOX_H |
| 24 |
#define GUI_SPINBOX_H |
| 25 |
|
| 26 |
#include <QSpinBox> |
| 27 |
#include <QValidator> |
| 28 |
#include "ExpressionBinding.h" |
| 29 |
|
| 30 |
|
| 31 |
class QStyleOptionSpinBox; |
| 32 |
|
| 33 |
namespace App |
| 34 |
{ |
| 35 |
class NumberExpression; |
| 36 |
} |
| 37 |
|
| 38 |
namespace Gui |
| 39 |
{ |
| 40 |
|
| 41 |
class GuiExport ExpressionSpinBox: public ExpressionWidget |
| 42 |
{ |
| 43 |
public: |
| 44 |
explicit ExpressionSpinBox(QAbstractSpinBox*); |
| 45 |
~ExpressionSpinBox() override; |
| 46 |
|
| 47 |
void bind(const App::ObjectIdentifier& _path) override; |
| 48 |
void setExpression(std::shared_ptr<App::Expression> expr) override; |
| 49 |
|
| 50 |
protected: |
| 51 |
/*! Expression handling */ |
| 52 |
//@{ |
| 53 |
enum class Number |
| 54 |
{ |
| 55 |
KeepCurrent = 0, |
| 56 |
SetIfNumber = 1 |
| 57 |
}; |
| 58 |
void showInvalidExpression(const QString&); |
| 59 |
void showValidExpression(Number number); |
| 60 |
void clearExpression(); |
| 61 |
void updateExpression(); |
| 62 |
//@} |
| 63 |
|
| 64 |
void onChange() override; |
| 65 |
virtual void setNumberExpression(App::NumberExpression*) = 0; |
| 66 |
virtual void showIcon(); |
| 67 |
virtual void validateInput(); |
| 68 |
void resizeWidget(); |
| 69 |
int getMargin(); |
| 70 |
|
| 71 |
bool handleKeyEvent(const QString&); |
| 72 |
virtual void openFormulaDialog(); |
| 73 |
|
| 74 |
void drawControl(QStyleOptionSpinBox&); |
| 75 |
|
| 76 |
private: |
| 77 |
void showExpression(Number number); |
| 78 |
|
| 79 |
protected: |
| 80 |
QLineEdit* lineedit; |
| 81 |
QAbstractSpinBox* spinbox; |
| 82 |
}; |
| 83 |
|
| 84 |
/** |
| 85 |
* A validator that allows only input of unsigned int values in the range |
| 86 |
* from 0 to UINT_MAX. |
| 87 |
*/ |
| 88 |
class GuiExport UnsignedValidator: public QValidator |
| 89 |
{ |
| 90 |
Q_OBJECT |
| 91 |
Q_PROPERTY(uint bottom READ bottom WRITE setBottom) // clazy:exclude=qproperty-without-notify |
| 92 |
Q_PROPERTY(uint top READ top WRITE setTop) // clazy:exclude=qproperty-without-notify |
| 93 |
|
| 94 |
public: |
| 95 |
explicit UnsignedValidator(QObject* parent); |
| 96 |
UnsignedValidator(uint bottom, uint top, QObject* parent); |
| 97 |
~UnsignedValidator() override; |
| 98 |
|
| 99 |
QValidator::State validate(QString&, int&) const override; |
| 100 |
|
| 101 |
void setBottom(uint); |
| 102 |
void setTop(uint); |
| 103 |
virtual void setRange(uint bottom, uint top); |
| 104 |
|
| 105 |
uint bottom() const |
| 106 |
{ |
| 107 |
return b; |
| 108 |
} |
| 109 |
uint top() const |
| 110 |
{ |
| 111 |
return t; |
| 112 |
} |
| 113 |
|
| 114 |
private: |
| 115 |
uint b, t; |
| 116 |
}; |
| 117 |
|
| 118 |
class UIntSpinBoxPrivate; |
| 119 |
/** |
| 120 |
* The UIntSpinBox class does basically the same as Qt's QSpinBox unless |
| 121 |
* that it works with unsigned int's instead. |
| 122 |
* This allows one to use numbers in the range of [0, UINT_MAX] |
| 123 |
* @author Werner Mayer |
| 124 |
*/ |
| 125 |
class GuiExport UIntSpinBox: public QSpinBox, public ExpressionSpinBox |
| 126 |
{ |
| 127 |
Q_OBJECT |
| 128 |
Q_OVERRIDE(uint maximum READ maximum WRITE setMaximum) |
| 129 |
Q_OVERRIDE(uint minimum READ minimum WRITE setMinimum) |
| 130 |
Q_OVERRIDE(uint value READ value WRITE setValue) |
| 131 |
|
| 132 |
public: |
| 133 |
explicit UIntSpinBox(QWidget* parent = nullptr); |
| 134 |
~UIntSpinBox() override; |
| 135 |
|
| 136 |
void setRange(uint minVal, uint maxVal); |
| 137 |
uint value() const; |
| 138 |
QValidator::State validate(QString& input, int& pos) const override; |
| 139 |
uint minimum() const; |
| 140 |
void setMinimum(uint value); |
| 141 |
uint maximum() const; |
| 142 |
void setMaximum(uint value); |
| 143 |
|
| 144 |
bool apply(const std::string& propName) override; |
| 145 |
using ExpressionSpinBox::apply; |
| 146 |
|
| 147 |
void keyPressEvent(QKeyEvent* event) override; |
| 148 |
void resizeEvent(QResizeEvent* event) override; |
| 149 |
void paintEvent(QPaintEvent* event) override; |
| 150 |
|
| 151 |
Q_SIGNALS: |
| 152 |
void unsignedChanged(uint value); |
| 153 |
|
| 154 |
public Q_SLOTS: |
| 155 |
void setValue(uint value); |
| 156 |
|
| 157 |
private Q_SLOTS: |
| 158 |
void valueChange(int value); |
| 159 |
|
| 160 |
protected: |
| 161 |
QString textFromValue(int v) const override; |
| 162 |
int valueFromText(const QString& text) const override; |
| 163 |
void setNumberExpression(App::NumberExpression*) override; |
| 164 |
|
| 165 |
private: |
| 166 |
void updateValidator(); |
| 167 |
UIntSpinBoxPrivate* d; |
| 168 |
}; |
| 169 |
|
| 170 |
|
| 171 |
/** |
| 172 |
* The IntSpinBox class does exactly the same as Qt's QSpinBox but has expression support |
| 173 |
* @author Stefan Tröger |
| 174 |
*/ |
| 175 |
class GuiExport IntSpinBox: public QSpinBox, public ExpressionSpinBox |
| 176 |
{ |
| 177 |
Q_OBJECT |
| 178 |
|
| 179 |
public: |
| 180 |
explicit IntSpinBox(QWidget* parent = nullptr); |
| 181 |
~IntSpinBox() override; |
| 182 |
|
| 183 |
bool apply(const std::string& propName) override; |
| 184 |
using ExpressionSpinBox::apply; |
| 185 |
void setNumberExpression(App::NumberExpression*) override; |
| 186 |
|
| 187 |
void keyPressEvent(QKeyEvent* event) override; |
| 188 |
void resizeEvent(QResizeEvent* event) override; |
| 189 |
void paintEvent(QPaintEvent* event) override; |
| 190 |
}; |
| 191 |
|
| 192 |
/** |
| 193 |
* The DoubleSpinBox class does exactly the same as Qt's QDoubleSpinBox but has expression |
| 194 |
* support |
| 195 |
* @author Stefan Tröger |
| 196 |
*/ |
| 197 |
class GuiExport DoubleSpinBox: public QDoubleSpinBox, public ExpressionSpinBox |
| 198 |
{ |
| 199 |
Q_OBJECT |
| 200 |
|
| 201 |
public: |
| 202 |
explicit DoubleSpinBox(QWidget* parent = nullptr); |
| 203 |
~DoubleSpinBox() override; |
| 204 |
|
| 205 |
bool apply(const std::string& propName) override; |
| 206 |
using ExpressionSpinBox::apply; |
| 207 |
void setNumberExpression(App::NumberExpression*) override; |
| 208 |
|
| 209 |
void keyPressEvent(QKeyEvent* event) override; |
| 210 |
void resizeEvent(QResizeEvent* event) override; |
| 211 |
void paintEvent(QPaintEvent* event) override; |
| 212 |
}; |
| 213 |
|
| 214 |
} // namespace Gui |
| 215 |
|
| 216 |
#endif // GUI_SPINBOX_H |