src/job_editor_widget/jobpropertyitemnodes.cpp

Go to the documentation of this file.
00001 
00008 #include "jobpropertyitemnodes.h"
00009 #include "jobpropertyitemstring.h"
00010 #include <QLineEdit>
00011 #include <QMenu>
00012 #include <QAction>
00013 #include <QDebug>
00014 
00015 // ---------------------- Nodes -------------------- //
00016 
00023 JobPropertyItemNodes::JobPropertyItemNodes(JobPropertyModel * model, JobPropertyItem *parent)
00024         :JobPropertyItem(model)
00025 {
00026     _name = QString(tr("Nodes"));
00027     parentItem = parent;
00028     QVector<QVariant> data;
00029     data << _name << "";
00030     itemData = data;
00031     setReadOnly(true);
00032     setHaveContextMenu(true);
00033 }
00034 
00038 JobPropertyItemNodes::~JobPropertyItemNodes()
00039 {
00040     qDeleteAll(childItems);
00041 }
00042 
00049 bool JobPropertyItemNodes::setData(int column, const QVariant &value)
00050 {
00051     Q_UNUSED(column);
00052     Q_UNUSED(value);
00053     return false;
00054 }
00055 
00063 QWidget *JobPropertyItemNodes::createEditor(QWidget *parent, const QObject *target, const char *receiver) const
00064 {
00065     Q_UNUSED(target);
00066     Q_UNUSED(receiver);
00067     return new QWidget(parent);
00068 }
00069 
00075 void JobPropertyItemNodes::updateEditorContents(QWidget *editor)
00076 {
00077     Q_UNUSED(editor);
00078 }
00079 
00085 void JobPropertyItemNodes::updateValue(QWidget *editor)
00086 {
00087     Q_UNUSED(editor);
00088 }
00089 
00095 void JobPropertyItemNodes::set(const QString & str)
00096 {
00097     if (_nodes != str)
00098     {
00099         _nodes = str;
00100         QStringList l = str.split("+", QString::SkipEmptyParts);
00101         _model->emitLayoutAboutToBeChanged();
00102         qDeleteAll(childItems);
00103         childItems.clear();
00104         for (int i = 0; i < l.size(); ++i)
00105         {
00106             JobPropertyItemNode * node = new JobPropertyItemNode(_model, this);
00107             node->set(l.at(i));
00108             QObject::connect(node, SIGNAL(itemChanged()),
00109                              this, SLOT(onChildChanged()));
00110             childItems.append(static_cast<JobPropertyItem*>(node));
00111         }
00112         _model->emitLayoutChanged();
00113     }
00114 }
00115 
00121 void JobPropertyItemNodes::onChildChanged()
00122 {
00123     update();
00124     emit itemChanged(_nodes);
00125 }
00126 
00130 void JobPropertyItemNodes::update()
00131 {
00132     QStringList l;
00133     for (int i = 0; i < childItems.size(); ++i)
00134     {
00135         l.append(dynamic_cast<JobPropertyItemNode*>(childItems.at(i))->toString());
00136     }
00137     if (l.empty())
00138         _nodes =  QString("");
00139     else
00140         _nodes = l.join(QString("+"));
00141 }
00142 
00146 QString JobPropertyItemNodes::toString()
00147 {
00148     return _nodes;
00149 }
00150 
00157 void JobPropertyItemNodes::createCustomContextMenu(QWidget *parent, const QPoint & pos)
00158 {
00159     QMenu menu(parent);
00160     QAction *addNodeAction = menu.addAction(tr("&Add a node"));
00161     addNodeAction->setEnabled(true);
00162     const QAction *result = menu.exec(parent->mapToGlobal(pos));
00163     if (result == addNodeAction)
00164     {
00165         addNode();
00166     }
00167 }
00168 
00172 void JobPropertyItemNodes::addNode()
00173 {
00174     JobPropertyItemNode * node;
00175     _model->emitLayoutAboutToBeChanged();
00176     node = new JobPropertyItemNode(_model, this);
00177     QObject::connect(node, SIGNAL(itemChanged()),
00178                      this, SLOT(onChildChanged()));
00179     childItems.append(static_cast<JobPropertyItem*>(node));
00180     _model->emitLayoutChanged();
00181 }
00182 
00188 bool JobPropertyItemNodes::deleteChild(int row)
00189 {
00190     if(row >=0 && row<childItems.size())
00191     {
00192         delete childItems.takeAt(row);
00193         update();
00194         _nodes = this->toString();
00195         emit itemChanged(_nodes);
00196         return true;
00197     }
00198     return false;
00199 }
00200 
00201 
00202 // ---------------------- Node -------------------- //
00209 JobPropertyItemNode::JobPropertyItemNode(JobPropertyModel * model, JobPropertyItemNodes *parent)
00210         :JobPropertyItem(model)
00211 {
00212     _name = tr("Node");
00213     parentItem = parent;
00214     QVector<QVariant> data;
00215     data << _name << "";
00216     itemData = data;
00217     setHaveContextMenu(true);
00218 
00219     JobPropertyItemString * ppn = new JobPropertyItemString(model, QString("ppn="), this);
00220     QObject::connect(ppn, SIGNAL(itemChanged(const QString &)),
00221                      this, SLOT(onChildChanged(const QString &)));
00222     ppn->setRegExpValidator(QRegExp("(\\d+)"));
00223     childItems.append(static_cast<JobPropertyItem*>(ppn));
00224 }
00225 
00229 JobPropertyItemNode::~JobPropertyItemNode()
00230 {
00231     qDeleteAll(childItems);
00232 }
00233 
00240 bool JobPropertyItemNode::setData(int column, const QVariant &value)
00241 {
00242     if (column < 1 || column >= itemData.size())
00243         return false;
00244 
00245     itemData[column] = value;
00246     return true;
00247 }
00248 
00256 QWidget *JobPropertyItemNode::createEditor(QWidget *parent, const QObject *target, const char *receiver) const
00257 {
00258     QLineEdit * strEditor = new QLineEdit(parent);
00259 
00260     QObject::connect(strEditor, SIGNAL(textChanged(QString)), target, receiver);
00261     return strEditor;
00262 }
00263 
00269 void JobPropertyItemNode::updateEditorContents(QWidget *editor)
00270 {
00271     QLineEdit * lineEditor = qobject_cast<QLineEdit *>(editor);
00272     QString value = itemData[1].toString();
00273     lineEditor->setText(value);
00274 }
00275 
00281 void JobPropertyItemNode::updateValue(QWidget *editor)
00282 {
00283     QLineEdit * lineEditor = qobject_cast<QLineEdit *>(editor);
00284     QString newValue = lineEditor->text();
00285     if (newValue != itemData[1])
00286     {
00287         itemData[1] = newValue;
00288         update();
00289         emit itemChanged();
00290     }
00291 }
00292 
00298 void JobPropertyItemNode::set(const QString & newnode)
00299 {
00300     if (_node != newnode)
00301     {
00302         _node = newnode;
00303         QStringList l = newnode.split(":", QString::SkipEmptyParts);
00304         _model->emitLayoutAboutToBeChanged();
00305         qDeleteAll(childItems);
00306         childItems.clear();
00307 
00308         itemData[1] = l.at(0);
00309 
00310         JobPropertyItemString * ppn = new JobPropertyItemString(_model, QString("ppn="), this);
00311         ppn->setRegExpValidator(QRegExp("(\\d+)"));
00312         QObject::connect(ppn, SIGNAL(itemChanged(const QString &)),
00313                          this, SLOT(onChildChanged(const QString &)));
00314         childItems.append(static_cast<JobPropertyItem*>(ppn));
00315 
00316         for (int i = 1; i < l.size(); ++i)
00317         {
00318             QString str = l.at(i);
00319             if (str.startsWith("ppn="))
00320             {
00321                 ppn->set(str.mid(4));
00322             } else {
00323                 JobPropertyItemNodeProperty * pty = new JobPropertyItemNodeProperty(_model, this);
00324                 pty->set(str);
00325                 QObject::connect(pty, SIGNAL(itemChanged(const QString &)),
00326                                  this, SLOT(onChildChanged(const QString &)));
00327                 childItems.append(static_cast<JobPropertyItem*>(pty));
00328             }
00329         }
00330         _model->emitLayoutChanged();
00331     }
00332 }
00333 
00337 QString JobPropertyItemNode::toString()
00338 {
00339     return _node;
00340 }
00341 
00348 void JobPropertyItemNode::createCustomContextMenu(QWidget *parent, const QPoint & pos)
00349 {
00350     QMenu menu(parent);
00351     QAction *deleteNode = menu.addAction(tr("&Delete this node"));
00352     QAction *addProperty = menu.addAction(tr("&Add a property"));
00353     deleteNode->setEnabled(true);
00354     addProperty->setEnabled(true);
00355     const QAction *result = menu.exec(parent->mapToGlobal(pos));
00356     if (result == deleteNode)
00357         _model->removeRow(childNumber(), parentItem->getSelfIndex());
00358     else if (result == addProperty)
00359     {
00360         JobPropertyItemNodeProperty * pty;
00361         _model->emitLayoutAboutToBeChanged();
00362         pty = new JobPropertyItemNodeProperty(_model, this);
00363         QObject::connect(pty, SIGNAL(itemChanged(const QString &)),
00364                          this, SLOT(onChildChanged(const QString &)));
00365         childItems.append(static_cast<JobPropertyItem*>(pty));
00366         _model->emitLayoutChanged();
00367     }
00368 }
00369 
00375 void JobPropertyItemNode::onChildChanged(const QString & str)
00376 {
00377     Q_UNUSED(str);
00378     update();
00379     emit itemChanged();
00380 }
00381 
00385 void JobPropertyItemNode::update()
00386 {
00387     QStringList l;
00388     QString s;
00389     l.append(itemData[1].toString());
00390     s = dynamic_cast<JobPropertyItemString*>(childItems.at(0))->toString();
00391     if (!s.isEmpty())
00392         l.append("ppn="+s);
00393     for (int i = 1; i < childItems.size(); ++i)
00394     {
00395         l.append(dynamic_cast<JobPropertyItemNodeProperty*>(childItems.at(i))->toString());
00396     }
00397     _node = l.join(QString(":"));
00398 }
00399 
00405 bool JobPropertyItemNode::deleteChild(int row)
00406 {
00407     if(row >=0 && row<childItems.size())
00408     {
00409         delete childItems.takeAt(row);
00410         update();
00411         _node = this->toString();
00412         emit itemChanged();
00413         return true;
00414     }
00415     return false;
00416 }
00417 
00418 
00419 // ---------------------- Node Property -------------------- //
00426 JobPropertyItemNodeProperty::JobPropertyItemNodeProperty(JobPropertyModel * model,
00427                                                          JobPropertyItemNode *parent)
00428 :JobPropertyItem(model)
00429 {
00430     _name = tr("Property");
00431     parentItem = parent;
00432     QVector<QVariant> data;
00433     data << _name << "";
00434     itemData = data;
00435     setHaveContextMenu(true);
00436 }
00437 
00441 JobPropertyItemNodeProperty::~JobPropertyItemNodeProperty()
00442 {
00443     qDeleteAll(childItems);
00444 }
00445 
00452 bool JobPropertyItemNodeProperty::setData(int column, const QVariant &value)
00453 {
00454     if (column < 1 || column >= itemData.size())
00455         return false;
00456 
00457     itemData[column] = value;
00458     return true;
00459 }
00460 
00468 QWidget *JobPropertyItemNodeProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const
00469 {
00470     QLineEdit * strEditor = new QLineEdit(parent);
00471 
00472     QObject::connect(strEditor, SIGNAL(textChanged(QString)), target, receiver);
00473     return strEditor;
00474 }
00475 
00481 void JobPropertyItemNodeProperty::updateEditorContents(QWidget *editor)
00482 {
00483     QLineEdit * lineEditor = qobject_cast<QLineEdit *>(editor);
00484     QString value = itemData[1].toString();
00485     lineEditor->setText(value);
00486 }
00487 
00493 void JobPropertyItemNodeProperty::updateValue(QWidget *editor)
00494 {
00495     QLineEdit * lineEditor = qobject_cast<QLineEdit *>(editor);
00496     QString newValue = lineEditor->text();
00497     if (newValue != itemData[1])
00498     {
00499         itemData[1] = newValue;
00500         emit itemChanged(newValue);
00501         emit jobPropertyChanged(itemData);
00502     }
00503 }
00504 
00510 void JobPropertyItemNodeProperty::set(const QString & str)
00511 {
00512     QString curvalue = itemData[1].toString();
00513     if (curvalue != str)
00514         itemData[1] = str;
00515 }
00516 
00520 QString JobPropertyItemNodeProperty::toString()
00521 {
00522     return itemData[1].toString();
00523 }
00524 
00531 void JobPropertyItemNodeProperty::createCustomContextMenu(QWidget *parent, const QPoint & pos)
00532 {
00533     QMenu menu(parent);
00534     QAction *deleteNode = menu.addAction(tr("&Delete this property"));
00535     deleteNode->setEnabled(true);
00536     const QAction *result = menu.exec(parent->mapToGlobal(pos));
00537     if (result == deleteNode)
00538         _model->removeRow(childNumber(), parentItem->getSelfIndex());
00539 }

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