Big code refactor
The design was defective and I didn't expect the popularity of the project. After these changes the code will be more mantainable and understandable. Among the changes we can see: - A better code structure - Decoupled button widget from its logic - More code reuse - Easier way to add buttons - Specialized classes
This commit is contained in:
149
src/utils/confighandler.cpp
Normal file
149
src/utils/confighandler.cpp
Normal file
@@ -0,0 +1,149 @@
|
||||
// Copyright 2017 Alejandro Sirgo Rica
|
||||
//
|
||||
// This file is part of Flameshot.
|
||||
//
|
||||
// Flameshot 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
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Flameshot 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 Flameshot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "confighandler.h"
|
||||
#include <QSettings>
|
||||
|
||||
ConfigHandler::ConfigHandler(QObject *parent) : QObject(parent) {
|
||||
m_settings = new QSettings(this);
|
||||
}
|
||||
|
||||
QList<CaptureButton::ButtonType> ConfigHandler::getButtons() {
|
||||
QList<int> buttons = m_settings->value("buttons").value<QList<int> >();
|
||||
bool modified = normalizeButtons(buttons);
|
||||
if (modified) {
|
||||
m_settings->setValue("buttons", QVariant::fromValue(buttons));
|
||||
}
|
||||
return fromIntToButton(buttons);
|
||||
}
|
||||
|
||||
void ConfigHandler::setButtons(const QList<CaptureButton::ButtonType> &buttons) {
|
||||
QList<int> l = fromButtonToInt(buttons);
|
||||
normalizeButtons(l);
|
||||
m_settings->setValue("buttons", QVariant::fromValue(l));
|
||||
}
|
||||
|
||||
QString ConfigHandler::getSavePath() {
|
||||
return m_settings->value("savePath").toString();
|
||||
}
|
||||
|
||||
void ConfigHandler::setSavePath(const QString &savePath) {
|
||||
m_settings->setValue("savePath", savePath);
|
||||
}
|
||||
|
||||
QColor ConfigHandler::getUIMainColor() {
|
||||
return m_settings->value("uiColor").value<QColor>();
|
||||
}
|
||||
|
||||
void ConfigHandler::setUIMainColor(const QColor &c) {
|
||||
m_settings->setValue("uiColor", c);
|
||||
}
|
||||
|
||||
QColor ConfigHandler::getUIContrastColor() {
|
||||
return m_settings->value("contastUiColor").value<QColor>();
|
||||
}
|
||||
|
||||
void ConfigHandler::setUIContrastColor(const QColor &c) {
|
||||
m_settings->setValue("contastUiColor", c);
|
||||
}
|
||||
|
||||
QColor ConfigHandler::getDrawColor() {
|
||||
return m_settings->value("drawColor").value<QColor>();
|
||||
}
|
||||
|
||||
void ConfigHandler::setDrawColor(const QColor &c) {
|
||||
m_settings->setValue("drawColor", c);
|
||||
}
|
||||
|
||||
bool ConfigHandler::getShowHelp() {
|
||||
return m_settings->value("showHelp").toBool();
|
||||
}
|
||||
|
||||
void ConfigHandler::setShowHelp(const bool showHelp) {
|
||||
m_settings->setValue("showHelp", showHelp);
|
||||
}
|
||||
|
||||
bool ConfigHandler::getDesktopNotification() {
|
||||
return m_settings->value("showDesktopNotification").toBool();
|
||||
}
|
||||
|
||||
void ConfigHandler::setDesktopNotification(const bool showDesktopNotification) {
|
||||
m_settings->setValue("showDesktopNotification", showDesktopNotification);
|
||||
}
|
||||
|
||||
QString ConfigHandler::getFilenamePattern() {
|
||||
return m_settings->value("filenamePattern").toString();
|
||||
}
|
||||
|
||||
void ConfigHandler::setFilenamePattern(const QString &pattern) {
|
||||
return m_settings->setValue("filenamePattern", pattern);
|
||||
}
|
||||
|
||||
bool ConfigHandler::initiatedIsSet() {
|
||||
return m_settings->value("initiated").toBool();
|
||||
}
|
||||
|
||||
void ConfigHandler::setInitiated() {
|
||||
m_settings->setValue("initiated", true);
|
||||
}
|
||||
|
||||
void ConfigHandler::setNotInitiated() {
|
||||
m_settings->setValue("initiated", false);
|
||||
}
|
||||
|
||||
void ConfigHandler::setAllTheButtons() {
|
||||
QList<int> buttons;
|
||||
auto listTypes = CaptureButton::getIterableButtonTypes();
|
||||
for (CaptureButton::ButtonType t: listTypes) {
|
||||
buttons << static_cast<int>(t);
|
||||
}
|
||||
m_settings->setValue("buttons", QVariant::fromValue(buttons));
|
||||
}
|
||||
|
||||
bool ConfigHandler::normalizeButtons(QList<int> &buttons) {
|
||||
auto listTypes = CaptureButton::getIterableButtonTypes();
|
||||
QList<int> listTypesInt;
|
||||
for(auto i: listTypes) listTypesInt << static_cast<int>(i);
|
||||
|
||||
bool hasChanged = false;
|
||||
QMutableListIterator<int> i(buttons);
|
||||
while (i.hasNext()) {
|
||||
if (!listTypesInt.contains(i.next())) {
|
||||
i.remove();
|
||||
hasChanged = true;
|
||||
}
|
||||
}
|
||||
return hasChanged;
|
||||
}
|
||||
|
||||
QList<CaptureButton::ButtonType> ConfigHandler::fromIntToButton(
|
||||
const QList<int> &l)
|
||||
{
|
||||
QList<CaptureButton::ButtonType> buttons;
|
||||
for(auto i: l)
|
||||
buttons << static_cast<CaptureButton::ButtonType>(i);
|
||||
return buttons;
|
||||
}
|
||||
|
||||
QList<int> ConfigHandler::fromButtonToInt(
|
||||
const QList<CaptureButton::ButtonType> &l)
|
||||
{
|
||||
QList<int> buttons;
|
||||
for(auto i: l)
|
||||
buttons << static_cast<int>(i);
|
||||
return buttons;
|
||||
}
|
||||
75
src/utils/confighandler.h
Normal file
75
src/utils/confighandler.h
Normal file
@@ -0,0 +1,75 @@
|
||||
// Copyright 2017 Alejandro Sirgo Rica
|
||||
//
|
||||
// This file is part of Flameshot.
|
||||
//
|
||||
// Flameshot 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
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Flameshot 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 Flameshot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#ifndef CONFIGHANDLER_H
|
||||
#define CONFIGHANDLER_H
|
||||
|
||||
#include "src/capture/capturebutton.h"
|
||||
#include <QObject>
|
||||
#include <QList>
|
||||
|
||||
class QSettings;
|
||||
|
||||
class ConfigHandler : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ConfigHandler(QObject *parent = nullptr);
|
||||
|
||||
QList<CaptureButton::ButtonType> getButtons();
|
||||
void setButtons(const QList<CaptureButton::ButtonType> &);
|
||||
|
||||
QString getSavePath();
|
||||
void setSavePath(const QString &);
|
||||
|
||||
QColor getUIMainColor();
|
||||
void setUIMainColor(const QColor &);
|
||||
|
||||
QColor getUIContrastColor();
|
||||
void setUIContrastColor(const QColor &);
|
||||
|
||||
QColor getDrawColor();
|
||||
void setDrawColor(const QColor &);
|
||||
|
||||
bool getShowHelp();
|
||||
void setShowHelp(const bool);
|
||||
|
||||
bool getDesktopNotification();
|
||||
void setDesktopNotification(const bool);
|
||||
|
||||
QString getFilenamePattern();
|
||||
void setFilenamePattern(const QString &);
|
||||
|
||||
bool initiatedIsSet();
|
||||
void setInitiated();
|
||||
void setNotInitiated();
|
||||
|
||||
void setAllTheButtons();
|
||||
|
||||
private:
|
||||
QSettings *m_settings;
|
||||
|
||||
bool normalizeButtons(QList<int> &);
|
||||
|
||||
QList<CaptureButton::ButtonType> fromIntToButton(const QList<int> &l);
|
||||
QList<int> fromButtonToInt(const QList<CaptureButton::ButtonType> &l);
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // CONFIGHANDLER_H
|
||||
@@ -16,20 +16,18 @@
|
||||
// along with Flameshot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "filenamehandler.h"
|
||||
#include "src/utils/confighandler.h"
|
||||
#include <ctime>
|
||||
#include <locale>
|
||||
#include <QSettings>
|
||||
#include <QStandardPaths>
|
||||
#include <QDir>
|
||||
|
||||
FileNameHandler::FileNameHandler(QObject *parent) : QObject(parent) {
|
||||
std::locale::global(std::locale(std::locale("").name()));
|
||||
}
|
||||
|
||||
QString FileNameHandler::getActualPattern() {
|
||||
return QSettings().value("filenamePattern").toString();
|
||||
}
|
||||
|
||||
QString FileNameHandler::getParsedPattern() {
|
||||
return parseFilename(getActualPattern());
|
||||
return parseFilename(ConfigHandler().getFilenamePattern());
|
||||
}
|
||||
|
||||
QString FileNameHandler::parseFilename(const QString &name) {
|
||||
@@ -50,7 +48,40 @@ QString FileNameHandler::parseFilename(const QString &name) {
|
||||
}
|
||||
|
||||
void FileNameHandler::savePattern(const QString &pattern) {
|
||||
QSettings().setValue("filenamePattern", pattern);
|
||||
ConfigHandler().setFilenamePattern(pattern);
|
||||
}
|
||||
|
||||
QString FileNameHandler::getAbsoluteSavePath() {
|
||||
ConfigHandler config;
|
||||
QString savePath = config.getSavePath();
|
||||
bool changed = false;
|
||||
if (savePath.isEmpty() || !QDir(savePath).exists() || !QFileInfo(savePath).isWritable()) {
|
||||
changed = true;
|
||||
savePath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
|
||||
if (savePath.isEmpty()) {
|
||||
savePath = QDir::currentPath();
|
||||
}
|
||||
}
|
||||
if(changed) {
|
||||
config.setSavePath(savePath);
|
||||
}
|
||||
QString tempName = "/"+ FileNameHandler().getParsedPattern();
|
||||
// find unused name adding _n where n is a number
|
||||
QFileInfo checkFile(savePath + tempName + ".png");
|
||||
if (checkFile.exists()) {
|
||||
tempName += "_";
|
||||
int i = 1;
|
||||
while (true) {
|
||||
checkFile.setFile(
|
||||
savePath + tempName + QString::number(i) + ".png");
|
||||
if (!checkFile.exists()) {
|
||||
tempName += QString::number(i);
|
||||
break;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
}
|
||||
return savePath + tempName + ".png";
|
||||
}
|
||||
|
||||
QString FileNameHandler::charArrToQString(const char *c) {
|
||||
|
||||
@@ -27,7 +27,6 @@ class FileNameHandler : public QObject
|
||||
public:
|
||||
explicit FileNameHandler(QObject *parent = nullptr);
|
||||
|
||||
QString getActualPattern();
|
||||
QString getParsedPattern();
|
||||
QString parseFilename(const QString &name);
|
||||
|
||||
@@ -35,6 +34,7 @@ public:
|
||||
|
||||
public slots:
|
||||
void savePattern(const QString &pattern);
|
||||
QString getAbsoluteSavePath();
|
||||
|
||||
private:
|
||||
//using charArr = char[MAX_CHARACTERS];
|
||||
|
||||
Reference in New Issue
Block a user