GstPipelineStudio/src/plugindialogs.rs

182 lines
6.3 KiB
Rust
Raw Normal View History

// plugindialogs.rs
//
// Copyright 2021 Stéphane Cerveau <scerveau@collabora.com>
//
// 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/>.
//
// SPDX-License-Identifier: GPL-3.0-only
use crate::app::GPSApp;
use crate::pipeline::ElementInfo;
use crate::pipeline::Pipeline;
use gtk::glib;
2021-11-23 17:04:57 +00:00
use gtk::prelude::*;
use gtk::TextBuffer;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use gtk::{
Box, Button, CellRendererText, Dialog, Entry, Label, ListStore, TextView, TreeView,
TreeViewColumn,
};
fn create_and_fill_model(elements: &[ElementInfo]) -> ListStore {
// Creation of a model with two rows.
let model = ListStore::new(&[u32::static_type(), String::static_type()]);
// Filling up the tree view.
for (i, entry) in elements.iter().enumerate() {
model.insert_with_values(
None,
&[(0, &(i as u32 + 1)), (1, &entry.name.as_ref().unwrap())],
);
}
2021-11-23 17:04:57 +00:00
model
}
fn append_column(tree: &TreeView, id: i32) {
let column = TreeViewColumn::new();
let cell = CellRendererText::new();
column.pack_start(&cell, true);
// Association of the view's column with the model's `id` column.
column.add_attribute(&cell, "text", id);
tree.append_column(&column);
}
pub fn display_plugin_list(app: &GPSApp, elements: &[ElementInfo]) {
let dialog: Dialog = app
.builder
.object("dialog-plugin-list")
.expect("Couldn't get window");
2021-11-23 17:04:57 +00:00
dialog.set_title(Some("Plugin list"));
dialog.set_default_size(640, 480);
2021-11-23 17:04:57 +00:00
let text_view: TextView = app
.builder
2021-11-23 17:04:57 +00:00
.object("textview-plugin-list")
.expect("Couldn't get window");
2021-11-23 17:04:57 +00:00
let text_buffer: TextBuffer = text_view.buffer();
2021-11-23 17:04:57 +00:00
let tree: TreeView = app
.builder
2021-11-23 17:04:57 +00:00
.object("treeview-plugin-list")
.expect("Couldn't get window");
if tree.n_columns() < 2 {
append_column(&tree, 0);
append_column(&tree, 1);
}
2021-11-23 17:04:57 +00:00
tree.set_search_column(1);
let model = create_and_fill_model(elements);
// Setting the model into the view.
tree.set_model(Some(&model));
// The closure responds to selection changes by connection to "::cursor-changed" signal,
// that gets emitted when the cursor moves (focus changes).
2021-11-23 17:04:57 +00:00
tree.connect_cursor_changed(glib::clone!(@weak dialog, @weak text_buffer => move |tree_view| {
let selection = tree_view.selection();
if let Some((model, iter)) = selection.selected() {
let element_name = model
2021-11-23 17:04:57 +00:00
.get(&iter, 1)
.get::<String>()
.expect("Treeview selection, column 1");
let description = Pipeline::element_description(&element_name).expect("Unable to get element list from GStreamer");
text_buffer.set_text("");
text_buffer.insert_markup(&mut text_buffer.end_iter(), &description);
}
}));
let app_weak = app.downgrade();
tree.connect_row_activated(
2021-11-23 17:04:57 +00:00
glib::clone!(@weak dialog => move |tree_view, _tree_path, _tree_column| {
let app = upgrade_weak!(app_weak);
let selection = tree_view.selection();
if let Some((model, iter)) = selection.selected() {
// Now getting back the values from the row corresponding to the
// iterator `iter`.
//
let element_name = model
2021-11-23 17:04:57 +00:00
.get(&iter, 1)
.get::<String>()
.expect("Treeview selection, column 1");
println!("{}", element_name);
app.add_new_element(element_name);
}
}),
);
2021-11-23 17:04:57 +00:00
dialog.show();
}
pub fn display_plugin_properties(app: &GPSApp, element_name: &str, node_id: u32) {
let dialog: Dialog = app
.builder
.object("dialog-plugin-properties")
.expect("Couldn't get window");
dialog.set_title(Some(&format!("{} properties", element_name)));
dialog.set_default_size(640, 480);
dialog.set_modal(true);
let properties_box: Box = app
.builder
.object("box-plugin-properties")
.expect("Couldn't get window");
let update_properties: Rc<RefCell<HashMap<String, String>>> =
Rc::new(RefCell::new(HashMap::new()));
let properties = Pipeline::element_properties(element_name).unwrap();
for (name, value) in properties {
let entry_box = Box::new(gtk::Orientation::Horizontal, 6);
let label = Label::new(Some(&name));
label.set_hexpand(true);
label.set_halign(gtk::Align::Start);
label.set_margin_start(4);
entry_box.append(&label);
let entry: Entry = Entry::new();
entry.set_text(&value);
entry.set_hexpand(true);
entry.set_halign(gtk::Align::Start);
entry.set_widget_name(&name);
entry.connect_changed(
glib::clone!(@weak entry, @strong update_properties => move |_| {
println!("{}:{}", entry.widget_name(), entry.text());
update_properties.borrow_mut().insert(entry.widget_name().to_string(), entry.text().to_string());
}),
);
entry_box.append(&entry);
properties_box.append(&entry_box);
}
let properties_apply_btn: Button = app
.builder
.object("apply-plugin-properties")
.expect("Couldn't get window");
let app_weak = app.downgrade();
properties_apply_btn.connect_clicked(
glib::clone!(@strong update_properties, @weak dialog => move |_| {
let app = upgrade_weak!(app_weak);
app.update_element_properties(node_id, &update_properties.borrow());
dialog.close();
}),
);
dialog.show();
for p in update_properties.borrow().values() {
println!("updated properties {}", p);
}
}