swift
aircraftcategorytreeview.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2018 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QAction>
7 #include <QMenu>
8 #include <QModelIndex>
9 
10 #include "gui/menus/menuaction.h"
12 #include "misc/icons.h"
13 
14 using namespace swift::misc;
15 using namespace swift::misc::aviation;
16 using namespace swift::gui::models;
17 
18 namespace swift::gui::views
19 {
20  CAircraftCategoryTreeView::CAircraftCategoryTreeView(QWidget *parent) : QTreeView(parent)
21  {
22  this->setModel(new CAircraftCategoryTreeModel(this));
23  this->setContextMenuPolicy(Qt::CustomContextMenu);
24  connect(this, &CAircraftCategoryTreeView::customContextMenuRequested, this,
25  &CAircraftCategoryTreeView::customMenu);
26  connect(this, &CAircraftCategoryTreeView::expanded, this, &CAircraftCategoryTreeView::onExpanded,
27  Qt::QueuedConnection);
28  }
29 
31  {
32  if (!this->categoryModel()) { return; }
33  this->categoryModel()->updateContainer(categories);
34 
37  // using Qt::QueuedConnection seems to fix for expand all
38  // also this->expandToDepth(0) seems to work
39  this->expandAll();
40  }
41 
43  {
44  if (!this->categoryModel()) { return; }
45  this->categoryModel()->clear();
46  }
47 
49 
51  {
52  if (this->categoryModel()) { this->categoryModel()->setColumns(columns); }
53  }
54 
55  void CAircraftCategoryTreeView::fullResizeToContentsImpl()
56  {
57  if (this->isEmpty()) { return; }
58  const int cc = this->categoryModel()->columnCount();
59  for (int c = 0; c < cc; c++) { this->resizeColumnToContents(c); }
60  }
61 
62  bool CAircraftCategoryTreeView::isEmpty() const { return this->categoryModel()->rowCount() < 1; }
63 
64  const CAircraftCategoryTreeModel *CAircraftCategoryTreeView::categoryModel() const
65  {
66  return qobject_cast<const CAircraftCategoryTreeModel *>(this->model());
67  }
68 
69  CAircraftCategoryTreeModel *CAircraftCategoryTreeView::categoryModel()
70  {
71  return qobject_cast<CAircraftCategoryTreeModel *>(this->model());
72  }
73 
74  CAircraftCategory CAircraftCategoryTreeView::selectedObject() const
75  {
76  const CAircraftCategoryTreeModel *model = this->categoryModel();
77  if (!model) { return CAircraftCategory(); }
78  return model->container().frontOrDefault();
79  }
80 
81  void CAircraftCategoryTreeView::onExpanded(const QModelIndex &index)
82  {
83  Q_UNUSED(index);
84  this->fullResizeToContents();
85  }
86 
87  void CAircraftCategoryTreeView::customMenu(const QPoint &point)
88  {
89  if (!this->categoryModel()) { return; }
90  if (this->categoryModel()->container().isEmpty()) { return; }
91 
92  QMenu *menu = new QMenu(this); // menu
93  QAction *resize = new QAction(CIcons::resize16(), "Resize", this);
94  connect(resize, &QAction::triggered, this, &CAircraftCategoryTreeView::fullResizeToContentsImpl);
95 
96  menu->addAction(resize);
97  menu->popup(this->viewport()->mapToGlobal(point));
98  }
99 } // namespace swift::gui::views
const swift::misc::aviation::CAircraftCategoryList & container() const
Get container.
void updateContainer(const swift::misc::aviation::CAircraftCategoryList &categories)
Update container.
void setColumns(const CColumns &columns)
Set columns.
Header data for a table.
Definition: columns.h:160
void updateContainer(const swift::misc::aviation::CAircraftCategoryList &categories)
Update container.
void setColumns(const models::CColumns &columns)
Set columns.
void inputSignal()
Received input signal, or manually trigger.
const_reference frontOrDefault() const
Access the first element, or a default-initialized value if the sequence is empty.
Definition: sequence.h:239
Value object for aircraft categories.
Value object encapsulating a list of ICAO codes.
Models to be used with views, mainly QTableView.
Views, mainly QTableView.
Free functions in swift::misc.