#include "dataslothelper.h" #include #include DataSlotHelper *DataSlotHelper::p_helper=NULL; DataSlotHelper::DataSlotHelper(QObject *parent) : QObject(parent) { } DataSlotHelper::~DataSlotHelper() { } DataSlotHelper * DataSlotHelper::instance() { if(NULL == p_helper) { p_helper=new DataSlotHelper(); } return p_helper; } void DataSlotHelper::movedUp(QTableWidget *table) { bool moveUpMax=false; //重置选择区域 resetRangeSelection(table); m_sourceRowsData.clear(); m_descRowsData.clear(); //拾取表格数据 pickTableRangeData(table,m_sourceRowsData); //表格数据改变 isUp 是否向上,moveFixed是否到头固定模式 tableSelectionRangeChange(table,true,moveUpMax); if (moveUpMax) { return; } pickTableRangeData(table,m_descRowsData); //交换移动原始数据和移动的目标数据 swapSelRowMoveRow(true); //刷新移动数据 updateMoveRowData(table); m_sourceRowsData.clear(); m_descRowsData.clear(); } void DataSlotHelper::movedDow(QTableWidget *table) { bool moveDowMax=false; resetRangeSelection(table); m_sourceRowsData.clear(); m_descRowsData.clear(); pickTableRangeData(table,m_sourceRowsData); tableSelectionRangeChange(table,false,moveDowMax); if (moveDowMax) { return; } pickTableRangeData(table,m_descRowsData); swapSelRowMoveRow(false); updateMoveRowData(table); m_sourceRowsData.clear(); m_descRowsData.clear(); } //表格数据改变 isUp 是否向上,moveFixed是否到头固定模式 void DataSlotHelper::tableSelectionRangeChange(QTableWidget *table,bool isUp,bool &moveFixed) { QList selRanges=table->selectedRanges(); int selCount=selRanges.size(); int rowCount=table->rowCount(); bool isSel=selCount>0; if(!isSel) { return; } int sRow=0; int eRow=0; int beginRow=0; int endRow=0; QItemSelectionModel *selModel=new QItemSelectionModel(table->model()); QTableWidgetSelectionRange range; QModelIndex topLeft; QModelIndex bottomRight; QItemSelection rowSelection; for (int i=0;imodel()->index(beginRow,0); bottomRight=table->model()->index(endRow,0); rowSelection.select(topLeft,bottomRight); selModel->select(rowSelection,QItemSelectionModel::Select|QItemSelectionModel::Rows); } if (!moveFixed) { table->setSelectionModel(selModel); } table->setFocus(); } QTableWidgetSelectionRange operator +(QTableWidgetSelectionRange rg1,QTableWidgetSelectionRange rg2) { int top = qMin( rg1.topRow(),rg2.topRow()); int bottom = qMax( rg1.bottomRow(),rg2.bottomRow()); return QTableWidgetSelectionRange(top,0,bottom,0); } void DataSlotHelper::resetRangeSelection(QTableWidget* table) { QList selRanges=table->selectedRanges(); int selCount=selRanges.size(); int rowCount=table->rowCount(); bool isSel=selCount>0; if(!isSel) { return; } int sRow=0; int eRow=0; QItemSelectionModel *selModel=new QItemSelectionModel(table->model()); QTableWidgetSelectionRange range; QList newRangeList; megerRange(newRangeList,selRanges); selCount=newRangeList.size(); QModelIndex topLeft; QModelIndex bottomRight; QItemSelection rowSelection; bool isMerge=false; int nindex=0; for (int i=0;imodel()->index(sRow,0); bottomRight=table->model()->index(eRow,0); rowSelection.select(topLeft,bottomRight); selModel->select(rowSelection,QItemSelectionModel::Select|QItemSelectionModel::Rows); } table->setSelectionModel(selModel); table->setFocus(); } void DataSlotHelper::megerRange(QList &rangeLst, const QList &selRanges) { int selCount=selRanges.size(); bool isSel=selCount>0; if(!isSel) { return; } QTableWidgetSelectionRange netRange; QTableWidgetSelectionRange preRange; bool isMerge=false; int nindex=0; for (int i=0;i (selCount-1) ) { rangeLst <= rowCount; if (moveFixed) { endRow=rowCount-1; }else { beginRow++; } } } //获取表格行数据 QStringList DataSlotHelper::tableRowData(QTableWidget* table,int row) { QStringList rowData; int colCount=table->columnCount(); QTableWidgetItem *item=NULL; for(int i=0;iitem(row,i); if(NULL !=item ) { rowData<text(); } } return rowData; } //拾取表格数据 void DataSlotHelper::pickTableRangeData(QTableWidget* table, QList &rangeData) { QList selRanges=table->selectedRanges(); int selCount=selRanges.size(); bool isSel=selCount>0; if(!isSel) { return; } int sRow=0; int eRow=0; QTableWidgetSelectionRange range; for (int i=0;i rgData; for(int r=sRow;r<=eRow;r++) { rgData< sourceKeys; QList< QList > sourceValues; pinkTableDataRanges(m_sourceRowsData,sourceKeys,sourceValues); QList descKeys; QList< QList > descValues; pinkTableDataRanges(m_descRowsData,descKeys,descValues); m_sourceRowsData.clear(); m_descRowsData.clear(); resetNewDaataRow(descValues,isUp); int sSize=sourceKeys.size(); QTableWidgetSelectionRange srcRange; QTableWidgetSelectionRange desRange; QList srcRowData; QList desRowData; int desSize=0; //目标与源进行交换 for (int i=0;i > & restDataRow,bool isUp) { QList rangeRowData; QList fillRangeRowData; QStringList fillRowData; int resetSize=restDataRow.size(); int fillSize=0; QList< QList > result; if (resetSize == 0) { return ; } for (int i=0;i &pinkData, QList &ranges,QList< QList > &rowsData) { QList result; QListIterator itor(pinkData); PinkTableData pdata; while(itor.hasNext()) { pdata=itor.next(); ranges< &data) { int count=data.size(); int rowCount=table->rowCount(); bool isData=count>0; if(!isData) { return; } int sRow=0; int eRow=0; int rIndex=0; QTableWidgetSelectionRange range; PinkTableData pData; QListIterator itor(data); QList rowData; while(itor.hasNext()) { pData=itor.next(); range=pData.m_Range; sRow=range.topRow(); eRow=range.bottomRow(); rowData=pData.m_rowData; rIndex=0; for (int i=sRow;i<=eRow;i++) { QStringList dtm=rowData.value(rIndex); updateTableView(table,rowData.value(rIndex),i); rIndex++; } } } //刷新表格视图数据 void DataSlotHelper::updateTableView(QTableWidget *table,const QStringList &rowsData,int row) { if ( NULL == table) { return; } int colCount=table->columnCount(); QTableWidgetItem *item=NULL; for(int i=0;iitem(row,i); if(NULL !=item ) { item->setText(rowsData.value(i)); } } } DataSlotHelper::GC::~GC() { if(NULL != p_helper) { delete DataSlotHelper::p_helper; DataSlotHelper::p_helper=NULL; } } PinkTableData::PinkTableData(const PinkTableData & other) { m_Range=other.m_Range; m_rowData=other.m_rowData; } PinkTableData::PinkTableData(const QTableWidgetSelectionRange &range,const QList &rowdata) { m_Range=QTableWidgetSelectionRange(range); m_rowData=rowdata; } PinkTableData::PinkTableData() { } PinkTableData::~PinkTableData() { m_rowData.clear(); } PinkTableData PinkTableData::operator=(const PinkTableData &other) { m_Range=other.m_Range; m_rowData=other.m_rowData; return *this; }