/* * LogableMainFrame.cpp * * Created on: Mar 16, 2016 * Author: Hovanes Egiyan */ #include "LogableMainFrame.hh" #include "elog.h" #include "logItem.h" #include "logentry.h" #include "logException.h" #include "config.h" using namespace std; ClassImp(LogableMainFrame) unsigned LogableMainFrame::lmfLogEntryBodySize = 8192; string LogableMainFrame::lmfLogbookName = "HDLOG,ELOG"; LogableMainFrame::LogableMainFrame(const TGWindow *p, UInt_t width, UInt_t height) : TGMainFrame( p, 1500, 500, kVerticalFrame ){ SetBackgroundColor(0x000000); } LogableMainFrame::~LogableMainFrame() { this->Cleanup(); return; } void LogableMainFrame::MakeMenuBar() { TGHorizontalFrame* menuFrame = new TGHorizontalFrame(this, fWidth, 20 ); // a popup menu TGPopupMenu* fMenuFile = new TGPopupMenu(gClient->GetRoot()); // adding menu entries fMenuFile->AddEntry("&Save as..." , kM_FILE_SAVEAS ); fMenuFile->AddEntry("&Log entry" , kM_FILE_LOG); fMenuFile->AddEntry("&Close " , kM_FILE_CLOSE); fMenuFile->AddEntry("E&xit" , kM_FILE_EXIT); fMenuFile->Connect("Activated(Int_t)","OverviewMainFrame",this,"HandleMenu(Int_t)"); // Create the menu bar itself TGLayoutHints* fMenuBarItemLayout = new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 4, 0, 0); // menu bar lmfMenuBar = new TGMenuBar(menuFrame,100,20,kHorizontalFrame); // adding popup menus lmfMenuBar->AddPopup("&File",fMenuFile,fMenuBarItemLayout); menuFrame->AddFrame(lmfMenuBar, new TGLayoutHints(kLHintsExpandX | kLHintsTop, 5, 5, 5, 5)); this->AddFrame(menuFrame, new TGLayoutHints(kLHintsExpandX | kLHintsTop, 5, 5, 5, 5)); return; } void LogableMainFrame::HandleMenu(Int_t choice) { cout << "Choice " << choice << " has been made " << endl; switch (choice) { case kM_FILE_SAVEAS: this->SaveFrameAsCodeOrImage(); break; case kM_FILE_LOG: cout << "There it is" << endl; cout << "Will make a log entry" << endl; MakeLogEntry(); break; case kM_FILE_CLOSE: this->CloseWindow(); break; case kM_FILE_EXIT: this->ExitApplication(); break; default: break; } return; } void LogableMainFrame::ExitApplication() { CloseWindow(); cout << "Closing the main application window and exiting the program" << endl; gApplication->Terminate(0); return; } void LogableMainFrame::MakeLogEntry() { try { cout << "The name of LogableMainFrame is " << this->GetWindowName() << endl; string imgFileName = this->SaveAsImage( GetTempImageFileName() ); string logTitle = string( "Scan result for " ) + string( this->GetWindowName() ); // std::replace( imgFileName.begin(), imgFileName.end(), ':', '_' ); // std::replace( logTitle.begin(), logTitle.end(), ':', '_' ); // ELOG::Logentry logEntry( logTitle, "TLOG" ); ELOG::Logentry logEntry( logTitle, lmfLogbookName.c_str() ); logEntry.setBody( this->GetLogEntryBody().c_str() ); logEntry.setTags("HarpScan,Beamline"); logEntry.addAttachment( imgFileName ); // unsigned elogID = 0; unsigned elogID = logEntry.submit(); if ( elogID == 0 ) { cout << "ELOG entry has been scheduled for indirect submission" << endl; } else if ( elogID > 0 ) { cout << "ELOG entry has been submitted successfully" << endl; } else { cout << "ELOG submission failed" << endl; } } catch ( runtime_error& e ) { cout << e.what() << endl; } return; } string LogableMainFrame::SaveAsImage( const string fileName ) { string actualFileName; if ( fileName == "" ) { actualFileName = GetTempImageFileName(); } else { actualFileName = fileName; } TString fname = gSystem->UnixPathName( actualFileName.c_str() ); TImage::EImageFileTypes gtype = TImage::kUnknown; if ( fname.EndsWith( "gif" ) ) { gtype = TImage::kGif; } else if ( fname.EndsWith( ".png" ) ) { gtype = TImage::kPng; } else if ( fname.EndsWith( ".jpg" ) ) { gtype = TImage::kJpeg; } else if ( fname.EndsWith( ".tiff" ) ) { gtype = TImage::kTiff; } else if ( fname.EndsWith( ".xpm" ) ) { gtype = TImage::kXpm; } if ( gtype != TImage::kUnknown ) { Int_t saver = gErrorIgnoreLevel; gErrorIgnoreLevel = kFatal; TImage *img = TImage::Create(); RaiseWindow(); img->FromWindow( GetId() ); img->WriteImage( fname, gtype ); gErrorIgnoreLevel = saver; delete img; } else { Int_t retval; new TGMsgBox( fClient->GetDefaultRoot(), this, "Error...", TString::Format( "file (%s) cannot be saved with this extension", fname.Data() ), kMBIconExclamation, kMBCancel, &retval ); } return actualFileName; } string LogableMainFrame::GetTempImageFileName() { string frameName = this->GetName(); size_t lastSlash = frameName.find_last_of( "/" ); if( lastSlash != string::npos ) { frameName = frameName.substr(lastSlash+1, frameName.size() ); } string fileName = string("/tmp/") + frameName + ".png" ; return fileName; } string LogableMainFrame::GetLogEntryBody() { TGTextBuffer textBuffer( lmfLogEntryBodySize ); LogEntryFrame* logWindow = new LogEntryFrame( 900, 600, textBuffer ); gClient->WaitFor(logWindow); string logEntryBody = textBuffer.GetString(); size_t lastNonSpace = logEntryBody.find_last_not_of( " " ); // If there are trailing spaces trip the string to remove extra spaces from the end. if( lastNonSpace != string::npos ) { return logEntryBody.substr(0,lastNonSpace+1); } // Return the whole string return logEntryBody; }