1 ///////////////////////////////////////////////////////////////////////
2 // File:        paramsd.h
3 // Description: Tesseract parameter editor
4 // Author:      Joern Wanke
5 //
6 // (C) Copyright 2007, Google Inc.
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 ///////////////////////////////////////////////////////////////////////
18 //
19 // Tesseract parameter editor is used to edit all the parameters used
20 // within tesseract from the ui.
21 #ifndef TESSERACT_CCMAIN_PARAMSD_H_
22 #define TESSERACT_CCMAIN_PARAMSD_H_
23 
24 #ifndef GRAPHICS_DISABLED
25 
26 #  include "elst.h"       // for ELIST_ITERATOR, ELISTIZEH, ELIST_LINK
27 #  include "scrollview.h" // for ScrollView (ptr only), SVEvent (ptr only)
28 
29 namespace tesseract {
30 
31 class SVMenuNode;
32 
33 class BoolParam;
34 class DoubleParam;
35 class IntParam;
36 class StringParam;
37 class Tesseract;
38 
39 // A list of all possible parameter types used.
40 enum ParamType { VT_INTEGER, VT_BOOLEAN, VT_STRING, VT_DOUBLE };
41 
42 // A rather hackish helper structure which can take any kind of parameter input
43 // (defined by ParamType) and do a couple of common operations on them, like
44 // comparisond or getting its value. It is used in the context of the
45 // ParamsEditor as a bridge from the internal tesseract parameters to the
46 // ones displayed by the ScrollView server.
47 class ParamContent : public ELIST_LINK {
48 public:
49   // Compare two VC objects by their name.
50   static int Compare(const void *v1, const void *v2);
51 
52   // Gets a VC object identified by its ID.
53   static ParamContent *GetParamContentById(int id);
54 
55   // Constructors for the various ParamTypes.
56   ParamContent() = default;
57   explicit ParamContent(tesseract::StringParam *it);
58   explicit ParamContent(tesseract::IntParam *it);
59   explicit ParamContent(tesseract::BoolParam *it);
60   explicit ParamContent(tesseract::DoubleParam *it);
61 
62   // Getters and Setters.
63   void SetValue(const char *val);
64   std::string GetValue() const;
65   const char *GetName() const;
66   const char *GetDescription() const;
67 
GetId()68   int GetId() const {
69     return my_id_;
70   }
HasChanged()71   bool HasChanged() const {
72     return changed_;
73   }
74 
75 private:
76   // The unique ID of this VC object.
77   int my_id_;
78   // Whether the parameter was changed_ and thus needs to be rewritten.
79   bool changed_ = false;
80   // The actual ParamType of this VC object.
81   ParamType param_type_;
82 
83   union {
84     tesseract::StringParam *sIt;
85     tesseract::IntParam *iIt;
86     tesseract::BoolParam *bIt;
87     tesseract::DoubleParam *dIt;
88   };
89 };
90 
ELISTIZEH(ParamContent)91 ELISTIZEH(ParamContent)
92 
93 // The parameters editor enables the user to edit all the parameters used within
94 // tesseract. It can be invoked on its own, but is supposed to be invoked by
95 // the program editor.
96 class ParamsEditor : public SVEventHandler {
97 public:
98   // Integrate the parameters editor as popupmenu into the existing scrollview
99   // window (usually the pg editor). If sv == null, create a new empty
100   // empty window and attach the parameter editor to that window (ugly).
101   explicit ParamsEditor(tesseract::Tesseract *, ScrollView *sv = nullptr);
102 
103   // Event listener. Waits for SVET_POPUP events and processes them.
104   void Notify(const SVEvent *sve) override;
105 
106 private:
107   // Gets the up to the first 3 prefixes from s (split by _).
108   // For example, tesseract_foo_bar will be split into tesseract,foo and bar.
109   void GetPrefixes(const char *s, std::string *level_one, std::string *level_two, std::string *level_three);
110 
111   // Gets the first n words (split by _) and puts them in t.
112   // For example, tesseract_foo_bar with N=2 will yield tesseract_foo_.
113   void GetFirstWords(const char *s, // source string
114                      int n,         // number of words
115                      char *t);      // target string
116 
117   // Find all editable parameters used within tesseract and create a
118   // SVMenuNode tree from it.
119   SVMenuNode *BuildListOfAllLeaves(tesseract::Tesseract *tess);
120 
121   // Write all (changed_) parameters to a config file.
122   void WriteParams(char *filename, bool changes_only);
123 
124   ScrollView *sv_window_;
125 };
126 
127 } // namespace tesseract
128 
129 #endif // !GRAPHICS_DISABLED
130 #endif // TESSERACT_CCMAIN_PARAMSD_H_
131