Closed MVC loop for VariablesView.

This commit is contained in:
Jim Evins
2019-03-16 18:12:17 -04:00
parent c9e26c45fb
commit 6255939f39
11 changed files with 448 additions and 45 deletions
+13 -13
View File
@@ -41,10 +41,10 @@ namespace glabels
///
void EditVariableDialog::setVariable( const model::Variable& variable )
{
typeCombo->setCurrentIndex( variable.type() );
typeCombo->setCurrentIndex( static_cast<int>(variable.type()) );
nameEdit->setText( variable.name() );
valueEdit->setText( variable.value() );
incrementCombo->setCurrentIndex( variable.incrementPolicy() );
incrementCombo->setCurrentIndex( static_cast<int>(variable.increment()) );
stepSizeEdit->setText( variable.stepSize() );
updateControls();
@@ -59,7 +59,7 @@ namespace glabels
return model::Variable( static_cast<model::Variable::Type>(typeCombo->currentIndex()),
nameEdit->text(),
valueEdit->text(),
static_cast<model::Variable::IncrementPolicy>(incrementCombo->currentIndex()),
static_cast<model::Variable::Increment>(incrementCombo->currentIndex()),
stepSizeEdit->text() );
}
@@ -104,24 +104,24 @@ namespace glabels
void EditVariableDialog::updateControls()
{
model::Variable::Type type = static_cast<model::Variable::Type>(typeCombo->currentIndex());
model::Variable::IncrementPolicy incrementPolicy = static_cast<model::Variable::IncrementPolicy>(incrementCombo->currentIndex());
model::Variable::Increment increment = static_cast<model::Variable::Increment>(incrementCombo->currentIndex());
if ( type != model::Variable::TYPE_NUMERIC )
if ( type != model::Variable::Type::NUMERIC )
{
incrementCombo->setCurrentIndex( model::Variable::INCREMENT_NEVER );
incrementCombo->setCurrentIndex( static_cast<int>(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) );
}
+104 -4
View File
@@ -22,9 +22,22 @@
#include "EditVariableDialog.h"
#include <QTableWidgetItem>
#include <QtDebug>
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
+3
View File
@@ -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 );
/////////////////////////////////
+2
View File
@@ -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})
+24
View File
@@ -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
///
+6
View File
@@ -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<ModelObject*>& 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<ModelObject*> mObjectList;
Variables* mVariables;
merge::Merge* mMerge;
};
+61 -4
View File
@@ -29,12 +29,12 @@ namespace glabels
Variable::Variable( Variable::Type type,
const QString& name,
const QString& value,
Variable::IncrementPolicy incrementPolicy,
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";
}
}
}
}
+20 -11
View File
@@ -22,6 +22,7 @@
#define model_Variable_h
#include <QCoreApplication>
#include <QString>
@@ -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,7 +57,7 @@ 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;
@@ -63,16 +66,22 @@ namespace glabels
Type type() const;
QString name() const;
QString value() const;
IncrementPolicy incrementPolicy() 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;
Increment mIncrement;
QString mStepSize;
};
+90
View File
@@ -0,0 +1,90 @@
/* Variables.cpp
*
* Copyright (C) 2013-2016 Jim Evins <evins@snaught.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "Variables.h"
#include <QtDebug>
namespace glabels
{
namespace model
{
///
/// Copy constructor
///
Variables::Variables( const Variables* variables )
: QMap<QString,Variable>(*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
+85
View File
@@ -0,0 +1,85 @@
/* Variables.h
*
* Copyright (C) 2013-2016 Jim Evins <evins@snaught.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef model_Variables_h
#define model_Variables_h
#include "Variable.h"
#include <QMap>
#include <QObject>
#include <QString>
namespace glabels
{
namespace model
{
///
/// Variables Collection
///
class Variables : public QObject, public QMap<QString,Variable>
{
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
+27
View File
@@ -1082,6 +1082,33 @@
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>Variable</name>
<message>
<source>Numeric</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>String</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Never</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Per copy</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Per merge record</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Per page</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>VariablesView</name>
<message>