tilde~
Puredata Qt-based GUI
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
UIScript copy.h
Go to the documentation of this file.
1 // (c) 2017 Alex Nadzharov
2 // License: GPL3
3 
4 #ifndef cmo_script_H
5 #define cmo_script_H
6 
7 #include <QPlainTextEdit>
8 
9 #include "Port.h"
10 #include "UIObject.h"
11 
12 #include <QFileDialog>
13 
14 //#include "cm_pdlink.h"
15 
16 #include "UIScriptTextEdit.h"
17 
18 namespace qtpd {
19 
23 class UIScript : public UIObject {
24  Q_OBJECT
25 
26 private:
27  bool _clicked;
28  UIScriptTextEdit* _editor;
29  QStringList _inputList;
30 
31 public:
32  explicit UIScript();
33 
34  static UIObject* createObject(QString objectData, t_canvas* pdCanvas, QGraphicsView* parent = 0)
35  {
36  qDebug() << "ui.script";
37 
38  UIScript* b = new UIScript();
39  b->setCanvas((void*)parent);
40 
41  b->_editor->setContext(pyWrapper::inst().withCanvas((QObject*)parent));
42 
43  QString data1 = b->properties()->extractFromPdFileString(objectData);
44  //if (data1 != "")
45 
46  b->setObjectData("ui.script");
47 
48  // the zoo lol
49  QString data = b->properties()->get("Script")->asQString().split("\\n ").join("\n");
50  b->_editor->document()->setPlainText(data);
51 
52  // pd object
53  std::string message = "ui.script";
54 
55  //temp
56  t_object* new_obj = 0;
57  if (!pdCanvas) {
58  qDebug("bad pd canvas instance");
59  } else {
60  QPoint pos = QPoint(0, 0);
61  new_obj = cmp_create_object(pdCanvas, message, pos.x(), pos.y());
62  }
63 
64  if (new_obj) {
65  qDebug("created ui.script %s | ptr %lu\n", message.c_str(), (long)new_obj);
66  b->setPdObject(new_obj);
67 
68  b->_editor->setContext(pyWrapper::inst().withCanvasPdObjectAndInput((UIObject*)parent, new_obj, &b->_inputList));
69 
70  b->addInlet();
71  b->addOutlet();
72 
73  } else {
74  qDebug("Error: no such object %s", message.c_str());
75  }
76 
77  b->resizeEvent();
78 
79  return (UIObject*)b;
80  };
81 
83  {
84  UIObject::initProperties();
85  QStringList list; // = QString("#empty").split("-"); //lol
86 
87  properties()->create("Script", "Data", "0.1", list);
88  };
89 
90  virtual void paint(QPainter* p, const QStyleOptionGraphicsItem* option, QWidget*)
91  {
92  p->setClipRect(option->exposedRect);
93 
94  QBrush brush(bgColor());
95  p->setBrush(brush);
96  p->drawRect(boundingRect());
97  p->setBrush(QBrush());
98 
99  if (getEditMode() == em_Unlocked) {
100  if (isSelected()) {
101  p->setPen(QPen(QColor(0, 192, 255), 1, Qt::SolidLine, Qt::SquareCap, Qt::BevelJoin));
102  } else if (_clicked) {
103  p->setPen(QPen(QColor(0, 192, 255), 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
104  } else {
105  p->setPen(QPen(QColor(128, 128, 128), 1, Qt::SolidLine, Qt::SquareCap, Qt::BevelJoin));
106  }
107 
108  p->drawRect(0, 0, width(), height());
109  }
110  }
111 
112  void resizeEvent()
113  {
114  UIObject::resizeEvent();
115  _editor->setFixedWidth(width() - 5);
116  _editor->setFixedHeight(height() - 25);
117  }
118 
119  // ------------------------
120 
121  void mousePressEvent(QGraphicsSceneMouseEvent* ev)
122  {
123 
124  emit selectBox(this, ev);
125  dragOffset = ev->pos().toPoint();
126  ev->accept();
127  }
128 
129  void mouseReleaseEvent(QGraphicsSceneMouseEvent*ev)
130  {
131  ev->accept();
132  }
133 
134  void mouseMoveEvent(QGraphicsSceneMouseEvent* event)
135  {
136  if (event->buttons() & Qt::LeftButton) {
137  emit moveBox(this, event);
138  }
139  event->accept();
140 
141  //todo move!
142  if (getEditMode() != em_Unlocked) {
143  setCursor(QCursor(Qt::PointingHandCursor));
144  } else {
145  setCursor(QCursor(Qt::ArrowCursor));
146  }
147  }
148 
149  // ----------------------
150 
151  void setPdMessage(QString message)
152  {
153  setObjectData(message);
154 
155  setSize(300, 200);
156  }
157 
158  static void updateUI(void* uiobj, ceammc::AtomList msg)
159  {
160  qDebug("script << pd message");
161  qDebug() << (long)uiobj << msg.size();
162 
163  UIScript* x = (UIScript*)uiobj;
164  if (x) {
165  //todo atomlist to qstringlist; atom as qstring
166  QStringList list;
167  for (size_t i = 0; i < msg.size(); i++) {
168  list.push_back(msg.at(i).asString().c_str());
169  }
170 
171  x->_inputList = list;
172  emit x->callRun();
173  }
174  }
175 
176  QStringList getEditorData()
177  {
178  return _editor->document()->toPlainText().split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
179  }
180 
181  void setPdObject(void* obj)
182  {
183  UIObject::setPdObject(obj);
184  connect(this, &UIScript::callRun, this, &UIScript::btnRun);
185  cmp_connectUI((t_pd*)pdObject(), (void*)this, &UIScript::updateUI);
186 
187  //cmp_connectUI("ui.script", (void*)this, &UIScript::updateUI);
188 
189  qDebug() << "connect ui: uiobj " << (long)this << " pdobj " << (long)pdObject();
190  }
191 signals:
192  void callRun();
193 
194 public slots:
195  void btnRun()
196  {
197  qDebug() << "btnRun";
198 
199  //this code is from PythonQt
200 
201  PythonQtObjectPtr context = _editor->context(); //PythonQt::self()->getMainModule();
202  QString _stdOut = "";
203  QString _stdErr = "";
204  PythonQtObjectPtr p;
205  PyObject* dict = NULL;
206  if (PyModule_Check(context)) {
207  dict = PyModule_GetDict(context);
208  } else if (PyDict_Check(context)) {
209  dict = context;
210  }
211 
212  QStringList list = getEditorData();
213 
214  QString line = list.join("\r\n"); //*it;
215 
216  if (!line.isNull()) {
217 
218  if (dict) {
219  context.evalScript(line);
220 
221  qDebug() << "line: " << line;
222  }
223 
224  if (!p) {
225  PythonQt::self()->handleError();
226  }
227 
228  if (_stdOut != "") {
229 
230  cmp_post((std::string) "Python: " + _stdOut.toStdString());
231  }
232  if (_stdErr != "") {
233  cmp_post((std::string) "Python error: " + _stdOut.toStdString());
234  }
235  }
236  }
237 
238 private slots:
239  void editorChanged();
240 
241  void btnLoad()
242  {
243  QString fname = QFileDialog::getOpenFileName(0, QString("Open Python script"), QString("~/"), QString("*.py"), 0, 0);
244  if (fname != "") {
245  QFile file(fname);
246 
247  file.open(QFile::ReadOnly | QFile::Text);
248 
249  QTextStream ReadFile(&file);
250  _editor->document()->setPlainText(ReadFile.readAll());
251  file.close();
252  }
253  }
254 
255  void btnSave()
256  {
257  QString fname = QFileDialog::getSaveFileName(0, QString("Save Python script"), QString("~/"), QString("*.py"), 0, 0);
258  if (fname != "") {
259  QFile file(fname);
260 
261  file.open(QFile::WriteOnly | QFile::Text);
262 
263  QTextStream WriteFile(&file);
264  WriteFile << _editor->document()->toPlainText();
265  file.close();
266  }
267  }
268 
269  void btnClear()
270  {
271  _editor->clear();
272  }
273 };
274 }
275 #endif // cmo_text_H
gui object: script editor box (ui.script)
Definition: UIScript copy.h:23
QStringList getEditorData()
Definition: UIScript copy.h:176
static UIObject * createObject(QString objectData, t_canvas *pdCanvas, QGraphicsView *parent=0)
Definition: UIScript copy.h:34
static void updateUI(void *uiobj, ceammc::AtomList msg)
Definition: UIScript copy.h:158
void setPdMessage(QString message)
Definition: UIScript copy.h:151
void resizeEvent()
Definition: UIScript copy.h:112
virtual void paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *)
Definition: UIScript copy.h:90
#define t_canvas
Definition: m_pd.h:205
Definition: CommonTypes.h:14
void setPdObject(void *obj)
Definition: UIScript copy.h:181
void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
Definition: UIScript copy.h:134
Atom & at(size_t pos)
returns reference to element at specified position
Definition: ceammc_atomlist.h:29
void btnRun()
Definition: UIScript copy.h:195
t_class * t_pd
Definition: m_pd.h:207
void initProperties()
Definition: UIScript copy.h:82
void mousePressEvent(QGraphicsSceneMouseEvent *ev)
Definition: UIScript copy.h:121
UIScript()
Definition: UIScript copy.cpp:13
size_t size() const
Definition: m_pd.h:222
void mouseReleaseEvent(QGraphicsSceneMouseEvent *ev)
Definition: UIScript copy.h:129
std::string asString() const
Definition: g_canvas.h:89