MainWindow::MainWindow()
{
	QAction *exitAct = new QAction(tr("E&xit"), this);
	exitAct->setShortcuts(QKeySequence::Quit);
	connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));

	QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
	fileMenu->addSeparator();
	fileMenu->addAction(exitAct);

	columnsMenu = menuBar()->addMenu(tr("&Columns"));
	connect(columnsMenu, SIGNAL(aboutToShow()), this, SLOT(showColumnsMenu()));

	model = new PeerDrive::FolderModel(this);
	connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)), this,
		SLOT(modelRowsInserted(QModelIndex,int,int)));
	connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this,
		SLOT(modelDataChanged(QModelIndex,QModelIndex)));
	connect(model, SIGNAL(modelReset()), this, SLOT(modelReset()));

	treeView = new QTreeView;
	treeView->setModel(model);
	treeView->sortByColumn(0, Qt::AscendingOrder);
	treeView->setSortingEnabled(true);
	connect(treeView, SIGNAL(activated(QModelIndex)), this,
		SLOT(itemActivated(QModelIndex)));

	setCentralWidget(treeView);
	setWindowTitle(QObject::tr("Simple Tree Model"));
	setUnifiedTitleAndToolBarOnMac(true);
}
Playlist::Playlist(QWidget *parent) :
	QTableWidget(parent), track(-1)
{
	// Initialize values for the Header (label and horizontal resize mode)
	QStringList labels = (QStringList() << "#" << tr("Title") << tr("Album") << tr("Length") << tr("Artist") << tr("Rating") << tr("Year"));
	const QStringList untranslatedLabels = (QStringList() << "#" << "Title" << "Album" << "Length" << "Artist" << "Rating" << "Year");
	// Test: 0 = Fixed, n>0 = real ratio for each column
	const QList<int> ratios(QList<int>() << 0 << 5 << 4 << 1 << 3 << 0 << 0);

	this->setColumnCount(labels.size());
	this->setColumnHidden(5, true);
	this->setColumnHidden(6, true);
	this->setShowGrid(false);
	Settings *settings = Settings::getInstance();
	this->setStyleSheet(settings->styleSheet(this));
	this->verticalScrollBar()->setStyleSheet(settings->styleSheet(this->verticalScrollBar()));
	this->setAlternatingRowColors(settings->colorsAlternateBG());

	// Select only one row, not cell by cell
	this->setSelectionMode(QAbstractItemView::ExtendedSelection);
	this->setSelectionBehavior(QAbstractItemView::SelectRows);
	this->setItemDelegate(new NoFocusItemDelegate(this));

	/// test
	this->setAcceptDrops(true);

	verticalHeader()->setVisible(false);
	this->setHorizontalHeader(new QHeaderView(Qt::Horizontal, this));
	horizontalHeader()->setStyleSheet(settings->styleSheet(horizontalHeader()));
	horizontalHeader()->setContextMenuPolicy(Qt::CustomContextMenu);
	horizontalHeader()->setHighlightSections(false);
	horizontalHeader()->setMovable(true);

	this->installEventFilter(horizontalHeader());

	// Context menu on header of columns
	QList<QAction*> actionColumns;
	columns = new QMenu(this);

	for (int i = 0; i < labels.size(); i++) {
		QString label = labels.at(i);
		QTableWidgetItem *item = new QTableWidgetItem(label);

		// Stores original text to switch between translations on the fly
		// Might evolve latter, start to be "unreadable"
		item->setData(Qt::UserRole+1, untranslatedLabels.at(i));
		item->setData(Qt::UserRole+2, ratios.at(i));
		this->setHorizontalHeaderItem(i, item);

		// Match actions with columns using index of labels
		QAction *actionColumn = new QAction(label, this);
		actionColumn->setData(i);
		actionColumn->setEnabled(actionColumn->text() != tr("Title"));
		actionColumn->setCheckable(true);
		actionColumn->setChecked(!isColumnHidden(i));
		actionColumns.append(actionColumn);

		// Then populate the context menu
		columns->addAction(actionColumn);
	}

	// Load columns state
	QByteArray state = settings->value("playlistColumnsState").toByteArray();
	if (!state.isEmpty()) {
		horizontalHeader()->restoreState(state);
		for (int i = 0; i < horizontalHeader()->count(); i++) {
			bool hidden = horizontalHeader()->isSectionHidden(i);
			setColumnHidden(i, hidden);
			columns->actions().at(i)->setChecked(!hidden);
		}
	}

	// Link this playlist with the Settings instance to change fonts at runtime
	connect(settings, SIGNAL(currentFontChanged()), this, SLOT(highlightCurrentTrack()));

	// Change track
	// no need to cast parent as a TabPlaylist instance
	connect(this, SIGNAL(itemDoubleClicked(QTableWidgetItem*)), parent, SLOT(changeTrack(QTableWidgetItem*)));

	// Hide the selected column in context menu
	connect(horizontalHeader(), SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showColumnsMenu(QPoint)));
	connect(columns, SIGNAL(triggered(QAction*)), this, SLOT(toggleSelectedColumn(QAction*)));
	connect(horizontalHeader(), SIGNAL(sectionMoved(int,int,int)), this, SLOT(saveColumnsState(int,int,int)));
}