From f414c6e19f743412aea40611da447f073fabeea8 Mon Sep 17 00:00:00 2001 From: Dominik Wenger Date: Wed, 10 Sep 2008 19:41:18 +0000 Subject: WpsEditor: add linenumbrs WpsEditor: Add linenumbers to the WPS Code, and highlight the error line, if parsing fails. (FS#9362) git-svn-id: svn://svn.rockbox.org/rockbox/trunk@18483 a1c6a512-1295-4272-9138-f99709370657 --- utils/wpseditor/gui/gui.pro | 12 +- utils/wpseditor/gui/src/numberedtextview.cpp | 181 +++++++++++++++++++++++++++ utils/wpseditor/gui/src/numberedtextview.h | 87 +++++++++++++ utils/wpseditor/gui/src/qwpseditorwindow.cpp | 48 ++++--- utils/wpseditor/gui/src/qwpseditorwindow.h | 7 +- utils/wpseditor/gui/ui/mainwindow.ui | 104 ++++----------- 6 files changed, 339 insertions(+), 100 deletions(-) create mode 100644 utils/wpseditor/gui/src/numberedtextview.cpp create mode 100644 utils/wpseditor/gui/src/numberedtextview.h diff --git a/utils/wpseditor/gui/gui.pro b/utils/wpseditor/gui/gui.pro index ada7ea3c98..088ab04119 100644 --- a/utils/wpseditor/gui/gui.pro +++ b/utils/wpseditor/gui/gui.pro @@ -1,7 +1,7 @@ TEMPLATE = app TARGET = DEPENDPATH += . build src ui -INCLUDEPATH += . src/QPropertyEditor ../libwps/src +INCLUDEPATH += . src src/QPropertyEditor ../libwps/src DESTDIR = bin OBJECTS_DIR = build MOC_DIR = build @@ -17,7 +17,9 @@ HEADERS += ../libwps/src/api.h \ src/qwpseditorwindow.h \ src/utils.h \ src/qwpsdrawer.h \ - src/qsyntaxer.h + src/qsyntaxer.h \ + src/numberedtextview.h + FORMS += ui/mainwindow.ui ui/slider.ui SOURCES += src/main.cpp \ src/slider.cpp \ @@ -27,8 +29,10 @@ SOURCES += src/main.cpp \ src/utils.cpp \ src/qwpsdrawer.cpp \ src/qwpsdrawer_static.cpp \ - src/qsyntaxer.cpp -LIBS += -Lbin + src/qsyntaxer.cpp \ + src/numberedtextview.cpp + + LIBS += -Lbin CONFIG(debug, debug|release) { LIBS += -lQPropertyEditord TARGET = wpseditord diff --git a/utils/wpseditor/gui/src/numberedtextview.cpp b/utils/wpseditor/gui/src/numberedtextview.cpp new file mode 100644 index 0000000000..81c4208b59 --- /dev/null +++ b/utils/wpseditor/gui/src/numberedtextview.cpp @@ -0,0 +1,181 @@ +/* This file is part of the KDE libraries + Copyright (C) 2005, 2006 KJSEmbed Authors + See included AUTHORS file. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. + + + -------------------------------------------------------------------------------------- + Imported into the WPS editor and simplified by Dominik Wenger + +*/ + + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "numberedtextview.h" + +NumberBar::NumberBar( QWidget *parent ) + : QWidget( parent ), edit(0), markedLine(-1) +{ + // Make room for 4 digits and the breakpoint icon + setFixedWidth( fontMetrics().width( QString("0000") + 10 + 32 ) ); + markerIcon = QPixmap( "images/marker.png" ); +} + +NumberBar::~NumberBar() +{ +} + + +void NumberBar::markLine( int lineno ) +{ + markedLine = lineno; +} + +void NumberBar::setTextEdit( QTextEdit *edit ) +{ + this->edit = edit; + connect( edit->document()->documentLayout(), SIGNAL( update(const QRectF &) ), + this, SLOT( update() ) ); + connect( edit->verticalScrollBar(), SIGNAL(valueChanged(int) ), + this, SLOT( update() ) ); +} + +void NumberBar::paintEvent( QPaintEvent * ) +{ + QAbstractTextDocumentLayout *layout = edit->document()->documentLayout(); + int contentsY = edit->verticalScrollBar()->value(); + qreal pageBottom = contentsY + edit->viewport()->height(); + const QFontMetrics fm = fontMetrics(); + const int ascent = fontMetrics().ascent() + 1; // height = ascent + descent + 1 + int lineCount = 1; + + QPainter p(this); + + markedRect = QRect(); + + for ( QTextBlock block = edit->document()->begin(); + block.isValid(); block = block.next(), ++lineCount ) + { + + const QRectF boundingRect = layout->blockBoundingRect( block ); + + QPointF position = boundingRect.topLeft(); + if ( position.y() + boundingRect.height() < contentsY ) + continue; + if ( position.y() > pageBottom ) + break; + + const QString txt = QString::number( lineCount ); + p.drawText( width() - fm.width(txt), qRound( position.y() ) - contentsY + ascent, txt ); + + // marker + if ( markedLine == lineCount ) + { + p.drawPixmap( 1, qRound( position.y() ) - contentsY, markerIcon ); + markedRect = QRect( 1, qRound( position.y() ) - contentsY, markerIcon.width(), markerIcon.height() ); + } + } +} + +NumberedTextView::NumberedTextView( QWidget *parent ) + : QFrame( parent ) +{ + setFrameStyle( QFrame::StyledPanel | QFrame::Sunken ); + setLineWidth( 2 ); + + // Setup the main view + view = new QTextEdit( this ); + //view->setFontFamily( "Courier" ); + view->setLineWrapMode( QTextEdit::NoWrap ); + view->setFrameStyle( QFrame::NoFrame ); + + connect( view->document(), SIGNAL(contentsChange(int,int,int)), this, SLOT(textChanged(int,int,int)) ); + + // Setup the line number pane + numbers = new NumberBar( this ); + numbers->setTextEdit( view ); + + // Test + markLine(2); + + //setup layout + box = new QHBoxLayout( this ); + box->setSpacing( 0 ); + box->setMargin( 0 ); + box->addWidget( numbers ); + box->addWidget( view ); +} + +NumberedTextView::~NumberedTextView() +{ +} + +void NumberedTextView::markLine( int lineno ) +{ + markedLine = lineno; + numbers->markLine( lineno ); + textChanged(1,1,1); +} + +void NumberedTextView::scrolltoLine( int lineno ) +{ + int max = view->verticalScrollBar()->maximum(); + int min = view->verticalScrollBar()->minimum(); + int lines = view->document()->blockCount(); + view->verticalScrollBar()->setValue( (max*lineno)/lines+min ); +} + +void NumberedTextView::textChanged( int pos, int removed, int added ) +{ + Q_UNUSED( pos ); + + if ( removed == 0 && added == 0 ) + return; + + QTextBlock block = highlight.block(); + QTextBlockFormat fmt = block.blockFormat(); + QColor bg = view->palette().base().color(); + fmt.setBackground( bg ); + highlight.setBlockFormat( fmt ); + + int lineCount = 1; + for ( QTextBlock block = view->document()->begin(); + block.isValid(); block = block.next(), ++lineCount ) + { + if ( lineCount == markedLine ) + { + fmt = block.blockFormat(); + QColor bg = Qt::red; + fmt.setBackground( bg.light(150) ); + + highlight = QTextCursor( block ); + highlight.movePosition( QTextCursor::EndOfBlock, QTextCursor::KeepAnchor ); + highlight.setBlockFormat( fmt ); + + break; + } + } +} + diff --git a/utils/wpseditor/gui/src/numberedtextview.h b/utils/wpseditor/gui/src/numberedtextview.h new file mode 100644 index 0000000000..2a0d1de068 --- /dev/null +++ b/utils/wpseditor/gui/src/numberedtextview.h @@ -0,0 +1,87 @@ +/* This file is part of the KDE libraries + Copyright (C) 2005, 2006 KJSEmbed Authors + See included AUTHORS file. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. + + -------------------------------------------------------------------------------------- + Imported into the WPS editor and simplified by Dominik Wenger + +*/ + + +#ifndef NUMBERED_TEXT_VIEW_H +#define NUMBERED_TEXT_VIEW_H + +#include +#include +#include + +class QTextEdit; +class QHBoxLayout; + +// Shows the Line numbers +class NumberBar : public QWidget +{ + Q_OBJECT + +public: + NumberBar( QWidget *parent ); + ~NumberBar(); + + void markLine( int lineno ); + + void setTextEdit( QTextEdit *edit ); + void paintEvent( QPaintEvent *ev ); + +private: + QTextEdit *edit; + QPixmap markerIcon; + int markedLine; + QRect markedRect; +}; + +// Shows a QTextEdit with Line numbers +class NumberedTextView : public QFrame +{ + Q_OBJECT + +public: + NumberedTextView( QWidget *parent = 0 ); + ~NumberedTextView(); + + /** Returns the QTextEdit of the main view. */ + QTextEdit *textEdit() const { return view; } + + /* marks the line with a icon */ + void markLine( int lineno ); + + void scrolltoLine( int lineno ); + +private slots: + void textChanged( int pos, int removed, int added ); + +private: + QTextEdit *view; + NumberBar *numbers; + QHBoxLayout *box; + QTextCursor highlight; + int markedLine; +}; + + +#endif // NUMBERED_TEXT_VIEW_H + diff --git a/utils/wpseditor/gui/src/qwpseditorwindow.cpp b/utils/wpseditor/gui/src/qwpseditorwindow.cpp index 29887c38bb..1527fef57a 100644 --- a/utils/wpseditor/gui/src/qwpseditorwindow.cpp +++ b/utils/wpseditor/gui/src/qwpseditorwindow.cpp @@ -47,6 +47,7 @@ const char *playmodeNames[] = { QWpsEditorWindow::QWpsEditorWindow( QWidget * parent, Qt::WFlags f) : QMainWindow(parent, f) { logEdit = 0; + scrollingLine = -1; setupUi(this); drawer = new QWpsDrawer(&wpsState,&trackState, this); QWpsDrawer::api.verbose = 1; @@ -54,7 +55,8 @@ QWpsEditorWindow::QWpsEditorWindow( QWidget * parent, Qt::WFlags f) connectActions(); m_propertyEditor->addObject(&trackState); m_propertyEditor->addObject(&wpsState); - new QSyntaxer(plainWpsEdit->document()); + new QSyntaxer(plainWpsEdit->textEdit()->document()); + plainWpsEdit->markLine(-1); } void QWpsEditorWindow::connectActions() { @@ -65,7 +67,7 @@ void QWpsEditorWindow::connectActions() { connect(actShowGrid, SIGNAL(triggered(bool)), drawer, SLOT(slotShowGrid(bool))); connect(actUpdatePlainWps, SIGNAL(triggered()), SLOT(slotUpdatePlainWps())); - connect(plainWpsEdit->document(),SIGNAL(modificationChanged(bool)),SLOT(slotPlainDocModChanged(bool))); + connect(plainWpsEdit->textEdit()->document(),SIGNAL(modificationChanged(bool)),SLOT(slotPlainDocModChanged(bool))); connect(&wpsState, SIGNAL(stateChanged(wpsstate)), drawer, SLOT(slotWpsStateChanged(wpsstate))); connect(&trackState, SIGNAL(stateChanged(trackstate)), drawer, SLOT(slotTrackStateChanged(trackstate))); @@ -124,17 +126,23 @@ void QWpsEditorWindow::slotOpenWps() { DEBUGF1(tr("File wasn't chosen")); return; } - m_propertyEditor->setEnabled(true); + scrollingLine = -1; drawer->WpsInit(wpsfile); - plainWpsEdit->clear(); - plainWpsEdit->append(drawer->wpsString()); - trackState.setAlbum(trackState.album()); ////updating property editor - actGroupAudios->setEnabled(true); - + plainWpsEdit->textEdit()->clear(); + plainWpsEdit->textEdit()->append(drawer->wpsString()); + postWpsUpdate(); } void QWpsEditorWindow::logMsg(QString s) { logEdit->append(s); + // check for error line: + if (s.contains("ERR: Failed parsing on line ")) { + QRegExp error("\\d+"); + if (error.indexIn(s) != -1) { + scrollingLine = error.cap(0).toInt(); + plainWpsEdit->markLine(scrollingLine); + } + } } void QWpsEditorWindow::slotVerboseLevel() { @@ -146,18 +154,18 @@ void QWpsEditorWindow::slotVerboseLevel() { void QWpsEditorWindow::slotUpdatePlainWps() { DEBUGF1(tr("Updating WPS")); - plainWpsEdit->document()->setModified(false); - drawer->WpsInit(plainWpsEdit->toPlainText(),false); - m_propertyEditor->setEnabled(true); - actGroupAudios->setEnabled(true); - trackState.setAlbum(trackState.album()); //updating property editor + scrollingLine = -1; + drawer->WpsInit(plainWpsEdit->textEdit()->toPlainText(),false); + postWpsUpdate(); } void QWpsEditorWindow::slotPlainDocModChanged(bool changed) { - if (changed) + if (changed) { dockPlainWps->setWindowTitle(tr("PlainWps*")); - else + plainWpsEdit->markLine(-1); + } else { dockPlainWps->setWindowTitle(tr("PlainWps")); + } } void QWpsEditorWindow::slotSetTarget(const QString & target) { if (drawer->setTarget(target)) { @@ -169,5 +177,15 @@ void QWpsEditorWindow::slotSetTarget(const QString & target) { slotUpdatePlainWps(); } +void QWpsEditorWindow::postWpsUpdate() { + m_propertyEditor->setEnabled(true); + actGroupAudios->setEnabled(true); + trackState.setAlbum(trackState.album()); ////updating property editor + plainWpsEdit->markLine(scrollingLine); + plainWpsEdit->textEdit()->document()->setModified(false); + plainWpsEdit->scrolltoLine(scrollingLine); + scrollingLine = -1; +} + diff --git a/utils/wpseditor/gui/src/qwpseditorwindow.h b/utils/wpseditor/gui/src/qwpseditorwindow.h index 59670d8309..0009548be2 100644 --- a/utils/wpseditor/gui/src/qwpseditorwindow.h +++ b/utils/wpseditor/gui/src/qwpseditorwindow.h @@ -35,7 +35,7 @@ class QWpsEditorWindow : public QMainWindow, public Ui::MainWindow { Q_OBJECT - + QWpsState wpsState; QTrackState trackState; QPointer drawer; @@ -47,9 +47,11 @@ class QWpsEditorWindow : public QMainWindow, public Ui::MainWindow { QHash actTargets; QActionGroup *actGroupTargets; QSignalMapper *targetsSignalMapper; - + + int scrollingLine; protected: void connectActions(); + void postWpsUpdate(); public: QWpsEditorWindow( QWidget * parent = 0, Qt::WFlags f = 0 ); void logMsg(QString s); @@ -74,3 +76,4 @@ signals: + diff --git a/utils/wpseditor/gui/ui/mainwindow.ui b/utils/wpseditor/gui/ui/mainwindow.ui index f1186e4e7a..a14eb832ad 100644 --- a/utils/wpseditor/gui/ui/mainwindow.ui +++ b/utils/wpseditor/gui/ui/mainwindow.ui @@ -12,23 +12,14 @@ WPS Editor - - - - 262 - 19 - 340 - 346 - - - + 0 0 882 - 19 + 21 @@ -57,25 +48,8 @@ - - - - 0 - 650 - 882 - 19 - - - + - - - 0 - 371 - 882 - 279 - - 0 @@ -89,25 +63,7 @@ 8 - - - 0 - 20 - 882 - 259 - - - - - - false - - - false - - - @@ -128,18 +84,26 @@ + + + + + 2 + 0 + + + + QFrame::StyledPanel + + + QFrame::Raised + + + - - - 0 - 19 - 256 - 346 - - Property Editor @@ -150,25 +114,9 @@ false - - - 0 - 20 - 256 - 326 - - - - - 608 - 19 - 274 - 346 - - 0 @@ -182,14 +130,6 @@ 2 - - - 0 - 20 - 274 - 326 - - @@ -257,6 +197,12 @@ QTreeView
QPropertyEditorWidget.h
+ + NumberedTextView + QFrame +
numberedtextview.h
+ 1 +
-- cgit v1.2.3