source: palm/trunk/UTIL/mrungui/mainwindow.cpp @ 794

Last change on this file since 794 was 794, checked in by maronga, 12 years ago

last commit documented

  • Property svn:keywords set to Id
File size: 44.6 KB
RevLine 
[793]1//----------------------------------------------------------------------------//
2// Current revisions:
3// -----------------
4//
5// Former revisions:
6// -----------------
7// $Id: mainwindow.cpp 794 2011-12-12 15:19:22Z maronga $
8//
[794]9// 793 2011-12-12 15:15:24Z maronga
10// Initial revision
11//
[793]12// Description:
13// ------------
14// All subroutines for mrungui
15//----------------------------------------------------------------------------//
16
17#include <QtGui/QApplication>
18#include "mainwindow.h"
19#include "ui_mainwindow.h"
20#include "ui_about.h"
21#include "ui_help.h"
22#include <QString>
23#include <QFileDialog>
24#include <QDateTime>
25#include "stdlib.h"
26#include <QProcess>
27#include <QTextStream>
28
29
30// Define username on host as global variable
31QString username = getenv("USER");
32// Define variable branch (location of mrungui, e.g.
33// "/home/user/palm/current_version"
34QString branch;
35
36
37//****************************************************************************//
38//  Initialization of the main window
39MainWindow::MainWindow(QWidget *parent) :
40    QMainWindow(parent),
41    ui(new Ui::MainWindow)
42{
43    ui->setupUi(this);
44
45//  Empty the mrun command line (hereafter mrunline)
46    ui->commandline->setText("");
47
48//  Get path of the program and set default file
49    branch = QCoreApplication::applicationDirPath();
50    branch = branch.left(branch.length() - 14);
51
52
53    QFile file(branch+"/.mrun.gui.default");
54
55//  Read default settings
56    if ( file.exists() == true && file.size() > 10)
57    {
58
59       QString mrunline;
60       if ( file.open(QIODevice::ReadOnly | QIODevice::Text ) )
61       {
62
63//        File opened successfully
64          QTextStream in(&file);
65
66          QString line = in.readLine();
67          while (!line.isNull())
68          {
69             mrunline = line;
70             line = in.readLine();
71          }
72
73          file.close();
74       }
75
76       mrunline = mrunline.right(mrunline.length() - 17);
77       ui->commandline->setText(mrunline);
78
79       setup_gui(mrunline);
80
81    }
82
83//  Load jobs into the recent jobs list
84    recent_jobs(10);
85}
86
87
88//****************************************************************************//
89//  Routine for listing jobs in the recent jobs list
90int MainWindow::recent_jobs(int noj)
91{
92    branch = QCoreApplication::applicationDirPath();
93    branch = branch.left(branch.length() - 14);
94
95    QFile file(branch+"/.mrun.history");
96    QString listitem, timestamp;
97
98//  Read mrun history and select the last used jobs
99    if ( file.exists() == true && file.size() > 10)
100    {
101
102//     Open history
103       QStringList history, tmphistory;
104       if ( file.open(QIODevice::ReadOnly | QIODevice::Text ) )
105       {
106
107//        file opened successfully
108          QTextStream in(&file);
109
110          QString line = in.readLine();
111          while (!line.isNull())
112          {
113             history.append(line);
114             line = in.readLine();
115          }
116
117          file.close();
118       }
119
120       int j = 0;
121
122       ui->list_jobname->clear();
123
124//     Read history entries and append to recent job list
125       for (int i=history.count(); i>=1; i--)
126       {
127           timestamp = history[i-1].left(16);
128           listitem = history[i-1].right(history[i-1].length() - 17);
129           listitem = listitem.split("-d ", QString::SkipEmptyParts)[1];
130           listitem = listitem.split(" -", QString::SkipEmptyParts)[0];
131           listitem = listitem.replace(" ","");
132
133           QList<QListWidgetItem *> matchitems = \
134                   ui->list_jobname->findItems(listitem, Qt::MatchExactly);
135
136           if ( matchitems.count() == 0 )
137           {
138              ui->list_jobname->addItem(listitem);
139              tmphistory.append(listitem+" ("+timestamp+")");
140              j++;
141           }
142           if ( j == noj )
143           {
144               break;
145           }
146       }
147
148//     Send to list
149       ui->list_jobname->clear();
150       for (int i=tmphistory.count(); i>=1; i--)
151       {
152           ui->list_jobname->addItem(tmphistory[i-1]);
153       }
154
155    }
156    return 0;
157}
158
159//****************************************************************************//
160//  Exit program
161MainWindow::~MainWindow()
162{
163
164    delete ui;
165}
166
167
168//****************************************************************************//
169//  Start the mrun command via xterm
170int MainWindow::startmrun()
171{
172
173    QString xtermoutput;
174    QString mrunline_save;
175    QString history_line;
176    QString mrunline = ui->commandline->text();
177    QString userline = ui->line_user->text();
178
179//  Check for empty line
180    mrunline_save = mrunline;
181    if (userline != "")
182    {
183        mrunline = mrunline + " " + userline;
184    }
185    history_line = mrunline;
186
187//  Disable the main window
188    ui->group_job->setEnabled(false);
189    ui->group_execution->setEnabled(false);
190    ui->group_runcontrol->setEnabled(false);
191    ui->group_advanced->setEnabled(false);
192    ui->check_advanced->setEnabled(false);
193    ui->check_verbose->setEnabled(false);
194    ui->button_start->setEnabled(false);
195    ui->button_start->setText("wait...");
196
197    branch = QCoreApplication::applicationDirPath();
198    branch = branch.left(branch.length() - 14);
199
200    ui->commandline->setText("Executing MRUN in xterm...");
201
202//  Wait until all commands have been executed (ugly)
203    for(int i=0; i<20; i++)
204       qApp->processEvents();
205
206//  Start xterm as QProcess
207    QProcess mrun;
208    mrun.setProcessChannelMode(QProcess::MergedChannels);
209    mrun.setWorkingDirectory(branch);
210
211    mrunline = " -title \"Executing MRUN...\" -geometry \"100x55+970+0\" -e \""\
212          +mrunline.replace("\"","\'")\
213          +";echo -n '--> Press Enter to continue...';read yesno\"";
214
215    mrun.start("xterm"+mrunline);
216
217    if(!mrun.waitForStarted())
218        return 0;
219
220//    while(mrun.waitForReadyRead())
221//        xtermoutput = xtermoutput+mrun.readAllStandardOutput();
222
223//  Wait until mrun has finished or wait for 20 minutes
224    mrun.waitForFinished(1200000);
225
226
227//  Jobs has been submitted or aborted. Continuing...
228//  Save the mrun command to history file
229    QString filename = branch+"/.mrun.history";
230
231    QDateTime time = QDateTime::currentDateTime();
232    QString tmptime = time.toString("yyyy/MM/dd hh:mm");
233
234    QFile file(filename);
235    file.open(QIODevice::Append | QIODevice::Text);
236    QTextStream out(&file);
237    out << tmptime + " " + history_line + "\n";
238    file.close();
239
240//  Enable main window again
241    ui->group_job->setEnabled(true);
242    ui->group_execution->setEnabled(true);
243    ui->group_runcontrol->setEnabled(true);
244    if ( ui->check_advanced->isChecked() == true)
245    {
246       ui->group_advanced->setEnabled(true);
247    }
248    ui->check_advanced->setEnabled(true);
249    ui->check_verbose->setEnabled(true);
250    ui->button_start->setEnabled(true);
251    ui->action_save->setEnabled(true);
252    ui->button_start->setText("MRUN Start");
253    ui->commandline->setText(mrunline_save);
254
255//  Reload recent jobs
256    recent_jobs(10);
257
258    return 0;
259}
260
261
262//****************************************************************************//
263//  Disable/Enable advanced settings dialog
264int MainWindow::enable_advanced()
265{
266    bool status;
267
268    status = ui->group_advanced->isEnabled();
269    if (status == true)
270    {
271       ui->group_advanced->setEnabled(false);
272    }
273    else
274    {
275       ui->group_advanced->setEnabled(true);
276    }
277
278    return 0;
279}
280
281
282//****************************************************************************//
283//  Disable/enable dialog for coupled runs
284int MainWindow::enable_coupled()
285{
286    QString status;
287
288    status = ui->drop_job->currentText();
289    if (status == "Coupled restart")
290    {
291       ui->label_coupled1->setEnabled(true);
292       ui->label_coupled2->setEnabled(true);
293       ui->label_coupled3->setEnabled(true);
294       ui->line_PE_atmos->setEnabled(true);
295       ui->line_PE_ocean->setEnabled(true);
296
297       QString pe_total = ui->line_pe->text();
298       QString pe_atmos = ui->line_PE_atmos->text();
299       QString pe_ocean = ui->line_PE_ocean->text();
300       if (pe_total != "")
301       {
302           int PE_split = pe_total.toInt()/2;
303           ui->line_PE_atmos->setText(QString::number(PE_split));
304           ui->line_PE_ocean->setText(QString::number(PE_split));
305       }
306    }
307    else
308    {
309        ui->label_coupled1->setEnabled(false);
310        ui->label_coupled2->setEnabled(false);
311        ui->label_coupled3->setEnabled(false);
312        ui->line_PE_atmos->setEnabled(false);
313        ui->line_PE_ocean->setEnabled(false);
314    }
315
316    return 0;
317}
318
319
320//****************************************************************************//
321//  Choose job from dialog
322int MainWindow::choosejob()
323{
324    QString filename;
325    branch = QCoreApplication::applicationDirPath();
326    branch = branch.left(branch.length() - 14);
327
328    QString jobdir = branch+"/JOBS";
329
330
331//  Pick a job from dialog
332    filename = QFileDialog::getExistingDirectory(this, \
333                            tr("Choose a job directory"), branch+"/JOBS", \
334               QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | \
335               QFileDialog::DontUseNativeDialog | QFileDialog::ReadOnly | \
336               QFileDialog::DontConfirmOverwrite);
337
338
339
340
341
342//  If a file was selected, load it into mainwindow
343    if ( filename != "")
344    {
345        filename = filename.right(filename.length() - jobdir.length() - 1);
346
347        ui->line_jobname->setText(filename);
348        ui->list_jobname->clearSelection();
349
350//      Change mrunline accordingly
351        change_commandline("d","");
352        change_commandline("r","");
353        return 0;
354    }
355    else
356    {
357        return 1;
358    }
359}
360
361
362//****************************************************************************//
363//  Choose job from the recent job list and load all settings
364int MainWindow::choosejob_list()
365{
366    QString filename, timestamp, jobname;
367
368//  Get selected item from list
369    filename = ui->list_jobname->currentItem()->text();
370    int itemint = ui->list_jobname->currentRow();
371
372//  Reload list
373    ui->setupUi(this);
374    recent_jobs(10);
375
376//  Set selected item to jobname
377    ui->list_jobname->item(itemint)->setSelected(true);
378
379    timestamp = filename.right(17).left(16);
380    jobname = filename.left(filename.length() - 19);
381
382    branch = QCoreApplication::applicationDirPath();
383    branch = branch.left(branch.length() - 14);
384    QFile file(branch+"/.mrun.history");
385    QString listitem;
386
387//  Load history
388    if ( file.exists() == true && file.size() > 10)
389    {
390
391       QStringList history;
392       if ( file.open(QIODevice::ReadOnly | QIODevice::Text ) )
393       {
394
395//        file opened successfully
396          QTextStream in(&file);
397
398          QString line = in.readLine();
399          while (!line.isNull())
400          {
401             history.append(line);
402             line = in.readLine();
403          }
404
405          file.close();
406       }
407
408       for (int i=history.count(); i>=1; i--)
409       {
410           listitem = history[i-1].right(history[i-1].length() - 17);
411           listitem = listitem.split("-d ", QString::SkipEmptyParts)[1];
412           listitem = listitem.split(" -", QString::SkipEmptyParts)[0];
413           listitem = listitem.replace(" ","");
414
415
416//         Select command line with correct timestamp and jobname
417           if (history[i-1].left(16) == timestamp && listitem == jobname)
418           {
419              QString mrunline = history[i-1];
420              mrunline = mrunline.right(mrunline.length() - 17);
421              ui->commandline->setText(mrunline);
422
423              setup_gui(mrunline);
424           }
425       }
426    }
427
428     return 0;
429}
430
431
432//****************************************************************************//
433//  Change run identifer (initial, restart, coupled...)
434int MainWindow::change_rc_list()
435{
436
437
438   QString drop_job = ui->drop_job->currentText();
439
440   change_commandline("r","");
441
442// Enable PE distribution for atmosphere/ocean
443   if ( drop_job == "Coupled restart")
444   {
445       QString drop_atmos = ui->line_PE_atmos->text();
446       QString drop_ocean = ui->line_PE_ocean->text();
447
448       change_commandline("Y",drop_atmos+" "+drop_ocean);
449   }
450
451// Check of ocean runs
452   else
453   {
454      delete_commandline("Y");
455      if (drop_job == "Precursor run (Ocean)")
456      {
457          activate_flag("y");
458      }
459      else
460      {
461          deactivate_flag("y");
462      }
463   }
464
465
466    return 0;
467}
468
469
470//****************************************************************************//
471//  Routine for processing any changes in the mainwindow settings
472int MainWindow::change_commandline(QString id,QString fwstring)
473{
474
475//  First get the mrunline
476    QString newmrunline;
477    bool    initialize=false;
478
479    QString mrunline = ui->commandline->text();
480
481    QStringList splitline = mrunline.split(" -"+id+"", QString::SkipEmptyParts);
482    if ( splitline.count() == 1)
483    {
484        splitline.append(" ");
485    }
486    else if ( splitline.count() == 0 )
487    {
488       splitline.append("mrun");
489       splitline.append(" ");
490       initialize = true;
491    }
492
493    QStringList param = splitline[1].split("-");
494
495//  Change in parameter "d" (jobname)
496    if (id == "d")
497    {
498       QString filename = ui->line_jobname->text();
499
500       param[0] = filename.replace(" ","");
501
502       if ( initialize == true && ui->group_runcontrol->isEnabled() == true )
503       {
504           ui->group_runcontrol->setEnabled(true);
505           ui->group_execution->setEnabled(true);
506           ui->drop_job->setEnabled(true);
507           ui->check_advanced->setEnabled(true);
508           ui->button_start->setEnabled(true);
509           ui->action_save->setEnabled(true);
510       }
511       else if ( param[0] == "")
512       {
513           ui->group_runcontrol->setEnabled(false);
514           ui->group_execution->setEnabled(false);
515           ui->drop_job->setEnabled(false);
516           ui->button_start->setEnabled(false);
517           ui->action_save->setEnabled(false);
518           ui->group_advanced->setEnabled(false);
519           ui->check_advanced->setEnabled(false);
520           delete_commandline("d");
521           change_commandline("r","remove");
522           ui->label_usercode->setText("");
523           return 1;
524       }
525       else
526       {
527
528//         Check if user code is available
529           branch = QCoreApplication::applicationDirPath();
530           branch = branch.left(branch.length() - 14);
531           QDir usercode = branch+"/USER_CODE/"+param[0];
532           if (usercode.exists() == true)
533           {
534               ui->label_usercode->setText("User code found.");
535           }
536           else
537           {
538               ui->label_usercode->setText("Warning: no user code found!");
539           }
540
541//         Check if _pdf file is available, otherwise notice user
542           if (ui->check_restarts->checkState() == 2)
543           {
544              QString jobname = ui->line_jobname->text();
545              QFile restartfile(branch+"/JOBS/"+jobname+"/INPUT/"+jobname+"_p3df");
546              if (restartfile.exists() == true)
547              {
548                 ui->label_restart->setText("");
549              }
550              else
551              {
552                 ui->label_restart->setText("Warning: No p3df file found!");
553              }
554           }
555
556           ui->group_runcontrol->setEnabled(true);
557           ui->group_execution->setEnabled(true);
558           ui->drop_job->setEnabled(true);
559           ui->check_advanced->setEnabled(true);
560
561           if ( ui->check_advanced->isChecked() == true )
562           {
563              ui->group_advanced->setEnabled(true);
564              if ( ui->line_i->text() != "" || ui->line_o->text() != "")
565              {
566                 change_commandline("r","remove");
567                 ui->group_runcontrol->setEnabled(false);
568              }
569           }
570       }
571
572    }
573
574//  Change in parameter "r" (run control list)
575    else if (id == "r")
576    {
577        int status_ts = ui->check_ts->checkState();
578        int status_pr = ui->check_pr->checkState();
579        int status_xy = ui->check_xy->checkState();
580        int status_xz = ui->check_xz->checkState();
581        int status_yz = ui->check_yz->checkState();
582        int status_3d = ui->check_3d->checkState();
583        int status_ma = ui->check_ma->checkState();
584        int status_sp = ui->check_sp->checkState();
585        int status_pts = ui->check_pts->checkState();
586        int status_prt = ui->check_prt->checkState();
587
588        QString drop_job = ui->drop_job->currentText();
589        QString rc_flag = "#";
590
591        if (drop_job == "Initial run")
592        {
593            rc_flag = "#";
594        }
595        else if (drop_job == "Restart run")
596        {
597            rc_flag = "f";
598        }
599        else if (drop_job == "Precursor run (Atmosphere)")
600        {
601            rc_flag = "#";
602        }
603        else if (drop_job == "Precursor run (Ocean)")
604        {
605           rc_flag = "o#";
606        }
607        else if (drop_job == "Coupled restart")
608        {
609           rc_flag = "f";
610        }
611
612        param[0] = "\"d3"+rc_flag;
613
614        if (status_ts == 2)
615        {
616            param[0] = param[0]+" ts"+rc_flag;
617        }
618        if (status_pr == 2)
619        {
620           param[0] = param[0]+" pr"+rc_flag;
621        }
622        if (status_xy == 2)
623        {
624           param[0] = param[0]+" xy"+rc_flag;
625        }
626        if (status_xz == 2)
627        {
628           param[0] = param[0]+" xz"+rc_flag;
629        }
630        if (status_yz == 2)
631        {
632           param[0] = param[0]+" yz"+rc_flag;
633        }
634        if (status_3d == 2)
635        {
636           param[0] = param[0]+" 3d"+rc_flag;
637        }
638        if (status_ma == 2)
639        {
640           param[0] = param[0]+" ma"+rc_flag;
641        }
642        if (status_sp == 2)
643        {
644           param[0] = param[0]+" sp"+rc_flag;
645        }
646        if (status_prt == 2)
647        {
648           param[0] = param[0]+" prt"+rc_flag;
649        }
650        if (status_pts == 2)
651        {
652           param[0] = param[0]+" pts"+rc_flag;
653        }
654
655        if (drop_job == "Coupled restart")
656        {
657            rc_flag = "of";
658            param[0] = param[0]+" d3"+rc_flag;
659
660            if (status_ts == 2)
661            {
662                param[0] = param[0]+" ts"+rc_flag;
663            }
664            if (status_pr == 2)
665            {
666               param[0] = param[0]+" pr"+rc_flag;
667            }
668            if (status_xy == 2)
669            {
670               param[0] = param[0]+" xy"+rc_flag;
671            }
672            if (status_xz == 2)
673            {
674               param[0] = param[0]+" xz"+rc_flag;
675            }
676            if (status_yz == 2)
677            {
678               param[0] = param[0]+" yz"+rc_flag;
679            }
680            if (status_3d == 2)
681            {
682               param[0] = param[0]+" 3d"+rc_flag;
683            }
684            if (status_ma == 2)
685            {
686               param[0] = param[0]+" ma"+rc_flag;
687            }
688            if (status_sp == 2)
689            {
690               param[0] = param[0]+" sp"+rc_flag;
691            }
692            if (status_prt == 2)
693            {
694               param[0] = param[0]+" prt"+rc_flag;
695            }
696            if (status_pts == 2)
697            {
698               param[0] = param[0]+" pts"+rc_flag;
699            }
700        }
701
702        int status_restarts = ui->check_restarts->checkState();
703
704
705
706        if (status_restarts == 2)
707        {
708            param[0]=param[0]+" restart";
709
710//          Check if _pdf file is available, otherwise notice user
711            if (ui->check_restarts->checkState() == 2)
712            {
713               QString jobname = ui->line_jobname->text();
714               branch = QCoreApplication::applicationDirPath();
715               branch = branch.left(branch.length() - 14);
716               QFile restartfile(branch+"/JOBS/"+jobname+"/INPUT/"+jobname+ \
717                                 "_p3df");
718               if (restartfile.exists() == true)
719               {
720                  ui->label_restart->setText("");
721               }
722               else
723               {
724                  ui->label_restart->setText("Warning: No p3df file found!");
725               }
726            }
727
728        }
729        else  {
730          ui->label_restart->setText("");
731        }
732
733        param[0]=param[0]+"\" ";
734
735        if ( fwstring == "remove")
736        {
737            delete_commandline(id);
738            return 1;
739        }
740        else
741        {
742           ui->button_start->setEnabled(true);
743           ui->action_save->setEnabled(true);
744        }
745    }
746//  Change in any other parameter
747    else
748    {
749        if ( fwstring != "")
750        {
751           param[0] = "\""+fwstring+"\"";
752        }
753        else
754        {
755            delete_commandline(id);
756            return 1;
757        }
758
759    }
760    param[0] = param[0] + " ";
761
762//  Join the new mrunline
763    splitline[1]= param.join("-");
764    newmrunline = splitline.join(" -"+id+" ");
765
766//  Print the new mrunline to mainwindow
767    newmrunline.replace("  "," ");
768    ui->commandline->setText(newmrunline);
769
770    return 0;
771}
772
773
774//****************************************************************************//
775//  Get all signals from mainwindow and perform change via change_commandline()
776int MainWindow::change_lineinput()
777{
778    QString tmptext;
779
780    if ( sender() == ui->line_host )
781    {
782        tmptext = ui->line_host->text();
783        change_commandline("h",tmptext);
784    }
785    else if ( sender() == ui->line_jobname)
786    {
787        tmptext = ui->line_jobname->text();
788        change_commandline("d",tmptext);
789    }
790    else if ( sender() == ui->line_q)
791    {
792        tmptext = ui->line_q->text();
793        change_commandline("q",tmptext);
794    }
795    else if ( sender() == ui->line_account)
796    {
797        tmptext = ui->line_account->text();
798        change_commandline("u",tmptext);
799    }
800    else if ( sender() ==  ui->line_pe)
801    {
802        tmptext = ui->line_pe->text();
803        change_commandline("X",tmptext);
804    }
805    else if ( sender() == ui->line_tpn)
806    {
807        tmptext = ui->line_tpn->text();
808        change_commandline("T",tmptext);
809    }
810    else if ( sender() == ui->line_branch)
811    {
812        tmptext = ui->line_branch->text();
813        change_commandline("K",tmptext);
814    }
815    else if ( sender() == ui->line_time)
816    {
817        tmptext = ui->line_time->text();
818        change_commandline("t",tmptext);
819    }
820    else if ( sender() == ui->line_M)
821    {
822        tmptext = ui->line_M->text();
823        change_commandline("M",tmptext);
824    }
825    else if ( sender() == ui->line_m)
826    {
827        tmptext = ui->line_m->text();
828        change_commandline("m",tmptext);
829    }
830    else if ( sender() == ui->line_a)
831    {
832        tmptext = ui->line_a->text();
833        change_commandline("a",tmptext);
834    }
835    else if ( sender() == ui->line_D)
836    {
837        tmptext = ui->line_D->text();
838        change_commandline("D",tmptext);
839    }
840    else if ( sender() == ui->line_c)
841    {
842        tmptext = ui->line_c->text();
843        if ( tmptext == ".mrun.config")
844        {
845            tmptext = "";
846        }
847        change_commandline("c",tmptext);
848    }
849    else if ( sender() == ui->line_p)
850    {
851        tmptext = ui->line_p->text();
852        change_commandline("p",tmptext);
853    }
854    else if ( sender() == ui->line_s)
855    {
856        tmptext = ui->line_s->text();
857        change_commandline("s",tmptext);
858    }
859    else if ( sender() == ui->line_i)
860    {
861        tmptext = ui->line_i->text();
862        if ( tmptext != "")
863        {
864            change_commandline("r","remove");
865            ui->group_runcontrol->setEnabled(false);
866
867            ui->button_start->setEnabled(true);
868            ui->action_save->setEnabled(true);
869        }
870        else if (ui->line_o->text() == "" )
871        {
872           ui->group_runcontrol->setEnabled(true);
873           change_commandline("r","");
874        }
875
876        change_commandline("i",tmptext);
877    }
878    else if ( sender() == ui->line_o)
879    {
880        tmptext = ui->line_o->text();
881        if ( tmptext != "")
882        {
883            change_commandline("r","remove");
884            ui->button_start->setEnabled(true);
885            ui->action_save->setEnabled(true);
886            ui->group_runcontrol->setEnabled(false);
887        }
888        else if (ui->line_i->text() == "" )
889        {
890           ui->group_runcontrol->setEnabled(true);
891           change_commandline("r","");
892        }
893
894        change_commandline("o",tmptext);
895
896    }
897    else if ( sender() == ui->line_w)
898    {
899        tmptext = ui->line_w->text();
900        change_commandline("w",tmptext);
901    }
902    else if ( sender() == ui->line_PE_atmos || sender() == ui->line_PE_ocean)
903    {
904        tmptext = ui->line_PE_atmos->text() + " "  + ui->line_PE_ocean->text();
905        change_commandline("Y",tmptext);
906        int PE_total = ui->line_PE_atmos->text().toInt() + \
907                        ui->line_PE_ocean->text().toInt();
908        ui->line_pe->setText(QString::number(PE_total));
909        change_commandline("X",QString::number(PE_total));
910    }
911
912    else if ( sender() == ui->combo_n)
913    {
914        tmptext = ui->combo_n->currentText();
915        if ( tmptext == "default")
916        {
917            tmptext = "";
918        }
919        change_commandline("n",tmptext);
920    }
921    else if ( sender() == ui->line_user)
922    {
923       return 0;
924    }
925
926    return 0;
927}
928
929
930//****************************************************************************//
931//  Delete a parameter from mrunline
932int MainWindow::delete_commandline(QString id)
933{
934
935//  Read mrunline
936    QString newmrunline;
937    QString mrunline = ui->commandline->text();
938    QStringList splitline = mrunline.split(" -"+id, QString::SkipEmptyParts);
939    if ( splitline.count() == 1)
940    {
941       return 0;
942    }
943    else
944    {
945       QStringList param = splitline[1].split("-");
946       param[0] = "";
947       splitline[1]= param.join(" -");
948       newmrunline = splitline.join("");
949
950//     Print new mrunline to screen
951       ui->commandline->setText(newmrunline);
952
953       return 0;
954    }
955}
956
957
958//****************************************************************************//
959//  Check routine for all flag parameters
960int MainWindow::check_flags()
961{
962
963    int status = ui->check_delete_tmp_files->checkState();
964
965    if (status == 2)
966    {
967        activate_flag("B");
968    }
969    else
970    {
971        deactivate_flag("B");
972    }
973
974    status = ui->check_verbose->checkState();
975
976    if (status == 2)
977    {
978        activate_flag("v");
979    }
980    else
981    {
982        deactivate_flag("v");
983    }
984
985    status = ui->check_A->checkState();
986
987    if (status == 2)
988    {
989        activate_flag("A");
990    }
991    else
992    {
993        deactivate_flag("A");
994    }
995
996    status = ui->check_b->checkState();
997
998    if (status == 2)
999    {
1000        activate_flag("b");
1001    }
1002    else
1003    {
1004        deactivate_flag("b");
1005    }
1006
1007    status = ui->check_F->checkState();
1008
1009    if (status == 2)
1010    {
1011        activate_flag("F");
1012    }
1013    else
1014    {
1015        deactivate_flag("F");
1016    }
1017    status = ui->check_I->checkState();
1018
1019    if (status == 2)
1020    {
1021        activate_flag("I");
1022    }
1023    else
1024    {
1025        deactivate_flag("I");
1026    }
1027    status = ui->check_k->checkState();
1028
1029    if (status == 2)
1030    {
1031        activate_flag("k");
1032    }
1033    else
1034    {
1035        deactivate_flag("k");
1036    }
1037    status = ui->check_O->checkState();
1038
1039    if (status == 2)
1040    {
1041        activate_flag("O");
1042    }
1043    else
1044    {
1045        deactivate_flag("O");
1046    }
1047    status = ui->check_S->checkState();
1048
1049    if (status == 2)
1050    {
1051        activate_flag("S");
1052    }
1053    else
1054    {
1055        deactivate_flag("S");
1056    }
1057    status = ui->check_x->checkState();
1058
1059    if (status == 2)
1060    {
1061        activate_flag("x");
1062    }
1063    else
1064    {
1065        deactivate_flag("x");
1066    }
1067    return 0;
1068
1069}
1070
1071
1072//****************************************************************************//
1073//  Activate flag
1074int MainWindow::activate_flag(QString id)
1075{
1076    QString newmrunline;
1077
1078    QString mrunline = ui->commandline->text();
1079
1080    QStringList splitline = mrunline.split(" -"+id, QString::SkipEmptyParts);
1081    if ( splitline.count() == 1)
1082    {
1083        splitline.append("");
1084        newmrunline = splitline.join(" -"+id);
1085
1086 //     print new commandline to screen
1087        newmrunline.replace("  "," ");
1088        ui->commandline->setText(newmrunline);
1089
1090       return 0;
1091    }
1092    else
1093    {
1094       return 0;
1095    }
1096}
1097
1098
1099//****************************************************************************//
1100//  Deactivate flag
1101int MainWindow::deactivate_flag(QString id)
1102{
1103    QString newmrunline;
1104
1105    QString mrunline = ui->commandline->text();
1106
1107    QStringList splitline = mrunline.split(" -"+id, QString::SkipEmptyParts);
1108
1109    newmrunline = splitline.join("");
1110//  print new commandline to screen
1111    newmrunline.replace("  "," ");
1112    ui->commandline->setText(newmrunline);
1113
1114    return 0;
1115}
1116
1117
1118//****************************************************************************//
1119//  Mainwindow reset
1120int MainWindow::reset_window()
1121{
1122    ui->setupUi(this);
1123    recent_jobs(10);
1124    return 0;
1125}
1126
1127
1128//****************************************************************************//
1129//  Save current setting as default
1130int MainWindow::save_default()
1131{
1132
1133//  Get mrunline
1134    QString mrunline_save;
1135    QString mrunline = ui->commandline->text();
1136    QString userline = ui->line_user->text();
1137
1138    mrunline_save = mrunline;
1139    if (userline != "")
1140    {
1141        mrunline = mrunline + " " + userline;
1142    }
1143
1144
1145//  Define filename
1146    branch = QCoreApplication::applicationDirPath();
1147    branch = branch.left(branch.length() - 14);
1148
1149    QString filename = branch+"/.mrun.gui.default";
1150
1151//  Prepare data output
1152    QDateTime time = QDateTime::currentDateTime();
1153    QString tmptime = time.toString("yyyy/MM/dd hh:mm");
1154
1155//  Write to file
1156    QFile file(filename);
1157    file.open(QIODevice::WriteOnly | QIODevice::Text);
1158    QTextStream out(&file);
1159    if ( mrunline == "" || mrunline == " ")
1160    {
1161        out << "";
1162    }
1163    else
1164    {
1165       out << tmptime + " " + mrunline;
1166    }
1167
1168    file.close();
1169
1170    return 0;
1171}
1172
1173
1174//****************************************************************************//
1175//  Save current settings to a file of choice
1176int MainWindow::save_to_file()
1177{
1178
1179//  Get mrunline
1180    QString mrunline_save;
1181    QString mrunline = ui->commandline->text();
1182    QString userline = ui->line_user->text();
1183
1184    mrunline_save = mrunline;
1185    if (userline != "")
1186    {
1187        mrunline = mrunline + " " + userline;
1188    }
1189
1190//  Define a filename
1191    QString filename = QFileDialog::getSaveFileName(this, tr("Save to file"), \
1192                                    "", tr("Save files (*.sav)"));
1193    QString extension = filename.right(4);
1194
1195    if ( extension != ".sav" )
1196    {
1197        filename = filename + ".sav";
1198    }
1199
1200//  Prepare data output
1201    QDateTime time = QDateTime::currentDateTime();
1202    QString tmptime = time.toString("yyyy/MM/dd hh:mm");
1203
1204//  Write to file
1205    QFile file(filename);
1206    file.open(QIODevice::WriteOnly | QIODevice::Text);
1207    QTextStream out(&file);
1208    out << tmptime + " " + mrunline;
1209    file.close();
1210
1211    return 0;
1212}
1213
1214
1215//****************************************************************************//
1216//  Open job from file (previously saved by the user)
1217int MainWindow::open_from_file()
1218{
1219
1220//  Select filename and open it
1221    QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), \
1222                                    "", tr("Save files (*.sav)"));
1223
1224    QFile file(filename);
1225    QString mrunline;
1226    if ( filename != "")
1227    {
1228       if ( file.open(QIODevice::ReadOnly | QIODevice::Text ) )
1229       {
1230
1231//        File opened successfully
1232          QTextStream in(&file);
1233
1234          QString line = in.readLine();
1235          while (!line.isNull())
1236          {
1237             mrunline = line;
1238             line = in.readLine();
1239          }
1240          file.close();
1241       }
1242
1243//     In case a mrunline was found, load it to mainwindow
1244       if ( mrunline != "")
1245       {
1246
1247          mrunline = mrunline.right(mrunline.length() - 17);
1248          ui->commandline->setText(mrunline);
1249
1250          setup_gui(mrunline);
1251       }
1252    }
1253    return 0;
1254}
1255
1256
1257//****************************************************************************//
1258//  Open the last submitted job
1259int MainWindow::open_last()
1260{
1261    branch = QCoreApplication::applicationDirPath();
1262    branch = branch.left(branch.length() - 14);
1263
1264    QFile file(branch+"/.mrun.history");
1265
1266//  Load mrun history
1267    QString mrunline;
1268    if ( file.open(QIODevice::ReadOnly | QIODevice::Text ) )
1269    {
1270
1271//     file opened successfully
1272       QTextStream in(&file);
1273
1274       QString line = in.readLine();
1275       while (!line.isNull())
1276       {
1277          mrunline = line;
1278          line = in.readLine();
1279       }
1280
1281       file.close();
1282    }
1283
1284//  Select the last submitted job and print it to mainwindow
1285    if ( mrunline != "" )
1286    {
1287       mrunline = mrunline.right(mrunline.length() - 17);
1288       ui->commandline->setText(mrunline);
1289
1290       setup_gui(mrunline);
1291    }
1292    return 0;
1293}
1294
1295//****************************************************************************//
1296//  Display help window
1297int MainWindow::help()
1298{
1299
1300    QDialog *child_help = new QDialog;
1301    Ui::child_help ui;
1302    ui.setupUi(child_help);
1303    child_help->show();
1304
1305
1306    return 0;
1307}
1308
1309//****************************************************************************//
1310//  Display about window
1311int MainWindow::about_gui()
1312{
1313
1314    QDialog *about = new QDialog;
1315    Ui::about ui;
1316    ui.setupUi(about);
1317    about->show();
1318
1319    return 0;
1320}
1321
1322//****************************************************************************//
1323//  Setup mainwindow according to a given mrunline
1324int MainWindow::setup_gui(QString mrunline)
1325{
1326
1327//  Some initial settings
1328    QString user = "";
1329
1330    bool coupled_run = false;
1331    bool ocean_run   = false;
1332    bool advanced    = false;
1333    bool nojob       = false;
1334    bool rc_manual   = false;
1335
1336//  Split parameters in mrunline
1337    QStringList splitline = mrunline.split(" -", QString::SkipEmptyParts);
1338
1339    if ( splitline[0] != "mrun")
1340    {
1341        return 1;
1342
1343    }
1344    else
1345    {
1346        ui->group_job->setEnabled(true);
1347
1348//      Loop for all parameters in mrunline
1349        for(int i=splitline.count()-1; i>=1; i--)
1350        {
1351
1352//          Determine parameter
1353            QStringList splitparameter = splitline[i].split(" ", \
1354                                                      QString::SkipEmptyParts);
1355
1356            QString parameter = splitparameter[0];
1357            splitparameter.removeFirst();
1358            QString options = splitparameter.join(" ");
1359            options = options.replace("\"","");
1360
1361//          Check for suitable switch
1362            if ( parameter == "d")
1363            {
1364               if ( options != "")
1365               {
1366                  ui->line_jobname->setText(options);
1367                  nojob = false;
1368               }
1369               else
1370               {
1371                  nojob = true;
1372               }
1373            }
1374            else if ( parameter == "h")
1375            {
1376               ui->line_host->setText(options);
1377            }
1378            else if ( parameter == "q")
1379            {
1380               ui->line_q->setText(options);
1381            }
1382            else if ( parameter == "K")
1383            {
1384               ui->line_branch->setText(options);
1385            }
1386            else if ( parameter == "u")
1387            {
1388               ui->line_account->setText(options);
1389            }
1390            else if ( parameter == "X")
1391            {
1392               ui->line_pe->setText(options);
1393            }
1394            else if ( parameter == "T")
1395            {
1396               ui->line_tpn->setText(options);
1397            }
1398            else if ( parameter == "t")
1399            {
1400               ui->line_time->setText(options);
1401            }
1402            else if ( parameter == "B")
1403            {
1404               ui->check_delete_tmp_files->setChecked(true);
1405            }
1406            else if ( parameter == "v")
1407            {
1408               ui->check_verbose->setChecked(true);
1409            }
1410            else if ( parameter == "A")
1411            {
1412               ui->check_A->setChecked(true);
1413
1414            }
1415            else if ( parameter == "b")
1416            {
1417               ui->check_b->setChecked(true);
1418               advanced = true;
1419            }
1420            else if ( parameter == "F")
1421            {
1422               ui->check_F->setChecked(true);
1423               advanced = true;
1424            }
1425            else if ( parameter == "I")
1426            {
1427               ui->check_I->setChecked(true);
1428               advanced = true;
1429            }
1430            else if ( parameter == "k")
1431            {
1432               ui->check_k->setChecked(true);
1433               advanced = true;
1434            }
1435            else if ( parameter == "O")
1436            {
1437               ui->check_O->setChecked(true);
1438               advanced = true;
1439            }
1440            else if ( parameter == "S")
1441            {
1442               ui->check_S->setChecked(true);
1443               advanced = true;
1444            }
1445            else if ( parameter == "x")
1446            {
1447               ui->check_x->setChecked(true);
1448               advanced = true;
1449            }
1450            else if ( parameter == "m")
1451            {
1452               ui->line_m->setText(options);
1453               advanced = true;
1454            }
1455            else if ( parameter == "M")
1456            {
1457               ui->line_M->setText(options);
1458               advanced = true;
1459            }
1460            else if ( parameter == "a")
1461            {
1462               ui->line_a->setText(options);
1463               advanced = true;
1464            }
1465            else if ( parameter == "D")
1466            {
1467               ui->line_D->setText(options);
1468               advanced = true;
1469            }
1470            else if ( parameter == "c")
1471            {
1472               ui->line_c->setText(options);
1473               advanced = true;
1474            }
1475            else if ( parameter == "p")
1476            {
1477               ui->line_p->setText(options);
1478               advanced = true;
1479            }
1480            else if ( parameter == "s")
1481            {
1482               ui->line_s->setText(options);
1483               advanced = true;
1484            }
1485            else if ( parameter == "i")
1486            {
1487               ui->line_i->setText(options);
1488               advanced = true;
1489               rc_manual = true;
1490            }
1491            else if ( parameter == "o")
1492            {
1493               ui->line_o->setText(options);
1494               advanced = true;
1495               rc_manual = true;
1496            }
1497            else if ( parameter == "w")
1498            {
1499               ui->line_w->setText(options);
1500               advanced = true;
1501            }
1502
1503//          Determine settings for coupled restart runs
1504            else if ( parameter == "Y")
1505            {
1506                QStringList optionssplit = options.split(" ", \
1507                                                   QString::SkipEmptyParts);
1508
1509                ui->line_PE_atmos->setEnabled(true);
1510                ui->line_PE_ocean->setEnabled(true);
1511                ui->label_coupled1->setEnabled(true);
1512                ui->label_coupled2->setEnabled(true);
1513                ui->label_coupled3->setEnabled(true);
1514                ui->label_coupling->setEnabled(true);
1515
1516                if (optionssplit.count() == 2)
1517                {
1518                   ui->line_PE_atmos->setText(optionssplit[0]);
1519                   ui->line_PE_ocean->setText(optionssplit[1]);
1520                }
1521                else
1522                {
1523                   ui->line_PE_atmos->setText("");
1524                   ui->line_PE_ocean->setText("");
1525                }
1526                coupled_run = true;
1527            }
1528            else if ( parameter == "n")
1529            {
1530                if ( options == "shared")
1531                {
1532                    ui->combo_n->setCurrentIndex(1);
1533                }
1534                else if ( options == "non_shared")
1535                {
1536                    ui->combo_n->setCurrentIndex(2);
1537                }
1538                else
1539                {
1540                    ui->combo_n->setCurrentIndex(0);
1541                }
1542            }
1543            else if ( parameter == "y")
1544            {
1545               ui->drop_job->setCurrentIndex(3);
1546            }
1547
1548//          Determine settings for the run control list
1549            else if ( parameter == "r")
1550            {
1551               QStringList optionssplit = options.split(" ", \
1552                                          QString::SkipEmptyParts);
1553
1554               QString options_2;
1555               QString options_all;
1556               for (int j=0; j<optionssplit.count(); j++)
1557               {
1558                   options_all = optionssplit[j];
1559                   options_2 = optionssplit[j].left(2);
1560                   if (options_2 == "ts")
1561                   {
1562                       ui->check_ts->setChecked(true);
1563                   }
1564                   if (options_2 == "pr" && options_all.left(3) != "prt")
1565                   {
1566                       ui->check_pr->setChecked(true);
1567                   }
1568                   if (options_2 == "xy")
1569                   {
1570                       ui->check_xy->setChecked(true);
1571                   }
1572                   if (options_2 == "xz")
1573                   {
1574                       ui->check_xz->setChecked(true);
1575                   }
1576                   if (options_2 == "yz")
1577                   {
1578                       ui->check_yz->setChecked(true);
1579                   }
1580                   if (options_2 == "3d")
1581                   {
1582                       ui->check_3d->setChecked(true);
1583                   }
1584                   if (options_2 == "ma")
1585                   {
1586                       ui->check_ma->setChecked(true);
1587                   }
1588                   if (options_2 == "sp")
1589                   {
1590                       ui->check_sp->setChecked(true);
1591                   }
1592                   if (options_all.left(3) == "prt")
1593                   {
1594                       ui->check_prt->setChecked(true);
1595                   }
1596                   if (options_all.left(3) == "pts")
1597                   {
1598                       ui->check_pts->setChecked(true);
1599                   }
1600                   if (options_2 == "d3")
1601                   {
1602                       if (options_all.left(3).right(1) == "#")
1603                       {
1604                          ui->drop_job->setCurrentIndex(0);
1605                       }
1606                       else if (options_all.left(3).right(1) == "f")
1607                       {
1608                          ui->drop_job->setCurrentIndex(1);
1609                       }
1610                       else if (options_all.left(3).right(1) == "o")
1611                       {
1612                          ocean_run = true;
1613                       }
1614                   }
1615                   if (options_all == "restart")
1616                   {
1617                      ui->check_restarts->setChecked(true);
1618//                    Check if _pdf file is available, otherwise notice user
1619                      QString jobname = ui->line_jobname->text();
1620                      branch = QCoreApplication::applicationDirPath();
1621                      branch = branch.left(branch.length() - 14);
1622
1623                      QFile restartfile(branch+"/JOBS/"+jobname+"/INPUT/"+ \
1624                                        jobname+"_p3df");
1625                      if (restartfile.exists() == true)
1626                      {
1627                         ui->label_restart->setText("");
1628                      }
1629                      else
1630                      {
1631                         ui->label_restart->setText("Warning: No p3df file \
1632                                                    found!");
1633                      }
1634                   }
1635
1636               }
1637
1638            }
1639
1640//          All unknown parameters are set as extra user parameters
1641            else
1642            {
1643                user = user + "-" + parameter + " \"" + options + "\" ";
1644                splitline.removeAt(i);
1645            }
1646        }
1647
1648//      Change drop box state in case of ocean precursor or coupled restart runs
1649        if ( ocean_run == true )
1650        {
1651           if ( coupled_run == true )
1652           {
1653             ui->drop_job->setCurrentIndex(4);
1654           }
1655           else
1656           {
1657              ui->drop_job->setCurrentIndex(3);
1658           }
1659        }
1660
1661        if ( user != "")
1662        {
1663           ui->line_user->setText(user);
1664        }
1665
1666//      Join mrunline and post it to mainwindow
1667        mrunline = splitline.join(" -");
1668        ui->commandline->setText(mrunline);
1669
1670//      Check if advanced settings are used and enable advanced dialog
1671        if ( advanced == true )
1672        {
1673           ui->check_advanced->setChecked(true);
1674        }
1675
1676//      Disable mainwindow if no job was found, otherwise enable
1677        if ( nojob == true )
1678        {
1679           ui->group_execution->setEnabled(false);
1680           ui->group_runcontrol->setEnabled(false);
1681           ui->button_start->setEnabled(false);
1682           ui->action_save->setEnabled(false);
1683           ui->drop_job->setEnabled(false);
1684           ui->group_advanced->setEnabled(false);
1685           ui->check_advanced->setEnabled(false);
1686        }
1687        else
1688        {
1689           ui->group_execution->setEnabled(true);
1690           ui->group_runcontrol->setEnabled(true);
1691           ui->button_start->setEnabled(true);
1692           ui->action_save->setEnabled(true);
1693           ui->check_advanced->setEnabled(true);
1694           ui->drop_job->setEnabled(true);
1695           if ( advanced == true )
1696           {
1697              ui->group_advanced->setEnabled(true);
1698           }
1699           else
1700           {
1701              ui->group_advanced->setEnabled(false);
1702           }
1703        }
1704
1705//      Disable run control box, if manual settings of -i and -o are used
1706        if ( rc_manual == true )
1707        {
1708           ui->group_runcontrol->setEnabled(false);
1709           change_commandline("r","remove");
1710           ui->button_start->setEnabled(true);
1711           ui->action_save->setEnabled(true);
1712        }
1713
1714    }
1715
1716    return 0;
1717}
Note: See TracBrowser for help on using the repository browser.