Read@CVL
Elements.h
Go to the documentation of this file.
1 /*******************************************************************************************************
2  ReadFramework is the basis for modules developed at CVL/TU Wien for the EU project READ.
3 
4  Copyright (C) 2016 Markus Diem <diem@caa.tuwien.ac.at>
5  Copyright (C) 2016 Stefan Fiel <fiel@caa.tuwien.ac.at>
6  Copyright (C) 2016 Florian Kleber <kleber@caa.tuwien.ac.at>
7 
8  This file is part of ReadFramework.
9 
10  ReadFramework is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  ReadFramework is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23  The READ project has received funding from the European Union’s Horizon 2020
24  research and innovation programme under grant agreement No 674943
25 
26  related links:
27  [1] http://www.caa.tuwien.ac.at/cvl/
28  [2] https://transkribus.eu/Transkribus/
29  [3] https://github.com/TUWien/
30  [4] http://nomacs.org
31  *******************************************************************************************************/
32 
33 #pragma once
34 
35 #include "Shapes.h"
36 
37 #pragma warning(push, 0) // no warnings from includes
38 #include <QVector>
39 #include <QSharedPointer>
40 #include <QPolygon>
41 #include <QDateTime>
42 #pragma warning(pop)
43 
44 #pragma warning(disable: 4251) // dll interface warning
45 
46 #ifndef DllCoreExport
47 #ifdef DLL_CORE_EXPORT
48 #define DllCoreExport Q_DECL_EXPORT
49 #else
50 #define DllCoreExport Q_DECL_IMPORT
51 #endif
52 #endif
53 
54 // Qt defines
55 class QXmlStreamReader;
56 class QXmlStreamWriter;
57 class QPainter;
58 
59 namespace rdf {
60 
61 // read defines
62 class RegionTypeConfig;
63 class PixelSet;
64 
66 
67 public:
68  enum Type {
69  type_unknown = 0,
81 
82  type_end
83  };
84 
85  Region(const Type& type = Type::type_unknown, const QString& id = "");
86 
87  friend DllCoreExport QDataStream& operator<<(QDataStream& s, const Region& r);
88  friend DllCoreExport QDebug operator<< (QDebug d, const Region &r);
89 
90  bool isEmpty() const;
91 
92  void setSelected(bool select);
93  bool selected() const;
94 
95  void setType(const Region::Type& type);
96  Region::Type type() const;
97 
98  void setId(const QString& id);
99  QString id() const;
100 
101  void setCustom(const QString& c);
102  QString custom() const;
103 
104  void setPolygon(const Polygon& polygon);
105  Polygon polygon() const;
106 
107  void scaleRegion(double scale);
108 
109  void addChild(QSharedPointer<Region> child);
110  bool reassignChild(QSharedPointer<Region> child);
111  void addUniqueChild(QSharedPointer<Region> child, bool update = false);
112  void removeChild(QSharedPointer<Region> child);
113  void removeAllChildren();
114  void setChildren(const QVector<QSharedPointer<Region> >& children);
115  QVector<QSharedPointer<Region> > children() const;
116 
117  static QVector<QSharedPointer<Region> > selectedRegions(const Region* root);
118  static QVector<QSharedPointer<Region> > allRegions(const Region* root);
119  static QVector<QSharedPointer<Region> > filter(const Region* root, const Region::Type& type);
120 
121  virtual void draw(QPainter& p, const RegionTypeConfig& config) const;
122 
123  virtual QString toString(bool withChildren = false) const;
124  virtual QString childrenToString() const;
125 
126  virtual bool read(QXmlStreamReader& reader);
127  virtual void readAttributes(QXmlStreamReader& reader);
128  virtual void write(QXmlStreamWriter& writer) const;
129  virtual void writeChildren(QXmlStreamWriter& writer) const;
130  void createElement(QXmlStreamWriter& writer) const;
131  void writePolygon(QXmlStreamWriter& writer) const;
132 
133  virtual bool operator==(const Region& r1);
134 
135 protected:
136  Type mType = Type::type_unknown;
137  bool mSelected = false;
138  QString mId;
139  QString mCustom;
141  QVector<QSharedPointer<Region> > mChildren;
142 
143  void collectRegions(QVector<QSharedPointer<Region> >& allRegions, const Region::Type& type = type_unknown) const;
144  virtual bool readPoints(QXmlStreamReader& reader);
145 };
146 
148 
149 public:
150  RootRegion(const Type& type = Type::type_unknown);
151 
152  QVector<QSharedPointer<Region> > selectedRegions() const;
153  QVector<QSharedPointer<Region> > allRegions() const;
154  QVector<QSharedPointer<Region> > filter(const Region::Type& type) const;
155 
156 };
157 
159 
160 public:
161  TableRegion(const Type& type = Type::type_unknown);
162 
163  //virtual bool read(QXmlStreamReader& reader);
164  virtual void readAttributes(QXmlStreamReader& reader) override;
165  virtual bool operator==(const Region& sr1);
166 
167  rdf::Line topBorder() const;
168  rdf::Line bottomBorder() const;
169  rdf::Line leftBorder() const;
170  rdf::Line rightBorder() const;
171 
172  QPointF leftUpper() const;
173  QPointF rightDown() const;
174 
175  QPointF leftUpperCorner() const;
176  QPointF rightDownCorner() const;
177 
178  void setRows(int r);
179  int rows() const;
180 
181  void setCols(int c);
182  int cols() const;
183 
184 protected:
185  int mRows = -1;
186  int mCols = -1;
187 
188  //QColor mLineColor;
189  //QColor mBgColor;
190 };
191 
193 
194 public:
195  TableCell(const Type& type = Type::type_unknown);
196 
197  rdf::Line topBorder() const;
198  rdf::Line bottomBorder() const;
199  rdf::Line leftBorder() const;
200  rdf::Line rightBorder() const;
201 
202  rdf::Vector2D upperLeft() const;
203  rdf::Vector2D upperRight() const;
204  rdf::Vector2D downLeft() const;
205  rdf::Vector2D downRight() const;
206 
207  double width() const;
208  double height() const;
209 
210  virtual void readAttributes(QXmlStreamReader& reader) override;
211  virtual bool read(QXmlStreamReader& reader) override;
212  virtual void write(QXmlStreamWriter& writer) const override;
213 
214  void setRow(int r);
215  int row() const;
216  void setCol(int c);
217  int col() const;
218 
219  void setRowSpan(int r);
220  int rowSpan() const;
221  void setColSpan(int c);
222  int colSpan() const;
223 
224  void setLeftBorderVisible(bool b);
225  bool leftBorderVisible() const;
226 
227  void setRightBorderVisible(bool b);
228  bool rightBorderVisible() const;
229 
230  void setTopBorderVisible(bool b);
231  bool topBorderVisible() const;
232 
233  void setBottomBorderVisible(bool b);
234  bool bottomBorderVisible() const;
235 
236  void setHeader(bool b);
237  bool header() const;
238 
239  void setCornerPts(QVector<int> &cPts);
240  QVector<int> cornerPty() const;
241 
242  //sorts Cells according row and cell
243  bool operator< (const TableCell& cell) const;
244  static bool compareCells(const QSharedPointer<rdf::TableCell> l1, const QSharedPointer<rdf::TableCell> l2);
245 
246 protected:
247  int mRow = -1;
248  int mCol = -1;
249 
250  int mRowSpan = -1;
251  int mColSpan = -1;
252 
253  bool mLeftBorderVisible = false;
254  bool mRightBorderVisible = false;
255  bool mTopBorderVisible = false;
256  bool mBottomBorderVisible = false;
257 
258  bool mHeader = false;
259 
260  //QString mComments;
261  //Polygon mPoly;
262  //Polygon mCornerPts;
263 
264  //orientation of cornerpts stored by Transkribus:
265  // topleft: 0, bottomleft: 1, bottomright: 2, topright: 3
266  QVector<int> mCornerPts;
267 };
268 
270 
271 public:
272  TextEquiv();
273  TextEquiv(const QString& text);
274 
275  static TextEquiv read(QXmlStreamReader& reader);
276 
277  void write(QXmlStreamWriter& writer) const;
278 
279  QString text() const;
280  bool isNull() const;
281 
282 protected:
283  QString mText; // unicode
284  bool mIsNull = false;
285 };
286 
287 class DllCoreExport TextLine : public Region {
288 
289 public:
290  TextLine(const Type& type = Type::type_unknown);
291 
292  void setBaseLine(const BaseLine& baseLine);
293  BaseLine baseLine() const;
294 
295  void setText(const QString& text);
296  QString text() const;
297 
298  virtual bool read(QXmlStreamReader& reader) override;
299  virtual void write(QXmlStreamWriter& writer) const override;
300 
301  virtual QString toString(bool withChildren = false) const override;
302 
303  virtual void draw(QPainter& p, const RegionTypeConfig& config) const override;
304 
305 protected:
308 };
309 
311 
312 public:
313  TextRegion(const Type& type = Type::type_unknown);
314 
315  void setText(const QString& text);
316  QString text() const;
317 
318  virtual bool read(QXmlStreamReader& reader) override;
319  virtual void write(QXmlStreamWriter& writer) const override;
320 
321  virtual QString toString(bool withChildren = false) const override;
322 
323  virtual void draw(QPainter& p, const RegionTypeConfig& config) const override;
324 
325 protected:
327 };
328 
330 
331 public:
332  SeparatorRegion(const Type& type = Type::type_unknown);
333 
334  void setLine(const Line& line);
335  Line line() const;
336 
337  //virtual bool read(QXmlStreamReader& reader) override;
338  //virtual void write(QXmlStreamWriter& writer, bool withChildren = true, bool close = true) const override;
339 
340  //virtual QString toString(bool withChildren = false) const override;
341 
342  //virtual void draw(QPainter& p, const RegionTypeConfig& config) const override;
343  //virtual bool operator==(const SeparatorRegion& sr1);
344  virtual bool operator==(const Region& sr1);
345 
346  static SeparatorRegion fromLine(const Line& l);
347 
348 protected:
350 };
351 
353 
354 public:
355 
356  LayerElement();
357 
358  bool readAttributes(QXmlStreamReader& reader);
359  void readChildren(QXmlStreamReader& reader);
360  void write(QXmlStreamWriter& writer) const;
361 
362  void setId(const QString& id);
363  QString id() const;
364 
365  void setZIndex(int zIndex);
366  int zIndex() const;
367 
368  void setCaption(const QString& caption);
369  QString caption() const;
370 
371  void setRegionRefIds(const QVector<QString>& regionRefIds);
372  QVector<QString> regionRefIds() const;
373 
374  void setRegions(const QVector<QSharedPointer<Region>>& regions);
375  QVector<QSharedPointer<Region>> regions() const;
376 
377 protected:
378  bool mChanged = false;
379  QString mId = "";
380  int mZIndex = 0;
381  QString mCaption = "";
382  QVector<QString> mRegionRefIds;
383  QVector<QSharedPointer<Region>> mRegions;
384 
385 };
386 
388 
389 public:
390  PageElement(const QString& xmlPath = QString());
391 
392  bool isEmpty();
393 
394  void setXmlPath(const QString& xmlPath);
395  QString xmlPath() const;
396 
397  void setImageFileName(const QString& name);
398  QString imageFileName() const;
399 
400  void setImageSize(const QSize& size);
401  QSize imageSize() const;
402 
403  void setRootRegion(QSharedPointer<RootRegion> region);
404  QSharedPointer<RootRegion> rootRegion() const;
405 
406  void setCreator(const QString& creator);
407  QString creator() const;
408 
409  void setDateCreated(const QDateTime& date);
410  QDateTime dateCreated() const;
411 
412  void setDateModified(const QDateTime& date);
413  QDateTime dateModified() const;
414 
415  void setLayers(const QVector<QSharedPointer<LayerElement>>& layers);
416  QVector<QSharedPointer<LayerElement>> layers() const;
417 
418  void setDefaultLayer(const QSharedPointer<LayerElement>& defaultLayer);
419  QSharedPointer<LayerElement> defaultLayer() const;
420 
421  void redefineLayersByType(const QVector<Region::Type>& layerTypeAssignment);
422 
423  void sortLayers(bool checkIfSorted = false);
424 
425  friend DllCoreExport QDebug operator<< (QDebug d, const PageElement &p);
426  friend DllCoreExport QDataStream& operator<<(QDataStream& s, const PageElement& p);
427  virtual QString toString() const;
428 
429 protected:
430  QString mXmlPath;
431  QString mImageFileName;
432  QSize mImageSize;
433 
434  // meta attributes
435  QString mCreator;
436  QDateTime mDateCreated;
437  QDateTime mDateModified;
438 
439  QSharedPointer<RootRegion> mRoot;
440  QVector<QSharedPointer<LayerElement>> mLayers;
441  QSharedPointer<LayerElement> mDefaultLayer;
442 
443  static bool layerZIndexGt(const QSharedPointer<LayerElement>& l1, const QSharedPointer<LayerElement>& l2);
444 
445 };
446 
447 }
Definition: Elements.h:77
Definition: Elements.h:269
Definition: Elements.h:75
Definition: Elements.h:74
Definition: Elements.h:158
BaseLine mBaseLine
Definition: Elements.h:306
QString mCustom
Definition: Elements.h:139
QString mXmlPath
Definition: Elements.h:430
#define DllCoreExport
Definition: BaseImageElement.h:43
Type
Definition: Elements.h:68
QString mCreator
Definition: Elements.h:435
Definition: Elements.h:287
QString mText
Definition: Elements.h:283
QDataStream & operator<<(QDataStream &s, const BaseElement &e)
Definition: BaseImageElement.cpp:81
Definition: Elements.h:78
Definition: Elements.h:71
Definition: Elements.h:76
Definition: Elements.h:310
TextEquiv mTextEquiv
Definition: Elements.h:307
TextEquiv mTextEquiv
Definition: Elements.h:326
Definition: Elements.h:79
QString mId
Definition: Elements.h:138
Definition: Elements.h:329
QVector< int > mCornerPts
Definition: Elements.h:266
QSize mImageSize
Definition: Elements.h:432
Definition: Elements.h:352
Definition: Elements.h:70
Polygon mPoly
Definition: Elements.h:140
Definition: Shapes.h:135
bool operator<(const PixelEdge &pe1, const PixelEdge &pe2)
Definition: Pixel.cpp:537
config
Definition: DependencyCollector.py:271
Definition: Shapes.h:468
QVector< QSharedPointer< LayerElement > > mLayers
Definition: Elements.h:440
bool operator==(const BaseElement &l, const QString &id)
Definition: BaseImageElement.cpp:57
QDateTime mDateModified
Definition: Elements.h:437
Definition: Elements.h:72
A basic line class including stroke width (thickness).
Definition: Shapes.h:68
Line mLine
Definition: Elements.h:349
Definition: Shapes.h:493
Definition: Elements.h:65
QDateTime mDateCreated
Definition: Elements.h:436
QVector< QSharedPointer< Region > > mRegions
Definition: Elements.h:383
Definition: Elements.h:192
Definition: Elements.h:73
QString mImageFileName
Definition: Elements.h:431
Definition: Elements.h:80
Definition: Elements.h:387
Definition: ElementsHelper.h:59
Definition: Algorithms.cpp:45
QVector< QSharedPointer< Region > > mChildren
Definition: Elements.h:141
Definition: Elements.h:147
QVector< QString > mRegionRefIds
Definition: Elements.h:382
QSharedPointer< RootRegion > mRoot
Definition: Elements.h:439
QSharedPointer< LayerElement > mDefaultLayer
Definition: Elements.h:441