Refactoring. Improve VWidgetDetails.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2019-07-26 13:32:58 +03:00
parent c8287c34dc
commit 82a4e011b4
2 changed files with 60 additions and 40 deletions

View File

@ -37,6 +37,18 @@
#include <QMenu> #include <QMenu>
#include <QUndoStack> #include <QUndoStack>
namespace
{
enum PieceColumn
{
InLayout = 0,
PieceName = 1
};
Q_GLOBAL_STATIC_WITH_ARGS(const QString, allowDetailIcon, (QLatin1String("://icon/16x16/allow_detail.png")))
Q_GLOBAL_STATIC_WITH_ARGS(const QString, forbidDetailIcon, (QLatin1String("://icon/16x16/forbid_detail.png")))
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VWidgetDetails::VWidgetDetails(VContainer *data, VAbstractPattern *doc, QWidget *parent) VWidgetDetails::VWidgetDetails(VContainer *data, VAbstractPattern *doc, QWidget *parent)
: QWidget(parent), : QWidget(parent),
@ -74,7 +86,7 @@ void VWidgetDetails::SelectDetail(quint32 id)
const int rowCount = ui->tableWidget->rowCount(); const int rowCount = ui->tableWidget->rowCount();
for (int row = 0; row < rowCount; ++row) for (int row = 0; row < rowCount; ++row)
{ {
QTableWidgetItem *item = ui->tableWidget->item(row, 0); QTableWidgetItem *item = ui->tableWidget->item(row, PieceColumn::InLayout);
if (item->data(Qt::UserRole).toUInt() == id) if (item->data(Qt::UserRole).toUInt() == id)
{ {
@ -90,7 +102,7 @@ void VWidgetDetails::ToggledPiece(quint32 id)
const int rowCount = ui->tableWidget->rowCount(); const int rowCount = ui->tableWidget->rowCount();
for (int row = 0; row < rowCount; ++row) for (int row = 0; row < rowCount; ++row)
{ {
QTableWidgetItem *item = ui->tableWidget->item(row, 0); QTableWidgetItem *item = ui->tableWidget->item(row, PieceColumn::InLayout);
if (item && item->data(Qt::UserRole).toUInt() == id) if (item && item->data(Qt::UserRole).toUInt() == id)
{ {
@ -103,7 +115,7 @@ void VWidgetDetails::ToggledPiece(quint32 id)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VWidgetDetails::InLayoutStateChanged(int row, int column) void VWidgetDetails::InLayoutStateChanged(int row, int column)
{ {
QTableWidgetItem *item = ui->tableWidget->item(row, 0); QTableWidgetItem *item = ui->tableWidget->item(row, PieceColumn::InLayout);
const quint32 id = item->data(Qt::UserRole).toUInt(); const quint32 id = item->data(Qt::UserRole).toUInt();
emit Highlight(id); emit Highlight(id);
@ -135,40 +147,11 @@ void VWidgetDetails::FillTable(const QHash<quint32, VPiece> *details)
++currentRow; ++currentRow;
const VPiece det = i.value(); const VPiece det = i.value();
QTableWidgetItem *item = new QTableWidgetItem(); ui->tableWidget->setItem(currentRow, PieceColumn::InLayout, PrepareInLayoutColumnCell(det, i.key()));
item->setTextAlignment(Qt::AlignHCenter); ui->tableWidget->setItem(currentRow, PieceColumn::PieceName, PreparePieceNameColumnCell(det));
if (det.IsInLayout())
{
item->setIcon(QIcon("://icon/16x16/allow_detail.png"));
}
else
{
item->setIcon(QIcon("://icon/16x16/forbid_detail.png"));
}
item->setData(Qt::UserRole, i.key());
// 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);
ui->tableWidget->setItem(currentRow, 0, item);
QString name = det.GetName();
if (name.isEmpty())
{
name = tr("Unnamed");
}
item = new QTableWidgetItem(name);
item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
flags = item->flags();
flags &= ~(Qt::ItemIsEditable); // reset/clear the flag
item->setFlags(flags);
ui->tableWidget->setItem(currentRow, 1, item);
++i; ++i;
} }
ui->tableWidget->sortItems(1, Qt::AscendingOrder); ui->tableWidget->sortItems(PieceColumn::PieceName, Qt::AscendingOrder);
ui->tableWidget->resizeColumnsToContents(); ui->tableWidget->resizeColumnsToContents();
ui->tableWidget->resizeRowsToContents(); ui->tableWidget->resizeRowsToContents();
@ -186,7 +169,7 @@ void VWidgetDetails::ToggleSectionDetails(bool select)
for (int i = 0; i<ui->tableWidget->rowCount(); ++i) for (int i = 0; i<ui->tableWidget->rowCount(); ++i)
{ {
const quint32 id = ui->tableWidget->item(i, 0)->data(Qt::UserRole).toUInt(); const quint32 id = ui->tableWidget->item(i, PieceColumn::InLayout)->data(Qt::UserRole).toUInt();
if (allDetails->contains(id)) if (allDetails->contains(id))
{ {
if (not (select == allDetails->value(id).IsInLayout())) if (not (select == allDetails->value(id).IsInLayout()))
@ -210,8 +193,8 @@ void VWidgetDetails::ToggledPieceItem(QTableWidgetItem *item)
if (details->contains(id)) if (details->contains(id))
{ {
const bool inLayout = details->value(id).IsInLayout(); const bool inLayout = details->value(id).IsInLayout();
inLayout ? item->setIcon(QIcon("://icon/16x16/allow_detail.png")) inLayout ? item->setIcon(QIcon(*allowDetailIcon))
: item->setIcon(QIcon("://icon/16x16/forbid_detail.png")); : item->setIcon(QIcon(*forbidDetailIcon));
VToolSeamAllowance *tool = nullptr; VToolSeamAllowance *tool = nullptr;
try try
@ -227,6 +210,40 @@ void VWidgetDetails::ToggledPieceItem(QTableWidgetItem *item)
} }
} }
//---------------------------------------------------------------------------------------------------------------------
QTableWidgetItem *VWidgetDetails::PrepareInLayoutColumnCell(const VPiece &det, quint32 id) const
{
QTableWidgetItem *item = new QTableWidgetItem();
item->setTextAlignment(Qt::AlignHCenter);
item->setIcon(det.IsInLayout() ? QIcon(*allowDetailIcon) : QIcon(*forbidDetailIcon));
item->setData(Qt::UserRole, id);
// 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);
return item;
}
//---------------------------------------------------------------------------------------------------------------------
QTableWidgetItem *VWidgetDetails::PreparePieceNameColumnCell(const VPiece &det) const
{
QString name = det.GetName();
if (name.isEmpty())
{
name = tr("Unnamed");
}
QTableWidgetItem *item = new QTableWidgetItem(name);
item->setTextAlignment(Qt::AlignLeft | 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);
return item;
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VWidgetDetails::ShowContextMenu(const QPoint &pos) void VWidgetDetails::ShowContextMenu(const QPoint &pos)
{ {
@ -290,7 +307,7 @@ void VWidgetDetails::ShowContextMenu(const QPoint &pos)
for (int i = 0; i<ui->tableWidget->rowCount(); ++i) for (int i = 0; i<ui->tableWidget->rowCount(); ++i)
{ {
QTableWidgetItem *item = ui->tableWidget->item(i, 0); QTableWidgetItem *item = ui->tableWidget->item(i, PieceColumn::InLayout);
const quint32 id = item->data(Qt::UserRole).toUInt(); const quint32 id = item->data(Qt::UserRole).toUInt();
if (allDetails->contains(id)) if (allDetails->contains(id))
{ {
@ -315,7 +332,7 @@ void VWidgetDetails::on_checkBoxHideNotInLayout_stateChanged()
{ {
for (int i = 0; i < ui->tableWidget->rowCount(); ++i) for (int i = 0; i < ui->tableWidget->rowCount(); ++i)
{ {
if (QTableWidgetItem *item = ui->tableWidget->item(i, 0)) if (QTableWidgetItem *item = ui->tableWidget->item(i, PieceColumn::InLayout))
{ {
ToggledPieceItem(item); ToggledPieceItem(item);
} }

View File

@ -72,6 +72,9 @@ private:
void FillTable(const QHash<quint32, VPiece> *details); void FillTable(const QHash<quint32, VPiece> *details);
void ToggleSectionDetails(bool select); void ToggleSectionDetails(bool select);
void ToggledPieceItem(QTableWidgetItem *item); void ToggledPieceItem(QTableWidgetItem *item);
Q_REQUIRED_RESULT QTableWidgetItem *PrepareInLayoutColumnCell(const VPiece &det, quint32 id) const;
Q_REQUIRED_RESULT QTableWidgetItem *PreparePieceNameColumnCell(const VPiece &det) const;
}; };
#endif // VWIDGETDETAILS_H #endif // VWIDGETDETAILS_H