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

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

GUI for mrun added

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