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

Last change on this file since 1804 was 1804, checked in by maronga, 8 years ago

removed parameter file check. update of mrungui for compilation with qt5

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