Salome HOME
067e5d4e458912c08ee553e5d047d854b4a8dab6
[modules/yacs.git] / src / genericgui / SceneProgressItem.cxx
1 // Copyright (C) 2006-2023  CEA, EDF
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, or (at your option) any later version.
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 "SceneProgressItem.hxx"
21 #include "SceneHeaderNodeItem.hxx"
22 #include "SceneTextItem.hxx"
23 #include "Scene.hxx"
24 #include <QGraphicsSceneMouseEvent>
25
26 #include "Resource.hxx"
27
28 #include <cassert>
29
30 //#define _DEVDEBUG_
31 #include "YacsTrace.hxx"
32
33 using namespace std;
34 using namespace YACS::ENGINE;
35 using namespace YACS::HMI;
36
37
38 SceneProgressItem::SceneProgressItem(QGraphicsScene *scene, SceneItem *parent,
39                                  QString label)
40   : SceneItem(scene, parent, label)
41 {
42   YASSERT(_parent);
43   _progress = 0;
44   _width  = Resource::Corner_Margin + 2*Resource::DataPort_Width + 2*Resource::Space_Margin;
45   _height = Resource::progressBar_Height;
46   _text=0;
47   _tooltip = "";
48   _brushColor   = Resource::Header_brush;
49   _hiBrushColor = Resource::Header_hiBrush;
50   _penColor     = Resource::Header_pen;
51   _hiPenColor   = Resource::Header_hiPen;
52   int x = Resource::Border_Margin;
53   int y = Resource::DataPort_Height + 2*Resource::Space_Margin;
54   setTopLeft(QPointF(x, y));
55 }
56
57 SceneProgressItem::~SceneProgressItem()
58 {
59 }
60
61 QRectF SceneProgressItem::getMinimalBoundingRect() const
62 {
63   return QRectF(x(), y(), _width, _height);
64 }
65
66 void SceneProgressItem::paint(QPainter *painter,
67                             const QStyleOptionGraphicsItem *option,
68                             QWidget *widget)
69 {
70 //   DEBTRACE("SceneProgressItem::paint");
71   painter->save();
72
73   int w = Resource::Corner_Margin + 2*Resource::DataPort_Width + 2*Resource::Space_Margin;
74   if (_parent->getWidth() > w) w = _parent->getWidth() - Resource::Corner_Margin - Resource::Space_Margin;
75   int h = Resource::progressBar_Height;
76   QPen pen(getPenColor());
77   pen.setWidth(Resource::Thickness);
78   painter->setPen(pen);
79   QRect boundRect(0, 0, w, h);
80   painter->drawRect(boundRect);
81   
82   painter->setBrush(Resource::progressBarColor);
83   //correct width according to progress
84   int corr_w = w * _progress / 100;
85   painter->drawRect(QRect(0, 0, corr_w, h));
86   painter->restore();
87 }
88
89 void SceneProgressItem::setProgress(QString newProgress)
90 {
91   QString percentageLabel;
92   QString nbStepsLabel = "-/-";
93   QStringList aSteps = newProgress.split('/');
94   if (aSteps.count() == 2)
95   { //case '5/10' view of progress
96     _progress = aSteps.at(0).toInt() * 100 / aSteps.at(1).toInt();
97     nbStepsLabel = newProgress;
98   }
99   else
100   { //case '50' view of progress
101     _progress = newProgress.toInt(); //set 0 if the conversion fails.
102   }
103   percentageLabel = QString("%1\%").arg(_progress);
104   QString resultLabel;
105   switch(Resource::progressBarLabel)
106   {
107     case 0: //Percentage: 50%
108       resultLabel = QString("%1").arg(percentageLabel);
109       break;
110     case 1: //Nb.steps:   5/10
111       resultLabel = QString("%1").arg(nbStepsLabel);
112       break;
113     case 2: //Both:       50% (5/10)
114       resultLabel = QString("%1 (%2)").arg(percentageLabel).arg(nbStepsLabel);
115       break;
116   }
117   setText(resultLabel);
118   _tooltip = QString("%1 (%2)").arg(percentageLabel).arg(nbStepsLabel);
119   update();
120 }
121
122 void SceneProgressItem::setText(QString label)
123 {
124   if (!_text)
125     _text = new SceneTextItem(_scene, this, label, true);
126   else
127     _text->setPlainTextTrunc(label);
128   //QGraphicsItem::update();
129 }
130
131 void SceneProgressItem::popupMenu(QWidget *caller, const QPoint &globalPos)
132 {
133   if (_parent) _parent->popupMenu(caller, globalPos);
134 }
135
136 void SceneProgressItem::adjustGeometry()
137 {
138   prepareGeometryChange();
139   _width = _parent->getWidth() - Resource::Corner_Margin - Resource::Space_Margin;
140   update();
141 }
142
143 QColor SceneProgressItem::getPenColor()
144 {
145   return _hiPenColor;
146 }
147
148 QColor SceneProgressItem::getBrushColor()
149 {
150   QColor color = _brushColor;
151   if (dynamic_cast<SceneHeaderNodeItem*>(this))
152     if (getParent()->isSelected())
153       color = _hiBrushColor;
154   if (_hover)
155     color = hoverColor(color);
156   return color;
157 }
158
159 void SceneProgressItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
160 {
161   event->ignore();
162 }
163
164 QString SceneProgressItem::getToolTip()
165 {
166   return _tooltip;;
167 }