Skip to content

Commit

Permalink
update line number display in program view dick widget
Browse files Browse the repository at this point in the history
program line delegate now has base line number, one digit pixel width,
and line number area width set up in the constructor; a new function to
calculate the line number area width when the line count changed, which
emits a signal that the program view needs updating when the width
changes; and paints the line number with a gray background in its own
rectangle

in main window, program line delegate and program view needed to be set
up before loading the program so that the program view has a change to
calculate the line number area width

main window now maintains the program line count and can update the
program line delegate when the line count changes; and processes the
signal from the program line delegate when it requests the program view
to be update

the edit box base line number constant was made public to be accessible
when the program line delegate is created
  • Loading branch information
thunder422 committed Mar 13, 2013
1 parent 3b68b11 commit a7f38c9
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 15 deletions.
8 changes: 4 additions & 4 deletions editbox.h
Expand Up @@ -44,6 +44,10 @@ class EditBox : public QPlainTextEdit
int lineNumberWidgetWidth(void);
void lineNumberWidgetPaint(QPaintEvent *event);

enum {
BaseLineNumber = 0 // number of first line
};

protected:
void keyPressEvent(QKeyEvent *event);
void resizeEvent(QResizeEvent *event);
Expand All @@ -63,10 +67,6 @@ private slots:
private:
bool pasteSelection(const QPoint &pos = QPoint());

enum {
BaseLineNumber = 0 // number of first line
};

int m_lineModified; // current line that has been modified
bool m_lineModifiedIsNew; // modified line is a new line flag
QWidget *m_lineNumberWidget; // widget to display line numbers
Expand Down
32 changes: 27 additions & 5 deletions mainwindow.cpp
Expand Up @@ -101,6 +101,18 @@ MainWindow::MainWindow(QWidget *parent) :
m_editBox->addActions(actions);
m_editBox->setContextMenuPolicy(Qt::ActionsContextMenu);

// setup program view and its program line delegate
m_programLineDelegate = new ProgramLineDelegate(EditBox::BaseLineNumber,
ui->programView->fontMetrics(), this);
ui->programView->setItemDelegate(m_programLineDelegate);
ui->programView->setModel(m_programModel);
ui->programView->setFont(m_editBox->font());
m_programLines = 0; // FIXME temporary

// connect update requests to update program view
connect(m_programLineDelegate, SIGNAL(programViewUpdate()),
this, SLOT(programViewUpdate()));

// if a file name was specified on the command line
// then it overrides the restored program
if (!m_commandLine->fileName().isEmpty())
Expand All @@ -123,10 +135,6 @@ MainWindow::MainWindow(QWidget *parent) :
// TODO should an warning message be issued here?
}

m_programLineDelegate = new ProgramLineDelegate(this);
ui->programView->setItemDelegate(m_programLineDelegate);
ui->programView->setModel(m_programModel);
ui->programView->setFont(m_editBox->font());
m_guiActive = true;
}

Expand Down Expand Up @@ -436,7 +444,7 @@ bool MainWindow::programSave(const QString &programPath)
void MainWindow::programChanged(int lineNumber, int linesDeleted,
int linesInserted, QStringList lines)
{
// FIXME for now just echo to the console
// FIXME this is temporary until program model functionality is expanded
int i;
int count = lines.count();
for (i = 0; i < count - linesInserted; i++)
Expand All @@ -457,6 +465,20 @@ void MainWindow::programChanged(int lineNumber, int linesDeleted,
m_programModel->setData(index, lines.at(i++));
}
}
if (m_programModel->rowCount() != m_programLines)
{
m_programLines = m_programModel->rowCount();
m_programLineDelegate->lineNumberWidthUpdate(m_programLines);
}
}


// function to force update of program view

void MainWindow::programViewUpdate(void)
{
// FIXME expanded program model functionality should handle this
ui->programView->update(ui->programView->rect());
}


Expand Down
2 changes: 2 additions & 0 deletions mainwindow.h
Expand Up @@ -75,6 +75,7 @@ private slots:
void on_actionSelectAll_triggered(void);
void programChanged(int lineNumber, int linesDeleted, int linesInserted,
QStringList lines);
void programViewUpdate(void);

private:
bool isOkToContinue(void);
Expand All @@ -98,6 +99,7 @@ private slots:
QString m_curDirectory;
QStringListModel *m_programModel;
ProgramLineDelegate *m_programLineDelegate;
int m_programLines; // FIXME temporary

};

Expand Down
47 changes: 42 additions & 5 deletions programlinedelegate.cpp
Expand Up @@ -26,15 +26,52 @@

#include "programlinedelegate.h"

ProgramLineDelegate::ProgramLineDelegate(QObject *parent) :
QItemDelegate(parent)
ProgramLineDelegate::ProgramLineDelegate(int baseLineNumber,
const QFontMetrics &fontMetrics, QObject *parent) :
QItemDelegate(parent),
m_baseLineNumber(baseLineNumber),
m_digitWidth(fontMetrics.width(QLatin1Char('9'))),
m_lineNumberWidth(0)
{
}


// function to update the width needed for the line number area

void ProgramLineDelegate::lineNumberWidthUpdate(int newLineCount)
{
int digits = 2; // plus 2 * 0.5 spacing on either side
while (newLineCount > 10 - m_baseLineNumber)
{
newLineCount /= 10;
digits++;
}
int width = m_digitWidth * digits;
if (width != m_lineNumberWidth)
{
m_lineNumberWidth = width;
emit programViewUpdate();
}
}


// function that paints one program line to the program view list widget

void ProgramLineDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QString text = QString("%1: %2").arg(index.row())
.arg(index.model()->data(index, Qt::DisplayRole).toString());
painter->drawText(option.rect, text);
// draw line number with gray background
QRect rect = option.rect;
rect.setWidth(m_lineNumberWidth);
painter->fillRect(rect, Qt::lightGray);
rect.setLeft(rect.left() + m_digitWidth / 2);
rect.setWidth(m_lineNumberWidth - m_digitWidth);
QString text = QString("%1").arg(index.row() + m_baseLineNumber);
painter->drawText(rect, Qt::AlignRight, text);

// draw program line text
rect.setLeft(rect.left() + m_lineNumberWidth);
rect.setWidth(option.rect.width() - m_lineNumberWidth);
text = index.model()->data(index, Qt::DisplayRole).toString();
painter->drawText(rect, text);
}
9 changes: 8 additions & 1 deletion programlinedelegate.h
Expand Up @@ -31,14 +31,21 @@ class ProgramLineDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit ProgramLineDelegate(QObject *parent = 0);
explicit ProgramLineDelegate(int baseLineNumber,
const QFontMetrics &fontMetrics, QObject *parent = 0);
void lineNumberWidthUpdate(int newLineCount);
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const;

signals:
void programViewUpdate(void);

public slots:

private:
int m_baseLineNumber; // first line number
int m_digitWidth; // pixel width of one digit
int m_lineNumberWidth; // width of line number area
};

#endif // PROGRAMLINEDELEGATE_H

0 comments on commit a7f38c9

Please sign in to comment.