swift
Loading...
Searching...
No Matches
atcstationtreeview.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#include <QtGlobal>
10
11#include "gui/menus/menuaction.h"
15#include "misc/icons.h"
16
17using namespace swift::misc;
18using namespace swift::misc::aviation;
19using namespace swift::gui::models;
20
21namespace swift::gui::views
22{
24 {
25 this->setModel(new CAtcStationTreeModel(this));
27 connect(this, &CAtcStationTreeView::customContextMenuRequested, this, &CAtcStationTreeView::customMenu);
28 connect(this, &CAtcStationTreeView::expanded, this, &CAtcStationTreeView::onExpanded, Qt::QueuedConnection);
29 connect(this->selectionModel(), &QItemSelectionModel::selectionChanged, this, &CAtcStationTreeView::onSelected,
31 }
32
34 {
35 if (!this->stationModel())
36 {
37 return;
38 }
39 this->stationModel()->changedAtcStationConnectionStatus(station, added);
40 }
41
43 {
44 if (!this->stationModel())
45 {
46 return;
47 }
48 this->storeState();
49 this->stationModel()->updateContainer(stations);
50 this->restoreState();
51 }
52
54 {
55 if (!this->stationModel())
56 {
57 return;
58 }
59 this->stationModel()->clear();
60 }
61
62 bool CAtcStationTreeView::isEmpty() const { return this->stationModel()->rowCount() < 1; }
63
65 {
66 if (this->stationModel())
67 {
68 this->stationModel()->setColumns(columns);
69 }
70 }
71
72 void CAtcStationTreeView::fullResizeToContents() { m_dsFullResize.inputSignal(); }
73
74 void CAtcStationTreeView::fullResizeToContentsImpl()
75 {
76 if (this->isEmpty())
77 {
78 return;
79 }
80 for (int c = 0; c < this->model()->columnCount(); c++)
81 {
83 }
84 }
85
86 const CAtcStationTreeModel *CAtcStationTreeView::stationModel() const
87 {
89 }
90
91 CAtcStationTreeModel *CAtcStationTreeView::stationModel()
92 {
94 }
95
96 CAtcStation CAtcStationTreeView::selectedObject() const
97 {
98 const QModelIndex index = this->currentIndex();
99 return this->selectedObject(index);
100 }
101
102 CAtcStation CAtcStationTreeView::selectedObject(const QModelIndex &index) const
103 {
104 const QVariant data = this->model()->data(index.siblingAtColumn(0)); // supposed to be the callsign
105 const QString callsign = data.toString();
106 const CAtcStationTreeModel *model = this->stationModel();
107 if (!model)
108 {
109 return {};
110 }
111 return model->container().findFirstByCallsign(CCallsign(callsign, CCallsign::Atc));
112 }
113
114 QString CAtcStationTreeView::suffixForIndex(const QModelIndex &index)
115 {
116 const QVariant data = this->model()->data(index); // supposed to be the suffix
117 return data.toString();
118 }
119
120 void CAtcStationTreeView::onExpanded(const QModelIndex &index)
121 {
122 Q_UNUSED(index);
123 this->fullResizeToContents();
124 }
125
126 void CAtcStationTreeView::onSelected(const QItemSelection &selected, const QItemSelection &deselected)
127 {
128 Q_UNUSED(deselected);
129 if (selected.isEmpty())
130 {
131 return;
132 }
133 const CAtcStation atcStation = this->selectedObject(selected.indexes().front());
134 if (!atcStation.hasCallsign())
135 {
136 return;
137 }
138 emit this->objectSelected(atcStation);
139 }
140
141 void CAtcStationTreeView::customMenu(const QPoint &point)
142 {
143 if (!this->stationModel())
144 {
145 return;
146 }
147 if (this->stationModel()->container().isEmpty())
148 {
149 return;
150 }
151
152 auto *menu = new QMenu(this); // menu
153
154 auto *com1 = new QAction(CIcons::appCockpit16(), "Tune in COM1 (active)", this);
155 auto *com2 = new QAction(CIcons::appCockpit16(), "Tune in COM2 (active)", this);
156 auto *com1_stby = new QAction(CIcons::appCockpit16(), "Tune in COM1 (standby)", this);
157 auto *com2_stby = new QAction(CIcons::appCockpit16(), "Tune in COM2 (standby)", this);
158 auto *text = new QAction(CIcons::appTextMessages16(), "Show text messages", this);
159 auto *resize = new QAction(CIcons::resize16(), "Resize", this);
160
161 connect(com1, &QAction::triggered, this, [this]() { tuneInAtc(CComSystem::Com1, true); });
162 connect(com2, &QAction::triggered, this, [this]() { tuneInAtc(CComSystem::Com2, true); });
163 connect(com1_stby, &QAction::triggered, this, [this]() { tuneInAtc(CComSystem::Com1, false); });
164 connect(com2_stby, &QAction::triggered, this, [this]() { tuneInAtc(CComSystem::Com2, false); });
165 connect(text, &QAction::triggered, this, &CAtcStationTreeView::requestTextMessage);
166 connect(resize, &QAction::triggered, this, &CAtcStationTreeView::fullResizeToContentsImpl);
167
168 menu->addAction(com1);
169 menu->addAction(com2);
170 menu->addAction(com1_stby);
171 menu->addAction(com2_stby);
172 menu->addAction(text);
173 menu->addSeparator();
174 menu->addAction(resize);
175
176 menu->popup(this->viewport()->mapToGlobal(point));
177 }
178
179 void CAtcStationTreeView::storeState()
180 {
181 m_expanded.clear();
182 for (int row = 0; row < this->model()->rowCount(); ++row)
183 {
184 const QModelIndex i = this->model()->index(row, 0);
185 const bool expanded = this->isExpanded(i);
186 const QString suffix = this->suffixForIndex(i);
187 m_expanded.insert(suffix, expanded);
188 }
189 }
190
191 void CAtcStationTreeView::restoreState()
192 {
193 for (int row = 0; row < this->model()->rowCount(); ++row)
194 {
195 const QModelIndex i = this->model()->index(row, 0);
196 const QString suffix = this->suffixForIndex(i); // suffix of new data
197 const bool expanded = m_expanded.value(suffix, true); // default expanded
198 this->setExpanded(i, expanded);
199 }
200 }
201
202 void CAtcStationTreeView::tuneInAtc(const misc::aviation::CComSystem::ComUnit unit, const bool active)
203 {
204 const CAtcStation s(this->selectedObject());
205 if (s.getCallsign().isEmpty())
206 {
207 return;
208 }
209 emit this->requestComFrequency(s.getFrequency(), unit, active);
210 }
211
212 void CAtcStationTreeView::requestTextMessage()
213 {
214 const CAtcStation s(this->selectedObject());
215 if (s.getCallsign().isEmpty())
216 {
217 return;
218 }
219 emit this->requestTextMessageWidget(s.getCallsign());
220 }
221} // namespace swift::gui::views
COverlayMessagesTreeView(QWidget *parent=nullptr)
Constructor.
Header data for a table.
Definition columns.h:160
void requestComFrequency(const swift::misc::physical_quantities::CFrequency &frequency, swift::misc::aviation::CComSystem::ComUnit unit, bool active)
Request COM frequency.
CAtcStationTreeView(QWidget *parent=nullptr)
Constructor.
void objectSelected(const swift::misc::aviation::CAtcStation &station)
This object has been selected.
void updateContainer(const swift::misc::aviation::CAtcStationList &stations)
Update container.
void requestTextMessageWidget(const swift::misc::aviation::CCallsign &callsign)
Request a text message to.
void fullResizeToContents()
Resize all columns.
void changedAtcStationConnectionStatus(const swift::misc::aviation::CAtcStation &station, bool added)
Used to quickly update single station (better response for the user).
void setColumns(const models::CColumns &columns)
Set columns.
static const QPixmap & appCockpit16()
Application cockpit.
static const QPixmap & resize16()
Resize.
static const QPixmap & appTextMessages16()
Application text messages.
Value object encapsulating information about an ATC station.
Definition atcstation.h:38
bool hasCallsign() const
Has callsign?
Definition atcstation.h:87
Value object for a list of ATC stations.
Models to be used with views, mainly QTableView.
Views, mainly QTableView.
Free functions in swift::misc.
virtual int columnCount(const QModelIndex &parent) const const=0
virtual QVariant data(const QModelIndex &index, int role) const const=0
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual int rowCount(const QModelIndex &parent) const const=0
QModelIndex currentIndex() const const
QAbstractItemModel * model() const const
QItemSelectionModel * selectionModel() const const
QWidget * viewport() const const
void triggered(bool checked)
QModelIndexList indexes() const const
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
bool isEmpty() const const
QModelIndex siblingAtColumn(int column) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
T qobject_cast(QObject *object)
QueuedConnection
CustomContextMenu
void expanded(const QModelIndex &index)
bool isExpanded(const QModelIndex &index) const const
void resizeColumnToContents(int column)
void setExpanded(const QModelIndex &index, bool expanded)
virtual void setModel(QAbstractItemModel *model) override
QString toString() const const
QWidget(QWidget *parent, Qt::WindowFlags f)
void setContextMenuPolicy(Qt::ContextMenuPolicy policy)
void customContextMenuRequested(const QPoint &pos)
QPoint mapToGlobal(const QPoint &pos) const const
void resize(const QSize &)