Added different units to pixels conversion functions

This commit is contained in:
FalsinSoft 2021-04-07 23:11:25 +02:00
parent a1fdf22e23
commit 8bf4450d04
5 changed files with 100 additions and 1 deletions

View File

@ -54,6 +54,7 @@ if(QTAT_SYSTEM)
add_compile_definitions(QTAT_SYSTEM)
list(APPEND QTAT_SOURCE_FILES QAndroidSystem.cpp)
list(APPEND QTAT_HEADER_FILES QAndroidSystem.h)
list(APPEND QTAT_JAVA_FILES ${QTAT_JAVA_DIR}/AndroidSystem.java)
endif()
option(QTAT_APK_EXPANSION_FILES "Enable QtAndroidTools Apk Expansion.")

View File

@ -25,7 +25,9 @@
QAndroidSystem *QAndroidSystem::m_pInstance = nullptr;
QAndroidSystem::QAndroidSystem()
QAndroidSystem::QAndroidSystem() : m_javaSystem("com/falsinsoft/qtandroidtools/AndroidSystem",
"(Landroid/app/Activity;)V",
QtAndroid::androidActivity().object<jobject>())
{
m_pInstance = this;
loadStandardPaths();
@ -44,6 +46,33 @@ QAndroidSystem* QAndroidSystem::instance()
return m_pInstance;
}
int QAndroidSystem::spToPx(float sp)
{
if(m_javaSystem.isValid())
{
return m_javaSystem.callMethod<jint>("spToPx", "(F)I", sp);
}
return -1;
}
int QAndroidSystem::dipToPx(float dip)
{
if(m_javaSystem.isValid())
{
return m_javaSystem.callMethod<jint>("dipToPx", "(F)I", dip);
}
return -1;
}
int QAndroidSystem::ptToPx(float pt)
{
if(m_javaSystem.isValid())
{
return m_javaSystem.callMethod<jint>("ptToPx", "(F)I", pt);
}
return -1;
}
const QString& QAndroidSystem::getDataLocation() const
{
return m_standardPaths.dataLocation;

View File

@ -44,6 +44,10 @@ public:
static QObject* qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine);
static QAndroidSystem* instance();
Q_INVOKABLE int spToPx(float sp);
Q_INVOKABLE int dipToPx(float dip);
Q_INVOKABLE int ptToPx(float pt);
const QString& getDataLocation() const;
const QString& getConfigLocation() const;
const QString& getDownloadLocation() const;
@ -52,6 +56,7 @@ public:
const QString& getPicturesLocation() const;
private:
const QAndroidJniObject m_javaSystem;
static QAndroidSystem *m_pInstance;
struct {

View File

@ -47,6 +47,12 @@ contains(DEFINES, QTAT_SCREEN) {
contains(DEFINES, QTAT_SYSTEM) {
HEADERS += $$PWD/QAndroidSystem.h
SOURCES += $$PWD/QAndroidSystem.cpp
OTHER_FILES += $$PWD/src/com/falsinsoft/qtandroidtools/AndroidSystem.java
equals(COPY_JAVA_FILE, true) {
copy_system.commands = $(COPY_FILE) $$shell_path($$PWD/src/com/falsinsoft/qtandroidtools/AndroidSystem.java) $$shell_path($$ANDROID_PACKAGE_SOURCE_DIR/src/com/falsinsoft/qtandroidtools/)
PRE_TARGETDEPS += copy_system
QMAKE_EXTRA_TARGETS += copy_system
}
}
contains(DEFINES, QTAT_APK_EXPANSION_FILES) {
HEADERS += $$PWD/QAndroidApkExpansionFiles.h

View File

@ -0,0 +1,58 @@
/*
* MIT License
*
* Copyright (c) 2018 Fabio Falsini <falsinsoft@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES 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.
*/
package com.falsinsoft.qtandroidtools;
import android.content.Context;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.util.TypedValue;
public class AndroidSystem
{
private static final String TAG = "AndroidSystem";
private final Activity mActivityInstance;
public AndroidSystem(Activity activityInstance)
{
mActivityInstance = activityInstance;
}
public int spToPx(float sp)
{
return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, mActivityInstance.getResources().getDisplayMetrics());
}
public int dipToPx(float dip)
{
return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, mActivityInstance.getResources().getDisplayMetrics());
}
public int ptToPx(float pt)
{
return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PT, pt, mActivityInstance.getResources().getDisplayMetrics());
}
}