tilde~
Puredata Qt-based GUI
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
UIClass.h
Go to the documentation of this file.
1 // (c) 2017 Alex Nadzharov
2 // License: GPL3
3 
4 #ifndef CM_UICLASS_H
5 #define CM_UICLASS_H
6 
7 #include <QLineEdit>
8 #include <QMainWindow>
9 #include <QtGui>
10 
11 #include "Port.h"
12 
13 #include "UIObject.h"
14 #include "UIBox.h"
15 
16 #include "OOPDHeaders.h"
17 
18 #include <QGraphicsView>
19 
20 #include "PropertyList.h"
21 #include "Preferences.h"
22 
23 #include "UIObjectData.h"
24 
25 namespace tilde {
26 
30 class UIClass : public UIBox {
31 
32  Q_OBJECT
33 
34 private:
35  OPClass* _opClass;
36 
37  bool _dynamicClass;
38 
39 public:
40  explicit UIClass(); //UIObject* parent = 0);
42  {
43  if (_opClass) {
44  _opClass->writeFile();
45  }
46  };
47 
48  static UIObject* createObject(QString objectData, t_canvas* pdCanvas, QGraphicsView* parent = 0)
49  {return 0;} /*
50 
51  {
52  //TODO fix all constructors
53  //t_canvas* pd_Canvas;
54 
55  //temporary fix
56  if (objectData == "")
57  objectData = "pdclass";
58 
59  UIClass* b = new UIClass(); //(UIObject*)parent);
60  //b->setCanvas((void*)parent);
61 
62  QStringList list = QString(objectData).split(" ");
63 
64  const char* obj_name = objectData.toStdString().c_str();
65  QString data1 = b->properties()->extractFromPdFileString(obj_name); //test
66  //const char* obj_name2 = data1.c_str();
67 
68  // fix size changes
69  b->setObjectData(data1);
70  b->autoResize();
71 
72  qDebug() << QString(data1) << "data1";
73 
74  t_object* new_obj = 0;
75  int in_c = 1, out_c = 0;
76 
77  b->setHelpName("pdclass-help.pd");
78 
79  if (!pdCanvas) {
80  qDebug("bad pd canvas instance");
81  b->setErrorBox(true);
82  } else {
83  new_obj = cmp_create_object(pdCanvas, "pdclass", 0, 0);
84  }
85 
86  // new class w/canvas
87  if (list.size() > 1) {
88 
89  b->_opClass = new OPClass(list.at(1).toStdString());
90  // TODO FIX
91  //
92 
93  //attempt to load
94  b->_opClass->readFile();
95 
96  b->_dynamicClass = false;
97 
98  } else {
99  b->_opClass = 0; //new OPClass();
100  b->_dynamicClass = true;
101  }
102 
103  if (new_obj) {
104  in_c = cmp_get_inlet_count(new_obj);
105  out_c = cmp_get_outlet_count(new_obj);
106 
107  //b->setPdObject(new_obj);
108 
109  cmp_connectUI((t_pd*)new_obj, (void*)b, &UIClass::updateUI);
110 
111  } else {
112  qDebug("Error: no pd object");
113  b->setErrorBox(true);
114  in_c = 1;
115  out_c = 0;
116  }
117 
118  for (int i = 0; i < in_c; i++)
119  b->addInlet();
120  for (int i = 0; i < out_c; i++)
121  b->addOutlet();
122 
123  connect(b, &UIClass::updateUISignal, b, &UIClass::updateUISlot);
124 
125  return (UIObject*)b;
126  };
127 */
131  //void paintEvent(QPaintEvent*)
132  virtual void paint(QPainter* p, const QStyleOptionGraphicsItem*, QWidget*)
133 
134  {
135  QBrush brush(bgColor());
136  p->setBrush(brush);
137  p->drawRect(boundingRect());
138  p->setBrush(QBrush());
139 
140  //QPainter p(viewport());
141  p->setRenderHint(QPainter::HighQualityAntialiasing, true);
142  p->scale(scale(), scale());
143 
144  p->setPen(QPen(QColor(255, 192, 0), 2, Qt::SolidLine, Qt::SquareCap, Qt::BevelJoin));
145  p->drawRect(0, 1, width(), height() - 2);
146 
147  //remove this later
148 // if (subpatchWindow()) {
149 // p->setPen(QPen(QColor(192, 192, 192), 1, Qt::SolidLine, Qt::SquareCap, Qt::BevelJoin));
150 // p->drawRect(0, 2, width(), height() - 4);
151 // }
152 
153  QColor rectColor = (errorBox()) ? QColor(255, 0, 0) : properties()->get("BorderColor")->asQColor(); //QColor(128, 128, 128);
154  p->setPen(QPen(rectColor, 1, (errorBox()) ? Qt::DashLine : Qt::SolidLine, Qt::SquareCap, Qt::BevelJoin));
155  p->drawRect(0, 0, width(), height());
156  QTextOption* op = new QTextOption;
157  op->setAlignment(Qt::AlignLeft);
158  p->setPen(QPen(QColor(0, 0, 0), 1, Qt::SolidLine, Qt::SquareCap, Qt::BevelJoin));
159 
160  p->setFont(QFont(PREF_QSTRING("Font"), properties()->get("FontSize")->asFontSize(), 0, false));
161  p->drawText(2, 3, width() - 2, height() - 3, 0, objectData()->toQString(), 0);
162 
163  if (isSelected()) {
164  p->setPen(QPen(QColor(0, 192, 255), 1, (errorBox()) ? Qt::DashLine : Qt::SolidLine, Qt::SquareCap, Qt::BevelJoin));
165  p->drawRect(0, 0, width(), height());
166  }
167  }
168 
173  void mousePressEvent(QGraphicsSceneMouseEvent* ev)
174  {
175  //context menu
176  if (ev->button() == Qt::RightButton) {
177 
178  if (scene()
179  && !scene()->views().isEmpty()
180  && scene()->views().first()
181  && scene()->views().first()->viewport()) {
182 
183  QGraphicsView* v = scene()->views().first();
184  QPoint pos = v->viewport()->mapToGlobal(ev->pos().toPoint());
185 
186  // TODO
187  showPopupMenu(pos + this->pos().toPoint());
188  ev->accept();
189  }
190 
191  return;
192  }
193 
194  //open canvas for subpatch
195  // if (getEditMode() != em_Unlocked) {
196  // if (subpatchWindow()) {
197  // subpatchWindow()->show();
198  // }
199  // }
200 
201  //window opening. Fix
202  if (getEditMode() != em_Unlocked) {
203  _opClass->showWindow();
204  }
205 
206  if ((getEditMode() == em_Unlocked) && isSelected()) {
207  // editor_->setText(QString(_objectDataModel.objectData()));
208  // editor_->show();
209 
210  emit signalEditObject(this);
211  return;
212  }
213 
214  emit selectBox(this, ev);
215  dragOffset = ev->pos().toPoint();
216  }
217 
221  void mouseReleaseEvent(QGraphicsSceneMouseEvent*)
222  {
223  update();
224  }
225 
230  void mouseMoveEvent(QGraphicsSceneMouseEvent* event)
231  {
232  if (event->buttons() & Qt::LeftButton) {
233  emit moveBox(this, event);
234  }
235  event->ignore();
236 
237  if ((getEditMode() != em_Unlocked) && (subpatchController())) {
238  setCursor(QCursor(Qt::PointingHandCursor));
239  } else {
240  setCursor(QCursor(Qt::ArrowCursor));
241  }
242  }
243 
244  void setPdMessage(QString message)
245  {
246  fromQString(message);
247  autoResize();
248 
249  QFont myFont(PREF_QSTRING("Font"), 11);
250  QFontMetrics fm(myFont);
251  int new_w = fm.width(QString(objectData()->toQString())) + 10;
252  new_w = (new_w < 25) ? 25 : new_w;
253  setWidth(new_w);
254  //editor_->setFixedWidth(width() - 5);
255 
256  //todo: del object and create new + patchcords
257 
258  //
259  setInletsPos();
260  setOutletsPos();
261  }
262 
263  // ---------------------------------------------------------------
264 
265  void msgNewClass(PdArguments msg)
266  {
267  //XPD-TODO
268  //
269  /*
270  if (_dynamicClass) {
271  _opClass = new OPClass(msg.at(1).asString());
272 
273  } else {
274  // TODO
275  // cmp_post("cannot create new class - use empty 'pdclass' box for dynamic class creation");
276  }
277  */
278  }
279 
280  void msgAddProperty(PdArguments msg)
281  {
282  //XPD-TODO
283  //_opClass->addProperty(msg.at(1).asString(), msg.at(2).asString());
284  }
285 
286  void msgDelProperty(PdArguments msg)
287  {
288  //XPD-TODO
289  //_opClass->freeProperty(msg.at(1).asString());
290  }
291 
292  void msgAddMethod(PdArguments msg)
293  {
294  //XPD-TODO
295  //_opClass->addMethod(msg.at(1).asString(), msg.at(2).asString()); //addmethod pname sendsymbol
296  }
297 
298  void msgDelMethod(PdArguments msg)
299  {
300  //XPD-TODO
301  //_opClass->freeMethod(msg.at(1).asString());
302  }
303 
304  void msgInfo(PdArguments)
305  {
306  //XPD-TODO
307  /*
308  if (_opClass) {
309  AtomList l1 = _opClass->getPropertyList();
310  AtomList l2 = _opClass->getMethodList();
311  AtomList l3 = _opClass->getSignalList();
312 
313  QStringList output;
314 
315  output += "class info: ";
316  output += _opClass->getClassName().c_str();
317 
318  output += "properties:";
319  for (size_t i = 0; i < l1.size(); i++) {
320  output += (l1.at(i).asString().c_str());
321  }
322 
323  output += "methods:";
324  for (size_t i = 0; i < l2.size(); i++) {
325  output += (l2.at(i).asString().c_str());
326  }
327 
328  output += "signals:";
329  for (size_t i = 0; i < l3.size(); i++) {
330  output += (l3.at(i).asString().c_str());
331  }
332 
333  // TODO
334  // cmp_post(output.join("\n").toStdString().c_str());
335  } else {
336  // TODO
337  //cmp_post("no class");
338  }
339  */
340  }
341 
342  // ---------------------------------------------------------------
343 
344  static void updateUI(void* uiobj, PdArguments msg)
345  {
346  //XPD-TODO
347  /*
348 
349  // message handling here - probably move somewhere else?
350 
351  UIClass* x = (UIClass*)uiobj;
352 
353  if (msg.size() < 1)
354  return;
355  if (!msg.at(0).isSymbol())
356  return;
357 
358  if ((msg.at(0).asString() == "newclass") && (msg.size() > 1)) {
359  x->msgNewClass(msg);
360  }
361 
362  if ((msg.at(0).asString() == "addproperty") && (msg.size() > 2)) //addproperty pname sendsymbol
363  x->msgAddProperty(msg);
364 
365  if ((msg.at(0).asString() == "delproperty") && (msg.size() > 1))
366  x->msgDelProperty(msg);
367 
368  if ((msg.at(0).asString() == "addmethod") && (msg.size() > 2))
369  x->msgAddMethod(msg);
370 
371  if ((msg.at(0).asString() == "delmethod") && (msg.size() > 1))
372  x->msgDelMethod(msg);
373 
374  if (msg.at(0).asString() == "info")
375  x->msgInfo(msg);
376 
377  // -----------
378 
379  if ((msg.at(0).asString() == "read"))
380  x->_opClass->readFile();
381 
382  if ((msg.at(0).asString() == "write"))
383  x->_opClass->writeFile();
384 
385  emit((UIClass*)uiobj)->updateUISignal();
386  */
387  }
388 
389 signals:
390 
391  void mouseMoved();
392  void rightClicked();
393 
394  //void editObject(UIObject* box);
395 signals:
396  void updateUISignal();
397 
398 private slots:
399  void updateUISlot()
400  {
401  update();
402  }
403 };
404 }
405 
406 #endif // CM_UICLASS_H
float height()
Definition: UIItem.h:43
void fromQString(QString message)
basic mouse up
Definition: UIBox.cpp:206
virtual void paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *)
paint event
Definition: UIClass.h:132
void mouseReleaseEvent(QGraphicsSceneMouseEvent *)
mouse up
Definition: UIClass.h:221
Property * get(QString pName)
Definition: PropertyList.cpp:22
QColor asQColor()
Definition: Property.cpp:229
gui object: oopd class (pdclass)
Definition: UIClass.h:30
float width()
Definition: UIItem.h:42
void writeFile()
Definition: OPClass.cpp:102
void msgDelMethod(PdArguments msg)
Definition: UIClass.h:298
void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
mouse move
Definition: UIClass.h:230
QPoint dragOffset
Definition: UIItem.h:31
QString toQString()
sets object text data
Definition: UIObject.cpp:509
#define t_canvas
Definition: m_pd.h:205
QColor bgColor()
Definition: UIItem.h:49
PropertyList * properties()
>init properties for the class - called from constructor
Definition: UIObject.cpp:215
UIObjectData * objectData()
Definition: UIObject.cpp:101
Definition: CommonTypes.h:14
~UIClass()
Definition: UIClass.h:41
#define PREF_QSTRING(x)
Definition: Preferences.h:17
void moveBox(UIItem *box, QGraphicsSceneMouseEvent *event)
void showPopupMenu(QPoint pos)
createContextMenu
Definition: UIObject.cpp:243
QRectF boundingRect() const
get edit mode flag value
Definition: UIItem.h:71
t_editMode getEditMode()
get edit mode pointer - for inlets/outlets
Definition: UIItem.cpp:54
PatchWindowController * subpatchController()
Definition: UIBox.h:72
void rightClicked()
virtual void setInletsPos()
returns help patch name with path if the file is found
Definition: UIObject.cpp:250
virtual void autoResize()
custom resize event
Definition: UIBox.cpp:321
virtual void setOutletsPos()
sets inlet position. could be overriden for custom inlet positions
Definition: UIObject.cpp:268
void msgDelProperty(PdArguments msg)
Definition: UIClass.h:286
void signalEditObject(void *box)
basic update - calls update() probably remove later
void msgNewClass(PdArguments msg)
Definition: UIClass.h:265
void msgInfo(PdArguments)
Definition: UIClass.h:304
UIClass()
Definition: UIClass.cpp:8
static void updateUI(void *uiobj, PdArguments msg)
Definition: UIClass.h:344
bool errorBox()
Definition: UIObject.cpp:522
void msgAddMethod(PdArguments msg)
Definition: UIClass.h:292
OOPD class structure.
Definition: OPClass.h:38
void selectBox(UIItem *box, QGraphicsSceneMouseEvent *event)
void updateUISignal()
void msgAddProperty(PdArguments msg)
Definition: UIClass.h:280
bool isSelected()
deselect object
Definition: UIItem.cpp:40
Definition: UIObject.h:44
void setWidth(float w)
Definition: UIItem.h:37
static UIObject * createObject(QString objectData, t_canvas *pdCanvas, QGraphicsView *parent=0)
Definition: UIClass.h:48
void showWindow()
Definition: OPClass.cpp:48
void setPdMessage(QString message)
Definition: UIClass.h:244
void mousePressEvent(QGraphicsSceneMouseEvent *ev)
mouse down
Definition: UIClass.h:173
gui object: standard object box
Definition: UIBox.h:46