ukui-screensaver/Common/commonfunc.cpp

151 lines
4.2 KiB
C++
Raw Normal View History

/*
* Copyright (C) 2019 Tianjin KYLIN Information Technology Co., Ltd.
*
* 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, 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/>.
*
**/
#include <QMimeType>
#include <QSettings>
#include <QPainterPath>
#include <QMimeDatabase>
2022-11-01 18:14:46 +08:00
#include <QPainter>
#include <QFileInfo>
#include <QFontMetrics>
#include <QDir>
#include <QTextStream>
#include <QtX11Extras/QX11Info>
#include <X11/XKBlib.h>
#include "commonfunc.h"
#define DRM_DIR "/sys/class/leds/"
/**
* @brief
* @return true:
*/
bool checkCapsLockState()
{
QDir ledDir(DRM_DIR);
QStringList leds = ledDir.entryList(QDir::Dirs);
QString capsFile;
for(int i = 0;i<leds.count();i++){
if(leds.at(i).contains("capslock"))
capsFile = leds.at(i);
}
QFile drmStatusFile(DRM_DIR + capsFile + "/brightness");
if(drmStatusFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&drmStatusFile);
QString status = in.readLine();
if(status == "0") {
return false;
}else{
return true;
}
}
bool capsState = false;
unsigned int n;
XkbGetIndicatorState(QX11Info::display(), XkbUseCoreKbd, &n);
capsState = (n & 0x01) == 1;
return capsState;
}
bool ispicture(QString filepath)
{
QFileInfo file(filepath);
if(file.exists() == false)
return false;
QMimeDatabase db;
QMimeType mime = db.mimeTypeForFile(filepath);
return mime.name().startsWith("image/");
}
QString getSystemVersion()
{
QSettings settings("/etc/lsb-release", QSettings::IniFormat);
QString release = settings.value("DISTRIB_RELEASE").toString();
QString description = settings.value("DISTRIB_DESCRIPTION").toString();
if(description.right(3) == "LTS")
release = release + " LTS";
return release;
}
QString getSystemDistrib()
{
QSettings settings("/etc/lsb-release", QSettings::IniFormat);
QString distribId = settings.value("DISTRIB_ID").toString();
return distribId;
}
bool getUseFirstDevice()
{
QSettings settings("/etc/biometric-auth/ukui-biometric.conf", QSettings::IniFormat);
return settings.value("UseFirstDevice").toBool();
}
commonFunc::commonFunc()
{
}
QString ElideText(QFont font,int width,QString strInfo)
{
QFontMetrics fontMetrics(font);
//如果当前字体下,字符串长度大于指定宽度
if(fontMetrics.width(strInfo) > width)
{
strInfo= QFontMetrics(font).elidedText(strInfo, Qt::ElideRight, width);
}
return strInfo;
}
2022-11-01 18:14:46 +08:00
/*修改图片缩放机制,图片长宽不一致时,先取图片中央的部分*/
QPixmap scaledPixmap(QPixmap src)
{
QPixmap rectPixmap;
if (src.width() > src.height()) {
QPixmap iconPixmap = src.copy((src.width() - src.height())/2, 0, src.height(), src.height());
// 根据label高度等比例缩放图片
rectPixmap = iconPixmap.scaledToHeight(src.height());
} else {
QPixmap iconPixmap = src.copy(0, (src.height() - src.width())/2, src.width(), src.width());
// 根据label宽度等比例缩放图片
rectPixmap = iconPixmap.scaledToWidth(src.width());
}
return rectPixmap;
}
QPixmap PixmapToRound(const QPixmap &src, int radius)
{
if (src.isNull()) {
return QPixmap();
}
QPixmap pixmapa(src);
QPixmap pixmap(radius*2,radius*2);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
QPainterPath path;
path.addEllipse(0, 0, radius*2, radius*2);
painter.setClipPath(path);
painter.drawPixmap(0, 0, radius*2, radius*2, pixmapa);
return pixmap;
}