35 using namespace crn::literals;
37 const Path Document::thumbdir(
"/thumbs/");
38 size_t Document::thumbWidth = 70;
39 size_t Document::thumbHeight = 100;
87 void Document::addView(
const Path &fname,
const String &
id)
89 views.push_back(view(fname,
id));
107 if (pos > views.size())
110 if (pos == views.size())
115 views.insert(views.begin() + pos, view(fname,
id));
133 auto it(std::find(views.begin(), views.end(), v));
134 if (it == views.end())
149 if (num >= views.size())
152 Path thumbname(basename + thumbdir + views[num].
id);
158 Path xmlname(basename +
"/" + views[num].
id +
".xml");
164 views.erase(views.begin() + num);
197 if (num >= views.size())
201 if (views[num].ptr.expired())
207 s +=
Path(views[num].
id) +
".xml";
209 SBlock b(
Block::New(views[num].filename, s, views[num].filename));
213 return views[num].ptr.lock();
260 for (
size_t tmp = 0; tmp < views.size(); ++tmp)
262 if (views[tmp].
id ==
id)
278 for (
size_t tmp = 0; tmp < views.size(); ++tmp)
280 if (views[tmp].filename == fname)
295 if (num >= views.size())
298 return views[num].id;
310 for (
auto & elem : views)
312 if (elem.filename == fname)
327 if (num >= views.size())
330 return views[num].filename;
357 if (from.size() != views.size())
360 _(
"bad changeset size."));
362 std::set<size_t> cont;
363 for (
size_t i : from)
367 if (cont.size() != views.size())
370 _(
"changeset contains duplicates."));
372 if ((*cont.begin() != 0) || (*cont.rbegin() != views.size() - 1))
375 _(
"changeset contains values out of bounds."));
377 std::vector<view> newviews;
378 for (
size_t i : from)
380 newviews.push_back(views[i]);
382 views.swap(newviews);
397 if (to.size() != views.size())
400 _(
"bad changeset size."));
402 std::set<size_t> cont;
407 if (cont.size() != views.size())
410 _(
"changeset contains duplicates."));
412 if ((*cont.begin() != 0) || (*cont.rbegin() != views.size() - 1))
415 _(
"changeset contains values out of bounds."));
417 std::vector<view> newviews(views.size());
418 for (
size_t tmp = 0; tmp < to.size(); ++tmp)
420 newviews[to[tmp]] = views[tmp];
422 views.swap(newviews);
448 void Document::load(
const Path &fname)
452 if (root.GetName() !=
"Document")
455 _(
"Not a Document file."));
458 std::multimap<int, std::pair<Path, String> > xmlviews;
464 try { num = vi.GetAttribute<
int>(
"num",
false); }
catch (...) { num = int(xmlviews.size()); }
468 xmlviews.insert(std::make_pair(num, std::make_pair(fname,
id)));
469 vi = vi.GetNextSiblingElement(
"View");
489 for (
auto & xmlview : xmlviews)
491 addView(xmlview.second.first, xmlview.second.second);
520 return basename + thumbdir;
531 UImage Document::createThumbnail(
const Path &imagename)
const
536 nh = img->GetHeight() * thumbWidth / img->GetWidth();
537 if (nh <= thumbHeight)
543 nw = img->GetWidth() * thumbHeight / img->GetHeight();
546 img->ScaleToSize(nw, nh);
547 return std::forward<UImage>(img);
597 throw ExceptionDomain(
StringUTF8(
"const Path Document::GetThumbnailFilename(size_t index, bool refresh) const: ") +
_(
"index out of bounds."));
614 img->SavePNG(thumbname);
644 void Document::save(
const Path &fname)
659 root.SetAttribute(
"author", author.
CStr());
660 root.SetAttribute(
"date", date.
CStr());
663 for (
size_t tmp = 0; tmp < views.size(); tmp++)
667 el.SetAttribute(
"id", views[tmp].
id.CStr());
668 el.SetAttribute(
"num",
int(tmp));
699 Path Document::completeFilename(
const Path &fn)
const
711 std::vector<Path> flist;
712 for (
const view &v : views)
713 flist.push_back(v.filename);
724 std::vector<String> flist;
725 for (
const view &v : views)
726 flist.push_back(v.id);
734 String Document::createNewId()
const
739 for (
const view &v : views)
742 return createNewId();
747 #ifdef CRN_USING_HARU
761 std::vector<Path> images;
762 Path tmpimg(tmpnam(
nullptr));
765 for (
const view &v : views)
767 PDF::Page page = pdf.AddPage();
773 image = pdf.AddJPEG(tmpimg);
778 img->SavePNG(images.back());
779 images.push_back(tmpnam(
nullptr));
780 image = pdf.AddPNG(images.back());
782 page.SetWidth((
double)image.GetWidth());
783 page.SetHeight((
double)image.GetHeight());
784 page.DrawImage(image, {0, 0, (int)image.GetWidth() - 1, (int)image.GetHeight() - 1});
800 for (
const Path &fname : images)
828 throw ExceptionDomain{
"Document::SetThumbHeight()"_s +
_(
"Null height.") };
void ReorderViewsTo(const std::vector< size_t > &to)
Reorders the views.
Comment PushBackComment(const StringUTF8 &text)
Adds a comment at the end of the children list.
static void SetThumbHeight(size_t h)
static void SetThumbWidth(size_t w)
Base class for a progress display.
virtual ~Document() override
Destructor.
complex base abstract class
void SetDate(const String &s)
Sets the date of the document.
SBlock GetView(size_t num) const
Returns a pointer to a view.
std::unique_ptr< ImageBase > UImage
const char * CStr() const
Conversion to UTF8 cstring.
Unintialized object error.
Element PushBackElement(const StringUTF8 &name)
Adds an element at the end of the children list.
bool IsNotEmpty() const noexcept
Checks if the string is not empty.
static void Mkdir(const Path &name)
Creates a directory.
static char Separator() noexcept
Local directory separator.
std::vector< Path > GetFilenames() const
Gets the list of the image files of the document.
void serialize_internal_data(xml::Element &el) const
Dumps some internal data to an XML element.
void Advance()
Progresses of one step.
A UTF32 character string class.
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)
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.
String InsertView(const Path &fname, size_t pos)
Inserts a new image.
StringUTF8 SubString(size_t pos, size_t n=0) const
Extracts a part of the string.
static SBlock New(const SImage &src, const String &nam=U"")
Top block creator.
const char * CStr() const noexcept
Conversion to UTF8 cstring.
Path GetViewFilename(size_t num) const
Returns the filename of a view.
A convenience class for file paths.
static size_t GetThumbHeight() noexcept
void ClearUserData()
Deletes all user data entries.
Path GetThumbnailPath() const
Returns the path of the thumbnails.
static String CreateUniqueId(size_t len=8)
Generates an almost unique id.
String GetViewId(size_t num) const
Returns the id of a view.
void SetAttribute(const StringUTF8 &name, const StringUTF8 &value)
Sets the value of an attribute.
void Save(const Path &fname)
Saves to file.
static size_t GetThumbWidth() noexcept
void SetMaxCount(size_t maxcount, bool reset=true)
Sets the total number of steps.
static Path GetTopDataPath()
Gets the top directory name.
void RemoveView(const Path &fname)
Removes a view.
UImage NewImageFromFile(const Path &fname)
Loads an image from a file.
void SetName(const String &s)
Sets the name of the object.
Attributes to create a PDF export of a document.
size_t GetViewIndex(const String &id) const
Returns the index of a view.
static void Rm(const Path &name)
Removes a file.
static bool Access(const Path &name, int mode)
Checks rights on a file.
size_t GetNbViews() const noexcept
Returns the number of views.
void Clear()
Removes all views and unsets all data.
A character string class.
void ReorderViewsFrom(const std::vector< size_t > &from)
Reorders the views.
void SetAuthor(const String &s)
Sets the author of the document.
std::vector< String > GetViewIds() const
Gets the list of the view ids of the document.
size_t BackwardFindAnyOf(const StringUTF8 &s, size_t from_pos=NPos()) const
Finds the last occurrence of character in a list.
void deserialize_internal_data(xml::Element &el)
Initializes some internal data from an XML element.
Invalid argument error (e.g.: nullptr pointer)
static size_t NPos() noexcept
Last position in a string.
An item was not found in a container.