Implemented TemplateDesigner.
This commit is contained in:
+59
-10
@@ -23,9 +23,11 @@
|
||||
#include "Config.h"
|
||||
#include "StrUtil.h"
|
||||
#include "FileUtil.h"
|
||||
#include "Settings.h"
|
||||
#include "XmlCategoryParser.h"
|
||||
#include "XmlPaperParser.h"
|
||||
#include "XmlTemplateParser.h"
|
||||
#include "XmlTemplateCreator.h"
|
||||
#include "XmlVendorParser.h"
|
||||
|
||||
#include <QtDebug>
|
||||
@@ -497,6 +499,22 @@ namespace glabels
|
||||
}
|
||||
|
||||
|
||||
bool Db::isSystemTemplateKnown( const QString& brand, const QString& part )
|
||||
{
|
||||
foreach ( Template *tmplate, mTemplates )
|
||||
{
|
||||
if ( (tmplate->brand() == brand) &&
|
||||
(tmplate->part() == part) &&
|
||||
!tmplate->isUserDefined() )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
QStringList Db::getNameListOfSimilarTemplates( const QString& name )
|
||||
{
|
||||
QStringList list;
|
||||
@@ -523,21 +541,52 @@ namespace glabels
|
||||
}
|
||||
|
||||
|
||||
void Db::registerUserTemplate( Template *templat )
|
||||
QString Db::userTemplateFilename( const QString& brand, const QString& part )
|
||||
{
|
||||
// TODO
|
||||
QString filename = brand + "_" + part + ".template";
|
||||
return FileUtil::userTemplatesDir().filePath( filename );
|
||||
}
|
||||
|
||||
|
||||
void Db::deleteUserTemplateByName( const QString& name )
|
||||
void Db::registerUserTemplate( Template *tmplate )
|
||||
{
|
||||
// TODO
|
||||
QString filename = userTemplateFilename( tmplate->brand(), tmplate->part() );
|
||||
|
||||
// Write file
|
||||
if ( XmlTemplateCreator().writeTemplate( tmplate, filename ) )
|
||||
{
|
||||
// Add template to list of registered templates
|
||||
registerTemplate( tmplate );
|
||||
Settings::addToRecentTemplateList( tmplate->name() );
|
||||
}
|
||||
else
|
||||
{
|
||||
qWarning() << "Problem writing user template" << filename;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Db::deleteUserTemplateByBrandPart( const QString& brand, const QString& part )
|
||||
{
|
||||
// TODO
|
||||
Template* tmplate;
|
||||
foreach ( Template *candidate, mTemplates )
|
||||
{
|
||||
if ( candidate->isUserDefined() &&
|
||||
(candidate->brand() == brand) && (candidate->part() == part) )
|
||||
{
|
||||
tmplate = candidate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( tmplate )
|
||||
{
|
||||
mTemplates.removeOne( tmplate );
|
||||
delete tmplate;
|
||||
|
||||
QString filename = userTemplateFilename( brand, part );
|
||||
QFile( filename ).remove();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -667,15 +716,15 @@ namespace glabels
|
||||
|
||||
void Db::readTemplates()
|
||||
{
|
||||
readTemplatesFromDir( FileUtil::systemTemplatesDir() );
|
||||
|
||||
// TODO: Read user directories
|
||||
readTemplatesFromDir( FileUtil::systemTemplatesDir(), false );
|
||||
readTemplatesFromDir( FileUtil::manualUserTemplatesDir(), false );
|
||||
readTemplatesFromDir( FileUtil::userTemplatesDir(), true );
|
||||
|
||||
std::stable_sort( mTemplates.begin(), mTemplates.end(), partNameLessThan );
|
||||
}
|
||||
|
||||
|
||||
void Db::readTemplatesFromDir( const QDir& dir )
|
||||
void Db::readTemplatesFromDir( const QDir& dir, bool isUserDefined )
|
||||
{
|
||||
QStringList filters;
|
||||
filters << "*-templates.xml" << "*.template";
|
||||
@@ -684,7 +733,7 @@ namespace glabels
|
||||
|
||||
foreach ( QString fileName, dir.entryList( filters, QDir::Files ) )
|
||||
{
|
||||
parser.readFile( dir.absoluteFilePath( fileName ) );
|
||||
parser.readFile( dir.absoluteFilePath( fileName ), isUserDefined );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -90,10 +90,11 @@ namespace glabels
|
||||
static const Template *lookupTemplateFromBrandPart( const QString& brand,
|
||||
const QString& part );
|
||||
static bool isTemplateKnown( const QString& brand, const QString& part );
|
||||
static bool isSystemTemplateKnown( const QString& brand, const QString& part );
|
||||
static QStringList getNameListOfSimilarTemplates( const QString& name );
|
||||
|
||||
static QString userTemplateFilename( const QString& brand, const QString& part );
|
||||
static void registerUserTemplate( Template *tmplate );
|
||||
static void deleteUserTemplateByName( const QString& name );
|
||||
static void deleteUserTemplateByBrandPart( const QString& brand,
|
||||
const QString& part );
|
||||
|
||||
@@ -116,7 +117,7 @@ namespace glabels
|
||||
static void readVendorsFromDir( const QDir& dir );
|
||||
|
||||
static void readTemplates();
|
||||
static void readTemplatesFromDir( const QDir& dir );
|
||||
static void readTemplatesFromDir( const QDir& dir, bool isUserDefined );
|
||||
|
||||
|
||||
private:
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "Config.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QStandardPaths>
|
||||
|
||||
|
||||
namespace glabels
|
||||
@@ -64,6 +65,27 @@ namespace glabels
|
||||
}
|
||||
|
||||
|
||||
QDir FileUtil::manualUserTemplatesDir()
|
||||
{
|
||||
// Location for manually created user-defined templates
|
||||
QDir dir( QStandardPaths::writableLocation(QStandardPaths::HomeLocation) );
|
||||
dir.mkpath( ".glabels" );
|
||||
dir.cd( ".glabels" );
|
||||
|
||||
return dir;
|
||||
}
|
||||
|
||||
|
||||
QDir FileUtil::userTemplatesDir()
|
||||
{
|
||||
// Location for user-defined templates created using TemplateDesigner
|
||||
QDir dir( QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) );
|
||||
dir.mkpath( "." );
|
||||
|
||||
return dir;
|
||||
}
|
||||
|
||||
|
||||
QDir FileUtil::translationsDir()
|
||||
{
|
||||
QDir dir;
|
||||
|
||||
@@ -37,6 +37,7 @@ namespace glabels
|
||||
QString addExtension( const QString& rawFilename, const QString& extension );
|
||||
|
||||
QDir systemTemplatesDir();
|
||||
QDir manualUserTemplatesDir();
|
||||
QDir userTemplatesDir();
|
||||
|
||||
QDir translationsDir();
|
||||
|
||||
@@ -92,6 +92,40 @@ namespace glabels
|
||||
}
|
||||
|
||||
|
||||
Settings::PageSizeFamily Settings::preferedPageSizeFamily()
|
||||
{
|
||||
// Guess at a suitable default
|
||||
QString defaultFamily;
|
||||
switch (QLocale::system().country())
|
||||
{
|
||||
case QLocale::UnitedStates:
|
||||
case QLocale::Canada:
|
||||
defaultFamily = "us";
|
||||
break;
|
||||
|
||||
default:
|
||||
defaultFamily = "iso";
|
||||
break;
|
||||
}
|
||||
|
||||
mInstance->beginGroup( "Locale" );
|
||||
QString value = mInstance->value( "preferedPageSizeFamily", defaultFamily ).toString();
|
||||
mInstance->endGroup();
|
||||
|
||||
return (value == "iso") ? ISO : US;
|
||||
}
|
||||
|
||||
|
||||
void Settings::setPreferedPageSizeFamily( PageSizeFamily preferedPageSizeFamily )
|
||||
{
|
||||
mInstance->beginGroup( "Locale" );
|
||||
mInstance->setValue( "preferedPageSizeFamily", preferedPageSizeFamily == ISO ? "iso" : "us" );
|
||||
mInstance->endGroup();
|
||||
|
||||
emit mInstance->changed();
|
||||
}
|
||||
|
||||
|
||||
bool Settings::searchIsoPaperSizes()
|
||||
{
|
||||
// Guess at a suitable default
|
||||
|
||||
+4
-1
@@ -41,7 +41,7 @@ namespace glabels
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum class PageSizeFamilty { ISO, US, };
|
||||
enum PageSizeFamily { ISO, US, };
|
||||
|
||||
|
||||
/////////////////////////////////
|
||||
@@ -69,6 +69,9 @@ namespace glabels
|
||||
static Units units();
|
||||
static void setUnits( const Units& units );
|
||||
|
||||
static PageSizeFamily preferedPageSizeFamily();
|
||||
static void setPreferedPageSizeFamily( PageSizeFamily preferedPageSizeFamily );
|
||||
|
||||
static bool searchIsoPaperSizes();
|
||||
static void setSearchIsoPaperSizes( bool searchIsoPaperSizes );
|
||||
|
||||
|
||||
+9
-1
@@ -35,13 +35,15 @@ namespace glabels
|
||||
const QString& description,
|
||||
const QString& paperId,
|
||||
const Distance& pageWidth,
|
||||
const Distance& pageHeight )
|
||||
const Distance& pageHeight,
|
||||
bool isUserDefined )
|
||||
: mBrand(brand),
|
||||
mPart(part),
|
||||
mDescription(description),
|
||||
mPaperId(paperId),
|
||||
mPageWidth(pageWidth),
|
||||
mPageHeight(pageHeight),
|
||||
mIsUserDefined(isUserDefined),
|
||||
mIsSizeIso(false),
|
||||
mIsSizeUs(false),
|
||||
mName("")
|
||||
@@ -179,6 +181,12 @@ namespace glabels
|
||||
}
|
||||
|
||||
|
||||
bool Template::isUserDefined() const
|
||||
{
|
||||
return mIsUserDefined;
|
||||
}
|
||||
|
||||
|
||||
QString Template::equivPart() const
|
||||
{
|
||||
return mEquivPart;
|
||||
|
||||
+6
-1
@@ -48,7 +48,8 @@ namespace glabels
|
||||
const QString& description,
|
||||
const QString& paperId,
|
||||
const Distance& pageWidth,
|
||||
const Distance& pageHeight );
|
||||
const Distance& pageHeight,
|
||||
bool isUserDefined = false );
|
||||
|
||||
Template( const Template& other );
|
||||
|
||||
@@ -74,6 +75,8 @@ namespace glabels
|
||||
bool isSizeUs() const;
|
||||
bool isSizeOther() const;
|
||||
|
||||
bool isUserDefined() const;
|
||||
|
||||
QString equivPart() const;
|
||||
void setEquivPart( const QString& value );
|
||||
|
||||
@@ -104,6 +107,8 @@ namespace glabels
|
||||
bool mIsSizeIso;
|
||||
bool mIsSizeUs;
|
||||
|
||||
bool mIsUserDefined;
|
||||
|
||||
QString mEquivPart;
|
||||
QString mName;
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ namespace glabels
|
||||
namespace model
|
||||
{
|
||||
|
||||
bool XmlTemplateParser::readFile( const QString &fileName )
|
||||
bool XmlTemplateParser::readFile( const QString &fileName, bool isUserDefined )
|
||||
{
|
||||
QFile file( fileName );
|
||||
|
||||
@@ -73,18 +73,18 @@ namespace glabels
|
||||
return false;
|
||||
}
|
||||
|
||||
parseRootNode( root );
|
||||
parseRootNode( root, isUserDefined );
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void XmlTemplateParser::parseRootNode( const QDomElement &node )
|
||||
void XmlTemplateParser::parseRootNode( const QDomElement &node, bool isUserDefined )
|
||||
{
|
||||
for ( QDomNode child = node.firstChild(); !child.isNull(); child = child.nextSibling() )
|
||||
{
|
||||
if ( child.toElement().tagName() == "Template" )
|
||||
{
|
||||
Template *tmplate = parseTemplateNode( child.toElement() );
|
||||
Template *tmplate = parseTemplateNode( child.toElement(), isUserDefined );
|
||||
if ( tmplate != nullptr )
|
||||
{
|
||||
Db::registerTemplate( tmplate );
|
||||
@@ -104,7 +104,7 @@ namespace glabels
|
||||
}
|
||||
|
||||
|
||||
Template *XmlTemplateParser::parseTemplateNode( const QDomElement &node )
|
||||
Template *XmlTemplateParser::parseTemplateNode( const QDomElement &node, bool isUserDefined )
|
||||
{
|
||||
QString brand = XmlUtil::getStringAttr( node, "brand", "" );
|
||||
QString part = XmlUtil::getStringAttr( node, "part", "" );
|
||||
@@ -163,14 +163,14 @@ namespace glabels
|
||||
}
|
||||
|
||||
tmplate = new Template( brand, part, description,
|
||||
paper->id(), paper->width(), paper->height() );
|
||||
paper->id(), paper->width(), paper->height(), isUserDefined );
|
||||
}
|
||||
else
|
||||
{
|
||||
Distance width = XmlUtil::getLengthAttr( node, "width", Distance(0) );
|
||||
Distance height = XmlUtil::getLengthAttr( node, "height", Distance(0) );
|
||||
|
||||
tmplate = new Template( brand, part, description, paperId, width, height );
|
||||
tmplate = new Template( brand, part, description, paperId, width, height, isUserDefined );
|
||||
}
|
||||
|
||||
for ( QDomNode child = node.firstChild(); !child.isNull(); child = child.nextSibling() )
|
||||
|
||||
@@ -38,11 +38,11 @@ namespace glabels
|
||||
public:
|
||||
XmlTemplateParser() = default;
|
||||
|
||||
bool readFile( const QString &fileName );
|
||||
Template *parseTemplateNode( const QDomElement &node );
|
||||
bool readFile( const QString &fileName, bool isUserDefined = false );
|
||||
Template *parseTemplateNode( const QDomElement &node, bool isUserDefined = false );
|
||||
|
||||
private:
|
||||
void parseRootNode( const QDomElement &node );
|
||||
void parseRootNode( const QDomElement &node, bool isUserDefined );
|
||||
void parseMetaNode( const QDomElement &node, Template *tmplate );
|
||||
void parseLabelRectangleNode( const QDomElement &node, Template *tmplate );
|
||||
void parseLabelEllipseNode( const QDomElement &node, Template *tmplate );
|
||||
|
||||
Reference in New Issue
Block a user