swift
Loading...
Searching...
No Matches
aircraftmodelview.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
4#include "aircraftmodelview.h"
5
6#include <QAction>
7#include <QDropEvent>
8#include <QMap>
9#include <QShortcut>
10#include <QString>
11#include <QStringBuilder>
12#include <QWidget>
13#include <QtGlobal>
14
16#include "gui/guiapplication.h"
17#include "gui/guiutility.h"
18#include "gui/menus/menuaction.h"
19#include "gui/shortcut.h"
22#include "gui/views/viewbase.h"
28#include "misc/directories.h"
29#include "misc/icons.h"
34#include "misc/variant.h"
35
36using namespace swift::misc;
37using namespace swift::misc::simulation;
38using namespace swift::misc::aviation;
39using namespace swift::gui::models;
40using namespace swift::gui::filters;
41using namespace swift::gui::menus;
42
43namespace swift::gui::views
44{
46 {
47 // default
49 this->setSettingsDirectoryIndex(CDirectories::IndexDirLastModelJsonOrDefault);
50
51 // shortcut
52 auto *stashShortcut = new QShortcut(CShortcut::keyStash(), this);
53 stashShortcut->setContext(Qt::WidgetShortcut);
54 connect(stashShortcut, &QShortcut::activated, this, &CAircraftModelView::requestedStash);
55
56 // default mode
58 this->setAircraftModelMode(mode);
59 this->setSortIndicator();
60 }
61
63 {
64 this->setCustomMenu(nullptr, false); // delete everything
66 this->setSortIndicator();
67 switch (mode)
68 {
69 case CAircraftModelListModel::StashModel: m_menus = MenuDefaultNoClear; break;
70 case CAircraftModelListModel::Database: m_menus = MenuDefaultDbViews; break;
71 case CAircraftModelListModel::VPilotRuleModel: m_menus = MenuDefaultNoClear | MenuStashing; break;
73 m_menus = MenuDefaultNoClear | MenuStashing | MenuLoadAndSave;
74 break;
76 default: m_menus = MenuDefaultNoClear | MenuBackend; break;
77 }
78 }
79
81 {
82 if (!hasSelection())
83 {
84 return 0;
85 }
86 const int c = this->updateSelected(CVariant::fromValue(livery), CAircraftModel::IndexLivery);
87 return c;
88 }
89
91 {
92 if (!hasSelection())
93 {
94 return 0;
95 }
96 const int c = this->updateSelected(CVariant::fromValue(icao), CAircraftModel::IndexAircraftIcaoCode);
97 return c;
98 }
99
101 {
102 if (!hasSelection())
103 {
104 return 0;
105 }
106 const int c = this->updateSelected(CVariant::fromValue(distributor), CAircraftModel::IndexDistributor);
107 return c;
108 }
109
111 {
112 if (!hasSelection())
113 {
114 return 0;
115 }
116 const int c = this->updateSelected(vm);
117 return c;
118 }
119
121 {
122 return m_menus.testFlag(MenuCanStashModels) && hasSelection();
123 }
124
126 {
127 Q_ASSERT(m_model);
128 m_model->setAcceptedMetaTypeIds({
129 qMetaTypeId<CAirlineIcaoCode>(),
130 qMetaTypeId<CAirlineIcaoCodeList>(),
131 qMetaTypeId<CAircraftIcaoCode>(),
132 qMetaTypeId<CAircraftIcaoCodeList>(),
133 qMetaTypeId<CLivery>(),
134 qMetaTypeId<CLiveryList>(),
135 qMetaTypeId<CDistributor>(),
136 qMetaTypeId<CDistributorList>(),
137 qMetaTypeId<CAircraftModel>(),
138 qMetaTypeId<CAircraftModelList>(),
139 });
140 }
141
143
148
150 Qt::CaseSensitivity sensitivity)
151 {
152 if (modelStrings.isEmpty())
153 {
154 return 0;
155 }
157 const int delta = copy.removeModelsWithString(modelStrings, sensitivity);
158 if (delta > 0)
159 {
160 this->updateContainerMaybeAsync(copy);
161 }
162 return delta;
163 }
164
170
172 Qt::CaseSensitivity sensitivity)
173 {
174 if (models.isEmpty())
175 {
176 return 0;
177 }
179 int c = copy.replaceOrAddModelsWithString(models, sensitivity);
180 if (c == 0)
181 {
182 return 0;
183 }
184 this->updateContainerMaybeAsync(copy);
185 return c;
186 }
187
189 {
190 this->derivedModel()->setHighlightModelStrings(highlightModelsStrings);
191 }
192
194 {
195 this->clearHighlighting();
196 CAircraftModelList invaliddModels;
197 invaliddModels.loadInvalidModels();
198 if (!invaliddModels.isEmpty())
199 {
200 this->setHighlight(true);
202 this->setHighlightModels(invaliddModels);
203
204 const int r = this->rowOf(invaliddModels.front());
205 const QModelIndex i = m_model->index(r, 0);
206 this->scrollTo(i);
207 }
208 }
209
211 {
212 this->derivedModel()->setHighlightModels(highlightModelsList);
213 }
214
215 void CAircraftModelView::setHighlight(bool highlight) { this->derivedModel()->setHighlight(highlight); }
216
218
220
222 {
223 m_correspondingSimulator = simulator;
224 m_correspondingSimulatorDir = simDir;
225 }
226
228 {
229 // moves from myself are ignored
230 // depends on isDropAllowed() / acceptDrop() if this function is called
231 Qt::DropAction action = event->dropAction();
232 if (action == Qt::MoveAction)
233 {
235 return;
236 }
237 if (!this->isDropAllowed())
238 {
239 return;
240 }
241 if (!event)
242 {
243 return;
244 }
245 const QMimeData *mime = event->mimeData();
246 if (!mime)
247 {
248 return;
249 }
250
252 {
253 const CVariant valueVariant(CGuiUtility::fromSwiftDragAndDropData(mime));
254 if (valueVariant.isValid())
255 {
256 if (valueVariant.canConvert<CAircraftModel>())
257 {
258 const auto model = valueVariant.value<CAircraftModel>();
259 if (!model.hasModelString())
260 {
261 return;
262 }
265 return;
266 }
267 else if (valueVariant.canConvert<CAircraftModelList>())
268 {
269 const auto models(valueVariant.value<CAircraftModelList>());
270 if (models.isEmpty())
271 {
272 return;
273 }
275 return;
276 }
277
278 // only for selected members
279 if (!this->hasSelection())
280 {
281 return;
282 }
283 if (valueVariant.canConvert<CAircraftIcaoCode>())
284 {
285 const auto icao = valueVariant.value<CAircraftIcaoCode>();
286 if (icao.validate().hasErrorMessages())
287 {
288 return;
289 }
290 this->applyToSelected(icao);
291 }
292 else if (valueVariant.canConvert<CAircraftIcaoCodeList>())
293 {
294 const auto icaos(valueVariant.value<CAircraftIcaoCodeList>());
295 if (icaos.size() != 1)
296 {
297 return;
298 }
299 const CAircraftIcaoCode icao = icaos.front();
300 if (icao.validate().hasErrorMessages())
301 {
302 return;
303 }
304 this->applyToSelected(icao);
305 }
306 else if (valueVariant.canConvert<CLivery>())
307 {
308 const auto livery = valueVariant.value<CLivery>();
309 if (livery.validate().hasErrorMessages())
310 {
311 return;
312 }
313 this->applyToSelected(livery);
314 }
315 else if (valueVariant.canConvert<CLiveryList>())
316 {
317 const auto liveries(valueVariant.value<CLiveryList>());
318 if (liveries.size() != 1)
319 {
320 return;
321 }
322 const CLivery livery = liveries.front();
323 if (livery.validate().hasErrorMessages())
324 {
325 return;
326 }
327 this->applyToSelected(livery);
328 }
329 else if (valueVariant.canConvert<CDistributor>())
330 {
331 const auto distributor = valueVariant.value<CDistributor>();
332 if (distributor.validate().hasErrorMessages())
333 {
334 return;
335 }
336 this->applyToSelected(distributor);
337 }
338 else if (valueVariant.canConvert<CDistributorList>())
339 {
340 const auto distributors(valueVariant.value<CDistributorList>());
341 if (distributors.size() != 1)
342 {
343 return;
344 }
345 const CDistributor distributor = distributors.front();
346 if (distributor.validate().hasErrorMessages())
347 {
348 return;
349 }
350 this->applyToSelected(distributor);
351 }
352 else if (valueVariant.canConvert<CAirlineIcaoCode>())
353 {
354 const auto airline = valueVariant.value<CAirlineIcaoCode>();
355 if (airline.validate().hasErrorMessages())
356 {
357 return;
358 }
360 airline); // I need to convert to stanard livery, which I can`t do here
361 }
362 else if (valueVariant.canConvert<CAirlineIcaoCodeList>())
363 {
364 const auto airlines(valueVariant.value<CAirlineIcaoCodeList>());
365 if (airlines.size() != 1)
366 {
367 return;
368 }
369 const CAirlineIcaoCode airline = airlines.front();
370 if (airline.validate().hasErrorMessages())
371 {
372 return;
373 }
375 airline); // I need to convert to stanard livery, which I can`t do here
376 }
377 }
378 }
379 else
380 {
382 }
383 }
384
386 {
387 // Statistics and validation
388 if (!this->isEmpty())
389 {
390 menuActions.addAction(CIcons::appAircraft16(), "Model statistics", CMenuAction::pathModel(),
391 { this, &CAircraftModelView::displayModelStatisticsDialog });
392 if (m_withValidationContextMenu)
393 {
394 QDialog *parentDialog = CGuiUtility::findParentDialog(this);
395 if (!(parentDialog && (qobject_cast<CAircraftModelValidationDialog *>(parentDialog))))
396 {
397 menuActions.addAction(CIcons::disk16(), "Model validation (selected)", CMenuAction::pathModel(),
398 { this, &CAircraftModelView::displayModelValidationDialog });
399 }
400 }
402 {
403 menuActions.addAction(CIcons::disk16(), "Highlight invalid models (from file)",
406 }
407 }
408
409 // Stash menus
410 bool addStashMenu = false;
411 if (m_menus.testFlag(MenuCanStashModels))
412 {
414 {
415 CMenuActions ma;
417 { this, &CAircraftModelView::requestedStash });
418 QAction *added =
419 ma.addAction(CIcons::appDbStash16(), "Stashing clears selection (on/off)",
420 CMenuAction::pathModelStash(), { this, &CAircraftModelView::stashingClearsSelection });
421 added->setCheckable(true);
423 }
424
425 // modify menu items
426 const int selected = this->selectedRowCount();
427 const bool tooMany = selected > 500;
428 const bool canStash = selected > 0 && !tooMany;
430 a->setEnabled(canStash);
432 a->setChecked(m_stashingClearsSelection);
433 addStashMenu = true;
434 }
435 if (m_menus.testFlag(MenuHighlightStashed))
436 {
437 // this function requires that someone provides the model strings to be highlighted
439 {
440 CMenuActions ma;
441 QAction *added =
442 ma.addAction(CIcons::appDbStash16(), "Highlight stashed (on/off)", CMenuAction::pathModelStash(),
443 { this, &CAircraftModelView::toggleHighlightStashedModels });
444 added->setCheckable(true);
446 }
449 addStashMenu = true;
450 }
451
452 // client specific
453 if (m_menus.testFlag(MenuDisableModelsTemp) && this->hasSelection())
454 {
456 {
457 CMenuActions ma;
458 ma.addAction(CIcons::delete16(), "Temp.disable model", CMenuAction::pathModel(),
459 { this, &CAircraftModelView::requestTempDisable });
461 }
463 }
464
465 if (addStashMenu)
466 {
467 menuActions.addMenuStash();
468 }
469
470 // base class menus
472 }
473
475 {
476 if (m_correspondingSimulator.isNoSimulator())
477 {
478 return {};
479 }
480 if (models.isEmpty())
481 {
482 return { this, CStatusMessage::SeverityDebug, u"Empty models", true };
483 }
484
485 // multiple sims with same count
486 const int removed = models.removeIfNotMatchingSimulator(m_correspondingSimulator);
487 if (removed < 1)
488 {
489 return {};
490 }
491 return CStatusMessage(this, CStatusMessage::SeverityWarning, u"Reduced by %1 model(s) to only use %2 models",
492 true)
493 << removed << m_correspondingSimulator.toQString(true);
494 }
495
497 {
498 if (models.isEmpty())
499 {
501 }
502 if (m_correspondingSimulator.isNoSimulator())
503 {
505 }
506 if (models.containsNotMatchingSimulator(m_correspondingSimulator))
507 {
508 return CStatusMessage(this, CStatusMessage::SeverityError, u"Found entry not matching %1 in model data",
509 true)
510 << m_correspondingSimulator.toQString();
511 }
513 }
514
516 {
517 if (models.isEmpty())
518 {
520 }
521 else
522 {
524 }
525 }
526
527 void CAircraftModelView::toggleHighlightStashedModels()
528 {
529 const bool h = derivedModel()->highlightModels();
532 }
533
534 void CAircraftModelView::toggleHighlightInvalidModels()
535 {
536 const bool h = this->highlightModels();
537 this->setHighlight(!h);
538 }
539
540 void CAircraftModelView::stashingClearsSelection() { m_stashingClearsSelection = !m_stashingClearsSelection; }
541
542 void CAircraftModelView::requestedStash()
543 {
544 if (!m_menus.testFlag(MenuCanStashModels))
545 {
546 return;
547 }
548 if (!this->hasSelection())
549 {
550 return;
551 }
552 const CAircraftModelList models(this->selectedObjects());
553 emit this->requestStash(models);
554 if (m_stashingClearsSelection)
555 {
556 this->clearSelection();
557 }
559 CStatusMessage(CStatusMessage::SeverityInfo, u"Stashed " % models.getModelStringList(true).join(" ")));
560 }
561
562 void CAircraftModelView::requestTempDisable()
563 {
564 if (!m_menus.testFlag(MenuDisableModelsTemp))
565 {
566 return;
567 }
568 if (!sGui || sGui->isShuttingDown())
569 {
570 return;
571 }
572 if (!this->hasSelection())
573 {
574 return;
575 }
576
577 const CAircraftModelList selectedModels(this->selectedObjects());
579 u"Temp.disabled " % selectedModels.getModelStringList(true).join(" ")));
580 emit this->requestTempDisableModelsForMatching(selectedModels);
581 }
582
583 void CAircraftModelView::displayModelStatisticsDialog()
584 {
585 if (!m_statisticsDialog)
586 {
587 m_statisticsDialog = new CAircraftModelStatisticsDialog(this);
588 }
589 m_statisticsDialog->analyzeModels(this->container());
590 m_statisticsDialog->exec();
591 }
592
593 void CAircraftModelView::displayModelValidationDialog()
594 {
595 if (!m_fileValidationDialog)
596 {
597 m_fileValidationDialog = new CAircraftModelValidationDialog(this);
598 }
599 m_fileValidationDialog->setModels(this->selectedObjects(), m_correspondingSimulator,
600 m_correspondingSimulatorDir);
601 m_fileValidationDialog->triggerValidation(1000);
602 m_fileValidationDialog->exec();
603 }
604} // namespace swift::gui::views
bool isShuttingDown() const
Is application shutting down?
bool displayInStatusBar(const swift::misc::CStatusMessage &message)
direct access to main application window
static QDialog * findParentDialog(QWidget *widget)
Find parent dialog if there is any, otherwise null.
static swift::misc::CVariant fromSwiftDragAndDropData(const QMimeData *mime)
From text dropped.
static bool hasSwiftVariantMimeType(const QMimeData *mime)
Mime data with swift type.
static const QKeySequence & keyStash()
For stashing models.
Definition shortcut.cpp:12
static const QString & pathModel()
Model.
Definition menuaction.h:143
static const QString & pathModelStash()
Stash sub menu.
Definition menuaction.h:171
Bunch of CMenuAction objects.
Definition menuaction.h:384
CMenuAction addMenuStash()
Stash menu.
CMenuAction last() const
Last action.
Definition menuaction.h:488
CMenuActions addActions(const CMenuActions &actions)
Add menu actions, returns last valid QAction.
CMenuAction addAction(const CMenuAction &menuAction)
Add menu action.
CMenuAction first() const
First action.
Definition menuaction.h:485
void setAircraftModelMode(CAircraftModelListModel::AircraftModelMode stationMode)
Mode.
void setHighlight(bool highlightModels)
Highlight models.
void setHighlightColor(const QBrush &brush)
The highlight color.
@ OwnAircraftModelMappingTool
models of my simulator, but in mapping mode
@ OwnAircraftModelClient
models existing for my simulator
void setHighlightModels(const swift::misc::simulation::CAircraftModelList &highlightModels)
Highlight models.
void replaceOrAddByModelString(const swift::misc::simulation::CAircraftModelList &models)
Replace models with same model string, or just add.
void setHighlightModelStrings(const QStringList &modelStrings=QStringList())
Highlight models.
void customMenu(menus::CMenuActions &menuActions)
Method creating the menu.
bool highlightModels() const
Highlight models.
void setHighlightColor(const QBrush &brush)
The highlight color.
int removeModelsWithModelString(const QStringList &modelStrings, Qt::CaseSensitivity sensitivity=Qt::CaseInsensitive)
Remove models with model strings.
void setAircraftModelMode(models::CAircraftModelListModel::AircraftModelMode mode)
Set display mode.
swift::gui::filters::CAircraftModelFilterDialog * getFilterDialog() const
Filter dialog if any.
void jsonLoadedAndModelUpdated(const swift::misc::simulation::CAircraftModelList &models)
In swift::gui::views::CViewBaseNonTemplate::loadJson the view has been updated because of loaded JSON...
swift::misc::CStatusMessage modifyLoadedJsonData(swift::misc::simulation::CAircraftModelList &models) const
Modify JSON data loaded in swift::gui::views::CViewBaseNonTemplate::loadJson.
swift::misc::CStatusMessage validateLoadedJsonData(const swift::misc::simulation::CAircraftModelList &models) const
Verify JSON data loaded in swift::gui::views::CViewBaseNonTemplate::loadJson.
void setHighlightModelsForInvalidModels()
Set the highlight models based on the saved file.
void requestTempDisableModelsForMatching(const swift::misc::simulation::CAircraftModelList &models)
Temp.disable model.
int replaceOrAddModelsWithString(const swift::misc::simulation::CAircraftModelList &models, Qt::CaseSensitivity sensitivity=Qt::CaseInsensitive)
Replace models with sme model string, otherwise add.
void setHighlight(bool highlight)
Highlight models.
void setHighlightModels(const swift::misc::simulation::CAircraftModelList &highlightModelsList)
Highlight models.
void setCorrespondingSimulator(const swift::misc::simulation::CSimulatorInfo &simulator, const QString &simDir)
Corresponding simulator.
void jsonModelsForSimulatorLoaded(const swift::misc::simulation::CSimulatorInfo &simulator)
Models for simulator loaded (JSON).
void toggledHighlightStashedModels()
Highlight stashed models has been toggled.
void requestHandlingOfStashDrop(const swift::misc::aviation::CAirlineIcaoCode &airlineIcao)
Request further handling of drops I cannot handle on my own.
void setHighlightModelStrings(const QStringList &highlightModelsStrings)
Highlight models.
bool hasSelectedModelsToStash() const
Has any models to stash and it is allowed to stash.
int applyToSelected(const swift::misc::aviation::CLivery &livery)
Apply to selected objects.
CAircraftModelView(QWidget *parent=nullptr)
Constructor.
void addFilterDialog()
Add my own filter dialog.
void setAcceptedMetaTypeIds()
Add the technically supported metatypes allowed for drag and drop.
void requestStash(const swift::misc::simulation::CAircraftModelList &models)
Request to stash if applicable.
void customMenu(swift::gui::menus::CMenuActions &menuActions)
Method creating the menu.
void updateContainerMaybeAsync(const ContainerType &container, bool sort=true, bool resize=true)
Definition viewbase.cpp:120
virtual swift::misc::CStatusMessage validateLoadedJsonData(const ContainerType &data) const
Verify JSON data loaded in swift::gui::views::CViewBaseNonTemplate::loadJson.
Definition viewbase.cpp:771
int updateSelected(const swift::misc::CVariant &variant, const swift::misc::CPropertyIndex &index)
Definition viewbase.cpp:366
void dropEvent(QDropEvent *event)
Definition viewbase.cpp:515
menus::IMenuDelegate * setCustomMenu(menus::IMenuDelegate *menu, bool nestPreviousMenu=true)
Set custom menu if applicable.
Menu m_menus
Default menu settings.
Definition viewbase.h:603
int selectedRowCount() const
Number of selected rows.
void setSettingsDirectoryIndex(swift::misc::CDirectories::ColumnIndex directoryIndex)
Index of the directory we "remember".
Definition viewbase.h:333
QMap< MenuFlag, menus::CMenuActions > m_menuFlagActions
initialized actions for menu flag (enum)
Definition viewbase.h:608
QWidget * getFilterWidget() const
Filter widget if any.
Definition viewbase.h:268
menus::CMenuActions initMenuActions(MenuFlag menu)
Init menu actions.
void setFilterDialog(filters::CFilterDialog *filterDialog)
Filter dialog.
@ MenuHighlightStashed
highlight stashed models
Definition viewbase.h:142
@ MenuBackend
allow to request data from backend
Definition viewbase.h:123
bool hasSelection() const
Selection (selected rows).
static const QPixmap & appAircraft16()
Application aircrafts.
static const QPixmap & appDbStash16()
Stash.
static const QPixmap & disk16()
Disk.
static const QPixmap & delete16()
Delete.
Specialized value object compliant map for variants, based on indexes.
reference front()
Access the first element.
Definition sequence.h:225
bool isEmpty() const
Synonym for empty.
Definition sequence.h:285
Streamable status message, e.g.
static constexpr auto SeverityInfo
Status severities.
static constexpr auto SeverityError
Status severities.
static constexpr auto SeverityDebug
Status severities.
static constexpr auto SeverityWarning
Status severities.
bool hasErrorMessages() const
Error messages.
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition variant.h:69
static CVariant fromValue(T &&value)
Construct a variant from a value.
Definition variant.h:139
T value() const
Return the value converted to the type T.
Definition variant.h:154
bool isValid() const
True if this variant is valid.
Definition variant.h:230
bool canConvert(int typeId) const
True if this variant can be converted to the type with the given metatype ID.
Value object for ICAO classification.
CStatusMessageList validate() const
Validate data.
Value object encapsulating a list of ICAO codes.
Value object for ICAO classification.
CStatusMessageList validate() const
Validate data.
Value object encapsulating a list of ICAO codes.
Value object encapsulating information about an airpot.
Definition livery.h:29
CStatusMessageList validate() const
Validate data.
Definition livery.cpp:132
Value object for a list of airports.
Definition liverylist.h:29
QString toQString(bool i18n=false) const
Cast as QString.
Definition mixinstring.h:74
Aircraft model (used by another pilot, my models on disk).
Value object encapsulating a list of aircraft models.
QStringList getModelStringList(bool sort=true) const
Model strings.
static bool hasInvalidModelFile()
Save/load invalid models.
CStatusMessage loadInvalidModels()
Save/load invalid models.
CSimulatorInfo simulatorsWithMaxEntries() const
Which simulator(s) have the most entries?
Value object encapsulating information of software distributor.
Definition distributor.h:33
swift::misc::CStatusMessageList validate() const
Validate data.
Value object encapsulating a list of distributors.
Simple hardcoded info about the corresponding simulator.
SWIFT_GUI_EXPORT swift::gui::CGuiApplication * sGui
Single instance of GUI application object.
Filter to search data sets.
Models to be used with views, mainly QTableView.
Views, mainly QTableView.
Free functions in swift::misc.
virtual bool event(QEvent *event) override
QAbstractItemModel * model() const const
void setCheckable(bool)
void setChecked(bool)
void setEnabled(bool)
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
T qobject_cast(QObject *object)
void activated()
CaseSensitivity
DropAction
WidgetShortcut
virtual void scrollTo(const QModelIndex &index, QAbstractItemView::ScrollHint hint) override
virtual void dropEvent(QDropEvent *event)