Skip to content

Commit

Permalink
Add new sidebar to main window
Browse files Browse the repository at this point in the history
This also includes changes to the tag editor and tag menu context. It
also adds a new context menu and editor for saved searches.
  • Loading branch information
diegogangl committed Jun 24, 2022
1 parent ab25129 commit e14856d
Show file tree
Hide file tree
Showing 12 changed files with 1,115 additions and 89 deletions.
14 changes: 14 additions & 0 deletions GTG/gtk/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from GTG.core.dates import Date
from GTG.gtk.backends import BackendsDialog
from GTG.gtk.browser.tag_editor import TagEditor
from GTG.gtk.browser.search_editor import SearchEditor
from GTG.core.timer import Timer
from GTG.gtk.errorhandler import do_error_dialog

Expand Down Expand Up @@ -513,6 +514,19 @@ def open_tag_editor(self, tag):
self.edit_tag_dialog.set_transient_for(self.browser)
self.edit_tag_dialog.insert_action_group('app', self)


def open_search_editor(self, search):
"""Open Saved search editor dialog."""

self.edit_search_dialog = SearchEditor(self.req, self, search)
self.edit_search_dialog.set_transient_for(self.browser)
self.edit_search_dialog.insert_action_group('app', self)

def close_search_editor(self):
"""Close search editor dialog."""

self.edit_search_dialog = None

def close_tag_editor(self):
"""Close tag editor dialog."""

Expand Down
1 change: 1 addition & 0 deletions GTG/gtk/browser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ class GnomeConfig():
MENUS_UI_FILE = os.path.join(data, "context_menus.ui")
MODIFYTAGS_UI_FILE = os.path.join(data, "modify_tags.ui")
TAG_EDITOR_UI_FILE = os.path.join(data, "tag_editor.ui")
SEARCH_EDITOR_UI_FILE = os.path.join(data, "search_editor.ui")
21 changes: 8 additions & 13 deletions GTG/gtk/browser/delete_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
from gettext import gettext as _, ngettext


class DeleteTagsDialog():
class DeleteTagsDialog:

MAXIMUM_TAGS_TO_SHOW = 5

def __init__(self, req, browser):
self.req = req
def __init__(self, browser):
self.browser = browser
self.tags_todelete = []

Expand All @@ -37,16 +36,12 @@ def on_delete_confirm(self):
otherwise, we will look which tid is selected"""

for tag in self.tags_todelete:
self.req.delete_tag(tag)

# TODO: New Core
the_tag = self.browser.app.ds.tags.find(tag)
tasks = self.browser.app.ds.tasks.filter(Filter.TAG, the_tag)
tasks = self.browser.app.ds.tasks.filter(Filter.TAG, tag)

for t in tasks:
t.remove_tag(tag)
t.remove_tag(tag.name)

self.browser.app.ds.tags.remove(the_tag.id)
self.browser.app.ds.tags.remove(tag.id)

self.tags_todelete = []

Expand All @@ -61,7 +56,7 @@ def on_response(self, dialog, response, tagslist, callback):
if callback:
callback(tagslist)

def delete_tags_async(self, tags=None, callback=None):
def show(self, tags=None, callback=None):
self.tags_todelete = tags or self.tags_todelete

if not self.tags_todelete:
Expand Down Expand Up @@ -97,9 +92,9 @@ def delete_tags_async(self, tags=None, callback=None):

if len(tagslist) == 1:
# Don't show a bulleted list if there's only one item
titles = "".join(tag for tag in tagslist)
titles = "".join(tag.name for tag in tagslist)
else:
titles = "".join("\n• " + tag for tag in tagslist)
titles = "".join("\n• " + tag.name for tag in tagslist)

# Build and run dialog
dialog = Gtk.MessageDialog(transient_for=self.browser, modal=True)
Expand Down
23 changes: 13 additions & 10 deletions GTG/gtk/browser/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
from GTG.gtk.browser.backend_infobar import BackendInfoBar
from GTG.gtk.browser.modify_tags import ModifyTagsDialog
from GTG.gtk.browser.delete_tag import DeleteTagsDialog
from GTG.gtk.browser.tag_context_menu import TagContextMenu
# from GTG.gtk.browser.tag_context_menu import TagContextMenu
from GTG.gtk.browser.sidebar import Sidebar
# from GTG.gtk.browser.treeview_factory import TreeviewFactory
from GTG.gtk.editor.calendar import GTGCalendar
from GTG.gtk.tag_completion import TagCompletion
Expand Down Expand Up @@ -113,6 +114,8 @@ def __init__(self, requester, app):
# Timeout handler for search
self.search_timeout = None

self.sidebar_container.set_child(Sidebar(app, app.ds))

# Treeviews handlers
# self.vtree_panes = {}
# self.tv_factory = TreeviewFactory(self.req, self.config)
Expand Down Expand Up @@ -281,7 +284,7 @@ def _init_ui_widget(self):
# self.modifytags_dialog.set_transient_for(self)
self.modifytags_dialog = None

self.deletetags_dialog = DeleteTagsDialog(self.req, self)
self.deletetags_dialog = DeleteTagsDialog(self)
self.calendar = GTGCalendar()
self.calendar.set_transient_for(self)
self.calendar.connect("date-changed", self.on_date_changed)
Expand Down Expand Up @@ -316,10 +319,10 @@ def init_tags_sidebar(self):
self.tagtreeview = self.tv_factory.tags_treeview(self.tagtree)
self.tagtreeview.get_selection().connect('changed', self.on_select_tag)

self.tagpopup = TagContextMenu(self.req, self.app)
self.tagpopup.set_parent(self.sidebar_container)
self.tagpopup.set_halign(Gtk.Align.START)
self.tagpopup.set_position(Gtk.PositionType.BOTTOM)
# self.tagpopup = TagContextMenu(self.req, self.app)
# self.tagpopup.set_parent(self.sidebar_container)
# self.tagpopup.set_halign(Gtk.Align.START)
# self.tagpopup.set_position(Gtk.PositionType.BOTTOM)

tagtree_gesture_single = Gtk.GestureSingle(button=Gdk.BUTTON_SECONDARY)
tagtree_gesture_single.connect('begin', self.on_tag_treeview_click_begin)
Expand Down Expand Up @@ -977,9 +980,9 @@ def on_tag_treeview_key_press_event(self, controller, keyval, keycode, state):
self.on_delete_tag_activate()
return True

def on_delete_tag_activate(self):
tags = self.get_selected_tags()
self.deletetags_dialog.delete_tags_async(tags)
def on_delete_tag_activate(self, tags=[]):
tags = tags or self.get_selected_tags()
self.deletetags_dialog.show(tags)

def on_delete_tag(self, event):
tags = self.get_selected_tags()
Expand Down Expand Up @@ -1786,4 +1789,4 @@ def expand_search_tag(self):
model = self.tagtreeview.get_model()
search_iter = model.my_get_iter((SEARCH_TAG, ))
search_path = model.get_path(search_iter)
self.tagtreeview.expand_row(search_path, False)
self.tagtreeview.expand_row(search_path, False)
243 changes: 243 additions & 0 deletions GTG/gtk/browser/search_editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
# -----------------------------------------------------------------------------
# Getting Things GNOME! - a personal organizer for the GNOME desktop
# Copyright (c) 2008-2013 - Lionel Dricot & Bertrand Rousseau
#
# 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/>.
# -----------------------------------------------------------------------------

"""
This module contains the SearchEditor class which is a window that allows the
user to edit a saved search properties.
"""
from gi.repository import GObject, Gtk, Gdk, GdkPixbuf, GLib

import logging
import random
from gettext import gettext as _
from GTG.gtk.browser import GnomeConfig

log = logging.getLogger(__name__)


@Gtk.Template(filename=GnomeConfig.SEARCH_EDITOR_UI_FILE)
class SearchEditor(Gtk.Dialog):
"""
A window to edit certain properties of a saved search.
"""

__gtype_name__ = 'GTG_SearchEditor'
_emoji_chooser = Gtk.Template.Child('emoji-chooser')
_icon_button = Gtk.Template.Child('icon-button')
_name_entry = Gtk.Template.Child('name-entry')

def __init__(self, req, app, search=None):
super().__init__(use_header_bar=1)

set_icon_shortcut = Gtk.Shortcut.new(
Gtk.ShortcutTrigger.parse_string("<Control>I"),
Gtk.CallbackAction.new(self._set_icon))

self.add_shortcut(set_icon_shortcut)

self.req = req
self.app = app
self.search = search

self.set_transient_for(app.browser)
self._title_format = self.get_title()
self._emoji_chooser.set_parent(self._icon_button)

self.is_valid = True
self._emoji = None
self.use_icon = False
self._search_name = ''

self.set_search(search)
self.show()


@GObject.Property(type=str, default='')
def search_name(self):
"""The (new) name of the search."""

return self._search_name

@search_name.setter
def search_name(self, value: str):
self._search_name = value
self._validate()

@GObject.Property(type=str, default='')
def search_query(self):
"""The (new) name of the search."""

return self._search_query

@search_query.setter
def search_query(self, value: str):
self._search_query = value
self._validate()

@GObject.Property(type=bool, default=True)
def is_valid(self):
"""
Whenever it is valid to apply the changes (like malformed search name).
"""

return self._is_valid

@is_valid.setter
def is_valid(self, value: bool):
self._is_valid = value

@GObject.Property(type=bool, default=False)
def has_icon(self):
"""
Whenever the search will have an icon.
"""

return bool(self._emoji)

def _validate(self):
"""
Validates the current search preferences.
Returns true whenever it passes validation, False otherwise,
and modifies the is_valid property appropriately.
On failure, the widgets are modified accordingly to show the user
why it doesn't accept it.
"""

valid = True
valid &= self._validate_search_name()
self.is_valid = valid
return valid

def _validate_search_name(self):
"""
Validates the current search name.
Returns true whenever it passes validation, False otherwise.
On failure, the widgets are modified accordingly to show the user
why it doesn't accept it.
"""

if self.search_name == '':
self._name_entry.add_css_class("error")
self._name_entry.props.tooltip_text = \
_("search name can not be empty")
return False
else:
self._name_entry.remove_css_class("error")
self._name_entry.props.tooltip_text = ""
return True

def set_search(self, search):
"""
Set the search to edit.
Widgets are updated with the information of the search,
the previous information/state is lost.
"""
self.search = search
if search is None:
return

icon = search.icon
self._set_emoji(self._emoji_chooser, text=icon if icon else '')

self.search_name = search.name
self.search_query = search.query
self.set_title(self._title_format % self.search_name)


def do_destroy(self):

self.app.close_search_editor()
super().destroy()

def _cancel(self):
"""
Cancel button has been clicked, closing the editor window without
applying changes.
"""

self.destroy()

def _apply(self):
"""
Apply button has been clicked, applying the settings and closing the
editor window.
"""
if self.search is None:
log.warning("Trying to apply but no search set, shouldn't happen")
self._cancel()
return

if self.has_icon and self._emoji:
self.search.icon = self._emoji
elif self.has_icon: # Should never happen, but just in case
log.warning("Tried to set icon for %r but no icon given",
self.search.name)
self.search.icon = None
else:
self.search.icon = None

if self.search_name != self.search.name:
log.debug("Renaming %r → %r", self.search.name, self.search_name)
self.search.name = self.search_name

self.app.ds.saved_searches.model.emit('items-changed', 0, 0, 0)
self.destroy()

# CALLBACKS #####
@Gtk.Template.Callback('response')
def _response(self, widget: GObject.Object, response: Gtk.ResponseType):
if response == Gtk.ResponseType.APPLY:
self._apply()
else:
self._cancel()


@Gtk.Template.Callback('set_icon')
def _set_icon(self, widget: GObject.Object, shargs: GLib.Variant = None):
"""
Button to set the icon/emoji has been clicked.
"""
self._emoji_chooser.popup()

@Gtk.Template.Callback('emoji_set')
def _set_emoji(self, widget: GObject.Object, text: str = None):
"""
Callback when an emoji has been inserted.
"""
self._emoji = text if text else None

if text:
self._emoji = text
self._icon_button.set_label(text)
if label := self._icon_button.get_child():
label.set_opacity(1)
else:
self._emoji = None
self._icon_button.set_label('🏷️')
if label := self._icon_button.get_child():
label.set_opacity(0.4)

self.notify('has-icon')

@Gtk.Template.Callback('remove_icon')
def _remove_icon(self, widget: GObject.Object):
"""
Callback to remove the icon.
"""

self._set_emoji(self._emoji_chooser, text='')
Loading

0 comments on commit e14856d

Please sign in to comment.