src/job_editor_widget/jobtextedit.cpp

Go to the documentation of this file.
00001 
00008 #include <QRegExp>
00009 #include "jobtextedit.h"
00010 #include <QtDebug>
00011 
00017 JobTextEdit::JobTextEdit(QWidget *parent) : QTextEdit(parent)
00018 {
00019     highlighter = new Highlighter(document());
00020     connect(this, SIGNAL(textChanged()),
00021             this, SLOT(onTextChanged()));
00022     setAcceptRichText(false);
00023 
00024 
00025     outPty = new JobTextEditProperty(QString(""),
00026                                      QString("o"),
00027                                      QString("([\\S]*)"), // TODO
00028                                      this);
00029     errPty = new JobTextEditProperty(QString(""),
00030                                      QString("e"),
00031                                      QString("([\\S]*)"), // TODO
00032                                      this);
00033     joinPty = new JobTextEditProperty(QString(""),
00034                                       QString("j"),
00035                                       QString("(oe|eo|n)"),
00036                                       this);
00037     mailPty = new JobTextEditProperty(QString(""),
00038                                       QString("M"),
00039                                       QString("([\\S]*)"), // TODO: user[@host][,user[@host],...]
00040                                       this);
00041     namePty = new JobTextEditProperty(QString(""),
00042                                       QString("N"),
00043                                       QString("([A-Z,a-z]\\S{,14})"),
00044                                       this);
00045     walltimePty = new JobTextEditProperty(QString("walltime"),
00046                                           QString("l"),
00047                                           QString("(\\d+|\\d+:\\d+|\\d+:\\d+:\\d+)"),
00048                                           this);
00049     nodesPty = new JobTextEditProperty(QString("nodes"),
00050                                        QString("l"),
00051                                        QString("([\\S\\:\\+\\=]*)"),
00052                                        this);
00053     nicePty = new JobTextEditProperty(QString("nice"),
00054                                       QString("l"),
00055                                       QString("(-?[0-2]?\\d)"),
00056                                       this);
00057     cputPty = new JobTextEditProperty(QString("cput"),
00058                                       QString("l"),
00059                                       QString("(\\d+|\\d+:\\d+|\\d+:\\d+:\\d+)"),
00060                                       this);
00061     pcputPty = new JobTextEditProperty(QString("pcput"),
00062                                        QString("l"),
00063                                        QString("(\\d+|\\d+:\\d+|\\d+:\\d+:\\d+)"),
00064                                        this);
00065     filePty = new JobTextEditProperty(QString("file"),
00066                                       QString("l"),
00067                                       QString("(\\d+[kmgt]?[bw]?)"),
00068                                       this);
00069     memPty = new JobTextEditProperty(QString("mem"),
00070                                      QString("l"),
00071                                      QString("(\\d+[kmgt]?[bw]?)"),
00072                                      this);
00073     pmemPty = new JobTextEditProperty(QString("pmem"),
00074                                       QString("l"),
00075                                       QString("(\\d+[kmgt]?[bw]?)"),
00076                                       this);
00077     vmemPty = new JobTextEditProperty(QString("vmem"),
00078                                       QString("l"),
00079                                       QString("(\\d+[kmgt]?[bw]?)"),
00080                                       this);
00081     pvmemPty = new JobTextEditProperty(QString("pvmem"),
00082                                       QString("l"),
00083                                       QString("(\\d+[kmgt]?[bw]?)"),
00084                                       this);
00085     archPty = new JobTextEditProperty(QString("arch"),
00086                                       QString("l"),
00087                                       QString("([\\S]*)"),
00088                                       this);
00089     hostPty = new JobTextEditProperty(QString("host"),
00090                                       QString("l"),
00091                                       QString("([\\S]*)"),
00092                                       this);
00093     otherPty = new JobTextEditProperty(QString("other"),
00094                                       QString("l"),
00095                                       QString("([\\S]*)"),
00096                                       this);
00097     opsysPty = new JobTextEditProperty(QString("opsys"),
00098                                       QString("l"),
00099                                       QString("([\\S]*)"),
00100                                       this);
00101     softwarePty = new JobTextEditProperty(QString("software"),
00102                                       QString("l"),
00103                                       QString("([\\S]*)"),
00104                                       this);
00105 }
00106 
00110 JobTextEdit::~JobTextEdit()
00111 {
00112     delete walltimePty;
00113     delete nodesPty;
00114     delete errPty;
00115     delete outPty;
00116     delete joinPty;
00117     delete mailPty;
00118     delete namePty;
00119     delete cputPty;
00120     delete filePty;
00121     delete nicePty;
00122     delete pcputPty;
00123     delete pmemPty;
00124     delete pvmemPty;
00125     delete vmemPty;
00126     delete archPty;
00127     delete hostPty;
00128     delete otherPty;
00129     delete softwarePty;
00130     delete opsysPty;
00131 
00132     //delete highlighter; // FIXME
00133 }
00134 
00138 void JobTextEdit::onTextChanged()
00139 {
00140     walltimePty->checkForPropertyChanged();
00141     nodesPty->checkForPropertyChanged();
00142     errPty->checkForPropertyChanged();
00143     outPty->checkForPropertyChanged();
00144     joinPty->checkForPropertyChanged();
00145     mailPty->checkForPropertyChanged();
00146     namePty->checkForPropertyChanged();
00147     cputPty->checkForPropertyChanged();
00148     filePty->checkForPropertyChanged();
00149     nicePty->checkForPropertyChanged();
00150     pcputPty->checkForPropertyChanged();
00151     pmemPty->checkForPropertyChanged();
00152     pvmemPty->checkForPropertyChanged();
00153     vmemPty->checkForPropertyChanged();
00154     archPty->checkForPropertyChanged();
00155     hostPty->checkForPropertyChanged();
00156     otherPty->checkForPropertyChanged();
00157     softwarePty->checkForPropertyChanged();
00158     opsysPty->checkForPropertyChanged();
00159 }
00160 
00161 
00162 // ------------------------- JobTextEditProperty ------------------------- //
00171 JobTextEditProperty::JobTextEditProperty(QString name,
00172                                          QString opt,
00173                                          QString rxPattern,
00174                                          JobTextEdit * jte)
00175 : QObject(), _name(name), _opt(opt), _jte(jte)
00176 {
00177     //TODO: improve: check for "," and for #PBS at the beginning of the line
00178     if (name.isEmpty())
00179         _rx = QRegExp("#PBS -"+opt+" "+rxPattern+"[^\\n]*");
00180     else
00181         _rx = QRegExp("#PBS -l "+_name+"="+rxPattern+"[^\\n]*");
00182 }
00183 
00187 void JobTextEditProperty::checkForPropertyChanged()
00188 {
00189     QString plainText = _jte->toPlainText();
00190     if (_rx.indexIn(plainText) != -1)
00191     {
00192         QStringList list = _rx.capturedTexts();
00193         QString newvalue = list.at(1);
00194         if (newvalue != _value)
00195         {
00196             _value = newvalue;
00197             emit propertyChanged(_value);
00198         }
00199     } else {
00200         if (!_value.isEmpty())
00201         {
00202             _value = "";
00203             emit propertyChanged(QString(""));
00204         }
00205     }
00206 }
00207 
00213 void JobTextEditProperty::setProperty(const QString & pty)
00214 {
00215    if (pty.compare(_value) != 0)
00216     {
00217         if (pty.isEmpty())
00218         { // This property is no more used, remove it from the text widget
00219             QTextDocument * doc = _jte->document();
00220             QTextCursor cur;
00221             if (_name.isEmpty())
00222                 cur = doc->find("#PBS -"+_opt+" "+_value);
00223             else
00224                 cur = doc->find("#PBS -"+_opt+" "+_name+"="+_value);
00225             cur.removeSelectedText();
00226             _value = pty;
00227         } else {
00228             QString plainText = _jte->toPlainText();
00229             _value = pty;
00230 
00231             if (_rx.indexIn(plainText) != -1)
00232             {
00233                 if (_name.isEmpty())
00234                     plainText.replace(_rx, "-"+_opt+" "+_value);
00235                 else
00236                     plainText.replace(_rx, " "+_name+"="+_value);
00237                 _jte->setPlainText(plainText);
00238             } else {
00239                 if (_name.isEmpty())
00240                     addPBSProperty("-"+_opt+" "+_value);
00241                 else
00242                     addPBSProperty("-"+_opt+" "+_name+"="+_value);
00243             }
00244         }
00245     }
00246 }
00247 
00256 void JobTextEditProperty::addPBSProperty(const QString & str)
00257 {
00258 
00259 
00260     QTextDocument * doc = _jte->document();
00261     QTextCursor cur = doc->find(QRegExp("^(?: |\t)*#PBS "));
00262     if (cur.isNull())
00263     {
00264         cur = doc->find(QRegExp("^(?: |\t)*(?!#)"));
00265         // cur can not be null
00266         cur.setPosition(cur.position());
00267     } else {
00268         cur.setPosition(cur.anchor());
00269     }
00270     cur.insertText(QString("#PBS ")+str+"\n");
00271 }

Generated on Mon Mar 16 18:46:05 2009 for QCJM by  doxygen 1.5.4