#include "ctextedit.h"
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QDir>
#include <QFileInfo>


CTextEdit::CTextEdit(QWidget *parent)
    : QTextEdit(parent)
{
    this->setAcceptDrops(true);
    mDragListFilter.clear();
}

CTextEdit::CTextEdit(const QString & str, QWidget *parent)
    : QTextEdit(str, parent)
{
    this->setAcceptDrops(true);
    mDragListFilter.clear();
}

void CTextEdit::setDragFilter(QStringList list)
{
    mDragListFilter = list;
}

void CTextEdit::dropEvent(QDropEvent *e)
{
    QList<QUrl> urls = e->mimeData()->urls();
    if(urls.isEmpty())
        return ;
    // qDebug() << urls.size();
    foreach (QUrl u, urls)
    {
        QString filepath = u.toLocalFile();        
        if(mDragListFilter.empty())
        {
            this->append(filepath);
        }
        else
        {
            QFileInfo fileInfo(filepath);
            QString completeSuffix = fileInfo.completeSuffix();

            if(mDragListFilter.contains(completeSuffix))
            {
                this->append(filepath);
            }
        }

//        if(fileInfo.isFile()) {
//            QString completeSuffix = fileInfo.completeSuffix();
//            if(completeSuffix.compare("exe") == 0) {
//                m_pProgramPathLineEdit->setText(fileInfo.absoluteFilePath());
//            } else {
//                m_pWorkDirLineEdit->setText(fileInfo.absolutePath());
//                m_pProgramParameteLineEdit->setText(fileInfo.fileName());
//            }
//            return;
//        } else if(fileInfo.isDir()) {
//            m_pWorkDirLineEdit->setText(filepath);
//            return;
//        }

    }
}

void CTextEdit::dragEnterEvent(QDragEnterEvent *e)
{
    e->acceptProposedAction();
}
