#include "QtFormulaDialogfactory.h" #include #include #include #include #include #include #include #include #include //#include "BaseObject.h" //#include "ObjectID.h" //#include "DialogFactory.h" ///*#include "geo"*/ //using namespace pai::datamodel; static inline void setupTreeViewEditorMargin(QLayout *lt) { enum { DecorationMargin = 4 }; if (QApplication::layoutDirection() == Qt::LeftToRight) lt->setContentsMargins(DecorationMargin, 0, 0, 0); else lt->setContentsMargins(0, 0, DecorationMargin, 0); } template static Value getData(const QMap &propertyMap, Value PrivateData::*data, const QtProperty *property, const Value &defaultValue = Value()) { typedef QMap PropertyToData; typedef typename PropertyToData::const_iterator PropertyToDataConstIterator; const PropertyToDataConstIterator it = propertyMap.constFind(property); if (it == propertyMap.constEnd()) return defaultValue; return it.value().*data; } template static Value getValue(const QMap &propertyMap, const QtProperty *property, const Value &defaultValue = Value()) { return getData(propertyMap, &PrivateData::val, property, defaultValue); } CQtFormulaDialogFactory::CQtFormulaDialogFactory(QObject *parent) : QtAbstractEditorFactory(parent), d_ptr(new CQtFormulaDialogFactoryPrivate()) { d_ptr->q_ptr = this; } /*! Destroys this factory, and all the widgets it has created. */ CQtFormulaDialogFactory::~CQtFormulaDialogFactory() { qDeleteAll(d_ptr->m_editorToProperty.keys()); } /*! \internal Reimplemented from the QtAbstractEditorFactory class. */ void CQtFormulaDialogFactory::connectPropertyManager(CQtFormulaDialogPropertyManager *manager) { connect(manager, SIGNAL(valueChanged(QtProperty*, QVariant)), this, SLOT(slotPropertyChanged(QtProperty*, QVariant))); } /*! \internal Reimplemented from the QtAbstractEditorFactory class. */ QWidget *CQtFormulaDialogFactory::createEditor(CQtFormulaDialogPropertyManager *manager, QtProperty *property, QWidget *parent) { CQtFormulaDialogEditWidget *editor = d_ptr->createEditor(property, parent); connect(editor,SIGNAL(tableChanged(const QString&)),manager,SIGNAL(tableChanged(const QString&)),Qt::UniqueConnection); editor->tableDataList=manager->tableDataList; editor->fieldList=manager->fieldDataList; editor->tableModelData=manager->tableModelData; connect(manager,SIGNAL(fieldListChanged(const QStringList&)),editor,SLOT(setComboxFieldDataList(const QStringList&)),Qt::UniqueConnection); connect(manager,SIGNAL(tableListChanged(const QStringList&)),editor,SLOT(setComboxTableDataList(const QStringList&)),Qt::UniqueConnection); connect(manager,SIGNAL(tableModelDataChanged(const ViewData&)),editor,SLOT(setTableModeData(const ViewData&)),Qt::UniqueConnection); //connect(manager,SIGNAL(signalCallFormulaDialogAction()),editor,SLOT(buttonClicked())); //editor->defaultDir=manager->defaultDir; //editor->filter=manager->filter; editor->setValue(manager->value(property)); connect(editor, SIGNAL(valueChanged(QString)), this, SLOT(slotSetValue(QString)),Qt::UniqueConnection); connect(editor, SIGNAL(destroyed(QObject*)), this, SLOT(slotEditorDestroyed(QObject*)),Qt::UniqueConnection); return editor; } /*! \internal Reimplemented from the QtAbstractEditorFactory class. */ void CQtFormulaDialogFactory::disconnectPropertyManager(CQtFormulaDialogPropertyManager *manager) { disconnect(manager, SIGNAL(valueChanged(QtProperty*,QString)), this, SLOT(slotPropertyChanged(QtProperty*,QString))); } void CQtFormulaDialogFactoryPrivate::slotPropertyChanged(QtProperty *property, QString value) { const PropertyToEditorListMap::iterator it = m_createdEditors.find(property); if (it == m_createdEditors.end()) return; QListIterator itEditor(it.value()); while (itEditor.hasNext()) itEditor.next()->setValue(value); } void CQtFormulaDialogFactoryPrivate::slotSetValue(QString value) { QObject *object = q_ptr->sender(); const QMap::ConstIterator ecend = m_editorToProperty.constEnd(); for (QMap::ConstIterator itEditor = m_editorToProperty.constBegin(); itEditor != ecend; ++itEditor) if (itEditor.key() == object) { QtProperty *property = itEditor.value(); CQtFormulaDialogPropertyManager *manager = q_ptr->propertyManager(property); if (!manager) return; manager->setValue(property, value); return; } } CQtFormulaDialogPropertyManager::CQtFormulaDialogPropertyManager(QObject *parent) : QtAbstractPropertyManager(parent), d_ptr(new CQtFormulaDialogPropertyManagerPrivate) { d_ptr->q_ptr = this; } /*! Destroys this manager, and all the properties it has created. */ CQtFormulaDialogPropertyManager::~CQtFormulaDialogPropertyManager() { clear(); } /*! Returns the given \a property's value which is an index in the list returned by enumNames() If the given property is not managed by this manager, this function returns -1. \sa enumNames(), setValue() */ QString CQtFormulaDialogPropertyManager::value(const QtProperty *property) const { return d_ptr->m_values.value(property, QString()); } /*! Returns the given \a property's list of enum names. \sa value(), setEnumNames() */ /*! \reimp */ QString CQtFormulaDialogPropertyManager::valueText(const QtProperty *property) const { const CQtFormulaDialogPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); if (it == d_ptr->m_values.constEnd()) return QString(); QString avalue=*it; QString strFilePath = avalue;//得到用户输入的文件名 strFilePath.replace("\\","/"); QString file = strFilePath.split("/").takeLast(); //QString filename = file.split(".").takeFirst(); return file; } void CQtFormulaDialogPropertyManager::setFieldDataList(const QStringList &fieldList) { fieldDataList=fieldList; emit fieldListChanged(fieldList); } void CQtFormulaDialogPropertyManager::setTableDataList(const QStringList &tableList) { tableDataList=tableList; emit tableListChanged(tableList); } void CQtFormulaDialogPropertyManager::setTableModelData(const ViewData &modelData) { tableModelData=modelData; emit tableModelDataChanged(modelData); } /*! \fn void QtLineStyleEnumPropertyManager::setValue(QtProperty *property, int value) Sets the value of the given \a property to \a value. The specified \a value must be less than the size of the given \a property's enumNames() list, and larger than (or equal to) 0. \sa value(), valueChanged() */ void CQtFormulaDialogPropertyManager::setValue(QtProperty *property, QString val) { QString strFilePath = val;//得到用户输入的文件名 strFilePath.replace("\\","/"); QString file = strFilePath.split("/").takeLast(); //QString filename = file.split(".").takeFirst(); const CQtFormulaDialogPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property); if (it == d_ptr->m_values.end()) return; *it = file; //m_label->setText(filename); emit propertyChanged(property); emit valueChanged(property, val); } /*! Sets the given \a property's list of enum names to \a enumNames. The \a property's current value is reset to 0 indicating the first item of the list. If the specified \a enumNames list is empty, the \a property's current value is set to -1. \sa enumNames(), enumNamesChanged() */ /*! Sets the given \a property's map of enum values to their icons to \a enumIcons. Each enum value can have associated icon. This association is represented with passed \a enumIcons map. \sa enumNames(), enumNamesChanged() */ /*! \reimp */ void CQtFormulaDialogPropertyManager::initializeProperty(QtProperty *property) { d_ptr->m_values[property] = "可疑气油层"; } /*! \reimp */ void CQtFormulaDialogPropertyManager::uninitializeProperty(QtProperty *property) { d_ptr->m_values.remove(property); } //void CQtFormulaDialogPropertyManager::onGraphicsHeadCellFormula(const QString &formla) //{ // cellFormula=formla; //} //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //CQTtGeoStraEditWidget CQtFormulaDialogEditWidget::CQtFormulaDialogEditWidget(QWidget *parent) : QWidget(parent), m_label(new QLabel), m_button(new QToolButton), m_formula(""), m_tableCombox(new QComboBox), m_fieldCombox(new QComboBox), m_tableView(new QTableWidget), btnAccpted(new QPushButton), btnCancel(new QPushButton), formulaDialog(new QDialog), lblTable(new QLabel), lblField(new QLabel), lblSmall(new QLabel), m_smallBox(new QSpinBox), m_listWidget(new QListWidget), btnAddCompute(new QPushButton), btnSubtractCompute(new QPushButton), btnMultiplyCompute(new QPushButton), btnDivideCompute(new QPushButton), btnAppend(new QPushButton), btnUndo(new QPushButton), editline(new QLineEdit()), Noline(new QLineEdit()), NoLabel(new QLabel), m_lable1(new QLabel) { m_computeSymbol<<"+"<<"-"<<"*"<<"/"; QHBoxLayout *lt = new QHBoxLayout(this); setupTreeViewEditorMargin(lt); lt->setSpacing(0); lt->addWidget(m_label); lt->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Ignored)); m_button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Ignored); m_button->setFixedWidth(20); setFocusProxy(m_button); setFocusPolicy(m_button->focusPolicy()); m_button->setText(tr("...")); m_button->installEventFilter(this); lt->addWidget(m_button); m_label->setText(m_formula); m_tableView->setMouseTracking(true); formulaDialog->setLayout(NULL); initConnect(); } void CQtFormulaDialogEditWidget::initConnect() { connect(m_tableCombox,SIGNAL(currentIndexChanged(const QString &)),this,SIGNAL(tableChanged(const QString &)),Qt::UniqueConnection); connect(m_button, SIGNAL(clicked()), this, SLOT(buttonClicked())); connect(btnAccpted,SIGNAL(clicked()),this,SLOT(onAcceptOk()),Qt::UniqueConnection); connect(btnCancel,SIGNAL(clicked()),this,SLOT(onCancel()),Qt::UniqueConnection); connect(btnAddCompute,SIGNAL(clicked()),this,SLOT(onAddCompute()),Qt::UniqueConnection); connect(btnSubtractCompute,SIGNAL(clicked()),this,SLOT(onSubtractCompute()),Qt::UniqueConnection); connect(btnMultiplyCompute,SIGNAL(clicked()),this,SLOT(onMultipyCompute()),Qt::UniqueConnection); connect(btnDivideCompute,SIGNAL(clicked()),this,SLOT(onDivideCompute()),Qt::UniqueConnection); connect(btnAppend,SIGNAL(clicked()),this,SLOT(onAppend()),Qt::UniqueConnection); connect(btnUndo,SIGNAL(clicked()),this,SLOT(onUndo()),Qt::UniqueConnection); connect(m_tableView,SIGNAL(cellPressed(int, int)),this,SLOT(onCellPressed(int,int))); } void CQtFormulaDialogEditWidget::onCellPressed(int row,int column) { int rows=m_tableView->rowCount(); int cols=m_tableView->columnCount(); if (row >=0 && column < cols) { m_fieldCombox->setCurrentIndex(column); } QTableWidgetItem *item=m_tableView->item(row,column); QString cellText,cellSam; if (item) { cellText=item->text(); } if (cellText.isEmpty()) { return; } bool isOk=false; float value=cellText.toFloat(&isOk); int leftMid=0,canSize=0,orgSize=0; if (isOk) { leftMid= cellText.lastIndexOf("."); if (leftMid >=0) { leftMid=cellText.size()-(leftMid+1); cellSam=cellText.right(leftMid); } } m_smallBox->setValue(cellSam.size()); } CQtFormulaDialogEditWidget::~CQtFormulaDialogEditWidget() { if(formulaDialog) { delete formulaDialog; formulaDialog=NULL; } } void CQtFormulaDialogEditWidget::setComboxFieldDataList() { m_fieldCombox->clear(); foreach(const QString &itemText,fieldList) { m_fieldCombox->addItem(itemText); } } void CQtFormulaDialogEditWidget::setComboxTableDataList() { m_tableCombox->clear(); foreach(const QString &itemText,tableDataList) { m_tableCombox->addItem(itemText); } } void CQtFormulaDialogEditWidget::setComboxFieldDataList(const QStringList& list) { fieldList=list; setComboxFieldDataList(); } void CQtFormulaDialogEditWidget::setComboxTableDataList(const QStringList& list) { tableDataList=list; setComboxTableDataList(); } void CQtFormulaDialogEditWidget::setTableModeData(const ViewData & tableData) { tableModelData=tableData; bindTableData(); } void CQtFormulaDialogEditWidget::setValue(const QString &c) { if (m_formula != c) { m_formula = c; m_label->setText(c); } } void CQtFormulaDialogEditWidget::showFormulaDlg() { setComboxFieldDataList(); initUI(); //bindTableData(); QString formulaStr("=TABLE(%1:%2:%3.%4)"); QString tableName,fieldName,mapFieldKey,mapFieldValue; int tbCount=0,fieldCount=0; bindCellFormula(); if(formulaDialog->exec() == QDialog::Accepted) { tbCount=m_tableCombox->count(); fieldCount=m_fieldCombox->count(); if(tbCount == 0 || fieldCount ==0) { return; } formulaStr=combinationFormual(); setValue(formulaStr); emit valueChanged(formulaStr); } } void CQtFormulaDialogEditWidget::initUI() { bool applayLayout=NULL == formulaDialog->layout(); if(applayLayout) { setComboxTableDataList(); //formulaDialog->setWindowFlags(Qt::FramelessWindowHint); m_tableView->setParent(formulaDialog); m_tableCombox->setParent(formulaDialog); m_fieldCombox->setParent(formulaDialog); btnAccpted->setParent(formulaDialog); btnCancel->setParent(formulaDialog); lblTable->setParent(formulaDialog); editline->setParent(formulaDialog); Noline->setParent(formulaDialog); m_lable1->setParent(formulaDialog); lblField->setParent(formulaDialog); lblSmall->setParent(formulaDialog); m_smallBox->setParent(formulaDialog); m_listWidget->setParent(formulaDialog); btnAddCompute->setParent(formulaDialog); btnSubtractCompute->setParent(formulaDialog); btnMultiplyCompute->setParent(formulaDialog); btnDivideCompute->setParent(formulaDialog); btnAppend->setParent(formulaDialog); btnUndo->setParent(formulaDialog); m_smallBox->setMinimum(0); m_smallBox->setMaximum(6); m_smallBox->setValue(0); formulaDialog->resize(800,500); m_lable1->setText("输入附加信息"); btnAccpted->setText("确定"); btnCancel->setText("取消"); lblTable->setText("选择数据表"); lblField->setText("选择字段"); lblSmall->setText("小数位数"); btnAddCompute->setText("+"); btnSubtractCompute->setText("-"); btnMultiplyCompute->setText("*"); btnDivideCompute->setText("/"); btnAppend->setText("添加"); btnUndo->setText("回退"); QHBoxLayout *hMainboxLayout=new QHBoxLayout(formulaDialog); QVBoxLayout *vRightBoxLayout=new QVBoxLayout(formulaDialog); QVBoxLayout *vboxLayout=new QVBoxLayout(formulaDialog); QHBoxLayout *hboxLayout=new QHBoxLayout(formulaDialog); QHBoxLayout *bottomhboxLayout=new QHBoxLayout(formulaDialog); hboxLayout->addWidget(lblTable); hboxLayout->addWidget(m_tableCombox); hboxLayout->addWidget(lblField); hboxLayout->addWidget(m_fieldCombox); hboxLayout->addWidget(lblSmall); hboxLayout->addWidget(m_smallBox); hboxLayout->addStretch(); vboxLayout->addItem(hboxLayout); vboxLayout->addWidget(m_listWidget,1); // vboxLayout->addWidget(m_tableView,2); bottomhboxLayout->addStretch(); bottomhboxLayout->addWidget(m_lable1); bottomhboxLayout->addWidget(editline); bottomhboxLayout->addWidget(btnAccpted); bottomhboxLayout->addWidget(btnCancel); vboxLayout->addStretch(); vboxLayout->addItem(bottomhboxLayout); vRightBoxLayout->addWidget(btnAddCompute); vRightBoxLayout->addWidget(btnSubtractCompute); vRightBoxLayout->addWidget(btnMultiplyCompute); vRightBoxLayout->addWidget(btnDivideCompute); vRightBoxLayout->addStretch(); vRightBoxLayout->addWidget(btnAppend); vRightBoxLayout->addWidget(btnUndo); vRightBoxLayout->addStretch(); NoLabel->setText("特定行"); vRightBoxLayout->addWidget(NoLabel); vRightBoxLayout->addWidget(Noline); vRightBoxLayout->addStretch(); hMainboxLayout->addItem(vboxLayout); hMainboxLayout->addItem(vRightBoxLayout); formulaDialog->setWindowTitle("选择表及字段"); } } QString CQtFormulaDialogEditWidget::combinationFormual() { QString formual; int rows=m_listWidget->count(); if(rows == 0) { return formual; } QListWidgetItem *item=NULL; QStringList symbolList; for(int i=0;iitem(i); if(item) { symbolList << item->text(); } } formual=symbolList.join(""); return formual; } void CQtFormulaDialogEditWidget::onAcceptOk() { formulaDialog->accept(); } void CQtFormulaDialogEditWidget::onCancel() { formulaDialog->close(); } void CQtFormulaDialogEditWidget::bindTableData() { int cols=0; int rows=m_tableView->rowCount(); QAbstractItemModel *itemModel=m_tableView->model(); if (itemModel) { itemModel->removeRows(0,rows); } // 表字段 cols= fieldList.size(); // 表数据 rows=tableModelData.size(); if(rows == 0) { return; } m_tableView->setRowCount(rows); m_tableView->setColumnCount(cols); m_tableView->setHorizontalHeaderLabels(fieldList); m_tableView->setSelectionMode(QAbstractItemView::SingleSelection); m_tableView->setSelectionBehavior(QAbstractItemView::SelectItems); QHeaderView *headView= m_tableView->horizontalHeader(); headView->setStretchLastSection(true); itemModel=m_tableView->model(); QString text; for(int r=0;rsetData(itemModel->index(r,c),text,Qt::DisplayRole); } } } bindCellFormula(); } void CQtFormulaDialogEditWidget::buttonClicked() { //QFileDialog fileDialog; showFormulaDlg(); } //compute slots void CQtFormulaDialogEditWidget::onAddCompute() { appendComputeSymbol(btnAddCompute); } void CQtFormulaDialogEditWidget::onSubtractCompute() { appendComputeSymbol(btnSubtractCompute); } void CQtFormulaDialogEditWidget::onMultipyCompute() { appendComputeSymbol(btnMultiplyCompute); } void CQtFormulaDialogEditWidget::onDivideCompute() { appendComputeSymbol(btnDivideCompute); } void CQtFormulaDialogEditWidget::appendComputeSymbol(QPushButton *button) { QString symbolStr; bool isAppend=false; if (button) { symbolStr=button->text().trimmed(); isAppend=isAppendValidate(symbolStr); if (isAppend) { m_listWidget->addItem(symbolStr); } } } bool CQtFormulaDialogEditWidget::isAppendValidate(const QString &rowItem) { int rows=m_listWidget->count(); bool isValidate=false; QString symbolStr; if(rows == 0 && !m_computeSymbol.contains(rowItem)) { isValidate=true; }else if(rows > 0) { QListWidgetItem *item= m_listWidget->item(rows-1); symbolStr=item->text().trimmed(); if(m_computeSymbol.contains(rowItem) && !m_computeSymbol.contains(symbolStr)) { isValidate=true; }else if (m_computeSymbol.contains(symbolStr) && !m_computeSymbol.contains(rowItem)) { isValidate=true; } } return isValidate; } // fun void CQtFormulaDialogEditWidget::onAppend() { int tbCount=m_tableCombox->count(); int fieldCount=m_fieldCombox->count(); if(tbCount == 0 || fieldCount ==0) { return; } QString formualTable=buildTableFormulaRow(); bool isAppend=isAppendValidate(formualTable); if(isAppend) { m_listWidget->addItem(formualTable); } } void CQtFormulaDialogEditWidget::onUndo() { int rows=m_listWidget->count(); if(rows == 0) { return; } int index=rows -1; QListWidgetItem* item=m_listWidget->takeItem(index); if (item) { delete item; item=NULL; } //QListWidgetItem *item= m_listWidget->item(rows-1); //m_listWidget->removeItemWidget(item); } QString CQtFormulaDialogEditWidget::buildTableFormulaRow(const QString &computeFlag) { QString formulaStr("=TABLE(%1:%2:%3.%4)"); QString tableName,fieldName; int recordNo=0,reatinNum=m_smallBox->value(); if(computeFlag.isEmpty()) { tableName=m_tableCombox->currentText(); fieldName=m_fieldCombox->currentText(); recordNo=m_tableView->currentRow()+1; if(editline->text()!="") { tableName="QString"; fieldName=editline->text(); recordNo=-1; } if(Noline->text()!="") { formulaStr=formulaStr.arg(tableName).arg(fieldName).arg(Noline->text()).arg(reatinNum); } else formulaStr=formulaStr.arg(tableName).arg(fieldName).arg(recordNo).arg(reatinNum); }else { formulaStr=computeFlag; } return formulaStr; } //QString GetImagePath() //{ // static QString imgpath; // if(imgpath!="") return imgpath; // QString strImagePath; // { // QString strPathTmp = QCoreApplication::applicationDirPath() + QDir::separator(); // strImagePath = QDir::toNativeSeparators( strPathTmp ); // QDir dir; // if( dir.exists( strPathTmp + "image" ) ) // { // return strPathTmp + "image" + QDir::separator(); // }else // { // strImagePath = strImagePath+".."+ "/LogPlus/image/"; // } // } // imgpath=strImagePath; // return imgpath; //} bool CQtFormulaDialogEditWidget::eventFilter(QObject *obj, QEvent *ev) { if (obj == m_button) { switch (ev->type()) { case QEvent::KeyPress: case QEvent::KeyRelease: { // Prevent the QToolButton from handling Enter/Escape meant control the delegate switch (static_cast(ev)->key()) { case Qt::Key_Escape: case Qt::Key_Enter: case Qt::Key_Return: ev->ignore(); return true; default: break; } } break; default: break; } } return QWidget::eventFilter(obj, ev); } void CQtFormulaDialogEditWidget::bindCellFormula() { //QString formulaStr("=TABLE(%1:%2:%3.%4)"); QStringList formulaLst; QString formula; QString computeChar; int sIndex=0,eIndex=0; if (m_formula.isEmpty()) { return; } while(true) { eIndex = m_formula.indexOf(")",sIndex); if (eIndex == -1) { break; } int tSize=m_formula.size(); eIndex++; if (eIndex != m_formula.size()) { computeChar= m_formula.mid(eIndex,1); } formula = m_formula.mid(sIndex,eIndex-sIndex); formulaLst << formula; if (!computeChar.isEmpty()) { formulaLst << computeChar; eIndex++; computeChar=QString(""); } sIndex=eIndex; } // 清空公式列表 int rows=m_listWidget->count(); while(rows > 0) { QListWidgetItem* item=m_listWidget->takeItem(0); if (item) { delete item; item=NULL; } rows=m_listWidget->count(); } // 绑定到listview for (int i=0;i < formulaLst.size(); i++) { formula=formulaLst.value(i); bool isAppend=isAppendValidate(formula); if(isAppend) { m_listWidget->addItem(formula); } } }