Salome HOME
Initial commit
[modules/jobmanager.git] / src / genericgui / BL_QModelManager.cxx
1 //  Copyright (C) 2009 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.
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
22 BL::QModelManager::QModelManager(QObject * parent, BL::JobsManager_QT * jobs_manager) : QObject(parent)
23 {
24   DEBTRACE("Creating BL::QModelManager");
25   BL_ASSERT(jobs_manager);
26   _jobs_manager = jobs_manager;
27
28   _model = new QStandardItemModel(this);
29   QStringList headers;
30   headers << "Job Name" << "Type" << "State" << "Machine";
31   _model->setHorizontalHeaderLabels(headers);
32 }
33
34 BL::QModelManager::~QModelManager()
35 {
36   DEBTRACE("Destroying BL::QModelManager");
37 }
38
39 QStandardItemModel *
40 BL::QModelManager::getModel()
41 {
42   DEBTRACE("getModel BL::QModelManager called");
43
44   return _model;
45 }
46
47 void
48 BL::QModelManager::new_job_added(const QString & name)
49 {
50   BL::Job * job = _jobs_manager->getJob(name.toStdString());
51   QStandardItem * new_job_name = new QStandardItem(name);
52   
53   QStandardItem * new_job_type;
54   if (job->getType() == BL::Job::YACS_SCHEMA)
55     new_job_type = new QStandardItem("YACS_Schema");
56   else
57     new_job_type = new QStandardItem("Command");
58
59   QStandardItem * new_job_state;
60   if (job->getState() == BL::Job::CREATED)
61     new_job_state = new QStandardItem("Created");
62   else if (job->getState() == BL::Job::QUEUED)
63     new_job_state = new QStandardItem("Queued");
64   else if (job->getState() == BL::Job::RUNNING)
65     new_job_state = new QStandardItem("Running");
66   else if (job->getState() == BL::Job::PAUSED)
67     new_job_state = new QStandardItem("Paused");
68   else if (job->getState() == BL::Job::ERROR)
69     new_job_state = new QStandardItem("Error");
70   else 
71     new_job_state = new QStandardItem("Finished");
72
73   QStandardItem * new_job_machine =  new QStandardItem(job->getMachine().c_str());
74
75   int row = _model->rowCount();
76   _model->setItem(row, 0, new_job_name);
77   _model->setItem(row, 1, new_job_type);
78   _model->setItem(row, 2, new_job_state);
79   _model->setItem(row, 3, new_job_machine);
80 }
81
82 void
83 BL::QModelManager::job_state_changed(const QString & name)
84 {
85   DEBTRACE("BL::QModelManager::job_state_changed received");
86
87   BL::Job * job = _jobs_manager->getJob(name.toStdString());
88   QList<QStandardItem *> item_list = _model->findItems(name);
89   QStandardItem * job_state_item = _model->item(item_list.at(0)->row(), 2);
90
91   if (job->getState() == BL::Job::CREATED)
92     job_state_item->setText("Created");
93   else if (job->getState() == BL::Job::QUEUED)
94     job_state_item->setText("Queued");
95   else if (job->getState() == BL::Job::RUNNING)
96     job_state_item->setText("Running");
97   else if (job->getState() == BL::Job::PAUSED)
98     job_state_item->setText("Paused");
99   else if (job->getState() == BL::Job::ERROR)
100     job_state_item->setText("Error");
101   else 
102     job_state_item->setText("Finished");
103 }
104
105 void
106 BL::QModelManager::deleteJob(int row)
107 {
108   _model->removeRow(row);
109 }
110
111 void
112 BL::QModelManager::job_selected(const QModelIndex & index)
113 {
114   DEBTRACE("BL::QModelManager::job_selected slot");
115   QStandardItem * item = _model->itemFromIndex(index);
116   int row = item->row();
117
118   // Algo un peu bourrin....
119   for (int i = 0; i < _model->rowCount(); i++)
120     for (int j = 0; j < _model->columnCount(); j++)
121     {
122       _model->item(i,j)->setBackground(QBrush(Qt::white));
123       _model->item(i,j)->setForeground(QBrush(Qt::black));
124     }
125
126   for (int j = 0; j < _model->columnCount(); j++)
127   {
128     _model->item(row,j)->setBackground(QBrush(Qt::darkBlue));
129     _model->item(row,j)->setForeground(QBrush(Qt::white));
130   }
131 }