Salome HOME
Create LICENSE
[modules/jobmanager.git] / src / genericgui / BL_QModelManager.cxx
1 // Copyright (C) 2009-2024  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 "BL_QModelManager.hxx"
21 #include "BL_JobsManager_QT.hxx"
22
23 #include <QStandardItemModel>
24
25 #ifdef WNT
26 #undef ERROR
27 #endif
28
29 BL::QModelManager::QModelManager(QObject * parent, BL::JobsManager_QT * jobs_manager) : QObject(parent)
30 {
31   DEBTRACE("Creating BL::QModelManager");
32   BL_ASSERT(jobs_manager);
33   _jobs_manager = jobs_manager;
34
35   _model = new QStandardItemModel(this);
36   jobs_manager->set_model_manager(this);
37   QStringList headers;
38   headers << "Job Name" << "Type" << "State" << "Resource" << "Launcher Id";
39   _model->setHorizontalHeaderLabels(headers);
40 }
41
42 BL::QModelManager::~QModelManager()
43 {
44   DEBTRACE("Destroying BL::QModelManager");
45 }
46
47 QStandardItemModel *
48 BL::QModelManager::getModel()
49 {
50   DEBTRACE("getModel BL::QModelManager called");
51
52   return _model;
53 }
54
55 void
56 BL::QModelManager::new_job_added(const QString & name)
57 {
58   DEBTRACE("Adding new job in the model manager");
59   BL::Job * job = _jobs_manager->getJob(name.toUtf8().constData());
60   QStandardItem * new_job_name = new QStandardItem(name);
61   
62   QStandardItem * new_job_type;
63   if (job->getType() == BL::Job::YACS_SCHEMA)
64     new_job_type = new QStandardItem("YACS_Schema");
65   else if (job->getType() == BL::Job::COMMAND)
66     new_job_type = new QStandardItem("Command");
67   else if (job->getType() == BL::Job::COMMAND_SALOME)
68     new_job_type = new QStandardItem("Command_Salome");
69   else if (job->getType() == BL::Job::PYTHON_SALOME)
70     new_job_type = new QStandardItem("Python_Salome");
71
72   QStandardItem * new_job_state;
73   if (job->getState() == BL::Job::CREATED)
74     new_job_state = new QStandardItem("Created");
75   else if (job->getState() == BL::Job::QUEUED)
76     new_job_state = new QStandardItem("Queued");
77   else if (job->getState() == BL::Job::RUNNING)
78     new_job_state = new QStandardItem("Running");
79   else if (job->getState() == BL::Job::PAUSED)
80     new_job_state = new QStandardItem("Paused");
81   else if (job->getState() == BL::Job::ERROR)
82     new_job_state = new QStandardItem("Error");
83   else if (job->getState() == BL::Job::FAILED)
84     new_job_state = new QStandardItem("Failed");
85   else if (job->getState() == BL::Job::NOT_CREATED)
86     new_job_state = new QStandardItem("Not Created");
87   else 
88     new_job_state = new QStandardItem("Finished");
89
90   QStandardItem * new_job_resource =  new QStandardItem(job->getResource().c_str());
91   QString id_str;
92   id_str.setNum(job->getSalomeLauncherId());
93   QStandardItem * new_job_id =  new QStandardItem(id_str);
94
95   int row = _model->rowCount();
96   _model->setItem(row, 0, new_job_name);
97   _model->setItem(row, 1, new_job_type);
98   _model->setItem(row, 2, new_job_state);
99   _model->setItem(row, 3, new_job_resource);
100   _model->setItem(row, 4, new_job_id);
101 }
102
103 void
104 BL::QModelManager::job_state_changed(const QString & name)
105 {
106   DEBTRACE("BL::QModelManager::job_state_changed received");
107
108   BL::Job * job = _jobs_manager->getJob(name.toUtf8().constData());
109   QList<QStandardItem *> item_list = _model->findItems(name);
110   QStandardItem * job_state_item = _model->item(item_list.at(0)->row(), 2);
111
112   if (job->getState() == BL::Job::CREATED)
113     job_state_item->setText("Created");
114   else if (job->getState() == BL::Job::QUEUED)
115     job_state_item->setText("Queued");
116   else if (job->getState() == BL::Job::RUNNING)
117     job_state_item->setText("Running");
118   else if (job->getState() == BL::Job::PAUSED)
119     job_state_item->setText("Paused");
120   else if (job->getState() == BL::Job::ERROR)
121     job_state_item->setText("Error");
122   else if (job->getState() == BL::Job::FAILED)
123     job_state_item->setText("Failed");
124   else if (job->getState() == BL::Job::NOT_CREATED)
125     job_state_item->setText("Not Created");
126   else 
127     job_state_item->setText("Finished");
128 }
129
130 void
131 BL::QModelManager::delete_job(const QString & name)
132 {
133   QList<QStandardItem *> list = _model->findItems(name);
134   if (list.size() != 1)
135   {
136     DEBMSG("LIST SIZE IS :" << list.size());
137     DEBMSG("FOR NAME :" << name.toUtf8().constData());
138   }
139   if (list.size() > 0)
140     _model->removeRow(list[0]->row());
141 }