123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- #include "modelpage.h"
- /*----------------------------------ModelPage----------------------------------------------------------*/
- ModelPage::ModelPage(QWidget *parent) : QWidget(parent)
- {
- modelTable = new QTableView();
- itemModel = new QStandardItemModel();
- QHBoxLayout* searchLayout = new QHBoxLayout();
- QLabel *searchLable = new QLabel("搜索选择:");
- QComboBox* searchChose = new QComboBox();
- searchChose->addItem("模型id");
- searchChose->addItem("模型名称");
- searchChose->addItem("模型类型");
- QLineEdit* setSearch = new QLineEdit();
- QPushButton* confirmSearch = new QPushButton();
- confirmSearch->setText("确认");
- searchLayout->addWidget(searchLable);
- searchLayout->addWidget(searchChose);
- searchLayout->addWidget(setSearch);
- searchLayout->addWidget(confirmSearch);
- QHBoxLayout* optLayout = new QHBoxLayout();
- mPageInfo = new QLabel();
- mPageInfo->setText("");
- QSpacerItem* spacer1 = new QSpacerItem(0,30,QSizePolicy::Expanding,QSizePolicy::Fixed);
- QPushButton* upPage = new QPushButton();
- upPage->setText("上一页");
- QPushButton* nextPage = new QPushButton();
- nextPage->setText("下一页");
- QLabel* skipLabel = new QLabel();
- skipLabel->setText("跳转");
- mSetPage = new QLineEdit();
- QPushButton* confirmSkip = new QPushButton();
- confirmSkip->setText("确认");
- optLayout->addWidget(mPageInfo);
- optLayout->addItem(spacer1);
- optLayout->addWidget(upPage);
- optLayout->addWidget(nextPage);
- optLayout->addWidget(skipLabel);
- optLayout->addWidget(mSetPage);
- optLayout->addWidget(confirmSkip);
- QVBoxLayout* mainLayout = new QVBoxLayout(this);
- mainLayout->addLayout(searchLayout);
- mainLayout->addWidget(modelTable);
- mainLayout->addLayout(optLayout);
- /*
- {
- itemModel->setItem(0,0,new QStandardItem("1"));
- itemModel->setItem(0,1,new QStandardItem("扇叶模型"));
- itemModel->setItem(0,2,new QStandardItem("This is a model"));
- itemModel->setItem(0,3,new QStandardItem("vga"));
- QPushButton* edit1 = new QPushButton("编辑");
- edit1->setFixedSize(QSize(120,30));
- edit1->setStyleSheet("QPushButton{color:white;background-color:rgb(51,204,255);font-family:黑体;border-radius: 15px;}"
- "QPushButton:pressed{background-color:rgb(51,129,172)}");
- QPushButton* del1 = new QPushButton("删除");
- del1->setFixedSize(QSize(120,30));
- del1->setStyleSheet("QPushButton{color:white;background-color:rgb(51,204,255);font-family:黑体;border-radius: 15px;}"
- "QPushButton:pressed{background-color:rgb(51,129,172)}");
- QHBoxLayout *vLayout1 = new QHBoxLayout();
- QWidget * widget1 = new QWidget;
- vLayout1->addWidget(edit1);
- vLayout1->addWidget(del1);
- vLayout1->addSpacing(10);
- widget1->setLayout(vLayout1);
- modelTable->setIndexWidget(itemModel->index(0,4),widget1);
- itemModel->setItem(1,0,new QStandardItem("2"));
- itemModel->setItem(1,1,new QStandardItem("机翼模型"));
- itemModel->setItem(1,2,new QStandardItem("This is another model"));
- itemModel->setItem(1,3,new QStandardItem("txt"));
- QPushButton* edit = new QPushButton("编辑");
- edit->setFixedSize(QSize(120,30));
- edit->setStyleSheet("QPushButton{color:white;background-color:rgb(51,204,255);font-family:黑体;border-radius: 15px;}"
- "QPushButton:pressed{background-color:rgb(51,129,172)}");
- QPushButton* del = new QPushButton("删除");
- del->setFixedSize(QSize(120,30));
- del->setStyleSheet("QPushButton{color:white;background-color:rgb(51,204,255);font-family:黑体;border-radius: 15px;}"
- "QPushButton:pressed{background-color:rgb(51,129,172)}");
- QHBoxLayout *vLayout = new QHBoxLayout();
- QWidget * widget = new QWidget;
- vLayout->addWidget(edit);
- vLayout->addWidget(del);
- vLayout->addSpacing(10);
- widget->setLayout(vLayout);
- modelTable->setIndexWidget(itemModel->index(1,4),widget);
- }
- */
- mTotalCount = 0;
- mTotalPage = 0;
- mCurrPage = 0;
- mPerPageCnt = 9;
- // for(int i = 0 ; i < 20; i++)
- // {
- // ModelInfo* model = new ModelInfo;
- // model->id = "i";
- // model->name = "lisi" ;
- // model->des = "1";
- // model->path = "D";
- // model->type ="Gmash";
- // modelList.append(model);
- // }
- int num = modelList.size();
- cptPageData(num);
- int nStart = 0;
- int nEnd = 0;
- cptSubscripRange(nStart,nEnd);
- displayTable(nStart,nEnd);
- setPageInfo(mTotalPage,mTotalCount,mCurrPage);
- connect(confirmSkip,&QPushButton::clicked,this,&ModelPage::onSearchButtonClick);
- connect(upPage,&QPushButton::clicked,this,&ModelPage::onUpButtonClick);
- connect(nextPage,&QPushButton::clicked,this,&ModelPage::onNextButtonClick);
- }
- void ModelPage::displayTable(int nStart, int nEnd)
- {
- int columenCnt;
- if( modelList.size() == 0)
- {
- columenCnt = 0;
- }
- else
- {
- columenCnt = nEnd - nStart + 1 ;
- }
- QList<ModelInfo*> tv = modelList.mid(nStart,columenCnt );
- // qDebug()<<nStart<<" " << nEnd << " " << columenCnt;
- if(tv.size() != columenCnt )
- {
- // qDebug()<<"分页数据异常";
- return;
- }
- itemModel->clear();
- modelTable->setModel(itemModel);
- QStringList tableHead;
- tableHead<<"模型ID"<<"模型名称"<<"模型描述"<<"模型路径"<<"模型类型"<<"操作";
- itemModel->setHorizontalHeaderLabels(tableHead);
- QFont fontHeadr("微软雅黑",11);
- for(int i = 0 ; i < itemModel->columnCount(); i++)
- {
- QStandardItem *headerItem = itemModel->horizontalHeaderItem(i);
- headerItem->setFont(fontHeadr);
- headerItem->setForeground(QBrush(QColor(0,0,0)));
- }
- itemModel->setColumnCount(6);
- for(int i = 0; i < columenCnt ; i++)
- {
- itemModel->setItem(i,0,new QStandardItem(QString::fromStdString(tv.at(i)->id)));
- itemModel->setItem(i,1,new QStandardItem(QString::fromStdString(tv.at(i)->name)));
- itemModel->setItem(i,2,new QStandardItem(QString::fromStdString(tv.at(i)->des)));
- itemModel->setItem(i,3,new QStandardItem(QString::fromStdString(tv.at(i)->path)));
- itemModel->setItem(i,4,new QStandardItem(QString::fromStdString(tv.at(i)->type)));
- // int subscript = (mCurrPage -1)* mPerPageCnt + i;
- QPushButton* editButton = new QPushButton();
- editButton->setText("编辑");
- editButton->setProperty("edit",i);
- QPushButton* delButton = new QPushButton();
- delButton->setText("删除");
- delButton->setProperty("del",i);
- connect(delButton,&QPushButton::clicked,this,&ModelPage::onDelButtonClick);
- QHBoxLayout *vLayout = new QHBoxLayout();
- QWidget * buttonWidget = new QWidget;
- vLayout->addWidget(editButton);
- vLayout->addWidget(delButton);
- vLayout->addSpacing(10);
- buttonWidget->setLayout(vLayout);
- modelTable->setIndexWidget(itemModel->index(i,5),buttonWidget);
- }
- modelTable->verticalHeader()->hide();
- modelTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
- modelTable->setSelectionBehavior(QAbstractItemView::SelectRows);
- modelTable->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
- modelTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
- }
- void ModelPage::cptPageData(int count)
- {
- if(mPerPageCnt == 0)
- {
- return;
- }
- mTotalCount = count;
- mTotalPage = mTotalCount/mPerPageCnt + (mTotalCount % mPerPageCnt ? 1:0);
- mCurrPage = 1;
- }
- void ModelPage::cptSubscripRange(int &mStart,int &mEnd)
- {
- if(mPerPageCnt == 0 )
- {
- return;
- }
- if(mCurrPage < mTotalPage)
- {
- mStart = (mCurrPage - 1)* mPerPageCnt;
- mEnd = mCurrPage * mPerPageCnt - 1;
- }
- else if(mCurrPage == mTotalPage)
- {
- if(mTotalCount%mPerPageCnt == 0)
- {
- mStart = (mCurrPage -1)* mPerPageCnt;
- mEnd = mCurrPage*mPerPageCnt -1;
- }
- else
- {
- mStart = (mCurrPage -1)* mPerPageCnt;
- mEnd = (mCurrPage -1)*mPerPageCnt + mTotalCount% mPerPageCnt -1;
- }
- }
- }
- void ModelPage::setPageInfo(int i,int count,int curr)
- {
- mPageInfo->setText(QString("共计%1页,%2条记录,当前第%3页").arg(QString::number(i)).arg(QString::number(count)).arg(QString::number(curr)));
- }
- void ModelPage::onSearchButtonClick()
- {
- int target = mSetPage->text().toInt();
- if(target <= 0 || target > mTotalPage)
- {
- return;
- }
- mCurrPage = target;
- int mStart = 0;
- int mEnd = 0;
- cptSubscripRange(mStart,mEnd);
- displayTable(mStart,mEnd);
- setPageInfo(mTotalPage,mTotalCount,mCurrPage);
- }
- void ModelPage::onUpButtonClick()
- {
- if(mCurrPage - 1 <= 0)
- {
- return;
- }
- mCurrPage = mCurrPage - 1;
- int mStart = 0, mEnd = 0;
- cptSubscripRange(mStart,mEnd);
- displayTable(mStart,mEnd);
- setPageInfo(mTotalPage,mTotalCount,mCurrPage);
- }
- void ModelPage::onNextButtonClick()
- {
- if(mCurrPage + 1 > mTotalPage)
- {
- return;
- }
- mCurrPage = mCurrPage+1;
- int mStart = 0, mEnd = 0;
- cptSubscripRange(mStart,mEnd);
- displayTable(mStart,mEnd);
- setPageInfo(mTotalPage,mTotalCount,mCurrPage);
- }
- void ModelPage::onEditButtonClick()
- {
- }
- void ModelPage::onDelButtonClick()
- {
- QPushButton* pSender = qobject_cast<QPushButton*>(sender());
- int id = pSender->property("del").toInt();
- int subscript = (mCurrPage - 1)*mPerPageCnt + id;
- if(subscript > modelList.size())
- {
- return;
- }
- modelList.removeAt(subscript);
- cptPageData(modelList.size());
- int mStart = 0, mEnd = 0;
- cptSubscripRange(mStart,mEnd);
- displayTable(mStart,mEnd);
- setPageInfo(mTotalPage,mTotalCount,mCurrPage);
- }
- void ModelPage::onShowNews()
- {
- int num = modelList.size();
- cptPageData(num);
- int nStart = 0;
- int nEnd = 0;
- cptSubscripRange(nStart,nEnd);
- displayTable(nStart,nEnd);
- setPageInfo(mTotalPage,mTotalCount,mCurrPage);
- }
- /*----------------------------------------------------------ModelAdd-------------------------------------*/
- ModelAdd::ModelAdd(QDialog *parent) : QDialog(parent)
- {
- resize(500,500);
- setWindowTitle("导入模型");
- mModelInfo = new ModelInfo;
- QLabel* modelId = new QLabel("模型ID:");
- QLabel* modelName = new QLabel("模型名:");
- QLabel* modelDes = new QLabel("模型描述:");
- QLabel* modelPath = new QLabel("模型路径:");
- QLabel* modelType = new QLabel("模型类型:");
- setModelId = new QLineEdit;
- setModelName = new QLineEdit;
- setModelDes = new QLineEdit;
- setModelPath = new QLineEdit;
- setModelType = new QLineEdit;
- QPushButton* addButton = new QPushButton("添加");
- addButton->setFixedSize(QSize(120,30));
- addButton->setStyleSheet("QPushButton{color:white;background-color:rgb(51,204,255);font-family:黑体;border-radius: 15px;}"
- "QPushButton:pressed{background-color:rgb(51,129,172)}");
- QPushButton* cancelButton = new QPushButton("取消");
- cancelButton->setFixedSize(QSize(120,30));
- cancelButton->setStyleSheet("QPushButton{color:white;background-color:rgb(51,204,255);font-family:黑体;border-radius: 15px;}"
- "QPushButton:pressed{background-color:rgb(51,129,172)}");
- QGridLayout* textLayout = new QGridLayout;
- textLayout->addWidget(modelId,0,0);
- textLayout->addWidget(setModelId,0,1);
- textLayout->addWidget(modelName,1,0);
- textLayout->addWidget(setModelName,1,1);
- textLayout->addWidget(modelDes,2,0);
- textLayout->addWidget(setModelDes,2,1);
- textLayout->addWidget(modelPath,3,0);
- textLayout->addWidget(setModelPath,3,1);
- textLayout->addWidget(modelType,4,0);
- textLayout->addWidget(setModelType,4,1);
- QHBoxLayout* buttonLayout = new QHBoxLayout;
- buttonLayout->addWidget(addButton);
- buttonLayout->addWidget(cancelButton);
- QVBoxLayout* layout = new QVBoxLayout(this);
- layout->addLayout(textLayout);
- layout->addLayout(buttonLayout);
- connect(addButton,&QPushButton::clicked,this,[this](){
- mModelInfo->id = setModelId->text().toStdString();
- mModelInfo->name = setModelName->text().toStdString();
- mModelInfo->des = setModelDes->text().toStdString();
- mModelInfo->path = setModelPath->text().toStdString();
- mModelInfo->type = setModelType->text().toStdString();
- modelList.append(mModelInfo);
- emit addModel();
- emit sendModel();
- });
- connect(cancelButton,&QPushButton::clicked,this,[this](){
- close();
- });
- }
|