Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Update UIMain example.
Browse files Browse the repository at this point in the history
  • Loading branch information
pasnox committed Jul 31, 2012
1 parent 693a4d1 commit ca2895b
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 20 deletions.
105 changes: 104 additions & 1 deletion src/example/UIMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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 );
Expand Down Expand Up @@ -51,11 +77,88 @@ 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()
{
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<QodeEditor*>();
}

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 );
}
11 changes: 11 additions & 0 deletions src/example/UIMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,6 +32,13 @@ class UIMain : public QMainWindow

protected:
Ui_UIMain* ui;
QHash<QString, QodeEditor*> mEditors;

QodeEditor* editor( int row ) const;

protected slots:
void on_lwEditors_currentRowChanged( int row );
void on_swEditors_currentChanged( int row );
};

#endif // UIMAIN_H
34 changes: 15 additions & 19 deletions src/example/UIMain.ui
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,27 @@
</rect>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="1">
<widget class="QStackedWidget" name="swEditors"/>
</item>
<item row="1" column="0">
<widget class="QListWidget" name="lwEditors">
<property name="maximumSize">
<size>
<width>120</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="Syntax::ComboBox" name="cbSyntax">
<property name="sizeAdjustPolicy">
<enum>QComboBox::AdjustToContents</enum>
</property>
</widget>
</item>
<item>
<widget class="QodeEditor" name="qeEdit"/>
</item>
</layout>
</widget>
<widget class="QToolBar" name="toolBar">
Expand All @@ -34,23 +44,9 @@
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="aOpen"/>
</widget>
<action name="aOpen">
<property name="text">
<string>Open...</string>
</property>
<property name="shortcut">
<string>Ctrl+O</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
<class>QodeEditor</class>
<extends>QPlainTextEdit</extends>
<header>QPlainTextEdit</header>
</customwidget>
<customwidget>
<class>Syntax::ComboBox</class>
<extends>QComboBox</extends>
Expand Down

0 comments on commit ca2895b

Please sign in to comment.