libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNDocument.h
Go to the documentation of this file.
1 /* Copyright 2006-2016 Yann LEYDIER, CoReNum, INSA-Lyon, ENS-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNDocument.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNDOCUMENT_HEADER
23 #define CRNDOCUMENT_HEADER
24 
25 #include <vector>
26 #include <CRNSavable.h>
27 #include <CRNBlock.h>
28 #include <CRNDocumentPtr.h>
29 #include <CRNData/CRNForeach.h>
30 #include <CRNIO/CRNPath.h>
31 #ifdef CRN_USING_HARU
33 #endif
34 
38 namespace crn
39 {
40  class Progress;
41 
42  /****************************************************************************/
52  class Document: public Savable
53  {
54  public:
56  Document();
58  virtual ~Document() override;
59 
60  Document(const Document &) = delete;
61  Document& operator=(const Document &) = delete;
62  Document(Document &&) = default;
63  Document& operator=(Document &&) = default;
64 
66  void SetBasename(const Path &s) { basename = s; }
68  void SetAuthor(const String &s) { author = s; }
70  void SetDate(const String &s) { date = s; }
71 
73  const Path& GetBasename() const noexcept { return basename; }
75  const String& GetAuthor() const noexcept { return author; }
77  const String& GetDate() const noexcept { return date; }
79  std::vector<Path> GetFilenames() const;
81  std::vector<String> GetViewIds() const;
82 
84  String AddView(const Path &fname);
86  String InsertView(const Path &fname, size_t pos);
88  void RemoveView(const Path &fname);
90  void RemoveView(size_t num);
92  void RemoveView(const String &id);
94  SBlock GetView(size_t num) const;
96  SBlock GetView(const String &id) const;
98  SBlock GetView(const Path &fname) const;
100  size_t GetViewIndex(const String &id) const;
102  size_t GetViewIndex(const Path &fname) const;
104  String GetViewId(size_t num) const;
106  String GetViewId(const Path &fname) const;
108  Path GetViewFilename(size_t num) const;
110  Path GetViewFilename(const String &id) const;
112  size_t GetNbViews() const noexcept { return views.size(); }
114  void ReorderViewsFrom(const std::vector<size_t> &from);
116  void ReorderViewsTo(const std::vector<size_t> &to);
118  void Clear();
119 
121  Path GetThumbnailPath() const;
123  UImage GetThumbnail(size_t index, bool refresh = false) const;
125  UImage GetThumbnail(const String &id, bool refresh = false) const;
127  Path GetThumbnailFilename(size_t index, bool refresh = false) const;
129  Path GetThumbnailFilename(const String &id, bool refresh = false) const;
130 
132  class iterator: public std::iterator<std::input_iterator_tag, SBlock>
133  {
134  public:
135  iterator(const Document *d, size_t n):doc(d),num(n) { init_block(); }
136  iterator(const iterator &it):doc(it.doc),num(it.num) { init_block(); }
137  iterator(iterator&&) = default;
138  iterator& operator=(const iterator &it) { doc = it.doc; num = it.num; init_block(); return *this; }
139  iterator& operator=(iterator&&) = default;
140  iterator& operator++() { num += 1; init_block(); return *this; }
141  iterator operator++(int nouse) { iterator it = *this; ++*this; return it; }
142  bool operator==(const iterator &it) const noexcept { return (doc == it.doc) && (num == it.num); }
143  bool operator!=(const iterator &it) const noexcept { return (doc != it.doc) || (num != it.num); }
144  value_type operator*() const noexcept { return b; }
145  value_type operator->() const noexcept { return b; }
146  size_t GetIndex() const noexcept { return num; }
147  String GetId() const { return doc->GetViewId(num); }
148  Path GetFilename() const { return doc->GetViewFilename(num); }
149  private:
150  void init_block() { if (num < doc->views.size()) b = doc->GetView(num); else b = nullptr; }
151  const Document *doc;
152  SBlock b;
153  size_t num;
154  };
156  iterator begin() const { return iterator(this, 0); }
158  iterator end() const { return iterator(this, views.size()); }
159 
161  static Path GetDefaultDirName();
162 
163 #ifdef CRN_USING_HARU
164 
165  void ExportPDF(const Path &fname, const PDF::Attributes &attr = PDF::Attributes(), Progress *prog = nullptr) const;
166 #endif
167 
168  static size_t GetThumbWidth() noexcept;
169  static size_t GetThumbHeight() noexcept;
170  static void SetThumbWidth(size_t w);
171  static void SetThumbHeight(size_t h);
172 
173  private:
175  void addView(const Path &fname, const String &id);
176 
178  virtual Path completeFilename(const Path &fn) const override;
180  virtual void load(const Path &fname) override;
182  virtual void save(const Path &fname) override;
183 
185  UImage createThumbnail(const Path &imagename) const;
186 
188  String createNewId() const;
191  struct view
192  {
194  view(const Path &fname = "", const String &unique_id = U""):filename(fname),id(unique_id) {}
195  /* using default copy constructor */
197  bool operator==(const view &other) const noexcept { return filename == other.filename; }
198  Path filename;
199  String id;
200  mutable WBlock ptr;
201  };
202 
203  std::vector<view> views;
205  Path basename;
206  String author;
207  String date;
209  static const Path thumbdir;
210  static size_t thumbWidth;
211  static size_t thumbHeight;
212  };
213 
215  inline size_t Size(const Document &doc) noexcept { return doc.GetNbViews(); }
216 }
218 #endif
Iterator on the blocks of the document.
Definition: CRNDocument.h:132
void ReorderViewsTo(const std::vector< size_t > &to)
Reorders the views.
Path GetFilename() const
Definition: CRNDocument.h:148
bool operator!=(const iterator &it) const noexcept
Definition: CRNDocument.h:143
iterator(const iterator &it)
Definition: CRNDocument.h:136
static void SetThumbHeight(size_t h)
static void SetThumbWidth(size_t w)
Base class for a progress display.
Definition: CRNProgress.h:39
size_t GetIndex() const noexcept
Definition: CRNDocument.h:146
virtual ~Document() override
Destructor.
Definition: CRNDocument.cpp:58
Document()
Constructor.
Definition: CRNDocument.cpp:46
complex base abstract class
Definition: CRNSavable.h:58
void SetDate(const String &s)
Sets the date of the document.
Definition: CRNDocument.h:70
SBlock GetView(size_t num) const
Returns a pointer to a view.
iterator & operator=(const iterator &it)
Definition: CRNDocument.h:138
Document & operator=(const Document &)=delete
std::unique_ptr< ImageBase > UImage
Definition: CRNImage.h:105
iterator(const Document *d, size_t n)
Definition: CRNDocument.h:135
#define CRN_ADD_RANGED_FOR_TO_POINTERS(TYPE)
Enables ranged for for smart pointers on a type.
Definition: CRNForeach.h:37
void SetBasename(const Path &s)
Sets the base name of the document if any.
Definition: CRNDocument.h:66
std::vector< Path > GetFilenames() const
Gets the list of the image files of the document.
iterator & operator++()
Definition: CRNDocument.h:140
A UTF32 character string class.
Definition: CRNString.h:61
static Path GetDefaultDirName()
Returns the default directory where the documents are saved.
Path GetThumbnailFilename(size_t index, bool refresh=false) const
Returns the filename of a thumbnail of a view (cached)
String GetId() const
Definition: CRNDocument.h:147
UImage GetThumbnail(size_t index, bool refresh=false) const
Returns a thumbnail of a view (cached)
String AddView(const Path &fname)
Adds a new image.
Definition: CRNDocument.cpp:71
String InsertView(const Path &fname, size_t pos)
Inserts a new image.
size_t Size(const Vector &v) noexcept
Size of a vector.
Definition: CRNVector.h:169
const String & GetAuthor() const noexcept
Gets the author of the document.
Definition: CRNDocument.h:75
Path GetViewFilename(size_t num) const
Returns the filename of a view.
A convenience class for file paths.
Definition: CRNPath.h:39
static size_t GetThumbHeight() noexcept
iterator operator++(int nouse)
Definition: CRNDocument.h:141
Path GetThumbnailPath() const
Returns the path of the thumbnails.
String GetViewId(size_t num) const
Returns the id of a view.
iterator begin() const
Returns an iterator to the first block.
Definition: CRNDocument.h:156
value_type operator*() const noexcept
Definition: CRNDocument.h:144
static size_t GetThumbWidth() noexcept
bool operator==(const iterator &it) const noexcept
Definition: CRNDocument.h:142
void RemoveView(const Path &fname)
Removes a view.
Attributes to create a PDF export of a document.
size_t GetViewIndex(const String &id) const
Returns the index of a view.
iterator end() const
Returns an iterator after the last block.
Definition: CRNDocument.h:158
const String & GetDate() const noexcept
Gets the date of the document.
Definition: CRNDocument.h:77
size_t GetNbViews() const noexcept
Returns the number of views.
Definition: CRNDocument.h:112
void Clear()
Removes all views and unsets all data.
bool operator==(const String &s1, const String &s2)
Tests equality of two strings.
Definition: CRNString.h:287
const Path & GetBasename() const noexcept
Gets the file base name of the document.
Definition: CRNDocument.h:73
void ReorderViewsFrom(const std::vector< size_t > &from)
Reorders the views.
void SetAuthor(const String &s)
Sets the author of the document.
Definition: CRNDocument.h:68
std::vector< String > GetViewIds() const
Gets the list of the view ids of the document.
value_type operator->() const noexcept
Definition: CRNDocument.h:145
Document utility class.
Definition: CRNDocument.h:52