#include "cctrllist.h"

CCtrlList::CCtrlList(QWidget *parent) : QListWidget(parent)
{

    setEditTriggers(QListWidget::AnyKeyPressed);
    setAlternatingRowColors(true);
}

void CCtrlList::keyPressEvent(QKeyEvent *event)
{
    switch(event->key())
    {
    case Qt::Key_Return:
    {
        int row= currentRow();
        if(row<0) return;
        QListWidgetItem *item=new QListWidgetItem();
        item->setFlags(Qt::ItemIsEnabled|
                                          Qt::ItemIsEditable|
                                          Qt::ItemIsSelectable);
        item->setText("");
        insertItem(row+1,item);
        setCurrentRow(row+1);
        emit Listedit(row+1);
        break;
    }
    case Qt::Key_Down:
    {
        int row= currentRow();
        if(row<0) return;
        if(row<count()-1)
        {
            QListWidgetItem* itemcur=item(row);
            QListWidgetItem* itemnext=item(row+1);
            QString s=item(row)->text();
            itemcur->setText(itemnext->text());
            itemnext->setText(s);
            setCurrentRow(row+1);
            emit Listedit(row+1);
        }
        break;
    }

    case Qt::Key_Up:
    {
        int row= currentRow();
        if(row<0) return;
        if(row>0)
        {
            QListWidgetItem* itemcur=item(row);
            QListWidgetItem* itempre=item(row-1);
            QString s=item(row)->text();
            itemcur->setText(itempre->text());
            itempre->setText(s);
            setCurrentRow(row-1);
            emit Listedit(row-1);
        }
        break;
    }
    case Qt::Key_Delete:
    {
        int row=  currentRow();
        if(row!=-1)
        {
            QListWidgetItem *aitem= takeItem(row);
            delete aitem;
            emit Listedit(row);
        }
        break;
    }
    default:
        QListWidget::keyPressEvent(event);
    }
}

