Skip to content

Commit

Permalink
Fix delete and add show archived mode
Browse files Browse the repository at this point in the history
  • Loading branch information
amka committed Jun 28, 2020
1 parent 1ad493f commit 4593b88
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
12 changes: 12 additions & 0 deletions norka/services/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,17 @@ def update(self, doc_id: int, data: dict) -> bool:

return True

def delete(self, doc_id: int) -> bool:
query = f"DELETE FROM documents WHERE id=?"

try:
self.conn.execute(query, (doc_id,))
self.conn.commit()
except Exception as e:
Logger.error(e)
return False

return True


storage = Storage()
20 changes: 11 additions & 9 deletions norka/widgets/document_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(self):

self.model = Gtk.ListStore(Pixbuf, str, str, int)

self.show_archived = False
self.selected_path = None
self.selected_document = None

Expand Down Expand Up @@ -74,17 +75,18 @@ def __init__(self):

self.add(scrolled)

def reload_items(self, sender: Gtk.Widget = None):
def reload_items(self, sender: Gtk.Widget = None) -> None:
self.model.clear()
for document in storage.all():
for document in storage.all(self.show_archived):
# icon = Gtk.IconTheme.get_default().load_icon('text-x-generic', 64, 0)
icon = self.gen_preview(document.content[:200], size=8)
opacity = 0.2 if document.archived else 1
icon = self.gen_preview(document.content[:200], opacity=opacity)
self.model.append([icon, document.title, document.content, document._id])

if self.selected_path:
self.view.select_path(self.selected_path)

def gen_preview(self, text, size=12) -> Pixbuf:
def gen_preview(self, text, size=12, opacity=1) -> Pixbuf:
pix = Pixbuf.new(Colorspace.RGB, True, 8, 60, 80)
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, pix.get_width(), pix.get_height())
context = cairo.Context(surface)
Expand All @@ -99,24 +101,24 @@ def gen_preview(self, text, size=12) -> Pixbuf:
grad.add_color_stop_rgb(0, 0.95, 0.95, 0.95)
grad.add_color_stop_rgb(pix.get_height(), 0.93, 0.93, 0.93)
context.set_source(grad)
context.paint()
context.paint_with_alpha(opacity)

# Document Outline
grad = cairo.LinearGradient(0, 0, 0, pix.get_height())
grad.add_color_stop_rgb(0, 1, 1, 1)
grad.add_color_stop_rgb(pix.get_height(), 0.94, 0.94, 0.94)
grad.add_color_stop_rgba(0, 1, 1, 1, opacity)
grad.add_color_stop_rgba(pix.get_height(), 0.94, 0.94, 0.94, opacity)
context.rectangle(1, 1, pix.get_width() - 2, pix.get_height() - 2)
context.set_source(grad)
context.stroke()

# Border
context.rectangle(0, 0, pix.get_width(), pix.get_height())
context.set_source_rgb(0.9, 0.9, 0.9)
context.set_source_rgba(0.9, 0.9, 0.9, opacity)
context.stroke()

# add the text
for num, line in enumerate(text.split('\n'), 1):
context.set_source_rgba(0.2, 0.2, 0.24, 1)
context.set_source_rgba(0.2, 0.2, 0.24, opacity)

# Fix to remove \r if it exists
if line.startswith('\r'):
Expand Down
8 changes: 8 additions & 0 deletions norka/widgets/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ def __init__(self, settings):
self.export_button.set_action_name('document.export')
self.export_button.set_visible(False)

self.archived_button = Gtk.ToggleButton()
self.archived_button.set_image(Gtk.Image.new_from_icon_name('user-trash', Gtk.IconSize.LARGE_TOOLBAR))
self.archived_button.set_tooltip_markup(Granite.markup_accel_tooltip(('<Control>h',), 'Show Archived files'))
self.archived_button.set_action_name('document.toggle_archived')
self.archived_button.set_visible(True)

self.menu_button = Gtk.MenuButton(tooltip_text="Menu")
self.menu_button.set_image(Gtk.Image.new_from_icon_name('open-menu', Gtk.IconSize.LARGE_TOOLBAR))
self.menu_button.set_popover(MenuPopover(settings=self.settings))
Expand All @@ -83,6 +89,7 @@ def __init__(self, settings):
self.pack_start(self.import_button)
self.pack_end(self.menu_button)
self.pack_end(self.export_button)
self.pack_end(self.archived_button)
# self.pack_end(self.search_button)

def toggle_document_mode(self) -> None:
Expand All @@ -97,6 +104,7 @@ def toggle_document_mode(self) -> None:
self.export_button.set_visible(self.document_mode_active)
self.add_button.set_visible(not self.document_mode_active)
self.import_button.set_visible(not self.document_mode_active)
self.archived_button.set_visible(not self.document_mode_active)

def update_title(self, title: str = None) -> None:
self.set_title(title or APP_TITLE)
18 changes: 12 additions & 6 deletions norka/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class NorkaWindow(Gtk.ApplicationWindow):
def __init__(self, settings: Gio.Settings, **kwargs):
super().__init__(**kwargs)

self.set_default_icon(Pixbuf. new_from_resource_at_scale(
self.set_default_icon(Pixbuf.new_from_resource_at_scale(
'/com/github/tenderowl/norka/icons/com.github.tenderowl.norka.svg',
128, 128, True
))
Expand Down Expand Up @@ -181,6 +181,11 @@ def init_actions(self) -> None:
'action': self.on_text_search_backward,
'accels': ('<Control><Shift>g',)
},
{
'name': 'toggle_archived',
'action': self.on_toggle_archive,
'accels': ('<Control>h',)
},
]
}

Expand Down Expand Up @@ -331,7 +336,7 @@ def import_document(self, file_path: str) -> bool:
_doc = Document(title=filename, content='\r\n'.join(lines))
_doc_id = storage.add(_doc)

self.document_grid.reload_items(self)
self.document_grid.reload_items()
return True
except Exception as e:
print(e)
Expand Down Expand Up @@ -396,10 +401,7 @@ def on_document_delete_activated(self, sender: Gtk.Widget = None, event=None) ->
result = prompt.run()
prompt.destroy()

if result == Gtk.ResponseType.APPLY and storage.update(
doc_id=doc._id,
data={'archived': True}
):
if result == Gtk.ResponseType.APPLY and storage.delete(doc._id):
self.document_grid.reload_items()
self.check_documents_count()

Expand Down Expand Up @@ -529,3 +531,7 @@ def get_current_font(self) -> str:
def get_current_font_size(self) -> float:
font = self.settings.get_string("font")
return float(font[font.rfind(" ") + 1:])

def on_toggle_archive(self, sender: Gio.SimpleAction, name: str = None):
self.document_grid.show_archived = self.header.archived_button.get_active()
self.document_grid.reload_items()

0 comments on commit 4593b88

Please sign in to comment.