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