📄 flyingmain.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <SysInit.hpp>
#include <jpeg.hpp>
#include <math.h>
#pragma hdrstop
#include "FlyingMain.h"
#include "FlyingSeatCalc.h"
#include "FlyingSupport.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxControls"
#pragma link "cxSSheet"
#pragma resource "*.dfm"
TFlyingMainForm *FlyingMainForm;
//---------------------------------------------------------------------------
__fastcall TFlyingMainForm::TFlyingMainForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFlyingMainForm::FormCreate(TObject *Sender)
{
TResourceStream *Rs;
Rs = new TResourceStream((int)HInstance, "DEFAULTSHEET", RT_RCDATA); // Resource Stream
try {
cxSheetBook->LoadFromStream(Rs); // Load the sheet from the resource stream
cxSheetBook->ShowHeaders = false; // No Headers
cxSheetBook->ShowGrid = false; // No Grid
}
__finally {
delete Rs; // Free the Resource stream
}
fMenuItemsCount = cxSheetBook->PageCount; // Get the number of pages in the book
for(int i = 0; i < fMenuItemsCount; i++) // For each Page
{
fMenuItems = new TMenuItem(this); // Create the menu item
fMenuItems->Caption = cxSheetBook->Pages[i]->Caption; // Set the caption
fMenuItems->Tag = i; // Set the tag
fMenuItems->OnClick = moSelectPlaneClick; // Set the event
mmAircraft->Add(fMenuItems); // Now add the menu item to the aircraft menu
}
fDepCofGColour = clRed; // Set default departure C of G Colour
fArrCofGColour = clYellow; // Set default arrival C of G Colour
fNormalCellColour = GetCellColour(0, 0); // Get the normal cell colour
fErrorCellColour = GetCellColour(2, 17); // Get the error cell colour
SetupPlane(); // Read the variables for this plane
}
//---------------------------------------------------------------------------
void __fastcall TFlyingMainForm::FormDestroy(TObject *Sender)
{
if (fCofGEnvelope != NULL)
delete [] fCofGEnvelope;
}
void __fastcall TFlyingMainForm::moSelectPlaneClick(TObject *Sender) // They selected aplane from the menu
{
if (cxSheetBook->ActivePage != ((TMenuItem*)Sender)->Tag)
{
cxSheetBook->ActivePage = ((TMenuItem*)Sender)->Tag; // Get the sheet they selected
Application->ProcessMessages(); // breath a while
SetupPlane(); // read the plane variables
Caption = Format("ExpressSpreadSheet Demo : %s Load Sheet", ARRAYOFCONST((fPlaneReg))); // Set the form title
moFuelTabs->Enabled = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TFlyingMainForm::FormShow(TObject *Sender)
{
moTodayClick(Sender); // Set today's date
cxSheetBook->ActiveCell = Point(2, 3); // Set the active cell
cxSheetBookSetSelection(Sender, cxSheetBook->ActiveSheet); // Trigger the display of cell hints
}
//---------------------------------------------------------------------------
void __fastcall TFlyingMainForm::moTodayClick(TObject *Sender)
{
TcxSSCellObject *CellObject = cxSheetBook->ActiveSheet->GetCellObject(2, 0);
try {
CellObject->Text = "=now()"; // Set formula
}
__finally {
delete CellObject; // Free the cell object
}
cxSheetBook->Recalc(); // Recalc
}
//---------------------------------------------------------------------------
void __fastcall TFlyingMainForm::cxSheetBookSetSelection(TObject *Sender,
TcxSSBookSheet *ASheet) // Selection has changed update the hint in the status bar
{
const // A B C D
int HelpIds[21][4] = {{0, 0, 1, 0}, // 1
{0, 0, 0, 0}, // 2
{0, 0, 0, 0}, // 3
{0, 0, 2, 0}, // 4
{0, 0, 0, 0}, // 5
{0, 0, 0, 0}, // 6
{0, 0, 0, 0}, // 7
{0, 0, 0, 0}, // 8
{0, 0, 0, 3}, // 9
{0, 0, 0, 4}, // 10
{0, 0, 0, 0}, // 11
{0, 0, 0, 0}, // 12
{0, 0, 0, 0}, // 13
{0, 0, 0, 0}, // 14
{0, 5, 0, 0}, // 15
{0, 5, 0, 0}, // 16
{0, 6, 0, 0}, // 17
{0, 0, 7, 0}, // 18
{0, 0, 0, 0}, // 19
{0, 0, 8, 0}, // 20
{0, 0, 0, 0}}; // 21
const
String HelpText[9] = {"The date the flight is being made",
"Flight duration in Hrs {ie 210 Nm @ 95 Knts Enter = 210 / 95 or 2.21}",
"The minimum required fuel for the flight",
"The actual fuel onboard the aircraft on departure",
"Wt of seat occupants {Right click for a Wizard}",
"Total weight of those items in the baggage compartment 1",
"The C of G on Take-off",
"The C of G on Landing",
"Total weight of those items in the baggage compartment 2"
};
TPoint fCell = ASheet->ActiveCell; // Get the Active Cell;
int fHelpId; // The Help ID
if ((fCell.y < 20) && (fCell.x < 4)) { // If we are on the active area of the sheet
fHelpId = HelpIds[fCell.y][fCell.x]; // Get the helpid
if (fMaxPersons == 2) { // If this is a 2 seater it has two baggage areas
if (fCell.y == 15) // If we are on the Second seat in a 4 seater then display the baggage help
fHelpId++; // bump the help id
else if (fCell.y == 16) // If we are on the baggae area on a 4 seater the display special help for second baggae area
fHelpId = 9; // specila 2 setare second baggae area
}
}
else
fHelpId = 0; // ensure no help for out of limits
if (fHelpId != 0) // if we have help
StatusBar->Panels->Items[0]->Text = HelpText[fHelpId - 1]; // update the status bar
else
StatusBar->Panels->Items[0]->Text = ""; // clear the last help
}
//---------------------------------------------------------------------------
void __fastcall TFlyingMainForm::cxSheetBookSheetPopupMenu(TObject *Sender,
int X, int Y)
{
TPoint fCell = cxSheetBook->ActiveSheet->ActiveCell; // The active cell
pmWizard->Visible = false; // ensure wizard is hidden, it only applies to seats
if (fCell.x == 1) { // If we are in column 2
if (fCell.y == 14) // Front Seats for both planes
pmWizard->Visible = true; // Show the wizard option
else
pmWizard->Visible = (fCell.y == 15) &&
(fMaxPersons == 4); // Also need the wizard for a 4 seater in the back
}
ssPm->Popup(X, Y); // popup the wizrd
}
//---------------------------------------------------------------------------
void __fastcall TFlyingMainForm::cxSheetBookTopLeftChanging(
TcxSSBookSheet *Sender, TPoint &ATopLeft)
{
ATopLeft.x = 0; // Don't move from top cell
ATopLeft.y = 0;
}
//---------------------------------------------------------------------------
void __fastcall TFlyingMainForm::cxSheetBookAfterCalculation(
TObject *Sender)
{
Pb->Repaint(); // Update the graph
CheckFigures(); // Check figures for validity
}
//---------------------------------------------------------------------------
bool TFlyingMainForm::CofGOutsideEnvelope(const double aCofG, const double aWeight) // Check weightr and C of G inside safety envelope
{
int X = Round(aWeight * 10); // Scale up X
int Y = Round(aCofG * 100); // Scale up Y
bool res;
HRGN fRegion = CreatePolygonRgn(fCofGEnvelope, fEnvelopeSize, WINDING); // Create the region
try {
res = !PtInRegion(fRegion, X, Y); // Use window to do the work
}
__finally {
DeleteObject(fRegion); // Delete the region
}
return res;
}
void __fastcall TFlyingMainForm::UpdateCofG() // Update the C of G
{
Pb->Repaint(); // Repaint the paint box
}
void __fastcall TFlyingMainForm::CheckFigures()
{
double fVal1, fVal2; // Local vars for figures
UpdateCofG(); // Ensure we are up to date on the graph
SetCellMessage(0, 10, "", fNormalCellColour, 1); // Clear the upper error row
SetCellMessage(0, 20, "", fNormalCellColour, 1); // Clear the lower error row
fVal1 = GetCellDouble(3, 8); // Get Calculated minimum fuel
if (fVal1 > fMaxFuelCapacity){ // if we exceed the maximum fuel capacity
SetCellMessage(0, 10, Format("Maximum fuel capacity exceeded by %0.2f Ltrs",
ARRAYOFCONST((fVal1 - fMaxFuelCapacity))), 2, 1); // Display the error
return; // No more messages
}
fVal2 = GetCellDouble(3, 9); // Get Actual fuel
if (fVal1 > fVal2) // If actual fuel is less than we need
{
SetCellMessage(0, 10, Format("%0.2f more Ltrs of fuel required", ARRAYOFCONST((fVal1 - fVal2)) ), fErrorCellColour,
1); // Display the error
return; // no more errors
}
if (fVal2 > fMaxFuelCapacity) // If Actual fuel is more than the capacity ?
{
SetCellMessage(0, 10, "Maximum Fuel load exceeded", 23, 1); // Display the error
return; // No more errors
}
fVal1 = GetCellDouble(1, 17); // Get Take Off Weight
if (fVal1 > fMaxTakeOffWt) // If we are more that certified max
{
SetCellMessage(0, 20, Format("Maximum Take Off Weight exceeded by %0.2f %s",
ARRAYOFCONST((fVal1 - fMaxTakeOffWt, fWeightUnits))), 2, 1); // Display the error
return; // No more errors
}
fVal2 = GetCellDouble(2, 17) * fCofgMultiplier; // Get the Take of C of G
// Get Take Off C of G
if (CofGOutsideEnvelope(fVal2, fVal1)) // If outside envelope
{
SetCellMessage(0, 20, "Take Off C of G outside safe envelope", 2, 1); // Display the error
return; // No more errors
}
fVal1 = GetCellDouble(1, 19); // Get Landing Weight
if (fVal1 > fMaxLandingWt) // Check against max allowed
{
SetCellMessage(0, 20, Format("Maximum Landing Weight exceeded by %0.2f %s",
ARRAYOFCONST((fVal1 - fMaxTakeOffWt, fWeightUnits))), 2, 1); // Display the error
return; // No more errors
}
fVal2 = GetCellDouble(2, 19) * fCofgMultiplier; // Get the landing C of G
// Get Landing C of G
if (CofGOutsideEnvelope(fVal2, fVal1)) // Check envelope
SetCellMessage(0, 20, "Landing C of G outside safe envelope", 2, 1); // Display the error
}
void __fastcall TFlyingMainForm::SetupPlane()
{
LoadPlaneVars(); // Load the plane variables
Image1->Picture->Bitmap->LoadFromResourceName((int)HInstance,
fPlaneCofgBitmap); // Load the C of G bitmap
LoadImage(fPlanePhoto, Image2); // Load the photo
try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -