Commit 45d71dcb authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add functionality to add a custom view with the 'C' shortcut.

parent c261462b
...@@ -22,6 +22,7 @@ from topydo.Commands import get_subcommand ...@@ -22,6 +22,7 @@ from topydo.Commands import get_subcommand
from topydo.ui.CommandLineWidget import CommandLineWidget from topydo.ui.CommandLineWidget import CommandLineWidget
from topydo.ui.ConsoleWidget import ConsoleWidget from topydo.ui.ConsoleWidget import ConsoleWidget
from topydo.ui.TodoListWidget import TodoListWidget from topydo.ui.TodoListWidget import TodoListWidget
from topydo.ui.ViewWidget import ViewWidget
from topydo.lib.Config import config from topydo.lib.Config import config
from topydo.lib.Sorter import Sorter from topydo.lib.Sorter import Sorter
from topydo.lib import TodoFile from topydo.lib import TodoFile
...@@ -33,15 +34,32 @@ class UIApplication(CLIApplicationBase): ...@@ -33,15 +34,32 @@ class UIApplication(CLIApplicationBase):
def __init__(self): def __init__(self):
super(UIApplication, self).__init__() super(UIApplication, self).__init__()
self.todofile = TodoFile.TodoFile(config().todotxt())
self.todolist = TodoList.TodoList(self.todofile.read())
self.columns = urwid.Columns([], dividechars=0, min_width=COLUMN_WIDTH) self.columns = urwid.Columns([], dividechars=0, min_width=COLUMN_WIDTH)
self.commandline = CommandLineWidget(u('topydo> ')) self.commandline = CommandLineWidget(u('topydo> '))
# console widget
self.console = ConsoleWidget() self.console = ConsoleWidget()
urwid.connect_signal(self.commandline, 'blur', urwid.connect_signal(self.commandline, 'blur',
self._blur_commandline) self._blur_commandline)
urwid.connect_signal(self.commandline, 'execute_command', urwid.connect_signal(self.commandline, 'execute_command',
self._execute_handler) self._execute_handler)
urwid.connect_signal(self.console, 'close', self._hide_console)
def hide_console():
self._console_visible = False
urwid.connect_signal(self.console, 'close', hide_console)
# view widget
self.viewwidget = ViewWidget(self.todolist)
urwid.connect_signal(self.viewwidget, 'save', self._create_view)
def hide_viewwidget():
self._viewwidget_visible = False
urwid.connect_signal(self.viewwidget, 'close', hide_viewwidget)
self.mainwindow = urwid.Pile([ self.mainwindow = urwid.Pile([
('weight', 1, self.columns), ('weight', 1, self.columns),
...@@ -111,6 +129,10 @@ class UIApplication(CLIApplicationBase): ...@@ -111,6 +129,10 @@ class UIApplication(CLIApplicationBase):
if self.columns.focus_position > 0: if self.columns.focus_position > 0:
self.columns.focus_position -= 1 self.columns.focus_position -= 1
def _new_view(self):
self.viewwidget.reset()
self._viewwidget_visible = True
def _handle_input(self, p_input): def _handle_input(self, p_input):
dispatch = { dispatch = {
':': self._focus_commandline, ':': self._focus_commandline,
...@@ -120,6 +142,7 @@ class UIApplication(CLIApplicationBase): ...@@ -120,6 +142,7 @@ class UIApplication(CLIApplicationBase):
'h': self._focus_previous_column, 'h': self._focus_previous_column,
'right': self._focus_next_column, 'right': self._focus_next_column,
'l': self._focus_next_column, 'l': self._focus_next_column,
'C': self._new_view,
} }
try: try:
...@@ -128,6 +151,10 @@ class UIApplication(CLIApplicationBase): ...@@ -128,6 +151,10 @@ class UIApplication(CLIApplicationBase):
# the key is unknown, ignore # the key is unknown, ignore
pass pass
def _create_view(self):
self._add_column(self.viewwidget.view, self.viewwidget.title)
self._viewwidget_visible = False
def _add_column(self, p_view, p_title): def _add_column(self, p_view, p_title):
todolist = TodoListWidget(p_view, p_title) todolist = TodoListWidget(p_view, p_title)
no_output = lambda _: None no_output = lambda _: None
...@@ -144,22 +171,39 @@ class UIApplication(CLIApplicationBase): ...@@ -144,22 +171,39 @@ class UIApplication(CLIApplicationBase):
self.columns.contents.append(item) self.columns.contents.append(item)
self.columns.focus_position = len(self.columns.contents) - 1 self.columns.focus_position = len(self.columns.contents) - 1
def _show_console(self): @property
self.mainwindow.contents.append((self.console, ('pack', None))) def _console_visible(self):
self.mainwindow.focus_position = 2 contents = self.mainwindow.contents
return len(contents) == 3 and isinstance(contents[2][0], ConsoleWidget)
@_console_visible.setter
def _console_visible(self, p_enabled):
contents = self.mainwindow.contents
def _hide_console(self): if p_enabled == True and len(contents) == 2:
if self._console_is_visible(): contents.append((self.console, ('pack', None)))
self.mainwindow.focus_position = 2
elif p_enabled == False and self._console_visible:
self.console.clear() self.console.clear()
del self.mainwindow.contents[2] del contents[2]
def _console_is_visible(self): @property
return len(self.mainwindow.contents) == 3 def _viewwidget_visible(self):
contents = self.mainwindow.contents
return len(contents) == 3 and isinstance(contents[2][0], ViewWidget)
def _print_to_console(self, p_text): @_viewwidget_visible.setter
if not self._console_is_visible(): def _viewwidget_visible(self, p_enabled):
self._show_console() contents = self.mainwindow.contents
if p_enabled == True and len(contents) == 2:
contents.append((self.viewwidget, ('pack', None)))
self.mainwindow.focus_position = 2
elif p_enabled == False and self._viewwidget_visible:
del contents[2]
def _print_to_console(self, p_text):
self._console_visible = True
self.console.print_text(p_text) self.console.print_text(p_text)
def _input(self, p_question): def _input(self, p_question):
...@@ -171,14 +215,11 @@ class UIApplication(CLIApplicationBase): ...@@ -171,14 +215,11 @@ class UIApplication(CLIApplicationBase):
self.mainloop.draw_screen() self.mainloop.draw_screen()
user_input = self.mainloop.screen.get_input() user_input = self.mainloop.screen.get_input()
self._hide_console() self._console_visible = False
return user_input[0] return user_input[0]
def run(self): def run(self):
self.todofile = TodoFile.TodoFile(config().todotxt())
self.todolist = TodoList.TodoList(self.todofile.read())
view1 = self.todolist.view(Sorter(), []) view1 = self.todolist.view(Sorter(), [])
self._add_column(view1, "View 1") self._add_column(view1, "View 1")
......
# Topydo - A todo.txt client written in Python.
# Copyright (C) 2015 Bram Schoenmakers <me@bramschoenmakers.nl>
#
# 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 3 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import urwid
from six import u
from topydo.lib.Filter import get_filter_list, DependencyFilter, RelevanceFilter
from topydo.lib.Sorter import Sorter
class ViewWidget(urwid.LineBox):
def __init__(self, p_todolist):
self._todolist = p_todolist
self.titleedit = urwid.Edit(u("Title: "), u(""))
self.sortedit = urwid.Edit(u("Sort expression: "), u(""))
self.filteredit = urwid.Edit(u("Filter expression: "), u(""))
group = []
self.relevantradio = urwid.RadioButton(group, u("Only show relevant todo items"), True)
self.allradio = urwid.RadioButton(group, u("Show all todo items"))
self.pile = urwid.Pile([
self.titleedit,
self.sortedit,
self.filteredit,
self.relevantradio,
self.allradio,
urwid.Button(u("Save"), lambda _: urwid.emit_signal(self, 'save')),
urwid.Button(u("Cancel"), lambda _: urwid.emit_signal(self, 'close')),
])
self.reset()
super(ViewWidget, self).__init__(self.pile)
urwid.register_signal(ViewWidget, ['save', 'close'])
@property
def view(self):
""" Returns a tuple (title, view). """
sorter = Sorter(self.sortedit.edit_text)
filters = []
if self.relevantradio.state == True:
filters.append(DependencyFilter(self._todolist))
filters.append(RelevanceFilter())
filters += get_filter_list(self.filteredit.edit_text.split())
return self._todolist.view(sorter, filters)
@view.setter
def view(self, p_view):
pass # TODO
@property
def title(self):
return self.titleedit.edit_text
@title.setter
def title(self, p_title):
self.titleedit.edit_text = p_title
def reset(self):
""" Resets the form. """
self.titleedit.set_edit_text(u(""))
self.sortedit.set_edit_text(u(""))
self.filteredit.set_edit_text(u(""))
self.relevantradio.set_state(True)
self.pile.focus_item = 0
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