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

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

bugfix in mbuild (utility compilation), bugfix in mrungui

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