Debian Codegraph @

src/Gui/ExpressionBinding.h

raw
1 /***************************************************************************
2 * Copyright (c) 2015 Eivind Kvedalen <[email protected]> *
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., 51 Franklin Street, *
19 * Fifth Floor, Boston, MA 02110-1301, USA *
20 * *
21 ***************************************************************************/
22
23 #ifndef EXPRESSIONBINDING_H
24 #define EXPRESSIONBINDING_H
25
26 #include <memory>
27 #include <string>
28 #include <App/ObjectIdentifier.h>
29 #include <QPalette>
30 #include <boost/signals2.hpp>
31
32
33 namespace App
34 {
35 class Expression;
36 }
37
38 class ExpressionLabel;
39 class QLineEdit;
40
41 namespace Gui
42 {
43
44 class GuiExport ExpressionBinding
45 {
46 public:
47 ExpressionBinding();
48 virtual ~ExpressionBinding();
49
50 virtual void bind(const App::ObjectIdentifier& _path);
51 virtual void bind(const App::Property& prop);
52 bool isBound() const;
53 virtual void unbind();
54 virtual bool apply(const std::string& propName);
55 virtual bool apply();
56 bool hasExpression() const;
57
58 // auto apply means that the python code is issued not only on apply() but
59 // also on setExpression
60 bool autoApply() const
61 {
62 return m_autoApply;
63 }
64 void setAutoApply(bool value)
65 {
66 m_autoApply = value;
67 }
68
69 protected:
70 const App::ObjectIdentifier& getPath() const
71 {
72 return path;
73 }
74 std::shared_ptr<App::Expression> getExpression() const;
75 std::string getExpressionString(bool no_throw = true) const;
76 std::string getEscapedExpressionString() const;
77 bool assignToProperty(const std::string& propName, double);
78 virtual void setExpression(std::shared_ptr<App::Expression> expr);
79
80 // gets called when the bound expression is changed, either by this binding or any external action
81 virtual void onChange()
82 {}
83
84 private:
85 App::ObjectIdentifier path;
86 std::shared_ptr<App::Expression> lastExpression;
87
88 protected:
89 void expressionChange(const App::ObjectIdentifier& id);
90 void objectDeleted(const App::DocumentObject&);
91 void onDocumentDeleted(const App::Document&);
92 boost::signals2::scoped_connection expressionchanged;
93 boost::signals2::scoped_connection objectdeleted;
94 boost::signals2::scoped_connection documentdeleted;
95 bool m_autoApply {false};
96 };
97
98 class GuiExport ExpressionWidget: public ExpressionBinding
99 {
100 public:
101 ExpressionWidget();
102 QPixmap getIcon(const char* name, const QSize& size) const;
103 void unbind() override;
104
105 protected:
106 void makeLabel(QLineEdit* parent);
107
108 protected:
109 ExpressionLabel* iconLabel {nullptr};
110 QPalette defaultPalette;
111 int iconHeight {-1};
112 };
113
114 } // namespace Gui
115
116 #endif // EXPRESSIONBINDING_H