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

Last change on this file since 2068 was 2068, checked in by maronga, 7 years ago

last commit documented

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