简体   繁体   中英

How to use set_source_pixbuf in rust?

I have a code where i use set_source_pixbuf,

use gdk::prelude::*;
use gdk_pixbuf::{Colorspace, Pixbuf};
use gtk::prelude::*;
use std::sync::{Arc, Mutex};

use super::prelude::*;
mod control_panel;
use control_panel::setup_control_panel;

macro_rules! clone {
    (@param _) => ( _ );
    (@param $x:ident) => ( $x );
    ($($n:ident),+ => move || $body:expr) => (
        {
            $( let $n = $n.clone(); )+
                move || $body
        }
    );
    ($($n:ident),+ => move |$($p:tt),+| $body:expr) => (
        {
            $( let $n = $n.clone(); )+
                move |$(clone!(@param $p),)+| $body
        }
    );
}

pub fn process_key(controller: &Arc<Mutex<Controller>>, drawing_area: &gtk::DrawingArea, key: u16) {
    {
        let mut contr = controller.lock().unwrap();
        contr.process_key(key);
    }

    drawing_area.queue_draw();
}

pub fn build_ui(app: &gtk::Application) {
    let window = gtk::ApplicationWindow::new(app);
    window.set_title("Muscle");

    let glade_src = "config/control_window.glade";
    let builder = gtk::Builder::from_file(glade_src);
    let control_window: gtk::Window = builder.get_object("window").expect("Couldn't get window");
    control_window.set_application(Some(app));
    control_window.set_title("Control panel");

    let fixed = gtk::Fixed::new();
    let drawing_area = gtk::DrawingArea::new();
    fixed.add(&drawing_area);
    window.add(&fixed);
    drawing_area.set_size_request(constants::WIDTH as i32, constants::HEIGHT as i32);

    let Config {
        muscle_config: mconf,
        carcass_config: cconf,
    } = read_from_config();
    let muscle = Arc::new(Mutex::new(Muscle::new(
        mconf.radiuses,
        mconf.grow_mults,
        mconf.len,
    )));
    let carcass = Arc::new(Mutex::new(Carcass::new(
        cconf.data,
        cconf.thickness,
        mconf.len,
    )));
    let pixbuf = Pixbuf::new(
        Colorspace::Rgb,
        constants::HAS_ALPHA,
        constants::BITS_PER_COLOR,
        constants::WIDTH as i32,
        constants::HEIGHT as i32,
    )
    .unwrap();

    let mut controller = Controller::new(pixbuf.clone(), muscle, carcass);
    controller.update_pixbuf();
    let controller = Arc::new(Mutex::new(controller));

    drawing_area.connect_draw(clone!(pixbuf => move |_, context| {
        context.set_source_pixbuf(&pixbuf, 0_f64, 0_f64);
        context.paint();
        Inhibit(false)
    }));

    window.connect_key_press_event(clone!(controller, drawing_area => move |_, key| {
        process_key(&controller, &drawing_area, key.get_hardware_keycode());
        Inhibit(false)
    }));

    window.show_all();
    setup_control_panel(&builder, &controller, &drawing_area);
    control_window.show_all();
}

when i try to build this module it gives me an error:

better0fdead@better0fdead-To-be-filled-by-O-E-M:~/Downloads/bmstu-cg-course$ cargo run --release
warning: the cargo feature `edition2021` has been stabilized in the 1.56 release and is no longer necessary to be listed in the manifest
  See https://doc.rust-lang.org/nightly/cargo/reference/manifest.html#the-edition-field for more information about using this feature.
   Compiling course_g v0.1.0 (/home/better0fdead/Downloads/bmstu-cg-course)
error[E0599]: no method named `set_source_pixbuf` found for reference `&cairo::context::Context` in the current scope
  --> src/lib/ui.rs:80:17
   |
80 |         context.set_source_pixbuf(&pixbuf, 0_f64, 0_f64);
   |                 ^^^^^^^^^^^^^^^^^
   |
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
   |
1  | use gdk::cairo_interaction::GdkContextExt;
   |
help: there is an associated function with a similar name
   |
80 |         context.set_source_rgb(&pixbuf, 0_f64, 0_f64);
   |                 ~~~~~~~~~~~~~~

warning: unused import: `gdk::prelude`
 --> src/lib/ui.rs:1:5
  |
1 | use gdk::prelude::*;
  |     ^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

For more information about this error, try `rustc --explain E0599`.
warning: `course_g` (bin "course_g") generated 1 warning
error: could not compile `course_g` due to previous error; 1 warning emitted

i tried adding command: use gdk::cairo_interaction::GdkContextExt; to the file, which does not help me.

better0fdead@better0fdead-To-be-filled-by-O-E-M:~/Downloads/bmstu-cg-course$ cargo run --release
warning: the cargo feature `edition2021` has been stabilized in the 1.56 release and is no longer necessary to be listed in the manifest
  See https://doc.rust-lang.org/nightly/cargo/reference/manifest.html#the-edition-field for more information about using this feature.
   Compiling course_g v0.1.0 (/home/better0fdead/Downloads/bmstu-cg-course)
error[E0603]: module `cairo_interaction` is private
  --> src/lib/ui.rs:5:10
   |
5  | use gdk::cairo_interaction::GdkContextExt;
   |          ^^^^^^^^^^^^^^^^^ private module
   |
note: the module `cairo_interaction` is defined here
  --> /home/better0fdead/.cargo/registry/src/github.com-1ecc6299db9ec823/gdk-0.15.2/src/lib.rs:31:1
   |
31 | mod cairo_interaction;
   | ^^^^^^^^^^^^^^^^^^^^^^

error[E0599]: no method named `set_source_pixbuf` found for reference `&cairo::context::Context` in the current scope
  --> src/lib/ui.rs:81:17
   |
81 |         context.set_source_pixbuf(&pixbuf, 0_f64, 0_f64);
   |                 ^^^^^^^^^^^^^^^^^
   |
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
   |
1  | use gdk::cairo_interaction::GdkContextExt;
   |
help: there is an associated function with a similar name
   |
81 |         context.set_source_rgb(&pixbuf, 0_f64, 0_f64);
   |                 ~~~~~~~~~~~~~~

warning: unused import: `gdk::prelude`
 --> src/lib/ui.rs:1:5
  |
1 | use gdk::prelude::*;
  |     ^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

Some errors have detailed explanations: E0599, E0603.
For more information about an error, try `rustc --explain E0599`.
warning: `course_g` (bin "course_g") generated 1 warning
error: could not compile `course_g` due to 2 previous errors; 1 warning emitted
use gdk::prelude::GdkContextExt;

does not work aswell

better0fdead@better0fdead-To-be-filled-by-O-E-M:~/Downloads/bmstu-cg-course$ cargo run --release
warning: the cargo feature `edition2021` has been stabilized in the 1.56 release and is no longer necessary to be listed in the manifest
  See https://doc.rust-lang.org/nightly/cargo/reference/manifest.html#the-edition-field for more information about using this feature.
   Compiling course_g v0.1.0 (/home/better0fdead/Downloads/bmstu-cg-course)
error[E0599]: no method named `set_source_pixbuf` found for reference `&cairo::context::Context` in the current scope
  --> src/lib/ui.rs:80:17
   |
80 |         context.set_source_pixbuf(&pixbuf, 0_f64, 0_f64);
   |                 ^^^^^^^^^^^^^^^^^
   |
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
   |
1  | use gdk::cairo_interaction::GdkContextExt;
   |
help: there is an associated function with a similar name
   |
80 |         context.set_source_rgb(&pixbuf, 0_f64, 0_f64);
   |                 ~~~~~~~~~~~~~~

warning: unused import: `gdk::prelude`
 --> src/lib/ui.rs:1:5
  |
1 | use gdk::prelude::*;
  |     ^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `gdk::prelude::GdkContextExt`
 --> src/lib/ui.rs:5:5
  |
5 | use gdk::prelude::GdkContextExt;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0599`.
warning: `course_g` (bin "course_g") generated 2 warnings
error: could not compile `course_g` due to previous error; 2 warnings emitted

As the error message says, gdt::cairo_interaction is a private module, so we can't use it.

The documentation says GdkContentExt is exposed in the gdk::prelude module. So importing it from the module solves the problem.

use gdk::prelude::GdkContentExt;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM