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

Last change on this file since 1046 was 1046, checked in by maronga, 11 years ago

put scripts and utilities under GPL

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