]> SALOME platform Git repositories - modules/yacs.git/blob - src/prs/YACSPrs_ServiceNode.cxx
Salome HOME
copy tag mergefrom_BR_V0_1_CC_Salome_04oct07
[modules/yacs.git] / src / prs / YACSPrs_ServiceNode.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "YACSPrs_ServiceNode.h"
21 #include "YACSPrs_Def.h"
22
23 #include <SUIT_ResourceMgr.h>
24
25 #include <qpainter.h>
26
27 #include <ServiceNode.hxx>
28 #include <ComponentInstance.hxx>
29
30 using namespace YACS::ENGINE;
31
32 void drawText1(QPainter& thePainter, const QString& theText, 
33               const QRect& theRect, int theHAlign = Qt::AlignAuto)
34 {
35   int flags = theHAlign | Qt::AlignVCenter;
36   QRect r(theRect.x() + TEXT_MARGIN, theRect.y(), 
37           theRect.width() - 2*TEXT_MARGIN, theRect.height());
38
39   QWMatrix aMat = thePainter.worldMatrix();
40   if (aMat.m11() != 1.0) { 
41     // for scaled picture only
42     QRect r1 = aMat.mapRect(r);
43     QFont saved = thePainter.font();
44     QFont f(saved);
45     if (f.pointSize() == -1) {
46       f.setPixelSize((int)(f.pixelSize()*aMat.m11()));
47     }
48     else {
49       f.setPointSize((int)(f.pointSize()*aMat.m11()));
50     }
51     thePainter.save();
52     QWMatrix m;
53     thePainter.setWorldMatrix(m);
54     thePainter.setFont(f);
55     thePainter.drawText(r1, flags, theText);
56     thePainter.setFont(saved);
57     thePainter.restore();
58   }
59   else {
60     thePainter.drawText(r, flags, theText);
61   }
62 }
63
64
65 /*!
66   Constructor
67 */
68 YACSPrs_ServiceNode::YACSPrs_ServiceNode(SUIT_ResourceMgr* theMgr, QCanvas* theCanvas, YACS::ENGINE::Node* theNode):
69   YACSPrs_ElementaryNode(theMgr, theCanvas, theNode)
70 {
71   myServiceHeight = TITLE_HEIGHT;
72   myComponentHeight = TITLE_HEIGHT;
73   myMachineHeight = TITLE_HEIGHT;
74   
75   setNodeColor(SERVICENODE_COLOR);
76   setNodeSubColor(SERVICENODE_SUBCOLOR);
77
78   setStoreColor(nodeColor());
79   setStoreSubColor(nodeSubColor());
80
81   //updatePorts(); // will be called in moveBy(...) function
82   moveBy(2*HOOKPOINT_SIZE,0);
83
84   update();
85 }
86
87 /*!
88   Destructor
89 */
90 YACSPrs_ServiceNode::~YACSPrs_ServiceNode() 
91 {
92 }
93
94 int YACSPrs_ServiceNode::rtti() const
95 {
96   return 0;//YACSPrs_Canvas::Rtti_ServiceNode;
97 }
98
99 int YACSPrs_ServiceNode::getInfoHeight() const
100 {
101   return getTitleHeight() + NODE_SPACE + 
102          getServiceHeight() + NODE_SPACE + 
103          getComponentHeight() + NODE_SPACE + 
104          getMachineHeight() + NODE_SPACE + 
105          getStatusHeight();
106 }
107   
108 int YACSPrs_ServiceNode::getServiceHeight() const
109 {
110   return myServiceHeight;
111 }
112
113 int YACSPrs_ServiceNode::getComponentHeight() const
114 {
115   return myComponentHeight;
116 }
117
118 int YACSPrs_ServiceNode::getMachineHeight() const
119 {
120   return myMachineHeight;
121 }
122
123 QRect YACSPrs_ServiceNode::getServiceRect() const
124 {
125   QRect aPMRect = getPixmapRect(true,true);
126   int width = getTitleRect().width() - aPMRect.width() - NODE_MARGIN;
127   return QRect(aPMRect.right() + NODE_MARGIN, getTitleRect().bottom()+NODE_SPACE,
128                width, getServiceHeight());
129 }
130
131 QRect YACSPrs_ServiceNode::getComponentRect() const
132 {
133   QRect aPMRect = getPixmapRect(true,true);
134   int width = getTitleRect().width() - aPMRect.width() - NODE_MARGIN;
135   return QRect(aPMRect.right() + NODE_MARGIN, getServiceRect().bottom()+NODE_SPACE,
136                width, getComponentHeight());
137 }
138
139 QRect YACSPrs_ServiceNode::getMachineRect() const
140 {
141   QRect aPMRect = getPixmapRect(true,true);
142   int width = getTitleRect().width() - aPMRect.width() - NODE_MARGIN;
143   return QRect(aPMRect.right() + NODE_MARGIN, getComponentRect().bottom()+NODE_SPACE,
144                width, getMachineHeight());
145 }
146
147 QRect YACSPrs_ServiceNode::getStatusRect() const
148 {
149   QRect aPMRect = getPixmapRect(true,true);
150   int width = getTitleRect().width() - aPMRect.width() - NODE_MARGIN;
151   return QRect(aPMRect.right() + NODE_MARGIN, getMachineRect().bottom()+NODE_SPACE,
152                width, getStatusHeight());
153 }
154
155 QRect YACSPrs_ServiceNode::getWholeRect() const
156 {
157   return QRect(getStatusRect().topLeft()+QPoint(getStatusRect().width()/3+3,3),
158                getStatusRect().bottomRight()-QPoint(3,3));
159 }
160
161 QRect YACSPrs_ServiceNode::getPercentRect() const
162 {
163   int aPercentageW = (int)(getWholeRect().width()*( (getStoredPercentage() < 0) ? getPercentage() : getStoredPercentage() )/100.);
164   return QRect(getWholeRect().topLeft(), getWholeRect().bottomLeft()+QPoint(aPercentageW,0));
165 }
166
167 /*!
168   Update service, component, machine
169 */
170 void YACSPrs_ServiceNode::updateInfo()
171 {
172
173 }
174   
175 void YACSPrs_ServiceNode::drawTitleShape(QPainter& thePainter)
176 {
177   // draw information: title, service, component, machine, status
178   int aXRnd = getTitleRect().width()*myXRnd/getServiceRect().width();
179   int aYRnd = getTitleRect().height()*myYRnd/TITLE_HEIGHT;
180
181   QPen savedP = thePainter.pen();
182   thePainter.setPen(thePainter.brush().color().dark(140));
183
184   // title
185   thePainter.drawRoundRect(getTitleRect(),myXRnd,myYRnd);
186   // service
187   thePainter.drawRoundRect(getServiceRect(),aXRnd,aYRnd);
188   // component
189   thePainter.drawRoundRect(getComponentRect(),aXRnd,aYRnd);
190   // machine
191   thePainter.drawRoundRect(getMachineRect(),aXRnd,aYRnd);
192   //state and time
193   thePainter.drawRoundRect(getStatusRect(),aXRnd,aYRnd);
194
195   // draw progress bar
196   thePainter.setPen(NoPen);
197
198   QBrush savedB = thePainter.brush();
199
200   thePainter.setBrush(savedB.color().light(130));
201   thePainter.drawRect(getWholeRect());
202   
203   if ( getPercentRect().width() > 1 ) {
204     thePainter.setBrush(savedB.color().dark(160));
205     thePainter.drawRect(getPercentRect());
206   }
207
208   thePainter.setBrush(savedB);
209
210   // draw texts
211   thePainter.setPen(Qt::white);
212
213   ServiceNode* aSEngine = dynamic_cast<ServiceNode*>( myEngine );
214   drawText1(thePainter, QString(myEngine->getName()), getTitleRect(), Qt::AlignLeft);
215   drawText1(thePainter, QString( "Service: ") + QString( aSEngine ? aSEngine->getMethod() : "..." ), getServiceRect(), Qt::AlignLeft);
216   drawText1(thePainter, QString( "Component: " ) + QString( aSEngine ? aSEngine->getComponent()->getName() : "..." ), getComponentRect(), Qt::AlignLeft);
217   drawText1(thePainter, QString("Machine: ..."), getMachineRect(), Qt::AlignLeft);
218
219   int aMidX = getTitleRect().right()-getTitleRect().width()/2;
220   thePainter.drawLine(aMidX, getStatusRect().top()+2, aMidX, getStatusRect().bottom()-2);
221   
222   QRect aStateRect(getStatusRect().x(), getStatusRect().y(), aMidX-getStatusRect().left(), TITLE_HEIGHT);
223   QRect aTimeRect(aMidX, getStatusRect().y(), 2*getStatusRect().width()/3, TITLE_HEIGHT);
224
225   drawText1(thePainter, myStatus, aStateRect, Qt::AlignLeft);
226   drawText1(thePainter, myTime, aTimeRect, Qt::AlignLeft);
227   drawText1(thePainter, QString::number(( (getStoredPercentage() < 0) ? getPercentage() : getStoredPercentage() ))+QString("%"), aTimeRect, Qt::AlignRight); // percentage
228   
229   thePainter.setPen(savedP);
230
231   // draw pixmap
232   thePainter.setBrush(NoBrush);
233
234   aXRnd = getTitleRect().width()*myXRnd/getPixmapRect(true,true).width();
235   aYRnd = getTitleRect().height()*myYRnd/getPixmapRect(true,true).height();
236   thePainter.drawRoundRect(getPixmapRect(true,true),aXRnd,aYRnd);
237   QRect aPRect = getPixmapRect(true,true);
238   aPRect.setX(aPRect.x()+PIXMAP_MARGIN+2);
239   aPRect.setY(aPRect.y()+PIXMAP_MARGIN);
240   aPRect.setWidth(aPRect.width()-2*PIXMAP_MARGIN);
241   aPRect.setHeight(aPRect.height()-2*PIXMAP_MARGIN);
242   thePainter.drawPixmap(aPRect,myStatePixmap);
243
244   thePainter.setBrush(savedB);
245 }
246
247 QString YACSPrs_ServiceNode::getToolTipText(const QPoint& theMousePos, QRect& theRect) const
248 {
249   // Check if the tooltip for ports is needed
250   if (getBodyRect().contains(theMousePos, true) || getGateRect().contains(theMousePos, true))
251     return YACSPrs_ElementaryNode::getToolTipText(theMousePos,theRect);
252   
253   // Return tooltip text for node
254   QString aText = QString("");
255   aText += QString("Name: %1\n").arg(getEngine()->getName());
256   aText += QString("Type: %1\n").arg("Service node");
257   theRect = getTitleRect();
258   theRect = theRect.unite(getServiceRect());
259   theRect = theRect.unite(getComponentRect());
260   theRect = theRect.unite(getMachineRect());
261   theRect = theRect.unite(getStatusRect());
262   theRect = theRect.unite(getPixmapRect());
263   return aText;
264 }