diff --git a/build-all.pro b/build-all.pro new file mode 100644 index 0000000..aad4dd5 --- /dev/null +++ b/build-all.pro @@ -0,0 +1,10 @@ +XUP.QT_VERSION = Qt System (4.8.1) +XUP.OTHERS_PLATFORM_TARGET_DEFAULT = bin/Linux/QodeEdit_debug + +include( config.pri ) + +TEMPLATE = subdirs +CONFIG *= ordered + +SUBDIRS = QodeEdit.pro \ + example.pro \ No newline at end of file diff --git a/example.pro b/example.pro new file mode 100644 index 0000000..afd2015 --- /dev/null +++ b/example.pro @@ -0,0 +1,31 @@ +########################################################################################### +## Created using Monkey Studio IDE v1.9.0.1 (1.9.0.1) +## +## Author : You Name +## Project : qodeedit +## FileName : qodeedit.pro +## Date : 2012-05-27T13:18:50 +## License : GPL +## Comment : Creating using Monkey Studio RAD +## Home Page : https://github.com/pasnox/qodeedit +## +## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +## +########################################################################################### +include( config.pri ) +initializeProject( app, $${BUILD_TARGET}, $${BUILD_MODE}, $${BUILD_PATH}/$${TARGET_NAME}, $${BUILD_TARGET_PATH} ) + +INCLUDEPATH *= $$getFolders( example ) $$getFolders( src ) +DEPENDPATH *= $${INCLUDEPATH} + +LIBS *= -L$${BUILD_TARGET_PATH} -l$$targetForMode( QodeEdit ) + +FORMS *= \ + example/UIMain.ui + +HEADERS *= \ + example/UIMain.h + +SOURCES *= main.cpp \ + example/UIMain.cpp \ No newline at end of file diff --git a/example/UIMain.cpp b/example/UIMain.cpp new file mode 100644 index 0000000..d70d192 --- /dev/null +++ b/example/UIMain.cpp @@ -0,0 +1,227 @@ +#include "UIMain.h" +#include "ui_UIMain.h" +#include "CodeEditor.h" +#include "MarginStacker.h" +#include "SyntaxFactory.h" +#include "QodeEdit.h" +#include "SyntaxHighlighter.h" +#include "TextDocument.h" + +#include + +UIMain* UIMain::qMain = 0; + +// QodeEditor + +QodeEditor::QodeEditor( QWidget* parent ) + : CodeEditor( parent ) +{ + setCaretLineBackground( QColor( 150, 150, 150, 150 ) ); + + MarginStacker* margins = new MarginStacker( this ); + margins->setVisible( QodeEdit::BookmarkMargin, true ); + margins->setVisible( QodeEdit::NumberMargin, true ); + margins->setVisible( QodeEdit::RevisionMargin, true ); + margins->setVisible( QodeEdit::FoldMargin, true ); + margins->setVisible( QodeEdit::SpaceMargin, true ); + + // fake save document shortcut + 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 ); +} + +// SpacerWidget + +class SpacerWidget : public QWidget { +public: + SpacerWidget( QWidget* parent = 0 ) + : QWidget( parent ) + { + setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum ); + } +}; + +// UIMain + +UIMain::UIMain( QWidget* parent ) + : QMainWindow( parent ), ui( new Ui_UIMain ) +{ + if ( !UIMain::qMain ) { + UIMain::qMain = this; + } + + ui->setupUi( this ); + ui->toolBar->addWidget( new SpacerWidget( this ) ); + ui->toolBar->addWidget( ui->cbSyntax ); + + qInstallMsgHandler( UIMain::messageHandler ); + + QString error; + + if ( Syntax::Factory::load( &error ) ) { + qWarning() << Q_FUNC_INFO << "Syntaxes loaded correctly"; + } + + //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( QString( "%1 (%2)" ).arg( file.fileName() ).arg( 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 ); + + /*qWarning() << Syntax::Factory::mimeTypeForFile( "toto.h" ); + qWarning() << Syntax::Factory::mimeTypeForFile( "toto.c" ); + qWarning() << Syntax::Factory::mimeTypeForFile( "toto.cpp" ); + qWarning() << Syntax::Factory::mimeTypeForFile( "toto.adb" ); + + qWarning() << Syntax::Factory::mimeTypeForData( "#include \n" ); + qWarning() << Syntax::Factory::mimeTypeForData( "#include \n" ); + qWarning() << Syntax::Factory::mimeTypeForData( "#import \n" ); + + qWarning() << Syntax::Factory::mimeTypeForUrl( QUrl( "http://toto.com/test.html" ) ); + qWarning() << Syntax::Factory::mimeTypeForUrl( QUrl( "http://toto.com/test.pdf" ) ); + qWarning() << Syntax::Factory::mimeTypeForUrl( QUrl( "http://toto.com/test.jpg" ) ); + + qWarning() << Syntax::Factory::mimeTypesForFileName( "toto.h" ); + qWarning() << Syntax::Factory::mimeTypesForFileName( "toto.c" ); + qWarning() << Syntax::Factory::mimeTypesForFileName( "toto.cpp" ); + qWarning() << Syntax::Factory::mimeTypesForFileName( "toto.adb" ); + + qWarning() << QodeEdit::rulerToString( QodeEdit::NoRuler ); + qWarning() << QodeEdit::rulerToString( QodeEdit::BackgroundRuler ); + qWarning() << QodeEdit::stringToRuler( "background" ); + qWarning() << QodeEdit::stringToRuler( "backgroundRuler" ); + qWarning() << QodeEdit::stringToRuler( "Background" );*/ +} + +UIMain::~UIMain() +{ + if ( UIMain::qMain == this ) { + UIMain::qMain = 0; + } + + delete ui; + Syntax::Factory::free(); +} + +void UIMain::appendDebugMessage( const QString& message ) +{ + ui->pteDebug->appendPlainText( message ); + printf( "%s\n", qPrintable( message ) ); +} + +void UIMain::messageHandler( QtMsgType type, const char* msg ) +{ + switch ( type ) { + case QtDebugMsg: + UIMain::qMain->appendDebugMessage( QString( "Debug: %1" ).arg( msg ) ); + break; + case QtWarningMsg: + UIMain::qMain->appendDebugMessage( QString( "Warning: %1" ).arg( msg ) ); + break; + case QtCriticalMsg: + UIMain::qMain->appendDebugMessage( QString( "Critical: %1" ).arg( msg ) ); + break; + case QtFatalMsg: + UIMain::qMain->appendDebugMessage( QString( "Fatal: %1" ).arg( msg ) ); + //abort(); + break; + } +} + +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/example/UIMain.h b/example/UIMain.h new file mode 100644 index 0000000..dea242f --- /dev/null +++ b/example/UIMain.h @@ -0,0 +1,49 @@ +#ifndef UIMAIN_H +#define UIMAIN_H + +#include + +#include "CodeEditor.h" + +class Ui_UIMain; + +class QodeEditor : public CodeEditor +{ + Q_OBJECT + +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 + +public: + UIMain( QWidget* parent = 0 ); + virtual ~UIMain(); + + void appendDebugMessage( const QString& message ); + + static void messageHandler( QtMsgType type, const char* msg ); + static UIMain* qMain; + +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/example/UIMain.ui b/example/UIMain.ui new file mode 100644 index 0000000..e5ab95f --- /dev/null +++ b/example/UIMain.ui @@ -0,0 +1,108 @@ + + + UIMain + + + + 0 + 0 + 557 + 386 + + + + + + + + QComboBox::AdjustToContents + + + + + + + Qt::Vertical + + + + Qt::Horizontal + + + + + 0 + 0 + + + + + 240 + 16777215 + + + + + + + 0 + 0 + + + + + + + + 0 + 0 + + + + + 16777215 + 140 + + + + true + + + false + + + QPlainTextEdit::NoWrap + + + true + + + 40 + + + + + + + + + toolBar + + + TopToolBarArea + + + false + + + + + + Syntax::ComboBox + QComboBox +
SyntaxComboBox.h
+
+
+ + +
diff --git a/example/main.cpp b/example/main.cpp new file mode 100644 index 0000000..136cbc0 --- /dev/null +++ b/example/main.cpp @@ -0,0 +1,17 @@ +#include + +#include "UIMain.h" + +int main( int argc, char** argv ) +{ + QApplication app( argc, argv ); + + QRect rect = QRect( QPoint(), QSize( 640, 480 ) ); + rect.moveCenter( QApplication::desktop()->availableGeometry().center() ); + + UIMain window; + window.setGeometry( rect ); + window.showMaximized(); + + return app.exec(); +}