/* MSHV HvRadList
 * Copyright 2015 Hrisimir Hristov, LZ2HV
 * (Edited by Harper Innes, VK1TTY - to remove Gendered Language and Replace with Non-Gendered language)  NOTE:May be used under the terms of the GNU General Public License (GPL)
 */
#include "radlist.h"

#include <QHeaderView>
//#include <QtGui>

/*HvHeaderStInfo::HvHeaderStInfo( Qt::Orientation orientation, QWidget *parent )
        : QHeaderView(orientation, parent)
{

    //setResizeMode(QHeaderView::Interactive);
}

HvHeaderStInfo::~HvHeaderStInfo()
{}*/

#define _MODESTRFORFREQ_H_
#include "../../config_band_all.h"

HvRadList::HvRadList(bool f,QWidget *parent)
        : QTreeView(parent)
{
	dsty = f;
    //setMinimumSize(100, 206);
    //setMinimumSize(100, 192);//2.68 192
    setMinimumSize(100,522);//2.68 522
    //this->setMaximumHeight(136);
    //setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);

    //setWordWrap(true);
    //this->setTextElideMode(Qt::ElideMiddle);
    setRootIsDecorated(false);

    setModel(&model);
    //HvHeaderStInfo *THvHeader = new HvHeaderStInfo(Qt::Horizontal);
    QHeaderView *THvHeader = new QHeaderView(Qt::Horizontal);

    //connect(THvHeader, SIGNAL(sectionPressed(int)),this,SIGNAL(SortClicked(int)));
    //THvHeader->setre

    setHeader(THvHeader);
    setAllColumnsShowFocus(true);  // za da o4ertae delia row

    //setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    setSelectionBehavior(QAbstractItemView::SelectRows);
    //setSelectionMode(QAbstractItemView::ExtendedSelection);
    //setSelectionMode(QAbstractItemView::ExtendedSelection);//QAbstractItemView::SingleSelection
    //setSelectionMode(QAbstractItemView::SingleSelection);


    setDragDropMode(QAbstractItemView::NoDragDrop);//QAbstractItemView::InternalMove
    setDragEnabled (false);
    // setAcceptDrops(true);
    setVerticalScrollMode(QAbstractItemView::ScrollPerItem);// tova e za na mi6kata whella i dragdrop QAbstractItemView::ScrollPerPixel
    setAutoScroll(false); // s tova tarsi samo v 1 colona i za scroll pri drag
    /*   m_file = new QMenu(this);
       QAction *file_info = new QAction("File Info", this);
       m_file->addAction(file_info);
       connect(file_info, SIGNAL(triggered()), this, SLOT(File_Info()));
       m_file->addSeparator();
       QAction *clr_sel = new QAction("Clear Selected", this);
       m_file->addAction(clr_sel);
       connect(clr_sel, SIGNAL(triggered()), this, SLOT(DellBtSelItems()));
       TFileEditInfoHV = new FileEditInfoHV(this);
       connect(TFileEditInfoHV, SIGNAL(TagChange(QString)), this, SLOT(TagChange_lis(QString)));*/

    QPalette PaletteL = palette();
    PaletteL.setColor(QPalette::Shadow, QColor(120,120,120));// zaradi bordera okantovaneto na lista
    setPalette(PaletteL);

    QStringList list_A;
    list_A <<tr("Band")<<tr("Antenna Description")<<tr("Mode")<<tr("Frequency In")+" Hz";
    model.setHorizontalHeaderLabels(list_A);

    THvHeader->setSectionResizeMode(QHeaderView::Fixed);//qt5


    //THvHeader->setResizeMode(2, QHeaderView::Fixed);
    THvHeader->resizeSection(0, 100);//80
    THvHeader->resizeSection(1, 150);//150
    THvHeader->resizeSection(2, 70);//70
    THvHeader->resizeSection(3, 155);//120


    //bavi THvHeader->setSectionResizeMode(0, QHeaderView::Stretch);//1.81 bavi for font
    THvHeader->setSectionResizeMode(1, QHeaderView::Stretch);//qt5
    //bavi THvHeader->setSectionResizeMode(3, QHeaderView::Stretch);//1.81 bavi for font


    //THvHeader->hideSection(12);
    //setSortingEnabled(true);
    //2.31 false for "\n" jt6m=6 jtms=1
	setUniformRowHeights(true);//2.30 fast view

    //setSelectionMode(QAbstractItemView::SingleSelection);
    //setSelectionMode(QAbstractItemView::ExtendedSelection);

    //QFont font("arial", 10);  // times new roman // slaga se tuk zaradi resize coll
    //setFont(font);

    //QFont f_t = font();
    //f_t.setPointSize(10);
    //setFont(f_t);

    //ActiveIndex = -1;
    //ActiveRowText = QColor(255,0,0);
    //ActiveRowBackg = QColor(0,255,0);
    //clipboard = QApplication::clipboard();
    // DrawDropLinePosY = -1;

    //this->setAlternatingRowColors(true);
    //this->resize(100,100);
    f_row_color = false;
}

HvRadList::~HvRadList()
{}
/*
void HvLogList::SetFocus()
{
	this->show();
}
void HvRadList::resizeEvent(QResizeEvent * event)
{
    event->ignore();
    //else QTreeView::resizeEvent(event);
}*/
void HvRadList::paintEvent(QPaintEvent *event)
{
    QPainter painter(viewport());
    //QModelIndex index = model.index(ActiveIndex, 1, QModelIndex());
    //painter.fillRect(QRect(0,visualRect(index).y(),width(),visualRect(index).height()), ActiveRowBackg);
    QTreeView::paintEvent(event);


    //QTreeView::paintEvent(event);
    //QPainter painter(viewport());

    painter.save();
    QColor cc = QColor(200,200,200);
    if (dsty) cc = QColor(80,80,80);
    painter.setPen(QPen(cc, 1, Qt::DashLine));

    //qDebug()<<model.columnCount();
    for (int i = 1; i<model.columnCount(); ++i)
        painter.drawLine(header()->sectionPosition(i),1,header()->sectionPosition(i),height());

    painter.restore();
}
/*void HvRadList::drawRow(QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
    QStyleOptionViewItem opt = option;

    //if (index.row() == ActiveIndex)
        opt.palette.setColor(QPalette::HighlightedText, ActiveRowText);
    else
        opt.palette.setBrush(QPalette::HighlightedText, palette().highlightedText());

    if (index.row() == ActiveIndex)
        opt.palette.setColor(QPalette::Text, ActiveRowText);
    else
        opt.palette.setBrush(QPalette::Text, palette().text());

    if (index.row() == ActiveIndex)
        opt.palette.setColor(QPalette::Highlight, ActiveRowBackg);
    else
        opt.palette.setBrush(QPalette::Highlight, palette().highlight());//

    QTreeView::drawRow(painter, opt, index);
}*/
/*void HvRadList::SetEditRow(int numbr)
{
    ActiveIndex = numbr;
    viewport()->update();
}*/
void HvRadList::InsertItem_hv(QStringList list)
{   
    if (!list.isEmpty())
    {
    	QList<QStandardItem *> qlsi;
        int k = 0;
        for (QStringList::iterator it =  list.begin(); it != list.end(); ++it)
        {
            QStandardItem *item = new QStandardItem(QString(*it));

            if (k==0)
                item->setTextAlignment(Qt::AlignCenter);
            item->setEditable(false);

            if (!f_row_color)
            {
            	QColor cc = QColor(255,255,255);
                if (dsty) cc = QColor(25,25,25); 
            	item->setBackground(cc);
           	}

            else
            {
            	if (dsty) item->setBackground(QColor(45,45,45));
                else item->setBackground(QColor(212,222,242)); //rgb            	
           	}
            qlsi.append(item);

            if (k==1)//sluzebno
            {
                k++;
                QStandardItem *item_mod;
                QString mod;
                for (int i  = 0; i < COUNT_FREQ_MODES; i++)
                {
                    mod.append(ModeStrForFerq[i]);
                    if (i<COUNT_FREQ_MODES-1)
                        mod.append("\n");
                }
                item_mod = new QStandardItem(mod);
                if (!f_row_color)
                    item_mod->setBackground(QColor(255,255,255));
                else
                    item_mod->setBackground(QColor(212,222,242));
                qlsi.append(item_mod);
            }

            k++;
        }
        model.insertRow(model.rowCount(), qlsi);

        if (!f_row_color)
            f_row_color = true;
        else
            f_row_color = false;
        //this->setCurrentIndex(model.index(model.rowCount()-1,0));
        //this->scrollTo(model.index(model.rowCount()-1,0));
    }
    //this->setCurrentIndex(model.index(model.rowCount(), 1, QModelIndex()));
}
void HvRadList::SetItem_hv(QStringList list,int index_edit)//2.57 remove  new QStandardItem
{	
    int column_numb = 0;
    for (QStringList::iterator it =  list.begin(); it != list.end(); ++it)
    {
        //QStandardItem *item = new QStandardItem(QString(*it));
		QStandardItem *item = model.itemFromIndex(model.index(index_edit, column_numb));
		item->setText(QString(*it));

        if (column_numb==0)
            item->setTextAlignment(Qt::AlignCenter);
        item->setEditable(false);

        QBrush bcolor = model.item(index_edit,column_numb)->background();
        item->setBackground(bcolor);
        model.setItem(index_edit,column_numb,item);
        
        if (column_numb==1)//sluzebno
        {
            column_numb++;
            QStandardItem *item_mod = model.itemFromIndex(model.index(index_edit, column_numb));
            QString mod;
            for (int i  = 0; i < COUNT_FREQ_MODES; i++)
            {
                mod.append(ModeStrForFerq[i]);
                if (i<COUNT_FREQ_MODES-1)
                    mod.append("\n");
            }
            //item_mod = new QStandardItem(mod);
            item_mod->setText(mod);
            item_mod->setBackground(bcolor);
            model.setItem(index_edit,column_numb,item_mod);
        }
        column_numb++;
    }   
    //this->setCurrentIndex(model.index(index_edit, 1, QModelIndex()));// ina4e ne refreshva 1 row    
}

void HvRadList::Clear_List()
{ 
    for (int i = model.rowCount()-1; i >= 0; --i) // ok pravilno hv
        model.removeRow(i);
}
/*void HvRadList::DeleteSel()
{
    if (!selectionModel()->selection().empty())
    {
        //setAutoScroll(false); // za da ne skrolira pri iztrivane kam poslednia markiran
        QItemSelection selection( selectionModel()->selection() );

        QList<int> rows;
        foreach( const QModelIndex & index, selection.indexes() )
        {
            rows.append( index.row() );
        }

        //q_Sort( rows );
        std::sort(rows.begin(),rows.end());

        int prev = -1;
        for ( int i = rows.count() - 1; i >= 0; i -= 1 )
        {
            int current = rows[i];
            if ( current != prev )
            {
                model.removeRows( current, 1 );
                prev = current;
            }
        }
        //setAutoScroll(true);  // za da ne skrolira pri iztrivane kam poslednia markiran
    }
}*/
/*void HvRadList::keyPressEvent(QKeyEvent * event)
{   
    if (event->matches(QKeySequence::Copy) && !selectionModel()->selection().empty() )
    {
        QModelIndexList lst = selectionModel()->selectedRows(0);
        QList<int> rows;
        foreach( const QModelIndex & index, lst )
        {
            rows.append( index.row() );
        }
        q_Sort( rows );
        QString str_n;
        for (int j = 0; j < rows.count(); j++)
        {

            for (int i = 0; i < model.columnCount(); i++)
            {
                str_n.append(model.item(rows[j], i)->text());
                if (i < model.columnCount()-1)
                    str_n.append(" ");
            }
            if (j < rows.count()-1)
                str_n.append("\n");
        }
        clipboard->setText(str_n);
    }
    else // vazno da e else ina4e kopira samo edna kletka
    
    QTreeView::keyPressEvent(event); // za da raboti keybord shortkeys Application
}*/
void HvRadList::mouseDoubleClickEvent(QMouseEvent * event)
{
    if (event->button() == Qt::LeftButton)
    {
        if (selectionModel()->selection().empty())
        {
            return;
        }
        QModelIndex index = selectionModel()->currentIndex();
        int index_n = index.row();
        QStringList list;

		// no crash ->model.item(index.row(),1)->text();//2.48
        list << index.sibling(index.row(),0).data().toString(); //band
        list << index.sibling(index.row(),1).data().toString(); //ant
        list << index.sibling(index.row(),3).data().toString(); //frq
        /*list << model.item(index.row(),0)->text(); //band
        list << model.item(index.row(),1)->text(); //ant
        list << model.item(index.row(),3)->text(); //frq*/

        emit EmitDoubleClick(list,index_n);
    }
    QTreeView::mousePressEvent(event);
}





