Diana Software
QGCutsEditorWindow.cc
Go to the documentation of this file.
1 #include "QGCutsEditorWindow.hh"
2 #include <list>
3 #include <sstream>
4 #include <string>
5 #include <vector>
6 #include "QGCutsFrame.hh"
7 #include "QGDefaultsHandler.hh"
8 #include "QGGraphicalCut.hh"
9 #include "QGSessionHandler.hh"
10 #include "QGTextFileHandler.hh"
11 #include "TGButton.h"
12 #include "TGFileDialog.h"
13 #include "TGPicture.h"
14 #include "TGTextEdit.h"
15 #include "TString.h"
16 
17 // Images
18 #include "ImagePaste.xpm"
19 #include "ImageSave.xpm"
20 
22 
23 using std::flush;
24 using std::list;
25 using std::string;
26 using std::stringstream;
27 using std::vector;
28 
29 QGCutsEditorWindow::QGCutsEditorWindow(const TGWindow *p, UInt_t w, UInt_t h) :
30 QGWindow(p, w, h)
31 {
32  SetWindowName("Cuts Editor");
33 
34  fTextEditor = new TGTextEdit(this);
35  AddFrame(fTextEditor, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 5, 5, 5, 0));
36 
37  fButtonsFrame = new TGHorizontalFrame(this);
38  AddFrame(fButtonsFrame, new TGLayoutHints(kLHintsCenterX, 5, 5, 5, 5));
39 
40  fPasteButton = new TGPictureButton(fButtonsFrame, gClient->GetPicturePool()->GetPicture("Paste", (char**)ImagePaste));
41  fPasteButton->Connect("Clicked()", "QGCutsEditorWindow", this, "HandlePasteButton()");
42  fPasteButton->SetToolTipText("Paste cuts");
43  fButtonsFrame->AddFrame(fPasteButton, new TGLayoutHints(kLHintsCenterY, 0, 0, 0, 0));
44 
45  fSaveButton = new TGPictureButton(fButtonsFrame, gClient->GetPicturePool()->GetPicture("Save", (char**)ImageSave));
46  fSaveButton->Connect("Clicked()", "QGCutsEditorWindow", this, "HandleSaveButton()");
47  fSaveButton->SetToolTipText("Save these cuts as well as any graphical cuts to file");
48  fButtonsFrame->AddFrame(fSaveButton, new TGLayoutHints(kLHintsCenterY, 0, 0, 0, 0));
49 
50  MapSubwindows();
51  Resize(GetDefaultSize());
52  Resize(w, h);
53  MapWindow();
54 }
55 
57 {
58 }
59 
61 {
62  const vector<string>& cuts = QGCutsFrame::GetCopiedCuts();
63  if (cuts.size() != 0) {
64  if (fTextEditor->ReturnLineCount() > 1) {
65  fTextEditor->AddLineFast("");
66  fTextEditor->AddLineFast(" || ");
67  fTextEditor->AddLineFast("");
68  }
69  fTextEditor->AddLineFast("(");
70  vector<string>::const_iterator cutIter;
71  for (cutIter = cuts.begin(); cutIter != cuts.end(); ++cutIter) {
72  string cut = " (" + *cutIter + ")";
73  fTextEditor->AddLineFast(cut.c_str());
74  if (cutIter + 1 != cuts.end()) {
75  fTextEditor->AddLineFast(" && ");
76  }
77  }
78  fTextEditor->AddLineFast(")");
79  fTextEditor->Update();
80  }
81 }
82 
84 {
85  // open file selection dialog box
86  string cutsDirectory = QGDefaultsHandler::Instance()->GetCutsDirectory();
87  TString dir(cutsDirectory.c_str());
88  TGFileInfo fi;
89  fi.fIniDir = StrDup(dir);
90  const char *filetypes[] = {"GUI session files" , "*.gui",
91  0, 0};
92  fi.fFileTypes = filetypes;
93  new TGFileDialog(gClient->GetRoot(), 0, kFDSave, &fi);
94  if (fi.fFilename) {
95  TString filename(fi.fFilename);
96  TString extension(fi.fFileTypes[fi.fFileTypeIdx+1]);
97  extension.Remove(TString::kLeading, '*');
98  if (!filename.EndsWith(extension)) {
99  filename.Append(extension);
100  }
101  Save(filename.Data());
102  string currentDirectory = fi.fIniDir;
103  if (currentDirectory != cutsDirectory) {
104  QGDefaultsHandler::Instance()->SetCutsDirectory(currentDirectory);
105  }
106  }
107 }
108 
109 void QGCutsEditorWindow::Save(const string& filename)
110 {
111  // open file handler for output
112  QGTextFileHandler *textFileHandler = new QGTextFileHandler();
113 
114  // save cuts string
115  textFileHandler->AppendLine(CUTS_STRING_KEY);
116  textFileHandler->AppendLine(fTextEditor->GetText()->AsString().Data());
117  textFileHandler->AppendLine();
118 
119  // save graphical cuts
120  const list<QGPlot*>& plots = QGPlot::GetPlots();
121  list<QGPlot*>::const_iterator plotIter;
122  for (plotIter = plots.begin(); plotIter != plots.end(); ++plotIter) {
123  if (QGGraphicalCut *graphicalCut = dynamic_cast<QGGraphicalCut*>(*plotIter)) {
124  string name = graphicalCut->GetName();
125  string xVariable = graphicalCut->GetXVariable();
126  string yVariable = graphicalCut->GetYVariable();
127  textFileHandler->AppendLine(GRAPHICAL_CUT_KEY);
128  textFileHandler->AppendLine(NAME_KEY, name);
129  textFileHandler->AppendLine(X_VARIABLE_KEY, xVariable);
130  textFileHandler->AppendLine(Y_VARIABLE_KEY, yVariable);
131  for (Int_t vertex = 0; vertex < graphicalCut->GetN() - 1; ++vertex) {
132  stringstream vertexStream;
133  vertexStream.precision(16);
134  vertexStream << graphicalCut->GetX()[vertex] << " , " << graphicalCut->GetY()[vertex] << flush;
135  textFileHandler->AppendLine(VERTEX_KEY, vertexStream.str());
136  }
137  textFileHandler->AppendLine();
138  }
139  }
140 
141  // write to file
142  textFileHandler->WriteFile(filename.c_str());
143  delete textFileHandler;
144 }
#define VERTEX_KEY
#define Y_VARIABLE_KEY
#define GRAPHICAL_CUT_KEY
#define NAME_KEY
#define X_VARIABLE_KEY
#define CUTS_STRING_KEY
ClassImp(QObject)
Window used to manage writing cuts to a file.
TGPictureButton * fPasteButton
Paste button.
QGCutsEditorWindow(const TGWindow *p=0, UInt_t w=600, UInt_t h=600)
Constructor.
TGTextEdit * fTextEditor
Text editor.
TGPictureButton * fSaveButton
Save button.
void HandleSaveButton()
Handle save button.
void Save(const std::string &filename)
Save cuts to file.
virtual ~QGCutsEditorWindow()
Destructor.
void HandlePasteButton()
Handle paste button.
TGHorizontalFrame * fButtonsFrame
Buttons frame.
static const std::vector< std::string > & GetCopiedCuts()
Get copied cuts.
Definition: QGCutsFrame.hh:39
void SetCutsDirectory(const std::string &directory)
Set cuts directory.
const std::string & GetCutsDirectory()
Get cuts directory.
static QGDefaultsHandler * Instance()
Singleton.
Class for graphical cuts.
static const std::list< QGPlot * > & GetPlots()
Get collection of plots.
Definition: QGPlot.hh:84
Class to handle input and output for text files.
bool WriteFile(std::string filename)
Write file, returns true if successful.
void AppendLine(std::string key="", std::string value="")
Append a key and value to the file.
Base class for GUI windows.
Definition: QGWindow.hh:15
virtual void SetWindowName(const char *name=0)
Set name of window.
Definition: QGWindow.cc:52
std::string Resize(const std::string &s, size_t len)
resize a string to len, adding spaces if necessary