//--------------------------------------------------------------------------- #include #pragma hdrstop #include "BaseYarnDesign_F.h" #include "common.h" #include "Exception.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "PColorGrid" #pragma link "PCMY" #pragma link "PHLS" #pragma link "DIB256s" #pragma link "TPSpin" #pragma resource "*.dfm" TBaseYarnDesignForm *BaseYarnDesignForm; //--------------------------------------------------------------------------- #define IDS_TYPE StringTable[0] #define IDS_COLOR StringTable[1] #define IDS_COLORNUMBER StringTable[2] #define IDS_COLORTABLE StringTable[3] #define IDS_PALETTE StringTable[4] #define IDS_DATA StringTable[5] #define IDS_METHOD StringTable[6] #define IDS_COUNT StringTable[7] #define IDS_TPI StringTable[8] #define IDS_HAIREFFECT StringTable[9] #define IDS_OK StringTable[10] #define IDS_CANCEL StringTable[11] #define IDS_SDIRECTION StringTable[12] #define IDS_ZDIRECTION StringTable[13] //--------------------------------------------------------------------------- struct TBaseData { TYarnMethod Method; TColorData *Color; bool change; TBaseData() { Color = NULL; change = false; } }; //--------------------------------------------------------------------------- __fastcall TBaseYarnDesignForm::TBaseYarnDesignForm(TComponent* Owner) : TForm(Owner) { //=================================================================== StringTable.Create(DirectoryBin, Language, "BaseYarnDesign"); SetSmallFont(Font); SetSmallFont(stTwist->Font); SetSmallFont(lbCount->Font); SetSmallFont(lbUnit->Font); SetSmallFont(lbChoiceIndex->Font); lnType->Caption = IDS_TYPE; gbColor->Caption = IDS_COLOR; Label1->Caption = IDS_COLORNUMBER; Label2->Caption = IDS_COLORTABLE; tsPalette->Caption = IDS_PALETTE; GroupBox1->Caption = IDS_DATA; Label6->Caption = IDS_METHOD; Label5->Caption = IDS_COUNT; Label7->Caption = IDS_TPI; cbHairEffect->Caption = IDS_HAIREFFECT; btnOK->Caption = IDS_OK; btnCancel->Caption = IDS_CANCEL; stTwist->Caption = IDS_SDIRECTION; //=================================================================== } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::FormCreate(TObject *Sender) { ColorIndex = 0; Item = 0; PalIndex = 2; Normal = new TColorData; Melange = new TList; Rainbow = new TList; DIB256Palette->ChoiceIndex = 2; SZ = false; type = false; } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::FormDestroy(TObject *Sender) { ClearData(); delete Normal; delete Melange; delete Rainbow; } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::FormShow(TObject *Sender) { cbType->ItemIndex = FYarnInfor->Detail.Type; Item = FYarnInfor->Detail.Type; // InitBaseData() Áغñ... type = true; // | seColors->Value = FYarnInfor->Colors; // | type = false; //------ InitBaseData(); cbTypeChange(cbType); if (FYarnInfor->Detail.SZ) { stTwist->Caption = IDS_ZDIRECTION; SZ = true; } else { stTwist->Caption = IDS_SDIRECTION; SZ = false; } seTPI->Value = FYarnInfor->Detail.TPI; cbHairEffect->Checked = FYarnInfor->Detail.Hair; seCount->Value = FYarnInfor->Detail.Count; lbUnit->Caption = FYUnit.Name(); btnOK->SetFocus(); //lhskys ¿£ÅÍ·Î ½ÇÇàÇϱâ À§ÇØ focus¸¦ ÁØ´Ù } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::cbTypeChange(TObject *Sender) { type = true; Item = cbType->ItemIndex; switch (cbType->ItemIndex) { case 0: ChangeNormal(); seColors->Enabled = false; tsDensityLength->Caption = "None"; tsDensityLength->Enabled = false; lbDenLen->Caption = ""; break; case 1: ChangeMelange(); seColors->Enabled = true; tsDensityLength->Enabled = true; tsDensityLength->Caption = "Density"; lbDenLen->Caption = "%"; break; case 2: ChangeRainbow(); seColors->Enabled = true; tsDensityLength->Enabled = true; tsDensityLength->Caption = "Length"; lbDenLen->Caption = (inch)? "Inch" : "Cm"; break; } sbTable->Position = 0; sbTable->Enabled = (seColors->Value > 17)? 1 : 0; ColorIndex = 0; lbChoiceIndex->Caption = "1"; type = false; } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::seColorsChange(TObject *Sender) { int num, i, cnt, k; TBaseData *bd; if (type) return; if (seColors->Text.Length()>0) { if (seColors->Value < 1) { seColors->Value = 1; } if (seColors->Value > 8) { seColors->Value = 8; } ColorIndex = seColors->Value - 1; lbChoiceIndex->Caption = IntToStr((int)seColors->Value); //qe if (cgTable->ColorCount > seColors->Value) { cnt = cgTable->ColorCount - seColors->Value; for (i = 0; i < cnt; i++) DeleteData(); cgTable->ColorCount = seColors->Value; } else { k = cgTable->ColorCount; cnt = seColors->Value - k; cgTable->ColorCount = seColors->Value; for (i = 0; i < cnt; i++) { AddData(DIB256Palette->Palette->ColorData[PalIndex]); cgTable->ReDrawCell(k+i, DIB256Palette->Palette->ColorData[PalIndex]->Color); PalIndex++; } } CalculateMethodNumber(); if (Item == 1) { bd = (TBaseData *) Melange->Items[0]; edDenLen->Text = bd->Method.Number; } else if (Item == 2) { bd = (TBaseData *) Rainbow->Items[0]; if (inch) edDenLen->Text = Format("%.2f", OPENARRAY(TVarRec, (bd->Method.Number / 160.0))); else edDenLen->Text = Format("%.2f", OPENARRAY(TVarRec, (bd->Method.Number * 2.54 / 160.0))); } if (seColors->Value>17) { sbTable->Max = seColors->Value-17; sbTable->Enabled = true; } else { sbTable->Enabled = false; } } } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::cgTableMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { ColorIndex = cgTable->GetColorIndex(X, Y); if (ColorIndex < 0) return; lbChoiceIndex->Caption = IntToStr(ColorIndex+1); if (pcColor->ActivePage == tsCMY) { CMY->Color = cgTable->CellColor[ColorIndex]; } else if (pcColor->ActivePage == tsHLS) { HLS->Color = cgTable->CellColor[ColorIndex]; } else if (pcColor->ActivePage == tsDensityLength) { shChoiceColor->Brush->Color = cgTable->CellColor[ColorIndex]; ChangeMethodData(ColorIndex); } } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::DIB256PaletteDblClick(TObject *Sender) { TBaseData *bd; cgTable->ReDrawCell(ColorIndex, DIB256Palette->Palette->ColorData[DIB256Palette->ChoiceIndex]->Color); if (Item == 0) { Normal->Copy(DIB256Palette->Palette->ColorData[DIB256Palette->ChoiceIndex]); } else if (Item == 1) { bd = (TBaseData *) Melange->Items[ColorIndex]; bd->Color->Copy(DIB256Palette->Palette->ColorData[DIB256Palette->ChoiceIndex]); } else if (Item == 2) { bd = (TBaseData *) Rainbow->Items[ColorIndex]; bd->Color->Copy(DIB256Palette->Palette->ColorData[DIB256Palette->ChoiceIndex]); } } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::sbTableScroll(TObject *Sender, TScrollCode ScrollCode, int &ScrollPos) { cgTable->Pos = sbTable->Position; } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::pcColorChange(TObject *Sender) { if (pcColor->ActivePage == tsCMY) { CMY->Color = cgTable->CellColor[ColorIndex]; } else if (pcColor->ActivePage == tsHLS) { HLS->Color = cgTable->CellColor[ColorIndex]; } else if (pcColor->ActivePage == tsDensityLength) { shChoiceColor->Brush->Color = cgTable->CellColor[ColorIndex]; ChangeMethodData(ColorIndex); } } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::CMYColorChange(TObject *Sender) { Byte r, g, b; TBaseData *bd; cgTable->ReDrawCell(ColorIndex, CMY->Color); r = GetRValue(CMY->Color); g = GetGValue(CMY->Color); b = GetBValue(CMY->Color); if (Item == 0) { Normal->SetNormal(r, g, b); } else if (Item == 1) { bd = (TBaseData *) Melange->Items[ColorIndex]; bd->Color->SetNormal(r, g, b); } else { bd = (TBaseData *) Rainbow->Items[ColorIndex]; bd->Color->SetNormal(r, g, b); } } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::HLSColorChange(TObject *Sender) { Byte r, g, b; TBaseData *bd; cgTable->ReDrawCell(ColorIndex, HLS->Color); r = GetRValue(HLS->Color); g = GetGValue(HLS->Color); b = GetBValue(HLS->Color); if (Item == 0) { Normal->SetNormal(r, g, b); } else if (Item == 1) { bd = (TBaseData *) Melange->Items[ColorIndex]; bd->Color->SetNormal(r, g, b); } else { bd = (TBaseData *) Rainbow->Items[ColorIndex]; bd->Color->SetNormal(r, g, b); } } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::seCountChange(TObject *Sender) { if (seCount->Text.Length() > 0) { if (FYUnit.Method && (FYUnit.System == YU_WORSTED || FYUnit.System == YU_Y_S_W)) lbCount->Caption = Format("1/%d", OPENARRAY(TVarRec, ((int)seCount->Value))); else lbCount->Caption = Format("%d/1", OPENARRAY(TVarRec, ((int)seCount->Value))); } } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::btnOKClick(TObject *Sender) { int i; TBaseData *bd; FYarnInfor->Detail.Type = Item; FYarnInfor->Detail.Hair = cbHairEffect->Checked; FYarnInfor->Detail.Count = seCount->Value; FYarnInfor->Detail.SZ = SZ; FYarnInfor->Detail.TPI = seTPI->Value; if (FYarnInfor->Color) { for (i = 0; i < FYarnInfor->Colors; i++) { delete FYarnInfor->Color[i]; } delete[] FYarnInfor->Color; FYarnInfor->Color = NULL; } if (FYarnInfor->Method) { delete[] FYarnInfor->Method; FYarnInfor->Method = NULL; } FYarnInfor->Colors = seColors->Value; FYarnInfor->Methods = (Item!=0)? seColors->Value : 0; if (FYarnInfor->Colors > 0) { if ((FYarnInfor->Color = new TColorData *[FYarnInfor->Colors]) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } for (i = 0; i < FYarnInfor->Colors; i++) { if ((FYarnInfor->Color[i] = new TColorData) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } if (Item == 0) { FYarnInfor->Color[i]->Copy(Normal); } else if (Item == 1) { bd = (TBaseData *) Melange->Items[i]; FYarnInfor->Color[i]->Copy(bd->Color); } else { bd = (TBaseData *) Rainbow->Items[i]; FYarnInfor->Color[i]->Copy(bd->Color); } } } if (FYarnInfor->Methods > 0 && Item > 0) { if ((FYarnInfor->Method = new TYarnMethod[FYarnInfor->Methods]) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } for (i = 0; i < FYarnInfor->Methods; i++) { FYarnInfor->Method[i].Code = i; if (Item == 1) bd = (TBaseData *) Melange->Items[i]; else bd = (TBaseData *) Rainbow->Items[i]; FYarnInfor->Method[i].Number = bd->Method.Number; } } } //--------------------------------------------------------------------------- //// Private Method //////////////////////////////////////////////////// //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::ClearData() { TBaseData *bd; while (Melange->Count) { bd = (TBaseData *) Melange->Last(); Melange->Remove(bd); delete bd->Color; delete bd; } while (Rainbow->Count) { bd = (TBaseData *) Rainbow->Last(); Rainbow->Remove(bd); delete bd->Color; delete bd; } } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::AddData(TColorData *cd) { TBaseData *bd; if ((bd = new TBaseData) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } if ((bd->Color = new TColorData) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } bd->Color->Copy(cd); bd->Method.Code = (Item == 1)? Melange->Count : Rainbow->Count; bd->Method.Number = 0; if (Item == 1) Melange->Add(bd); else Rainbow->Add(bd); } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::DeleteData() { TBaseData *bd; if (Item == 1) { bd = (TBaseData *) Melange->Last(); Melange->Remove(bd); } else { bd = (TBaseData *) Rainbow->Last(); Rainbow->Remove(bd); } delete bd->Color; delete bd; } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::InitBaseData() { TBaseData *bd; int i; if (FYarnInfor->Colors > 0) { if (FYarnInfor->Color) { if (Item == 0) { if ((Normal = new TColorData) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } Normal->Copy(FYarnInfor->Color[0]); } else if (Item == 1) { for (i = 0; i < seColors->Value; i++) { if ((bd = new TBaseData) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } if ((bd->Color = new TColorData) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } bd->Color->Copy(FYarnInfor->Color[i]); if (FYarnInfor->Method) { bd->Method.Code = FYarnInfor->Method[i].Code; bd->Method.Number = FYarnInfor->Method[i].Number; } else { bd->Method.Code = i; bd->Method.Number = 100 / seColors->Value; } Melange->Add(bd); } } else { for (i = 0; i < seColors->Value; i++) { if ((bd = new TBaseData) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } if ((bd->Color = new TColorData) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } bd->Color->Copy(FYarnInfor->Color[i]); if (FYarnInfor->Method) { bd->Method.Code = FYarnInfor->Method[i].Code; bd->Method.Number = FYarnInfor->Method[i].Number; } else { bd->Method.Code = i; bd->Method.Number = 1; } Rainbow->Add(bd); } } } } else { if (Item == 0) { if ((Normal = new TColorData) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } Normal->Copy(DIB256Palette->Palette->ColorData[PalIndex]); } else if (Item == 1) { if ((bd = new TBaseData) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } if ((bd->Color = new TColorData) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } bd->Color->Copy(DIB256Palette->Palette->ColorData[PalIndex]); bd->Method.Code = 0; bd->Method.Number = 100; Melange->Add(bd); } else { if ((bd = new TBaseData) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } if ((bd->Color = new TColorData) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } bd->Color->Copy(DIB256Palette->Palette->ColorData[PalIndex]); bd->Method.Code = 0; bd->Method.Number = 1; Rainbow->Add(bd); } PalIndex++; } } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::ChangeNormal() { seColors->Value = 1; cgTable->ColorCount = 1; cgTable->ReDrawCell(0, Normal->Color); } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::ChangeMelange() { TBaseData *bd; if (Melange->Count) { seColors->Value = Melange->Count; cgTable->ColorCount = Melange->Count; for (int i = 0; i < Melange->Count; i++) { bd = (TBaseData *) Melange->Items[i]; cgTable->ReDrawCell(i, bd->Color->Color); } } else { seColors->Value = 1; cgTable->ColorCount = 1; if ((bd = new TBaseData) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } if ((bd->Color = new TColorData) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } bd->Method.Code = 0; bd->Method.Number = 100; bd->Color->Copy(DIB256Palette->Palette->ColorData[PalIndex]); cgTable->ReDrawCell(0, bd->Color->Color); PalIndex++; Melange->Add(bd); } bd = (TBaseData *) Melange->Items[0]; edDenLen->Text = IntToStr(bd->Method.Number); } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::ChangeRainbow() { TBaseData *bd; if (Rainbow->Count) { seColors->Value = Rainbow->Count; cgTable->ColorCount = Rainbow->Count; for (int i = 0; i < Rainbow->Count; i++) { bd = (TBaseData *) Rainbow->Items[i]; cgTable->ReDrawCell(i, bd->Color->Color); } } else { seColors->Value = 1; cgTable->ColorCount = 1; if ((bd = new TBaseData) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } if ((bd->Color = new TColorData) == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } bd->Method.Code = 0; bd->Method.Number = 1; bd->Color->Copy(DIB256Palette->Palette->ColorData[PalIndex]); cgTable->ReDrawCell(0, bd->Color->Color); PalIndex++; Rainbow->Add(bd); } bd = (TBaseData *) Rainbow->Items[0]; if (inch) edDenLen->Text = Format("%.2f", OPENARRAY(TVarRec, (bd->Method.Number / 160.0))); else edDenLen->Text = Format("%.2f", OPENARRAY(TVarRec, (bd->Method.Number * 2.54 / 160.0))); } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::ChangeMethodData(int i) { TBaseData *bd; if (Item == 1) { if (Melange->Count) { bd = (TBaseData *) Melange->Items[i]; edDenLen->Text = IntToStr(bd->Method.Number); } } else { if (Rainbow->Count) { bd = (TBaseData *) Rainbow->Items[i]; if (inch) edDenLen->Text = Format("%.2f", OPENARRAY(TVarRec, (bd->Method.Number / 160.0))); else edDenLen->Text = Format("%.2f", OPENARRAY(TVarRec, (bd->Method.Number * 2.54 / 160.0))); } } } //--------------------------------------------------------------------------- bool __fastcall TBaseYarnDesignForm::ProperInput(double value) { int dot; if (Item == 1) { return (value <= 100); } else if (Item == 2) { if (inch) dot = value * 160 + 0.5; else dot = value / 2.54 * 160 + 0.5; return (dot <= 1024); } } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::CalculateMethodNumber() { TBaseData *bd; int i, count, value, cnt = 0, total = 0, tot = 0; if (Item == 1) { for (i = 0; i < Melange->Count; i++) { bd = (TBaseData *) Melange->Items[i]; if (bd->change) { total += bd->Method.Number; cnt++; } } if (total > 100 || cnt == Melange->Count) { for (i = 0; i < Melange->Count; i++) { bd = (TBaseData *) Melange->Items[i]; if (i != ColorIndex) { tot += bd->Method.Number; } } value = total - 100; for (i = 0; i < Melange->Count; i++) { bd = (TBaseData *) Melange->Items[i]; if (i != ColorIndex) { bd->Method.Number -= bd->Method.Number * value / tot; } } } else { count = Melange->Count - cnt; if (count == 0) count = 1; value = (100 - total) / count; cnt = 0; for (i = 0; i < Melange->Count; i++) { bd = (TBaseData *) Melange->Items[i]; if (!bd->change) { bd->Method.Number = value; total += value; cnt = i; } } bd = (TBaseData *) Melange->Items[cnt]; bd->Method.Number = value + (100 - total); } } else { for (i = 0; i < Rainbow->Count; i++) { bd = (TBaseData *) Rainbow->Items[i]; if (!bd->change) { bd->Method.Number = 160; } } } } //--------------------------------------------------------------------------- ///// Public Method //////////////////////////////////////////////////// //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::TakeUnit(TYarnUnit unit, bool i) { FYUnit.Method = unit.Method; FYUnit.System = unit.System; inch = i; } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::stTwistClick(TObject *Sender) { if (SZ) { stTwist->Caption = IDS_SDIRECTION; SZ = false; } else { stTwist->Caption = IDS_ZDIRECTION; SZ = true; } } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::edDenLenKeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { TBaseData *bd; double value; if (Key == VK_RETURN) { if (edDenLen->Text.Length() > 0) { value = StrToFloat(edDenLen->Text); if (ProperInput(value)) { if (Item == 1) { bd = (TBaseData *) Melange->Items[ColorIndex]; bd->Method.Number = value; bd->change = true; } else if (Item == 2) { bd = (TBaseData *) Rainbow->Items[ColorIndex]; if (inch) bd->Method.Number = value *160; else bd->Method.Number = value / 2.54 * 160; bd->change = true; } CalculateMethodNumber(); } else { if (Item == 1) { bd = (TBaseData *) Melange->Items[ColorIndex]; edDenLen->Text = IntToStr(bd->Method.Number); } else if (Item == 2) { bd = (TBaseData *) Rainbow->Items[ColorIndex]; if (inch) edDenLen->Text = Format("%.2f", OPENARRAY(TVarRec, (bd->Method.Number / 160.0))); else edDenLen->Text = Format("%.2f", OPENARRAY(TVarRec, (bd->Method.Number * 2.54 / 160.0))); } } } } } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::cbHairEffectKeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { if (Key == 13)btnOK->SetFocus(); //lhskys ¿£ÅÍ·Î ½ÇÇàÇϱâ À§ÇØ focus¸¦ ÁØ´Ù } //--------------------------------------------------------------------------- void __fastcall TBaseYarnDesignForm::seCountKeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { if (Key == 13)btnOK->SetFocus(); //lhskys ¿£ÅÍ·Î ½ÇÇàÇϱâ À§ÇØ focus¸¦ ÁØ´Ù } //---------------------------------------------------------------------------