Implement config checking (#1859)
* Add error handling functions to ConfigHandler Refurbished functions setValue and value which were previously unused. These functions now set/get a setting with error handling. Currently recognizes only errors recognizable by QSettings. * Make use of value and setValue in ConfigHandler Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Add checker for unrecognized general options Extraneous config options in [General] will be reported as errors. Added some placeholder functions to be implemented in future commits. * Introduce keysFromGroup function Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Check shortcut names for duplicates Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Fix notification spam Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Implement shortcut conflict checking Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Fix reading of fallbacks on error If there is a config error, some values would not be loaded correctly. Using the newly implemented function ConfigHandler::contains instead of QSettings::contains solves this issue. These changes reveal u bug that causes a crash on startup. Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Fix crashes introduced in previous commit Because ConfigHandler is a dependency of most other classes, calling functions from those classes inside ConfigHandler caused infinite recursions in some cases. Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Add config file watcher Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Add missing config options Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Fix bug in shortcut conflict detection Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Add error resolved notification Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Add GUI error message overlay Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Add indicator in config window Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Use ConfigHandler::fileChanged in ConfigWindow Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Fix watcher sometimes not firing Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Improve config file watching performance Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Add new way to handle config This is only a fundamental implementation. Future commits will replace everything with this new paradigm. Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Fix getButtons and related functions Also refactored related code to use QList instead of QVector because QSettings does not work well with QVector. Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Make good use of the new way * Implement proper checking for basic types Everything is covered, apart from KeySequence. * Move fallback path to ExistingDir value handler Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Use consistent naming scheme in ConfigHandler Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Implement config getters/setters via macro Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Surround text with tr and clang-format Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Fix colors being saved obfuscated Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Add ValueHandler::represenation Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Move ValueHandler to separate files Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * confighandler.cpp: rename macro CUSTOM to OPTION Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Fix bug with shortcut conflict checker Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Update docs and fix setAllTheButtons Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Handle filenamePattern properly Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Fix failing build due to wrong function name Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Fix QSet error due to Qt version mismatch Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Replace QSharedPointer::get with data for older Qt versions Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Fix failing build on MacOS and ubuntu 18.04 Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Add column headers to recognizedGeneralOptions map Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Fix ubuntu 18.04 error Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Fix false positive when shortcuts empty Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Fix wrong shortcut group prefix Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Implement proper shortcut checking Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Add shortcut map in ConfigHandler Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Move ConfigShortcuts functions to ShortcutsWidget Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Fix minor bugs Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Add fallback scheme: Pictures, HOME, TMP Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Add config --check CLI option Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Add config error log to GUI Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Rename ValueHandler::description to expected * Convert Qt's #AARRGGBB to #RRGGBBAA and vice versa Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Remove obsolete `saveAfterCopyPath` Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Fix errors in example config Also added an additional ; in front of actual comments to differentiate them from commented options. Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Allow special value 'picker' in userColors Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Allow only name, #RRGGBB, and #RRGGBBAA color formats Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>
This commit is contained in:
@@ -29,6 +29,8 @@
|
||||
#include <QApplication>
|
||||
#include <QDateTime>
|
||||
#include <QDesktopWidget>
|
||||
#include <QFontMetrics>
|
||||
#include <QLabel>
|
||||
#include <QPaintEvent>
|
||||
#include <QPainter>
|
||||
#include <QScreen>
|
||||
@@ -56,6 +58,8 @@ CaptureWidget::CaptureWidget(uint id,
|
||||
, m_captureDone(false)
|
||||
, m_previewEnabled(true)
|
||||
, m_adjustmentButtonPressed(false)
|
||||
, m_configError(false)
|
||||
, m_configErrorResolved(false)
|
||||
, m_activeButton(nullptr)
|
||||
, m_activeTool(nullptr)
|
||||
, m_toolWidget(nullptr)
|
||||
@@ -85,9 +89,9 @@ CaptureWidget::CaptureWidget(uint id,
|
||||
this,
|
||||
&CaptureWidget::childLeave);
|
||||
setAttribute(Qt::WA_DeleteOnClose);
|
||||
m_opacity = m_config.contrastOpacityValue();
|
||||
m_uiColor = m_config.uiMainColorValue();
|
||||
m_contrastUiColor = m_config.uiContrastColorValue();
|
||||
m_opacity = m_config.contrastOpacity();
|
||||
m_uiColor = m_config.uiColor();
|
||||
m_contrastUiColor = m_config.contrastUiColor();
|
||||
setMouseTracking(true);
|
||||
initContext(savePath, fullScreen);
|
||||
initShortcuts();
|
||||
@@ -200,10 +204,25 @@ CaptureWidget::CaptureWidget(uint id,
|
||||
|
||||
initPanel();
|
||||
|
||||
m_config.checkAndHandleError();
|
||||
if (m_config.hasError()) {
|
||||
m_configError = true;
|
||||
}
|
||||
connect(ConfigHandler::getInstance(), &ConfigHandler::error, this, [=]() {
|
||||
m_configError = true;
|
||||
m_configErrorResolved = false;
|
||||
update();
|
||||
});
|
||||
connect(
|
||||
ConfigHandler::getInstance(), &ConfigHandler::errorResolved, this, [=]() {
|
||||
m_configError = false;
|
||||
m_configErrorResolved = true;
|
||||
update();
|
||||
});
|
||||
OverlayMessage::init(this,
|
||||
QGuiAppCurrentScreen().currentScreen()->geometry());
|
||||
|
||||
if (m_config.showHelpValue()) {
|
||||
if (m_config.showHelp()) {
|
||||
OverlayMessage::push(
|
||||
tr("Select an area with the mouse, or press Esc to exit."
|
||||
"\nPress Enter to capture the screen."
|
||||
@@ -225,7 +244,7 @@ CaptureWidget::~CaptureWidget()
|
||||
void CaptureWidget::initButtons()
|
||||
{
|
||||
auto allButtonTypes = CaptureToolButton::getIterableButtonTypes();
|
||||
auto visibleButtonTypes = m_config.getButtons();
|
||||
auto visibleButtonTypes = m_config.buttons();
|
||||
QVector<CaptureToolButton*> vectorButtons;
|
||||
|
||||
// Add all buttons but hide those that were disabled in the Interface config
|
||||
@@ -376,6 +395,10 @@ void CaptureWidget::paintEvent(QPaintEvent* paintEvent)
|
||||
|
||||
// draw inactive region
|
||||
drawInactiveRegion(&painter);
|
||||
|
||||
if (m_configError || m_configErrorResolved) {
|
||||
drawConfigErrorMessage(&painter);
|
||||
}
|
||||
}
|
||||
|
||||
void CaptureWidget::showColorPicker(const QPoint& pos)
|
||||
@@ -683,8 +706,7 @@ void CaptureWidget::mouseMoveEvent(QMouseEvent* e)
|
||||
m_buttonHandler->show();
|
||||
}
|
||||
}
|
||||
} else if (m_activeButton && m_activeButton->tool() &&
|
||||
m_activeButton->tool()->showMousePreview()) {
|
||||
} else if (m_activeButton && m_activeButton->tool()) {
|
||||
update();
|
||||
} else {
|
||||
if (!m_selection->isVisible()) {
|
||||
@@ -705,7 +727,7 @@ void CaptureWidget::mouseReleaseEvent(QMouseEvent* e)
|
||||
}
|
||||
m_colorPicker->hide();
|
||||
if (!m_context.color.isValid()) {
|
||||
m_context.color = ConfigHandler().drawColorValue();
|
||||
m_context.color = ConfigHandler().drawColor();
|
||||
m_panel->show();
|
||||
}
|
||||
} else if (m_mouseIsClicked) {
|
||||
@@ -907,11 +929,11 @@ void CaptureWidget::moveEvent(QMoveEvent* e)
|
||||
|
||||
void CaptureWidget::initContext(const QString& savePath, bool fullscreen)
|
||||
{
|
||||
m_context.color = m_config.drawColorValue();
|
||||
m_context.color = m_config.drawColor();
|
||||
m_context.savePath = savePath;
|
||||
m_context.widgetOffset = mapToGlobal(QPoint(0, 0));
|
||||
m_context.mousePos = mapFromGlobal(QCursor::pos());
|
||||
m_context.thickness = m_config.drawThicknessValue();
|
||||
m_context.thickness = m_config.drawThickness();
|
||||
m_context.fullscreen = fullscreen;
|
||||
}
|
||||
|
||||
@@ -934,7 +956,7 @@ void CaptureWidget::initPanel()
|
||||
#endif
|
||||
}
|
||||
|
||||
if (ConfigHandler().showSidePanelButtonValue()) {
|
||||
if (ConfigHandler().showSidePanelButton()) {
|
||||
auto* panelToggleButton =
|
||||
new OrientablePushButton(tr("Tool Settings"), this);
|
||||
makeChild(panelToggleButton);
|
||||
@@ -1107,9 +1129,9 @@ void CaptureWidget::loadDrawThickness()
|
||||
if ((m_activeButton && m_activeButton->tool() &&
|
||||
m_activeButton->tool()->type() == ToolType::TEXT) ||
|
||||
(m_activeTool && m_activeTool->type() == ToolType::TEXT)) {
|
||||
m_context.thickness = m_config.drawFontSizeValue();
|
||||
m_context.thickness = m_config.drawFontSize();
|
||||
} else {
|
||||
m_context.thickness = m_config.drawThicknessValue();
|
||||
m_context.thickness = m_config.drawThickness();
|
||||
}
|
||||
emit m_sidePanel->thicknessChanged(m_context.thickness);
|
||||
}
|
||||
@@ -1670,6 +1692,28 @@ QRect CaptureWidget::extendedRect(const QRect& r) const
|
||||
r.height() * devicePixelRatio);
|
||||
}
|
||||
|
||||
void CaptureWidget::drawConfigErrorMessage(QPainter* painter)
|
||||
{
|
||||
QString msg;
|
||||
if (m_configError) {
|
||||
msg = ConfigHandler().errorMessage();
|
||||
} else if (m_configErrorResolved) {
|
||||
msg = tr("Configuration error resolved. Launch `flameshot "
|
||||
"gui` again to apply it.");
|
||||
}
|
||||
|
||||
auto textRect = painter->fontMetrics().boundingRect(msg);
|
||||
int w = textRect.width(), h = textRect.height();
|
||||
textRect = { size().width() - w, size().height() - h, w + 100, h + 100 };
|
||||
QScreen* currentScreen = QGuiAppCurrentScreen().currentScreen();
|
||||
|
||||
if (!textRect.contains(QCursor::pos(currentScreen))) {
|
||||
QColor textColor(Qt::white);
|
||||
painter->setPen(textColor);
|
||||
painter->drawText(textRect, msg);
|
||||
}
|
||||
}
|
||||
|
||||
void CaptureWidget::drawInactiveRegion(QPainter* painter)
|
||||
{
|
||||
QColor overlayColor(0, 0, 0, m_opacity);
|
||||
|
||||
Reference in New Issue
Block a user