Skip to content
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

Shorten paths and modernize imports #137

Merged
merged 2 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ Presentation Blog Post: https://woboq.com/blog/qmetaobject-from-rust.html
## Overview

```rust
#[macro_use] extern crate cstr;
extern crate qmetaobject;

use cstr::cstr;
use qmetaobject::*;

// The `QObject` custom derive macro allows to expose a class to Qt and QML
#[derive(QObject,Default)]
#[derive(QObject, Default)]
struct Greeter {
// Specify the base class with the qt_base_class macro
base: qt_base_class!(trait QObject),
Expand Down
1 change: 0 additions & 1 deletion examples/graph/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FO
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
extern crate cpp_build;

fn main() {
let qt_include_path = std::env::var("DEP_QT_INCLUDE_PATH").unwrap();
Expand Down
10 changes: 4 additions & 6 deletions examples/graph/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#![allow(non_snake_case)]
#![allow(unused_variables)]
#[macro_use]
extern crate qmetaobject;

use cpp::cpp;
use cstr::cstr;

use qmetaobject::scenegraph::*;
use qmetaobject::*;
#[macro_use]
extern crate cstr;
#[macro_use]
extern crate cpp;

mod nodes;

Expand Down
5 changes: 4 additions & 1 deletion examples/graph/src/nodes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use cpp::cpp;

use qmetaobject::scenegraph::SGNode;
use qmetaobject::{QColor, QQuickItem, QRectF};
use qmetaobject::{qrc, QQuickItem};
use qttypes::{QColor, QRectF};

qrc! {
init_resource,
Expand Down
3 changes: 2 additions & 1 deletion examples/qmlextensionplugins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ name = "qmlqtimeexampleplugin"
crate-type = ["cdylib"]

[dependencies]
qmetaobject = { path = "../../qmetaobject"}
qmetaobject = { path = "../../qmetaobject" }
chrono = "^0.4"
cstr = "0.2"
21 changes: 9 additions & 12 deletions examples/qmlextensionplugins/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
extern crate qmetaobject;
use qmetaobject::*;
extern crate chrono;
use chrono::Timelike;
use std::ffi::CStr;
use std::sync::{Arc, Condvar, Mutex};
use std::thread::JoinHandle;

use chrono::Timelike;
use cstr::cstr;

use qmetaobject::*;

#[derive(Default)]
struct AbortCondVar {
is_aborted: Mutex<bool>,
Expand Down Expand Up @@ -75,13 +77,8 @@ struct QExampleQmlPlugin {
}

impl QQmlExtensionPlugin for QExampleQmlPlugin {
fn register_types(&mut self, uri: &std::ffi::CStr) {
//assert_eq!(uri, std::ffi::CStr::from_bytes_with_nul(b"TimeExample\0"));
qml_register_type::<TimeModel>(
uri,
1,
0,
std::ffi::CStr::from_bytes_with_nul(b"Time\0").unwrap(),
);
fn register_types(&mut self, uri: &CStr) {
//assert_eq!(uri, cstr!("TimeExample"));
qml_register_type::<TimeModel>(uri, 1, 0, cstr!("Time"));
}
}
3 changes: 2 additions & 1 deletion examples/todos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ edition = "2018"
authors = ["Olivier Goffart <ogoffart@woboq.com>"]

[dependencies]
qmetaobject = { path = "../../qmetaobject"}
qmetaobject = { path = "../../qmetaobject" }
cstr = "0.2"
3 changes: 2 additions & 1 deletion examples/todos/src/implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use qmetaobject::*;
use std::collections::HashMap;

use qmetaobject::*;

#[derive(Default, Clone)]
struct TodosItem {
completed: bool,
Expand Down
12 changes: 3 additions & 9 deletions examples/todos/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
extern crate qmetaobject;
use qmetaobject::*;
use cstr::cstr;

use std::ffi::CStr;
use qmetaobject::*;

mod implementation;

Expand All @@ -13,12 +12,7 @@ qrc!(my_resource,

fn main() {
my_resource();
qml_register_type::<implementation::Todos>(
CStr::from_bytes_with_nul(b"RustCode\0").unwrap(),
1,
0,
CStr::from_bytes_with_nul(b"Todos\0").unwrap(),
);
qml_register_type::<implementation::Todos>(cstr!("RustCode"), 1, 0, cstr!("Todos"));
let mut engine = QmlEngine::new();
engine.load_file("qrc:/todos/qml/main.qml".into());
engine.exec();
Expand Down
1 change: 0 additions & 1 deletion examples/webengine/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
extern crate qmetaobject;
use qmetaobject::*;

qrc!(my_resource,
Expand Down
7 changes: 5 additions & 2 deletions qmetaobject/src/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#![deny(missing_docs)]
use std::os::raw::c_void;

use cpp::{cpp, cpp_class};

use super::*;

/// Inner functor type of a `QRustClosureSlotObject` class.
Expand Down Expand Up @@ -282,8 +284,9 @@ impl<Args> Signal<Args> {
/// # Example
///
/// ```
/// # #[macro_use] extern crate cpp;
/// # use qmetaobject::*;
/// use cpp::cpp;
/// use qmetaobject::*;
///
/// fn object_name_changed() -> Signal<fn(QString)> {
/// unsafe {
/// Signal::new(cpp!([] -> SignalInner as "SignalInner" {
Expand Down
9 changes: 6 additions & 3 deletions qmetaobject/src/future.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::future::Future;
use std::mem::replace;
use std::os::raw::c_void;
use std::pin::Pin;
use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};

use cpp::cpp;

use crate::connections::SignalArgArrayToTuple;

static QT_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(
Expand Down Expand Up @@ -90,7 +93,7 @@ cpp! {{

~Waker() {
rust!(QtDestroyFuture [future: *mut dyn Future<Output = ()> as "TraitObject"] {
std::mem::drop(Box::from_raw(future))
drop(Box::from_raw(future));
});
}
};
Expand Down Expand Up @@ -160,7 +163,7 @@ pub unsafe fn wait_on_signal<Args: SignalArgArrayToTuple>(
type Output = <Args as SignalArgArrayToTuple>::Tuple;
fn poll(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Self::Output> {
let state = &mut self.0;
*state = match std::mem::replace(state, ConnectionFutureState::Invalid) {
*state = match replace(state, ConnectionFutureState::Invalid) {
ConnectionFutureState::Finished { result } => {
return Poll::Ready(result);
}
Expand All @@ -181,7 +184,7 @@ pub unsafe fn wait_on_signal<Args: SignalArgArrayToTuple>(
for *mut ConnectionFutureState<Args>
{
unsafe fn apply(&mut self, a: *const *const c_void) {
if let ConnectionFutureState::Started { mut handle, waker } = std::mem::replace(
if let ConnectionFutureState::Started { mut handle, waker } = replace(
&mut **self,
ConnectionFutureState::Finished { result: Args::args_array_to_tuple(a) },
) {
Expand Down
4 changes: 3 additions & 1 deletion qmetaobject/src/itemmodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FO
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

use std::collections::HashMap;

use cpp::cpp;

use crate::*;

/// This trait allow to override a Qt QAbstractItemModel
Expand Down Expand Up @@ -58,6 +59,7 @@ pub trait QAbstractItemModel: QObject {
fn role_names(&self) -> HashMap<i32, QByteArray> {
HashMap::new()
}

/// Refer to the Qt documentation of QAbstractListModel::beginInsertRows
fn begin_insert_rows(&self, parent: QModelIndex, first: i32, last: i32) {
let obj = self.get_cpp_object();
Expand Down
Loading