2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 Dmitry Savchenko
|
|
|
|
|
// Copyright (C) 2016 Vasiliy Sorokin
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2011-10-25 23:14:27 +03:00
|
|
|
|
|
|
|
|
#include "keyworddialog.h"
|
2022-07-11 16:59:51 +02:00
|
|
|
|
2012-02-24 09:43:52 +01:00
|
|
|
#include "keyword.h"
|
2012-04-26 20:37:48 +03:00
|
|
|
#include "lineparser.h"
|
2022-07-12 12:52:55 +02:00
|
|
|
#include "todotr.h"
|
2011-10-25 23:14:27 +03:00
|
|
|
|
2022-07-11 16:59:51 +02:00
|
|
|
#include <utils/layoutbuilder.h>
|
|
|
|
|
#include <utils/qtcolorbutton.h>
|
|
|
|
|
|
2011-10-25 23:14:27 +03:00
|
|
|
#include <QColorDialog>
|
2022-07-11 16:59:51 +02:00
|
|
|
#include <QDialogButtonBox>
|
|
|
|
|
#include <QLabel>
|
|
|
|
|
#include <QLineEdit>
|
|
|
|
|
#include <QListWidget>
|
2011-10-25 23:14:27 +03:00
|
|
|
|
|
|
|
|
namespace Todo {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2012-03-15 18:51:52 +03:00
|
|
|
KeywordDialog::KeywordDialog(const Keyword &keyword, const QSet<QString> &alreadyUsedKeywordNames,
|
|
|
|
|
QWidget *parent) :
|
2011-10-25 23:14:27 +03:00
|
|
|
QDialog(parent),
|
2012-03-15 18:51:52 +03:00
|
|
|
m_alreadyUsedKeywordNames(alreadyUsedKeywordNames)
|
2011-10-25 23:14:27 +03:00
|
|
|
{
|
2022-07-12 12:52:55 +02:00
|
|
|
setWindowTitle(Tr::tr("Keyword"));
|
2022-07-11 16:59:51 +02:00
|
|
|
|
|
|
|
|
m_listWidget = new QListWidget(this);
|
|
|
|
|
|
|
|
|
|
m_colorEdit = new QLineEdit;
|
|
|
|
|
m_colorEdit->setInputMask(QString::fromUtf8("\\#HHHHHH; "));
|
|
|
|
|
m_colorEdit->setText(QString::fromUtf8("#000000"));
|
|
|
|
|
|
|
|
|
|
m_colorButton = new Utils::QtColorButton;
|
|
|
|
|
m_colorButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
|
|
|
|
|
m_colorButton->setMinimumSize(QSize(64, 0));
|
|
|
|
|
|
|
|
|
|
m_keywordNameEdit = new QLineEdit(keyword.name);
|
|
|
|
|
|
2022-07-12 12:52:55 +02:00
|
|
|
m_errorLabel = new QLabel(Tr::tr("errorLabel"), this);
|
2022-07-11 16:59:51 +02:00
|
|
|
m_errorLabel->setStyleSheet(QString::fromUtf8("color: red;"));
|
|
|
|
|
m_errorLabel->hide();
|
|
|
|
|
|
|
|
|
|
m_buttonBox = new QDialogButtonBox(this);
|
|
|
|
|
m_buttonBox->setOrientation(Qt::Horizontal);
|
|
|
|
|
m_buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
|
|
|
|
|
|
|
|
|
|
using namespace Utils::Layouting;
|
|
|
|
|
|
|
|
|
|
Column {
|
2022-07-12 12:52:55 +02:00
|
|
|
new QLabel(Tr::tr("Icon")),
|
2022-07-11 16:59:51 +02:00
|
|
|
m_listWidget,
|
|
|
|
|
Row {
|
|
|
|
|
Group {
|
2022-07-26 13:46:27 +02:00
|
|
|
title(Tr::tr("Color")),
|
2022-07-11 16:59:51 +02:00
|
|
|
Row { m_colorEdit, m_colorButton }
|
|
|
|
|
},
|
|
|
|
|
Group {
|
2022-07-26 13:46:27 +02:00
|
|
|
title(Tr::tr("Keyword")),
|
2022-07-21 11:10:34 +02:00
|
|
|
Column { m_keywordNameEdit }
|
2022-07-11 16:59:51 +02:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
m_errorLabel,
|
|
|
|
|
m_buttonBox
|
|
|
|
|
}.attachTo(this);
|
|
|
|
|
|
2015-11-16 16:45:05 +01:00
|
|
|
setupListWidget(keyword.iconType);
|
2011-10-25 23:14:27 +03:00
|
|
|
setupColorWidgets(keyword.color);
|
2012-03-15 18:51:52 +03:00
|
|
|
|
2022-07-11 16:59:51 +02:00
|
|
|
connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
|
|
|
|
|
connect(m_buttonBox, &QDialogButtonBox::accepted, this, &KeywordDialog::acceptButtonClicked);
|
|
|
|
|
connect(m_keywordNameEdit, &QLineEdit::textChanged, m_errorLabel, &QWidget::hide);
|
2011-10-25 23:14:27 +03:00
|
|
|
}
|
|
|
|
|
|
2022-07-11 16:59:51 +02:00
|
|
|
KeywordDialog::~KeywordDialog() = default;
|
2011-10-25 23:14:27 +03:00
|
|
|
|
|
|
|
|
Keyword KeywordDialog::keyword()
|
|
|
|
|
{
|
|
|
|
|
Keyword result;
|
2012-03-15 18:51:52 +03:00
|
|
|
result.name = keywordName();
|
2022-07-11 16:59:51 +02:00
|
|
|
result.iconType = static_cast<IconType>(m_listWidget->currentItem()->data(Qt::UserRole).toInt());
|
|
|
|
|
result.color = m_colorEdit->text();
|
2011-10-25 23:14:27 +03:00
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void KeywordDialog::colorSelected(const QColor &color)
|
|
|
|
|
{
|
2022-07-11 16:59:51 +02:00
|
|
|
m_colorEdit->setText(color.name());
|
2011-10-25 23:14:27 +03:00
|
|
|
}
|
|
|
|
|
|
2012-03-15 18:51:52 +03:00
|
|
|
void KeywordDialog::acceptButtonClicked()
|
|
|
|
|
{
|
|
|
|
|
if (canAccept())
|
|
|
|
|
accept();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-16 16:45:05 +01:00
|
|
|
void KeywordDialog::setupListWidget(IconType selectedIcon)
|
2011-10-25 23:14:27 +03:00
|
|
|
{
|
2022-07-11 16:59:51 +02:00
|
|
|
m_listWidget->setViewMode(QListWidget::IconMode);
|
|
|
|
|
m_listWidget->setDragEnabled(false);
|
2015-11-16 16:45:05 +01:00
|
|
|
|
2017-10-01 14:26:10 +02:00
|
|
|
QListWidgetItem *item = new QListWidgetItem(icon(IconType::Info), "information");
|
2015-11-16 16:45:05 +01:00
|
|
|
item->setData(Qt::UserRole, static_cast<int>(IconType::Info));
|
2022-07-11 16:59:51 +02:00
|
|
|
m_listWidget->addItem(item);
|
2011-10-25 23:14:27 +03:00
|
|
|
|
2017-10-01 14:26:10 +02:00
|
|
|
item = new QListWidgetItem(icon(IconType::Warning), "warning");
|
2015-11-16 16:45:05 +01:00
|
|
|
item->setData(Qt::UserRole, static_cast<int>(IconType::Warning));
|
2022-07-11 16:59:51 +02:00
|
|
|
m_listWidget->addItem(item);
|
2011-10-25 23:14:27 +03:00
|
|
|
|
2017-10-01 14:26:10 +02:00
|
|
|
item = new QListWidgetItem(icon(IconType::Error), "error");
|
2015-11-16 16:45:05 +01:00
|
|
|
item->setData(Qt::UserRole, static_cast<int>(IconType::Error));
|
2022-07-11 16:59:51 +02:00
|
|
|
m_listWidget->addItem(item);
|
2011-10-25 23:14:27 +03:00
|
|
|
|
2017-10-01 14:26:10 +02:00
|
|
|
item = new QListWidgetItem(icon(IconType::Bug), "bug");
|
2016-07-20 21:24:00 +03:00
|
|
|
item->setData(Qt::UserRole, static_cast<int>(IconType::Bug));
|
2022-07-11 16:59:51 +02:00
|
|
|
m_listWidget->addItem(item);
|
2016-07-20 21:24:00 +03:00
|
|
|
|
2017-10-01 14:26:10 +02:00
|
|
|
item = new QListWidgetItem(icon(IconType::Todo), "todo");
|
2016-07-20 21:24:00 +03:00
|
|
|
item->setData(Qt::UserRole, static_cast<int>(IconType::Todo));
|
2022-07-11 16:59:51 +02:00
|
|
|
m_listWidget->addItem(item);
|
2016-07-20 21:24:00 +03:00
|
|
|
|
2022-07-11 16:59:51 +02:00
|
|
|
for (int i = 0; i < m_listWidget->count(); ++i) {
|
|
|
|
|
item = m_listWidget->item(i);
|
2015-11-16 16:45:05 +01:00
|
|
|
if (static_cast<IconType>(item->data(Qt::UserRole).toInt()) == selectedIcon) {
|
2022-07-11 16:59:51 +02:00
|
|
|
m_listWidget->setCurrentItem(item);
|
2011-10-25 23:14:27 +03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void KeywordDialog::setupColorWidgets(const QColor &color)
|
|
|
|
|
{
|
2022-07-11 16:59:51 +02:00
|
|
|
m_colorButton->setColor(color);
|
|
|
|
|
m_colorEdit->setText(color.name());
|
|
|
|
|
connect(m_colorButton, &Utils::QtColorButton::colorChanged, this, &KeywordDialog::colorSelected);
|
2011-10-25 23:14:27 +03:00
|
|
|
}
|
|
|
|
|
|
2012-03-15 18:51:52 +03:00
|
|
|
bool KeywordDialog::canAccept()
|
|
|
|
|
{
|
|
|
|
|
if (!isKeywordNameCorrect()) {
|
2022-07-12 12:52:55 +02:00
|
|
|
showError(Tr::tr("Keyword cannot be empty, contain spaces, colons, slashes or asterisks."));
|
2012-03-15 18:51:52 +03:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isKeywordNameAlreadyUsed()) {
|
2022-07-12 12:52:55 +02:00
|
|
|
showError(Tr::tr("There is already a keyword with this name."));
|
2012-03-15 18:51:52 +03:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool KeywordDialog::isKeywordNameCorrect()
|
|
|
|
|
{
|
|
|
|
|
// Make sure keyword is not empty and contains no spaces or colons
|
|
|
|
|
|
|
|
|
|
QString name = keywordName();
|
|
|
|
|
|
|
|
|
|
if (name.isEmpty())
|
|
|
|
|
return false;
|
|
|
|
|
|
2018-12-10 00:19:56 +01:00
|
|
|
for (const QChar i : name)
|
|
|
|
|
if (LineParser::isKeywordSeparator(i))
|
2012-03-15 18:51:52 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool KeywordDialog::isKeywordNameAlreadyUsed()
|
|
|
|
|
{
|
|
|
|
|
return m_alreadyUsedKeywordNames.contains(keywordName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void KeywordDialog::showError(const QString &text)
|
|
|
|
|
{
|
2022-07-11 16:59:51 +02:00
|
|
|
m_errorLabel->setText(text);
|
|
|
|
|
m_errorLabel->show();
|
2012-03-15 18:51:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString KeywordDialog::keywordName()
|
|
|
|
|
{
|
2022-07-11 16:59:51 +02:00
|
|
|
return m_keywordNameEdit->text().trimmed();
|
2012-03-15 18:51:52 +03:00
|
|
|
}
|
|
|
|
|
|
2011-10-25 23:14:27 +03:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Todo
|