diff --git a/glabels/EditVariableDialog.cpp b/glabels/EditVariableDialog.cpp index ea7d0f3..62de4db 100644 --- a/glabels/EditVariableDialog.cpp +++ b/glabels/EditVariableDialog.cpp @@ -41,10 +41,10 @@ namespace glabels /// void EditVariableDialog::setVariable( const model::Variable& variable ) { - typeCombo->setCurrentIndex( variable.type() ); + typeCombo->setCurrentIndex( static_cast(variable.type()) ); nameEdit->setText( variable.name() ); valueEdit->setText( variable.value() ); - incrementCombo->setCurrentIndex( variable.incrementPolicy() ); + incrementCombo->setCurrentIndex( static_cast(variable.increment()) ); stepSizeEdit->setText( variable.stepSize() ); updateControls(); @@ -59,7 +59,7 @@ namespace glabels return model::Variable( static_cast(typeCombo->currentIndex()), nameEdit->text(), valueEdit->text(), - static_cast(incrementCombo->currentIndex()), + static_cast(incrementCombo->currentIndex()), stepSizeEdit->text() ); } @@ -104,24 +104,24 @@ namespace glabels void EditVariableDialog::updateControls() { model::Variable::Type type = static_cast(typeCombo->currentIndex()); - model::Variable::IncrementPolicy incrementPolicy = static_cast(incrementCombo->currentIndex()); + model::Variable::Increment increment = static_cast(incrementCombo->currentIndex()); - if ( type != model::Variable::TYPE_NUMERIC ) + if ( type != model::Variable::Type::NUMERIC ) { - incrementCombo->setCurrentIndex( model::Variable::INCREMENT_NEVER ); + incrementCombo->setCurrentIndex( static_cast(model::Variable::Increment::NEVER) ); } - if ( incrementPolicy == model::Variable::INCREMENT_NEVER ) + if ( increment == model::Variable::Increment::NEVER ) { stepSizeEdit->setText( "0" ); } - incrementLabel->setEnabled( type == model::Variable::TYPE_NUMERIC ); - incrementCombo->setEnabled( type == model::Variable::TYPE_NUMERIC ); - stepSizeLabel->setEnabled( (type == model::Variable::TYPE_NUMERIC) && - (incrementPolicy != model::Variable::INCREMENT_NEVER) ); - stepSizeEdit->setEnabled( (type == model::Variable::TYPE_NUMERIC) && - (incrementPolicy != model::Variable::INCREMENT_NEVER) ); + incrementLabel->setEnabled( type == model::Variable::Type::NUMERIC ); + incrementCombo->setEnabled( type == model::Variable::Type::NUMERIC ); + stepSizeLabel->setEnabled( (type == model::Variable::Type::NUMERIC) && + (increment != model::Variable::Increment::NEVER) ); + stepSizeEdit->setEnabled( (type == model::Variable::Type::NUMERIC) && + (increment != model::Variable::Increment::NEVER) ); } diff --git a/glabels/VariablesView.cpp b/glabels/VariablesView.cpp index ffe64b8..c737b71 100644 --- a/glabels/VariablesView.cpp +++ b/glabels/VariablesView.cpp @@ -22,9 +22,22 @@ #include "EditVariableDialog.h" +#include #include +namespace +{ + enum ICol { + I_COL_NAME = 0, + I_COL_TYPE = 1, + I_COL_VALUE = 2, + I_COL_INCREMENT = 3, + I_COL_STEP_SIZE = 4 + }; +} + + namespace glabels { @@ -58,8 +71,9 @@ namespace glabels mUndoRedoModel = undoRedoModel; updateControls(); + loadTable(); - //connect( mModel, SIGNAL(variablesChanged()), this, SLOT(onVariablesChanged()) ); + connect( mModel, SIGNAL(variablesChanged()), this, SLOT(onVariablesChanged()) ); } @@ -79,16 +93,17 @@ namespace glabels { EditVariableDialog dialog( this ); - model::Variable v( model::Variable::TYPE_NUMERIC, + model::Variable v( model::Variable::Type::NUMERIC, "x", "0", - model::Variable::INCREMENT_NEVER, + model::Variable::Increment::NEVER, "0" ); dialog.setVariable( v ); if ( dialog.exec() == QDialog::Accepted ) { - qDebug() << "Add OK."; + mModel->variables()->addVariable( dialog.variable() ); + selectVariable( dialog.variable().name() ); } } @@ -98,6 +113,22 @@ namespace glabels /// void VariablesView::onEditButtonClicked() { + int iRow = table->selectedItems()[0]->row(); + QString name = table->item( iRow, I_COL_NAME )->text(); + + if ( mModel->variables()->hasVariable( name ) ) + { + model::Variable v = mModel->variables()->value( name ); + + EditVariableDialog dialog( this ); + dialog.setVariable( v ); + + if ( dialog.exec() == QDialog::Accepted ) + { + mModel->variables()->replaceVariable( name, dialog.variable() ); + selectVariable( dialog.variable().name() ); + } + } } @@ -106,6 +137,20 @@ namespace glabels /// void VariablesView::onDeleteButtonClicked() { + int iRow = table->selectedItems()[0]->row(); + + QString name = table->item( iRow, I_COL_NAME )->text(); + mModel->variables()->deleteVariable( name ); + } + + + /// + /// Variables Changed + /// + void VariablesView::onVariablesChanged() + { + // Reload table from variables + loadTable(); } @@ -121,4 +166,59 @@ namespace glabels } + /// + /// load table from variables + /// + void VariablesView::loadTable() + { + table->clearContents(); + table->setRowCount( mModel->variables()->size() ); + + int iRow = 0; + for( const auto& v : *mModel->variables() ) + { + auto* typeItem = new QTableWidgetItem( model::Variable::typeToI18nString(v.type()) ); + typeItem->setFlags( typeItem->flags() ^ Qt::ItemIsEditable ); + table->setItem( iRow, I_COL_TYPE, typeItem ); + + auto* nameItem = new QTableWidgetItem( v.name() ); + nameItem->setFlags( nameItem->flags() ^ Qt::ItemIsEditable ); + table->setItem( iRow, I_COL_NAME, nameItem ); + + auto* valueItem = new QTableWidgetItem( v.value() ); + valueItem->setFlags( valueItem->flags() ^ Qt::ItemIsEditable ); + table->setItem( iRow, I_COL_VALUE, valueItem ); + + auto* incrementItem = new QTableWidgetItem( model::Variable::incrementToI18nString(v.increment()) ); + incrementItem->setFlags( incrementItem->flags() ^ Qt::ItemIsEditable ); + table->setItem( iRow, I_COL_INCREMENT, incrementItem ); + + auto* stepSizeItem = new QTableWidgetItem( v.stepSize() ); + stepSizeItem->setFlags( stepSizeItem->flags() ^ Qt::ItemIsEditable ); + table->setItem( iRow, I_COL_STEP_SIZE, stepSizeItem ); + + table->showRow( iRow ); + iRow++; + } + } + + + void VariablesView::selectVariable( const QString& name ) + { + int iRow = 0; + for( const auto& v : *mModel->variables() ) + { + if ( v.name() == name ) + { + qDebug() << "Selecting row " << iRow; + table->setCurrentCell( iRow, 0, + (QItemSelectionModel::Select|QItemSelectionModel::Rows) ); + break; + } + + iRow++; + } + } + + } // namespace glabels diff --git a/glabels/VariablesView.h b/glabels/VariablesView.h index f648cd5..93dca3d 100644 --- a/glabels/VariablesView.h +++ b/glabels/VariablesView.h @@ -64,6 +64,7 @@ namespace glabels void onAddButtonClicked(); void onEditButtonClicked(); void onDeleteButtonClicked(); + void onVariablesChanged(); ///////////////////////////////// @@ -71,6 +72,8 @@ namespace glabels ///////////////////////////////// private: void updateControls(); + void loadTable(); + void selectVariable( const QString& name ); ///////////////////////////////// diff --git a/model/CMakeLists.txt b/model/CMakeLists.txt index 100d5bf..fc7db0c 100644 --- a/model/CMakeLists.txt +++ b/model/CMakeLists.txt @@ -57,6 +57,7 @@ set (Model_sources TextNode.cpp Units.cpp Variable.cpp + Variables.cpp Vendor.cpp XmlCategoryParser.cpp XmlLabelCreator.cpp @@ -81,6 +82,7 @@ set (Model_qobject_headers ModelTextObject.h PageRenderer.h Settings.h + Variables.h ) qt5_wrap_cpp (Model_moc_sources ${Model_qobject_headers}) diff --git a/model/Model.cpp b/model/Model.cpp index b0eb83d..896970d 100644 --- a/model/Model.cpp +++ b/model/Model.cpp @@ -57,7 +57,10 @@ namespace glabels Model::Model() : mUntitledInstance(0), mModified(true), mRotate(false) { + mVariables = new Variables(); mMerge = new merge::None(); + + connect( mVariables, SIGNAL(changed()), this, SLOT(onVariablesChanged()) ); } @@ -66,6 +69,7 @@ namespace glabels /// Model::~Model() { + delete mVariables; delete mMerge; } @@ -302,6 +306,15 @@ namespace glabels } + /// + /// Get variables object + /// + Variables* Model::variables() const + { + return mVariables; + } + + /// /// Get merge object /// @@ -451,6 +464,17 @@ namespace glabels } + /// + /// Variables Changed Slot + /// + void Model::onVariablesChanged() + { + setModified(); + emit changed(); + emit variablesChanged(); + } + + /// /// Merge Source Changed Slot /// diff --git a/model/Model.h b/model/Model.h index 992606e..13b9f4c 100644 --- a/model/Model.h +++ b/model/Model.h @@ -24,6 +24,7 @@ #include "Settings.h" #include "Template.h" +#include "Variables.h" #include "merge/Merge.h" #include "merge/Record.h" @@ -76,6 +77,7 @@ namespace glabels void sizeChanged(); void selectionChanged(); void modifiedChanged(); + void variablesChanged(); void mergeChanged(); void mergeSourceChanged(); void mergeSelectionChanged(); @@ -107,6 +109,8 @@ namespace glabels const QList& objectList() const; + Variables* variables() const; + merge::Merge* merge() const; void setMerge( merge::Merge* merge ); @@ -213,6 +217,7 @@ namespace glabels private slots: void onObjectChanged(); void onObjectMoved(); + void onVariablesChanged(); void onMergeSourceChanged(); void onMergeSelectionChanged(); @@ -229,6 +234,7 @@ namespace glabels QList mObjectList; + Variables* mVariables; merge::Merge* mMerge; }; diff --git a/model/Variable.cpp b/model/Variable.cpp index 3eb309d..e4a3432 100644 --- a/model/Variable.cpp +++ b/model/Variable.cpp @@ -26,15 +26,15 @@ namespace glabels namespace model { - Variable::Variable( Variable::Type type, - const QString& name, - const QString& value, - Variable::IncrementPolicy incrementPolicy, - const QString& stepSize ) + Variable::Variable( Variable::Type type, + const QString& name, + const QString& value, + Variable::Increment increment, + const QString& stepSize ) : mType(type), mName(name), mValue(value), - mIncrementPolicy(incrementPolicy), + mIncrement(increment), mStepSize(stepSize) { // empty @@ -59,9 +59,9 @@ namespace glabels } - Variable::IncrementPolicy Variable::incrementPolicy() const + Variable::Increment Variable::increment() const { - return mIncrementPolicy; + return mIncrement; } @@ -70,5 +70,62 @@ namespace glabels return mStepSize; } + + QString Variable::typeToI18nString( Type type ) + { + switch (type) + { + case Type::NUMERIC: + return tr("Numeric"); + case Type::STRING: + return tr("String"); + } + } + + + QString Variable::typeToIdString( Type type ) + { + switch (type) + { + case Type::NUMERIC: + return "numeric"; + case Type::STRING: + return "string"; + } + } + + + QString Variable::incrementToI18nString( Increment increment ) + { + switch (increment) + { + case Increment::NEVER: + return tr("Never"); + case Increment::PER_COPY: + return tr("Per copy"); + case Increment::PER_MERGE_RECORD: + return tr("Per merge record"); + case Increment::PER_PAGE: + return tr("Per page"); + } + } + + + QString Variable::incrementToIdString( Increment increment ) + { + switch (increment) + { + case Increment::NEVER: + return "never"; + case Increment::PER_COPY: + return "per_copy"; + case Increment::PER_MERGE_RECORD: + return "per_merge_record"; + case Increment::PER_PAGE: + return "per_page"; + } + } + + } } diff --git a/model/Variable.h b/model/Variable.h index 333857f..d314642 100644 --- a/model/Variable.h +++ b/model/Variable.h @@ -22,6 +22,7 @@ #define model_Variable_h +#include #include @@ -32,19 +33,21 @@ namespace glabels class Variable { + Q_DECLARE_TR_FUNCTIONS(Variable) + public: - enum Type + enum class Type { - TYPE_NUMERIC, - TYPE_STRING + NUMERIC, + STRING }; - enum IncrementPolicy + enum class Increment { - INCREMENT_NEVER, - INCREMENT_PER_COPY, - INCREMENT_PER_MERGE_RECORD, - INCREMENT_PER_PAGE + NEVER, + PER_COPY, + PER_MERGE_RECORD, + PER_PAGE }; @@ -54,26 +57,32 @@ namespace glabels Variable( Type type, const QString& name, const QString& value, - IncrementPolicy incrementPolicy = INCREMENT_NEVER, + Increment increment = Increment::NEVER, const QString& stepSize = "0" ); virtual ~Variable() = default; - Type type() const; - QString name() const; - QString value() const; - IncrementPolicy incrementPolicy() const; - QString stepSize() const; - + Type type() const; + QString name() const; + QString value() const; + Increment increment() const; + QString stepSize() const; + + + static QString typeToI18nString( Type type ); + static QString typeToIdString( Type type ); + + static QString incrementToI18nString( Increment increment ); + static QString incrementToIdString( Increment increment ); private: - Type mType; - QString mName; - QString mValue; - IncrementPolicy mIncrementPolicy; - QString mStepSize; + Type mType; + QString mName; + QString mValue; + Increment mIncrement; + QString mStepSize; }; diff --git a/model/Variables.cpp b/model/Variables.cpp new file mode 100644 index 0000000..24d414f --- /dev/null +++ b/model/Variables.cpp @@ -0,0 +1,90 @@ +/* Variables.cpp + * + * Copyright (C) 2013-2016 Jim Evins + * + * This file is part of gLabels-qt. + * + * gLabels-qt is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * gLabels-qt 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gLabels-qt. If not, see . + */ + +#include "Variables.h" + +#include + + +namespace glabels +{ + namespace model + { + /// + /// Copy constructor + /// + Variables::Variables( const Variables* variables ) + : QMap(*variables) + { + } + + + /// + /// Clone + /// + Variables* Variables::clone() const + { + return new Variables( this ); + } + + + /// + /// Do we have variable? + /// + bool Variables::hasVariable( const QString& name ) const + { + return contains(name); + } + + + /// + /// Add variable ( will replace if name is the same ) + /// + void Variables::addVariable( const Variable& variable ) + { + insert( variable.name(), variable ); + emit changed(); + } + + + /// + /// Delete variable + /// + void Variables::deleteVariable( const QString& name ) + { + remove( name ); + emit changed(); + } + + + /// + /// Replace variable + /// + void Variables::replaceVariable( const QString& origName, const Variable& variable ) + { + remove( origName ); + insert( variable.name(), variable ); + emit changed(); + } + + + } // namespace model + +} // namespace glabels diff --git a/model/Variables.h b/model/Variables.h new file mode 100644 index 0000000..0c1a020 --- /dev/null +++ b/model/Variables.h @@ -0,0 +1,85 @@ +/* Variables.h + * + * Copyright (C) 2013-2016 Jim Evins + * + * This file is part of gLabels-qt. + * + * gLabels-qt is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * gLabels-qt 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gLabels-qt. If not, see . + */ + +#ifndef model_Variables_h +#define model_Variables_h + + +#include "Variable.h" + +#include +#include +#include + + +namespace glabels +{ + namespace model + { + + /// + /// Variables Collection + /// + class Variables : public QObject, public QMap + { + Q_OBJECT + + ///////////////////////////////// + // Life Cycle + ///////////////////////////////// + public: + Variables() = default; + Variables( const Variables* variables ); + + + ///////////////////////////////// + // Object duplication + ///////////////////////////////// + Variables* clone() const; + + + ///////////////////////////////// + // Methods + ///////////////////////////////// + bool hasVariable( const QString& name ) const; + void addVariable( const Variable& variable ); + void deleteVariable( const QString& name ); + void replaceVariable( const QString& name, const Variable& variable ); + + + ///////////////////////////////// + // Signals + ///////////////////////////////// + signals: + void changed(); + + + ///////////////////////////////// + // Private data + ///////////////////////////////// + private: + + }; + + } +} + + +#endif // model_Variables_h diff --git a/translations/glabels_C.ts b/translations/glabels_C.ts index 844a98f..59305a8 100644 --- a/translations/glabels_C.ts +++ b/translations/glabels_C.ts @@ -1082,6 +1082,33 @@ + + Variable + + Numeric + + + + String + + + + Never + + + + Per copy + + + + Per merge record + + + + Per page + + + VariablesView