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,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>;