Skip to content

Commit

Permalink
changed program model to store translated rpn lists of program lines
Browse files Browse the repository at this point in the history
added translator instance pointer member to program model class
added list of translated RPN lists member to program model class
translator instance created in constructor and deleted in destructor
modified program model to use new translated lines member
update slot updated to translate new lines and store RPN lists
(for now program lines string list member remains for change detection)
  • Loading branch information
thunder422 committed Apr 1, 2013
1 parent 27cd073 commit c77bdb9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
49 changes: 42 additions & 7 deletions programmodel.cpp
Expand Up @@ -23,13 +23,26 @@
// 2013-03-15 initial version

#include "programmodel.h"
#include "rpnlist.h"
#include "table.h"
#include "translator.h"


ProgramModel::ProgramModel(QObject *parent) :
QAbstractListModel(parent)
QAbstractListModel(parent),
m_translator(new Translator(Table::instance()))
{
}


ProgramModel::~ProgramModel(void)
{
delete m_translator;
}


// function to return data for a given program line

QVariant ProgramModel::data(const QModelIndex &index, int role) const
{
if (index.isValid())
Expand All @@ -40,7 +53,17 @@ QVariant ProgramModel::data(const QModelIndex &index, int role) const
}
else if (role == Qt::DisplayRole)
{
return m_lines.at(index.row());
RpnList *rpnList = m_linesTranslated.at(index.row());
if (!rpnList->hasError())
{
return rpnList->text();
}
else // translate error occurred
{
Token *token = rpnList->errorToken();
return QString("%1:%2 %3").arg(token->column())
.arg(token->length()).arg(rpnList->errorMessage());
}
}
}
return QVariant();
Expand All @@ -51,7 +74,7 @@ QVariant ProgramModel::data(const QModelIndex &index, int role) const

int ProgramModel::rowCount(const QModelIndex &parent) const
{
return m_lines.count();
return m_linesTranslated.count();
}


Expand All @@ -61,15 +84,21 @@ void ProgramModel::update(int lineNumber, int linesDeleted, int linesInserted,
QStringList lines)
{
int i;
int oldCount = m_lines.count();
int oldCount = m_linesTranslated.count();
int count = lines.count();
for (i = 0; i < count - linesInserted; i++)
{
// update changed program lines if they actually changed
// TODO will need RPN list comparison here
if (lines.at(i) != m_lines.at(lineNumber))
{
m_lines[lineNumber] = lines.at(i);

// delete old list, translate line and store new list
delete m_linesTranslated[lineNumber];
m_linesTranslated[lineNumber]
= m_translator->translate(lines.at(i));

// need to emit signal that data changed
QModelIndex index = this->index(lineNumber);
emit dataChanged(index, index);
Expand All @@ -84,6 +113,9 @@ void ProgramModel::update(int lineNumber, int linesDeleted, int linesInserted,
while (--linesDeleted >= 0)
{
m_lines.removeAt(lineNumber);
// delete rpn list and remove from list
delete m_linesTranslated[lineNumber];
m_linesTranslated.removeAt(lineNumber);
}
endRemoveRows();
}
Expand All @@ -94,14 +126,17 @@ void ProgramModel::update(int lineNumber, int linesDeleted, int linesInserted,
- 1);
while (i < count)
{
m_lines.insert(lineNumber++, lines.at(i++));
m_lines.insert(lineNumber, lines.at(i));
// translate new line and insert into list
m_linesTranslated.insert(lineNumber++,
m_translator->translate(lines.at(i++)));
}
endInsertRows();
}

if (m_lines.count() != oldCount)
if (m_linesTranslated.count() != oldCount)
{
// emit new line count if changed
emit lineCountChanged(m_lines.count());
emit lineCountChanged(m_linesTranslated.count());
}
}
6 changes: 6 additions & 0 deletions programmodel.h
Expand Up @@ -28,11 +28,15 @@
#include <QAbstractListModel>
#include <QStringList>

class RpnList;
class Translator;

class ProgramModel : public QAbstractListModel
{
Q_OBJECT
public:
explicit ProgramModel(QObject *parent = 0);
~ProgramModel(void);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;

Expand All @@ -45,6 +49,8 @@ public slots:

private:
QStringList m_lines; // text of the program lines
Translator *m_translator; // program line translator instance
QList<RpnList *>m_linesTranslated; // rpn lists of the program lines
};

#endif // PROGRAMMODEL_H

0 comments on commit c77bdb9

Please sign in to comment.