Skip to content

Commit

Permalink
Fixed #34: Добавлен фильтр ddmNeighborCountyFilter - соседи первого п…
Browse files Browse the repository at this point in the history
…орядка
  • Loading branch information
alexeymarunin committed Mar 19, 2015
1 parent f0aa573 commit ecfef47
Show file tree
Hide file tree
Showing 7 changed files with 419 additions and 0 deletions.
73 changes: 73 additions & 0 deletions filters/ddmNeighborCountyFilter.cpp
Original file line number Diff line number Diff line change
@@ -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<ddmNeighborCountyFilterModel>();
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()
{
}
29 changes: 29 additions & 0 deletions filters/ddmNeighborCountyFilter.h
Original file line number Diff line number Diff line change
@@ -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
109 changes: 109 additions & 0 deletions models/ddmNeighborCountyFilterModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include <QSqlRecord>

#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<int>
* @author Марунин А.В.
* @since 2.8
*/
QSet<int> ddmNeighborCountyFilterModel::neighbors() const
{
return this->m_neighbors;
}

/**
* Деструктор класса
*
* @author Марунин А.В.
* @since 2.8
*/
ddmNeighborCountyFilterModel::~ddmNeighborCountyFilterModel()
{
}
37 changes: 37 additions & 0 deletions models/ddmNeighborCountyFilterModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef DDM_NEIGHBOR_COUNTY_FILTER_MODEL_H
#define DDM_NEIGHBOR_COUNTY_FILTER_MODEL_H

#include <QSet>
#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<int> neighbors() const;

virtual ~ddmNeighborCountyFilterModel();

protected:
QSet<int> m_neighbors;

void updateNeighbors();
};

#endif // DDM_NEIGHBOR_COUNTY_FILTER_MODEL_H
25 changes: 25 additions & 0 deletions widgets/ddmNeighborCountyFilterWidget.cpp
Original file line number Diff line number Diff line change
@@ -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()
{
}
30 changes: 30 additions & 0 deletions widgets/ddmNeighborCountyFilterWidget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef DDM_NEIGHBOR_COUNTY_FILTER_WIDGET_H
#define DDM_NEIGHBOR_COUNTY_FILTER_WIDGET_H

#include <QComboBox>
#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
Loading

0 comments on commit ecfef47

Please sign in to comment.