1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
/*
* Copyright (C) 2019 ~ 2020 Uniontech Software Technology Co.,Ltd.
*
* Author: zyc <zyc@uniontech.com>
*
* Maintainer: zyc <zyc@uniontech.com>
*
* 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 of the License, or
* 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 "logsettings.h"
#include <QStandardPaths>
#include <QApplication>
#include <QDir>
#include <QDebug>
#define MAINWINDOW_HEIGHT_NAME "logMainWindowHeightName"
#define MAINWINDOW_WIDTH_NAME "logMainWindowWidthName"
std::atomic<LogSettings *> LogSettings::m_instance;
std::mutex LogSettings::m_mutex;
/**
* @brief LogSettings::LogSettings 构造函数从配置文件初始化配置
* @param parent 父对象
*/
LogSettings::LogSettings(QObject *parent)
: QObject(parent),
m_winInfoConfig(nullptr),
m_logDirConfig(nullptr),
m_configPath(""),
m_logDirPath("")
{
QDir infoPath(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation));
if (!infoPath.exists()) {
infoPath.mkpath(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation));
}
m_configPath = infoPath.filePath("wininfo-config.conf");
m_winInfoConfig = new QSettings(m_configPath, QSettings::IniFormat, this);
m_logDirPath = infoPath.filePath("logdir-config.conf");
m_logDirConfig = new QSettings(m_logDirPath, QSettings::IniFormat, this);
}
/**
* @brief LogSettings::getConfigWinSize 通过配置文件获取之前保存的窗口大小
* @return 之前保存的窗口大小
*/
QSize LogSettings::getConfigWinSize()
{
QVariant tempHeight = m_winInfoConfig->value(MAINWINDOW_HEIGHT_NAME);
QVariant tempWidth = m_winInfoConfig->value(MAINWINDOW_WIDTH_NAME);
int winHeight = MAINWINDOW_HEIGHT;
int winWidth = MAINWINDOW_WIDTH;
if (tempHeight.isValid()) {
winHeight = tempHeight.toInt();
winHeight = winHeight > MAINWINDOW_HEIGHT ? winHeight : MAINWINDOW_HEIGHT;
}
if (tempHeight.isValid()) {
winWidth = tempWidth.toInt();
winWidth = winWidth > MAINWINDOW_WIDTH ? winWidth : MAINWINDOW_WIDTH;
}
return QSize(winWidth, winHeight);
}
/**
* @brief LogSettings::saveConfigWinSize 保存当前窗口尺寸到配置文件
* @param w 当前窗口宽度
* @param h 当前窗口高度
*/
void LogSettings::saveConfigWinSize(int w, int h)
{
int winWidth = w > MAINWINDOW_WIDTH ? w : MAINWINDOW_WIDTH;
int winHeight = h > MAINWINDOW_HEIGHT ? h : MAINWINDOW_HEIGHT;
m_winInfoConfig->setValue(MAINWINDOW_HEIGHT_NAME, winHeight);
m_winInfoConfig->setValue(MAINWINDOW_WIDTH_NAME, winWidth);
m_winInfoConfig->sync();
}
|