Exemplo n.º 1
0
void HaiQTextEdit::process_key(QKeyEvent event) {
	if ((event.text()!="")&&(textCursor().selectedText().count()<=1)) { 
		///this is to avoid spreading of marker highlighting
		bool holdmodified=document()->isModified();
		QTextCharFormat format=currentCharFormat();
		format.clearBackground();
		setCurrentCharFormat(format);
		document()->setModified(holdmodified);
	}
	if ((event.key()==Qt::Key_Tab)||(event.key()==Qt::Key_Backtab)||(event.text()=="\t")) {
		process_tab(event);
	}
        else if (event.key()==Qt::Key_Insert && QApplication::keyboardModifiers() == Qt::NoModifier) {
		setOverwriteMode(!overwriteMode());
	}
	else if ((event.key()==Qt::Key_Home)&&((event.modifiers()==Qt::NoModifier)||(event.modifiers()==Qt::ShiftModifier))) {
		bool shift_key=(event.modifiers()==Qt::ShiftModifier);
		QTextCursor cursor=textCursor();
		int col=cursor.columnNumber();
		QString txt=cursor.block().text();
		if (shift_key)
			cursor.movePosition(QTextCursor::StartOfLine,QTextCursor::KeepAnchor);
		else
			cursor.movePosition(QTextCursor::StartOfLine);
		int ct=0;
		while ((ct<txt.count())&&((txt[ct]==' ')||(txt[ct]=='\t'))) {
			if (shift_key)
				cursor.movePosition(QTextCursor::Right,QTextCursor::KeepAnchor);
			else
				cursor.movePosition(QTextCursor::Right);
			ct++;
		}
		int col2=cursor.columnNumber();
		if (col2==col) { //moved nowhere
			if (shift_key)
				cursor.movePosition(QTextCursor::StartOfLine,QTextCursor::KeepAnchor);
			else
				cursor.movePosition(QTextCursor::StartOfLine);
		}
		setTextCursor(cursor);
	}
	else if (event.key()==Qt::Key_Backspace) {
		process_backspace();
		return;
	}
	else if ((event.key()==Qt::Key_Space)&&(event.modifiers()==Qt::ControlModifier)) {
		return; // don't process a space, because Ctrl+Space is for completion
	}
	else {
		QTextEdit::keyPressEvent(&event);
	}

	if ((event.key()==Qt::Key_Enter)||(event.key()==Qt::Key_Return)) {
		indent_to_previous_line();
		QTextCursor cursor=textCursor();
		do_marker_highlighting(cursor);
		QString line=cursor.block().previous().text();
		QStringList symbols=split_into_symbols(line);
		if ((symbols.count()>0)&&
		    ( (symbols[symbols.count()-1]=="{") || (((symbols[symbols.count()-1]!=";"))&&((symbols[0]=="if") || (symbols[0]=="else") || (symbols[0]=="while") || (symbols[0]=="do") || (symbols[0]=="for"))) ) 
		    ) {
			QString line2=cursor.block().text();
			QStringList symbols2=split_into_symbols(line2);
			if ((symbols2.count()==0)||(symbols2[0]!="}")) //only indent if there is not a "{" starting the line
				cursor.insertText(tab_text(cursor));
		}
		//kind of a hack
		if (!blocks_highlighted_for_braces.isEmpty()) {
			blocks_highlighted_for_braces << textCursor().block();
		}
	}
	if (event.text()=="}") {
		QTextCursor cursor=textCursor();
		QString line=cursor.block().text();
		QStringList symbols=split_into_symbols(line);
		if (symbols.count()==1) {
			//unindent to last brace
			QTextBlock block=cursor.block();
			bool found=false;
			int braces_level=1;
			while ((!found)&&(block.isValid())) {
				block=block.previous();
				QString line2=block.text();
				QStringList symbols=split_into_symbols(line2);
				
				for (int j=symbols.count()-1; j>=0; j--) {
					if (symbols[j]=="{")
						braces_level--;
					if (symbols[j]=="}")
						braces_level++;
					if (braces_level==0)
						found=true;
				}
			}
			if (found) {
				QString line2=block.text();
				
				for (int j=0; j<line.count(); j++)
					cursor.deletePreviousChar();
				int j=0;
				while ((j<line2.count())&&((line2[j]==' ')||(line2[j]=='\t'))) {
					cursor.insertText(QString(line2[j]));
					j++;
				}
				cursor.insertText("}");
			}
		}
	}
	if ((event.text()==")")||(event.text()=="}")/*||(event.text()==">")*/) {
		highlight_braces(event.text(),true);
	}
	if ((event.text()=="(")||(event.text()=="{")/*||(event.text()=="<")*/) {
		highlight_braces(event.text(),false);
	}
}
gboolean keypress_cb( GtkWidget *widget, GdkEventKey *event, gpointer data )
{
    gboolean wasConsumed = FALSE; /* default to report event not consumed */
    EgeAdjustmentAction* action = EGE_ADJUSTMENT_ACTION(data);
    guint key = 0;
    gdk_keymap_translate_keyboard_state( gdk_keymap_get_for_display( gdk_display_get_default() ),
                                         event->hardware_keycode, (GdkModifierType)event->state,
                                         0, &key, 0, 0, 0 );

    switch ( key ) {
        case GDK_Escape:
        {
            action->private_data->transferFocus = TRUE;
            gtk_spin_button_set_value( GTK_SPIN_BUTTON(widget), action->private_data->lastVal );
            ege_adjustment_action_defocus( action );
            wasConsumed = TRUE;
        }
        break;

        case GDK_Return:
        case GDK_KP_Enter:
        {
            action->private_data->transferFocus = TRUE;
            ege_adjustment_action_defocus( action );
            wasConsumed = TRUE;
        }
        break;

        case GDK_Tab:
        {
            action->private_data->transferFocus = FALSE;
            wasConsumed = process_tab( widget, 1 );
        }
        break;

        case GDK_ISO_Left_Tab:
        {
            action->private_data->transferFocus = FALSE;
            wasConsumed = process_tab( widget, -1 );
        }
        break;

        case GDK_Up:
        case GDK_KP_Up:
        {
            action->private_data->transferFocus = FALSE;
            gdouble val = gtk_spin_button_get_value( GTK_SPIN_BUTTON(widget) );
            gtk_spin_button_set_value( GTK_SPIN_BUTTON(widget), val + action->private_data->step );
            wasConsumed = TRUE;
        }
        break;

        case GDK_Down:
        case GDK_KP_Down:
        {
            action->private_data->transferFocus = FALSE;
            gdouble val = gtk_spin_button_get_value( GTK_SPIN_BUTTON(widget) );
            gtk_spin_button_set_value( GTK_SPIN_BUTTON(widget), val - action->private_data->step );
            wasConsumed = TRUE;
        }
        break;

        case GDK_Page_Up:
        case GDK_KP_Page_Up:
        {
            action->private_data->transferFocus = FALSE;
            gdouble val = gtk_spin_button_get_value( GTK_SPIN_BUTTON(widget) );
            gtk_spin_button_set_value( GTK_SPIN_BUTTON(widget), val + action->private_data->page );
            wasConsumed = TRUE;
        }
        break;

        case GDK_Page_Down:
        case GDK_KP_Page_Down:
        {
            action->private_data->transferFocus = FALSE;
            gdouble val = gtk_spin_button_get_value( GTK_SPIN_BUTTON(widget) );
            gtk_spin_button_set_value( GTK_SPIN_BUTTON(widget), val - action->private_data->page );
            wasConsumed = TRUE;
        }
        break;

        case GDK_z:
        case GDK_Z:
        {
            action->private_data->transferFocus = FALSE;
            gtk_spin_button_set_value( GTK_SPIN_BUTTON(widget), action->private_data->lastVal );
            wasConsumed = TRUE;
        }
        break;

    }

    return wasConsumed;
}
Exemplo n.º 3
0
void HaiQTextEdit::keyReleaseEvent ( QKeyEvent * event ) {
    if (event->key() == Qt::Key_Backtab) {
        process_tab(*event);
    }
}