Vuo  2.1.2
VuoRendererTypecastPort.cc
Go to the documentation of this file.
1 
11 
12 #include "VuoCompilerPortClass.hh"
13 #include "VuoNodeClass.hh"
14 #include "VuoPort.hh"
15 #include "VuoRendererFonts.hh"
16 
17 const qreal VuoRendererTypecastPort::textPadding = 7 /*VuoRendererPort::portRadius - 1*/;
18 
27  VuoRendererPort *replacedPort,
28  VuoRendererSignaler *signaler)
29  : VuoRendererPort(replacedPort->getBase(),
30  signaler,
31  false,
32  replacedPort->getRefreshPort(),
33  false)
34 {
35  this->replacedPort = replacedPort;
36  this->setPortNameToRender(replacedPort->getPortNameToRender());
37  setUncollapsedTypecastNode(uncollapsedTypecastNode);
38 }
39 
44 void VuoRendererTypecastPort::setUncollapsedTypecastNode(VuoRendererNode *uncollapsedTypecastNode)
45 {
46  // @todo: Handle this elsewhere.
47  //setToolTip(uncollapsedTypecastNode->generateNodeClassToolTip(uncollapsedTypecastNode->getBase()->getNodeClass()));
48 
49  VuoPort *typecastInPort = uncollapsedTypecastNode->getBase()->getInputPorts()[VuoNodeClass::unreservedInputPortStartIndex];
50  VuoPort *typecastOutPort = uncollapsedTypecastNode->getBase()->getOutputPorts()[VuoNodeClass::unreservedOutputPortStartIndex];
51 
52  this->uncollapsedTypecastNode = uncollapsedTypecastNode;
53  this->sourceType = QString::fromStdString(typecastInPort->getRenderer()->getPortNameToRenderWhenDisplayed());
54  this->destinationType = QString::fromStdString(typecastOutPort->getRenderer()->getPortNameToRenderWhenDisplayed());
55  this->childPort = typecastInPort->getRenderer();
56  updateCachedBoundingRect();
57 }
58 
62 QPainterPath VuoRendererTypecastPort::getPortPath(bool includeNormalPort, bool includeFlag) const
63 {
64  QPainterPath p;
65 
66  // Draw a normal port...
67  if (includeNormalPort)
69 
70  // ...and add a rectangle connecting the host port to the child port.
71  if (includeFlag)
72  {
73  qreal r = VuoRendererPort::portRadius - 2;
74  p.addRoundedRect(getPortConstantTextRect().adjusted(-textPadding/2 - 1, .15, 0, -.85), r, r);
75  }
76 
77  return p;
78 }
79 
84 {
85  return getPortConstantTextRectForText(getCanvasTypecastTitle())
86  .adjusted(-VuoRendererPort::portRadius - textPadding/2 + 1, 0, -VuoRendererPort::portRadius, 0);
87 }
88 
93 {
94  return getPortConstantTextRect().width() + (VuoRendererPort::portRadius - 1)*2 - 1 + textPadding/2 + 1;
95 }
96 
101 QString VuoRendererTypecastPort::getCanvasTypecastTitle(void) const
102 {
103  return getTypecastTitleForNodeClass(uncollapsedTypecastNode->getBase()->getNodeClass(), false);
104 }
105 
113 {
114  string typecastClassName = typecastClass->getClassName();
115 
116  // Check whether this typecast class has a custom title.
117  map<string, string> customTypecastTitles;
118  customTypecastTitles["vuo.type.real.boolean"] = "Real ≠ 0";
119  customTypecastTitles["vuo.type.real.enum.VuoBoolean"] = "Real ≥ 0.5";
120 
121  map<string, string>::iterator typecastTitle = customTypecastTitles.find(typecastClassName);
122  if (typecastTitle != customTypecastTitles.end())
123  return typecastTitle->second.c_str();
124 
125  // If not, construct the typecast title from its input and output port names.
126  VuoPortClass *typecastInPortClass = typecastClass->getInputPortClasses()[VuoNodeClass::unreservedInputPortStartIndex];
127  VuoPortClass *typecastOutPortClass = typecastClass->getOutputPortClasses()[VuoNodeClass::unreservedOutputPortStartIndex];
128 
129  QString typecastInPortName = QString::fromStdString(static_cast<VuoCompilerPortClass *>(typecastInPortClass->getCompiler())->getDisplayName());
130  QString typecastOutPortName = QString::fromStdString(static_cast<VuoCompilerPortClass *>(typecastOutPortClass->getCompiler())->getDisplayName());
131 
132  QString separator = (inMenu? QString::fromUtf8(" → ") : " ");
133  return typecastInPortName + separator + typecastOutPortName;
134 }
135 
140 {
141  return uncollapsedTypecastNode;
142 }
143 
148 {
149  return childPort;
150 }
151 
156 {
157  return replacedPort;
158 }
159 
164 {
165  return cachedBoundingRect;
166 }
167 
168 void VuoRendererTypecastPort::updateCachedBoundingRect()
169 {
170  QPainterPath path = getPortPath(true, true);
171  cachedBoundingRect = path.controlPointRect();
172 
173  if (!isRefreshPort)
174  cachedBoundingRect = cachedBoundingRect.united(getNameRect());
175 
176  if (hasPortAction())
177  cachedBoundingRect = cachedBoundingRect.united(getActionIndicatorRect());
178 
179  // Antialiasing bleed
180  cachedBoundingRect.adjust(-1,-1,1,1);
181 
182  cachedBoundingRect = cachedBoundingRect.toAlignedRect();
183 }
184 
188 void VuoRendererTypecastPort::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
189 {
190  if (getRenderedParentNode()->getProxyNode())
191  return;
192 
193  // Draw the collapsed typecast...
194 
195  drawBoundingRect(painter);
196 
197  VuoRendererColors::SelectionType selectionType = (getRenderedParentNode()->isSelected()? VuoRendererColors::directSelection : VuoRendererColors::noSelection);
198  qint64 timeOfLastActivity = ((! getRenderActivity())? VuoRendererItem::notTrackingActivity :
200 
201  VuoRendererColors *colors = new VuoRendererColors(getRenderedParentNode()->getBase()->getTintColor(),
202  selectionType,
203  false,
205  timeOfLastActivity);
206 
207  // Fill
208  QPainterPath innerTypecastPath = getPortPath(false, true);
209  painter->fillPath(innerTypecastPath, colors->nodeFill());
210 
211  // Typecast description
212  QRectF textRect = getPortConstantTextRect();
213  painter->setPen(colors->portTitle());
214  painter->setFont(VuoRendererFonts::getSharedFonts()->nodePortConstantFont());
215  painter->drawText(textRect, Qt::AlignLeft, getCanvasTypecastTitle());
216 
217  // Draw the normal port.
218  VuoRendererPort::paint(painter,option,widget);
219 
220  delete colors;
221 }