From e8b515f8a2051b5d4509fa2d7fd3d41b79039714 Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Sun, 27 Feb 2022 00:07:44 +0530 Subject: [PATCH] Add methods for QStandardPathLocation. Also refactored QStandardPathLocation emum to seperate module in qtcore. --- qttypes/src/lib.rs | 32 +--------------- qttypes/src/qtcore/mod.rs | 2 + qttypes/src/qtcore/qstandardpaths.rs | 55 ++++++++++++++++++++++++++++ qttypes/src/qtcore/qstring.rs | 11 ++++++ 4 files changed, 70 insertions(+), 30 deletions(-) create mode 100644 qttypes/src/qtcore/qstandardpaths.rs diff --git a/qttypes/src/lib.rs b/qttypes/src/lib.rs index 781f6ece..951417c1 100644 --- a/qttypes/src/lib.rs +++ b/qttypes/src/lib.rs @@ -168,8 +168,8 @@ use internal_prelude::*; mod qtcore; pub use crate::qtcore::{ - qreal, NormalizationForm, QByteArray, QListIterator, QString, QStringList, QUrl, QVariantList, - UnicodeVersion, + qreal, NormalizationForm, QByteArray, QListIterator, QStandardPathLocation, QString, + QStringList, QUrl, QVariantList, UnicodeVersion, }; mod gui; @@ -1382,34 +1382,6 @@ impl QPen { // qreal widthF() const } -/// Bindings for [`QStandardPaths::StandardLocation`][enum] enum. -/// -/// [enum]: https://doc.qt.io/qt-5/qstandardpaths.html#StandardLocation-enum -#[repr(C)] -#[derive(Clone, Copy, PartialEq, Debug)] -#[allow(non_camel_case_types)] -pub enum QStandardPathLocation { - DesktopLocation = 0, - DocumentsLocation = 1, - FontsLocation = 2, - ApplicationsLocation = 3, - MusicLocation = 4, - MoviesLocation = 5, - PicturesLocation = 6, - TempLocation = 7, - HomeLocation = 8, - AppLocalDataLocation = 9, - CacheLocation = 10, - GenericDataLocation = 11, - RuntimeLocation = 12, - ConfigLocation = 13, - DownloadLocation = 14, - GenericCacheLocation = 15, - GenericConfigLocation = 16, - AppDataLocation = 17, - AppConfigLocation = 18, -} - /// Bindings for [`Qt::BrushStyle`][enum] enum. /// /// [enum]: https://doc.qt.io/qt-5/qt.html#BrushStyle-enum diff --git a/qttypes/src/qtcore/mod.rs b/qttypes/src/qtcore/mod.rs index 6805be1f..f4e9ac95 100644 --- a/qttypes/src/qtcore/mod.rs +++ b/qttypes/src/qtcore/mod.rs @@ -2,6 +2,7 @@ mod primitives; mod qbytearray; mod qchar; mod qlist; +mod qstandardpaths; mod qstring; mod qurl; @@ -9,5 +10,6 @@ pub use self::primitives::qreal; pub use self::qbytearray::QByteArray; pub use self::qchar::UnicodeVersion; pub use self::qlist::{QListIterator, QStringList, QVariantList}; +pub use self::qstandardpaths::QStandardPathLocation; pub use self::qstring::{NormalizationForm, QString}; pub use self::qurl::QUrl; diff --git a/qttypes/src/qtcore/qstandardpaths.rs b/qttypes/src/qtcore/qstandardpaths.rs new file mode 100644 index 00000000..9bcb4e56 --- /dev/null +++ b/qttypes/src/qtcore/qstandardpaths.rs @@ -0,0 +1,55 @@ +use crate::{cpp, QString, QStringList}; + +cpp! {{ + #include +}} + +/// Bindings for [`QStandardPaths::StandardLocation`][enum] enum. +/// +/// [enum]: https://doc.qt.io/qt-5/qstandardpaths.html#StandardLocation-enum +#[repr(C)] +#[derive(Clone, Copy, PartialEq, Debug)] +#[allow(non_camel_case_types)] +pub enum QStandardPathLocation { + DesktopLocation = 0, + DocumentsLocation = 1, + FontsLocation = 2, + ApplicationsLocation = 3, + MusicLocation = 4, + MoviesLocation = 5, + PicturesLocation = 6, + TempLocation = 7, + HomeLocation = 8, + AppLocalDataLocation = 9, + CacheLocation = 10, + GenericDataLocation = 11, + RuntimeLocation = 12, + ConfigLocation = 13, + DownloadLocation = 14, + GenericCacheLocation = 15, + GenericConfigLocation = 16, + AppDataLocation = 17, + AppConfigLocation = 18, +} + +impl QStandardPathLocation { + pub fn display_name(t: Self) -> Option { + cpp!(unsafe [t as "QStandardPaths::StandardLocation"] -> QString as "QString" { + return QStandardPaths::displayName(t); + }) + .to_option() + } + + pub fn standard_locations(t: Self) -> QStringList { + cpp!(unsafe [t as "QStandardPaths::StandardLocation"] -> QStringList as "QStringList" { + return QStandardPaths::standardLocations(t); + }) + } + + pub fn writable_location(t: Self) -> Option { + cpp!(unsafe [t as "QStandardPaths::StandardLocation"] -> QString as "QString" { + return QStandardPaths::writableLocation(t); + }) + .to_option() + } +} diff --git a/qttypes/src/qtcore/qstring.rs b/qttypes/src/qtcore/qstring.rs index 189ef8e6..1f92bdcd 100644 --- a/qttypes/src/qtcore/qstring.rs +++ b/qttypes/src/qtcore/qstring.rs @@ -184,6 +184,17 @@ impl QString { return self->normalized(mode, version); }) } + + /// A function that converts QString to Option. + /// Checks if the QString is null. + /// Note: null QString is differnt from empty QString. + pub fn to_option(self) -> Option { + if self.is_null() { + None + } else { + Some(self) + } + } } impl From for QString {