93 lines
2.4 KiB
C++
93 lines
2.4 KiB
C++
|
#include "attribute_edit.h"
|
||
|
#include "ui_attribute_edit.h"
|
||
|
|
||
|
CAttributeEdit::CAttributeEdit(QWidget* parent) : QDialog(parent), ui(new Ui::CAttributeEdit)
|
||
|
{
|
||
|
ui->setupUi(this);
|
||
|
|
||
|
setWindowTitle(u8"属性编辑");
|
||
|
setMinimumSize(400, 600);
|
||
|
|
||
|
connect(ui->btnOk, &QPushButton::clicked, this, &CAttributeEdit::handle_ok);
|
||
|
connect(ui->btnCancel, &QPushButton::clicked, this, [&]() {
|
||
|
is_ok_ = false;
|
||
|
close();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
void CAttributeEdit::handle_ok()
|
||
|
{
|
||
|
property_.clear();
|
||
|
int row = table_->rowCount();
|
||
|
for (int i = 0; i < row; ++i) {
|
||
|
QString key = table_->item(i, 0)->text();
|
||
|
QString value = table_->item(i, 1)->text();
|
||
|
property_.emplace_back(key.toLocal8Bit().constData(), value.toLocal8Bit().constData());
|
||
|
}
|
||
|
is_ok_ = true;
|
||
|
close();
|
||
|
}
|
||
|
|
||
|
|
||
|
CAttributeEdit::~CAttributeEdit()
|
||
|
{
|
||
|
delete ui;
|
||
|
}
|
||
|
|
||
|
void CAttributeEdit::showEvent(QShowEvent* event)
|
||
|
{
|
||
|
show_before();
|
||
|
QDialog::showEvent(event);
|
||
|
}
|
||
|
|
||
|
void CAttributeEdit::set_attribute(const Property_t& property)
|
||
|
{
|
||
|
property_.clear();
|
||
|
property_ = property;
|
||
|
}
|
||
|
|
||
|
void CAttributeEdit::get_attribute(Property_t& property)
|
||
|
{
|
||
|
property.clear();
|
||
|
property = property_;
|
||
|
}
|
||
|
|
||
|
void CAttributeEdit::show_before()
|
||
|
{
|
||
|
init_table();
|
||
|
|
||
|
for (const auto& item : property_) {
|
||
|
int row = table_->rowCount();
|
||
|
table_->insertRow(row);
|
||
|
|
||
|
QTableWidgetItem* pkey = new QTableWidgetItem();
|
||
|
pkey->setText(item.key.c_str());
|
||
|
pkey->setFlags(pkey->flags() & ~Qt::ItemIsEditable);
|
||
|
table_->setItem(row, 0, pkey);
|
||
|
|
||
|
QTableWidgetItem* pvalue = new QTableWidgetItem();
|
||
|
pvalue->setText(item.value.c_str());
|
||
|
table_->setItem(row, 1, pvalue);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void CAttributeEdit::init_table()
|
||
|
{
|
||
|
if (table_ == nullptr) {
|
||
|
QStringList list;
|
||
|
list << u8"属性" << u8"值";
|
||
|
table_ = new QTableWidget();
|
||
|
table_->setColumnCount(list.size());
|
||
|
table_->setHorizontalHeaderLabels(list);
|
||
|
table_->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||
|
table_->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection);
|
||
|
table_->setColumnWidth(0, 80);
|
||
|
table_->setColumnWidth(1, 200);
|
||
|
QHBoxLayout* ly = new QHBoxLayout();
|
||
|
ly->addWidget(table_);
|
||
|
ui->widget->setLayout(ly);
|
||
|
} else {
|
||
|
table_->clearContents();
|
||
|
table_->setRowCount(0);
|
||
|
}
|
||
|
}
|