Ref T472, category model/view + tree view

This commit is contained in:
Klaus Basan
2019-02-03 21:37:30 +01:00
committed by Mat Sutcliffe
parent b85222e1f5
commit 969600b65d
14 changed files with 500 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
/* Copyright (C) 2019
* Swift Project Community / Contributors
*
* This file is part of swift Project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "blackgui/models/aircraftcategorylistmodel.h"
#include "blackgui/models/columnformatters.h"
#include "blackgui/models/columns.h"
#include "blackmisc/db/datastore.h"
#include "blackmisc/timestampbased.h"
#include <Qt>
#include <QtGlobal>
using namespace BlackMisc::Aviation;
namespace BlackGui
{
namespace Models
{
CAircraftCategoryListModel::CAircraftCategoryListModel(QObject *parent) :
CListModelDbObjects("AircraftCategoryListModel", parent)
{
m_columns.addColumn(CColumn::standardString("id", CAircraftCategory::IndexDbIntegerKey, CDefaultFormatter::alignRightVCenter()));
m_columns.addColumn(CColumn::standardString("level", CAircraftCategory::IndexLevelString));
m_columns.addColumn(CColumn::standardString("path", CAircraftCategory::IndexPath));
m_columns.addColumn(CColumn::standardString("name", CAircraftCategory::IndexName));
m_columns.addColumn(CColumn::standardString("description", CAircraftCategory::IndexDescription));
m_columns.addColumn(CColumn::standardString("changed", CAircraftCategory::IndexUtcTimestampFormattedYmdhms));
// default sort order
this->setSortColumnByPropertyIndex(CAircraftCategory::IndexLevelString);
m_sortOrder = Qt::AscendingOrder;
}
} // ns
} // ns

View File

@@ -0,0 +1,39 @@
/* Copyright (C) 2019
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKGUI_MODELS_AIRCRAFTCATEGORYLISTMODEL_H
#define BLACKGUI_MODELS_AIRCRAFTCATEGORYLISTMODEL_H
#include "blackmisc/aviation/aircraftcategorylist.h"
#include "blackmisc/aviation/aircraftcategory.h"
#include "blackgui/models/listmodeldbobjects.h"
#include "blackgui/blackguiexport.h"
namespace BlackGui
{
namespace Models
{
//! Airport list model
class BLACKGUI_EXPORT CAircraftCategoryListModel :
public CListModelDbObjects<BlackMisc::Aviation::CAircraftCategoryList, int, true>
{
Q_OBJECT
public:
//! Constructor
explicit CAircraftCategoryListModel(QObject *parent = nullptr);
//! Destructor
virtual ~CAircraftCategoryListModel() {}
};
}
}
#endif // guard

View File

@@ -0,0 +1,109 @@
/* Copyright (C) 2019
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "blackgui/models/aircraftcategorytreemodel.h"
#include "blackgui/models/columnformatters.h"
#include "blackgui/models/columns.h"
#include "blackmisc/compare.h"
#include "blackmisc/icon.h"
#include "blackmisc/variant.h"
#include "blackmisc/propertyindex.h"
#include <QList>
#include <QMap>
#include <QStandardItem>
#include <QStandardItemModel>
#include <QString>
#include <Qt>
#include <QtDebug>
#include <QtGlobal>
using namespace BlackMisc;
using namespace BlackMisc::PhysicalQuantities;
using namespace BlackMisc::Aviation;
namespace BlackGui
{
namespace Models
{
CAircraftCategoryTreeModel::CAircraftCategoryTreeModel(QObject *parent) : QStandardItemModel(parent)
{
m_columns.addColumn(CColumn::standardString("description", CAircraftCategory::IndexDescription));
m_columns.addColumn(CColumn::standardString("changed", CAircraftCategory::IndexUtcTimestampFormattedYmdhms));
}
void CAircraftCategoryTreeModel::updateContainer(const CAircraftCategoryList &categories)
{
if (categories.isEmpty())
{
CAircraftCategoryTreeModel::clear();
return;
}
m_categories = categories;
m_categories.sortByLevel();
QStandardItemModel::clear();
QMap<int, QStandardItem *> items;
this->setColumnCount(m_columns.size() + 1);
for (const CAircraftCategory &category : m_categories)
{
QList<QStandardItem *> categoryRow;
// ownership of QStandardItem is taken by model
QStandardItem *si = new QStandardItem(
category.isAssignable() ? CIcons::paperPlane16() : CIcons::folder16(),
category.getLevelAndName()
);
si->setEditable(false);
categoryRow.push_back(si);
// add all clumns
for (const CColumn &column : m_columns.columns())
{
const CPropertyIndex i(column.getPropertyIndex());
const CVariant v(category.propertyByIndex(i));
if (column.getFormatter()->supportsRole(Qt::DecorationRole))
{
const QIcon icon = column.getFormatter()->decorationRole(v).toPixmap();
si = new QStandardItem(icon, QString());
}
else if (column.getFormatter()->supportsRole(Qt::DisplayRole))
{
const CVariant f = column.getFormatter()->displayRole(v);
si = new QStandardItem(f.toQString(true));
}
if (!si) { continue; }
si->setEditable(false); // make not editable
categoryRow.push_back(si);
} // columns
// add all items
if (categoryRow.isEmpty()) { continue; }
if (category.isFirstLevel())
{
this->invisibleRootItem()->appendRow(categoryRow);
}
else
{
const int p = category.getDepth() - 1;
items[p]->appendRow(categoryRow);
}
items.insert(category.getDepth(), categoryRow.front());
}
}
void CAircraftCategoryTreeModel::clear()
{
m_categories.clear();
QStandardItemModel::clear();
}
} // namespace
} // namespace

View File

@@ -0,0 +1,57 @@
/* Copyright (C) 2019
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKGUI_MODELS_AIRCRAFTCATEGORYTREEMODEL_H
#define BLACKGUI_MODELS_AIRCRAFTCATEGORYTREEMODEL_H
#include "columns.h"
#include "blackmisc/aviation/aircraftcategorylist.h"
#include "blackgui/blackguiexport.h"
#include <QStandardItemModel>
namespace BlackGui
{
namespace Models
{
//! ATC list model
class BLACKGUI_EXPORT CAircraftCategoryTreeModel : public QStandardItemModel
{
Q_OBJECT
public:
//! Constructor
explicit CAircraftCategoryTreeModel(QObject *parent = nullptr);
//! Destructor
virtual ~CAircraftCategoryTreeModel() override {}
//! Set columns
void setColumns(const CColumns &columns) { m_columns.setColumns(columns); }
//! Update container
void updateContainer(const BlackMisc::Aviation::CAircraftCategoryList &categories);
//! Clear everything
//! \remark hiding QStandardItemModel::clear()
void clear();
//! Get container
const BlackMisc::Aviation::CAircraftCategoryList &container() const { return m_categories; }
private:
CColumns m_columns { "CAircraftCategoryTreeModel" };
BlackMisc::Aviation::CAircraftCategoryList m_categories;
};
} // ns
} // ns
#endif // guard

View File

@@ -16,6 +16,7 @@
#include "blackmisc/simulation/simulatedaircraftlist.h"
#include "blackmisc/simulation/matchingstatistics.h"
#include "blackmisc/aviation/aircrafticaocodelist.h"
#include "blackmisc/aviation/aircraftcategorylist.h"
#include "blackmisc/aviation/airlineicaocodelist.h"
#include "blackmisc/aviation/airportlist.h"
#include "blackmisc/aviation/atcstationlist.h"

View File

@@ -12,6 +12,7 @@
#include "blackgui/models/actionhotkeylistmodel.h"
#include "blackgui/models/aircrafticaolistmodel.h"
#include "blackgui/models/aircraftcategorylistmodel.h"
#include "blackgui/models/aircraftmodellistmodel.h"
#include "blackgui/models/aircraftpartslistmodel.h"
#include "blackgui/models/aircraftsituationlistmodel.h"

View File

@@ -19,6 +19,7 @@ namespace BlackGui
template class CListModelBase<BlackMisc::Aviation::CAtcStationList, true>;
template class CListModelBase<BlackMisc::Aviation::CAirportList, true>;
template class CListModelBase<BlackMisc::Aviation::CAircraftIcaoCodeList, true>;
template class CListModelBase<BlackMisc::Aviation::CAircraftCategoryList, true>;
template class CListModelBase<BlackMisc::Aviation::CAirlineIcaoCodeList, true>;
template class CListModelBase<BlackMisc::Aviation::CAircraftPartsList, true>;
template class CListModelBase<BlackMisc::Aviation::CAircraftSituationList, true>;

View File

@@ -16,6 +16,8 @@
#include "blackmisc/simulation/distributor.h"
#include "blackmisc/aviation/aircrafticaocodelist.h"
#include "blackmisc/aviation/aircrafticaocode.h"
#include "blackmisc/aviation/aircraftcategorylist.h"
#include "blackmisc/aviation/aircraftcategory.h"
#include "blackmisc/aviation/airlineicaocodelist.h"
#include "blackmisc/aviation/airlineicaocode.h"
#include "blackmisc/aviation/liverylist.h"
@@ -114,6 +116,7 @@ namespace BlackGui
template class CListModelDbObjects<BlackMisc::Aviation::CLiveryList, int, true>;
template class CListModelDbObjects<BlackMisc::CCountryList, QString, true>;
template class CListModelDbObjects<BlackMisc::Aviation::CAircraftIcaoCodeList, int, true>;
template class CListModelDbObjects<BlackMisc::Aviation::CAircraftCategoryList, int, true>;
template class CListModelDbObjects<BlackMisc::Aviation::CAirlineIcaoCodeList, int, true>;
template class CListModelDbObjects<BlackMisc::Simulation::CAircraftModelList, int, true>;
template class CListModelDbObjects<BlackMisc::Simulation::CDistributorList, QString, true>;

View File

@@ -0,0 +1,108 @@
/* Copyright (C) 2018
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "blackgui/views/aircraftcategorytreeview.h"
#include "blackgui/models/aircraftcategorytreemodel.h"
#include "blackgui/menus/menuaction.h"
#include "blackmisc/icons.h"
#include "blackconfig/buildconfig.h"
#include <QFlags>
#include <QMenu>
#include <QtGlobal>
#include <QAction>
#include <QModelIndex>
using namespace BlackMisc;
using namespace BlackMisc::Aviation;
using namespace BlackGui::Models;
namespace BlackGui
{
namespace Views
{
CAircraftCategoryTreeView::CAircraftCategoryTreeView(QWidget *parent) : QTreeView(parent)
{
this->setModel(new CAircraftCategoryTreeModel(this));
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, &CAircraftCategoryTreeView::customContextMenuRequested, this, &CAircraftCategoryTreeView::customMenu);
connect(this, &CAircraftCategoryTreeView::expanded, this, &CAircraftCategoryTreeView::onExpanded);
}
void CAircraftCategoryTreeView::updateContainer(const CAircraftCategoryList &categories)
{
if (!this->categoryModel()) { return; }
this->categoryModel()->updateContainer(categories);
this->expandAll();
}
void CAircraftCategoryTreeView::clear()
{
if (!this->categoryModel()) { return; }
this->categoryModel()->clear();
}
void CAircraftCategoryTreeView::setColumns(const CColumns &columns)
{
if (this->categoryModel()) { this->categoryModel()->setColumns(columns); }
}
void CAircraftCategoryTreeView::fullResizeToContents()
{
for (int c = 0; c < this->model()->columnCount(); c++)
{
this->resizeColumnToContents(c);
}
}
const CAircraftCategoryTreeModel *CAircraftCategoryTreeView::categoryModel() const
{
return qobject_cast<const CAircraftCategoryTreeModel *>(this->model());
}
CAircraftCategoryTreeModel *CAircraftCategoryTreeView::categoryModel()
{
return qobject_cast<CAircraftCategoryTreeModel *>(this->model());
}
CAircraftCategory CAircraftCategoryTreeView::selectedObject() const
{
const QModelIndex index = this->currentIndex();
const QVariant data = this->model()->data(index.siblingAtColumn(0)); // supposed to be the callsign
const CAircraftCategoryTreeModel *model = this->categoryModel();
if (!model) { return CAircraftCategory(); }
return model->container().frontOrDefault();
}
QString CAircraftCategoryTreeView::suffixForIndex(const QModelIndex &index)
{
const QVariant data = this->model()->data(index); // supposed to be the suffix
return data.toString();
}
void CAircraftCategoryTreeView::onExpanded(const QModelIndex &index)
{
Q_UNUSED(index);
this->fullResizeToContents();
}
void CAircraftCategoryTreeView::customMenu(const QPoint &point)
{
if (!this->categoryModel()) { return; }
if (this->categoryModel()->container().isEmpty()) { return; }
QMenu *menu = new QMenu(this); // menu
QAction *resize = new QAction(CIcons::resize16(), "Resize", this);
connect(resize, &QAction::triggered, this, &CAircraftCategoryTreeView::fullResizeToContents);
menu->addAction(resize);
menu->popup(this->viewport()->mapToGlobal(point));
}
} // namespace
} // namespace

View File

@@ -0,0 +1,77 @@
/* Copyright (C) 2018
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKGUI_VIEWS_AIRCRAFTCATEGORYTREEVIEW_H
#define BLACKGUI_VIEWS_AIRCRAFTCATEGORYTREEVIEW_H
#include "blackgui/blackguiexport.h"
#include "blackmisc/aviation/aircraftcategorylist.h"
#include <QTreeView>
#include <QObject>
#include <QPoint>
#include <QMap>
#include <QModelIndex>
namespace BlackGui
{
namespace Models
{
class CAircraftCategoryTreeModel;
class CColumns;
}
namespace Views
{
//! ATC stations view
class BLACKGUI_EXPORT CAircraftCategoryTreeView : public QTreeView
{
Q_OBJECT
public:
//! Constructor
explicit CAircraftCategoryTreeView(QWidget *parent = nullptr);
//! Update container
void updateContainer(const BlackMisc::Aviation::CAircraftCategoryList &categories);
//! Clear
void clear();
//! Set columns
void setColumns(const Models::CColumns &columns);
//! Resize all columns
void fullResizeToContents();
private:
//! Used model
const Models::CAircraftCategoryTreeModel *categoryModel() const;
//! Used model
BlackGui::Models::CAircraftCategoryTreeModel *categoryModel();
//! The selected object
BlackMisc::Aviation::CAircraftCategory selectedObject() const;
//! Suffix for index
QString suffixForIndex(const QModelIndex &index);
//! Expanded
void onExpanded(const QModelIndex &index);
//! Custom menu
void customMenu(const QPoint &point);
};
} // ns
} // ns
#endif // guard

View File

@@ -0,0 +1,27 @@
/* Copyright (C) 2019
* swift Project Community / Contributors
*
* This file is part of swift Project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "blackgui/views/aircraftcategoryview.h"
#include "blackgui/views/viewbase.h"
using namespace BlackMisc;
using namespace BlackGui::Models;
namespace BlackGui
{
namespace Views
{
CAircraftCategoryView::CAircraftCategoryView(QWidget *parent) :
CViewWithDbObjects(parent)
{
this->standardInit(new CAircraftCategoryListModel(this));
this->setMenu(MenuDefaultDbViews);
}
}
} // namespace

View File

@@ -0,0 +1,35 @@
/* Copyright (C) 2019
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKGUI_VIEWS_AIRCRAFTCATEGORYVIEW_H
#define BLACKGUI_VIEWS_AIRCRAFTCATEGORYVIEW_H
#include "blackgui/models/aircraftcategorylistmodel.h"
#include "blackgui/views/viewdbobjects.h"
#include "blackgui/blackguiexport.h"
namespace BlackGui
{
namespace Views
{
//! Aircraft ICAO codes view
class BLACKGUI_EXPORT CAircraftCategoryView :
public CViewWithDbObjects<Models::CAircraftCategoryListModel>
{
Q_OBJECT
public:
//! Constructor
explicit CAircraftCategoryView(QWidget *parent = nullptr);
};
} // ns
} // ns
#endif // guard

View File

@@ -18,6 +18,7 @@ namespace BlackGui
template class CViewBase<BlackGui::Models::CAircraftSituationListModel>;
template class CViewBase<BlackGui::Models::CAircraftSituationChangeListModel>;
template class CViewBase<BlackGui::Models::CAirlineIcaoCodeListModel>;
template class CViewBase<BlackGui::Models::CAircraftCategoryListModel>;
template class CViewBase<BlackGui::Models::CAircraftIcaoCodeListModel>;
template class CViewBase<BlackGui::Models::CAirportListModel>;
template class CViewBase<BlackGui::Models::CAtcStationListModel>;

View File

@@ -233,6 +233,7 @@ namespace BlackGui
// see here for the reason of thess forward instantiations
// https://isocpp.org/wiki/faq/templates#separate-template-fn-defn-from-decl
template class CViewWithDbObjects<BlackGui::Models::CAircraftIcaoCodeListModel>;
template class CViewWithDbObjects<BlackGui::Models::CAircraftCategoryListModel>;
template class CViewWithDbObjects<BlackGui::Models::CAirlineIcaoCodeListModel>;
template class CViewWithDbObjects<BlackGui::Models::CCountryListModel>;
template class CViewWithDbObjects<BlackGui::Models::CLiveryListModel>;