Files
create/src/Mod/TechDraw/App/DrawTemplate.cpp

177 lines
6.4 KiB
C++

/***************************************************************************
* Copyright (c) 2014 Luke Parry <l.parry@warwick.ac.uk> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <sstream>
# include <QCollator>
# include <QDateTime>
#endif
#include <Base/Console.h>
#include <App/Application.h>
#include <App/Document.h>
#include "DrawTemplate.h"
#include "DrawTemplatePy.h"
#include "DrawPage.h"
#include "DrawUtil.h"
using namespace TechDraw;
PROPERTY_SOURCE(TechDraw::DrawTemplate, App::DocumentObject)
const char* DrawTemplate::OrientationEnums[]= {"Portrait",
"Landscape",
nullptr};
DrawTemplate::DrawTemplate()
{
const char *group = "Page Properties";
Orientation.setEnums(OrientationEnums);
ADD_PROPERTY(Orientation, (0l));
// Physical Properties inherent to every template class
ADD_PROPERTY_TYPE(Width, (0), group, App::PropertyType::Prop_None, "Width of page");
ADD_PROPERTY_TYPE(Height, (0), group, App::PropertyType::Prop_None, "Height of page");
ADD_PROPERTY_TYPE(EditableTexts, (), group, App::PropertyType::Prop_None,
"Editable strings in the template");
}
DrawTemplate::~DrawTemplate()
{
}
PyObject *DrawTemplate::getPyObject()
{
if (PythonObject.is(Py::_None())) {
// ref counter is set to 1
PythonObject = Py::Object(new DrawTemplatePy(this), true);
}
return Py::new_reference_to(PythonObject);
}
double DrawTemplate::getWidth() const
{
return Width.getValue();
}
double DrawTemplate::getHeight() const
{
return Height.getValue();
}
//find the (first) DrawPage which points to this template
DrawPage* DrawTemplate::getParentPage() const
{
TechDraw::DrawPage* page(nullptr);
std::vector<App::DocumentObject*> parents = getInList();
for (auto& obj : parents) {
if (obj->isDerivedFrom<DrawPage>()) {
page = static_cast<TechDraw::DrawPage *>(obj);
break;
}
}
return page;
}
QString DrawTemplate::getAutofillValue(const QString &id) const
{
// author
if (id.compare(QString::fromUtf8(Autofill::Author)) == 0) {
std::string value = App::GetApplication().GetUserParameter().GetGroup("BaseApp")->GetGroup("Preferences")
->GetGroup("Document")->GetASCII("prefAuthor");
if (!value.empty()) {
return QString::fromUtf8(value.c_str());
}
}
// date
else if (id.compare(QString::fromUtf8(Autofill::Date)) == 0) {
QDateTime date = QDateTime::currentDateTime();
return date.toString(QLocale().dateFormat(QLocale::ShortFormat));
}
// organization
else if (id.compare(QString::fromUtf8(Autofill::Organization)) == 0) {
std::string value = App::GetApplication().GetUserParameter().GetGroup("BaseApp")->GetGroup("Preferences")
->GetGroup("Document")->GetASCII("prefCompany");
if (!value.empty()) {
return QString::fromUtf8(value.c_str());
}
}
// scale
else if (id.compare(QString::fromUtf8(Autofill::Scale)) == 0) {
DrawPage *page = getParentPage();
if (page) {
std::pair<int, int> scale = DrawUtil::nearestFraction(page->Scale.getValue());
return QString::asprintf("%d : %d", scale.first, scale.second);
}
}
// sheet
else if (id.compare(QString::fromUtf8(Autofill::Sheet)) == 0) {
std::vector<DocumentObject *> pages = getDocument()->getObjectsOfType(TechDraw::DrawPage::getClassTypeId());
std::vector<QString> pageNames;
for (auto page : pages) {
pageNames.push_back(QString::fromUtf8(page->Label.getValue()));
}
QCollator collator;
std::sort(pageNames.begin(), pageNames.end(), collator);
int pos = 0;
DrawPage *page = getParentPage();
if (page) {
auto it = std::find(pageNames.begin(), pageNames.end(), QString::fromUtf8(page->Label.getValue()));
if (it != pageNames.end()) {
pos = it - pageNames.begin() + 1;
}
}
return QString::asprintf("%d / %d", pos, (int) pageNames.size());
}
// title
else if (id.compare(QString::fromUtf8(Autofill::Title)) == 0) {
return QString::fromUtf8(getDocument()->Label.getValue());
}
return QString();
}
// Python Template feature ---------------------------------------------------------
namespace App {
/// @cond DOXERR
PROPERTY_SOURCE_TEMPLATE(TechDraw::DrawTemplatePython, TechDraw::DrawTemplate)
template<> const char* TechDraw::DrawTemplatePython::getViewProviderName() const {
return "TechDrawGui::ViewProviderPython";
}
/// @endcond
// explicit template instantiation
template class TechDrawExport FeaturePythonT<TechDraw::DrawTemplate>;
} // namespace App