#include "sysrolemgt.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "sysrolemgtthread.h" SysRoleMgt::SysRoleMgt(const QString &iModuleNameStr, const QVariantMap iUrlPars, QWidget *iParent) : TopClassAbs(iParent) { this->appendLanguage("sys-role"); this->setLicenseKey("sys_common"); this->initModule(iModuleNameStr, iUrlPars); // 初始化productCategory配置 QVariant prodCfg = config("product_category"); if (prodCfg.type() == QVariant::List || prodCfg.type() == QVariant::StringList) { for (QVariant row : prodCfg.toList()) { mProductCategoryLst.append(row.toString()); } } else if (!prodCfg.toString().isEmpty()) { mProductCategoryLst.append(prodCfg.toString()); } if (mProductCategoryLst.isEmpty()) { mProductCategoryLst.append(APP->productCategory()); } mBodySplitter = new TSplitter(this); this->setCentralWidget(mBodySplitter); mBodyWidget = new QWidget(this); QVBoxLayout *vboxlayout = new QVBoxLayout(mBodyWidget); vboxlayout->setMargin(0); vboxlayout->setSpacing(0); initTableView(); if (QToolBar *toolbar = qobject_cast(uim()->getWidget("MAIN_TOOLBAR"))) { toolbar->setWindowTitle(ttr("ToolBar")); vboxlayout->addWidget(toolbar, 0); mSearchEntry = qobject_cast(uim()->getWidget("MAIN_TOOLBAR/SEARCH_ENTRY")); } if (QMenu *table_popup = qobject_cast(uim()->getWidget("TABLEVIEW_POPUP"))) { mTableView->setContextMenu(table_popup); } vboxlayout->addWidget(mTableView, 1); if (QToolBar *toolbar = qobject_cast(uim()->getWidget("BOTTOM_TOOLBAR"))) { vboxlayout->addWidget(toolbar, 0); mPageTool = qobject_cast(uim()->getWidget("BOTTOM_TOOLBAR/PAGE_TOOL")); } QString detailModuleName = config("detail_module_name").toString(); if (detailModuleName.isEmpty()) { detailModuleName = "sys-role"; } mBodySplitter->addWidget(mBodyWidget); mDetailView = new SysRole(detailModuleName, config(), this); connect(mDetailView, SIGNAL(dataSaved(QVariant)), this, SLOT(onDetailSaved(QVariant))); connect(mDetailView, SIGNAL(windowModifyChanged(bool)), this, SLOT(onDetailChanged())); mBodySplitter->addWidget(mDetailView); if (mSearchEntry != nullptr) { mSearchEntry->setOptionList(TDataParse::headerItem2searchList(mTableView->headerItem())); mSearchEntry->setPlaceholderText(ttr("Search %1")); connect(mSearchEntry, SIGNAL(search(QString,QVariant)), this, SLOT(refresh())); this->setFocusProxy(mSearchEntry); } if (mPageTool != nullptr) { mPageTool->setPageSizeVisible(true); connect(mPageTool, SIGNAL(pageChanged(int,int)), this, SLOT(onPageChanged())); } connect(mTableView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(onSelectionChanged())); if (QAction *openAct = this->getAction("open")) { connect(mTableView, SIGNAL(activated(QModelIndex)), openAct, SLOT(trigger())); } mDetailView->resize(TTHEME_DP(config("detail.perfect_size.width", mDetailView->perfectSize().width()).toInt()), mDetailView->height()); mBodySplitter->setStretchFactor(0, 1); this->restoreSizeState(); this->restoreObjectState(mTableView); refreshActionState(); QTimer::singleShot(0, this, SLOT(refresh())); } SysRoleMgt::~SysRoleMgt() { this->saveSizeState(); this->saveObjectState(mTableView); } void SysRoleMgt::refresh(bool iResetPageBol) { t::loading(this); TSqlSelectorV2 selector = getSqlSelector(iResetPageBol); QVariant data = doThreadWork(new TopClassSqlThread(this), TOPSQLTHREAD_SELECT_ARRAYMAP, QVariant::fromValue(selector)); TDataResponse dataRes(data.toMap()); unloading(); if (dataRes.hasError()) { alertError(ttr("Load data failed!"), dataRes.errText()); } else { fillTableData(dataRes); alertOk(ttr("Data loaded")); } } void SysRoleMgt::deleteItem(const QVariantList &iIdLst) { loading(ttr("Deleting data...")); QVariant data = doThreadWork(new SysRoleMgtThread(this), "DELETE", iIdLst); TDataResponse dataRes(data.toMap()); if (dataRes.hasError()) { alertError(ttr("Delete data failed!"), dataRes.errText()); } else { refresh(true); alertOk(ttr("Data deleted")); } } QVariantList SysRoleMgt::selectedItems() { return mSelectedLst; } TopClassAbs *SysRoleMgt::openItem(int iRoleIdInt) { QString detailModuleName = "sys-role"; if (!config("detail_module_name").toString().isEmpty()) { detailModuleName = config("detail_module_name").toString(); } SysRole *detail = qobject_cast(APP->openModule(detailModuleName)); if (detail != nullptr) { detail->setConfig("product_category", this->config("product_category")); connect(detail, SIGNAL(dataSaved(QVariant)), this, SLOT(onDetailSaved(QVariant))); connect(this, SIGNAL(destroyed()), detail, SLOT(close())); detail->setUid(iRoleIdInt); } return detail; } void SysRoleMgt::newItem() { mBodySplitter->cacheSizes(); mDetailView->createRole(); mDetailView->refreshActionState(); } /*! * \brief 清除无用权限 */ void SysRoleMgt::clearUnusedRgts() { loading(ttr("Clear unused rights...")); QVariant data = doThreadWork(new SysRoleMgtThread(this), "CLEAR"); mDetailView->blockSignals(true); mDetailView->loadRgtNames(); mDetailView->blockSignals(false); unloading(); mDetailView->setDataModified(false); TDataResponse dataRes(data.toMap()); if (dataRes.hasError()) { alertError(ttr("Clear needless rgts failed!"), dataRes.errText()); } else { refresh(true); alertOk(ttr("Clear needless rgts success!")); } } void SysRoleMgt::onPageChanged() { refresh(false); } void SysRoleMgt::onSelectionChanged() { if (mTableView != nullptr) { //因为Action的State函数中用到getSelectedList, 所以TableView选择更改时, //将其保存至mSelectedLst中, 这样可以加快Action状态的刷新速度; mSelectedLst = mTableView->getData("selected_primary_keys").toList(); int curIdInt = 0; if (!mSelectedLst.isEmpty()) { curIdInt = mSelectedLst.value(0).toInt(); } if (mDetailView->uid().toInt() != curIdInt) { //设置详情模块的上次Uid为当前选择的ID, 这样当如果详情模块处于编辑状态时点取消后显示的当前择的条目内容; mDetailView->setLastUid(curIdInt); if (!mDetailView->isDataModified()) { mDetailView->setUid(curIdInt); } } refreshActionState(); } } void SysRoleMgt::onDetailSaved(const QVariant &iUidStr) { this->refresh(false); mTableView->selectRow(iUidStr); } void SysRoleMgt::onDetailChanged() { bool isModified = mDetailView->isDataModified(); mTableView->setEnabled(!isModified); if (isModified) { this->showMaskFrame(mBodyWidget); } else { if (mBodySplitter->sizes().value(0) < 20) { mBodySplitter->setWidgetSize(0, mBodySplitter->getCacheSize(0), true); } this->hideMaskFrame(); } refreshActionState(); } TSqlSelectorV2 SysRoleMgt::getSqlSelector(bool iResetPageBol) { TSqlSelectorV2 selector; selector.setTable("sys_role"); selector.setReturnRowCount(true); selector.fieldRef() << "id"<<"name"<<"sys_name"<<"description"; int pageNumInt = 1; int pageSizeInt = -1; if (mPageTool != nullptr) { if (iResetPageBol) { mPageTool->setCurrentPage(1, true); } pageNumInt = mPageTool->currentPage(); pageSizeInt = mPageTool->pageSize(); } if (mSearchEntry != nullptr) { selector.whereRef().append(mSearchEntry->sqlWhere()); } selector.setOrder("id", Qt::DescendingOrder); QHeaderView *headerView = mTableView->horizontalHeader(); if (headerView != nullptr) { QString orderField = mTableView->columnName(headerView->sortIndicatorSection()); if (!orderField.isEmpty() && orderField != "remark") { selector.setOrder(orderField, headerView->sortIndicatorOrder()); } } TSqlWhereCompsiteV2 whereCompsite; if (!mProductCategoryLst.contains("*")) { whereCompsite.append("product_category", mProductCategoryLst, "@>"); whereCompsite.append("product_category", "{*}"); whereCompsite.setLogic(TSqlWhereCompsiteV2::Logic_Or); selector.whereRef().append(whereCompsite); } if (pageNumInt < 1) { pageNumInt = 1; } if (pageSizeInt < 1) { pageSizeInt = 100; } selector.setPage(pageNumInt, pageSizeInt); return selector; } void SysRoleMgt::initTableView() { mTableView = new TTableView(this); QVariantList hItems; QStringList dataKeyList; dataKeyList << "id" << "name" << "sys_name" << "description"; mTableView->setData("data_keys", dataKeyList); mTableView->setData("primary_key", "id"); hItems << QVariant(); hItems << TDataParse::variantList2Map( QVariantList() << "name" << "name" << "display" << ttr("Role Name") << "resizeMode" << "Interactive" << "displayRole" << "$name" << "search" << "string" << "size" << 200); hItems << TDataParse::variantList2Map( QVariantList() << "name" << "description" << "display" << ttr("Remark") << "resizeMode" << "Interactive" << "displayRole" << "$description" << "search" << "string" << "size" << 400); mTableView->setHeaderItem(hItems); connect(mTableView->horizontalHeader(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)), this, SLOT(refresh())); } void SysRoleMgt::fillTableData(const TDataResponse &iDataRes) { if (mPageTool != nullptr) { mPageTool->setRowCount(iDataRes.dataCount(), true); } mTableView->loadData(iDataRes.data().toList()); return; } void SysRoleMgt::keyPressEvent(QKeyEvent *iEvent) { if (iEvent->modifiers() == Qt::ControlModifier && iEvent->key() == Qt::Key_F && mSearchEntry) { mSearchEntry->setFocus(); mSearchEntry->selectAll(); } }