Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/plugins/preferences/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ set(HEADERS
common/preferencestreeproxymodel.h

common/preferenceswidget.h

common/inputmessagenotifier.h
common/inputdetectors.h
)

set(SOURCES
Expand Down Expand Up @@ -378,6 +381,9 @@ set(SOURCES
common/preferencestreeproxymodel.cpp

common/preferenceswidget.cpp

common/inputmessagenotifier.cpp
common/inputdetectors.cpp
)

set(UI_FORMS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,37 @@ context (user policy option)</translation>
<translation>Common</translation>
</message>
</context>
<context>
<name>preferences::InputMessageNotifier</name>
<message>
<source>The input field has a space at the beginning or at the end</source>
<translation type="vanished">The input field has a space at the beginning or at the end</translation>
</message>
<message>
<source>There is a folder &apos;.&apos;/&apos;..&apos; in the path input field</source>
<translation type="vanished">There is a folder &apos;.&apos;/&apos;..&apos; in the path input field</translation>
</message>
<message>
<source>The path input field contains the path to the folder</source>
<translation type="vanished">The path input field contains the path to the folder</translation>
</message>
<message>
<source>The path input field contains a path that is not compatible with windows</source>
<translation type="vanished">The path input field contains a path that is not compatible with windows</translation>
</message>
<message>
<source>The path input field contains a relative path</source>
<translation type="vanished">The path input field contains a relative path</translation>
</message>
<message>
<source>The path input field contains a path from root</source>
<translation type="vanished">The path input field contains a path from root</translation>
</message>
<message>
<source>The path input field contains a network path</source>
<translation type="vanished">The path input field contains a network path</translation>
</message>
</context>
<context>
<name>preferences::PreferencesTreeView</name>
<message>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,37 @@ context (user policy option)</source>
<translation>Общие</translation>
</message>
</context>
<context>
<name>preferences::InputMessageNotifier</name>
<message>
<source>The input field has a space at the beginning or at the end</source>
<translation type="vanished">Поле ввода содержит пробел в начале или в конце</translation>
</message>
<message>
<source>There is a folder &apos;.&apos;/&apos;..&apos; in the path input field</source>
<translation type="vanished">В поле ввода пути есть папка &quot;.&quot;/&quot;..&quot;</translation>
</message>
<message>
<source>The path input field contains the path to the folder</source>
<translation type="vanished">Поле ввода пути содержит путь к папке</translation>
</message>
<message>
<source>The path input field contains a path that is not compatible with windows</source>
<translation type="vanished">Поле ввода path содержит путь, который несовместим с Windows</translation>
</message>
<message>
<source>The path input field contains a relative path</source>
<translation type="vanished">Поле ввода path содержит относительный путь</translation>
</message>
<message>
<source>The path input field contains a path from root</source>
<translation type="vanished">Поле ввода path содержит путь от корня</translation>
</message>
<message>
<source>The path input field contains a network path</source>
<translation type="vanished">Поле ввода path содержит сетевой путь</translation>
</message>
</context>
<context>
<name>preferences::PreferencesTreeView</name>
<message>
Expand Down
15 changes: 15 additions & 0 deletions src/plugins/preferences/common/inputdetectors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "inputdetectors.h"

namespace preferences {

bool WhitespaceDetector::detect(const QString &input)
{
return input.trimmed() != input;
}

bool EmptyDetector::detect(const QString &input)
{
return input.length() == 0;
}

} // namespace preferences
42 changes: 42 additions & 0 deletions src/plugins/preferences/common/inputdetectors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/***********************************************************************************************************************
**
** Copyright (C) 2021 BaseALT Ltd. <org@basealt.ru>
**
** 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 2
** of the License, 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, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
***********************************************************************************************************************/

#ifndef GPUI_INPUTDETECTORS_H
#define GPUI_INPUTDETECTORS_H

#include "inputmessagenotifier.h"

namespace preferences {

class WhitespaceDetector : public InputDetector
{
public:
bool detect(const QString &input) override;
};

class EmptyDetector : public InputDetector
{
public:
bool detect(const QString &input) override;
};

} // namespace preferences

#endif // GPUI_INPUTDETECTORS_H
206 changes: 206 additions & 0 deletions src/plugins/preferences/common/inputmessagenotifier.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/***********************************************************************************************************************
**
** Copyright (C) 2021 BaseALT Ltd. <org@basealt.ru>
**
** 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 2
** of the License, 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, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
***********************************************************************************************************************/

#include "inputmessagenotifier.h"
#include <algorithm>
#include <iterator>

namespace preferences {

InputMessageNotifier::DetectElement::DetectElement(QWidget *parent, QLayout *layout,
const QString &message,
InputMessageNotifier::MessageLevel level)
{
this->m_label = new QLabel(parent);
layout->addWidget(this->m_label);

this->m_label->hide();

this->m_label->setText(message);

switch (level) {
case InputMessageNotifier::MessageLevel::Warning:
this->m_label->setStyleSheet("background-color: rgb(249, 240, 107);\n"
"color: rgb(0,0,0);\n"
"border-radius: 10px;\n"
"padding: 10px;");
break;

default:
case InputMessageNotifier::MessageLevel::Error:
this->m_label->setStyleSheet("background-color: rgb(246, 97, 81);\n"
"color: rgb(255, 255, 255);\n"
"border-radius: 10px;\n"
"padding: 10px;");
break;
}

this->m_level = level;
}
InputMessageNotifier::DetectElement::DetectElement(DetectElement &&element)
{
this->m_detected = element.m_detected;
this->m_label = element.m_label;
this->m_level = element.m_level;
element.m_label = nullptr;
}
InputMessageNotifier::DetectElement &
InputMessageNotifier::DetectElement::operator=(DetectElement &&element)
{
if (this->m_label) {
delete this->m_label;
}

this->m_detected = element.m_detected;
this->m_label = element.m_label;
this->m_level = element.m_level;
element.m_label = nullptr;

return *this;
}

void InputMessageNotifier::DetectElement::detect()
{
this->m_label->show();
this->m_detected = true;
}
void InputMessageNotifier::DetectElement::undetect()
{
this->m_label->hide();
this->m_detected = false;
}

bool InputMessageNotifier::DetectElement::detected()
{
return this->m_detected;
}

InputMessageNotifier::MessageLevel InputMessageNotifier::DetectElement::level()
{
return this->m_level;
}

void InputMessageNotifier::DetectElement::setMessage(const QString &message)
{
this->m_label->setText(message);
}

InputMessageNotifier::DetectElement::~DetectElement()
{
if (this->m_label) {
delete this->m_label;
}
}

InputMessageNotifier::InputMessageNotifier(QWidget *widget) : QWidget(widget)
{
this->m_layout = new QVBoxLayout;
this->setLayout(this->m_layout);
}

size_t InputMessageNotifier::addDetector(std::unique_ptr<InputDetector> detector)
{
this->m_detectors[this->m_nextDetector] = std::move(detector);
return this->m_nextDetector++;
}

void InputMessageNotifier::removeDetector(size_t id)
{
this->m_detectors.erase(id);
}

void InputMessageNotifier::addInput(const QString &name)
{
this->m_instances[name] = std::move(InputInstance());
}

void InputMessageNotifier::removeInput(const QString &name)
{
this->m_instances.erase(name);
}

void InputMessageNotifier::updateInput(const QString &name, const QString &input)
{
auto &instance = this->m_instances[name];

for (auto &detect : instance) {
bool detected = this->m_detectors[detect.first]->detect(input);

if (detected && !detect.second.detected()) {
detect.second.detect();
this->incCounter(detect.second.level());
} else if (!detected && detect.second.detected()) {
detect.second.undetect();
this->decCounter(detect.second.level());
}
}
}

void InputMessageNotifier::setMessage(const QString &name, size_t detector, const QString &message)
{
auto entry = this->m_instances[name].find(detector);
if (entry != this->m_instances[name].end()) {
entry->second.setMessage(message);
}
}

void InputMessageNotifier::attachDetector(const QString &name, size_t detector,
const QString &message, MessageLevel level)
{
this->m_instances[name].insert(std::pair<size_t, DetectElement>(
detector, DetectElement(this, this->m_layout, message, level)));
}

bool InputMessageNotifier::hasAnyError()
{
return this->m_errorCount != 0;
}

bool InputMessageNotifier::hasAnyWarning()
{
return this->m_warningCount != 0;
}

InputMessageNotifier::~InputMessageNotifier() { }

void InputMessageNotifier::incCounter(InputMessageNotifier::MessageLevel level)
{
switch (level) {
case InputMessageNotifier::MessageLevel::Warning:
++this->m_warningCount;
break;
case InputMessageNotifier::MessageLevel::Error:
++this->m_errorCount;
break;
}
}
void InputMessageNotifier::decCounter(MessageLevel level)
{
switch (level) {
case InputMessageNotifier::MessageLevel::Warning:
--this->m_warningCount;
break;
case InputMessageNotifier::MessageLevel::Error:
--this->m_errorCount;
break;
}
}

} // namespace preferences
Loading