//------------------------------------------------------------------------------ // gui_2D class -- // (C) Piero Giubilato 2008-2010, Berkeley Lab -- //------------------------------------------------------------------------------ //______________________________________________________________________________ // {Trace} // [File name] "gui_2D.cpp" // [Author] "Piero Giubilato" // [Version] "1.5" // [Modified by] "Piero Giubilato" // [Last revision] "31 Jul 2009" // [Language] "C++" // [Compiler] "Visual C++ 9.x" // [Member of] "Cool SEAL" // [Project] "SEAL" // [Description] "Source for the gui_2D class" // [Key documentation] // "Visual C++ Reference Help" // "Root reference guide" // {Trace} //______________________________________________________________________________ // Standard component #include #include // Root Component #include #include #include #include // Application component #include "cool.h" #include "global.h" #include "gui_2D.h" // CINT Preprocessor class import definition ClassImp(gui_2D) // ********************** // Object special methods // ********************** //______________________________________________________________________________ gui_2D::gui_2D(UInt_t left, UInt_t top, UInt_t width, UInt_t height, UInt_t textColor): gui_Object(0) { // Default, no data constructor dbg_Print("gui_2D::gui_2D:(default)", DBG_LVL_ZERO); // Sets up the map Init(left, top, width, height, textColor); } //______________________________________________________________________________ gui_2D::gui_2D(gui_Object* parent, UInt_t left, UInt_t top, UInt_t width, UInt_t height, UInt_t textColor): gui_Object(parent) { // Parent constructor dbg_Print("gui_2D::gui_2D:(gui_Object*)", DBG_LVL_ZERO); // Sets up the map Init(left, top, width, height, textColor); } //______________________________________________________________________________ void gui_2D::Init(UInt_t left, UInt_t top, UInt_t width, UInt_t height, UInt_t textColor) { // Common init procedure // Ks k_palette_Margin = 16; // Map-Palette margin k_palette_Width = 16; // Palette width k_label_Margin = 8; // Palette-label margin k_btn_Size = 24; // Accessory buttons k_btn_Radius = 6; // Accessory buttons radius k_btn_Depth = 1; // Accessory buttons depth k_btn_Color = cool::Gui->kc_gray_Dark ; // Accessory buttons color // Create the fit ROI button btn_Roi = new gso_Button(Parent()->Window(), "+", 18 , 18, gso_Button::km_Button, cool::Gui->kc_White, k_btn_Color, 0xFFFFFF, 4, 2); btn_Roi->Connect(btn_Roi, "Clicked()", "gui_2D", this, "btn_roi_Do(Long_t)"); Parent()->Window()->AddFrame(btn_Roi, new TGLayoutHints(kLHintsNormal, 0, 0, 0, 0)); // Creates the palette in case, map and places all the stuff palette = new gso_Palette(Parent()->Window(), k_palette_Width, height, gui_Const::bvl_Size, gui_Const::bvl_Thick, Parent()->backColor(), Parent()->backColor()); Parent()->Window()->AddFrame(palette, new TGLayoutHints(kLHintsNormal, 0, 0, 0, 0)); // Creates the 2D map map = new gso_Map2D(Parent()->Window(), palette, width, height, gui_Const::bvl_Size, gui_Const::bvl_Thick, 0x000000, Parent()->backColor(), Parent()->backColor()); Parent()->Window()->AddFrame(map, new TGLayoutHints(kLHintsNormal, 0, 0, 0, 0)); map->Connect(map, "roi_Updated()", "gui_2D", this, "map_roi_Do()"); //map->Connect(map, "opt_Clicked()", "gui_2D", this, "map_opt_Do()"); // Creates the file menu mnu_File = new TGPopupMenu(); mnu_File->AddEntry("Save current view...", 0); mnu_File->AddEntry("Save whole dataset...", 1); mnu_File->AddSeparator(); mnu_File->AddEntry("Save dataset as raw...", 2); mnu_File->Connect(mnu_File, "Activated(Int_t)", "gui_2D", this, "mnu_file_Do(Int_t)"); // Creates the option menu mnu_Roi = new TGPopupMenu(); mnu_Roi->AddEntry("Fit XY", 0); mnu_Roi->AddEntry("Fit X", 1); mnu_Roi->AddEntry("Fit Y", 2); mnu_Roi->AddSeparator(); mnu_Roi->AddEntry("1 to 1 ratio", 10); mnu_Roi->AddEntry("Pixel zoom", 11); mnu_Roi->Connect(mnu_Roi, "Activated(Int_t)", "gui_2D", this, "mnu_roi_Do(Int_t)"); // Creates the file button btn_File = new gso_Button(Parent()->Window(), "F", k_btn_Size, k_btn_Size, gso_Button::km_Button, cool::Gui->kc_White, k_btn_Color, Parent()->backColor(), (Float_t)k_btn_Radius, (Float_t)k_btn_Depth); btn_File->Connect(btn_File, "Clicked()", "gui_2D", this, "btn_file_Do(Long_t)"); Parent()->Window()->AddFrame(btn_File, new TGLayoutHints(kLHintsNormal, 0, 0, 0, 0)); // Creates four numeric for ROI definition for (UInt_t i = 0; i < 4; i++) { num_Roi.push_back(new TGNumberEntry(Parent()->Window(), 0)); num_Roi.back()->Connect(num_Roi.back(), "ValueSet(Long_t)", "gui_2D", this, "num_roi_Do(Long_t)"); Parent()->Window()->AddFrame(num_Roi.back(), new TGLayoutHints(kLHintsNormal, 0, 0, 0, 0)); } // Defines the object as transparent gui_Object::obj_Transparent = true; // Show the object Resize(width, height); Move(left, top); } //______________________________________________________________________________ gui_2D::~gui_2D() { // Default destructor. dbg_Print("gui_2D::~gui_2D()", DBG_LVL_ZERO); // Deletes the option menu delete mnu_Roi; // Deletes the map Parent()->Window()->RemoveFrame(map); map->DestroyWindow(); delete map; // Deletes the palette Parent()->Window()->RemoveFrame(palette); palette->DestroyWindow(); delete palette; // Removes the num_Roi for (UInt_t i = 0; i < num_Roi.size(); i++) { Parent()->Window()->RemoveFrame(num_Roi[i]); num_Roi[i]->DestroyWindow(); delete num_Roi[i]; } // Removes the num_Btn delete btn_Roi; } //______________________________________________________________________________ void gui_2D::roi_Limits(Double_t r_Left, Double_t r_Top, Double_t r_Width, Double_t r_Height, bool refresh) { // Sets a new data link for the map dbg_Print("gui_2D::roi_Limits:(UInt, UInt)", DBG_LVL_FLOW); // Updates the ROI limits num_Roi[0]->SetNumber(r_Left); num_Roi[1]->SetNumber(r_Left + r_Width); num_Roi[2]->SetNumber(r_Top); num_Roi[3]->SetNumber(r_Top + r_Height); // Refreshes map in case if (refresh) data_Refresh(); } //______________________________________________________________________________ void gui_2D::btn_file_Do(Long_t) { // Handles the file button dbg_Print("gui_2D::btn_file_Do:(Long_t)", DBG_LVL_FLOW); // Calculates menu position Int_t posX, posY; Window_t dummyW; gVirtualX->TranslateCoordinates(map->GetId(), gClient->GetDefaultRoot()->GetId(), 0, map->GetHeight() - map->BrdSize(), posX, posY, dummyW); // Shows the menu mnu_File->PlaceMenu(posX, posY, true, true); } //______________________________________________________________________________ void gui_2D::mnu_file_Do(Int_t Idx) { // Handles the file menu dbg_Print("gui_2D::mnu_file_Do:()", DBG_LVL_FLOW); // Selects what to do switch (Idx) { case 0: data_Save(""); break; // Save current viwe case 1: data_Save("", true); break; // Save whole data set case 2: data_Save("", true, true); break; // Save whole data as raw numbers } } //______________________________________________________________________________ void gui_2D::num_roi_Do(Long_t) { // Handles the ROI numeric events dbg_Print("gui_2D::num_roi_Do:(Long_t)", DBG_LVL_FLOW); // Refresh the map data_Refresh(); } //______________________________________________________________________________ void gui_2D::btn_roi_Do(Long_t) { // Handles the ROI numeric events dbg_Print("gui_2D::btn_roi_Do:(Long_t)", DBG_LVL_FLOW); // Calculates menu position Int_t posX, posY; Window_t dummyW; gVirtualX->TranslateCoordinates(map->GetId(), gClient->GetDefaultRoot()->GetId(), 0, map->GetHeight() - map->BrdSize(), posX, posY, dummyW); // Shows the menu mnu_Roi->PlaceMenu(posX, posY, true, true); } //______________________________________________________________________________ void gui_2D::map_roi_Do() { // Handles the ROI changes from the map dbg_Print("gui_2D::map_roi_Do:()", DBG_LVL_FLOW); // Updates the ROI numeric, with no refresh in that case!! roi_Limits(map->data_roi_Left(), map->data_roi_Top(), map->data_roi_Width(), map->data_roi_Height(), false); } //______________________________________________________________________________ void gui_2D::mnu_roi_Do(Int_t Idx) { // Handles the options menu dbg_Print("gui_2D::mnu_roi_Do:()", DBG_LVL_FLOW); // Selects what to do switch (Idx) { case 0: map->data_Refresh(true, true, false, false); break; // XY Fit case 1: map->data_Refresh(true, false, false, false); break; // X Fit case 2: map->data_Refresh(false, true, false, false); break; // Y Fit case 10: map->data_Refresh(false, false, true, false); break; // 1:1 aspect ratio case 11: map->data_Refresh(false, false, false, true); break; // Pixel 1:1 zoom } } //______________________________________________________________________________ void gui_2D::map_style_Do() { // Handles the style changes from the map dbg_Print("gui_2D::map_style_Do:()", DBG_LVL_FLOW); // Redraw the object accordingly std::cout << "** Map style event!\n"; //Resize(obj_Width, obj_Height); } // *********************** // gui_2d specific methods // *********************** //______________________________________________________________________________ void gui_2D::palette_Color(const UInt_t* col_List, UInt_t col_Count) { // Updates the palette dbg_Print("gui_2D::range_Color:(default)", DBG_LVL_FLOW); palette->color_Set(col_List, col_Count); // Refreshes the map map->Refresh(); } //______________________________________________________________________________ void gui_2D::data_Refresh() { // Rereads the map data dbg_Print("gui_2D::data_Refresh:()", DBG_LVL_FLOW); // Tells the map to update itself Double_t rL = num_Roi[0]->GetNumber(); Double_t rT = num_Roi[2]->GetNumber(); Double_t rW = num_Roi[1]->GetNumber() - rL; Double_t rH = num_Roi[3]->GetNumber() - rT; // Updates the map (ROI check are performed inside) map->data_Refresh(rL, rT, rW, rH); // Updates the control again to account for map ROI modifications roi_Limits(map->data_roi_Left(), map->data_roi_Top(), map->data_roi_Width(), map->data_roi_Height(), false); } //______________________________________________________________________________ void gui_2D::data_Refresh(Int_t r_Left, Int_t r_Top, Int_t r_Width, Int_t r_Height) { // Rereads the map data dbg_Print("gui_2D::data_Refresh:()", DBG_LVL_FLOW); // Refreshes the ROI numeric and redraw the map accordingly roi_Limits(r_Left, r_Top, r_Width, r_Height, true); } //______________________________________________________________________________ void gui_2D::data_refresh_Auto(UInt_t interval) { // Starts the map autoredraw dbg_Print("gui_2D::data_refresh_Auto:(UInt)", DBG_LVL_FLOW); // Refreshes the ROI numeric and redraw the map accordingly map->data_refresh_Auto(interval); } //______________________________________________________________________________ void gui_2D::data_Save(char* file, bool whole, bool raw) const { // Saves current image to a file dbg_Print("gui_2D::data_Save(const char*)", DBG_LVL_FLOW); // Check for file window if (file == "") { file = new char[16]; file = "MapImage"; } // Set file types TGFileInfo fi; const char *ft[] = {"TIFF", "*.tif", "GIF", "*.gif", "PNG", "*.png", "BMP", "*.bmp", "JPEG", "*.jpg", "XPM", "*.xpm", "XML", "*.xml", 0, 0}; fi.fFileTypes = ft; // Set start dir (file_Dir.Length() == 0) ? fi.fIniDir = "c:/" : fi.fIniDir = StrDup(file_Dir.Data()); // Show file dialog new TGFileDialog(gClient->GetRoot(), Parent()->Window(), kFDSave, &fi); // If Save was selected TImage::EImageFileTypes fType; if (fi.fFilename) { // Add the file type identifier TString fileName(fi.fFilename); switch (fi.fFileTypeIdx) { case 0: fileName.Append(".tiff"); fType = TImage::kTiff; break; case 2: fileName.Append(".gif"); fType = TImage::kGif; break; case 4: fileName.Append(".png"); fType = TImage::kPng; break; case 6: fileName.Append(".bmp"); fType = TImage::kBmp; break; case 8: fileName.Append(".jpg"); fType = TImage::kJpeg; break; case 10: fileName.Append(".xpm"); fType = TImage::kXpm; break; case 12: fileName.Append(".xml"); fType = TImage::kTga; break; default: fileName.Append(".gif"); fType = TImage::kGif; } // Saves dir and file name file_Dir.Format(fi.fIniDir); file_Name.Format(fi.fFilename); //std::cout << fi.fFileTypeIdx << "\n"; //std::cout << fi.fFilename << "\n"; //std::cout << fi.fIniDir << "\n"; //std::cout << fileName.Data() << "\n"; // Writes the file (images) TImage* img = TImage::Create(); img->SetImage(map->data_PxMap(whole)); img->WriteImage(fileName.Data(), fType); delete img; // Info cool::Out->Print("Image saved to: "); cool::Out->Print((char*)fileName.Data(), LWHITE, true); } } // ********************** // Object general methods // ********************** //______________________________________________________________________________ void gui_2D::ReDraw() { // Redraw the object to keep it in pair with the general GUI parameters dbg_Print("gui_CoolButton::ReDraw:()", DBG_LVL_FLOW); // What we need here is to change the desk background color, this will in // turn trigger the re-rendering of the whole map color_Set(obj_foreColor, Parent()->backColor()); } //______________________________________________________________________________ void gui_2D::color_Set(UInt_t foreColor, UInt_t backColor) { // Overloads the gui object color function dbg_Print("gui_2D::color_Set:(default)", DBG_LVL_FLOW); // Set map color map->SetColor(backColor, Parent()->shadedColor()); // Set button color btn_File->SetColor(k_btn_Color, backColor); // Set palette color palette->SetColor(backColor, backColor); // Provides connections for the standard gui_Object color calls obj_foreColor = foreColor; obj_backColor = backColor; } //______________________________________________________________________________ void gui_2D::Move(UInt_t left, UInt_t top) { // Moves the map frame dbg_Print("gui_2D::Move:(default)", DBG_LVL_FLOW); map->Move(left, top); // Moves the palette frame palette->Move(left + obj_Width + k_palette_Margin, top); // Moves the file button btn_File->Move(left - btn_File->GetWidth() - k_palette_Margin / 2, top + obj_Height + k_palette_Margin / 2); // Moves the num_Roi num_Roi[0]->Move(left, top + obj_Height + k_palette_Margin / 2); num_Roi[1]->Move(left + obj_Width - num_Roi[0]->GetWidth(), top + obj_Height + k_palette_Margin / 2); num_Roi[2]->Move(left - num_Roi[0]->GetWidth() - k_palette_Margin / 2, top + obj_Height - num_Roi[0]->GetHeight()); num_Roi[3]->Move(left - num_Roi[0]->GetWidth() - k_palette_Margin / 2, top); // ROI Button btn_Roi->Move(left, top + obj_Height - btn_Roi->GetHeight()); // Stores the new datas obj_Left = left; obj_Top = top; // Refresh the container Update(); } //______________________________________________________________________________ void gui_2D::Resize(UInt_t width, UInt_t height) { // Resizes the map frame dbg_Print("gui_2D::Resize:(default)", DBG_LVL_FLOW); map->Resize(width, height); // Resize the palette frame switch (map->data_Style()) { case gso_Map2D::ks_Map: palette->Resize(k_palette_Width, height); break; case gso_Map2D::ks_Plot: palette->Resize(k_palette_Width, k_palette_Width * (map->data_size_Y())); break; //case gso_Map2D::ks_Hist: palette->Resize(k_palette_Width, k_palette_Width); break; } // Stores the new datas obj_Width = map->ClrWidth(); obj_Height = map->ClrHeight(); // Updates the position of all the other objects // This call also update the object! Move(obj_Left, obj_Top); } //______________________________________________________________________________ void gui_2D::MoveResize(UInt_t left, UInt_t top, UInt_t width, UInt_t height) { // Pre-update the object position obj_Left= left; obj_Top = top; // Resizes the map frame (this call will also move // the object to the new obj_Left, obj_Top) Resize(width, height); } //______________________________________________________________________________ void gui_2D::LowerWindow() const { // Puts all the windows which compose the object to // the back of the Z-order stack dbg_Print("gui_2D::LowerWindow:()", DBG_LVL_FLOW); // Map map->LowerWindow(); // Palette palette->LowerWindow(); // File button btn_File->LowerWindow(); // ROI button btn_Roi->LowerWindow(); // ROI indicators for (UInt_t i = 0; i < num_Roi.size(); i++) num_Roi[i]->LowerWindow(); } //______________________________________________________________________________ void gui_2D::RaiseWindow() const { // Puts all the windows which compose the object on // the top of the Z-order stack dbg_Print("gui_2D::RaiseWindow:()", DBG_LVL_FLOW); // Map map->RaiseWindow(); // Palette palette->RaiseWindow(); // File button btn_File->RaiseWindow(); // ROI button btn_Roi->RaiseWindow(); // ROI indicators and buttons for (UInt_t i = 0; i < num_Roi.size(); i++) num_Roi[i]->RaiseWindow(); }