#include <QSignalMapper>
#include <QSortFilterProxyModel>
#include <QStandardItemModel>
+#include <QTableView>
#include <QTextEdit>
MCustomerListDialog::MCustomerListDialog(QWidget*par,bool isselect,qint64 presel)
/****************************************************************/
+static const int ContactIdRole = Qt::UserRole;
+static const int ContactTypeIdRole = Qt::UserRole+1;
+
MCustomerDialog::MCustomerDialog(MOCustomer c,QWidget*par)
:QDialog(par),m_cust(c)
{
connect(p,SIGNAL(clicked()),m_addr,SLOT(createAddress()));
tab->addTab(w=new QWidget,tr("Contact Information"));
- w->setLayout(gl=new QGridLayout);
- gl->addWidget(new QLabel(tr("Contact Information:")),++lc,0);
- gl->addWidget(new QTextEdit,lc,1);
- //FIXME
-// m_cont->setPlainText(m_cust.contact());
-
+ w->setLayout(vl2=new QVBoxLayout);
+ vl2->addWidget(m_contact=new QTableView,10);
+ m_contact->setModel(m_contactmodel=new QStandardItemModel(this));
+ m_contact->setItemDelegate(new MContactTableDelegate(this));
+ vl2->addLayout(hl=new QHBoxLayout,0);
+ hl->addStretch(10);
+ hl->addWidget(p=new QPushButton(tr("Add")),0);
+ connect(p,SIGNAL(clicked()),this,SLOT(addContact()));
+ hl->addWidget(p=new QPushButton(tr("Remove")),0);
+ connect(p,SIGNAL(clicked()),this,SLOT(removeContact()));
+ QList<MOContact>cts=m_cust.contacts();
+ m_contactmodel->insertColumns(0,2);
+ m_contactmodel->insertRows(0,cts.size());
+ m_contactmodel->setHorizontalHeaderLabels(QStringList()<<tr("Type","table: contact type")<<tr("Contact","table: contact info"));
+ for(int i=0;i<cts.size();i++){
+ m_contactmodel->setData(m_contactmodel->index(i,0),
+ cts[i].contacttype().value().contacttype().value());
+ m_contactmodel->setData(m_contactmodel->index(i,0),
+ cts[i].contactid().value(), ContactIdRole);
+ m_contactmodel->setData(m_contactmodel->index(i,0),
+ cts[i].contacttypeid().value(), ContactTypeIdRole);
+ m_contactmodel->setData(m_contactmodel->index(i,1),
+ cts[i].contact().value());
+ }
+ m_contact->resizeColumnsToContents();
vl->addLayout(hl=new QHBoxLayout,0);
hl->addStretch(10);
setSizeGripEnabled(true);
}
+void MCustomerDialog::addContact()
+{
+ //create new row
+ int rn=m_contactmodel->rowCount();
+ m_contactmodel->insertRows(rn,1);
+ //set basic data (guaranteed invalid IDs)
+ QModelIndex idx=m_contactmodel->index(rn,0);
+ m_contactmodel->setData(idx,-1,ContactIdRole);
+ m_contactmodel->setData(idx,-1,ContactTypeIdRole);
+ //scroll to it, edit it
+ m_contact->scrollTo(idx);
+ m_contact->edit(idx);
+}
+
+void MCustomerDialog::removeContact()
+{
+ QModelIndex idx=m_contact->currentIndex();
+ if(!idx.isValid())return;
+ m_contactmodel->removeRows(idx.row(),1);
+}
+
MOCustomer MCustomerDialog::getCustomer()
{
//copy data from input fields
m_cust.setfirstname(m_fname->text());
m_cust.settitle(m_title->text());
m_cust.setaddresses(m_addr->addressList());
- //m_cust.setcontact(m_cont->toPlainText());
m_cust.setcomments(m_comm->toPlainText());
+ QList<MOContact>clst;
+ for(int i=0;i<m_contactmodel->rowCount();i++){
+ MOContact c;
+ QModelIndex idx0=m_contactmodel->index(i,0);
+ c.setcontactid(m_contactmodel->data(idx0,ContactIdRole).toInt());
+ c.setcontacttypeid(m_contactmodel->data(idx0,ContactTypeIdRole).toInt());
+ c.setcustomerid(m_cust.customerid());
+ c.setcontact(m_contactmodel->data(m_contactmodel->index(i,1)).toString());
+ clst<<c;
+ }
+ m_cust.setcontacts(clst);
return m_cust;
}
m_cust=cc.getcustomer();
}
}
+/********************************************************************************/
+
+MContactTableDelegate::MContactTableDelegate(QObject*p)
+ :QItemDelegate(p)
+{
+}
+
+QWidget *MContactTableDelegate::createEditor(QWidget *parent,
+ const QStyleOptionViewItem &/* option */,
+ const QModelIndex & index ) const
+{
+ if(index.column()==0){//contact type: dropdown
+ QComboBox *editor = new QComboBox(parent);
+ editor->installEventFilter(const_cast<MContactTableDelegate*>(this));
+ return editor;
+ }
+ if(index.column()==1){//contact string: normal delegate, line edit
+ QLineEdit*editor=new QLineEdit(parent);
+ editor->installEventFilter(const_cast<MContactTableDelegate*>(this));
+ return editor;
+ }
+
+ return 0;
+}
+
+void MContactTableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
+{
+ int c=index.column();
+ if(c==0){//type
+ if(m_typelist.size()==0)m_typelist=MTGetAllContactTypes::query().gettypes();
+ QComboBox*box=(QComboBox*)editor;
+ int curidx=0,cur=index.model()->data(index,ContactTypeIdRole).toInt();
+ for(int i=0;i<m_typelist.size();i++){
+ box->addItem(m_typelist[i].contacttype(),m_typelist[i].contacttypeid().value());
+ if(m_typelist[i].contacttypeid().value()==cur)
+ curidx=i;
+ }
+ if(req->hasRight(req->RCreateContactType))
+ box->addItem(tr("(New Contact Type)"),-1);
+ }
+ if(c==1){//content
+ ((QLineEdit*)editor)->setText(index.model()->data(index).toString());
+ }
+}
+
+void MContactTableDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
+ const QModelIndex &index) const
+{
+ int c=index.column();
+ if(c==0){//type
+ //get content of box
+ QComboBox*box=(QComboBox*)editor;
+ QString tn=box->itemText(box->currentIndex());
+ int tid=box->itemData(box->currentIndex()).toInt();
+ if(tid<0){//create new one
+ QDialog d(editor->window());
+ d.setWindowTitle(tr("Create new Contact Type"));
+ QFormLayout*fl;
+ QVBoxLayout*vl;
+ QHBoxLayout*hl;
+ QLineEdit*nm,*up;
+ QPushButton*p;
+ d.setLayout(vl=new QVBoxLayout);
+ vl->addLayout(fl=new QFormLayout);
+ fl->addRow(tr("Contact Type Name:"),nm=new QLineEdit);
+ fl->addRow(tr("Contact Type URI Prefix:"),up=new QLineEdit);
+ vl->addStretch(1);
+ vl->addLayout(hl=new QHBoxLayout);
+ hl->addWidget(p=new QPushButton(tr("Ok")));
+ connect(p,SIGNAL(clicked()),&d,SLOT(accept()));
+ hl->addWidget(p=new QPushButton(tr("Cancel")));
+ connect(p,SIGNAL(clicked()),&d,SLOT(reject()));
+ if(d.exec()!=QDialog::Accepted)return;
+ MTCreateContactType ct=MTCreateContactType::query(nm->text(),up->text());
+ if(ct.hasError()){
+ QMessageBox::warning(editor->window(),tr("Warning"),tr("Error while creating contact type: %1").arg(ct.errorString()));
+ return;
+ }
+ //get data
+ MOContactType cty=ct.getcontacttype();
+ tn=cty.contacttype();
+ tid=cty.contacttypeid();
+ //clear cache, so it is retrieved again next time
+ m_typelist.clear();
+ }
+ //set new contact type
+ model->setData(index,tn);
+ model->setData(index,tid,ContactTypeIdRole);
+ }
+ if(c==1){//content
+ model->setData(index,((QLineEdit*)editor)->text());
+ }
+}
/*****************************************************************************/