Skip to content

Commit

Permalink
connect program model error changes to edit box
Browse files Browse the repository at this point in the history
added start/end indexes of changes to error list with access functions
overloaded functions in error list to capture start and indexes
modified program model to check when error list changes from an update
emit new signal from program model to send error list when changed
added new slot to edit box to update errors when changed (just prints)
connected new program model signal to new edit box slot in main window
  • Loading branch information
thunder422 committed Apr 26, 2013
1 parent 0049ab8 commit 666b15b
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 7 deletions.
16 changes: 16 additions & 0 deletions editbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <QTextBlock>

#include "editbox.h"
#include "errorlist.h"


EditBox::EditBox(QWidget *parent) :
Expand Down Expand Up @@ -381,6 +382,21 @@ void EditBox::captureModifiedLine(int offset)
}


// function to update errors when program error list changes
void EditBox::updateErrors(const ErrorList &errors)
{
int start = errors.changeIndexStart();
int end = errors.changeIndexEnd();
qDebug("Update: [%d-%d]", start, end);
for (int i = 0; i < errors.count(); i++)
{
qDebug("%d%s [%d]: %s", errors.at(i).lineNumber(),
i < start || i > end ? "" : "*", i,
qPrintable(errors.at(i).message()));
}
}


// function to calculate the width needed for the line number area widget

int EditBox::lineNumberWidgetWidth(void)
Expand Down
2 changes: 2 additions & 0 deletions editbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <QTextBlock>

class QEvent;
class ErrorList;


class EditBox : public QPlainTextEdit
Expand Down Expand Up @@ -61,6 +62,7 @@ class EditBox : public QPlainTextEdit
public slots:
void documentChanged(int position, int charsRemoved, int charsAdded);
void cursorMoved(void);
void updateErrors(const ErrorList &errors);

private slots:
void lineNumberWidgetUpdateWidth(void);
Expand Down
58 changes: 58 additions & 0 deletions errorlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,62 @@ int ErrorList::find(int lineNumber) const
}


// overloaded function for inserting an error into the list
void ErrorList::insert(int index, const ErrorItem &value)
{
QList<ErrorItem>::insert(index, value);
setChangeIndex(index);
}


// overloaded function for removing an error from the list
void ErrorList::removeAt(int index)
{
QList<ErrorItem>::removeAt(index);
setChangeIndex(index);
}


// overloaded function for replacing an error in the list
void ErrorList::replace(int index, const ErrorItem &value)
{
QList<ErrorItem>::replace(index, value);
setChangeIndex(index);
}


// function to increment line number of an error in the list
void ErrorList::incrementLineNumber(int index)
{
(*this)[index].incrementLineNumber();
setChangeIndex(index);
}


// function to decrement line number of an error in the list
void ErrorList::decrementLineNumber(int index)
{
(*this)[index].decrementLineNumber();
setChangeIndex(index);
}


// function to the change index if not already set
void ErrorList::setChangeIndex(int index)
{
if (m_changeIndexStart == -1)
{
m_changeIndexStart = m_changeIndexEnd = index;
}
else if (m_changeIndexStart > index)
{
m_changeIndexStart = index;
}
else if (m_changeIndexEnd < index)
{
m_changeIndexEnd = index;
}
}


// end: errorlist.cpp
28 changes: 28 additions & 0 deletions errorlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,34 @@ class ErrorList : public QList<ErrorItem>
public:
ErrorList(void);
int find(int lineNumber) const;
void insert(int index, const ErrorItem &value);
void removeAt(int index);
void replace(int index, const ErrorItem &value);
void incrementLineNumber(int index);
void decrementLineNumber(int index);

void resetChange(void)
{
m_changeIndexStart = m_changeIndexEnd = -1;
}
bool hasChanged(void) const
{
return m_changeIndexStart != -1;
}
int changeIndexStart(void) const
{
return m_changeIndexStart;
}
int changeIndexEnd(void) const
{
return m_changeIndexEnd;
}

private:
void setChangeIndex(int index);

int m_changeIndexStart; // index of first change
int m_changeIndexEnd; // index of last change
};

#endif // ERRORLIST_H
2 changes: 2 additions & 0 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ MainWindow::MainWindow(QWidget *parent) :
m_programModel = new ProgramModel(this);
connect(m_editBox, SIGNAL(linesChanged(int, int, int, QStringList)),
m_programModel, SLOT(update(int,int,int,QStringList)));
connect(m_programModel, SIGNAL(errorListChanged(ErrorList)),
m_editBox, SLOT(updateErrors(ErrorList)));

// setup program line delegate (connect to model line count changes)
m_programLineDelegate = new ProgramLineDelegate(EditBox::BaseLineNumber,
Expand Down
17 changes: 12 additions & 5 deletions programmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ QVariant ProgramModel::data(const QModelIndex &index, int role) const
else // translate error occurred
{
Token *token = rpnList->errorToken();
return QString("%1:%2 %3").arg(token->column())
return QString("ERROR %1:%2 %3").arg(token->column())
.arg(token->length()).arg(rpnList->errorMessage());
}
}
Expand All @@ -86,6 +86,7 @@ void ProgramModel::update(int lineNumber, int linesDeleted, int linesInserted,
int i;
int oldCount = m_lineInfo.count();
int count = lines.count();
m_errors.resetChange();
for (i = 0; i < count - linesInserted; i++)
{
// update changed program lines if they actually changed
Expand Down Expand Up @@ -125,6 +126,12 @@ void ProgramModel::update(int lineNumber, int linesDeleted, int linesInserted,
// emit new line count if changed
emit lineCountChanged(m_lineInfo.count());
}

if (m_errors.hasChanged())
{
// emit error list if changed
emit errorListChanged(m_errors);
}
}


Expand Down Expand Up @@ -189,9 +196,9 @@ void ProgramModel::setError(int lineNumber, LineInfo &lineInfo,
else if (lineInfo.errIndex != -1)
{
// replace current error
m_errors[lineInfo.errIndex] = ErrorItem(ErrorItem::Translator,
m_errors.replace(lineInfo.errIndex, ErrorItem(ErrorItem::Translator,
lineNumber, lineInfo.rpnList->errorToken(),
lineInfo.rpnList->errorMessage());
lineInfo.rpnList->errorMessage()));
}

// find location in error list for line number
Expand Down Expand Up @@ -222,7 +229,7 @@ void ProgramModel::setError(int lineNumber, LineInfo &lineInfo,
if (lineInserted)
{
// adjust error line number for inserted line
m_errors[errIndex].incrementLineNumber();
m_errors.incrementLineNumber(errIndex);
}
}
}
Expand Down Expand Up @@ -267,7 +274,7 @@ void ProgramModel::removeError(int lineNumber, LineInfo &lineInfo,
if (lineDeleted)
{
// adjust error line number for deleted line
m_errors[errIndex].decrementLineNumber();
m_errors.decrementLineNumber(errIndex);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions programmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ProgramModel : public QAbstractListModel

signals:
void lineCountChanged(int newLineCount);
void errorListChanged(const ErrorList &errorList);

public slots:
void update(int lineNumber, int linesDeleted, int linesInserted,
Expand All @@ -63,8 +64,7 @@ public slots:
};
bool updateLine(ModifyMode mode, int lineNumber,
const QString &line = QString());
void setError(int lineNumber,
LineInfo &lineInfo, bool lineInserted);
void setError(int lineNumber, LineInfo &lineInfo, bool lineInserted);
void removeError(int lineNumber, LineInfo &lineInfo, bool lineDeleted);

Translator *m_translator; // program line translator instance
Expand Down

0 comments on commit 666b15b

Please sign in to comment.