-
Notifications
You must be signed in to change notification settings - Fork 15
Add GOM demo #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add GOM demo #200
Changes from 7 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
af01c9f
[draft]library: Add GOM demo
BharatAtbrat abaf1f1
fix: demo description and category
BharatAtbrat c6c4d8f
minor improvements and additions
BharatAtbrat 36eb59a
changes and improvements
BharatAtbrat 35b5e0b
minor fix: remove comma
BharatAtbrat ff6af5b
minor change: change description
BharatAtbrat e49caef
Merge branch 'main' into atbrat/gom_demo
sonnyp af231fd
changes and improvements:
BharatAtbrat 4f368bd
minor fixes
BharatAtbrat 28f2b7e
small fix:
BharatAtbrat 1a41877
cleanup: remove Gom folder
BharatAtbrat 69f6079
tweaks
sonnyp b9b6d37
Merge branch 'main' into atbrat/gom_demo
sonnyp 909c833
further simplifications
sonnyp 79ed734
f
sonnyp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using Gtk 4.0; | ||
using Adw 1; | ||
|
||
Adw.ToastOverlay overlay { | ||
Adw.StatusPage { | ||
title: _("GOM Demo"); | ||
BharatAtbrat marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
description: _("Simple database operations using GOM"); | ||
BharatAtbrat marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
Box { | ||
orientation: vertical; | ||
halign: center; | ||
spacing: 18; | ||
|
||
Box { | ||
styles [ | ||
"linked" | ||
] | ||
|
||
Entry text_entry { | ||
placeholder-text: _("Enter Text"); | ||
width-request: 300; | ||
} | ||
|
||
Button insert_button { | ||
label: _("Insert"); | ||
} | ||
} | ||
|
||
Box { | ||
orientation: horizontal; | ||
spacing: 18; | ||
|
||
Box { | ||
orientation: vertical; | ||
spacing: 12; | ||
|
||
Label { | ||
label: _("Search by Text"); | ||
} | ||
|
||
SearchEntry search_entry { | ||
search-delay: 100; | ||
placeholder-text: _("Enter Text"); | ||
width-request: 250; | ||
} | ||
} | ||
|
||
Box { | ||
spacing: 12; | ||
orientation: vertical; | ||
|
||
Label { | ||
label: _("Search by ID"); | ||
} | ||
|
||
SpinButton id_entry { | ||
halign: center; | ||
orientation: horizontal; | ||
wrap: false; | ||
climb-rate: 1; | ||
|
||
adjustment: Adjustment { | ||
step-increment: 1; | ||
value: 0; | ||
}; | ||
} | ||
} | ||
} | ||
|
||
Frame { | ||
ColumnView column_view { | ||
show-column-separators: true; | ||
|
||
ColumnViewColumn col1 { | ||
title: _("Text"); | ||
expand: true; | ||
|
||
factory: SignalListItemFactory {}; | ||
} | ||
|
||
ColumnViewColumn col2 { | ||
title: _("ID"); | ||
expand: true; | ||
|
||
factory: SignalListItemFactory {}; | ||
} | ||
} | ||
} | ||
|
||
Label result_label { | ||
wrap: true; | ||
width-request: 250; | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import GObject from "gi://GObject"; | ||
import Gom from "gi://Gom"; | ||
import Gtk from "gi://Gtk"; | ||
import Adw from "gi://Adw"; | ||
import GLib from "gi://GLib"; | ||
import Gio from "gi://Gio"; | ||
|
||
const ItemClass = GObject.registerClass( | ||
{ | ||
GTypeName: "Item", | ||
Properties: { | ||
id: GObject.ParamSpec.int( | ||
"id", | ||
"ID", | ||
"An ID", | ||
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT, | ||
0, | ||
GLib.MAXINT32, | ||
0, | ||
), | ||
url: GObject.ParamSpec.string( | ||
"url", | ||
"URL", | ||
"A URL", | ||
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT, | ||
"", | ||
), | ||
}, | ||
}, | ||
class ItemClass extends Gom.Resource { }, | ||
); | ||
|
||
let adapter, repository; | ||
|
||
function initDatabase() { | ||
adapter = new Gom.Adapter(); | ||
adapter.open_sync(":memory:"); | ||
sonnyp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
repository = new Gom.Repository({ adapter: adapter }); | ||
|
||
// Set up table and primary key | ||
ItemClass.set_table("items"); | ||
ItemClass.set_primary_key("id"); | ||
|
||
// Perform automatic migration | ||
repository.automatic_migrate_sync(1, [ItemClass]); | ||
} | ||
|
||
function closeDatabase() { | ||
sonnyp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (adapter) { | ||
adapter.close_sync(); | ||
} | ||
} | ||
|
||
function showToast(overlay, message) { | ||
const toast = new Adw.Toast({ | ||
title: message, | ||
timeout: 2, | ||
}); | ||
overlay.add_toast(toast); | ||
} | ||
|
||
initDatabase(); | ||
|
||
const text_entry = workbench.builder.get_object("text_entry"); | ||
const id_entry = workbench.builder.get_object("id_entry"); | ||
const insert_button = workbench.builder.get_object("insert_button"); | ||
const search_entry = workbench.builder.get_object("search_entry"); | ||
const result_label = workbench.builder.get_object("result_label"); | ||
const overlay = workbench.builder.get_object("overlay"); | ||
const data_model = new Gio.ListStore({ item_type: ItemClass }); | ||
const column_view = workbench.builder.get_object("column_view"); | ||
const col1 = workbench.builder.get_object("col1"); | ||
const col2 = workbench.builder.get_object("col2"); | ||
var count = 0; | ||
BharatAtbrat marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
insert_button.connect("clicked", () => { | ||
const url = text_entry.text; | ||
const item = new ItemClass({ repository: repository, url: url }); | ||
const success = item.save_sync(); | ||
|
||
if (success) { | ||
showToast(overlay, "Item inserted successfully"); | ||
data_model.append(item); | ||
id_entry.set_range(1, ++count); | ||
} else { | ||
showToast(overlay, "Failed to insert item"); | ||
} | ||
}); | ||
|
||
id_entry.connect("value-changed", () => { | ||
data_model.remove_all(); | ||
const id = parseInt(id_entry.text); | ||
const filter = Gom.Filter.new_eq(ItemClass, "id", id); | ||
const found_item = repository.find_one_sync(ItemClass, filter); | ||
if (found_item) { | ||
data_model.append(found_item); | ||
} else { | ||
result_label.label = "Item not found"; | ||
} | ||
}); | ||
|
||
search_entry.connect("search-changed", () => { | ||
data_model.remove_all(); | ||
const filter_text = search_entry.text.trim(); | ||
if (filter_text === "") { | ||
result_label.label = ""; | ||
return; | ||
} | ||
// Create a filter for Text matching | ||
const filter = Gom.Filter.new_glob(ItemClass, "url", `*${filter_text}*`); | ||
const filtered_items = repository.find_sync(ItemClass, filter); | ||
|
||
if (filtered_items && filtered_items.get_count() > 0) { | ||
filtered_items.fetch_async(0, filtered_items.get_count(), () => { | ||
for (let i = 0; i < filtered_items.get_count(); i++) { | ||
const item = filtered_items.get_index(i); | ||
if (item) data_model.append(item); | ||
} | ||
result_label.label = "Loaded successfully"; | ||
}); | ||
} else { | ||
result_label.label = "No matching items found"; | ||
} | ||
}); | ||
|
||
const factory_col1 = col1.factory; | ||
|
||
factory_col1.connect("setup", (_self, list_item) => { | ||
const label = new Gtk.Label({ | ||
margin_start: 12, | ||
margin_end: 12, | ||
}); | ||
list_item.set_child(label); | ||
}); | ||
|
||
factory_col1.connect("bind", (_self, list_item) => { | ||
const label_widget = list_item.get_child(); | ||
const model_item = list_item.get_item(); | ||
label_widget.label = model_item.url; | ||
}); | ||
|
||
const factory_col2 = col2.factory; | ||
factory_col2.connect("setup", (_self, list_item) => { | ||
const label = new Gtk.Label({ | ||
margin_start: 12, | ||
margin_end: 12, | ||
}); | ||
list_item.set_child(label); | ||
}); | ||
|
||
factory_col2.connect("bind", (_self, list_item) => { | ||
const label_widget = list_item.get_child(); | ||
const model_item = list_item.get_item(); | ||
label_widget.label = model_item.id.toString(); | ||
}); | ||
|
||
column_view.model = new Gtk.SingleSelection({ | ||
model: data_model, | ||
}); | ||
BharatAtbrat marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"category": "platform", | ||
"description": "Search, load and store GObjects in a SQLite database", | ||
BharatAtbrat marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
"panels": ["ui", "preview"], | ||
BharatAtbrat marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
"autorun": true | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.