Fix code style

This commit is contained in:
Simon De Backer 2020-09-18 23:05:57 +02:00
parent 176dcd376b
commit b7c8ae88e2

View File

@ -1,17 +1,17 @@
/* /*
Copyright 2006-2020 The QElectroTech Team Copyright 2006-2020 The QElectroTech Team
This file is part of QElectroTech. This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or the Free Software Foundation, either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
QElectroTech is distributed in the hope that it will be useful, QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>. along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/ */
@ -23,6 +23,8 @@
// uncomment the line below to get more debug information // uncomment the line below to get more debug information
//#define TITLEBLOCK_TEMPLATE_DEBUG //#define TITLEBLOCK_TEMPLATE_DEBUG
#include <QRegularExpression>
#include <QRegularExpressionMatch>
/** /**
@brief TitleBlockTemplate::TitleBlockTemplate @brief TitleBlockTemplate::TitleBlockTemplate
Constructor Constructor
@ -97,7 +99,7 @@ bool TitleBlockTemplate::loadFromXmlFile(const QString &filepath) {
#ifdef TITLEBLOCK_TEMPLATE_DEBUG #ifdef TITLEBLOCK_TEMPLATE_DEBUG
qDebug() << Q_FUNC_INFO << filepath << "opened"; qDebug() << Q_FUNC_INFO << filepath << "opened";
#endif #endif
// parse its content as XML // parse its content as XML
QDomDocument xml_doc; QDomDocument xml_doc;
bool xml_parsing = xml_doc.setContent(&template_file); bool xml_parsing = xml_doc.setContent(&template_file);
@ -124,11 +126,11 @@ bool TitleBlockTemplate::loadFromXmlElement(const QDomElement &xml_element) {
return(false); return(false);
} }
name_ = xml_element.attribute("name"); name_ = xml_element.attribute("name");
loadInformation(xml_element); loadInformation(xml_element);
loadLogos(xml_element, true); loadLogos(xml_element, true);
loadGrid(xml_element); loadGrid(xml_element);
return(true); return(true);
} }
@ -141,14 +143,14 @@ bool TitleBlockTemplate::loadFromXmlElement(const QDomElement &xml_element) {
*/ */
bool TitleBlockTemplate::saveToXmlFile(const QString &filepath) { bool TitleBlockTemplate::saveToXmlFile(const QString &filepath) {
if (filepath.isEmpty()) return(false); if (filepath.isEmpty()) return(false);
// generate the XML document // generate the XML document
QDomDocument doc; QDomDocument doc;
QDomElement e = doc.createElement("root"); QDomElement e = doc.createElement("root");
bool saving = saveToXmlElement(e); bool saving = saveToXmlElement(e);
if (!saving) return(false); if (!saving) return(false);
doc.appendChild(e); doc.appendChild(e);
return(QET::writeXmlFile(doc, filepath)); return(QET::writeXmlFile(doc, filepath));
} }
@ -163,7 +165,7 @@ bool TitleBlockTemplate::saveToXmlElement(QDomElement &xml_element) const
{ {
// we are supposed to have at least a name // we are supposed to have at least a name
if (name_.isEmpty()) return(false); if (name_.isEmpty()) return(false);
xml_element.setTagName("titleblocktemplate"); xml_element.setTagName("titleblocktemplate");
xml_element.setAttribute("name", name_); xml_element.setAttribute("name", name_);
saveInformation(xml_element); saveInformation(xml_element);
@ -195,7 +197,7 @@ TitleBlockTemplate *TitleBlockTemplate::clone() const
TitleBlockTemplate *copy = new TitleBlockTemplate(); TitleBlockTemplate *copy = new TitleBlockTemplate();
copy -> name_ = name_; copy -> name_ = name_;
copy -> information_ = information_; copy -> information_ = information_;
// this does not really duplicates pixmaps, // this does not really duplicates pixmaps,
// only the objects that hold a key to the implicitly shared pixmaps // only the objects that hold a key to the implicitly shared pixmaps
foreach (QString logo_key, bitmap_logos_.keys()) { foreach (QString logo_key, bitmap_logos_.keys()) {
@ -209,20 +211,20 @@ TitleBlockTemplate *TitleBlockTemplate::clone() const
<< copy -> bitmap_logos_[logo_key] -> cacheKey(); << copy -> bitmap_logos_[logo_key] -> cacheKey();
#endif #endif
} }
// we have to create new QSvgRenderer objects from the data // we have to create new QSvgRenderer objects from the data
// (no copy constructor) // (no copy constructor)
foreach (QString logo_key, vector_logos_.keys()) { foreach (QString logo_key, vector_logos_.keys()) {
copy -> vector_logos_[logo_key] = copy -> vector_logos_[logo_key] =
new QSvgRenderer(data_logos_[logo_key]); new QSvgRenderer(data_logos_[logo_key]);
} }
copy -> data_logos_ = data_logos_; copy -> data_logos_ = data_logos_;
copy -> storage_logos_ = storage_logos_; copy -> storage_logos_ = storage_logos_;
copy -> type_logos_ = type_logos_; copy -> type_logos_ = type_logos_;
copy -> rows_heights_ = rows_heights_; copy -> rows_heights_ = rows_heights_;
copy -> columns_width_ = columns_width_; copy -> columns_width_ = columns_width_;
// copy cells basically // copy cells basically
copy -> cells_ = cells_; copy -> cells_ = cells_;
for (int j = 0 ; j < rows_heights_.count() ; ++ j) { for (int j = 0 ; j < rows_heights_.count() ; ++ j) {
@ -230,7 +232,7 @@ TitleBlockTemplate *TitleBlockTemplate::clone() const
copy -> cells_[i][j] = copy -> createCell(cells_[i][j]); copy -> cells_[i][j] = copy -> createCell(cells_[i][j]);
} }
} }
// ensure the copy has no spanner_cell attribute pointing to a cell // ensure the copy has no spanner_cell attribute pointing to a cell
// from the original object // from the original object
for (int j = 0 ; j < rows_heights_.count() ; ++ j) { for (int j = 0 ; j < rows_heights_.count() ; ++ j) {
@ -247,7 +249,7 @@ TitleBlockTemplate *TitleBlockTemplate::clone() const
} }
} }
} }
return(copy); return(copy);
} }
@ -278,15 +280,15 @@ bool TitleBlockTemplate::loadLogos(const QDomElement &xml_element, bool reset) {
if (reset) { if (reset) {
qDeleteAll(vector_logos_.begin(), vector_logos_.end()); qDeleteAll(vector_logos_.begin(), vector_logos_.end());
vector_logos_.clear(); vector_logos_.clear();
// Note: // Note:
// QPixmap are only a key to access the implicitly shared pixmap // QPixmap are only a key to access the implicitly shared pixmap
bitmap_logos_.clear(); bitmap_logos_.clear();
data_logos_.clear(); data_logos_.clear();
storage_logos_.clear(); storage_logos_.clear();
} }
// we look for //logos/logo elements // we look for //logos/logo elements
for (QDomNode n = xml_element.firstChild() ; for (QDomNode n = xml_element.firstChild() ;
!n.isNull() ; !n.isNull() ;
@ -302,7 +304,7 @@ bool TitleBlockTemplate::loadLogos(const QDomElement &xml_element, bool reset) {
} }
} }
} }
return(true); return(true);
} }
@ -321,7 +323,7 @@ bool TitleBlockTemplate::loadLogo(const QDomElement &xml_element) {
QString logo_name = xml_element.attribute("name"); QString logo_name = xml_element.attribute("name");
QString logo_type = xml_element.attribute("type", "png"); QString logo_type = xml_element.attribute("type", "png");
QString logo_storage = xml_element.attribute("storage", "base64"); QString logo_storage = xml_element.attribute("storage", "base64");
// Both QSvgRenderer and QPixmap read their data from a QByteArray, so // Both QSvgRenderer and QPixmap read their data from a QByteArray, so
// we convert the available data to that format. // we convert the available data to that format.
QByteArray logo_data; QByteArray logo_data;
@ -342,7 +344,7 @@ bool TitleBlockTemplate::loadLogo(const QDomElement &xml_element) {
qDebug() << Q_FUNC_INFO << logo_name << logo_type << logo_storage; qDebug() << Q_FUNC_INFO << logo_name << logo_type << logo_storage;
#endif #endif
addLogo(logo_name, &logo_data, logo_type, logo_storage); addLogo(logo_name, &logo_data, logo_type, logo_storage);
return(true); return(true);
} }
@ -362,12 +364,12 @@ bool TitleBlockTemplate::loadGrid(const QDomElement &xml_element) {
break; break;
} }
} }
if (!grid_element.hasAttribute("rows") if (!grid_element.hasAttribute("rows")
|| !grid_element.hasAttribute("cols")) { || !grid_element.hasAttribute("cols")) {
return(false); return(false);
} }
parseRows(grid_element.attribute("rows")); parseRows(grid_element.attribute("rows"));
parseColumns(grid_element.attribute("cols")); parseColumns(grid_element.attribute("cols"));
initCells(); initCells();
@ -508,7 +510,7 @@ void TitleBlockTemplate::saveInformation(QDomElement &xml_element) const
{ {
QDomNode information_text_node = QDomNode information_text_node =
xml_element.ownerDocument().createTextNode(information()); xml_element.ownerDocument().createTextNode(information());
QDomElement information_element = QDomElement information_element =
xml_element.ownerDocument().createElement("information"); xml_element.ownerDocument().createElement("information");
information_element.appendChild(information_text_node); information_element.appendChild(information_text_node);
@ -544,11 +546,11 @@ void TitleBlockTemplate::saveLogo(const QString &logo_name,
QDomElement &xml_element) const QDomElement &xml_element) const
{ {
if (!type_logos_.contains(logo_name)) return; if (!type_logos_.contains(logo_name)) return;
xml_element.setAttribute("name", logo_name); xml_element.setAttribute("name", logo_name);
xml_element.setAttribute("type", type_logos_[logo_name]); xml_element.setAttribute("type", type_logos_[logo_name]);
xml_element.setAttribute("storage", storage_logos_[logo_name]); xml_element.setAttribute("storage", storage_logos_[logo_name]);
if (storage_logos_[logo_name] == "xml" if (storage_logos_[logo_name] == "xml"
&& type_logos_[logo_name] == "svg") { && type_logos_[logo_name] == "svg") {
QDomDocument svg_logo; QDomDocument svg_logo;
@ -575,7 +577,7 @@ void TitleBlockTemplate::saveGrid(QDomElement &xml_element) const
{ {
QDomElement grid_element = QDomElement grid_element =
xml_element.ownerDocument().createElement("grid"); xml_element.ownerDocument().createElement("grid");
QString rows_attr, cols_attr; QString rows_attr, cols_attr;
foreach(int row_height, rows_heights_) foreach(int row_height, rows_heights_)
rows_attr += QString("%1;").arg(row_height); rows_attr += QString("%1;").arg(row_height);
@ -583,9 +585,9 @@ void TitleBlockTemplate::saveGrid(QDomElement &xml_element) const
cols_attr += col_width.toShortString(); cols_attr += col_width.toShortString();
grid_element.setAttribute("rows", rows_attr); grid_element.setAttribute("rows", rows_attr);
grid_element.setAttribute("cols", cols_attr); grid_element.setAttribute("cols", cols_attr);
saveCells(grid_element); saveCells(grid_element);
xml_element.appendChild(grid_element); xml_element.appendChild(grid_element);
} }
@ -625,12 +627,12 @@ void TitleBlockTemplate::saveCell(TitleBlockCell *cell,
if (cell -> spanner_cell) return; if (cell -> spanner_cell) return;
if (!save_empty && cell -> cell_type == TitleBlockCell::EmptyCell) if (!save_empty && cell -> cell_type == TitleBlockCell::EmptyCell)
return; return;
QDomElement cell_elmt = QDomElement cell_elmt =
xml_element.ownerDocument().createElement("cell"); xml_element.ownerDocument().createElement("cell");
xml_element.appendChild(cell_elmt); xml_element.appendChild(cell_elmt);
// save information dependent from this template // save information dependent from this template
cell_elmt.setAttribute("row", cell -> num_row); cell_elmt.setAttribute("row", cell -> num_row);
cell_elmt.setAttribute("col", cell -> num_col); cell_elmt.setAttribute("col", cell -> num_col);
@ -638,7 +640,7 @@ void TitleBlockTemplate::saveCell(TitleBlockCell *cell,
cell -> row_span); cell -> row_span);
if (cell -> col_span) cell_elmt.setAttribute("colspan", if (cell -> col_span) cell_elmt.setAttribute("colspan",
cell -> col_span); cell -> col_span);
// save other information // save other information
cell -> saveContentToXml(cell_elmt); cell -> saveContentToXml(cell_elmt);
} }
@ -659,15 +661,15 @@ bool TitleBlockTemplate::checkCell(const QDomElement &xml_element,
TitleBlockCell **titleblock_cell_ptr) { TitleBlockCell **titleblock_cell_ptr) {
int col_count = columns_width_.count(), int col_count = columns_width_.count(),
row_count = rows_heights_.count(); row_count = rows_heights_.count();
#ifdef TITLEBLOCK_TEMPLATE_DEBUG #ifdef TITLEBLOCK_TEMPLATE_DEBUG
qDebug() << Q_FUNC_INFO << "begin" << row_count << col_count; qDebug() << Q_FUNC_INFO << "begin" << row_count << col_count;
#endif #endif
int row_num, col_num, row_span, col_span; int row_num, col_num, row_span, col_span;
row_num = col_num = -1; row_num = col_num = -1;
row_span = col_span = 0; row_span = col_span = 0;
// parse the row and col attributes // parse the row and col attributes
if (!QET::attributeIsAnInteger(xml_element, "row", &row_num) if (!QET::attributeIsAnInteger(xml_element, "row", &row_num)
|| row_num < 0 || row_num < 0
@ -679,7 +681,7 @@ bool TitleBlockTemplate::checkCell(const QDomElement &xml_element,
|| col_num >= col_count) { || col_num >= col_count) {
return(false); return(false);
} }
// check whether the target cell can be used or not // check whether the target cell can be used or not
#ifdef TITLEBLOCK_TEMPLATE_DEBUG #ifdef TITLEBLOCK_TEMPLATE_DEBUG
qDebug() << Q_FUNC_INFO << "cell access" << col_num << row_num; qDebug() << Q_FUNC_INFO << "cell access" << col_num << row_num;
@ -692,20 +694,20 @@ bool TitleBlockTemplate::checkCell(const QDomElement &xml_element,
// ensure the num_row and num_col attributes are alright // ensure the num_row and num_col attributes are alright
cell_ptr -> num_row = row_num; cell_ptr -> num_row = row_num;
cell_ptr -> num_col = col_num; cell_ptr -> num_col = col_num;
// parse the rowspan and colspan attributes // parse the rowspan and colspan attributes
if (QET::attributeIsAnInteger(xml_element, "rowspan", &row_span) if (QET::attributeIsAnInteger(xml_element, "rowspan", &row_span)
&& row_span > 0) { && row_span > 0) {
cell_ptr -> row_span = row_span; cell_ptr -> row_span = row_span;
} }
if (QET::attributeIsAnInteger(xml_element, "colspan", &col_span) if (QET::attributeIsAnInteger(xml_element, "colspan", &col_span)
&& col_span > 0) { && col_span > 0) {
cell_ptr -> col_span = col_span; cell_ptr -> col_span = col_span;
} }
// these attributes are stored "as is" -- whether they can be applied // these attributes are stored "as is" -- whether they can be applied
// directly or must be restricted will be checked later // directly or must be restricted will be checked later
if (titleblock_cell_ptr) *titleblock_cell_ptr = cell_ptr; if (titleblock_cell_ptr) *titleblock_cell_ptr = cell_ptr;
return(true); return(true);
} }
@ -719,7 +721,7 @@ bool TitleBlockTemplate::checkCell(const QDomElement &xml_element,
void TitleBlockTemplate::initCells() void TitleBlockTemplate::initCells()
{ {
if (columns_width_.count() < 1 || rows_heights_.count() < 1) return; if (columns_width_.count() < 1 || rows_heights_.count() < 1) return;
cells_.clear(); cells_.clear();
qDeleteAll(registered_cells_); qDeleteAll(registered_cells_);
registered_cells_.clear(); registered_cells_.clear();
@ -838,12 +840,12 @@ int TitleBlockTemplate::rowsCount() const
QList<int> TitleBlockTemplate::columnsWidth(int total_width) const QList<int> TitleBlockTemplate::columnsWidth(int total_width) const
{ {
if (total_width < 0) return(QList<int>()); if (total_width < 0) return(QList<int>());
// we first iter to determine the absolute and total-width-related widths // we first iter to determine the absolute and total-width-related widths
QVector<int> final_widths(columns_width_.count()); QVector<int> final_widths(columns_width_.count());
int abs_widths_sum = 0, rel_widths_sum = 0; int abs_widths_sum = 0, rel_widths_sum = 0;
QList<int> relative_columns; QList<int> relative_columns;
for (int i = 0 ; i < columns_width_.count() ; ++ i) { for (int i = 0 ; i < columns_width_.count() ; ++ i) {
TitleBlockDimension icd = columns_width_.at(i); TitleBlockDimension icd = columns_width_.at(i);
if (icd.type == QET::Absolute) { if (icd.type == QET::Absolute) {
@ -856,10 +858,10 @@ QList<int> TitleBlockTemplate::columnsWidth(int total_width) const
final_widths[i] = abs_value; final_widths[i] = abs_value;
} }
} }
// we can now deduce the remaining width // we can now deduce the remaining width
int remaining_width = total_width - abs_widths_sum; int remaining_width = total_width - abs_widths_sum;
// we do a second iteration to build the final widths list // we do a second iteration to build the final widths list
for (int i = 0 ; i < columns_width_.count() ; ++ i) { for (int i = 0 ; i < columns_width_.count() ; ++ i) {
TitleBlockDimension icd = columns_width_.at(i); TitleBlockDimension icd = columns_width_.at(i);
@ -870,14 +872,14 @@ QList<int> TitleBlockTemplate::columnsWidth(int total_width) const
rel_widths_sum += final_widths[i]; rel_widths_sum += final_widths[i];
} }
} }
// Have we computed widths from percentage for relative columns? // Have we computed widths from percentage for relative columns?
if (relative_columns.count()) { if (relative_columns.count()) {
// Due to the rounding process, // Due to the rounding process,
// we may get a slight difference between the // we may get a slight difference between the
// sum of the columns widths and the total width. // sum of the columns widths and the total width.
int difference = total_width - abs_widths_sum - rel_widths_sum; int difference = total_width - abs_widths_sum - rel_widths_sum;
if (difference) { if (difference) {
// We consider we should not attempt to compensate // We consider we should not attempt to compensate
// this difference if it is under // this difference if it is under
@ -886,7 +888,7 @@ QList<int> TitleBlockTemplate::columnsWidth(int total_width) const
// columns can "bring" up to 0.5px of difference). // columns can "bring" up to 0.5px of difference).
qreal max_acceptable_difference = qreal max_acceptable_difference =
relative_columns.count() * 0.5; relative_columns.count() * 0.5;
int share = difference > 0 ? 1 : -1; int share = difference > 0 ? 1 : -1;
if (qAbs(difference) <= max_acceptable_difference) { if (qAbs(difference) <= max_acceptable_difference) {
while (difference) { while (difference) {
@ -914,32 +916,32 @@ QList<int> TitleBlockTemplate::rowsHeights() const
/** /**
@brief TitleBlockTemplate::columnTypeCount @brief TitleBlockTemplate::columnTypeCount
@param type : a column type @param type : a column type
@return the count of \a type columns @return the count of \a type columns
*/ */
int TitleBlockTemplate::columnTypeCount(QET::TitleBlockColumnLength type) { int TitleBlockTemplate::columnTypeCount(QET::TitleBlockColumnLength type) {
int count = 0; int count = 0;
for (int i = 0 ; i < columns_width_.count() ; ++ i) { for (int i = 0 ; i < columns_width_.count() ; ++ i) {
if (columns_width_.at(i).type == type) ++ count; if (columns_width_.at(i).type == type) ++ count;
} }
return(count); return(count);
} }
/** /**
@brief TitleBlockTemplate::columnTypeTotal @brief TitleBlockTemplate::columnTypeTotal
@param type : a column type @param type : a column type
@return the sum of values attached to \a type columns @return the sum of values attached to \a type columns
*/ */
int TitleBlockTemplate::columnTypeTotal(QET::TitleBlockColumnLength type) { int TitleBlockTemplate::columnTypeTotal(QET::TitleBlockColumnLength type) {
int total = 0; int total = 0;
for (int i = 0 ; i < columns_width_.count() ; ++ i) { for (int i = 0 ; i < columns_width_.count() ; ++ i) {
if (columns_width_.at(i).type == type) { if (columns_width_.at(i).type == type) {
total += columns_width_.at(i).value; total += columns_width_.at(i).value;
} }
} }
return(total); return(total);
} }
@ -951,7 +953,7 @@ int TitleBlockTemplate::minimumWidth()
// Abbreviations: ABS: absolute, RTT: relative to total, RTR: // Abbreviations: ABS: absolute, RTT: relative to total, RTR:
// relative to remaining, // relative to remaining,
// TOT: total diagram/TBT width (variable). // TOT: total diagram/TBT width (variable).
// Minimum size may be enforced by ABS and RTT widths: // Minimum size may be enforced by ABS and RTT widths:
// TOT >= ((sum(REL)/100)*TOT)+sum(ABS) // TOT >= ((sum(REL)/100)*TOT)+sum(ABS)
// => (1 - (sum(REL)/100))TOT >= sum(ABS) // => (1 - (sum(REL)/100))TOT >= sum(ABS)
@ -1050,7 +1052,7 @@ bool TitleBlockTemplate::insertRow(int dimension,
const QList<TitleBlockCell *> &row, const QList<TitleBlockCell *> &row,
int i) { int i) {
int index = (i == -1) ? rows_heights_.count() : i; int index = (i == -1) ? rows_heights_.count() : i;
for (int j = 0 ; j < columns_width_.count() ; ++ j) { for (int j = 0 ; j < columns_width_.count() ; ++ j) {
cells_[j].insert(index, row[j]); cells_[j].insert(index, row[j]);
} }
@ -1166,7 +1168,7 @@ TitleBlockCell *TitleBlockTemplate::cell(int row, int col) const
{ {
if (row >= rows_heights_.count()) return(nullptr); if (row >= rows_heights_.count()) return(nullptr);
if (col >= columns_width_.count()) return(nullptr); if (col >= columns_width_.count()) return(nullptr);
return(cells_[col][row]); return(cells_[col][row]);
} }
@ -1198,7 +1200,7 @@ QSet<TitleBlockCell *> TitleBlockTemplate::spannedCells(
? given_cell -> col_span ? given_cell -> col_span
: given_cell -> applied_col_span; : given_cell -> applied_col_span;
if (!final_row_span && !final_col_span) return(set); if (!final_row_span && !final_col_span) return(set);
for (int i = given_cell -> num_col ; for (int i = given_cell -> num_col ;
i <= given_cell -> num_col + final_col_span ; i <= given_cell -> num_col + final_col_span ;
++ i) { ++ i) {
@ -1269,7 +1271,7 @@ bool TitleBlockTemplate::addLogo(const QString &logo_name,
// we are replacing the logo // we are replacing the logo
removeLogo(logo_name); removeLogo(logo_name);
} }
// we can now create our image object from the byte array // we can now create our image object from the byte array
if (logo_type == "svg") { if (logo_type == "svg") {
// SVG format is handled by the QSvgRenderer class // SVG format is handled by the QSvgRenderer class
@ -1278,7 +1280,7 @@ bool TitleBlockTemplate::addLogo(const QString &logo_name,
return(false); return(false);
} }
vector_logos_.insert(logo_name, svg); vector_logos_.insert(logo_name, svg);
// we also memorize the way to store them in the final XML output // we also memorize the way to store them in the final XML output
QString final_logo_storage = logo_storage; QString final_logo_storage = logo_storage;
if (logo_storage != "xml" && logo_storage != "base64") { if (logo_storage != "xml" && logo_storage != "base64") {
@ -1286,7 +1288,7 @@ bool TitleBlockTemplate::addLogo(const QString &logo_name,
} }
storage_logos_.insert(logo_name, logo_storage); storage_logos_.insert(logo_name, logo_storage);
} else { } else {
// bitmap formats are handled by the QPixmap class // bitmap formats are handled by the QPixmap class
QPixmap logo_pixmap; QPixmap logo_pixmap;
logo_pixmap.loadFromData(*logo_data); logo_pixmap.loadFromData(*logo_data);
@ -1294,15 +1296,15 @@ bool TitleBlockTemplate::addLogo(const QString &logo_name,
return(false); return(false);
} }
bitmap_logos_.insert(logo_name, logo_pixmap); bitmap_logos_.insert(logo_name, logo_pixmap);
// bitmap logos can only be stored using a base64 encoding // bitmap logos can only be stored using a base64 encoding
storage_logos_.insert(logo_name, "base64"); storage_logos_.insert(logo_name, "base64");
} }
// we systematically store the raw data // we systematically store the raw data
data_logos_.insert(logo_name, *logo_data); data_logos_.insert(logo_name, *logo_data);
type_logos_.insert(logo_name, logo_type); type_logos_.insert(logo_name, logo_type);
return(true); return(true);
} }
@ -1320,15 +1322,15 @@ bool TitleBlockTemplate::addLogoFromFile(const QString &filepath,
QFileInfo filepath_info(filepath); QFileInfo filepath_info(filepath);
QString filename = name.isEmpty() ? filepath_info.fileName() : name; QString filename = name.isEmpty() ? filepath_info.fileName() : name;
QString filetype = filepath_info.suffix(); QString filetype = filepath_info.suffix();
// we read the provided logo // we read the provided logo
QFile logo_file(filepath); QFile logo_file(filepath);
if (!logo_file.open(QIODevice::ReadOnly)) return(false); if (!logo_file.open(QIODevice::ReadOnly)) return(false);
QByteArray file_content = logo_file.readAll(); QByteArray file_content = logo_file.readAll();
// first, we try to add it as an SVG image // first, we try to add it as an SVG image
if (addLogo(filename, &file_content, "svg", "xml")) return(true); if (addLogo(filename, &file_content, "svg", "xml")) return(true);
// we then try to add it as a bitmap image // we then try to add it as a bitmap image
return addLogo(filename, return addLogo(filename,
&file_content, &file_content,
@ -1349,12 +1351,12 @@ bool TitleBlockTemplate::saveLogoToFile(const QString &logo_name,
if (!data_logos_.contains(logo_name)) { if (!data_logos_.contains(logo_name)) {
return(false); return(false);
} }
QFile target_file(filepath); QFile target_file(filepath);
if (!target_file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { if (!target_file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
return(false); return(false);
} }
target_file.write(data_logos_[logo_name]); target_file.write(data_logos_[logo_name]);
target_file.close(); target_file.close();
return(true); return(true);
@ -1395,7 +1397,7 @@ bool TitleBlockTemplate::renameLogo(const QString &logo_name,
|| data_logos_.contains(new_name)) { || data_logos_.contains(new_name)) {
return(false); return(false);
} }
/// TODO check existing cells using this logo. /// TODO check existing cells using this logo.
if (vector_logos_.contains(logo_name)) { if (vector_logos_.contains(logo_name)) {
vector_logos_.insert(new_name, vector_logos_.take(logo_name)); vector_logos_.insert(new_name, vector_logos_.take(logo_name));
@ -1493,16 +1495,16 @@ void TitleBlockTemplate::render(QPainter &painter,
{ {
QList<int> widths = columnsWidth(titleblock_width); QList<int> widths = columnsWidth(titleblock_width);
int titleblock_height = height(); int titleblock_height = height();
painter.save(); painter.save();
//Setup the QPainter //Setup the QPainter
QPen pen(Qt::black); QPen pen(Qt::black);
pen.setCosmetic(true); pen.setCosmetic(true);
painter.setPen(pen); painter.setPen(pen);
// draw the titleblock border // draw the titleblock border
painter.drawRect(QRect(0, 0, titleblock_width, titleblock_height)); painter.drawRect(QRect(0, 0, titleblock_width, titleblock_height));
// run through each individual cell // run through each individual cell
for (int j = 0 ; j < rows_heights_.count() ; ++ j) { for (int j = 0 ; j < rows_heights_.count() ; ++ j) {
for (int i = 0 ; i < columns_width_.count() ; ++ i) { for (int i = 0 ; i < columns_width_.count() ; ++ i) {
@ -1510,13 +1512,13 @@ void TitleBlockTemplate::render(QPainter &painter,
|| cells_[i][j] -> cell_type || cells_[i][j] -> cell_type
== TitleBlockCell::EmptyCell) == TitleBlockCell::EmptyCell)
continue; continue;
// calculate the border rect of the current cell // calculate the border rect of the current cell
int x = lengthRange(0, cells_[i][j] -> num_col, widths); int x = lengthRange(0, cells_[i][j] -> num_col, widths);
int y = lengthRange(0, int y = lengthRange(0,
cells_[i][j] -> num_row, cells_[i][j] -> num_row,
rows_heights_); rows_heights_);
int row_span = 0, col_span = 0; int row_span = 0, col_span = 0;
if (cells_[i][j] -> span_state if (cells_[i][j] -> span_state
!= TitleBlockCell::Disabled) { != TitleBlockCell::Disabled) {
@ -1530,7 +1532,7 @@ void TitleBlockTemplate::render(QPainter &painter,
cells_[i][j] -> num_row + 1 + row_span, cells_[i][j] -> num_row + 1 + row_span,
rows_heights_); rows_heights_);
QRect cell_rect(x, y, w, h); QRect cell_rect(x, y, w, h);
renderCell(painter, *cells_[i][j], renderCell(painter, *cells_[i][j],
diagram_context, diagram_context,
cell_rect); cell_rect);
@ -1647,7 +1649,7 @@ void TitleBlockTemplate::renderCell(QPainter &painter,
pen.setColor(Qt::black); pen.setColor(Qt::black);
painter.setPen(pen); painter.setPen(pen);
painter.drawRect(cell_rect); painter.drawRect(cell_rect);
painter.save(); painter.save();
// render the inner content of the current cell // render the inner content of the current cell
if (cell.type() == TitleBlockCell::LogoCell) { if (cell.type() == TitleBlockCell::LogoCell) {
@ -1672,7 +1674,7 @@ void TitleBlockTemplate::renderCell(QPainter &painter,
renderTextCell(painter, final_text, cell, cell_rect); renderTextCell(painter, final_text, cell, cell_rect);
} }
painter.restore(); painter.restore();
// draw again the border rect of the current cell, without the brush this time // draw again the border rect of the current cell, without the brush this time
painter.setBrush(Qt::NoBrush); painter.setBrush(Qt::NoBrush);
painter.drawRect(cell_rect); painter.drawRect(cell_rect);
@ -1695,9 +1697,9 @@ QString TitleBlockTemplate::finalTextForCell(
{ {
QString cell_text = cell.value.name(); QString cell_text = cell.value.name();
QString cell_label = cell.label.name(); QString cell_label = cell.label.name();
cell_text = interpreteVariables(cell_text, diagram_context); cell_text = interpreteVariables(cell_text, diagram_context);
if (cell.display_label && !cell.label.isEmpty()) { if (cell.display_label && !cell.label.isEmpty()) {
cell_label = interpreteVariables(cell_label, diagram_context); cell_label = interpreteVariables(cell_label, diagram_context);
cell_text = QString(tr(" %1 : %2", "titleblock content - please let the blank space at the beginning")).arg(cell_label).arg(cell_text); cell_text = QString(tr(" %1 : %2", "titleblock content - please let the blank space at the beginning")).arg(cell_label).arg(cell_text);
@ -1749,7 +1751,7 @@ QStringList TitleBlockTemplate::listOfVariables()
#pragma message("@TODO not works on all cases...") #pragma message("@TODO not works on all cases...")
// TODO: not works on all cases... // TODO: not works on all cases...
list << cells_[i][j] -> value.name().replace("%",""); list << cells_[i][j] -> value.name().replace("%","");
} }
} }
qDebug() << list; qDebug() << list;
return list; return list;
@ -1778,25 +1780,25 @@ void TitleBlockTemplate::renderTextCell(QPainter &painter,
if (text.isEmpty()) return; if (text.isEmpty()) return;
QFont text_font = TitleBlockTemplate::fontForCell(cell); QFont text_font = TitleBlockTemplate::fontForCell(cell);
painter.setFont(text_font); painter.setFont(text_font);
if (cell.hadjust) { if (cell.hadjust) {
QFontMetricsF font_metrics(text_font); QFontMetricsF font_metrics(text_font);
QRectF font_rect = font_metrics.boundingRect( QRectF font_rect = font_metrics.boundingRect(
QRect(-10000, -10000, 10000, 10000), QRect(-10000, -10000, 10000, 10000),
cell.alignment, cell.alignment,
text); text);
if (font_rect.width() > cell_rect.width()) { if (font_rect.width() > cell_rect.width()) {
qreal ratio = qreal(cell_rect.width()) qreal ratio = qreal(cell_rect.width())
/ qreal(font_rect.width()); / qreal(font_rect.width());
painter.save(); painter.save();
painter.translate(cell_rect.topLeft()); painter.translate(cell_rect.topLeft());
qreal vertical_adjustment = qreal vertical_adjustment =
cell_rect.height() * (1 - ratio) / 2.0; cell_rect.height() * (1 - ratio) / 2.0;
painter.translate(0.0, vertical_adjustment); painter.translate(0.0, vertical_adjustment);
painter.scale(ratio, ratio); painter.scale(ratio, ratio);
QRectF new_world_cell_rect(cell_rect); QRectF new_world_cell_rect(cell_rect);
new_world_cell_rect.moveTo(0, 0.0); new_world_cell_rect.moveTo(0, 0.0);
new_world_cell_rect.setWidth(new_world_cell_rect.width() new_world_cell_rect.setWidth(new_world_cell_rect.width()
@ -1804,12 +1806,12 @@ void TitleBlockTemplate::renderTextCell(QPainter &painter,
painter.drawText(new_world_cell_rect, painter.drawText(new_world_cell_rect,
cell.alignment, cell.alignment,
text); text);
painter.restore(); painter.restore();
return; return;
} }
} }
// Still here? Let's draw the text normally // Still here? Let's draw the text normally
painter.drawText(cell_rect, cell.alignment, text); painter.drawText(cell_rect, cell.alignment, text);
} }
@ -1825,14 +1827,15 @@ void TitleBlockTemplate::renderTextCell(QPainter &painter,
@param h @param h
@param color @param color
*/ */
void TitleBlockTemplate::renderTextCellDxf(QString &file_path, void TitleBlockTemplate::renderTextCellDxf(
const QString &text, QString &file_path,
const TitleBlockCell &cell, const QString &text,
qreal x, const TitleBlockCell &cell,
qreal y, qreal x,
qreal w, qreal y,
qreal h, qreal w,
int color) const qreal h,
int color) const
{ {
if (text.isEmpty()) return; if (text.isEmpty()) return;
QFont text_font = TitleBlockTemplate::fontForCell(cell); QFont text_font = TitleBlockTemplate::fontForCell(cell);
@ -1841,61 +1844,73 @@ void TitleBlockTemplate::renderTextCellDxf(QString &file_path,
textHeight = text_font.pixelSize(); textHeight = text_font.pixelSize();
qreal x2 = x + w; qreal x2 = x + w;
qreal y1 = y; qreal y1 = y;
int vAlign = 0; int vAlign = 0;
int hAlign = 0; int hAlign = 0;
x2 = x; // default x2 = x; // default
if ( cell.alignment & Qt::AlignTop ) { if ( cell.alignment & Qt::AlignTop )
vAlign = 3; {
y1 = y + h - (textHeight*Createdxf::yScale / 8); vAlign = 3;
} else if ( cell.alignment & Qt::AlignVCenter ) { y1 = y + h - (textHeight*Createdxf::yScale / 8);
vAlign = 2; }
y1 = y + h/2; else if ( cell.alignment & Qt::AlignVCenter )
} else if ( cell.alignment & Qt::AlignBottom ) { {
y1 = y + (textHeight*Createdxf::yScale / 8); vAlign = 2;
} y1 = y + h/2;
}
else if ( cell.alignment & Qt::AlignBottom )
{
y1 = y + (textHeight*Createdxf::yScale / 8);
}
if ( cell.alignment & Qt::AlignRight ) { if ( cell.alignment & Qt::AlignRight )
{
hAlign = 2; hAlign = 2;
x2 = x + w; x2 = x + w;
} else if ( cell.alignment & Qt::AlignHCenter ) { }
else if ( cell.alignment & Qt::AlignHCenter )
{
hAlign = 1; hAlign = 1;
x2 = x + w/2; x2 = x + w/2;
} else if (cell.alignment & Qt::AlignJustify ) { }
else if (cell.alignment & Qt::AlignJustify )
{
hAlign = 5; hAlign = 5;
vAlign = 0; vAlign = 0;
x2 = x + w; x2 = x + w;
y1 = y + textHeight*Createdxf::yScale / 8; y1 = y + textHeight*Createdxf::yScale / 8;
} }
//painter.setFont(text_font); //painter.setFont(text_font);
qreal ratio = 1.0; qreal ratio = 1.0;
if (cell.hadjust) { if (cell.hadjust)
// Scale font width to fit string in cell width w {
// As DXF font aspect ratio is implementation dependent we add a fudge-factor based on tests with AutoCAD // Scale font width to fit string in cell width w
int len = text.length() * textHeight * Createdxf::xScale * 1.2; // As DXF font aspect ratio is implementation dependent we add a fudge-factor based on tests with AutoCAD
int len = text.length() * textHeight * Createdxf::xScale * 1.2;
if(len > w) if(len > w)
ratio = (w/len); ratio = (w/len);
} }
// x offset value below currently set heuristically based on appearance... // x offset value below currently set heuristically based on appearance...
Createdxf::drawTextAligned(file_path, Createdxf::drawTextAligned(
text, file_path,
x - 2*Createdxf::xScale, text,
y1, x - 2*Createdxf::xScale,
textHeight*Createdxf::yScale, y1,
0, textHeight*Createdxf::yScale,
0, 0,
hAlign, 0,
vAlign, hAlign,
x2, vAlign,
ratio, x2,
color); ratio,
color);
} }
/** /**
@ -1965,11 +1980,11 @@ void TitleBlockTemplate::applyCellSpans()
*/ */
bool TitleBlockTemplate::checkCellSpan(TitleBlockCell *cell) { bool TitleBlockTemplate::checkCellSpan(TitleBlockCell *cell) {
if (!cell) return(false); if (!cell) return(false);
cell -> span_state = TitleBlockCell::Enabled; cell -> span_state = TitleBlockCell::Enabled;
cell -> applied_row_span = cell -> row_span; cell -> applied_row_span = cell -> row_span;
cell -> applied_col_span = cell -> col_span; cell -> applied_col_span = cell -> col_span;
// ensure the cell can span as far as required // ensure the cell can span as far as required
if (cell -> num_col + cell -> col_span >= columnsCount()) { if (cell -> num_col + cell -> col_span >= columnsCount()) {
cell -> applied_col_span = columnsCount() - 1 - cell -> num_col; cell -> applied_col_span = columnsCount() - 1 - cell -> num_col;
@ -1979,7 +1994,7 @@ bool TitleBlockTemplate::checkCellSpan(TitleBlockCell *cell) {
cell -> applied_row_span = rowsCount() - 1 - cell -> num_row; cell -> applied_row_span = rowsCount() - 1 - cell -> num_row;
cell -> span_state = TitleBlockCell::Restricted; cell -> span_state = TitleBlockCell::Restricted;
} }
// ensure cells that will be spanned are either empty or free // ensure cells that will be spanned are either empty or free
for (int i = cell -> num_col ; for (int i = cell -> num_col ;
i <= cell -> num_col + cell -> applied_col_span ; i <= cell -> num_col + cell -> applied_col_span ;
@ -2006,7 +2021,7 @@ bool TitleBlockTemplate::checkCellSpan(TitleBlockCell *cell) {
} }
} }
} }
return(true); return(true);
} }
@ -2019,10 +2034,11 @@ bool TitleBlockTemplate::checkCellSpan(TitleBlockCell *cell) {
@param cell : @param cell :
Potentially spanning cell Potentially spanning cell
*/ */
void TitleBlockTemplate::applyCellSpan(TitleBlockCell *cell) { void TitleBlockTemplate::applyCellSpan(TitleBlockCell *cell)
{
if (!cell || (!cell -> row_span && !cell -> col_span)) return; if (!cell || (!cell -> row_span && !cell -> col_span)) return;
if (cell -> span_state == TitleBlockCell::Disabled) return; if (cell -> span_state == TitleBlockCell::Disabled) return;
// goes through every spanned cell // goes through every spanned cell
for (int i = cell -> num_col ; for (int i = cell -> num_col ;
i <= cell -> num_col + cell -> applied_col_span ; i <= cell -> num_col + cell -> applied_col_span ;
@ -2093,7 +2109,7 @@ int TitleBlockTemplate::lengthRange(
#endif #endif
return(0); return(0);
} }
int length = 0; int length = 0;
for (int i = start ; i < end ; ++i) { for (int i = start ; i < end ; ++i) {
length += lengths_list[i]; length += lengths_list[i];