[Kde-perl] Perl Qt Editor Include a Perl syntax highlighter !
Dirk Lindner
adrenalinux at t-online.de
Sun Feb 8 22:32:28 CET 2004
Hello !
I have beginn to writing a Perl sysntax Highlighting Text Editor and it is
more work as i thought so if somebody is interest to help me i would be very
thankful !
I also have some questions !
1. I need Image Colections for a IconView based FileView, should i simple
generate it from the kde images or are such collections already a part of
the planned Kde bindings ?
2. What is the way to access the kde classes ( reimplement in perl ) ?
3. Have somebody beginn to write bindings to the classes and can i see thee
Source of it ( if available) ?
4. Whats the way to use the pqtdesigner if i want use a image collections
and including slots ( puic -o program.pl -x -subimpl Form2 form1.ui will
only work with a c++ projekt ) ?
Dirk !
___________________________Code_________________________________________________
# Author : Dirk Lindner adrenalinux at t-online.de Siegen 06.02.2004
use strict;
package Pqsyntaxhighlight;
use Qt;
use Qt::isa qw|Qt::SyntaxHighlighter|;
use Qt::slots
highlightParagraph => [];
sub NEW{
shift->SUPER::NEW($_[0]);
}
#the enclosed keys => color( r,g,b)
my %keys = (
'\'' => [0,100,0],
'"' => [0,100,0]
);
# the enclosed keys => /pattern/
my %pattern =(
'"' => '("[^"]*")',
'\'' => "('[^']*')"
);
my %words = (
'(\$\w*)' => [0,0,100],
'(%\w*)' => [0,0,100],
'(@\w*)' => [0,0,100],
'(@\w*)' => [0,0,100],
'([=|!]~\\s*?m?/[^\/]*\/)' => [0,100,0],
'\b((s|tr|y)\/[^\/]*\/[^\/]*\/)'=> [0,100,0],
'\b((qw|qr|q|qq)/[^\/]*\/)' => [0,100,0],
'("[^"]+)$', => [255,0,0],
'(\'[^\']+)$', => [255,0,0],
'[^\\\\$](#.*)$' => [172,172,172],
'^(#.*)$' => [172,172,172],
'(\b(int\b))' => [0,0,255],
'(\b(my\b))' => [0,0,255],
'(\b(our\b))' => [0,0,255]
);
my @color;
my $text ;
sub highlightParagraph{
#warranted that the default color is black
for(my $i = 0; $i <= $#color ; $i++){$color[$i] = [0,0,0];}
setFormat(0,length($text),&Qt::Color(0,0,0));
#current Text
$text = shift;
#tells setColor with whichsoever $key she should beginn
NEXTKEY:for(my $i = 0; $i <= length($text);$i++){
foreach my $key (keys %keys){
my $currentchar = substr($text,$i,1);
$text =~ s/\\$key/ee/g; # devaluates the keys
if ($currentchar eq $key ){
setColor($key, $pattern{$key});
next NEXTKEY;
}
}
}
# set the color for each pattern
sub setColor{
my $key = shift;
my $pat = shift;
if($text =~/$pat/g){
my $pos = pos($text);
my $length = length($1);
my $beginn = ($pos - $length);
for(my $i = $beginn; $i < $pos ; $i++){
$color[$i] = $keys{$key};
substr($text, $beginn, $length ,("e" x $length));
}
}
}
for(my $i = 0; $i< $#color; $i++){
setFormat($i,1,&Qt::Color(@{$color[$i]}));
}
foreach my $word (keys %words){
while($text =~/$word/g){
my $position = pos($text);
my $length = length($1);
my $beginn = ($position - $length);
setFormat($beginn,$length,&Qt::Color(@{$words{$word}}));
}
}
}
1;
use strict;
use utf8;
package PqTextEdit;
use Qt;
use Pqsyntaxhighlight;
use Qt::isa qw|Qt::TextEdit|;
use Qt::attributes qw(
editMenu
editUndoAction
editRedoAction
editCutAction
editCopyAction
editPasteAction
editFindAction
syntax
fileMenu
menunewAction
);
use Qt::slots
createPopupMenu => [],
menunew => [],
if => [],
dropEvent =>[];
use Qt::signals
fileneu =>[];
sub uic_load_pixmap_editor
{
my $pix = Qt::Pixmap();
my $m = Qt::MimeSourceFactory::defaultFactory()->data(shift);
if($m)
{
Qt::ImageDrag::decode($m, $pix);
}
return $pix;
}
sub NEW{
shift->SUPER::NEW(@_[0]);
syntax = Pqsyntaxhighlight(this);
syntax->highlightParagraph;
this->setFrameShape(&Qt::TextEdit::MenuBarPanel);
this->setFrameShadow(&Qt::TextEdit::Plain);
this->setTextFormat(&Qt::TextEdit::Plain);
this->setHScrollBarMode(&Qt::TextEdit::AlwaysOff);
}
my $fileMenu;
sub createPopupMenu{
$fileMenu = Qt::PopupMenu(this);
editMenu= Qt::ActionGroup(this, "EditActionGroup");
editMenu->setText(trUtf8("edit"));
editMenu->setUsesDropDown(1);
editUndoAction= Qt::Action(this, "editUndoAction");
editUndoAction->setIconSet(Qt::IconSet(uic_load_pixmap_editor("undo")));
editUndoAction->setText(trUtf8("Undo"));
editUndoAction->setMenuText(trUtf8("&Undo"));
editUndoAction->setAccel(Qt::KeySequence(trUtf8("Ctrl+Z")));
editMenu->add(editUndoAction);
editRedoAction= Qt::Action(this, "editRedoAction");
editRedoAction->setIconSet(Qt::IconSet(uic_load_pixmap_editor("redo")));
editRedoAction->setText(trUtf8("Redo"));
editRedoAction->setMenuText(trUtf8("&Redo"));
editRedoAction->setAccel(Qt::KeySequence(trUtf8("Ctrl+Y")));
editMenu->add(editRedoAction);
editCutAction= Qt::Action(this, "editCutAction");
editCutAction->setIconSet(Qt::IconSet(uic_load_pixmap_editor("editcut")));
editCutAction->setText(trUtf8("Cut"));
editCutAction->setMenuText(trUtf8("&Cut"));
editCutAction->setAccel(Qt::KeySequence(trUtf8("Ctrl+X")));
editMenu->add(editCutAction);
editCopyAction= Qt::Action(this, "editCopyAction");
editCopyAction->setIconSet(Qt::IconSet(uic_load_pixmap_editor("editcopy")));
editCopyAction->setText(trUtf8("Copy"));
editCopyAction->setMenuText(trUtf8("C&opy"));
editCopyAction->setAccel(Qt::KeySequence(trUtf8("Ctrl+C")));
editMenu->add(editCopyAction);
editPasteAction= Qt::Action(this, "editPasteAction");
editPasteAction->setIconSet(Qt::IconSet(uic_load_pixmap_editor("editpaste")));
editPasteAction->setText(trUtf8("Paste"));
editPasteAction->setMenuText(trUtf8("&Paste"));
editPasteAction->setAccel(Qt::KeySequence(trUtf8("Ctrl+V")));
editMenu->add(editPasteAction);
editFindAction= Qt::Action(this, "editFindAction");
editFindAction->setIconSet(Qt::IconSet(uic_load_pixmap_editor("searchfind")));
editFindAction->setText(trUtf8("Find"));
editFindAction->setMenuText(trUtf8("&Find..."));
editFindAction->setAccel(Qt::KeySequence(trUtf8("Ctrl+F")));
editMenu->add(editFindAction);
editMenu->addTo($fileMenu);
menunewAction= Qt::Action(this,"menunewAction");
menunewAction->setText(trUtf8("New"));
menunewAction->setMenuText(trUtf8("&action"));
menunewAction->addTo($fileMenu);
$fileMenu->insertItem( "&New", this, SLOT "insertMenu()");
$fileMenu->insertSeparator();
my $newID = $fileMenu->insertItem( "&if", this, SLOT "if()" );
#fileMenu->setItemEnabled( $newID, FALSE );
Qt::Object::connect(editUndoAction, SIGNAL "activated()", this, SLOT
"undo()");
Qt::Object::connect(editRedoAction, SIGNAL "activated()", this, SLOT
"redo()");
Qt::Object::connect(editCutAction, SIGNAL "activated()", this, SLOT "cut()");
Qt::Object::connect(editCopyAction, SIGNAL "activated()", this, SLOT
"copy()");
Qt::Object::connect(editPasteAction, SIGNAL "activated()", this, SLOT
"paste()");
Qt::Object::connect(editFindAction, SIGNAL "activated()", this, SLOT
"scrollToBottom()");
return $fileMenu;
}
sub if {
insert("if ( ){ \n\n }");
}
1;
More information about the Kde-perl
mailing list