265 lines
7.2 KiB
C++
265 lines
7.2 KiB
C++
#include "designatormappingtablemodel.h"
|
|
#include <QColor>
|
|
#include <QFont>
|
|
|
|
DesignatorMappingTableModel::DesignatorMappingTableModel(DesignatorMappingModel *mappingModel, QObject *parent)
|
|
: QAbstractTableModel(parent)
|
|
, m_mappingModel(mappingModel)
|
|
, m_showOnlyCustom(false)
|
|
{
|
|
if (m_mappingModel) {
|
|
connect(m_mappingModel, &DesignatorMappingModel::mappingsChanged,
|
|
this, &DesignatorMappingTableModel::onMappingsChanged);
|
|
updateFilteredMappings();
|
|
}
|
|
}
|
|
|
|
int DesignatorMappingTableModel::rowCount(const QModelIndex &parent) const
|
|
{
|
|
if (parent.isValid()) {
|
|
return 0;
|
|
}
|
|
return m_filteredMappings.size();
|
|
}
|
|
|
|
int DesignatorMappingTableModel::columnCount(const QModelIndex &parent) const
|
|
{
|
|
if (parent.isValid()) {
|
|
return 0;
|
|
}
|
|
return ColumnCount;
|
|
}
|
|
|
|
QVariant DesignatorMappingTableModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
if (!index.isValid() || index.row() >= m_filteredMappings.size()) {
|
|
return QVariant();
|
|
}
|
|
|
|
const DesignatorMappingItem &item = m_filteredMappings[index.row()];
|
|
|
|
switch (role) {
|
|
case Qt::DisplayRole:
|
|
case Qt::EditRole:
|
|
switch (index.column()) {
|
|
case Designator:
|
|
return item.designator;
|
|
case SingularName:
|
|
return item.singularName;
|
|
case PluralName:
|
|
return item.pluralName;
|
|
case IsCustom:
|
|
return item.isCustom ? tr("Да") : tr("Нет");
|
|
}
|
|
break;
|
|
|
|
case Qt::BackgroundRole:
|
|
if (item.isCustom) {
|
|
return QColor(255, 255, 200); // Светло-желтый для пользовательских
|
|
}
|
|
break;
|
|
|
|
case Qt::FontRole:
|
|
if (item.isCustom) {
|
|
QFont font;
|
|
font.setBold(true);
|
|
return font;
|
|
}
|
|
break;
|
|
|
|
case Qt::ToolTipRole:
|
|
if (item.isCustom) {
|
|
return tr("Пользовательское определение");
|
|
}
|
|
break;
|
|
}
|
|
|
|
return QVariant();
|
|
}
|
|
|
|
QVariant DesignatorMappingTableModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
{
|
|
if (role != Qt::DisplayRole) {
|
|
return QVariant();
|
|
}
|
|
|
|
if (orientation == Qt::Horizontal) {
|
|
switch (section) {
|
|
case Designator:
|
|
return tr("Дезигнатор");
|
|
case SingularName:
|
|
return tr("Единственное число");
|
|
case PluralName:
|
|
return tr("Множественное число");
|
|
case IsCustom:
|
|
return tr("Пользовательское");
|
|
}
|
|
}
|
|
|
|
return QVariant();
|
|
}
|
|
|
|
bool DesignatorMappingTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
{
|
|
if (!index.isValid() || index.row() >= m_filteredMappings.size() || role != Qt::EditRole) {
|
|
return false;
|
|
}
|
|
|
|
DesignatorMappingItem item = m_filteredMappings[index.row()];
|
|
bool changed = false;
|
|
|
|
switch (index.column()) {
|
|
case Designator:
|
|
if (value.toString() != item.designator) {
|
|
item.designator = value.toString().toUpper();
|
|
changed = true;
|
|
}
|
|
break;
|
|
case SingularName:
|
|
if (value.toString() != item.singularName) {
|
|
item.singularName = value.toString();
|
|
changed = true;
|
|
}
|
|
break;
|
|
case PluralName:
|
|
if (value.toString() != item.pluralName) {
|
|
item.pluralName = value.toString();
|
|
changed = true;
|
|
}
|
|
break;
|
|
case IsCustom:
|
|
// Нельзя редактировать флаг пользовательского определения через таблицу
|
|
return false;
|
|
}
|
|
|
|
if (changed) {
|
|
// Помечаем как пользовательское, если редактируется
|
|
item.isCustom = true;
|
|
|
|
// Обновляем в основной модели
|
|
if (m_mappingModel) {
|
|
m_mappingModel->updateMapping(item.designator, item);
|
|
}
|
|
|
|
// Обновляем отфильтрованные данные
|
|
updateFilteredMappings();
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
Qt::ItemFlags DesignatorMappingTableModel::flags(const QModelIndex &index) const
|
|
{
|
|
if (!index.isValid()) {
|
|
return Qt::NoItemFlags;
|
|
}
|
|
|
|
Qt::ItemFlags flags = QAbstractTableModel::flags(index);
|
|
|
|
// Разрешаем редактирование всех колонок, кроме флага пользовательского определения
|
|
if (index.column() != IsCustom) {
|
|
flags |= Qt::ItemIsEditable;
|
|
}
|
|
|
|
return flags;
|
|
}
|
|
|
|
void DesignatorMappingTableModel::addMapping(const DesignatorMappingItem &item)
|
|
{
|
|
if (m_mappingModel) {
|
|
m_mappingModel->addMapping(item);
|
|
}
|
|
}
|
|
|
|
void DesignatorMappingTableModel::removeMapping(int row)
|
|
{
|
|
if (row >= 0 && row < m_filteredMappings.size()) {
|
|
const DesignatorMappingItem &item = m_filteredMappings[row];
|
|
if (m_mappingModel) {
|
|
m_mappingModel->removeMapping(item.designator);
|
|
}
|
|
}
|
|
}
|
|
|
|
void DesignatorMappingTableModel::updateMapping(int row, const DesignatorMappingItem &item)
|
|
{
|
|
if (row >= 0 && row < m_filteredMappings.size()) {
|
|
const DesignatorMappingItem &oldItem = m_filteredMappings[row];
|
|
if (m_mappingModel) {
|
|
m_mappingModel->updateMapping(oldItem.designator, item);
|
|
}
|
|
}
|
|
}
|
|
|
|
DesignatorMappingItem DesignatorMappingTableModel::getMapping(int row) const
|
|
{
|
|
if (row >= 0 && row < m_filteredMappings.size()) {
|
|
return m_filteredMappings[row];
|
|
}
|
|
return DesignatorMappingItem();
|
|
}
|
|
|
|
void DesignatorMappingTableModel::refreshData()
|
|
{
|
|
updateFilteredMappings();
|
|
}
|
|
|
|
void DesignatorMappingTableModel::setShowOnlyCustom(bool showOnly)
|
|
{
|
|
if (m_showOnlyCustom != showOnly) {
|
|
m_showOnlyCustom = showOnly;
|
|
updateFilteredMappings();
|
|
}
|
|
}
|
|
|
|
void DesignatorMappingTableModel::setFilterText(const QString &text)
|
|
{
|
|
if (m_filterText != text) {
|
|
m_filterText = text;
|
|
updateFilteredMappings();
|
|
}
|
|
}
|
|
|
|
void DesignatorMappingTableModel::onMappingsChanged()
|
|
{
|
|
updateFilteredMappings();
|
|
}
|
|
|
|
void DesignatorMappingTableModel::updateFilteredMappings()
|
|
{
|
|
beginResetModel();
|
|
|
|
m_filteredMappings.clear();
|
|
|
|
if (m_mappingModel) {
|
|
QList<DesignatorMappingItem> allMappings = m_mappingModel->mappings();
|
|
|
|
for (const auto &item : allMappings) {
|
|
if (matchesFilter(item)) {
|
|
m_filteredMappings.append(item);
|
|
}
|
|
}
|
|
}
|
|
|
|
endResetModel();
|
|
}
|
|
|
|
bool DesignatorMappingTableModel::matchesFilter(const DesignatorMappingItem &item) const
|
|
{
|
|
// Фильтр по пользовательским определениям
|
|
if (m_showOnlyCustom && !item.isCustom) {
|
|
return false;
|
|
}
|
|
|
|
// Фильтр по тексту
|
|
if (!m_filterText.isEmpty()) {
|
|
QString filterText = m_filterText.toLower();
|
|
return item.designator.toLower().contains(filterText) ||
|
|
item.singularName.toLower().contains(filterText) ||
|
|
item.pluralName.toLower().contains(filterText);
|
|
}
|
|
|
|
return true;
|
|
}
|