#include "CommandModel.h"
#include <new>
#include <QDebug>
//#include <QSize>
using std::unique_ptr;
using std::bad_alloc;
CommandModel
::CommandModel(QObject * parent
):mTreeItemRoot
(nullptr
){
setParent(parent);
try
{
mTreeItemRoot
= new TreeItem
(TreeItem
::TypeData::Root,
/*QVariantRootItem*/QVariant() );
}
catch (bad_alloc& ba ) { throw; }
}
CommandModel::~CommandModel()
{
delete mTreeItemRoot;
}
int CommandModel
::rowCount ( const QModelIndex & parent
) const {
if ( parent.isValid() )
{
TreeItem* ti_parent =static_cast<TreeItem*>(parent.internalPointer() );
return ti_parent->Childcount();
}
return mTreeItemRoot->Childcount();
}
int CommandModel
::columnCount ( const QModelIndex & parent
) const {
return 0;
}
{
if(index.isValid() )
{
if (role == Qt::DisplayRole || role == Qt::EditRole )
{
TreeItem* ti_index =static_cast<TreeItem*>(index.internalPointer() );
auto item_type = ti_index->getType();
switch( item_type )
{
case TreeItem::TypeData::GameProfile :
return QVariant(*(ti_index
->data
().
value<GameProfileData
*>
()->getGameProfileTitle
() ) );
case TreeItem::TypeData::GroupCommand :
return QVariant(ti_index
->data
().
value<GroupCommand
*>
()->getGroupCommandName
() );
case TreeItem::TypeData::VirtualKeyboard :
return QVariant(*(ti_index
->data
().
value<VirtualKeyboard
*>
()->getVirtualKeyboardName
() ) );
case TreeItem::TypeData::Command :
return QVariant(*(ti_index
->data
().
value<KeyItemData
*>
()->getCommandName
() ) );
case TreeItem::TypeData::MacroKey :
return QVariant(ti_index
->data
().
value<MacroKey
*>
()->getMacroKeyName
() );
}
}
else if(role == TypeDataRole)
{
auto ti_index =static_cast<TreeItem*>(index.internalPointer() );
qvariant_type_data.setValue(ti_index->getType() );
return qvariant_type_data;
}
}
}
Qt
::ItemFlags CommandModel
::flags ( const QModelIndex & index
) const{
if(index.isValid() )
return Qt
::ItemIsEnabled | Qt
::ItemIsEditable/*| Qt::ItemIsSelectable*/ |
QAbstractItemModel::flags(index
);
return 0;
}
{
TreeItem *parentItem;
if (parent.isValid() && parent.column() != 0)
if (!parent.isValid())
parentItem = mTreeItemRoot;
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());
TreeItem *childItem = parentItem->Child(row);
if (childItem)
return createIndex(row, column, childItem);
else
}
bool CommandModel::insertGameProfile()
{
auto row = mTreeItemRoot->Childcount();
if(row <= mTreeItemRoot->Childcount() && row >= 0 )
{
try
{
GameProfile_Variant_temp.setValue( new GameProfileData );
auto tree_item_gameprofile = new TreeItem(TreeItem::TypeData::GameProfile , GameProfile_Variant_temp ,mTreeItemRoot ) ;
mTreeItemRoot->appendChild(tree_item_gameprofile);
endInsertRows();
return true;
}
catch (bad_alloc& ba)
{
endInsertRows();
return false;
}
}
return false;
}
bool CommandModel
::insertCommandGroup( const QModelIndex & parent
) {
if(parent.isValid() )
{
try
{
auto parent_temp = static_cast<TreeItem*>(parent.internalPointer() );
auto row = parent_temp->Childcount();
beginInsertRows(parent,row,row);
CommandGroup_variant_temp.setValue( new GroupCommand );
auto tree_item_CommandGroup = new TreeItem(TreeItem::TypeData::GroupCommand , CommandGroup_variant_temp ,parent_temp ) ;
parent_temp->appendChild(tree_item_CommandGroup);
endInsertRows();
return true;
}
catch (bad_alloc& ba)
{
endInsertRows();
return false;
}
}
return false;
}
bool CommandModel
::insertVirtualKeyboard( const QModelIndex & parent
) {
if(parent.isValid() )
{
try
{
auto parent_temp = static_cast<TreeItem*>(parent.internalPointer() );
auto row = parent_temp->Childcount();
beginInsertRows(parent,row,row);
VirtualKeyboard_variant_temp.setValue( new VirtualKeyboard );
auto tree_item_VirtualKeyboard = new TreeItem(TreeItem::TypeData::VirtualKeyboard , VirtualKeyboard_variant_temp ,parent_temp );
parent_temp->appendChild(tree_item_VirtualKeyboard);
endInsertRows();
return true;
}
catch (bad_alloc& ba)
{
endInsertRows();
return false;
}
}
return false;
}
bool CommandModel
::insertCommand(const QModelIndex & parent
) {
if(parent.isValid() )
{
try
{
auto parent_temp = static_cast<TreeItem*>(parent.internalPointer() );
auto row = parent_temp->Childcount();
beginInsertRows(parent,row,row);
Command_variant_temp.setValue( new KeyItemData );
auto tree_item_Command = new TreeItem(TreeItem::TypeData::Command , Command_variant_temp ,parent_temp );
parent_temp->appendChild(tree_item_Command);
endInsertRows();
return true;
}
catch (bad_alloc& ba)
{
endInsertRows();
return false;
}
}
return false;
}
bool CommandModel
::insertMacro(const MacroKey
& macro ,
const QModelIndex & parent
) {
if(parent.isValid() )
{
try
{
auto parent_temp = static_cast<TreeItem*>(parent.internalPointer() );
int row = parent_temp->Childcount();
beginInsertRows(parent,row,row);
MacroKey_variant_temp.setValue( new MacroKey(macro) );
auto tree_item_MacroKey = new TreeItem(TreeItem::TypeData::MacroKey , MacroKey_variant_temp ,parent_temp );
parent_temp->appendChild(tree_item_MacroKey);
endInsertRows();
return true;
}
catch (bad_alloc& ba)
{
endInsertRows();
return false;
}
}
return false;
}
bool CommandModel
::removeRows ( int first,
int last,
const QModelIndex & parent
) {
if (parent.isValid() )
{
beginRemoveRows (parent, first, last );
auto parent_temp = static_cast<TreeItem*>(parent.internalPointer() );
auto result = parent_temp->removeChild(first , last);
endRemoveRows ();
return result;
}
return false;
}
{
if (role == Qt::DisplayRole && Qt::EditRole && index.isValid() )
{
auto index_temp = static_cast<TreeItem*>(index.internalPointer() );
index_temp->setData(value);
return true;
}
return false;
}
{
if ( index.isValid() )
{
auto index_temp = static_cast<TreeItem*>(index.internalPointer() ) ;
auto parent_temp = index_temp->parent();
if(parent_temp == mTreeItemRoot)
return createIndex(parent_temp->row(), 0 , parent_temp);
}
}
QVariant CommandModel
::headerData(int section, Qt
::Orientation orientation,
int role
) const {
}
bool CommandModel
::hasChildren(const QModelIndex & parent
) const {
if(parent.isValid() )
{
auto parent_item = static_cast<TreeItem*>( parent.internalPointer() );
if(parent_item->Childcount() )
return true;
}
if(mTreeItemRoot->Childcount())
return true;
return false;
}
#include "CommandModel.h"
#include <new>
#include <QDebug>
//#include <QSize>
using std::unique_ptr;
using std::bad_alloc;
CommandModel::CommandModel(QObject * parent):mTreeItemRoot(nullptr)
{
setParent(parent);
try
{
mTreeItemRoot = new TreeItem(TreeItem::TypeData::Root,/*QVariantRootItem*/QVariant() );
}
catch (bad_alloc& ba ) { throw; }
}
CommandModel::~CommandModel()
{
delete mTreeItemRoot;
}
int CommandModel::rowCount ( const QModelIndex & parent ) const
{
if ( parent.isValid() )
{
TreeItem* ti_parent =static_cast<TreeItem*>(parent.internalPointer() );
return ti_parent->Childcount();
}
return mTreeItemRoot->Childcount();
}
int CommandModel::columnCount ( const QModelIndex & parent ) const
{
return 0;
}
QVariant CommandModel::data ( const QModelIndex & index, int role) const
{
if(index.isValid() )
{
if (role == Qt::DisplayRole || role == Qt::EditRole )
{
TreeItem* ti_index =static_cast<TreeItem*>(index.internalPointer() );
auto item_type = ti_index->getType();
switch( item_type )
{
case TreeItem::TypeData::GameProfile :
return QVariant(*(ti_index->data().value<GameProfileData*>()->getGameProfileTitle() ) );
case TreeItem::TypeData::GroupCommand :
return QVariant(ti_index->data().value<GroupCommand*>()->getGroupCommandName() );
case TreeItem::TypeData::VirtualKeyboard :
return QVariant(*(ti_index->data().value<VirtualKeyboard*>()->getVirtualKeyboardName() ) );
case TreeItem::TypeData::Command :
return QVariant(*(ti_index->data().value<KeyItemData*>()->getCommandName() ) );
case TreeItem::TypeData::MacroKey :
return QVariant(ti_index->data().value<MacroKey*>()->getMacroKeyName() );
}
}
else if(role == TypeDataRole)
{
auto ti_index =static_cast<TreeItem*>(index.internalPointer() );
QVariant qvariant_type_data;
qvariant_type_data.setValue(ti_index->getType() );
return qvariant_type_data;
}
}
return QVariant();
}
Qt::ItemFlags CommandModel::flags ( const QModelIndex & index ) const
{
if(index.isValid() )
return Qt::ItemIsEnabled | Qt::ItemIsEditable/*| Qt::ItemIsSelectable*/ | QAbstractItemModel::flags(index);
return 0;
}
QModelIndex CommandModel::index ( int row, int column, const QModelIndex & parent ) const
{
TreeItem *parentItem;
if (parent.isValid() && parent.column() != 0)
return QModelIndex();
if (!parent.isValid())
parentItem = mTreeItemRoot;
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());
TreeItem *childItem = parentItem->Child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
bool CommandModel::insertGameProfile()
{
auto row = mTreeItemRoot->Childcount();
if(row <= mTreeItemRoot->Childcount() && row >= 0 )
{
beginInsertRows(QModelIndex(),row,row);
try
{
QVariant GameProfile_Variant_temp;
GameProfile_Variant_temp.setValue( new GameProfileData );
auto tree_item_gameprofile = new TreeItem(TreeItem::TypeData::GameProfile , GameProfile_Variant_temp ,mTreeItemRoot ) ;
mTreeItemRoot->appendChild(tree_item_gameprofile);
endInsertRows();
return true;
}
catch (bad_alloc& ba)
{
endInsertRows();
return false;
}
}
return false;
}
bool CommandModel::insertCommandGroup( const QModelIndex & parent )
{
if(parent.isValid() )
{
try
{
auto parent_temp = static_cast<TreeItem*>(parent.internalPointer() );
auto row = parent_temp->Childcount();
beginInsertRows(parent,row,row);
QVariant CommandGroup_variant_temp;
CommandGroup_variant_temp.setValue( new GroupCommand );
auto tree_item_CommandGroup = new TreeItem(TreeItem::TypeData::GroupCommand , CommandGroup_variant_temp ,parent_temp ) ;
parent_temp->appendChild(tree_item_CommandGroup);
endInsertRows();
return true;
}
catch (bad_alloc& ba)
{
endInsertRows();
return false;
}
}
return false;
}
bool CommandModel::insertVirtualKeyboard( const QModelIndex & parent )
{
if(parent.isValid() )
{
try
{
auto parent_temp = static_cast<TreeItem*>(parent.internalPointer() );
auto row = parent_temp->Childcount();
beginInsertRows(parent,row,row);
QVariant VirtualKeyboard_variant_temp;
VirtualKeyboard_variant_temp.setValue( new VirtualKeyboard );
auto tree_item_VirtualKeyboard = new TreeItem(TreeItem::TypeData::VirtualKeyboard , VirtualKeyboard_variant_temp ,parent_temp );
parent_temp->appendChild(tree_item_VirtualKeyboard);
endInsertRows();
return true;
}
catch (bad_alloc& ba)
{
endInsertRows();
return false;
}
}
return false;
}
bool CommandModel::insertCommand(const QModelIndex & parent )
{
if(parent.isValid() )
{
try
{
auto parent_temp = static_cast<TreeItem*>(parent.internalPointer() );
auto row = parent_temp->Childcount();
beginInsertRows(parent,row,row);
QVariant Command_variant_temp;
Command_variant_temp.setValue( new KeyItemData );
auto tree_item_Command = new TreeItem(TreeItem::TypeData::Command , Command_variant_temp ,parent_temp );
parent_temp->appendChild(tree_item_Command);
endInsertRows();
return true;
}
catch (bad_alloc& ba)
{
endInsertRows();
return false;
}
}
return false;
}
bool CommandModel::insertMacro(const MacroKey& macro , const QModelIndex & parent )
{
if(parent.isValid() )
{
try
{
auto parent_temp = static_cast<TreeItem*>(parent.internalPointer() );
int row = parent_temp->Childcount();
beginInsertRows(parent,row,row);
QVariant MacroKey_variant_temp;
MacroKey_variant_temp.setValue( new MacroKey(macro) );
auto tree_item_MacroKey = new TreeItem(TreeItem::TypeData::MacroKey , MacroKey_variant_temp ,parent_temp );
parent_temp->appendChild(tree_item_MacroKey);
endInsertRows();
return true;
}
catch (bad_alloc& ba)
{
endInsertRows();
return false;
}
}
return false;
}
bool CommandModel::removeRows ( int first, int last, const QModelIndex & parent )
{
if (parent.isValid() )
{
beginRemoveRows (parent, first, last );
auto parent_temp = static_cast<TreeItem*>(parent.internalPointer() );
auto result = parent_temp->removeChild(first , last);
endRemoveRows ();
return result;
}
return false;
}
bool CommandModel::setData ( const QModelIndex & index, const QVariant & value, int role )
{
if (role == Qt::DisplayRole && Qt::EditRole && index.isValid() )
{
auto index_temp = static_cast<TreeItem*>(index.internalPointer() );
index_temp->setData(value);
return true;
}
return false;
}
QModelIndex CommandModel::parent ( const QModelIndex & index ) const
{
if ( index.isValid() )
{
auto index_temp = static_cast<TreeItem*>(index.internalPointer() ) ;
auto parent_temp = index_temp->parent();
if(parent_temp == mTreeItemRoot)
return QModelIndex();
return createIndex(parent_temp->row(), 0 , parent_temp);
}
return QModelIndex();
}
QVariant CommandModel::headerData(int section, Qt::Orientation orientation,int role ) const
{
return QVariant();
}
bool CommandModel::hasChildren(const QModelIndex & parent ) const
{
if(parent.isValid() )
{
auto parent_item = static_cast<TreeItem*>( parent.internalPointer() );
if(parent_item->Childcount() )
return true;
}
if(mTreeItemRoot->Childcount())
return true;
return false;
}
To copy to clipboard, switch view to plain text mode
Bookmarks