diff --git a/src/nailab/templates/__init__.py b/src/nailab/templates/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/src/nailab/templates/new_strategy.py b/src/nailab/templates/new_strategy.py
new file mode 100644
index 0000000..82571ea
--- /dev/null
+++ b/src/nailab/templates/new_strategy.py
@@ -0,0 +1,16 @@
+
+new_strategy_template = '''
+from naiback.strategy import Strategy
+
+class MyStrategy(Strategy):
+
+ def __init__(self):
+ super().__init__()
+
+ def execute(self):
+ for i in self.bars.index[200:]:
+ if self.last_position_is_active():
+ pass
+ else:
+ pass
+'''
diff --git a/src/nailab/ui/applicationwindow.py b/src/nailab/ui/applicationwindow.py
index 4bf42ee..006b2a3 100644
--- a/src/nailab/ui/applicationwindow.py
+++ b/src/nailab/ui/applicationwindow.py
@@ -4,6 +4,7 @@ from gi.repository import Gtk, GtkSource
from nailab.data.datasource import DataSource
from nailab.data.datasourcemanager import DataSourceManager
from nailab.execution.executor import Executor
+from nailab.templates.new_strategy import new_strategy_template
from .resultstable import ResultsTableWidget
from .tabmanager import TabManager
@@ -18,6 +19,7 @@ class ApplicationWindow:
handlers = {
'on_ApplicationWindow_delete_event' : Gtk.main_quit,
+ 'on_menuNew' : self.new_file,
'on_OpenFile' : self.open_file,
'on_menuSave' : self.save_file,
'on_menuSaveAs' : self.save_file_as,
@@ -27,6 +29,9 @@ class ApplicationWindow:
builder.connect_signals(handlers)
self.window.show_all()
+ def new_file(self, arg):
+ self.tab_manager.new_from_template(new_strategy_template)
+
def open_file(self, arg):
dlg = Gtk.FileChooserDialog('Open file', self.window, Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
diff --git a/src/nailab/ui/tabmanager.py b/src/nailab/ui/tabmanager.py
index f22f669..8aaaba9 100644
--- a/src/nailab/ui/tabmanager.py
+++ b/src/nailab/ui/tabmanager.py
@@ -34,31 +34,43 @@ class TabManager(GObject.Object):
index = self.notebook.append_page(widget, header)
self.notebook.set_current_page(index)
+ def _read_source_file(self, source_file):
+ if source_file is not None:
+ with open(source_file, 'r') as f:
+ return f.read()
+ else:
+ return None
+
+
def new_tab(self, source_file):
- with open(source_file, 'r') as f:
- tab_id = self._next_tab_id()
- self.source_paths[tab_id] = source_file
- (sv, sv_controller) = self._init_sourceeditor()
+ tab_id = self._next_tab_id()
+ (sv, sv_controller) = self._init_sourceeditor()
+
+ source = self._read_source_file(source_file)
+ if source is not None:
sv_controller.set_source_text(f.read())
- self.source_controllers[tab_id] = sv_controller
- self.widgets[tab_id] = sv
- sv.show_all()
- header = Gtk.HBox()
- title_label = Gtk.Label(source_file)
- image = Gtk.Image()
- image.set_from_stock(Gtk.STOCK_CLOSE, Gtk.IconSize.MENU)
- close_button = Gtk.Button()
- close_button.set_image(image)
- close_button.set_relief(Gtk.ReliefStyle.NONE)
- close_button.connect('clicked', self.close_cb, tab_id)
-
- header.pack_start(title_label,
- expand=True, fill=True, padding=0)
- header.pack_end(close_button,
- expand=False, fill=False, padding=0)
- header.show_all()
- index = self.notebook.append_page(sv, header)
- self.notebook.set_current_page(index)
+
+ self.source_paths[tab_id] = source_file
+ self.source_controllers[tab_id] = sv_controller
+ self.widgets[tab_id] = sv
+ sv.show_all()
+ header = Gtk.HBox()
+ title_label = Gtk.Label(source_file)
+ image = Gtk.Image()
+ image.set_from_stock(Gtk.STOCK_CLOSE, Gtk.IconSize.MENU)
+ close_button = Gtk.Button()
+ close_button.set_image(image)
+ close_button.set_relief(Gtk.ReliefStyle.NONE)
+ close_button.connect('clicked', self.close_cb, tab_id)
+
+ header.pack_start(title_label,
+ expand=True, fill=True, padding=0)
+ header.pack_end(close_button,
+ expand=False, fill=False, padding=0)
+ header.show_all()
+ index = self.notebook.append_page(sv, header)
+ self.notebook.set_current_page(index)
+ return tab_id
def close_cb(self, arg, tab_id):
index = self._widget_num_by_tab_id(tab_id)
@@ -78,6 +90,10 @@ class TabManager(GObject.Object):
return None
+ def new_from_template(self, template):
+ tab_id = self.new_tab(None)
+ self.source_controllers[tab_id].set_source_text(template)
+
def save_current(self):
index = self.notebook.get_current_page()
w = self.notebook.get_nth_page(index)
diff --git a/ui/nailab.glade b/ui/nailab.glade
index ee02885..bc6553b 100644
--- a/ui/nailab.glade
+++ b/ui/nailab.glade
@@ -96,12 +96,13 @@
True
False
-