Methods AddEmpty, AddEmptyAfter.

--HG--
branch : feature
This commit is contained in:
Roman Telezhynskyi 2015-07-21 20:26:33 +03:00
parent f6ec3ecb95
commit b79807a8ef
4 changed files with 87 additions and 35 deletions

View File

@ -360,8 +360,6 @@ void TMainWindow::AddCustom()
{
ui->tableWidget->setFocus(Qt::OtherFocusReason);
ui->tableWidget->blockSignals(true);
const qint32 currentRow = ui->tableWidget->rowCount();
ui->tableWidget->insertRow( currentRow );
qint32 num = 1;
QString name;
@ -371,7 +369,21 @@ void TMainWindow::AddCustom()
num++;
} while (data->IsUnique(name) == false);
const int id = m->AddEmptyMeasurement(name);
qint32 currentRow;
if (ui->tableWidget->currentRow() == -1)
{
currentRow = ui->tableWidget->rowCount();
ui->tableWidget->insertRow( currentRow );
m->AddEmpty(name);
}
else
{
currentRow = ui->tableWidget->currentRow()+1;
ui->tableWidget->insertRow( currentRow );
QTableWidgetItem *nameField = ui->tableWidget->item(ui->tableWidget->currentRow(), 0);
m->AddEmptyAfter(nameField->text(), name);
}
VMeasurement *meash;
if (mType == MeasurementsType::Standard)
@ -380,13 +392,13 @@ void TMainWindow::AddCustom()
}
else
{
meash = new VMeasurement(data, id, name, 0, "0");
meash = new VMeasurement(data, currentRow, name, 0, "0");
}
data->AddVariable(name, meash);
if (mType == MeasurementsType::Individual)
{
AddCell(name, currentRow, 0, id); // name
AddCell(name, currentRow, 0); // name
AddCell("0", currentRow, 1); // calculated value
AddCell("0", currentRow, 2); // formula
}
@ -758,20 +770,16 @@ bool TMainWindow::MaybeSave()
}
//---------------------------------------------------------------------------------------------------------------------
void TMainWindow::AddCell(const QString &text, int row, int column, int id)
void TMainWindow::AddCell(const QString &text, int row, int column)
{
QTableWidgetItem *item = new QTableWidgetItem(text);
item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
// set the item non-editable (view only), and non-selectable
Qt::ItemFlags flags = item->flags();
flags &= ~(Qt::ItemIsEditable); // reset/clear the flag
item->setFlags(flags);
if (mType == MeasurementsType::Individual && id >= 0)
{
item->setData(Qt::UserRole, id);
}
ui->tableWidget->setItem(row, column, item);
}

View File

@ -70,6 +70,7 @@ private slots:
void SaveBirthDate(const QDate & date);
void SaveNotes();
void ReadOnly(bool ro);
void AddCustom();
void AddKnown();
@ -105,7 +106,7 @@ private:
bool MaybeSave();
void AddCell(const QString &text, int row, int column, int id = -1);
void AddCell(const QString &text, int row, int column);
QComboBox *SetGradationList(const QString &label, const QStringList &list);
void SetDefaultHeight(int value);

View File

@ -61,8 +61,7 @@ const QString VMeasurements::SexUnknown = QStringLiteral("unknown");
VMeasurements::VMeasurements(VContainer *data)
:VDomDocument(),
data(data),
type(MeasurementsType::Unknown),
id(-1)
type(MeasurementsType::Unknown)
{
SCASSERT(data != nullptr)
}
@ -71,8 +70,7 @@ VMeasurements::VMeasurements(VContainer *data)
VMeasurements::VMeasurements(Unit unit, VContainer *data)
:VDomDocument(),
data(data),
type(MeasurementsType::Individual),
id(-1)
type(MeasurementsType::Individual)
{
SCASSERT(data != nullptr);
@ -83,8 +81,7 @@ VMeasurements::VMeasurements(Unit unit, VContainer *data)
VMeasurements::VMeasurements(Unit unit, int baseSize, int baseHeight, VContainer *data)
:VDomDocument(),
data(data),
type(MeasurementsType::Standard),
id(-1)
type(MeasurementsType::Standard)
{
SCASSERT(data != nullptr);
@ -97,29 +94,30 @@ VMeasurements::~VMeasurements()
}
//---------------------------------------------------------------------------------------------------------------------
int VMeasurements::AddEmptyMeasurement(QString &name)
void VMeasurements::AddEmpty(const QString &name)
{
QDomElement element = createElement(TagMeasurement);
const QDomElement element = MakeEmpty(name);
SetAttribute(element, AttrName, name);
SetAttribute(element, AttrValue, QString("0"));
const QDomNodeList list = elementsByTagName(TagBodyMeasurements);
list.at(0).appendChild(element);
}
if (type == MeasurementsType::Standard)
//---------------------------------------------------------------------------------------------------------------------
void VMeasurements::AddEmptyAfter(const QString &after, const QString &name)
{
const QDomElement element = MakeEmpty(name);
const QDomElement sibling = FindM(after);
const QDomNodeList list = elementsByTagName(TagBodyMeasurements);
if (sibling.isNull())
{
SetAttribute(element, AttrSizeIncrease, QString("0"));
SetAttribute(element, AttrHeightIncrease, QString("0"));
list.at(0).appendChild(element);
}
else
{
++id;
SetAttribute(element, AttrId, id);
SetAttribute(element, AttrDescription, QString(""));
list.at(0).insertAfter(element, sibling);
}
QDomNodeList list = elementsByTagName(TagBodyMeasurements);
list.at(0).appendChild(element);
return id;
}
//---------------------------------------------------------------------------------------------------------------------
@ -411,3 +409,45 @@ qreal VMeasurements::UniqueTagAttr(const QString &tag, const QString &attr, qrea
}
return defVal;
}
//---------------------------------------------------------------------------------------------------------------------
QDomElement VMeasurements::MakeEmpty(const QString &name)
{
QDomElement element = createElement(TagMeasurement);
SetAttribute(element, AttrName, name);
SetAttribute(element, AttrValue, QString("0"));
if (type == MeasurementsType::Standard)
{
SetAttribute(element, AttrSizeIncrease, QString("0"));
SetAttribute(element, AttrHeightIncrease, QString("0"));
}
else
{
SetAttribute(element, AttrDescription, QString(""));
}
return element;
}
//---------------------------------------------------------------------------------------------------------------------
QDomElement VMeasurements::FindM(const QString &name) const
{
QDomNodeList list = elementsByTagName(TagMeasurement);
for (int i=0; i < list.size(); ++i)
{
const QDomElement domElement = list.at(i).toElement();
if (domElement.isNull() == false)
{
const QString parameter = domElement.attribute(AttrName);
if (parameter == name)
{
return domElement;
}
}
}
return QDomElement();
}

View File

@ -43,7 +43,8 @@ public:
VMeasurements(Unit unit, int baseSize, int baseHeight, VContainer *data);
virtual ~VMeasurements() Q_DECL_OVERRIDE;
int AddEmptyMeasurement(QString &name);
void AddEmpty(const QString &name);
void AddEmptyAfter(const QString &after, const QString &name);
MeasurementsType Type() const;
Unit MUnit() const;
@ -107,12 +108,14 @@ private:
/** @brief data container with data. */
VContainer *data;
MeasurementsType type;
int id;
void CreateEmptyStandardFile(Unit unit, int baseSize, int baseHeight);
void CreateEmptyIndividualFile(Unit unit);
qreal UniqueTagAttr(const QString &tag, const QString &attr, qreal defValue) const;
QDomElement MakeEmpty(const QString &name);
QDomElement FindM(const QString &name) const;
};
#endif // VMEASUREMENTS_H