159 lines
6.5 KiB
C++
159 lines
6.5 KiB
C++
#include "componentspreview.h"
|
||
|
||
|
||
ComponentsPreview::ComponentsPreview(ComponentTableModel* model, QWidget *parent)
|
||
: QWidget{parent}, m_model(model)
|
||
{
|
||
setUp();
|
||
}
|
||
|
||
void ComponentsPreview::setUp(){
|
||
setWidgets();
|
||
setUpLayout();
|
||
setUpConnections();
|
||
}
|
||
|
||
void ComponentsPreview::setWidgets(){
|
||
m_variantComboBox = new QComboBox(this);
|
||
m_tableView = new FreezeTableWidget(this);
|
||
|
||
// Устанавливаем модель для таблицы
|
||
if (m_model) {
|
||
m_tableView->setModel(m_model);
|
||
// Настраиваем замороженные столбцы (ID и обозначение) после установки модели
|
||
m_tableView->setFrozenColumns(2);
|
||
}
|
||
}
|
||
|
||
void ComponentsPreview::setUpLayout(){
|
||
QVBoxLayout* mainLayout = new QVBoxLayout(this);
|
||
QGroupBox* groupBox = new QGroupBox("Варианты исполнения", this);
|
||
QHBoxLayout* groupBoxLayout = new QHBoxLayout(groupBox);
|
||
groupBoxLayout->addWidget(m_variantComboBox);
|
||
mainLayout->addWidget(groupBox);
|
||
mainLayout->addWidget(m_tableView);
|
||
}
|
||
|
||
void ComponentsPreview::setUpConnections()
|
||
{
|
||
// Подключение изменения варианта
|
||
connect(m_variantComboBox, QOverload<const QString &>::of(&QComboBox::currentTextChanged),
|
||
[this](const QString &text) {
|
||
qDebug() << "ComponentsPreview: Изменение варианта в комбобоксе:" << text;
|
||
emit variantChanged(text);
|
||
});
|
||
|
||
// Подключение выбора компонента
|
||
connect(m_tableView->selectionModel(), &QItemSelectionModel::currentChanged,
|
||
[this](const QModelIndex ¤t, const QModelIndex &previous) {
|
||
if (current.isValid()) {
|
||
emit componentSelected(current.row());
|
||
}
|
||
});
|
||
|
||
// Подключение двойного клика
|
||
connect(m_tableView, &QTableView::doubleClicked,
|
||
[this](const QModelIndex &index) {
|
||
emit componentDoubleClicked(index.row());
|
||
});
|
||
|
||
// Подключение обновления списка вариантов
|
||
if (m_model) {
|
||
connect(m_model, &ComponentTableModel::variantsChanged,
|
||
this, &ComponentsPreview::updateVariantComboBox);
|
||
connect(m_model, &ComponentTableModel::currentVariantChanged,
|
||
[this]() {
|
||
if (m_model && m_variantComboBox->currentText() != m_model->currentVariant()) {
|
||
m_variantComboBox->setCurrentText(m_model->currentVariant());
|
||
}
|
||
});
|
||
|
||
// Инициализируем комбобокс
|
||
updateVariantComboBox();
|
||
}
|
||
}
|
||
|
||
void ComponentsPreview::updateVariantComboBox()
|
||
{
|
||
if (!m_model || !m_variantComboBox)
|
||
return;
|
||
|
||
qDebug() << "ComponentsPreview: Обновление комбобокса вариантов";
|
||
|
||
// Блокируем сигналы во время обновления
|
||
m_variantComboBox->blockSignals(true);
|
||
|
||
QString currentText = m_variantComboBox->currentText();
|
||
m_variantComboBox->clear();
|
||
|
||
// Добавляем варианты из модели
|
||
QStringList variants = m_model->variants();
|
||
m_variantComboBox->addItems(variants);
|
||
|
||
qDebug() << "ComponentsPreview: Доступные варианты:" << variants;
|
||
qDebug() << "ComponentsPreview: Текущий вариант модели:" << m_model->currentVariant();
|
||
qDebug() << "ComponentsPreview: Текущий текст комбобокса:" << currentText;
|
||
|
||
// Приоритет: сначала пытаемся установить текущий вариант модели
|
||
QString modelCurrentVariant = m_model->currentVariant();
|
||
if (!modelCurrentVariant.isEmpty() && variants.contains(modelCurrentVariant)) {
|
||
m_variantComboBox->setCurrentText(modelCurrentVariant);
|
||
qDebug() << "ComponentsPreview: Установлен вариант модели:" << modelCurrentVariant;
|
||
} else if (!currentText.isEmpty() && variants.contains(currentText)) {
|
||
// Если текущий вариант модели не найден, пытаемся сохранить предыдущий выбор
|
||
m_variantComboBox->setCurrentText(currentText);
|
||
qDebug() << "ComponentsPreview: Установлен предыдущий выбор:" << currentText;
|
||
} else if (!variants.isEmpty()) {
|
||
// Если ничего не подходит, устанавливаем первый доступный вариант
|
||
m_variantComboBox->setCurrentIndex(0);
|
||
qDebug() << "ComponentsPreview: Установлен первый доступный вариант:" << variants.first();
|
||
} else {
|
||
qDebug() << "ComponentsPreview: Нет доступных вариантов";
|
||
}
|
||
|
||
// Разблокируем сигналы
|
||
m_variantComboBox->blockSignals(false);
|
||
}
|
||
|
||
void ComponentsPreview::updateView()
|
||
{
|
||
// Обновляем комбобокс вариантов
|
||
updateVariantComboBox();
|
||
|
||
// Обновляем таблицу
|
||
if (m_tableView && m_model) {
|
||
m_tableView->reset();
|
||
m_tableView->resizeColumnsToContents();
|
||
}
|
||
}
|
||
|
||
void ComponentsPreview::setCurrentVariant(const QString &variant)
|
||
{
|
||
qDebug() << "ComponentsPreview: setCurrentVariant вызван с вариантом:" << variant;
|
||
|
||
if (!m_variantComboBox)
|
||
return;
|
||
|
||
// Блокируем сигналы во время установки
|
||
m_variantComboBox->blockSignals(true);
|
||
|
||
// Устанавливаем вариант в модели компонентов ПЕРЕД установкой в комбобокс
|
||
if (m_model) {
|
||
m_model->setCurrentVariant(variant);
|
||
qDebug() << "ComponentsPreview: Вариант установлен в модели компонентов";
|
||
}
|
||
|
||
// Устанавливаем вариант в комбобокс
|
||
int index = m_variantComboBox->findText(variant);
|
||
if (index >= 0) {
|
||
m_variantComboBox->setCurrentIndex(index);
|
||
qDebug() << "ComponentsPreview: Вариант установлен в комбобокс с индексом:" << index;
|
||
} else {
|
||
qDebug() << "ComponentsPreview: Вариант не найден в комбобоксе:" << variant;
|
||
}
|
||
|
||
// Разблокируем сигналы
|
||
m_variantComboBox->blockSignals(false);
|
||
}
|
||
|