[rkward-cvs] rkward/rkward rkconsole.h,1.14,1.15 rkconsole.cpp,1.20,1.21
Pierre
ecoch at users.sourceforge.net
Tue Apr 11 13:54:15 UTC 2006
Update of /cvsroot/rkward/rkward/rkward
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12004
Modified Files:
rkconsole.h rkconsole.cpp
Log Message:
Adding syntax highlighting in the console, using a kate-part.
Index: rkconsole.cpp
===================================================================
RCS file: /cvsroot/rkward/rkward/rkward/rkconsole.cpp,v
retrieving revision 1.20
retrieving revision 1.21
diff -C2 -d -r1.20 -r1.21
*** rkconsole.cpp 3 Nov 2005 15:24:56 -0000 1.20
--- rkconsole.cpp 11 Apr 2006 13:54:13 -0000 1.21
***************
*** 21,27 ****
--- 21,31 ----
#include <qclipboard.h>
#include <qapplication.h>
+ #include <qobjectlist.h>
+ #include <qevent.h>
#include <klocale.h>
#include <kaction.h>
+ #include <kactioncollection.h>
+
#include "rkglobals.h"
***************
*** 33,41 ****
#include "settings/rksettingsmoduleconsole.h"
! RKConsole::RKConsole () : QTextEdit (0) {
RK_TRACE (APP);
! QFont font ("Courier");
! setFont (font);
setCaption (i18n ("R Console"));
--- 37,125 ----
#include "settings/rksettingsmoduleconsole.h"
!
!
!
! RKConsole::RKConsole () : QWidget (0) {
RK_TRACE (APP);
! QVBoxLayout *layout = new QVBoxLayout (this);
!
! // create a Kate-part as command-editor
! #if !KDE_IS_VERSION (3, 2, 0)
! doc = static_cast<Kate::Document *> (KTextEditor::createDocument ("libkatepart", this, "Kate::Document"));
! view = static_cast<Kate::View *> (doc->createView (this));
! # else
! doc = Kate::document (KTextEditor::createDocument ("libkatepart", this, "Kate::Document"));
! RK_ASSERT (doc);
! view = Kate::view (doc->createView (this));
! RK_ASSERT (view);
! #endif
! layout->addWidget (view);
! doc->setText ("");
!
! view->setDynWordWrap (false);
!
! setFocusPolicy(QWidget::WheelFocus);
!
! /* We need to unplug kactions that were pluged to the KateViewInternal in kateview.cpp.
! These actions incluse Key_Up, Key_Down, etc.
! It's a bit boring to do, but there is no way to do that another way yet.
! Apparently, things will be different in KDE 4.*/
! const QObjectList *list = view->children ();
! QObjectListIt it (*list);
! QObject *obj;
!
! KActionCollection* ac=0;
! QWidget* Kvi=0; //here we store the KateViewInternal of the view, so we can uplug actions from it
!
!
! while ((obj = it.current()) != 0) {
! ++it;
! obj->installEventFilter (this);
! if (obj->inherits("KActionCollection")) {
! ac= (KActionCollection*) obj;
! } else if(obj->inherits("KateViewInternal")) {
! Kvi= (QWidget*) obj;
! }
! }
!
!
! if (ac) {
! unplugAction("move_line_up", ac);
! unplugAction("move_line_down", ac);
! unplugAction("move_cusor_left", ac);
! unplugAction("beginning_of_line", ac);
! unplugAction("backspace", ac);
! unplugAction("delete_next_character", ac);
! unplugAction("beginning_of_document", ac);
! unplugAction("select_beginning_of_line", ac);
! unplugAction("select_char_left", ac);
! unplugAction("word_left", ac); //TODO deal with that one
! unplugAction("select_word_left", ac); //TODO deal with that one
! unplugAction("select_beginning_of_document", ac);
! unplugAction("select_end_of_document", ac);
! unplugAction("select_line_up", ac);
! unplugAction("select_line_down", ac);
! unplugAction("select_page_up", ac);
! unplugAction("move_top_of_view", ac);
! unplugAction("select_top_of_view", ac);
! unplugAction("select_page_down", ac);
! unplugAction("move_bottom_of_view", ac);
! unplugAction("select_bottom_of_view", ac);
! unplugAction("to_matching_bracket", ac);
! unplugAction("select_matching_bracket", ac);
!
!
! } else {
! qDebug("Warning: could not retrieve the view's action collection");
! }
!
!
! view->focusProxy()->installEventFilter(this);
! view->installEventFilter(this);
!
!
! setRHighlighting ();
! doc->setModified (false);
setCaption (i18n ("R Console"));
***************
*** 45,50 ****
prefix = nprefix;
command_incomplete = false;
! setTextFormat (PlainText);
! setUndoRedoEnabled (false);
clear ();
--- 129,134 ----
prefix = nprefix;
command_incomplete = false;
! //TODO:
! //view->setUndoRedoEnabled (false);
clear ();
***************
*** 57,60 ****
--- 141,147 ----
current_command = 0;
+
+
+
}
***************
*** 72,120 ****
}
! void RKConsole::keyPressEvent (QKeyEvent *e) {
if (current_command) {
e->ignore ();
! return;
}
- int para=0; int p=0;
- getCursorPosition (¶, &p);
- // Explicitely converting so we get less warnings.
- uint pos=(uint) p;
if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
submitCommand ();
! return;
}
else if (e->key () == Qt::Key_Up){
commandsListUp ();
! return;
}
else if (e->key () == Qt::Key_Down){
commandsListDown ();
! return;
}
! else if (e->key () == Qt::Key_Left && pos<=prefix.length ()){
! return;
}
! else if (e->key () == Qt::Key_Backspace && pos<=prefix.length ()){
! return;
}
else if (e->key () == Qt::Key_Home){
cursorAtTheBeginning ();
! return;
}
!
! if (para<paragraphs () - 1 || pos <= prefix.length () - 1){
! moveCursor (MoveEnd, false);
! scrollToBottom ();
}
! QTextEdit::keyPressEvent (e);
}
QString RKConsole::currentCommand () {
RK_TRACE (APP);
! QString s = text (paragraphs () - 1).right (paragraphLength (paragraphs () - 1) - prefix.length () + 1);
s = s.stripWhiteSpace ();
--- 159,299 ----
}
! void RKConsole::setRHighlighting () {
! // set syntax-highlighting for R
! int modes_count = doc->hlModeCount ();
! bool found_mode = false;
! int i;
! RK_DO (qDebug ("%s", "Looking for syntax highlighting definition"), COMMANDEDITOR, DL_INFO);
! for (i = 0; i < modes_count; ++i) {
! RK_DO (qDebug ("%s", doc->hlModeName(i).lower().latin1 ()), COMMANDEDITOR, DL_DEBUG);
! if (doc->hlModeName(i).lower() == "rkward output") {
! found_mode = true;
! break;
! }
! }
! if (found_mode) {
! doc->setHlMode(i);
! } else {
! RK_DO (qDebug ("%s", doc->hlModeName(i).lower().latin1 ()), COMMANDEDITOR, DL_WARNING);
! }
! }
!
!
! bool RKConsole::handleKeyPress (QKeyEvent *e) {
!
! uint para=0; uint p=0;
! view->cursorPosition (¶, &p);
! uint pos = p;
!
!
! if (para < doc->numLines() - 1 || pos < prefix.length ()){
! int t=(int)pos;if(prefix.length()>pos) t=prefix.length();
! view-> setCursorPosition (doc->numLines() -1, t);
! return(TRUE);
! }
!
if (current_command) {
e->ignore ();
! return TRUE;
! }
!
! if (hasSelectedText()
! && (selectionInterfaceExt(doc)->selStartCol () < (int)prefix.length()
! ||selectionInterfaceExt(doc)->selStartLine ()< (int)doc->numLines() -1)){ // The selection is wider than the current command
! if (e->key () == Qt::Key_C && e->state () == Qt::ControlButton){ // We only allow to copy
! copy();
! }
!
! return TRUE;
}
+
if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
submitCommand ();
! return TRUE;
! }
! else if (e->state () == Qt::ShiftButton && e->key () == Qt::Key_Home){
! if(hasSelectedText())
! pos=selectionInterfaceExt(doc)->selEndCol (); //There is already a selection, we take it into account.
! selectionInterface(doc)->setSelection(doc->numLines()-1,prefix.length (),doc->numLines()-1, pos);
! cursorAtTheBeginning ();
! return TRUE;
! }
! else if (e->state () == Qt::ShiftButton && e->key () == Qt::Key_Left){
! if(pos<=prefix.length ()){
! return TRUE;
! } else {
! view-> shiftCursorLeft ();
! return FALSE;
! }
}
else if (e->key () == Qt::Key_Up){
commandsListUp ();
! return TRUE;
}
else if (e->key () == Qt::Key_Down){
commandsListDown ();
! return TRUE;
}
! else if (e->key () == Qt::Key_Left){
! if(pos<=prefix.length ()){
! return TRUE;
! } else {
! view->cursorLeft();
! return FALSE;
! }
}
! else if (e->key () == Qt::Key_Backspace){
! if(pos<=prefix.length ()){
! return TRUE;
! } else {
! view->backspace();
! return TRUE;
! }
}
else if (e->key () == Qt::Key_Home){
cursorAtTheBeginning ();
! return TRUE;
}
! else if (e->key() == Qt::Key_Delete) {
! view->keyDelete();
! return TRUE;
}
+
!
!
! return FALSE;
!
! }
!
! bool RKConsole::eventFilter( QObject *o, QEvent *e )
! {
! if ( (e->type() == QEvent::KeyPress) ) {
! QKeyEvent *k = (QKeyEvent *)e;
! return handleKeyPress(k);
! } else if ( (e->type() == QEvent::MouseButtonPress) ){
! QMouseEvent *m = (QMouseEvent *)e;
! if (m->button() == Qt::RightButton) {
! createPopupMenu(m->globalPos());
! return(TRUE);
! }
! return(FALSE);
! } else if ( (e->type() == QEvent::MouseButtonRelease) ){
! QMouseEvent *m = (QMouseEvent *)e;
! if (m->button() == Qt::MidButton) {
! paste();
! return(TRUE);
! }
! return(FALSE);
! } else {
! return FALSE;
! }
}
QString RKConsole::currentCommand () {
RK_TRACE (APP);
! QString s = editInterface(doc)->textLine (doc->numLines() -1).right (doc->lineLength (doc->numLines() -1) - prefix.length () + 1);
s = s.stripWhiteSpace ();
***************
*** 124,129 ****
void RKConsole::setCurrentCommand (QString command) {
RK_TRACE (APP);
! removeParagraph (paragraphs () - 1);
! append (prefix + command);
cursorAtTheEnd ();
}
--- 303,308 ----
void RKConsole::setCurrentCommand (QString command) {
RK_TRACE (APP);
! editInterface(doc)->removeText (doc->numLines() - 1, 0, doc->numLines() - 1, editInterface(doc)->textLine(doc->numLines() - 1).length());
! editInterface(doc)->insertText (doc->numLines() - 1, 0, prefix + command);
cursorAtTheEnd ();
}
***************
*** 131,142 ****
void RKConsole::cursorAtTheEnd () {
RK_TRACE (APP);
! scrollToBottom ();
! moveCursor (MoveEnd, false);
}
void RKConsole::cursorAtTheBeginning () {
RK_TRACE (APP);
! scrollToBottom ();
! setCursorPosition (paragraphs () - 1, prefix.length ());
}
--- 310,321 ----
void RKConsole::cursorAtTheEnd () {
RK_TRACE (APP);
! view-> setCursorPosition (doc->numLines() -1, editInterface(doc)->textLine (doc->numLines() -1).length());
! view->scrollDown ();
}
void RKConsole::cursorAtTheBeginning () {
RK_TRACE (APP);
! view->scrollDown ();
! view->setCursorPosition (doc->numLines() - 1, prefix.length ());
}
***************
*** 157,161 ****
current_command = new RCommand (c, RCommand::User | RCommand::Console | RCommand::ImmediateOutput, QString::null, this);
RKGlobals::rInterface ()->issueCommand (current_command);
- append (""); // new line
emit (doingCommand (true));
} else {
--- 336,339 ----
***************
*** 197,208 ****
if (!(command->type () & RCommand::ImmediateOutput)) { // I don't think we'll have the other case, but for future extension
if (command->hasOutput ()) {
! append (command->output ());
}
if (command->hasError ()) {
! append (command->error ());
}
}
if (command->errorSyntax ()) {
! append (i18n ("Syntax error"));
}
--- 375,386 ----
if (!(command->type () & RCommand::ImmediateOutput)) { // I don't think we'll have the other case, but for future extension
if (command->hasOutput ()) {
! editInterface(doc)->insertLine(doc->numLines(), command->output ());
}
if (command->hasError ()) {
! editInterface(doc)->insertLine(doc->numLines(), command->error ());
}
}
if (command->errorSyntax ()) {
! editInterface(doc)->insertLine(doc->numLines(), i18n ("Syntax error"));
}
***************
*** 224,239 ****
// TODO: handle different types of output, once we can differentiate between them
! // insertAt (output->output, paragraphs ()-1, paragraphLength (paragraphs () - 1));
! moveCursor (MoveEnd, false);
! insert (output->output, (uint) CheckNewLines);
if (RKSettingsModuleConsole::maxConsoleLines ()) {
! uint c = (uint) paragraphs ();
// TODO: WORKAROUND: Somehow, when removing paragraph 0, the QTextEdit scrolls to the top in between (yes, this also happens when using removeParagaph (0)). Since this may happen very often in newOutput, we're a bit sloppy, and only remove lines after a certain threshold (20) is exceeded. When the command is finished, this will be cleaned up automatically.
if (c > (RKSettingsModuleConsole::maxConsoleLines () + 20)) {
! setUpdatesEnabled (false); // major performace boost while removing lines!
! setSelection (0, 0, c - RKSettingsModuleConsole::maxConsoleLines (), 0, 1);
! removeSelectedText (1);
! setUpdatesEnabled (true);
}
}
--- 402,418 ----
// TODO: handle different types of output, once we can differentiate between them
! // insertAt (output->output, doc->numLines()-1, paragraphLength (doc->numLines() - 1));
! view->setCursorPosition (doc->numLines() -1, 1);
! editInterface(doc)->insertText (doc->numLines() , 0, output->output);
if (RKSettingsModuleConsole::maxConsoleLines ()) {
! uint c = (uint) doc->numLines();
// TODO: WORKAROUND: Somehow, when removing paragraph 0, the QTextEdit scrolls to the top in between (yes, this also happens when using removeParagaph (0)). Since this may happen very often in newOutput, we're a bit sloppy, and only remove lines after a certain threshold (20) is exceeded. When the command is finished, this will be cleaned up automatically.
if (c > (RKSettingsModuleConsole::maxConsoleLines () + 20)) {
! view->setUpdatesEnabled (false); // major performace boost while removing lines!
! //TODO : deal with the case when there is already a selection
! selectionInterface(doc)->setSelection (0, 0, c - RKSettingsModuleConsole::maxConsoleLines (), 0);
! selectionInterface(doc)-> removeSelectedText();
! view->setUpdatesEnabled (true);
}
}
***************
*** 253,264 ****
if (add_new_line) {
if (RKSettingsModuleConsole::maxConsoleLines ()) {
! uint c = (uint) paragraphs ();
setUpdatesEnabled (false);
for (uint ui = c; ui > RKSettingsModuleConsole::maxConsoleLines (); --ui) {
! removeParagraph (0);
}
setUpdatesEnabled (true);
}
! append (prefix); // somehow, it seems to be safer to do this after removing superflous lines, than before
cursorAtTheEnd ();
}
--- 432,443 ----
if (add_new_line) {
if (RKSettingsModuleConsole::maxConsoleLines ()) {
! uint c = (uint) doc->numLines();
setUpdatesEnabled (false);
for (uint ui = c; ui > RKSettingsModuleConsole::maxConsoleLines (); --ui) {
! editInterface(doc)->removeText (0, 0,0, editInterface(doc)->textLine(0).length());
}
setUpdatesEnabled (true);
}
! editInterface(doc)->insertLine(doc->numLines(), prefix); // somehow, it seems to be safer to do this after removing superflous lines, than before
cursorAtTheEnd ();
}
***************
*** 289,293 ****
void RKConsole::clear () {
RK_TRACE (APP);
! QTextEdit::clear ();
tryNextInBatch ();
}
--- 468,472 ----
void RKConsole::clear () {
RK_TRACE (APP);
! doc->clear ();
tryNextInBatch ();
}
***************
*** 306,318 ****
}
! QPopupMenu *RKConsole::createPopupMenu (const QPoint &pos) {
RK_TRACE (APP);
QPopupMenu *mp;
emit (fetchPopupMenu (&mp));
! if (mp) return mp;
! return QTextEdit::createPopupMenu (pos);
}
///################### END RKConsole ########################
///################### BEGIN RKConsolePart ####################
--- 485,521 ----
}
! void RKConsole::createPopupMenu (const QPoint &pos) {
RK_TRACE (APP);
QPopupMenu *mp;
emit (fetchPopupMenu (&mp));
! if (mp) {
! mp->exec(pos);
! }
! }
! void RKConsole::copy () {
! RK_TRACE (APP);
! view->copy();
! }
!
! int RKConsole::currentCursorPosition(){
! uint para=0; uint p=0;
! view->cursorPosition (¶, &p);
! return((int) p);
! }
!
! bool RKConsole::hasSelectedText(){
! return(selectionInterface(doc)->hasSelection());
! }
!
! void RKConsole::unplugAction(QString action, KActionCollection* ac)
! {
! KAction* a = ac->action(action);
! if( a ){
! a->setEnabled(false);
! }
}
+
///################### END RKConsole ########################
///################### BEGIN RKConsolePart ####################
***************
*** 338,342 ****
copy = new KAction (i18n ("Copy selection"), 0, console, SLOT (copy ()), actionCollection (), "rkconsole_copy");
paste = new KAction (i18n ("Paste"), KShortcut ("Ctrl+V"), console, SLOT (paste ()), actionCollection (), "rkconsole_paste");
!
connect (console, SIGNAL (fetchPopupMenu (QPopupMenu**)), this, SLOT (makePopupMenu (QPopupMenu**)));
}
--- 541,546 ----
copy = new KAction (i18n ("Copy selection"), 0, console, SLOT (copy ()), actionCollection (), "rkconsole_copy");
paste = new KAction (i18n ("Paste"), KShortcut ("Ctrl+V"), console, SLOT (paste ()), actionCollection (), "rkconsole_paste");
! // same HACK here
! paste->setShortcut ("Ctrl+V");
connect (console, SIGNAL (fetchPopupMenu (QPopupMenu**)), this, SLOT (makePopupMenu (QPopupMenu**)));
}
***************
*** 348,356 ****
void RKConsolePart::showContextHelp () {
RK_TRACE (APP);
!
! int para, p;
! console->getCursorPosition (¶, &p);
!
! RKGlobals::helpDialog ()->getContextHelp (console->text (para), p);
}
--- 552,556 ----
void RKConsolePart::showContextHelp () {
RK_TRACE (APP);
! RKGlobals::helpDialog ()->getContextHelp (console->currentCommand (), console->currentCursorPosition());
}
***************
*** 364,367 ****
--- 564,568 ----
RK_TRACE (APP);
RK_ASSERT (console->current_command);
+ qDebug("was here");
console->commands_batch.clear ();
***************
*** 378,381 ****
--- 579,583 ----
*menu = new QPopupMenu (console);
copy->plug (*menu, 9);
+
copy->setEnabled (console->hasSelectedText ());
paste->plug (*menu, 10);
***************
*** 386,388 ****
--- 588,597 ----
}
+
+
#include "rkconsole.moc"
+
+
+
+
+
Index: rkconsole.h
===================================================================
RCS file: /cvsroot/rkward/rkward/rkward/rkconsole.h,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -d -r1.14 -r1.15
*** rkconsole.h 3 Nov 2005 15:24:56 -0000 1.14
--- rkconsole.h 11 Apr 2006 13:54:13 -0000 1.15
***************
*** 18,24 ****
#define RKCONSOLE_H
! #include <ktextedit.h>
#include <kparts/part.h>
#include <qptrlist.h>
--- 18,27 ----
#define RKCONSOLE_H
!
#include <kparts/part.h>
+ #include <kate/document.h>
+ #include <kate/view.h>
+
#include <qptrlist.h>
***************
*** 28,32 ****
class KAction;
class RCommand;
! class QPopupMenu;
/**
--- 31,35 ----
class KAction;
class RCommand;
!
/**
***************
*** 43,47 ****
**/
! class RKConsole : public QTextEdit, public RCommandReceiver {
Q_OBJECT
public:
--- 46,50 ----
**/
! class RKConsole : public QWidget, public RCommandReceiver {
Q_OBJECT
public:
***************
*** 49,52 ****
--- 52,61 ----
\param batch a QString containing the batch of commands to be executed */
void submitBatch (QString batch);
+ /** Returns the command currently being edited (not executed yet) */
+ QString currentCommand();
+ /** Returns the current cursor position. Returns the column on which is the cursor. */
+ int currentCursorPosition();
+ /** Returns TRUE if some text is selected; otherwise returns FALSE. */
+ bool hasSelectedText();
protected:
/** Constructor. Protected. Construct an RKConsolePart instead */
***************
*** 55,62 ****
~RKConsole ();
! void keyPressEvent (QKeyEvent * e);
void rCommandDone (RCommand *command);
! /** reimplemented to provide our own context menu */
! QPopupMenu *createPopupMenu (const QPoint &pos);
/** reimplemented from RCommandReceiver::newOutput () to handle output of console commands */
void newOutput (RCommand *command, ROutput *output);
--- 64,73 ----
~RKConsole ();
! /** Handle keystrokes before they reach the kate-part. Return TRUE if we want the kate-part to ignore it
! \param e the QKeyEvent */
! bool handleKeyPress (QKeyEvent * e);
void rCommandDone (RCommand *command);
! /** provides our own context menu */
! void createPopupMenu (const QPoint &pos);
/** reimplemented from RCommandReceiver::newOutput () to handle output of console commands */
void newOutput (RCommand *command, ROutput *output);
***************
*** 66,69 ****
--- 77,83 ----
private:
friend class RKConsolePart;
+ bool eventFilter( QObject *o, QEvent *e );
+ /** set syntax-highlighting for R */
+ void setRHighlighting ();
QString incomplete_command;
bool command_incomplete;
***************
*** 75,80 ****
void cursorAtTheEnd();
- /** Returns the command currently being edited (not executed yet) */
- QString currentCommand();
/** Submits the current command */
void submitCommand();
--- 89,92 ----
***************
*** 85,92 ****
/** Sets the cursor position to the beginning of the last line. */
void cursorAtTheBeginning();
! /** We overload the paste function, in order to intercept paste commands and get them executed through submitBatch.
! @sa submitBatch */
! void paste();
! /** We overload the clear function, to add a prompt at the top. */
void clear();
/** Sets the current command. This is used from commandsListUp (), and commandsListDown ();
--- 97,101 ----
/** Sets the cursor position to the beginning of the last line. */
void cursorAtTheBeginning();
! /** Clear the view, and add a prompt at the top. */
void clear();
/** Sets the current command. This is used from commandsListUp (), and commandsListDown ();
***************
*** 103,108 ****
--- 112,129 ----
/** This string stores the continuation prefix. */
const char *iprefix;
+ /** This function unplugs a KAction
+ \param action the KAction to be unplugged
+ \param ac the action collection from which to retrieve the KAction*/
+ void unplugAction(QString action, KActionCollection* ac);
RCommand *current_command;
+ Kate::Document *doc;
+ Kate::View *view;
+
+ public slots:
+ /** We intercept paste commands and get them executed through submitBatch.
+ @sa submitBatch */
+ void paste();
+ void copy();
};
***************
*** 135,137 ****
--- 156,162 ----
};
+
+
+
+
#endif
More information about the rkward-tracker
mailing list