Skip to content

Commit

Permalink
added actions to move cursor to next/previous error
Browse files Browse the repository at this point in the history
added actions with new buttons to the toolbars and key commands
modified error list column and length to check for alternate column
changed edit box column to return actual column (don't add 1)
implemented edit box functions to go to next/previous error
  • Loading branch information
thunder422 committed May 18, 2013
1 parent c38cd02 commit f086ce5
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 10 deletions.
83 changes: 77 additions & 6 deletions editbox.cpp
Expand Up @@ -26,6 +26,7 @@
#include <QClipboard>
#include <QEvent>
#include <QKeyEvent>
#include <QMessageBox>
#include <QPainter>
#include <QTextBlock>

Expand Down Expand Up @@ -192,6 +193,81 @@ void EditBox::selectAll(void)
}


// function to move cursor to the next error

void EditBox::goNextError(void)
{
int errIndex = m_errors.find(lineNumber());
if (errIndex >= m_errors.count()
|| lineNumber() > m_errors.at(errIndex).lineNumber()
|| lineNumber() == m_errors.at(errIndex).lineNumber()
&& column() >= m_errors.at(errIndex).column())
{
// past current error, go to next error
errIndex++;
}
if (errIndex >= m_errors.count()) // past last error?
{
errIndex = 0;
if (m_errors.count() == 1 && lineNumber() == m_errors.at(0).lineNumber()
&& column() >= m_errors.at(0).column()
&& column() < m_errors.at(0).column() + m_errors.at(0).length()
|| QMessageBox::question(this, "Next Error",
"No more errors in program,\nmove to first error?",
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes)
!= QMessageBox::Yes)
{
return;
}
}
moveCursorToError(errIndex);
}


// function to move cursor to the previous error

void EditBox::goPrevError(void)
{
int errIndex = m_errors.find(lineNumber());
if (errIndex >= m_errors.count()
|| lineNumber() < m_errors.at(errIndex).lineNumber()
|| lineNumber() == m_errors.at(errIndex).lineNumber()
&& column() < m_errors.at(errIndex).column()
+ m_errors.at(errIndex).length())
{
// before current error, go to previous error
errIndex--;
}
if (errIndex < 0)
{
errIndex = m_errors.count() - 1;
if (m_errors.count() == 1
&& lineNumber() == m_errors.at(errIndex).lineNumber()
&& column() == m_errors.at(errIndex).column()
|| QMessageBox::question(this, "Previous Error",
"No more errors in program,\nmove to last error?",
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes)
!= QMessageBox::Yes)
{
return;
}
}
moveCursorToError(errIndex);
}


// function to move cursor to an error

void EditBox::moveCursorToError(int errIndex)
{
ErrorItem error = m_errors.at(errIndex);
QTextBlock block = document()->findBlockByLineNumber(error.lineNumber());
QTextCursor cursor = textCursor();
cursor.setPosition(block.position() + error.column());
setTextCursor(cursor);
}


// function to reset the document modified flag and other variables

void EditBox::resetModified(void)
Expand Down Expand Up @@ -472,12 +548,7 @@ const QTextEdit::ExtraSelection

int column = errorItem.column();
int length = errorItem.length();
if (length < 0) // alternate column?
{
column = -length;
length = 1;
}
lineColor = QColor(Qt::red).lighter(80);
lineColor = QColor(Qt::red).lighter(150);
selection.format.setBackground(lineColor);

block = document()->findBlockByLineNumber(errorItem.lineNumber());
Expand Down
5 changes: 4 additions & 1 deletion editbox.h
Expand Up @@ -42,6 +42,8 @@ class EditBox : public QPlainTextEdit
void remove(void);
void paste(QClipboard::Mode mode = QClipboard::Clipboard);
void selectAll(void);
void goNextError(void);
void goPrevError(void);
void resetModified(void);
void captureModifiedLine(int offset = 0);
int lineNumber(void) const
Expand All @@ -50,7 +52,7 @@ class EditBox : public QPlainTextEdit
}
int column(void) const
{
return textCursor().positionInBlock() + 1;
return textCursor().positionInBlock();
}
const QString message(void) const;

Expand Down Expand Up @@ -82,6 +84,7 @@ private slots:

private:
bool pasteSelection(const QPoint &pos = QPoint());
void moveCursorToError(int errIndex);
const QTextEdit::ExtraSelection extraSelection(const ErrorItem &errorItem);

int m_modifiedLine; // current line that has been modified
Expand Down
4 changes: 2 additions & 2 deletions errorlist.h
Expand Up @@ -65,11 +65,11 @@ class ErrorItem
}
int column(void) const
{
return m_column;
return m_length >= 0 ? m_column : -m_length;
}
int length(void) const
{
return m_length;
return m_length >= 0 ? m_length : 1;
}
QString message(void) const
{
Expand Down
2 changes: 2 additions & 0 deletions ibcp.qrc
Expand Up @@ -15,6 +15,8 @@
<file>images/edit-paste.png</file>
<file>images/edit-delete.png</file>
<file>images/edit-select-all.png</file>
<file>images/go-next-error.png</file>
<file>images/go-prev-error.png</file>
<file>images/help-about.png</file>
<file>images/help-about-qt.png</file>
</qresource>
Expand Down
Binary file added images/go-next-error.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/go-prev-error.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 17 additions & 1 deletion mainwindow.cpp
Expand Up @@ -207,7 +207,7 @@ void MainWindow::statusBarCreate(void)
void MainWindow::statusBarUpdate(void)
{
m_statusPositionLabel->setText(QString(" %1:%2 ")
.arg(m_editBox->lineNumber()).arg(m_editBox->column()));
.arg(m_editBox->lineNumber()).arg(m_editBox->column() + 1));
m_statusMessageLabel->setText(m_editBox->message());
}

Expand Down Expand Up @@ -353,6 +353,22 @@ void MainWindow::on_actionSelectAll_triggered(void)
}


// function called when move to next error has been requested

void MainWindow::on_actionGoNextError_triggered(void)
{
m_editBox->goNextError();
}


// function called when move to previous has been requested

void MainWindow::on_actionGoPrevError_triggered(void)
{
m_editBox->goPrevError();
}


// function called when help about has been requested

void MainWindow::on_actionAbout_triggered(void)
Expand Down
2 changes: 2 additions & 0 deletions mainwindow.h
Expand Up @@ -75,6 +75,8 @@ private slots:
void on_actionPaste_triggered(void);
void on_actionDelete_triggered(void);
void on_actionSelectAll_triggered(void);
void on_actionGoNextError_triggered(void);
void on_actionGoPrevError_triggered(void);

private:
void statusBarCreate(void);
Expand Down
36 changes: 36 additions & 0 deletions mainwindow.ui
Expand Up @@ -75,6 +75,9 @@
<addaction name="actionDelete"/>
<addaction name="separator"/>
<addaction name="actionSelectAll"/>
<addaction name="separator"/>
<addaction name="actionGoNextError"/>
<addaction name="actionGoPrevError"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuEdit"/>
Expand All @@ -101,6 +104,9 @@
<addaction name="separator"/>
<addaction name="actionUndo"/>
<addaction name="actionRedo"/>
<addaction name="separator"/>
<addaction name="actionGoPrevError"/>
<addaction name="actionGoNextError"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<widget class="QDockWidget" name="programViewDockWidget">
Expand Down Expand Up @@ -374,6 +380,36 @@
<string>Ctrl+A</string>
</property>
</action>
<action name="actionGoNextError">
<property name="icon">
<iconset resource="ibcp.qrc">
<normaloff>:/images/go-next-error.png</normaloff>:/images/go-next-error.png</iconset>
</property>
<property name="text">
<string>Go to Next Error</string>
</property>
<property name="toolTip">
<string>Move cursor to next error</string>
</property>
<property name="shortcut">
<string>Ctrl+.</string>
</property>
</action>
<action name="actionGoPrevError">
<property name="icon">
<iconset resource="ibcp.qrc">
<normaloff>:/images/go-prev-error.png</normaloff>:/images/go-prev-error.png</iconset>
</property>
<property name="text">
<string>Go to Previous Error</string>
</property>
<property name="toolTip">
<string>Move cursor to previous error</string>
</property>
<property name="shortcut">
<string>Ctrl+,</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>
Expand Down

0 comments on commit f086ce5

Please sign in to comment.