提交缺少文件
This commit is contained in:
parent
e2cf4476e8
commit
4f144b1f1e
521
DataMgr/src/dataslothelper.cpp
Normal file
521
DataMgr/src/dataslothelper.cpp
Normal file
|
|
@ -0,0 +1,521 @@
|
|||
#include "dataslothelper.h"
|
||||
#include <QItemSelectionModel>
|
||||
#include <QListIterator>
|
||||
|
||||
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<QTableWidgetSelectionRange> 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;i<selCount;i++)
|
||||
{
|
||||
if(moveFixed)
|
||||
{
|
||||
break;
|
||||
}
|
||||
range=selRanges.value(i);
|
||||
sRow=range.topRow();
|
||||
eRow=range.bottomRow();
|
||||
beginRow=sRow;
|
||||
endRow=eRow;
|
||||
rangeMoveRow(beginRow,endRow,rowCount,isUp,moveFixed);
|
||||
topLeft=table->model()->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<QTableWidgetSelectionRange> 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<QTableWidgetSelectionRange> newRangeList;
|
||||
megerRange(newRangeList,selRanges);
|
||||
selCount=newRangeList.size();
|
||||
QModelIndex topLeft;
|
||||
QModelIndex bottomRight;
|
||||
QItemSelection rowSelection;
|
||||
|
||||
bool isMerge=false;
|
||||
int nindex=0;
|
||||
for (int i=0;i<selCount;i++)
|
||||
{
|
||||
range=newRangeList.value(i);
|
||||
sRow=range.topRow();
|
||||
eRow=range.bottomRow();
|
||||
topLeft=table->model()->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<QTableWidgetSelectionRange> &rangeLst,
|
||||
const QList<QTableWidgetSelectionRange> &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;i++)
|
||||
{
|
||||
nindex=i+1;
|
||||
if(nindex > (selCount-1) )
|
||||
{
|
||||
rangeLst <<selRanges.value(i);
|
||||
break;
|
||||
}
|
||||
|
||||
if(!isMerge)
|
||||
{
|
||||
preRange=selRanges.value(i);
|
||||
}
|
||||
|
||||
netRange=selRanges.value(nindex);
|
||||
|
||||
|
||||
isMerge=qAbs(netRange.topRow()-preRange.bottomRow())==1 || qAbs( netRange.bottomRow()-preRange.topRow()) == 1;
|
||||
|
||||
if (isMerge)
|
||||
{
|
||||
preRange=preRange + netRange;
|
||||
if(nindex == (selCount-1) )
|
||||
{
|
||||
rangeLst <<preRange;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
rangeLst <<preRange;
|
||||
if(nindex == (selCount-1) )
|
||||
{
|
||||
rangeLst <<netRange;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//选中移动范围
|
||||
void DataSlotHelper::rangeMoveRow(int &beginRow,int &endRow,int rowCount,bool isUp,bool &moveFixed)
|
||||
{
|
||||
|
||||
if (isUp)
|
||||
{
|
||||
if (!moveFixed)
|
||||
{
|
||||
beginRow--;
|
||||
}
|
||||
moveFixed=beginRow<0;
|
||||
if (moveFixed)
|
||||
{
|
||||
beginRow=0;
|
||||
}else
|
||||
{
|
||||
endRow--;
|
||||
}
|
||||
|
||||
}else
|
||||
{
|
||||
if (!moveFixed)
|
||||
{
|
||||
endRow++;
|
||||
}
|
||||
moveFixed=endRow >= 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;i<colCount;i++)
|
||||
{
|
||||
item=table->item(row,i);
|
||||
if(NULL !=item )
|
||||
{
|
||||
rowData<<item->text();
|
||||
}
|
||||
|
||||
}
|
||||
return rowData;
|
||||
|
||||
}
|
||||
//拾取表格数据
|
||||
void DataSlotHelper::pickTableRangeData(QTableWidget* table,
|
||||
QList<PinkTableData> &rangeData)
|
||||
{
|
||||
QList<QTableWidgetSelectionRange> 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<selCount;i++)
|
||||
{
|
||||
range=selRanges.value(i);
|
||||
sRow=range.topRow();
|
||||
eRow=range.bottomRow();
|
||||
QList<QStringList> rgData;
|
||||
for(int r=sRow;r<=eRow;r++)
|
||||
{
|
||||
rgData<<tableRowData(table,r);
|
||||
}
|
||||
rangeData << PinkTableData(range,rgData);
|
||||
}
|
||||
}
|
||||
//交换移动原始数据和移动的目标数据
|
||||
void DataSlotHelper::swapSelRowMoveRow(bool isUp)
|
||||
{
|
||||
|
||||
|
||||
QList<QTableWidgetSelectionRange> sourceKeys;
|
||||
QList< QList<QStringList> > sourceValues;
|
||||
|
||||
pinkTableDataRanges(m_sourceRowsData,sourceKeys,sourceValues);
|
||||
|
||||
QList<QTableWidgetSelectionRange> descKeys;
|
||||
QList< QList<QStringList> > descValues;
|
||||
|
||||
pinkTableDataRanges(m_descRowsData,descKeys,descValues);
|
||||
|
||||
|
||||
m_sourceRowsData.clear();
|
||||
m_descRowsData.clear();
|
||||
resetNewDaataRow(descValues,isUp);
|
||||
int sSize=sourceKeys.size();
|
||||
QTableWidgetSelectionRange srcRange;
|
||||
QTableWidgetSelectionRange desRange;
|
||||
QList<QStringList> srcRowData;
|
||||
QList<QStringList> desRowData;
|
||||
int desSize=0;
|
||||
//目标与源进行交换
|
||||
for (int i=0;i<sSize;i++)
|
||||
{
|
||||
|
||||
//源插入到目标
|
||||
srcRange=sourceKeys.value(i);
|
||||
srcRowData=sourceValues.value(i);
|
||||
/////////////////////////////////////////////
|
||||
//目标插入到源
|
||||
desRange=descKeys.value(i);
|
||||
desRowData=descValues.value(i);
|
||||
|
||||
QStringList dp1=desRowData.value(0);
|
||||
QStringList dp2=desRowData.value(1);
|
||||
|
||||
QStringList sp1=srcRowData.value(0);
|
||||
QStringList sp2=srcRowData.value(1);
|
||||
|
||||
|
||||
m_descRowsData << PinkTableData(srcRange,desRowData);
|
||||
m_sourceRowsData<< PinkTableData(desRange,srcRowData);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
//重置数据行
|
||||
void DataSlotHelper::resetNewDaataRow(QList< QList<QStringList> > & restDataRow,bool isUp)
|
||||
{
|
||||
QList<QStringList> rangeRowData;
|
||||
QList<QStringList> fillRangeRowData;
|
||||
QStringList fillRowData;
|
||||
int resetSize=restDataRow.size();
|
||||
int fillSize=0;
|
||||
QList< QList<QStringList> > result;
|
||||
if (resetSize == 0)
|
||||
{
|
||||
return ;
|
||||
}
|
||||
|
||||
for (int i=0;i<resetSize;i++)
|
||||
{
|
||||
rangeRowData=restDataRow.value(i);
|
||||
fillSize=rangeRowData.size();
|
||||
|
||||
if(isUp)
|
||||
{
|
||||
fillRowData=rangeRowData.value(0);
|
||||
}else
|
||||
{
|
||||
fillRowData=rangeRowData.value(fillSize-1);
|
||||
}
|
||||
fillRangeRowData.clear();
|
||||
for (int c=0;c<fillSize;c++)
|
||||
{
|
||||
fillRangeRowData <<fillRowData;
|
||||
}
|
||||
result <<fillRangeRowData;
|
||||
}
|
||||
|
||||
restDataRow.clear();
|
||||
restDataRow=result;
|
||||
|
||||
}
|
||||
//分离表格数据和范围
|
||||
void DataSlotHelper::pinkTableDataRanges(const QList<PinkTableData> &pinkData,
|
||||
QList<QTableWidgetSelectionRange> &ranges,QList< QList<QStringList> > &rowsData)
|
||||
{
|
||||
QList<QTableWidgetSelectionRange> result;
|
||||
QListIterator<PinkTableData> itor(pinkData);
|
||||
PinkTableData pdata;
|
||||
while(itor.hasNext())
|
||||
{
|
||||
pdata=itor.next();
|
||||
ranges<<pdata.m_Range;
|
||||
|
||||
rowsData<<pdata.m_rowData;
|
||||
}
|
||||
}
|
||||
|
||||
//刷新移动数据
|
||||
void DataSlotHelper::updateMoveRowData(QTableWidget* table)
|
||||
{
|
||||
//先目标再源
|
||||
updateRowsData(table,m_descRowsData);
|
||||
updateRowsData(table,m_sourceRowsData);
|
||||
|
||||
}
|
||||
//刷新移动数据
|
||||
void DataSlotHelper::updateRowsData(QTableWidget* table,
|
||||
const QList<PinkTableData> &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<PinkTableData> itor(data);
|
||||
QList<QStringList> 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;i<colCount;i++)
|
||||
{
|
||||
item=table->item(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<QStringList> &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;
|
||||
|
||||
}
|
||||
79
DataMgr/src/dataslothelper.h
Normal file
79
DataMgr/src/dataslothelper.h
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
#ifndef DATASLOTHELPER_H
|
||||
#define DATASLOTHELPER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QTableWidget>
|
||||
class PinkTableData
|
||||
{
|
||||
|
||||
public:
|
||||
explicit PinkTableData();
|
||||
PinkTableData(const QTableWidgetSelectionRange &range,const QList<QStringList> &rowdata);
|
||||
PinkTableData( const PinkTableData & other);
|
||||
~PinkTableData();
|
||||
PinkTableData operator =(const PinkTableData &other);
|
||||
QTableWidgetSelectionRange m_Range;
|
||||
QList<QStringList> m_rowData;
|
||||
|
||||
};
|
||||
|
||||
class DataSlotHelper : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
~DataSlotHelper();
|
||||
static DataSlotHelper *instance();
|
||||
//向上移动
|
||||
void movedUp(QTableWidget *table);
|
||||
//向下移动
|
||||
void movedDow(QTableWidget *table);
|
||||
|
||||
private:
|
||||
//获取表格行数据
|
||||
QStringList tableRowData(QTableWidget* table,int row);
|
||||
//拾取表格数据
|
||||
void pickTableRangeData(QTableWidget* table
|
||||
,QList<PinkTableData> &rangeData);
|
||||
//表格数据改变 isUp 是否向上,moveFixed是否到头固定模式
|
||||
void tableSelectionRangeChange(QTableWidget *table,bool isUp,bool &moveFixed);
|
||||
//选中移动范围
|
||||
void rangeMoveRow(int &beginRow,int &endRow,int rowCount,bool isUp,bool &moveFixed);
|
||||
//交换移动原始数据和移动的目标数据
|
||||
void swapSelRowMoveRow(bool isUp);
|
||||
//刷新移动数据
|
||||
void updateMoveRowData(QTableWidget* table);
|
||||
//刷新行数据
|
||||
void updateRowsData(QTableWidget* table,const QList<PinkTableData> &data);
|
||||
//刷新表格视图数据
|
||||
void updateTableView(QTableWidget *table,const QStringList& rowsData,int row);
|
||||
//分离表格数据和范围
|
||||
void pinkTableDataRanges(const QList<PinkTableData> &pinkData,
|
||||
QList<QTableWidgetSelectionRange> &ranges,QList< QList<QStringList> > &rowsData);
|
||||
//重置数据行
|
||||
void resetNewDaataRow(QList< QList<QStringList> > & restDataRow,bool isUp);
|
||||
//重置选择区域
|
||||
void resetRangeSelection(QTableWidget* table);
|
||||
//合并单选行
|
||||
void megerRange(QList<QTableWidgetSelectionRange> &rangeLst,const QList<QTableWidgetSelectionRange> &selRanges);
|
||||
|
||||
private:
|
||||
|
||||
explicit DataSlotHelper(QObject *parent=NULL);
|
||||
class GC{
|
||||
public:
|
||||
~GC();
|
||||
};
|
||||
private:
|
||||
static DataSlotHelper *p_helper;
|
||||
GC m_gc;
|
||||
//源行数据
|
||||
QList<PinkTableData> m_sourceRowsData;
|
||||
//目标数据
|
||||
QList<PinkTableData> m_descRowsData;
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // DATASLOTHELPER_H
|
||||
Loading…
Reference in New Issue
Block a user