00001
00008 #include <QFile>
00009 #include <QTextStream>
00010 #include <QtGui>
00011 #include <QtGui/QHeaderView>
00012
00013
00014 #include "jobeditorwidget.h"
00015 #include "jobpropertymodel.h"
00016 #include "jobpropertyitem.h"
00017 #include "jobpropertyitemstring.h"
00018 #include "jobpropertyitemnodes.h"
00019 #include "jobpropertydelegate.h"
00020 #include "../mainwindowimpl.h"
00021 #include "../thread/jobexecute.h"
00022
00023 #include <QtDebug>
00024
00033 JobEditorWidget::JobEditorWidget(QWidget *parent)
00034 : QWidget(parent)
00035 {
00036 _filename = QString();
00037
00038 JobPropertyModel * model;
00039 JobPropertyDelegate * delegate;
00040 JobPropertyItemString * jpiStr;
00041 JobPropertyItemNodes * nodes;
00042 JobPropertyItemRoot * root;
00043
00044 setupUi(this);
00045
00046 model = new JobPropertyModel(properties_w);
00047 properties_w->setModel(model);
00048
00049 delegate = new JobPropertyDelegate(properties_w);
00050 properties_w->setItemDelegate(delegate);
00051 properties_w->header()->setResizeMode(1, QHeaderView::Stretch);
00052 properties_w->setAlternatingRowColors(true);
00053 properties_w->setContextMenuPolicy(Qt::CustomContextMenu);
00054 properties_w->resizeColumnToContents(0);
00055 QObject::connect(properties_w, SIGNAL(customContextMenuRequested( const QPoint &)),
00056 delegate, SLOT(onCustomContextMenuRequested ( const QPoint & )));
00057
00058 root = dynamic_cast<JobPropertyItemRoot *>(model->rootItem);
00059
00060
00061 jpiStr = dynamic_cast<JobPropertyItemString *>(root->child(0));
00062 QObject::connect(editor_w->outPty, SIGNAL(propertyChanged(const QString &)),
00063 jpiStr, SLOT(set(const QString &)));
00064 QObject::connect(jpiStr, SIGNAL(itemChanged(const QString &)),
00065 editor_w->outPty, SLOT(setProperty(const QString &)));
00066
00067
00068 jpiStr = dynamic_cast<JobPropertyItemString *>(root->child(1));
00069 QObject::connect(editor_w->errPty, SIGNAL(propertyChanged(const QString &)),
00070 jpiStr, SLOT(set(const QString &)));
00071 QObject::connect(jpiStr, SIGNAL(itemChanged(const QString &)),
00072 editor_w->errPty, SLOT(setProperty(const QString &)));
00073
00074
00075 jpiStr = dynamic_cast<JobPropertyItemString *>(root->child(2));
00076 QObject::connect(editor_w->joinPty, SIGNAL(propertyChanged(const QString &)),
00077 jpiStr, SLOT(set(const QString &)));
00078 QObject::connect(jpiStr, SIGNAL(itemChanged(const QString &)),
00079 editor_w->joinPty, SLOT(setProperty(const QString &)));
00080
00081
00082 jpiStr = dynamic_cast<JobPropertyItemString *>(root->child(3));
00083 QObject::connect(editor_w->mailPty, SIGNAL(propertyChanged(const QString &)),
00084 jpiStr, SLOT(set(const QString &)));
00085 QObject::connect(jpiStr, SIGNAL(itemChanged(const QString &)),
00086 editor_w->mailPty, SLOT(setProperty(const QString &)));
00087
00088
00089 jpiStr = dynamic_cast<JobPropertyItemString *>(root->child(4));
00090 QObject::connect(editor_w->namePty, SIGNAL(propertyChanged(const QString &)),
00091 jpiStr, SLOT(set(const QString &)));
00092 QObject::connect(jpiStr, SIGNAL(itemChanged(const QString &)),
00093 editor_w->namePty, SLOT(setProperty(const QString &)));
00094
00095
00096 jpiStr = dynamic_cast<JobPropertyItemString *>(root->child(5));
00097 QObject::connect(editor_w->walltimePty, SIGNAL(propertyChanged(const QString &)),
00098 jpiStr, SLOT(set(const QString &)));
00099 QObject::connect(jpiStr, SIGNAL(itemChanged(const QString &)),
00100 editor_w->walltimePty, SLOT(setProperty(const QString &)));
00101
00102
00103 nodes = dynamic_cast<JobPropertyItemNodes *>(root->child(6));
00104 QObject::connect(editor_w->nodesPty, SIGNAL(propertyChanged(const QString &)),
00105 nodes, SLOT(set(const QString &)));
00106 QObject::connect(nodes, SIGNAL(itemChanged(const QString &)),
00107 editor_w->nodesPty, SLOT(setProperty(const QString &)));
00108
00109
00110 jpiStr = dynamic_cast<JobPropertyItemString *>(root->child(7));
00111 QObject::connect(editor_w->nicePty, SIGNAL(propertyChanged(const QString &)),
00112 jpiStr, SLOT(set(const QString &)));
00113 QObject::connect(jpiStr, SIGNAL(itemChanged(const QString &)),
00114 editor_w->nicePty, SLOT(setProperty(const QString &)));
00115
00116
00117 jpiStr = dynamic_cast<JobPropertyItemString *>(root->child(8));
00118 QObject::connect(editor_w->cputPty, SIGNAL(propertyChanged(const QString &)),
00119 jpiStr, SLOT(set(const QString &)));
00120 QObject::connect(jpiStr, SIGNAL(itemChanged(const QString &)),
00121 editor_w->cputPty, SLOT(setProperty(const QString &)));
00122
00123
00124 jpiStr = dynamic_cast<JobPropertyItemString *>(root->child(9));
00125 QObject::connect(editor_w->pcputPty, SIGNAL(propertyChanged(const QString &)),
00126 jpiStr, SLOT(set(const QString &)));
00127 QObject::connect(jpiStr, SIGNAL(itemChanged(const QString &)),
00128 editor_w->pcputPty, SLOT(setProperty(const QString &)));
00129
00130
00131 jpiStr = dynamic_cast<JobPropertyItemString *>(root->child(10));
00132 QObject::connect(editor_w->filePty, SIGNAL(propertyChanged(const QString &)),
00133 jpiStr, SLOT(set(const QString &)));
00134 QObject::connect(jpiStr, SIGNAL(itemChanged(const QString &)),
00135 editor_w->filePty, SLOT(setProperty(const QString &)));
00136
00137
00138 jpiStr = dynamic_cast<JobPropertyItemString *>(root->child(11));
00139 QObject::connect(editor_w->memPty, SIGNAL(propertyChanged(const QString &)),
00140 jpiStr, SLOT(set(const QString &)));
00141 QObject::connect(jpiStr, SIGNAL(itemChanged(const QString &)),
00142 editor_w->memPty, SLOT(setProperty(const QString &)));
00143
00144
00145 jpiStr = dynamic_cast<JobPropertyItemString *>(root->child(12));
00146 QObject::connect(editor_w->pmemPty, SIGNAL(propertyChanged(const QString &)),
00147 jpiStr, SLOT(set(const QString &)));
00148 QObject::connect(jpiStr, SIGNAL(itemChanged(const QString &)),
00149 editor_w->pmemPty, SLOT(setProperty(const QString &)));
00150
00151
00152 jpiStr = dynamic_cast<JobPropertyItemString *>(root->child(13));
00153 QObject::connect(editor_w->vmemPty, SIGNAL(propertyChanged(const QString &)),
00154 jpiStr, SLOT(set(const QString &)));
00155 QObject::connect(jpiStr, SIGNAL(itemChanged(const QString &)),
00156 editor_w->vmemPty, SLOT(setProperty(const QString &)));
00157
00158
00159 jpiStr = dynamic_cast<JobPropertyItemString *>(root->child(14));
00160 QObject::connect(editor_w->pvmemPty, SIGNAL(propertyChanged(const QString &)),
00161 jpiStr, SLOT(set(const QString &)));
00162 QObject::connect(jpiStr, SIGNAL(itemChanged(const QString &)),
00163 editor_w->pvmemPty, SLOT(setProperty(const QString &)));
00164
00165
00166 jpiStr = dynamic_cast<JobPropertyItemString *>(root->child(15));
00167 QObject::connect(editor_w->archPty, SIGNAL(propertyChanged(const QString &)),
00168 jpiStr, SLOT(set(const QString &)));
00169 QObject::connect(jpiStr, SIGNAL(itemChanged(const QString &)),
00170 editor_w->archPty, SLOT(setProperty(const QString &)));
00171
00172
00173 jpiStr = dynamic_cast<JobPropertyItemString *>(root->child(16));
00174 QObject::connect(editor_w->hostPty, SIGNAL(propertyChanged(const QString &)),
00175 jpiStr, SLOT(set(const QString &)));
00176 QObject::connect(jpiStr, SIGNAL(itemChanged(const QString &)),
00177 editor_w->hostPty, SLOT(setProperty(const QString &)));
00178
00179
00180 jpiStr = dynamic_cast<JobPropertyItemString *>(root->child(17));
00181 QObject::connect(editor_w->otherPty, SIGNAL(propertyChanged(const QString &)),
00182 jpiStr, SLOT(set(const QString &)));
00183 QObject::connect(jpiStr, SIGNAL(itemChanged(const QString &)),
00184 editor_w->otherPty, SLOT(setProperty(const QString &)));
00185
00186
00187 jpiStr = dynamic_cast<JobPropertyItemString *>(root->child(18));
00188 QObject::connect(editor_w->opsysPty, SIGNAL(propertyChanged(const QString &)),
00189 jpiStr, SLOT(set(const QString &)));
00190 QObject::connect(jpiStr, SIGNAL(itemChanged(const QString &)),
00191 editor_w->opsysPty, SLOT(setProperty(const QString &)));
00192
00193
00194 jpiStr = dynamic_cast<JobPropertyItemString *>(root->child(19));
00195 QObject::connect(editor_w->softwarePty, SIGNAL(propertyChanged(const QString &)),
00196 jpiStr, SLOT(set(const QString &)));
00197 QObject::connect(jpiStr, SIGNAL(itemChanged(const QString &)),
00198 editor_w->softwarePty, SLOT(setProperty(const QString &)));
00199
00200
00201 connect(actionOpen, SIGNAL(triggered()), this, SLOT(open()));
00202 connect(actionSave, SIGNAL(triggered()), this, SLOT(save()));
00203 connect(actionSaveAs, SIGNAL(triggered()), this, SLOT(saveAs()));
00204 connect(actionExecute, SIGNAL(triggered()), this, SLOT(executeJob()));
00205
00206
00207 }
00208
00212 void JobEditorWidget::executeJob()
00213 {
00214 Myssh & ssh = dynamic_cast<MainWindowImpl*>(window())->_ssh;
00215 if (ssh.isConnected())
00216 {
00217 QMovie * movie = new QMovie(":/imgs/anim.gif");
00218 lAnim->setMovie(movie);
00219 movie->start();
00220
00221 QString text = editor_w->toPlainText();
00222 text.replace(QString("$"),QString("\\$"));
00223 text.replace(QString("'"),QString("\'"));
00224 JobExecute *job = new JobExecute (text, &ssh);
00225 QObject::connect(job, SIGNAL(jobSent(QStringList)), this, SLOT(onJobSent(QStringList)));
00226 job->start();
00227 }
00228 }
00229
00236 void JobEditorWidget::onJobSent(QStringList outerr)
00237 {
00238 QMessageBox msgBox;
00239 QString msg;
00240 if (outerr[1].isEmpty())
00241 {
00242
00243 msg = tr("Job %1 sent.").arg(outerr[0].remove('\n'));
00244 } else {
00245
00246 msg = tr("Job could not be sent.\n Error: ").arg(outerr[1]);
00247 }
00248
00249 lAnim->clear();
00250
00251
00252 ((MainWindowImpl*)this->window())->logs->addLog(msg);
00253
00254 msgBox.setText(msg);
00255 msgBox.exec();
00256 }
00257
00261 void JobEditorWidget::open()
00262 {
00263 QString fileName = QFileDialog::getOpenFileName(this,
00264 tr("Open File"), "", tr("Job Shell Script Files (*.sh)"));
00265 if (!fileName.isEmpty())
00266 {
00267 QFile file(fileName);
00268 if (file.open(QFile::ReadOnly | QFile::Text))
00269 {
00270 _filename = fileName;
00271 editor_w->setPlainText(file.readAll());
00272 }
00273 }
00274 }
00275
00280 void JobEditorWidget::save()
00281 {
00282 if (_filename.isEmpty())
00283 {
00284 _filename = QFileDialog::getSaveFileName(this,
00285 tr("Save File As"), "", tr("Job Shell Script Files (*.sh)"));
00286 }
00287 if (!_filename.isEmpty())
00288 saveToFile();
00289 }
00290
00294 void JobEditorWidget::saveAs()
00295 {
00296 QString _fileName = QFileDialog::getSaveFileName(this,
00297 tr("Save File As"), "", tr("Job Shell Script Files (*.sh)"));
00298 if (!_fileName.isEmpty())
00299 saveToFile();
00300 }
00301
00305 void JobEditorWidget::saveToFile()
00306 {
00307 QFile file(_filename);
00308 if (file.open(QFile::WriteOnly | QFile::Text | QFile::Truncate))
00309 {
00310 QTextStream out(&file);
00311 out << editor_w->toPlainText();
00312 }
00313 }