From ca2895b80c0349d432ad564bacb9f686c6097d38 Mon Sep 17 00:00:00 2001 From: "Nox P@sNox" Date: Tue, 31 Jul 2012 20:28:04 +0200 Subject: [PATCH] Update UIMain example. --- src/example/UIMain.cpp | 105 ++++++++++++++++++++++++++++++++++++++++- src/example/UIMain.h | 11 +++++ src/example/UIMain.ui | 34 ++++++------- 3 files changed, 130 insertions(+), 20 deletions(-) diff --git a/src/example/UIMain.cpp b/src/example/UIMain.cpp index cd237d1..47fd148 100644 --- a/src/example/UIMain.cpp +++ b/src/example/UIMain.cpp @@ -3,12 +3,17 @@ #include "CodeEditor.h" #include "MarginStacker.h" #include "SyntaxFactory.h" +#include "QodeEdit.h" +#include "SyntaxHighlighter.h" +#include "TextDocument.h" // QodeEditor QodeEditor::QodeEditor( QWidget* parent ) : CodeEditor( parent ) { + setCaretLineBackground( QColor( 150, 150, 150, 150 ) ); + MarginStacker* margins = new MarginStacker( this ); margins->setVisible( MarginStacker::LineBookmark, true ); margins->setVisible( MarginStacker::LineNumber, true ); @@ -20,6 +25,27 @@ QodeEditor::QodeEditor( QWidget* parent ) new QShortcut( QKeySequence::Save, this, SLOT( save() ) ); } +QString QodeEditor::fileContent( const QString& filePath, const QByteArray& textCodec ) +{ + QFile file( filePath ); + + if ( !file.exists() ) { + return QString::null; + } + + if ( !file.open( QIODevice::ReadOnly ) ) { + return QString::null; + } + + QTextCodec* codec = QTextCodec::codecForName( textCodec ); + + if ( !codec ) { + codec = QTextCodec::codecForLocale(); + } + + return codec->toUnicode( file.readAll() ); +} + void QodeEditor::save() { document()->setModified( false ); @@ -51,7 +77,56 @@ UIMain::UIMain( QWidget* parent ) error = "Syntaxes loaded correctly"; } - ui->qeEdit->setText( error ); + //qWarning() << Syntax::Factory::availableSyntaxes(); + + /*".desktop", "4GL", "4GL-PER", "ABAP", "ABC", "ActionScript 2.0", "Ada", -- + "AHDL", "Alerts", "Alerts_indent", "AMPLE", "ANS-Forth94", "ANSI C89", "Ansys", + "Apache Configuration", "Asm6502", "ASN.1", "ASP", "Asterisk", "AVR Assembler", + "AWK", "B-Method", "Bash", "BibTeX", "Boo", "C", "C#", "C++", "Cg", "CGiS", "ChangeLog", + "Cisco", "Clipper", "Clojure", "CMake", "CoffeeScript", "ColdFusion", "Common Lisp", + "Component-Pascal", "Crack", "CSS", "CUE Sheet", "D", "Debian Changelog", "Debian Control", + "Diff", "Django HTML Template", "dot", "Doxygen", "DoxygenLua", "DTD", "E Language", "Eiffel", + "Email", "Erlang", "Euphoria", "ferite", "Fortran", "FreeBASIC", "FSharp", "fstab", "GAP", + "GDB Backtrace", "GDL", "GlossTex", "GLSL", "GNU Assembler", "GNU Gettext", "GNU Linker Script", + "GNU M4", "Go", "Haskell", "Haxe", "HTML", "IDL", "ILERPG", "Inform", "INI Files", "Intel x86 (NASM)", + "Jam", "Java", "Javadoc", "JavaScript", "JSON", "JSP", "KBasic", "KDev-PG[-Qt] Grammar", "LaTeX", + "LDIF", "Lex/Flex", "LilyPond", "Literate Haskell", "Logtalk", "LPC", "LSL", "Lua", "M3U", "MAB-DB", + "Makefile", "Mason", "Matlab", "Maxima", "MediaWiki", "MEL", "mergetag text", "Metapost/Metafont", + "MIPS Assembler", "Modelica", "Modelines", "Modula-2", "MonoBasic", "Motorola 68k (VASM/Devpac)", + "Motorola DSP56k", "MS-DOS Batch", "Music Publisher", "Nemerle", "noweb", "Objective Caml", + "Objective-C", "Objective-C++", "Octave", "OORS", "OPAL", "Pango", "Pascal", "Perl", "PGN", + "PHP/PHP", "PicAsm", "Pig", "Pike", "PostScript", "POV-Ray", "progress", "Prolog", "PureBasic", + "Python", "QMake", "QML", "Quake Script", "R Script", "RapidQ", "RELAX NG", "RelaxNG-Compact", + "RenderMan RIB", "reStructuredText", "REXX", "Roff", "RPM Spec", "RSI IDL", "Ruby", "Ruby/Rails/RHTML", + "Sather", "Scala", "Scheme", "scilab", "SCSS", "sed", "SGML", "Sieve", "SiSU", "SML", "Spice", "SQL", + "SQL (MySQL)", "SQL (PostgreSQL)", "Stata", "SystemC", "SystemVerilog", "TADS 3", "Tcl/Tk", "Tcsh", + "Texinfo", "TI Basic", "Troff Mandoc", "txt2tags", "UnrealScript", "Valgrind Suppression", "Velocity", + "Vera", "Verilog", "VHDL", "VRML", "Wesnoth Markup Language", "WINE Config", "x.org Configuration", + "xHarbour", "XML", "XML (Debug)", "xslt", "XUL", "yacas", "Yacc/Bison", "YAML", "Zonnon", "Zsh"*/ + + QDir dir( QodeEdit::sharedDataFilePath( "/samples" ) ); + const QFileInfoList files = dir.entryInfoList( QDir::Files | QDir::NoDotAndDotDot ); + + foreach ( const QFileInfo& file, files ) { + const QString filePath = file.absoluteFilePath(); + Syntax::Highlighter* highlighter = Syntax::Factory::highlighterForFilePath( filePath ); + + if ( highlighter ) { + QodeEditor* editor = new QodeEditor( this ); + editor->setInitialText( QodeEditor::fileContent( filePath ) ); + editor->textDocument()->setSyntaxHighlighter( highlighter ); + + QListWidgetItem* item = new QListWidgetItem( ui->lwEditors ); + item->setText( highlighter->syntaxDocument().name ); + item->setData( Qt::UserRole, QVariant::fromValue( editor ) ); + ui->swEditors->addWidget( editor ); + } + else { + qWarning( "%s: Can't create highlighter for '%s'", Q_FUNC_INFO, qPrintable( filePath ) ); + } + } + + statusBar()->showMessage( error ); } UIMain::~UIMain() @@ -59,3 +134,31 @@ UIMain::~UIMain() delete ui; Syntax::Factory::free(); } + +QodeEditor* UIMain::editor( int row ) const +{ + QListWidgetItem* item = ui->lwEditors->item( row ); + + if ( !item ) { + Q_ASSERT( item ); + return 0; + } + + return item->data( Qt::UserRole ).value(); +} + +void UIMain::on_lwEditors_currentRowChanged( int row ) +{ + if ( ui->swEditors->currentIndex() != row ) { + ui->swEditors->setCurrentIndex( row ); + } +} + +void UIMain::on_swEditors_currentChanged( int row ) +{ + if ( ui->lwEditors->currentRow() != row ) { + ui->lwEditors->setCurrentRow( row ); + } + + ui->cbSyntax->setCurrentSyntax( editor( row )->textDocument()->syntaxHighlighter()->syntaxDocument().name ); +} diff --git a/src/example/UIMain.h b/src/example/UIMain.h index cf890f0..4ea79df 100644 --- a/src/example/UIMain.h +++ b/src/example/UIMain.h @@ -13,11 +13,15 @@ class QodeEditor : public CodeEditor public: QodeEditor( QWidget* parent = 0 ); + + static QString fileContent( const QString& filePath, const QByteArray& textCodec = QByteArray( "UTF-8" ) ); protected slots: void save(); }; +Q_DECLARE_METATYPE( QodeEditor* ) + class UIMain : public QMainWindow { Q_OBJECT @@ -28,6 +32,13 @@ class UIMain : public QMainWindow protected: Ui_UIMain* ui; + QHash mEditors; + + QodeEditor* editor( int row ) const; + +protected slots: + void on_lwEditors_currentRowChanged( int row ); + void on_swEditors_currentChanged( int row ); }; #endif // UIMAIN_H diff --git a/src/example/UIMain.ui b/src/example/UIMain.ui index 4ba6f56..222efdf 100644 --- a/src/example/UIMain.ui +++ b/src/example/UIMain.ui @@ -11,17 +11,27 @@ - - + + + + + + + + + 120 + 16777215 + + + + + QComboBox::AdjustToContents - - - @@ -34,23 +44,9 @@ false - - - - Open... - - - Ctrl+O - - - - QodeEditor - QPlainTextEdit -
QPlainTextEdit
-
Syntax::ComboBox QComboBox