#include "objects_view.h"

#include <QScrollBar>
#include <QTreeWidget>


enum ColumnObjects {
	coClassName,
	coName,
	coParents,
	coQueuedEvents,
};


ObjectsView::ObjectsView(QWidget * parent): QWidget(parent) {
	setupUi(this);
}


ObjectsView::~ObjectsView() {}


void ObjectsView::changeEvent(QEvent * e) {
	QWidget::changeEvent(e);
	switch (e->type()) {
	case QEvent::LanguageChange: retranslateUi(this); break;
	default: break;
	}
}


void ObjectsView::showObjects(const PIVector<PIIntrospection::ObjectInfo> & objects) {
	QHash<QString, int> stat;

	int vpos = treeObjects->verticalScrollBar()->value();
	treeObjects->clear();
	for (const auto & i: objects) {
		stat[PI2QString(i.classname)]++;
		QTreeWidgetItem * ti = new QTreeWidgetItem();
		ti->setText(coClassName, PI2QString(i.classname));
		ti->setText(coName, PI2QString(i.name));
		ti->setText(coParents, PI2QString(i.parents.join(":")));
		ti->setText(coQueuedEvents, QString::number(i.queued_events));
		treeObjects->addTopLevelItem(ti);
	}
	treeObjects->verticalScrollBar()->setValue(vpos);

	vpos = treeObjectsStat->verticalScrollBar()->value();
	treeObjectsStat->clear();
	for (QHash<QString, int>::const_iterator i = stat.begin(); i != stat.end(); ++i) {
		QTreeWidgetItem * ti = new QTreeWidgetItem();
		ti->setText(0, i.key());
		ti->setText(1, QString::number(i.value()));
		treeObjectsStat->addTopLevelItem(ti);
	}
	treeObjectsStat->verticalScrollBar()->setValue(vpos);
}
