diff --git a/filters/ddmNeighborCountyFilter.cpp b/filters/ddmNeighborCountyFilter.cpp new file mode 100644 index 0000000..5f01271 --- /dev/null +++ b/filters/ddmNeighborCountyFilter.cpp @@ -0,0 +1,73 @@ +#include "filters/ddmNeighborCountyFilter.h" +#include "models/ddmNeighborCountyFilterModel.h" +#include "ddmMapView.h" +#include "ddmSettings.h" +#include "widgets/ddmNeighborCountyFilterWidget.h" + +/** + * Конструктор класса + * + * @param parent Родитель (владелец) + * @author Марунин А.В. + * @since 2.8 + */ +ddmNeighborCountyFilter::ddmNeighborCountyFilter( QObject* parent ) : ddmBaseCountyFilter( parent ) +{ +} + +/** + * Создает фильтр + * + * Создает модель фильтра и виджет, настраивает сигнал-слоты + * + * @author Марунин А.В. + * @since 2.8 + */ +void ddmNeighborCountyFilter::setup() +{ + + ddmNeighborCountyFilterModel* model = new ddmNeighborCountyFilterModel( this ); + ddmNeighborCountyFilterWidget* widget = new ddmNeighborCountyFilterWidget( this ); + + this->m_model = model; + this->m_widget = widget; + + ddmBaseCountyFilter::setup(); + + +} + +/** + * Обновляет выделенные графства + * + * Помечает текущее графство и выделяет его соседей + * + * @author Марунин А.В. + * @since 2.8 + */ +void ddmNeighborCountyFilter::updateSelection() +{ + ddmNeighborCountyFilterModel* model = this->model_cast(); + this->resetSelection(); + + ddmCounty* currentCounty = model->currentCounty(); + currentCounty->show(); + + foreach ( int county_id, model->neighbors() ) + { + ddmCounty* neighbor = model->county( county_id ); + neighbor->show(); + neighbor->select(); + } + Q_EMIT selectionUpdated(); +} + +/** + * Деструктор класса + * + * @author Марунин А.В. + * @since 2.8 + */ +ddmNeighborCountyFilter::~ddmNeighborCountyFilter() +{ +} diff --git a/filters/ddmNeighborCountyFilter.h b/filters/ddmNeighborCountyFilter.h new file mode 100644 index 0000000..5dad080 --- /dev/null +++ b/filters/ddmNeighborCountyFilter.h @@ -0,0 +1,29 @@ +#ifndef DDM_NEIGHBOR_COUNTY_FILTER_H +#define DDM_NEIGHBOR_COUNTY_FILTER_H + +#include "filters/ddmBaseCountyFilter.h" + +/** + * Класс ddmNeighborCountyFilter описывает фильтр для отображения одного графства + * + * @author Марунин А.В. + * @since 2.8 + */ +class ddmNeighborCountyFilter : public ddmBaseCountyFilter +{ + Q_OBJECT + +public: + + ddmNeighborCountyFilter( QObject* parent = 0 ); + + virtual ~ddmNeighborCountyFilter(); + +protected: + + virtual void setup(); + virtual void updateSelection(); + +}; + +#endif // DDM_COUNTY_FILTER_H diff --git a/models/ddmNeighborCountyFilterModel.cpp b/models/ddmNeighborCountyFilterModel.cpp new file mode 100644 index 0000000..f72c7fb --- /dev/null +++ b/models/ddmNeighborCountyFilterModel.cpp @@ -0,0 +1,109 @@ +#include + +#include "models/ddmNeighborCountyFilterModel.h" + +/** + * Конструктор класса + * + * @param parent Владелец модели + * @author Марунин А.В. + * @since 2.8 + */ +ddmNeighborCountyFilterModel::ddmNeighborCountyFilterModel( QObject* parent ) : ddmBaseCountyFilterModel( parent ) +{ +} + +/** + * Загружает данные из БД + * + * @author Марунин А.В. + * @since 2.8 + */ +void ddmNeighborCountyFilterModel::reloadData() +{ + if ( this->currentState() ) + { + this->loadState( this->currentState()->id() ); + this->updateCountyNames(); + if ( this->currentCounty() ) + { + this->updateNeighbors(); + + QStringList neighbors; + neighbors.append( QString( "%1" ).arg( this->currentCounty()->id() ) ); + foreach ( int county_id, this->m_neighbors ) + { + neighbors.append( QString( "%1" ).arg( county_id ) ); + } + + // Выбираем соседние графства текущего штата и само текущее графство + QString sqlQuery = QString( "SELECT * FROM cache_boundaries WHERE county_id IN (%1)" ).arg( neighbors.join( ',' ) ); + //qDebug() << sqlQuery; + this->execQuery( sqlQuery ); + } + } +} + +/** + * Обновляет список соседей у текущего графства + * + * Находит все id соседей первого порядка + * + * @author Марунин А.В. + * @since 2.8 + */ +void ddmNeighborCountyFilterModel::updateNeighbors() +{ + this->m_neighbors.clear(); + if ( this->currentCounty() ) + { + ddmDatabase& db = this->database(); + QString sql; + QSqlQueryModel* query; + int i, rowCount; + + sql = QString( "SELECT county_b FROM ddm_county_neighbors WHERE county_a = %1" ).arg( this->currentCounty()->id() ); + query = db.select( sql ); + Q_ASSERT( db.hasErrors() == false ); + rowCount = query->rowCount(); + for ( i = 0; i < rowCount; i++ ) + { + int county_id = query->data( query->index( i, 0 ) ).toInt(); + this->m_neighbors.insert( county_id ); + // qDebug() << county_id; + } + + sql = QString( "SELECT county_a FROM ddm_county_neighbors WHERE county_b = %1" ).arg( this->currentCounty()->id() ); + query = db.select( sql ); + Q_ASSERT( db.hasErrors() == false ); + rowCount = query->rowCount(); + for ( i = 0; i < rowCount; i++ ) + { + int county_id = query->data( query->index( i, 0 ) ).toInt(); + this->m_neighbors.insert( county_id ); + // qDebug() << county_id; + } + } +} + +/** + * Возвращает список id соседей + * + * @return Объект типа QSet + * @author Марунин А.В. + * @since 2.8 + */ +QSet ddmNeighborCountyFilterModel::neighbors() const +{ + return this->m_neighbors; +} + +/** + * Деструктор класса + * + * @author Марунин А.В. + * @since 2.8 + */ +ddmNeighborCountyFilterModel::~ddmNeighborCountyFilterModel() +{ +} diff --git a/models/ddmNeighborCountyFilterModel.h b/models/ddmNeighborCountyFilterModel.h new file mode 100644 index 0000000..771cd5c --- /dev/null +++ b/models/ddmNeighborCountyFilterModel.h @@ -0,0 +1,37 @@ +#ifndef DDM_NEIGHBOR_COUNTY_FILTER_MODEL_H +#define DDM_NEIGHBOR_COUNTY_FILTER_MODEL_H + +#include +#include "models/ddmBaseCountyFilterModel.h" + +/** + * Класс ddmNeighborCountyFilterModel определяет модель фильтра ddmNeighborCountyFilter + * Отображает соседей первого порядка для выбранного графства + * + * Основными элементами модели являются: + * - neighbors список идентификаторов соседей + * + * @author Марунин А.В. + * @since 2.8 + */ +class ddmNeighborCountyFilterModel : public ddmBaseCountyFilterModel +{ + Q_OBJECT + +public: + + ddmNeighborCountyFilterModel( QObject* parent = 0 ); + + virtual void reloadData(); + + QSet neighbors() const; + + virtual ~ddmNeighborCountyFilterModel(); + +protected: + QSet m_neighbors; + + void updateNeighbors(); +}; + +#endif // DDM_NEIGHBOR_COUNTY_FILTER_MODEL_H diff --git a/widgets/ddmNeighborCountyFilterWidget.cpp b/widgets/ddmNeighborCountyFilterWidget.cpp new file mode 100644 index 0000000..830de5d --- /dev/null +++ b/widgets/ddmNeighborCountyFilterWidget.cpp @@ -0,0 +1,25 @@ +#include "widgets/ddmNeighborCountyFilterWidget.h" +#include "filters/ddmNeighborCountyFilter.h" +#include "ui_ddmNeighborCountyFilterWidget.h" + +/** + * Конструктор класса + * + * @param filter Фильтр, которому принадлежит виджет + * @param parent Родитель (владелец) виджета + * @author Марунин А.В. + * @since 2.8 + */ +ddmNeighborCountyFilterWidget::ddmNeighborCountyFilterWidget( ddmNeighborCountyFilter* filter, QWidget* parent ) : ddmBaseCountyFilterWidget( filter, parent ) +{ +} + +/** + * Деструктор класса + * + * @author Марунин А.В. + * @since 2.8 + */ +ddmNeighborCountyFilterWidget::~ddmNeighborCountyFilterWidget() +{ +} diff --git a/widgets/ddmNeighborCountyFilterWidget.h b/widgets/ddmNeighborCountyFilterWidget.h new file mode 100644 index 0000000..742739b --- /dev/null +++ b/widgets/ddmNeighborCountyFilterWidget.h @@ -0,0 +1,30 @@ +#ifndef DDM_NEIGHBOR_COUNTY_FILTER_WIDGET_H +#define DDM_NEIGHBOR_COUNTY_FILTER_WIDGET_H + +#include +#include "widgets/ddmBaseCountyFilterWidget.h" + +class ddmNeighborCountyFilter; + +/** + * Класс ddmNeighborCountyFilterWidget описывает виджет для задания параметров фильтра ddmNeighborCountyFilter + * + * @author Марунин А.В. + * @since 2.8 + * @todo Если понадобятся доп.настройки, то нужно создать панель (см.ddmCountyInfoFilterWidgetPanel) + */ +class ddmNeighborCountyFilterWidget : public ddmBaseCountyFilterWidget +{ + Q_OBJECT + +public: + + ddmNeighborCountyFilterWidget( ddmNeighborCountyFilter* filter, QWidget* parent = NULL ); + + virtual ~ddmNeighborCountyFilterWidget(); + +private: + +}; + +#endif // DDM_NEIGHBOR_COUNTY_FILTER_WIDGET_H diff --git a/widgets/ddmNeighborCountyFilterWidget.ui b/widgets/ddmNeighborCountyFilterWidget.ui new file mode 100644 index 0000000..36bfee9 --- /dev/null +++ b/widgets/ddmNeighborCountyFilterWidget.ui @@ -0,0 +1,116 @@ + + + ddmNeighborCountyFilterWidget + + + + 0 + 0 + 160 + 348 + + + + + 0 + 0 + + + + + 155 + 0 + + + + + 16777215 + 16777215 + + + + ddmCountyFilterWidget + + + + + + QLayout::SetMinimumSize + + + + + Штат + + + + + + + + 0 + 0 + + + + + 140 + 20 + + + + + + + + Графство + + + + + + + + 0 + 0 + + + + + 140 + 20 + + + + + + + + false + + + background-color: rgb(234, 234, 234); +border-color: rgb(234, 234, 234); + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + +