vector<QTreeWidgetItem *> DatabaseImportForm::updateObjectsTree(DatabaseImportHelper &import_helper, QTreeWidget *tree_wgt, vector<ObjectType> types, bool checkable_items, bool disable_empty_grps, QTreeWidgetItem *root, const QString &schema, const QString &table) { vector<QTreeWidgetItem *> items_vect; if(tree_wgt) { QTreeWidgetItem *group=nullptr, *item=nullptr; QFont grp_fnt=tree_wgt->font(); attribs_map extra_attribs={{ParsersAttributes::FILTER_TABLE_TYPES, ParsersAttributes::_TRUE_}}; QString tooltip=QString("OID: %1"), name, label; bool child_checked=false; vector<attribs_map> objects_vect; map<ObjectType, QTreeWidgetItem *> gen_groups; ObjectType obj_type; QList<QTreeWidgetItem*> groups_list; unsigned oid=0; int start=-1, end=-1; grp_fnt.setItalic(true); tree_wgt->blockSignals(true); tree_wgt->setUpdatesEnabled(false); tree_wgt->setSortingEnabled(false); try { for(ObjectType grp_type : types) { //Create a group item for the current type group=new QTreeWidgetItem(root); group->setIcon(0, QPixmap(PgModelerUiNS::getIconPath(BaseObject::getSchemaName(grp_type) + QString("_grp")))); group->setFont(0, grp_fnt); //Group items does contains a zero valued id to indicate that is not a valide object group->setData(OBJECT_ID, Qt::UserRole, 0); group->setData(OBJECT_TYPE, Qt::UserRole, grp_type); group->setData(OBJECT_COUNT, Qt::UserRole, 0); group->setData(OBJECT_SCHEMA, Qt::UserRole, schema); group->setData(OBJECT_TABLE, Qt::UserRole, table); gen_groups[grp_type]=group; groups_list.push_back(group); } objects_vect=import_helper.getObjects(types, schema, table, extra_attribs); for(attribs_map &attribs : objects_vect) { obj_type=static_cast<ObjectType>(attribs[ParsersAttributes::OBJECT_TYPE].toUInt()); group=gen_groups[obj_type]; group->setData(OBJECT_COUNT, Qt::UserRole, group->data(OBJECT_COUNT, Qt::UserRole).toUInt() + 1); //Creates individual items for each object of the current type oid=attribs[ParsersAttributes::OID].toUInt(); attribs[ParsersAttributes::NAME].remove(QRegExp(QString("( )(without)( time zone)"))); label=name=attribs[ParsersAttributes::NAME]; //Removing the trailing type string from op. families or op. classes names if(obj_type==OBJ_OPFAMILY || obj_type==OBJ_OPCLASS) { start=name.indexOf(QChar('[')); end=name.lastIndexOf(QChar(']')); name.remove(start, (end-start)+1); name=name.trimmed(); } item=new QTreeWidgetItem(group); item->setIcon(0, QPixmap(PgModelerUiNS::getIconPath(obj_type))); item->setText(0, label); item->setText(OBJECT_ID, attribs[ParsersAttributes::OID]); item->setData(OBJECT_NAME, Qt::UserRole, name); if(checkable_items) { if((oid > import_helper.getLastSystemOID()) || (obj_type==OBJ_SCHEMA && name==QString("public")) || (obj_type==OBJ_COLUMN && root && root->data(0, Qt::UserRole).toUInt() > import_helper.getLastSystemOID())) { item->setCheckState(0, Qt::Checked); child_checked=true; } else item->setCheckState(0, Qt::Unchecked); //Disabling items that refers to PostgreSQL's built-in data types if(obj_type==OBJ_TYPE && oid <= import_helper.getLastSystemOID()) { item->setDisabled(true); item->setToolTip(0, trUtf8("This is a PostgreSQL built-in data type and cannot be imported.")); } //Disabling items that refers to pgModeler's built-in system objects else if((obj_type==OBJ_TABLESPACE && (name==QString("pg_default") || name==QString("pg_global"))) || (obj_type==OBJ_ROLE && (name==QString("postgres"))) || (obj_type==OBJ_SCHEMA && (name==QString("pg_catalog") || name==QString("public"))) || (obj_type==OBJ_LANGUAGE && (name==~LanguageType(LanguageType::c) || name==~LanguageType(LanguageType::sql) || name==~LanguageType(LanguageType::plpgsql)))) { item->setFont(0, grp_fnt); item->setForeground(0, BaseObjectView::getFontStyle(ParsersAttributes::PROT_COLUMN).foreground()); item->setToolTip(0, trUtf8("This is a pgModeler's built-in object. It will be ignored if checked by user.")); } } //Stores the object's OID as the first data of the item item->setData(OBJECT_ID, Qt::UserRole, oid); if(!item->toolTip(0).isEmpty()) item->setToolTip(0,item->toolTip(0) + QString("\n") + tooltip.arg(oid)); else item->setToolTip(0,tooltip.arg(oid)); //Stores the object's type as the second data of the item item->setData(OBJECT_TYPE, Qt::UserRole, obj_type); //Stores the schema and the table's name of the object item->setData(OBJECT_SCHEMA, Qt::UserRole, schema); item->setData(OBJECT_TABLE, Qt::UserRole, table); if(obj_type==OBJ_SCHEMA || obj_type == OBJ_TABLE || obj_type == OBJ_VIEW) items_vect.push_back(item); } //Updating the object count in each group for(ObjectType grp_type : types) { group=gen_groups[grp_type]; group->setDisabled(disable_empty_grps && group->data(OBJECT_COUNT, Qt::UserRole).toUInt() == 0); group->setText(0, BaseObject::getTypeName(grp_type) + QString(" (%1)").arg(group->data(OBJECT_COUNT, Qt::UserRole).toUInt())); if(checkable_items) { if(!group->isDisabled() && child_checked) group->setCheckState(0, Qt::Checked); else group->setCheckState(0, Qt::Unchecked); } } tree_wgt->addTopLevelItems(groups_list); tree_wgt->setSortingEnabled(true); tree_wgt->setUpdatesEnabled(true); tree_wgt->blockSignals(false); } catch(Exception &e) { throw Exception(e.getErrorMessage(), e.getErrorType(),__PRETTY_FUNCTION__,__FILE__,__LINE__, &e); } } return(items_vect); }
vector<QTreeWidgetItem *> DatabaseImportForm::updateObjectsTree(DatabaseImportHelper &import_helper, QTreeWidget *tree_wgt, vector<ObjectType> types, bool checkable_items, bool disable_empty_grps, QTreeWidgetItem *root, const QString &schema, const QString &table) { vector<QTreeWidgetItem *> items_vect; if(tree_wgt) { QTreeWidgetItem *group=nullptr, *item=nullptr; QFont grp_fnt=tree_wgt->font(); attribs_map objects, extra_attribs={{ParsersAttributes::FILTER_TABLE_TYPES, "1"}}; attribs_map::iterator itr; QString tooltip="OID: %1", msg=trUtf8("Retrieving `%1'..."); bool child_checked=false; int progress=0; grp_fnt.setItalic(true); tree_wgt->blockSignals(true); try { for(unsigned i=0; i < types.size(); i++) { if(task_prog_wgt->isVisible()) { progress=task_prog_wgt->progress_pb->value() + (i/static_cast<float>(types.size())); task_prog_wgt->updateProgress(progress, msg.arg(BaseObject::getTypeName(types[i])), types[i]); } //Retrieve the objects of the current type from the database objects=import_helper.getObjects(types[i], schema, table, extra_attribs); //Create a group item for the current type group=new QTreeWidgetItem(root); group->setIcon(0, QPixmap(QString(":/icones/icones/") + BaseObject::getSchemaName(types[i]) + QString("_grp.png"))); group->setText(0, BaseObject::getTypeName(types[i]) + QString(" (%1)").arg(objects.size())); group->setFont(0, grp_fnt); //Group items does contains a zero valued id to indicate that is not a valide object group->setData(OBJECT_ID, Qt::UserRole, 0); group->setData(OBJECT_TYPE, Qt::UserRole, types[i]); group->setData(OBJECT_COUNT, Qt::UserRole, QVariant::fromValue<unsigned>(objects.size())); group->setData(OBJECT_SCHEMA, Qt::UserRole, schema); group->setData(OBJECT_TABLE, Qt::UserRole, table); //Creates individual items for each object of the current type itr=objects.begin(); while(itr!=objects.end()) { item=new QTreeWidgetItem(group); item->setIcon(0, QPixmap(QString(":/icones/icones/") + BaseObject::getSchemaName(types[i]) + QString(".png"))); item->setText(0, itr->second); item->setText(1, itr->first); if(checkable_items) { if((itr->first.toUInt() > import_helper.getLastSystemOID()) || (types[i]==OBJ_SCHEMA && itr->second=="public") || (types[i]==OBJ_COLUMN && root && root->data(0, Qt::UserRole).toUInt() > import_helper.getLastSystemOID())) { item->setCheckState(0, Qt::Checked); child_checked=true; } else item->setCheckState(0, Qt::Unchecked); //Disabling items that refers to PostgreSQL's built-in data types if(types[i]==OBJ_TYPE && itr->first.toUInt() <= import_helper.getLastSystemOID()) { item->setDisabled(true); item->setToolTip(0, trUtf8("This is a PostgreSQL built-in data type and cannot be imported.")); } //Disabling items that refers to pgModeler's built-in system objects else if((types[i]==OBJ_TABLESPACE && (itr->second=="pg_default" || itr->second=="pg_global")) || (types[i]==OBJ_ROLE && (itr->second=="postgres")) || (types[i]==OBJ_SCHEMA && (itr->second=="pg_catalog" || itr->second=="public")) || (types[i]==OBJ_LANGUAGE && (itr->second==~LanguageType(LanguageType::c) || itr->second==~LanguageType(LanguageType::sql) || itr->second==~LanguageType(LanguageType::plpgsql)))) { item->setFont(0, grp_fnt); item->setForeground(0, BaseObjectView::getFontStyle(ParsersAttributes::PROT_COLUMN).foreground()); item->setToolTip(0, trUtf8("This is a pgModeler's built-in object. It will be ignored if checked by user.")); } } //Stores the object's OID as the first data of the item item->setData(OBJECT_ID, Qt::UserRole, itr->first.toUInt()); if(!item->toolTip(0).isEmpty()) item->setToolTip(0,item->toolTip(0) + "\n" + tooltip.arg(itr->first)); else item->setToolTip(0,tooltip.arg(itr->first)); //Stores the object's type as the second data of the item item->setData(OBJECT_TYPE, Qt::UserRole, types[i]); //Stores the schema and the table's name of the object item->setData(OBJECT_SCHEMA, Qt::UserRole, schema); item->setData(OBJECT_TABLE, Qt::UserRole, table); if(types[i]==OBJ_SCHEMA || types[i]==OBJ_TABLE) items_vect.push_back(item); itr++; } group->setDisabled(disable_empty_grps && objects.empty()); if(checkable_items) { if(!group->isDisabled() && child_checked) group->setCheckState(0, Qt::Checked); else group->setCheckState(0, Qt::Unchecked); } tree_wgt->addTopLevelItem(group); } tree_wgt->blockSignals(false); } catch(Exception &e) { throw Exception(e.getErrorMessage(), e.getErrorType(),__PRETTY_FUNCTION__,__FILE__,__LINE__, &e); } } return(items_vect); }