open file by click on include line
This commit is contained in:
		
							
								
								
									
										11
									
								
								ChangeLog
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								ChangeLog
									
									
									
									
									
								
							| @@ -1,3 +1,14 @@ | ||||
| 2017-01-31 23:04   | ||||
|  | ||||
| 	* [r99] scripts/wt-mode.el, src/editor.hxx: | ||||
| 	  simple syntax highlighting | ||||
|  | ||||
| 2017-01-31 19:54   | ||||
|  | ||||
| 	* [r98] COPYING, ChangeLog, INSTALL, src/editor.hxx[ADD], | ||||
| 	  src/makefile.am, src/testgui.hxx, src/testgui.ui: | ||||
| 	  added CodeEditor with line numbering from Qt sample code | ||||
|  | ||||
| 2017-01-31 17:32   | ||||
|  | ||||
| 	* [r97] src/commands.hxx, src/webrunner.cxx: | ||||
|   | ||||
| @@ -11,6 +11,8 @@ | ||||
| #include <QRegularExpressionMatch> | ||||
| #include <cassert> | ||||
|  | ||||
| #include <iostream> | ||||
|  | ||||
| class Highlighter: public QSyntaxHighlighter { | ||||
|     Q_OBJECT; | ||||
|   Q_SIGNALS: | ||||
| @@ -29,8 +31,10 @@ class Highlighter: public QSyntaxHighlighter { | ||||
|         QTextCharFormat fmt; | ||||
|         if (QFile(m.captured(1)).exists()) { | ||||
|           fmt.setForeground(Qt::darkGreen); | ||||
|           fmt.setFontWeight(QFont::Bold); | ||||
|         } else { | ||||
|           fmt.setForeground(Qt::darkRed); | ||||
|           fmt.setFontStrikeOut(true); | ||||
|         } | ||||
|         setFormat(m.capturedStart(1), m.capturedLength(1), fmt); | ||||
|         include(m.captured(1)); | ||||
| @@ -47,6 +51,7 @@ class Highlighter: public QSyntaxHighlighter { | ||||
|         Expression(QString s): re(s) {} | ||||
|         Expression(QString s, QTextCharFormat f): re(s), fmt(f) {} | ||||
|         Expression& weight(int w) {fmt.setFontWeight(w); return *this;} | ||||
|         Expression& strike(bool s=true) {fmt.setFontStrikeOut(s); return *this;} | ||||
|         Expression& fg(const QBrush& b) {fmt.setForeground(b); return *this;} | ||||
|         QRegularExpression re; | ||||
|         QTextCharFormat fmt; | ||||
| @@ -70,6 +75,7 @@ class CodeEditor: public QPlainTextEdit { | ||||
|     Q_OBJECT; | ||||
|   Q_SIGNALS: | ||||
|     void include(QString); | ||||
|     void link(QString); | ||||
|   public: | ||||
|     CodeEditor(QWidget *parent = 0): QPlainTextEdit(parent) { | ||||
|       Highlighter *highlighter(new Highlighter(document())); | ||||
| @@ -111,6 +117,23 @@ class CodeEditor: public QPlainTextEdit { | ||||
|       int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits; | ||||
|       return space; | ||||
|     } | ||||
|     void mousePressEvent(QMouseEvent *e) { | ||||
|       clickedAnchor = (e->button() & Qt::LeftButton) | ||||
|         ? document()->findBlock(cursorForPosition(e->pos()).position()).text() | ||||
|         : QString(); | ||||
|       QPlainTextEdit::mousePressEvent(e); | ||||
|     } | ||||
|     void mouseReleaseEvent(QMouseEvent *e) { | ||||
|       if (e->button() & Qt::LeftButton && !clickedAnchor.isEmpty() | ||||
|           && document()->findBlock(cursorForPosition(e->pos()).position()).text() == clickedAnchor) { | ||||
|         static QRegularExpression inc("^ *include +([^ ].*.\\.wt)"); | ||||
|         QRegularExpressionMatch m(inc.match(clickedAnchor)); | ||||
|         if (m.hasMatch() && QFile(m.captured(1)).exists()) { | ||||
|           link(m.captured(1)); | ||||
|         } | ||||
|       } | ||||
|       QPlainTextEdit::mouseReleaseEvent(e); | ||||
|     } | ||||
|   protected: | ||||
|     void resizeEvent(QResizeEvent *e) override { | ||||
|       QPlainTextEdit::resizeEvent(e); | ||||
| @@ -144,6 +167,7 @@ class CodeEditor: public QPlainTextEdit { | ||||
|     } | ||||
|   private: | ||||
|     QWidget *lineNumberArea; | ||||
|     QString clickedAnchor; | ||||
| }; | ||||
|  | ||||
| inline LineNumberArea::LineNumberArea(CodeEditor *editor): QWidget(editor) { | ||||
|   | ||||
| @@ -7,12 +7,14 @@ | ||||
| class ScriptFile: public QDockWidget, protected Ui::ScriptFile { | ||||
|     Q_OBJECT; | ||||
|   Q_SIGNALS: | ||||
|     void link(QString); | ||||
|     void include(QString); | ||||
|     void close(ScriptFile*); | ||||
|   public: | ||||
|     ScriptFile(QWidget* p=0): QDockWidget(p) { | ||||
|       setupUi(this); | ||||
|       assert(connect(_editor, SIGNAL(include(QString)), SIGNAL(include(QString)))); | ||||
|       assert(connect(_editor, SIGNAL(link(QString)), SIGNAL(link(QString)))); | ||||
|       _searchBar->hide(); | ||||
|       _replaceBar->hide(); | ||||
|       _pageBar->hide(); | ||||
|   | ||||
| @@ -69,7 +69,8 @@ class TestGUI: public QMainWindow, protected Ui::TestGUI { | ||||
|                      SLOT(unsupportedContent(QNetworkReply*)))); | ||||
|       assert(connect(page, SIGNAL(downloadRequested(const QNetworkRequest&)), | ||||
|                      SLOT(downloadRequested(const QNetworkRequest&)))); | ||||
|       assert(connect(_testscript, SIGNAL(include(QString)), SLOT(include(QString)))); | ||||
|       //assert(connect(_testscript, SIGNAL(include(QString)), SLOT(include(QString)))); | ||||
|       assert(connect(_testscript, SIGNAL(link(QString)), SLOT(include(QString)))); | ||||
|       if (setupScript.size()) loadSetup(setupScript); | ||||
|       if (scriptFile.size()) loadFile(scriptFile); | ||||
|     } | ||||
| @@ -343,7 +344,8 @@ class TestGUI: public QMainWindow, protected Ui::TestGUI { | ||||
|     void include(QString name) { | ||||
|       if (_testscripts.contains(name)) return; | ||||
|       _testscripts[name] = new ScriptFile(this); | ||||
|       assert(connect(_testscripts[name], SIGNAL(include(QString)), SLOT(include(QString)))); | ||||
|       // assert(connect(_testscripts[name], SIGNAL(include(QString)), SLOT(include(QString)))); | ||||
|       assert(connect(_testscripts[name], SIGNAL(link(QString)), SLOT(include(QString)))); | ||||
|       assert(connect(_testscripts[name], SIGNAL(close(ScriptFile*)), SLOT(remove(ScriptFile*)))); | ||||
|       QFile file(name); | ||||
|       try { | ||||
| @@ -354,10 +356,11 @@ class TestGUI: public QMainWindow, protected Ui::TestGUI { | ||||
|           throw std::runtime_error("file read failed"); | ||||
|         _testscripts[name]->name(name); | ||||
|         tabifyDockWidget(_scriptDock, _testscripts[name]); | ||||
|         QDockWidget* d(0); | ||||
|         for (QWidget* w(QApplication::focusWidget()); w&&!(d=qobject_cast<QDockWidget*>(w)); | ||||
|              w=qobject_cast<QWidget*>(w->parent())); | ||||
|         if (d) d->raise(); | ||||
|         // QDockWidget* d(0); | ||||
|         // for (QWidget* w(QApplication::focusWidget()); w&&!(d=qobject_cast<QDockWidget*>(w)); | ||||
|         //      w=qobject_cast<QWidget*>(w->parent())); | ||||
|         // if (d) d->raise(); | ||||
|         _testscripts[name]->raise(); | ||||
|       } catch(const std::exception& x) { | ||||
|         remove(_testscripts[name]); | ||||
|       } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user