tilde~
Puredata Qt-based GUI
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
UIMethod.h
Go to the documentation of this file.
1 // (c) 2017 Alex Nadzharov
2 // License: GPL3
3 
4 #ifndef UI_METHOD_H
5 #define UI_METHOD_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 "PatchWindowController.h"
21 #include "PatchWindow.h"
22 
23 #include "Preferences.h"
24 #include "PropertyList.h"
25 
26 #include "UIObjectData.h"
27 
28 namespace tilde {
29 
33 class UIMethod : public UIBox {
34 
35  Q_OBJECT
36 
37 private:
38  // for abstracions
39  bool _isAbstraction;
40  QString _abstractionPath;
41 
42  OPClass* _opClass;
43  OPInstance* _opInstance;
44 
45  std::string _methodName;
46 
47 public:
48  explicit UIMethod(); //UIObject* parent = 0);
49  //~UIMethod();
50 
51  static UIObject* createObject(QString asQString, t_canvas* pdCanvas, QGraphicsView* parent = 0);
52 
56  virtual void paint(QPainter* p, const QStyleOptionGraphicsItem*, QWidget*)
57  {
58  QBrush brush(bgColor());
59  p->setBrush(brush);
60  p->drawRect(boundingRect());
61  p->setBrush(QBrush());
62 
63  //QPainter p(viewport());
64  p->setRenderHint(QPainter::HighQualityAntialiasing, true);
65  p->scale(scale(), scale());
66 
67  //remove this later
68  if (subpatchController()) {
69  p->setPen(QPen(QColor(192, 192, 192), 1, Qt::SolidLine, Qt::SquareCap, Qt::BevelJoin));
70  p->drawRect(0, 2, width(), height() - 4);
71  }
72 
73  QColor rectColor = (errorBox()) ? QColor(255, 0, 0) : properties()->get("BorderColor")->asQColor(); //QColor(128, 128, 128);
74  p->setPen(QPen(rectColor, 1 + _isAbstraction, (errorBox()) ? Qt::DashLine : Qt::SolidLine, Qt::SquareCap, Qt::BevelJoin));
75  p->drawRect(0, 0, width(), height());
76  QTextOption* op = new QTextOption;
77  op->setAlignment(Qt::AlignLeft);
78  p->setPen(QPen(QColor(0, 0, 0), 1, Qt::SolidLine, Qt::SquareCap, Qt::BevelJoin));
79 
80  p->setFont(QFont(PREF_QSTRING("Font"), properties()->get("FontSize")->asFontSize(), 0, false));
81  p->drawText(2, 3, width() - 2, height() - 3, 0, objectData()->toQString(), 0);
82 
83  if (isSelected()) {
84  p->setPen(QPen(QColor(0, 192, 255), 1, (errorBox()) ? Qt::DashLine : Qt::SolidLine, Qt::SquareCap, Qt::BevelJoin));
85  p->drawRect(0, 0, width(), height());
86  }
87  }
88 
93  void mousePressEvent(QGraphicsSceneMouseEvent* ev)
94  {
95  //context menu
96  if (ev->button() == Qt::RightButton) {
97 
98  if (scene()
99  && !scene()->views().isEmpty()
100  && scene()->views().first()
101  && scene()->views().first()->viewport()) {
102 
103  QGraphicsView* v = scene()->views().first();
104  QPoint pos = v->viewport()->mapToGlobal(ev->pos().toPoint());
105 
106  // TODO
107  showPopupMenu(pos + this->pos().toPoint());
108  ev->accept();
109  }
110 
111  return;
112  }
113 
114  //open canvas for subpatch
115  if (getEditMode() != em_Unlocked) {
116  if (subpatchController()) {
117  subpatchController()->mainWindow()->show();
118  }
119  }
120 
121  //abstraction opening. Fix
122  // if (getEditMode() != em_Unlocked) {
123  // if (_isAbstraction) {
124  // OpenFileProxy::openAbstraction(_abstractionPath);
125  // }
126  // }
127 
128  if ((getEditMode() == em_Unlocked) && isSelected()) {
129  // editor_->setText(QString(_objectDataModel.objectData()));
130  // editor_->show();
131 
132  emit signalEditObject(this);
133  return;
134  }
135 
136  emit selectBox(this, ev);
137  dragOffset = ev->pos().toPoint();
138  }
139 
143  void mouseReleaseEvent(QGraphicsSceneMouseEvent*)
144  {
145  update();
146  }
147 
152  void mouseMoveEvent(QGraphicsSceneMouseEvent* event)
153  {
154  if (event->buttons() & Qt::LeftButton) {
155  emit moveBox(this, event);
156  }
157  event->ignore();
158 
159  if ((getEditMode() != em_Unlocked) && (subpatchController())) {
160  setCursor(QCursor(Qt::PointingHandCursor));
161  } else {
162  setCursor(QCursor(Qt::ArrowCursor));
163  }
164  }
165 
166  void setPdMessage(QString message)
167  {
168  //XPD-TODO
169  /*
170  fromQString(message);
171  autoResize();
172 
173  QFont myFont(PREF_QSTRING("Font"), 11);
174  QFontMetrics fm(myFont);
175  int new_w = fm.width(QString(objectData()->toQString())) + 10;
176  new_w = (new_w < 25) ? 25 : new_w;
177  setWidth(new_w);
178  //editor_->setFixedWidth(width() - 5);
179 
180  //todo: del object and create new + patchcords
181 
182  //
183  setInletsPos();
184  setOutletsPos();
185  */
186  }
187 
188  static void updateUI(void* uiobj, PdArguments msg)
189  {
190  //XPD-TODO
191  /*
192  UIMethod* b = (UIMethod*)uiobj;
193 
194  //qDebug() << "method out";
195 
196  if (b->_opInstance) {
197  AtomList fullMsg(gensym(b->_methodName.c_str()));
198  fullMsg.append(msg);
199  b->_opInstance->multipleOutput(fullMsg);
200  }
201 
202  emit b->updateUISignal();
203  */
204  }
205 
206 signals:
207 
208  void mouseMoved();
209  void rightClicked();
210 
211  void updateUISignal();
212 
213 private slots:
214  void updateUISlot()
215  {
216  update();
217  }
218 };
219 }
220 
221 #endif // CM_BOX_H
float height()
Definition: UIItem.h:43
Property * get(QString pName)
Definition: PropertyList.cpp:22
QColor asQColor()
Definition: Property.cpp:229
float width()
Definition: UIItem.h:42
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
#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 setPdMessage(QString message)
Definition: UIMethod.h:166
static UIObject * createObject(QString asQString, t_canvas *pdCanvas, QGraphicsView *parent=0)
Definition: UIMethod.cpp:22
void signalEditObject(void *box)
basic update - calls update() probably remove later
void mouseReleaseEvent(QGraphicsSceneMouseEvent *)
mouse up
Definition: UIMethod.h:143
bool errorBox()
Definition: UIObject.cpp:522
static void updateUI(void *uiobj, PdArguments msg)
Definition: UIMethod.h:188
OOPD class structure.
Definition: OPClass.h:38
void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
mouse move
Definition: UIMethod.h:152
void updateUISignal()
void selectBox(UIItem *box, QGraphicsSceneMouseEvent *event)
virtual void paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *)
paint event
Definition: UIMethod.h:56
gui object: oop method box (method)
Definition: UIMethod.h:33
bool isSelected()
deselect object
Definition: UIItem.cpp:40
The OOPD class instance.
Definition: OPInstance.h:44
Definition: UIObject.h:44
UIMethod()
Definition: UIMethod.cpp:9
PatchWindow * mainWindow()
Definition: PatchWindowController.cpp:168
void mousePressEvent(QGraphicsSceneMouseEvent *ev)
mouse down
Definition: UIMethod.h:93
gui object: standard object box
Definition: UIBox.h:46