#include <QApplication>
#include <QWidget>
#include <QTableWidget>
#include <QVBoxLayout>
#include <QFont>
#include <QFontDatabase>

#include "helpers.hh"
#include "listfont.h"

#include <map>
#include <iostream>

#include "pdgmap.cc"

int
main(int argc, char* argv[])
{
  QApplication app(argc, argv);
  QWidget win;
  QTableWidget list(&win);
  QVBoxLayout layout(&win);
  layout.addWidget(&list);

  win.setMinimumWidth(800);
  win.setMinimumHeight(800);

  pdgmap_init();

  //  int fontid=
  QByteArray b64(listfont_b64, strlen(listfont_b64));
  QFontDatabase::addApplicationFontFromData(QByteArray::fromBase64(b64));
  //std::cout << qPrintable(QFontDatabase::applicationFontFamilies(fontid)[0]) << std::endl;
  QFont font;
  font.setFamily("Lucida Sans Unicode");
  list.setFont(font);
  QFont bigfont(list.font());
  bigfont.setPointSize((bigfont.pointSize()*5)/4);

  list.setSortingEnabled(true);
  list.setColumnCount(7);
  list.setItemDelegateForColumn(0, new DoubleItemDelegate(0));
  //  list.setItemDelegateForColumn(2, new RichTextDelegate);
  //  list.setItemDelegateForColumn(5, new RichTextDelegate);
  list.setHorizontalHeaderLabels(QStringList("code") << "plain" << "rich" << "notags" << "-plain" << "-rich" << "-notags");

  int count=0;
  for (std::map<int, std::pair<QString, QString> >::iterator it=pdgmap.begin();
       it!=pdgmap.end(); it++)
    if (it->first>0)
      count++;
  list.setRowCount(count);
  int i=0;
  for (std::map<int, std::pair<QString, QString> >::iterator it=pdgmap.begin();
       it!=pdgmap.end(); it++)
    {
      if (it->first>0)
	{
	  QTableWidgetItem* item=new QTableWidgetItem;
	  item->setData(Qt::DisplayRole, it->first);
	  item->setFlags(item->flags() &  ~Qt::ItemIsEditable);
	  list.setItem(i, 0, item);
	  item=new QTableWidgetItem;
	  item->setData(Qt::DisplayRole, it->second.first);
	  item->setFlags(item->flags() &  ~Qt::ItemIsEditable);
	  list.setItem(i, 1, item);
	  item=new QTableWidgetItem;
	  QString rich=it->second.second;
	  if (rich[0].isLower() && rich.length()<6)
	    item->setFont(bigfont);
	  item->setData(Qt::DisplayRole, it->second.second);
	  item->setFlags(item->flags() &  ~Qt::ItemIsEditable);
	  list.setItem(i, 2, item);
	  item=new QTableWidgetItem;
	  item->setData(Qt::DisplayRole, it->second.second.remove(QRegExp("<[^>]*>")));
	  item->setFlags(item->flags() &  ~Qt::ItemIsEditable);
	  list.setItem(i, 3, item);
	  std::map<int, std::pair<QString, QString> >::iterator antiit;
	  if ((antiit=pdgmap.find(-it->first))!=pdgmap.end())
	    {
	      item=new QTableWidgetItem;
	      item->setData(Qt::DisplayRole, antiit->second.first);
	      item->setFlags(item->flags() &  ~Qt::ItemIsEditable);
	      list.setItem(i, 4, item);
	      item=new QTableWidgetItem;
	      QString rich=antiit->second.second;
	      if (rich[0].isLower() && rich.length()<6)
		{
		  item->setFont(bigfont);
		}
	      item->setData(Qt::DisplayRole, antiit->second.second);
	      item->setFlags(item->flags() &  ~Qt::ItemIsEditable);
	      list.setItem(i, 5, item);
	      item=new QTableWidgetItem;
	      item->setData(Qt::DisplayRole, antiit->second.second.remove(QRegExp("<[^>]*>")));
	      item->setFlags(item->flags() &  ~Qt::ItemIsEditable);
	      list.setItem(i, 6, item);
	    }
	    i++;
    }
    }
  win.show();
  app.exec();
}
