Commit 75e25e0a authored by Kenny Meyer's avatar Kenny Meyer Committed by Johannes Schmid

Add Python wizard to class-gen plugin

parent 99f5afb0
......@@ -7,4 +7,4 @@ Interfaces=IAnjutaWizard
UserActivatable=no
[Wizard]
_Title=C++/GObject Class
_Title=Class Wizard
......@@ -199,8 +199,19 @@ cg_plugin_add_to_project (AnjutaClassGenPlugin *plugin,
result = g_list_length (added_files) == 2;
if (result)
{
*new_header_file = g_file_get_path((GFile *)added_files->data);
*new_source_file = g_file_get_path((GFile *)g_list_next (added_files)->data);
/*
* Check if we're dealing with a programming language not having header
* files.
*/
if (strcmp (header_file, source_file) == 0)
{
*new_source_file = g_file_get_path((GFile *)g_list_next (added_files)->data);
}
else
{
*new_header_file = g_file_get_path((GFile *)added_files->data);
*new_source_file = g_file_get_path((GFile *)g_list_next (added_files)->data);
}
}
g_list_foreach (added_files, (GFunc)g_object_unref, NULL);
......@@ -290,6 +301,7 @@ cg_plugin_generator_created_cb (CgGenerator *generator,
const gchar *header_file;
const gchar *source_file;
IAnjutaFileLoader *loader;
gboolean header_is_source = FALSE;
plugin = (AnjutaClassGenPlugin *) user_data;
header_file = cg_generator_get_header_destination (generator);
......@@ -298,14 +310,30 @@ cg_plugin_generator_created_cb (CgGenerator *generator,
loader = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
IAnjutaFileLoader, NULL);
/*
* Check if we're workign with a programming language that
* doesn't need header files. If yes, don't create two tabs,
* as the header and source will be the same file.
*/
if (g_strcmp0 (header_file, source_file) == 0)
{
header_is_source = TRUE;
}
if (cg_window_get_add_to_project (plugin->window))
{
GFile* header = g_file_new_for_path (header_file);
GFile* source = g_file_new_for_path (source_file);
IAnjutaProjectManager *manager;
ianjuta_file_loader_load (loader, header, FALSE, NULL);
ianjuta_file_loader_load (loader, source, FALSE, NULL);
if (header_is_source == TRUE)
{
ianjuta_file_loader_load (loader, source, FALSE, NULL);
}
else
{
ianjuta_file_loader_load (loader, header, FALSE, NULL);
ianjuta_file_loader_load (loader, source, FALSE, NULL);
}
if (cg_window_get_add_to_repository (plugin->window))
{
......@@ -315,8 +343,16 @@ cg_plugin_generator_created_cb (CgGenerator *generator,
manager = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell, IAnjutaProjectManager, NULL);
if (manager)
{
g_signal_emit_by_name (G_OBJECT (manager), "element_added", header);
g_signal_emit_by_name (G_OBJECT (manager), "element_added", source);
if (header_is_source == TRUE)
{
g_signal_emit_by_name (G_OBJECT (manager), "element_added", source);
}
else
{
g_signal_emit_by_name (G_OBJECT (manager), "element_added", header);
g_signal_emit_by_name (G_OBJECT (manager), "element_added", source);
}
}
g_object_unref (header);
......@@ -324,11 +360,18 @@ cg_plugin_generator_created_cb (CgGenerator *generator,
}
else
{
/* We do not just use ianjuta_file_leader_load here to ensure that
* the new documents are flagged as changed and no path is
* already set. */
cg_plugin_load (plugin, generator, header_file, NULL);
cg_plugin_load (plugin, generator, source_file, NULL);
if (header_is_source == TRUE)
{
/* We do not just use ianjuta_file_leader_load here to ensure that
* the new documents are flagged as changed and no path is
* already set. */
cg_plugin_load (plugin, generator, source_file, NULL);
}
else
{
cg_plugin_load (plugin, generator, header_file, NULL);
cg_plugin_load (plugin, generator, source_file, NULL);
}
}
g_object_unref (G_OBJECT (plugin->window));
......@@ -356,24 +399,24 @@ cg_plugin_window_response_cb (G_GNUC_UNUSED GtkDialog *dialog,
if (response_id == GTK_RESPONSE_ACCEPT)
{
if (cg_window_get_add_to_project (plugin->window))
{
result = cg_plugin_add_to_project (
plugin, cg_window_get_header_file (plugin->window),
cg_window_get_source_file (plugin->window),
&header_file, &source_file);
if (cg_window_get_add_to_project (plugin->window))
{
result = cg_plugin_add_to_project (
plugin, cg_window_get_header_file (plugin->window),
cg_window_get_source_file (plugin->window),
&header_file, &source_file);
}
else
{
header_file = g_build_filename (g_get_tmp_dir (),
cg_window_get_header_file (plugin->window), NULL);
source_file = g_build_filename (g_get_tmp_dir (),
cg_window_get_source_file (plugin->window), NULL);
header_file = g_build_filename (g_get_tmp_dir (),
cg_window_get_header_file (plugin->window), NULL);
source_file = g_build_filename (g_get_tmp_dir (),
cg_window_get_source_file (plugin->window), NULL);
result = TRUE;
result = TRUE;
}
if(result == TRUE)
if (result == TRUE)
{
values = cg_window_create_value_heap (plugin->window);
......
......@@ -4,7 +4,8 @@ clsgen_appdata_DATA = \
cc-header.tpl \
cc-source.tpl \
go-header.tpl \
go-source.tpl
go-source.tpl \
py-source.tpl
EXTRA_DIST = \
$(clsgen_appdata_DATA)
......
[+ AutoGen5 template
##
## py-source.tpl
## Copyright (C) 2010 Kenny Meyer <knny.myer@gmail.com>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU Library General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+]
# -*- Mode: Python; Coding: utf-8; tab-width: 4 -*-
#
# [+ProjectName+][+IF (=(get "Headings") "1")+]
# Copyright (C) [+AuthorName+] [+(shell "date +%Y")+] <[+AuthorEmail+]>[+ENDIF+]
#
[+CASE (get "License") +]
[+ == "BSD" +][+(bsd (get "ProjectName") (get "AuthorName") "# ")+]
[+ == "LGPL" +][+(lgpl (get "ProjectName") (get "AuthorName") "# ")+]
[+ == "GPL" +][+(gpl (get "ProjectName") "# ")+]
[+ESAC+]
class [+ClassName+]([+IF (not (=(get "BaseClass") ""))+][+BaseClass+][+ENDIF+]):[+
FOR Constvars +][+
IF (not (=(get "Name") "")) +]
[+Name+] = [+Value+][+
ENDIF +][+
ENDFOR +][+
FOR Methods +][+
IF (not (=(get "Name") "")) +]
def [+Name+][+Arguments+]:
pass
[+
ENDIF +][+
ENDFOR +]
......@@ -437,6 +437,65 @@ cg_transform_arguments (GHashTable *table,
}
}
/**
* Add a self reference to the arguments list, if necessary. Python only.
*/
void
cg_transform_python_arguments (GHashTable *table,
const gchar *index)
{
gchar *arg_res;
gchar *arguments;
size_t len;
arguments = g_hash_table_lookup (table, index);
arg_res = NULL;
if (arguments != NULL)
{
g_strstrip (arguments);
len = strlen (arguments);
/* Do nothing if the field was left empty */
if (len > 0)
{
if (arguments[0] != '(')
{
/* Check if self is in arguments. If yes,
* cg_transform_arguments will take care of the
* rest, if not then add the self argument. */
if (g_strcmp0 (arguments, "self") != 0)
{
g_hash_table_insert (table, (gpointer) index,
g_strdup_printf ("(self, %s)", arguments));
g_free (arg_res);
arg_res = NULL;
}
}
else
{
if (g_strcmp0 (arguments, "()") == 0)
{
g_hash_table_insert (table, (gpointer) index,
g_strdup ("(self)"));
g_free (arg_res);
arg_res = NULL;
}
}
}
else
{
g_hash_table_insert (table, (gpointer) index,
g_strdup_printf ("%s", "(self)"));
g_free (arg_res);
arg_res = NULL;
}
}
cg_transform_arguments (table, index, FALSE);
}
/* This function makes a valid C identifier out of a string. It does this
* by ignoring anything but digits, letters, hyphens and underscores. Digits
* at the beginning of the string are also ignored. Hpyhens are transformed
......
......@@ -69,6 +69,10 @@ cg_transform_arguments (GHashTable *table,
const gchar *index,
gboolean make_void);
void
cg_transform_python_arguments (GHashTable *table,
const gchar *index);
void
cg_transform_string_to_identifier (GHashTable *table,
const gchar *string_index,
......
......@@ -36,6 +36,8 @@
#define GO_HEADER_TEMPLATE PACKAGE_DATA_DIR"/class-templates/go-header.tpl"
#define GO_SOURCE_TEMPLATE PACKAGE_DATA_DIR"/class-templates/go-source.tpl"
#define PY_SOURCE_TEMPLATE PACKAGE_DATA_DIR"/class-templates/py-source.tpl"
typedef struct _CgWindowPrivate CgWindowPrivate;
struct _CgWindowPrivate
{
......@@ -46,6 +48,8 @@ struct _CgWindowPrivate
CgElementEditor *editor_go_members;
CgElementEditor *editor_go_properties;
CgElementEditor *editor_go_signals;
CgElementEditor *editor_py_methods;
CgElementEditor *editor_py_constvars;
CgValidator *validator;
};
......@@ -293,6 +297,35 @@ cg_window_validate_go (CgWindow *window)
GTK_ENTRY (gtk_builder_get_object (priv->bxml, "source_file")), NULL);
}
static void
cg_window_validate_py (CgWindow *window)
{
CgWindowPrivate *priv;
priv = CG_WINDOW_PRIVATE (window);
if (priv->validator != NULL) g_object_unref (G_OBJECT (priv->validator));
priv->validator = cg_validator_new (
GTK_WIDGET (gtk_builder_get_object (priv->bxml, "create_button")),
GTK_ENTRY (gtk_builder_get_object (priv->bxml, "py_name")), NULL);
}
static void
cg_window_header_file_entry_set_sensitive (gpointer user_data, gboolean sensitive)
{
CgWindow *window;
CgWindowPrivate *priv;
GtkWidget *file_header;
window = CG_WINDOW (user_data);
priv = CG_WINDOW_PRIVATE (window);
file_header = GTK_WIDGET (gtk_builder_get_object (priv->bxml, "header_file"));
gtk_widget_set_sensitive (file_header, sensitive);
}
static void
cg_window_top_notebook_switch_page_cb (G_GNUC_UNUSED GtkNotebook *notebook,
G_GNUC_UNUSED GtkWidget *page,
......@@ -305,11 +338,17 @@ cg_window_top_notebook_switch_page_cb (G_GNUC_UNUSED GtkNotebook *notebook,
switch(page_num)
{
case 0:
cg_window_header_file_entry_set_sensitive (user_data, TRUE);
cg_window_validate_cc (window);
break;
case 1:
cg_window_header_file_entry_set_sensitive (user_data, TRUE);
cg_window_validate_go (window);
break;
case 2: /* Python */
cg_window_header_file_entry_set_sensitive (user_data, FALSE);
cg_window_validate_py (window);
break;
default:
g_assert_not_reached ();
break;
......@@ -452,6 +491,36 @@ cg_window_go_name_changed_cb (GtkEntry *entry,
g_free (str_filesource);
}
static void
cg_window_py_name_changed_cb (GtkEntry *entry,
gpointer user_data)
{
CgWindow *window;
CgWindowPrivate *priv;
GtkWidget *file_header;
GtkWidget *file_source;
gchar* str_filebase;
gchar* str_filesource;
window = CG_WINDOW (user_data);
priv = CG_WINDOW_PRIVATE (window);
file_header = GTK_WIDGET (gtk_builder_get_object (priv->bxml, "header_file"));
file_source = GTK_WIDGET (gtk_builder_get_object (priv->bxml, "source_file"));
str_filebase = cg_window_class_name_to_file_name (
gtk_entry_get_text (GTK_ENTRY (entry)));
str_filesource = g_strconcat (str_filebase, ".py", NULL);
g_free (str_filebase);
gtk_entry_set_text (GTK_ENTRY (file_header), str_filesource);
gtk_entry_set_text (GTK_ENTRY (file_source), str_filesource);
g_free (str_filesource);
}
#if 0
static void
cg_window_associate_browse_button (GladeXML *xml,
......@@ -536,6 +605,23 @@ cg_window_set_builder (CgWindow *window,
_("Flags"), CG_ELEMENT_EDITOR_COLUMN_FLAGS, GO_SIGNAL_FLAGS,
_("Marshaller"), CG_ELEMENT_EDITOR_COLUMN_STRING);
priv->editor_py_methods = cg_element_editor_new (
GTK_TREE_VIEW (gtk_builder_get_object (priv->bxml, "py_methods")),
GTK_BUTTON (gtk_builder_get_object (priv->bxml, "py_methods_add")),
GTK_BUTTON (gtk_builder_get_object (priv->bxml, "py_methods_remove")),
2,
_("Name"), CG_ELEMENT_EDITOR_COLUMN_STRING,
_("Arguments"), CG_ELEMENT_EDITOR_COLUMN_ARGUMENTS);
priv->editor_py_constvars = cg_element_editor_new (
GTK_TREE_VIEW (gtk_builder_get_object (priv->bxml, "py_constvars")),
GTK_BUTTON (gtk_builder_get_object (priv->bxml, "py_constvars_add")),
GTK_BUTTON (gtk_builder_get_object (priv->bxml, "py_constvars_remove")),
2,
_("Name"), CG_ELEMENT_EDITOR_COLUMN_STRING,
_("Value"), CG_ELEMENT_EDITOR_COLUMN_STRING);
/* Active item property in glade cannot be set because no GtkTreeModel
* is assigned. */
gtk_combo_box_set_active (
......@@ -544,6 +630,9 @@ cg_window_set_builder (CgWindow *window,
gtk_combo_box_set_active (
GTK_COMBO_BOX (gtk_builder_get_object (priv->bxml, "cc_inheritance")),
0);
g_signal_connect (
G_OBJECT (gtk_builder_get_object (priv->bxml, "py_name")), "changed",
G_CALLBACK (cg_window_py_name_changed_cb), window);
/* This revalidates the appropriate validator */
g_signal_connect (
......@@ -668,6 +757,20 @@ cg_window_go_signals_transform_func (GHashTable *table,
cg_transform_flags (table, "Flags", GO_SIGNAL_FLAGS);
}
static void
cg_window_py_methods_transform_func (GHashTable *table,
G_GNUC_UNUSED gpointer user_data)
{
cg_transform_python_arguments (table, "Arguments");
}
static void
cg_window_py_constvars_transform_func (GHashTable *table,
G_GNUC_UNUSED gpointer user_data)
{
cg_transform_string (table, "Value");
}
#if 0
static gboolean
cg_window_scope_condition_func (const gchar **elements,
......@@ -715,6 +818,8 @@ cg_window_init (CgWindow *window)
priv->editor_go_members = NULL;
priv->editor_go_properties = NULL;
priv->editor_go_signals = NULL;
priv->editor_py_methods = NULL;
priv->editor_py_constvars = NULL;
priv->validator = NULL;
}
......@@ -736,6 +841,10 @@ cg_window_finalize (GObject *object)
g_object_unref (G_OBJECT (priv->editor_go_properties));
if (priv->editor_go_signals != NULL)
g_object_unref (G_OBJECT (priv->editor_go_signals));
if (priv->editor_py_methods != NULL)
g_object_unref (G_OBJECT (priv->editor_py_methods));
if (priv->editor_py_constvars != NULL)
g_object_unref (G_OBJECT (priv->editor_py_constvars));
if (priv->validator != NULL)
g_object_unref (G_OBJECT (priv->validator));
......@@ -995,6 +1104,21 @@ cg_window_create_value_heap (CgWindow *window)
"Flags", "Marshaller");
break;
case 2: /* Python */
cg_window_set_heap_value (window, values, G_TYPE_STRING,
"ClassName", "py_name");
cg_window_set_heap_value (window, values, G_TYPE_STRING,
"BaseClass", "py_base");
cg_window_set_heap_value (window, values, G_TYPE_BOOLEAN,
"Headings", "py_headings");
cg_element_editor_set_values (priv->editor_py_methods, "Methods", values,
cg_window_py_methods_transform_func,
window, "Name", "Arguments");
cg_element_editor_set_values (priv->editor_py_constvars, "Constvars",
values,
cg_window_py_constvars_transform_func,
window, "Name", "Value");
break;
default:
g_assert_not_reached ();
break;
......@@ -1045,6 +1169,8 @@ cg_window_get_header_template (CgWindow *window)
return CC_HEADER_TEMPLATE;
case 1:
return GO_HEADER_TEMPLATE;
case 2: /* Python */
return PY_SOURCE_TEMPLATE;
default:
g_assert_not_reached ();
return NULL;
......@@ -1082,6 +1208,8 @@ cg_window_get_source_template(CgWindow *window)
return CC_SOURCE_TEMPLATE;
case 1:
return GO_SOURCE_TEMPLATE;
case 2:
return PY_SOURCE_TEMPLATE;
default:
g_assert_not_reached ();
return NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment