typedef unsigned int MsgID;
enum ENHandleInfoWidgetType
{
ENHandleInfoWidgetType_Invalid = -1,
ENHandleInfoWidgetType_BaseInfo,
ENHandleInfoWidgetType_HandleInfoMsgSend,
ENHandleInfoWidgetType_HandleInfoMsgSetAdd,
ENHandleInfoWidgetType_HandleInfoMsgSetUpdate,
ENHandleInfoWidgetType_Max
};
struct STMsgDefineInfo
{
MsgID m_msgID;
string m_strServerName;
string m_strName;
STMsgDefineInfo()
{
m_msgID = 0;
}
};
class CTreeWidget: public WContainerWidget
{
public:
CTreeWidget(WContainerWidget *parent): WContainerWidget(parent)
{
m_pWAbstractItemModel = NULL;
m_pWTreeView = NULL;
}
int Init(vector<STMsgDefineInfo>& allMsgDefineInfoVec)
{
m_AllMsgDefineInfoVec = allMsgDefineInfoVec;
//初始化模型
{
if(m_pWAbstractItemModel)
{
delete m_pWAbstractItemModel;
m_pWAbstractItemModel = NULL;
}
if(m_pWTreeView)
{
delete m_pWTreeView;
m_pWTreeView = NULL;
}
m_pWAbstractItemModel = new WStandardItemModel;
m_pWTreeView = new WTreeView(this);
m_pWTreeView->setModel(m_pWAbstractItemModel);
{
m_pWTreeView->setRowHeight(24);
m_pWTreeView->setHeaderHeight(24);
m_pWTreeView->setSortingEnabled(false);
m_pWTreeView->setSelectionMode(Wt::SingleSelection);
m_pWTreeView->setEditTriggers(Wt::WAbstractItemView::NoEditTrigger);
}
m_pWTreeView->clicked().connect<CTreeWidget, CTreeWidget>(this, &CTreeWidget::HandleUsrClickEvent);
}
int iRet = InitMsgDataToList();
if(0 != iRet)
{
return -1;
}
iRet = InitModel();
if(0 != iRet)
{
return -1;
}
return 0;
}
protected:
//初始化数据到各数据集合
int InitMsgDataToList()
{
//先清理之前的数据
{
m_AllMsgDefineInfoMap.clear();
m_ServerMsgMap.clear();
}
//初始化所有消息定义map集合
{
const int iCount = m_AllMsgDefineInfoVec.size();
for(int i = 0; iCount > i; ++i)
{
STMsgDefineInfo& stMsgDefineInfo = m_AllMsgDefineInfoVec[i];
m_AllMsgDefineInfoMap[stMsgDefineInfo.m_msgID] = stMsgDefineInfo;
}
}
//初始化消息按不同服务分类map集合
{
const int iCount = m_AllMsgDefineInfoVec.size();
for(int i = 0; iCount > i; ++i)
{
STMsgDefineInfo& stMsgDefineInfo = m_AllMsgDefineInfoVec[i];
map< string, vector<MsgID> >::iterator it = m_ServerMsgMap.find(stMsgDefineInfo.m_strServerName);
if(m_ServerMsgMap.end() == it)
{
vector<MsgID> vec;
vec.push_back(stMsgDefineInfo.m_msgID);
m_ServerMsgMap[stMsgDefineInfo.m_strServerName] = vec;
}
else
{
vector<MsgID>& vec = (*it).second;
vec.push_back(stMsgDefineInfo.m_msgID);
}
}
}
return 0;
}
//初始化数据模型
int InitModel()
{
//基本设置
{
m_pWAbstractItemModel->insertColumn(0);
m_pWAbstractItemModel->insertRow(0);
boost::any dataEditRole, dataLevelRole;
dataEditRole = boost::any(string("base info"));
dataLevelRole = boost::any(GetIDByTypeAndMsgID(ENHandleInfoWidgetType_BaseInfo, 0));
m_pWAbstractItemModel->setData(0, 0, dataEditRole, EditRole);
m_pWAbstractItemModel->setData(0, 0, dataLevelRole, LevelRole);
}
//消息定义
{
m_pWAbstractItemModel->insertRow(1);
boost::any dataEditRole, dataLevelRole;
dataEditRole = boost::any(string("msg set"));
dataLevelRole = boost::any(GetIDByTypeAndMsgID(ENHandleInfoWidgetType_Max, 0));
m_pWAbstractItemModel->setData(1, 0, dataEditRole, EditRole);
m_pWAbstractItemModel->setData(1, 0, dataLevelRole, LevelRole);
WModelIndex msgSetIndex = m_pWAbstractItemModel->index(1, 0);
//添加消息
{
m_pWAbstractItemModel->insertColumn(0, msgSetIndex);
m_pWAbstractItemModel->insertRow(0, msgSetIndex);
boost::any dataEditRole, dataLevelRole;
dataEditRole = boost::any(string("add msg"));
dataLevelRole = boost::any(GetIDByTypeAndMsgID(ENHandleInfoWidgetType_HandleInfoMsgSetAdd, 0));
m_pWAbstractItemModel->setData(0, 0, dataEditRole, EditRole, msgSetIndex);
m_pWAbstractItemModel->setData(0, 0, dataLevelRole, LevelRole, msgSetIndex);
}
//修改消息
{
m_pWAbstractItemModel->insertRow(1, msgSetIndex);
boost::any dataEditRole, dataLevelRole;
dataEditRole = boost::any(string("msg update"));
dataLevelRole = boost::any(GetIDByTypeAndMsgID(ENHandleInfoWidgetType_Max, 0));
m_pWAbstractItemModel->setData(1, 0, dataEditRole, EditRole, msgSetIndex);
m_pWAbstractItemModel->setData(1, 0, dataLevelRole, LevelRole, msgSetIndex);
WModelIndex msgSetUpdateIndex = m_pWAbstractItemModel->index(1, 0, msgSetIndex);
AddAllMsgDataToModel(ENHandleInfoWidgetType_HandleInfoMsgSetUpdate, msgSetUpdateIndex);
}
}
//消息发送
{
m_pWAbstractItemModel->insertRow(2);
boost::any dataEditRole, dataLevelRole;
dataEditRole = boost::any(string("msg send"));
dataLevelRole = boost::any(GetIDByTypeAndMsgID(ENHandleInfoWidgetType_Max, 0));
m_pWAbstractItemModel->setData(2, 0, dataEditRole, EditRole);
m_pWAbstractItemModel->setData(2, 0, dataLevelRole, LevelRole);
WModelIndex msgSendIndex = m_pWAbstractItemModel->index(2, 0);
AddAllMsgDataToModel(ENHandleInfoWidgetType_HandleInfoMsgSend, msgSendIndex);
}
}
//单击事件处理槽函数
void HandleUsrClickEvent(WModelIndex index)
{
long ID = boost::any_cast<long>(index.data(LevelRole));
ENHandleInfoWidgetType enHandleInfoWidgetType;
MsgID msgID;
GetTypeAndMsgIDByID(ID, enHandleInfoWidgetType, msgID);
STMsgDefineInfo stMsgDefineInfo;
if(ENHandleInfoWidgetType_BaseInfo == enHandleInfoWidgetType || \
ENHandleInfoWidgetType_HandleInfoMsgSetAdd == enHandleInfoWidgetType)
{
}
else if(ENHandleInfoWidgetType_HandleInfoMsgSend == enHandleInfoWidgetType || \
ENHandleInfoWidgetType_HandleInfoMsgSetUpdate == enHandleInfoWidgetType)
{
stMsgDefineInfo = (*(m_AllMsgDefineInfoMap.find(msgID))).second;
}
else
{
return;
}
//告知外面
cout << "enHandleInfoWidgetType:" << enHandleInfoWidgetType << " msgID:" << msgID << endl;
}
protected:
//添加所有消息数据到模型
int AddAllMsgDataToModel(ENHandleInfoWidgetType enHandleInfoWidgetType,
WModelIndex& parentIndex)
{
int iRow = 0;
map< string, vector<MsgID> >::iterator it = m_ServerMsgMap.begin();
for(; m_ServerMsgMap.end() != it; ++it, ++iRow)
{
//设置服务名node
{
if(0 == iRow)
{
m_pWAbstractItemModel->insertColumn(0, parentIndex);
}
m_pWAbstractItemModel->insertRow(iRow, parentIndex);
boost::any dataEditRole, dataLevelRole;
dataEditRole = boost::any((*it).first);
dataLevelRole = boost::any(GetIDByTypeAndMsgID(ENHandleInfoWidgetType_Max, 0));
m_pWAbstractItemModel->setData(iRow, 0, dataEditRole, EditRole, parentIndex);
m_pWAbstractItemModel->setData(iRow, 0, dataLevelRole, LevelRole, parentIndex);
}
//处理服务下的所有消息
WModelIndex serverIndex = m_pWAbstractItemModel->index(iRow, 0, parentIndex);
vector<MsgID>& vec = (*it).second;
const int iCount = vec.size();
for(int i = 0; iCount > i; ++i)
{
if(0 == i)
{
m_pWAbstractItemModel->insertColumn(0, serverIndex);
}
m_pWAbstractItemModel->insertRow(i, serverIndex);
boost::any dataEditRole, dataLevelRole;
dataEditRole = boost::any((*(m_AllMsgDefineInfoMap.find(vec[i]))).second.m_strName);
dataLevelRole = boost::any(GetIDByTypeAndMsgID(enHandleInfoWidgetType, vec[i]));
m_pWAbstractItemModel->setData(i, 0, dataEditRole, EditRole, serverIndex);
m_pWAbstractItemModel->setData(i, 0, dataLevelRole, LevelRole, serverIndex);
}
}
return 0;
}
//通过类型与消息id得到唯一标识
long GetIDByTypeAndMsgID(ENHandleInfoWidgetType enHandleInfoWidgetType, MsgID msgID)
{
long ID = (((long)enHandleInfoWidgetType << 32) & 0x00ff00000000) | (msgID & 0xffffffff);
return ID;
}
//通过唯一标识得到类型和消息id
void GetTypeAndMsgIDByID(long ID, ENHandleInfoWidgetType& enHandleInfoWidgetType, MsgID& msgID )
{
enHandleInfoWidgetType = (ENHandleInfoWidgetType)((ID >> 32) & 0x00ff);
msgID = (MsgID)(ID & 0xffffffff);
}
private:
map<MsgID, STMsgDefineInfo> m_AllMsgDefineInfoMap;//所有消息定义map集合
map< string, vector<MsgID> > m_ServerMsgMap;//消息按不同服务分类map集合
vector<STMsgDefineInfo> m_AllMsgDefineInfoVec;//所有消息定义vector集合
WStandardItemModel *m_pWAbstractItemModel;//储存数据的模型
WTreeView *m_pWTreeView;//显示树形列表的视图
};