//------------------------------------------------------------------------------
void EstimateDetailsForm::populate(const QModelIndex& index)
{
	Estimate* estimate = static_cast<Estimate*>(index.internalPointer());

	// Populate fields with selected estimate's values
	nameField->setText(estimate->estimateName());
	descriptionField->setText(estimate->estimateDescription());
	typeField->setCurrentIndex(typeField->findData(estimate->estimateType()));
	amountField->setValue(estimate->estimatedAmount());
	finishedField->setChecked(estimate->isActivityFinished());

	int offset = estimate->activityDueDateOffset();
	QDate dueDate = period->startDate().addDays(offset);
	if (dueDate.isNull())
	{
		clearDueDate();
	}
	else
	{
		dueDateField->setDate(dueDate);
	}

	// Enable fields that are applicable for all non-root estimates
	nameField->setEnabled(estimate->parentEstimate());
	descriptionField->setEnabled(estimate->parentEstimate());
	// Disable fields that aren't applicable for parents
	amountField->setEnabled(estimate->childCount() == 0);
	dueDateField->setEnabled(estimate->childCount() == 0);
	clearDueDateButton->setEnabled(estimate->childCount() == 0);
	finishedField->setEnabled(estimate->childCount() == 0);

	// Type can only be changed at top-level estimates
	Estimate* parent = estimate->parentEstimate();
	if ( ! parent) // Parent is root
	{
		typeField->setEnabled(false);
	}
	else if ( ! parent->parentEstimate()) // Parent of parent is root
	{
		typeField->setEnabled(true);
	}
	else // Not top-level
	{
		typeField->setEnabled(false);
	}

	// Clear fields that aren't applicable for parents
	if (estimate->childCount() != 0)
	{
		amountField->clear();
		clearDueDate();
		finishedField->setChecked(false);
	}
}