[rkward-cvs] SF.net SVN: rkward: [2277] branches/KDE4_port/rkward/windows

tfry at users.sourceforge.net tfry at users.sourceforge.net
Fri Nov 30 01:02:22 UTC 2007


Revision: 2277
          http://rkward.svn.sourceforge.net/rkward/?rev=2277&view=rev
Author:   tfry
Date:     2007-11-29 17:02:22 -0800 (Thu, 29 Nov 2007)

Log Message:
-----------
Start working on block actions. Not complete, yet, but looking promising.

Modified Paths:
--------------
    branches/KDE4_port/rkward/windows/rkcommandeditorwindow.cpp
    branches/KDE4_port/rkward/windows/rkcommandeditorwindow.h
    branches/KDE4_port/rkward/windows/rkcommandeditorwindowpart.rc

Modified: branches/KDE4_port/rkward/windows/rkcommandeditorwindow.cpp
===================================================================
--- branches/KDE4_port/rkward/windows/rkcommandeditorwindow.cpp	2007-11-29 22:38:15 UTC (rev 2276)
+++ branches/KDE4_port/rkward/windows/rkcommandeditorwindow.cpp	2007-11-30 01:02:22 UTC (rev 2277)
@@ -58,13 +58,12 @@
 
 #include "../debug.h"
 
-RKCommandEditorWindowPart::RKCommandEditorWindowPart (QWidget *parent, RKCommandEditorWindow *editor_widget) : KParts::Part (parent) {
+RKCommandEditorWindowPart::RKCommandEditorWindowPart (QWidget *parent) : KParts::Part (parent) {
 	RK_TRACE (COMMANDEDITOR);
 
 	setComponentData (KGlobal::mainComponent ());
 	setWidget (parent);
 	setXMLFile ("rkcommandeditorwindowpart.rc");
-	editor_widget->initializeActions (actionCollection ());
 }
 
 RKCommandEditorWindowPart::~RKCommandEditorWindowPart () {
@@ -90,9 +89,10 @@
 	RKCommonFunctions::removeContainers (m_view, QString ("bookmarks,tools_spelling,tools_spelling_from_cursor,tools_spelling_selection,switch_to_cmd_line").split (','), true);
 	RKCommonFunctions::moveContainer (m_view, "Menu", "tools", "edit", true);
 
-	RKCommandEditorWindowPart* part = new RKCommandEditorWindowPart (m_view, this);
+	RKCommandEditorWindowPart* part = new RKCommandEditorWindowPart (m_view);
 	part->insertChildClient (m_view);
 	setPart (part);
+	initializeActions (part->actionCollection ());
 	initializeActivationSignals ();
 
 	QHBoxLayout *layout = new QHBoxLayout (this);
@@ -102,6 +102,8 @@
 	connect (m_doc, SIGNAL (documentUrlChanged (KTextEditor::Document*)), this, SLOT (updateCaption (KTextEditor::Document*)));
 	connect (m_doc, SIGNAL (modifiedChanged (KTextEditor::Document*)), this, SLOT (updateCaption (KTextEditor::Document*)));		// of course most of the time this causes a redundant call to updateCaption. Not if a modification is undone, however.
 	connect (m_doc, SIGNAL (textChanged (KTextEditor::Document*)), this, SLOT (tryCompletionProxy (KTextEditor::Document*)));
+	connect (m_view, SIGNAL (selectionChanged(KTextEditor::View*)), this, SLOT (selectionChanged(KTextEditor::View*)));
+	connect (m_view, SIGNAL (cursorPositionChanged(KTextEditor::View*,const KTextEditor::Cursor&)), this, SLOT (cursorPositionChanged(KTextEditor::View*,const KTextEditor::Cursor&)));
 	// somehow the katepart loses the context menu each time it loses focus
 	connect (m_view, SIGNAL (focusIn(KTextEditor::View*)), this, SLOT (focusIn(KTextEditor::View*)));
 	completion_timer = new QTimer (this);
@@ -123,6 +125,17 @@
 		hinter = new RKFunctionArgHinter (this, m_view);
 	}
 
+	top_block_range = 0;
+	last_active_block = 0;
+	smart_iface = qobject_cast<KTextEditor::SmartInterface*> (m_doc);
+	if (smart_iface) {
+		top_block_range = smart_iface->newSmartRange (m_doc->documentRange());
+		top_block_range->setInsertBehavior (KTextEditor::SmartRange::ExpandLeft | KTextEditor::SmartRange::ExpandRight);
+		smart_iface->addHighlightToView (m_view, top_block_range);
+	} else {
+		RK_ASSERT (false);
+	}
+
 	updateCaption ();	// initialize
 	QTimer::singleShot (0, this, SLOT (setPopupMenu ()));
 }
@@ -137,11 +150,23 @@
 	RK_TRACE (COMMANDEDITOR);
 
 	action_run_all = RKStandardActions::runAll (ac, "run_all", this, SLOT (runAll()));
+#warning TODO: move to RKStandardActions
+	action_run_block = ac->addAction ("run_block", this, SLOT (runBlock()));
+	action_run_block->setText (i18n ("Run this block"));
+	action_run_block->setEnabled (false);
 	action_run_selection = RKStandardActions::runSelection (ac, "run_selection", this, SLOT (runSelection()));
+	action_run_selection->setEnabled (false);
 	action_run_line = RKStandardActions::runLine (ac, "run_line", this, SLOT (runLine()));
 
 	action_help_function = RKStandardActions::functionHelp (ac, "function_reference", this, SLOT (showHelp()));
 
+	action_mark_block = ac->addAction ("mark_block", this, SLOT (markBlock()));
+	action_mark_block->setText (i18n ("Mark selection as block"));
+	action_mark_block->setEnabled (false);
+	action_unmark_block = ac->addAction ("unmark_block", this, SLOT (unmarkBlock()));
+	action_unmark_block->setText (i18n ("Unmark this block"));
+	action_unmark_block->setEnabled (false);
+
 	QAction* action_configure = ac->addAction ("configure_commandeditor", this, SLOT (configure()));
 	action_configure->setText (i18n ("Configure Script Editor"));
 }
@@ -362,7 +387,7 @@
 }
 
 
-void RKCommandEditorWindow::runAll() {
+void RKCommandEditorWindow::runAll () {
 	RK_TRACE (COMMANDEDITOR);
 
 	QString command = m_doc->text ();
@@ -371,6 +396,118 @@
 	RKConsole::pipeUserCommand (command);
 }
 
+void RKCommandEditorWindow::runBlock () {
+	RK_TRACE (COMMANDEDITOR);
+
+#warning implement
+}
+
+void RKCommandEditorWindow::markBlock () {
+	RK_TRACE (COMMANDEDITOR);
+
+	if (m_view->selection ()) {
+		KTextEditor::SmartRange* range = smart_iface->newSmartRange (m_view->selectionRange (), top_block_range, KTextEditor::SmartRange::ExpandRight);
+		range->addWatcher (this);
+
+		highlightBlock (range, true);
+	} else {
+		RK_ASSERT (false);
+	}
+}
+
+void RKCommandEditorWindow::highlightBlock (KTextEditor::SmartRange* block, bool active) {
+	RK_TRACE (COMMANDEDITOR);
+
+	if (!block) return;
+	if (!block->isValid ()) return;
+
+	QColor color;
+	if (active) color = QColor (255, 255, 30);
+	else color = QColor (255, 255, 180);
+
+	KTextEditor::Attribute::Ptr attribute (new KTextEditor::Attribute());
+	attribute->setBackground (color);
+	block->setAttribute (attribute);
+}
+
+void RKCommandEditorWindow::unmarkBlock () {
+	RK_TRACE (COMMANDEDITOR);
+
+	KTextEditor::SmartRange* block = currentBlock ();
+	RK_ASSERT (block);
+	delete block;
+	last_active_block = 0;
+
+	// update state
+	cursorPositionChanged (m_view, m_view->cursorPosition ());
+}
+
+void RKCommandEditorWindow::cursorPositionChanged (KTextEditor::View* view, const KTextEditor::Cursor &) {
+	RK_TRACE (COMMANDEDITOR);
+	RK_ASSERT (view == m_view);
+
+	KTextEditor::SmartRange* new_block = currentBlock ();
+	if (new_block) {
+		action_run_block->setEnabled (true);
+		action_unmark_block->setEnabled (true);
+	} else {
+		action_run_block->setEnabled (false);
+		action_unmark_block->setEnabled (false);
+	}
+
+	if (new_block != last_active_block) {
+		highlightBlock (last_active_block, false);
+		highlightBlock (new_block, true);
+#warning the kateview repainting is no good
+	}
+	last_active_block = new_block;
+}
+
+KTextEditor::SmartRange* RKCommandEditorWindow::currentBlock() const {
+	RK_TRACE (COMMANDEDITOR);
+
+	KTextEditor::Range active_range = KTextEditor::Range (m_view->cursorPosition (), m_view->cursorPosition ());
+	if (m_view->selection ()) {
+		active_range = m_view->selectionRange ();
+	}
+
+	KTextEditor::SmartRange* active_block = top_block_range->mostSpecificRange (active_range);
+	if (active_block && (active_block != top_block_range) && active_block->isValid () && !active_block->isEmpty()) {
+		return active_block;
+	} else {
+		return 0;
+	}
+}
+
+void RKCommandEditorWindow::selectionChanged (KTextEditor::View* view) {
+	RK_TRACE (COMMANDEDITOR);
+	RK_ASSERT (view == m_view);
+
+	if (view->selection ()) {
+		action_run_selection->setEnabled (true);
+
+		KTextEditor::Range selrange = view->selectionRange ();
+		bool intersects_existing_block = false;
+		QList<KTextEditor::SmartRange*> blocks = top_block_range->childRanges ();
+		for (int i = 0; i < blocks.size (); ++i) {
+			KTextEditor::SmartRange* range = blocks[i];
+			if (range->isEmpty () || !range->isValid ()) {
+				if (range == last_active_block) last_active_block = 0;
+				delete range;		// Needed?
+				continue;
+			}
+			if (range->overlaps (selrange)) {
+				intersects_existing_block = true;
+				break;
+			}
+		}
+		action_mark_block->setEnabled (!intersects_existing_block);
+	} else {
+		action_run_selection->setEnabled (false);
+		action_mark_block->setEnabled (false);
+	}
+}
+
 void RKCommandEditorWindow::configure () {
 	RK_TRACE (COMMANDEDITOR);
 
@@ -455,7 +592,7 @@
 			bool have_context = provider->provideContext (++line_rev, &current_line, &cursor_pos);
 			if ((!have_context) || (current_line.isEmpty ())) break;
 
-			RK_ASSERT (cursor_pos > 0);
+			RK_ASSERT (cursor_pos < 0);
 			current_context.prepend (current_line);
 			i = current_line.length () - 1;
 		}

Modified: branches/KDE4_port/rkward/windows/rkcommandeditorwindow.h
===================================================================
--- branches/KDE4_port/rkward/windows/rkcommandeditorwindow.h	2007-11-29 22:38:15 UTC (rev 2276)
+++ branches/KDE4_port/rkward/windows/rkcommandeditorwindow.h	2007-11-30 01:02:22 UTC (rev 2277)
@@ -25,6 +25,9 @@
 #include <ktexteditor/document.h>
 #include <ktexteditor/codecompletionmodel.h>
 #include <ktexteditor/codecompletioninterface.h>
+#include <ktexteditor/smartrange.h>
+#include <ktexteditor/smartinterface.h>
+#include <ktexteditor/rangefeedback.h>
 #include <kurl.h>
 
 #include "../windows/rkmdiwindow.h"
@@ -44,8 +47,7 @@
 class RKCommandEditorWindowPart : public KParts::Part {
 protected:
 friend class RKCommandEditorWindow;
-	RKCommandEditorWindowPart (QWidget *parent, RKCommandEditorWindow *editor_widget);
-
+	RKCommandEditorWindowPart (QWidget *parent);
 	~RKCommandEditorWindowPart ();
 };
 
@@ -116,7 +118,7 @@
 
 @author Pierre Ecochard
 */
-class RKCommandEditorWindow : public RKMDIWindow, public RKScriptContextProvider {
+class RKCommandEditorWindow : public RKMDIWindow, public RKScriptContextProvider, public KTextEditor::SmartRangeWatcher {
 // we need the Q_OBJECT thing for some inherits ("RKCommandEditorWindow")-calls in rkward.cpp.
 	Q_OBJECT
 public:
@@ -167,17 +169,34 @@
 	void runLine ();
 /** run the entire script */
 	void runAll ();
+/** run the current block */
+	void runBlock ();
 /** invoke the settings page for the command editor */
 	void configure ();
 
-	void initializeActions (KActionCollection* ac);
+/** mark current selection as a block */
+	void markBlock ();
+/** unmark current block */
+	void unmarkBlock ();
+
+/** selection has changed. Enable / disable actions accordingly */
+	void selectionChanged (KTextEditor::View* view);
+/** cursor position has changed. Enable / disable actions accordingly */
+	void cursorPositionChanged (KTextEditor::View* view, const KTextEditor::Cursor &new_position);
 protected:
-/** reimplemented from KMdiChildView: give the editor window a chance to object to being closed (if unsaved) */
+/** reimplemented from RKMDIWindow: give the editor window a chance to object to being closed (if unsaved) */
 	void closeEvent (QCloseEvent *e);
 private:
+	KTextEditor::SmartRange* currentBlock() const;
+	KTextEditor::SmartRange* last_active_block;
+
+	void highlightBlock (KTextEditor::SmartRange* block, bool active);
+
 	KTextEditor::Document *m_doc;
 	KTextEditor::View *m_view;
 	KTextEditor::CodeCompletionInterface *cc_iface;
+	KTextEditor::SmartInterface *smart_iface;
+	KTextEditor::SmartRange *top_block_range;
 	RKFunctionArgHinter *hinter;
 	RKCodeCompletionModel *completion_model;
 
@@ -185,8 +204,14 @@
 /** set syntax highlighting-mode to R syntax */
 	void setRHighlighting ();
 
+	void initializeActions (KActionCollection* ac);
+
+	QAction* action_mark_block;
+	QAction* action_unmark_block;
+
 	QAction* action_run_all;
 	QAction* action_run_selection;
+	QAction* action_run_block;
 	QAction* action_run_line;
 
 	QAction* action_help_function;

Modified: branches/KDE4_port/rkward/windows/rkcommandeditorwindowpart.rc
===================================================================
--- branches/KDE4_port/rkward/windows/rkcommandeditorwindowpart.rc	2007-11-29 22:38:15 UTC (rev 2276)
+++ branches/KDE4_port/rkward/windows/rkcommandeditorwindowpart.rc	2007-11-30 01:02:22 UTC (rev 2277)
@@ -1,5 +1,5 @@
 <!DOCTYPE kpartgui>
-<kpartgui name="rkward" version="0.4.2">
+<kpartgui name="rkward" version="0.5.0">
 	<MenuBar>
 		<Menu name="edit" noMerge="1"><text>&Edit</text>
 			<Menu name="tools"><text>&Tools Move</text>
@@ -7,10 +7,13 @@
 			</Menu>
 			<Action name="copy"/>
 			<Merge/>
+			<Action name="mark_block"/>
+			<Action name="unmark_block"/>
 		</Menu>
 		<Menu name="run"><text>&Run</text>
 			<Action name="run_line"/>
 			<Action name="run_selection"/>
+			<Action name="run_block"/>
 			<Action name="run_all"/>
 			<Separator/>
 		</Menu>


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the rkward-tracker mailing list