2013-02-11 14:01:00 +00:00
|
|
|
#include <QApplication>
|
2012-10-22 23:47:07 +00:00
|
|
|
#include <QString>
|
|
|
|
|
|
|
|
#include "profilescombobox.hpp"
|
|
|
|
|
2013-10-23 02:52:35 +00:00
|
|
|
ProfilesComboBox::ProfilesComboBox(QWidget* parent)
|
|
|
|
: ContentSelectorView::ComboBox(parent)
|
2012-10-22 23:47:07 +00:00
|
|
|
{
|
2022-08-23 17:35:54 +00:00
|
|
|
connect(this, qOverload<int>(&ProfilesComboBox::activated), this, &ProfilesComboBox::slotIndexChangedByUser);
|
2013-02-11 14:01:00 +00:00
|
|
|
|
2013-02-15 00:20:48 +00:00
|
|
|
setInsertPolicy(QComboBox::NoInsert);
|
2012-10-22 23:47:07 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 02:52:35 +00:00
|
|
|
void ProfilesComboBox::setEditEnabled(bool editable)
|
2012-10-22 23:47:07 +00:00
|
|
|
{
|
2013-02-11 14:01:00 +00:00
|
|
|
if (isEditable() == editable)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!editable)
|
|
|
|
{
|
2022-08-23 17:35:54 +00:00
|
|
|
disconnect(lineEdit(), &QLineEdit::editingFinished, this, &ProfilesComboBox::slotEditingFinished);
|
|
|
|
disconnect(lineEdit(), &QLineEdit::textChanged, this, &ProfilesComboBox::slotTextChanged);
|
2012-10-22 23:47:07 +00:00
|
|
|
return setEditable(false);
|
2013-02-11 14:01:00 +00:00
|
|
|
}
|
2012-10-22 23:47:07 +00:00
|
|
|
|
|
|
|
// Reset the completer and validator
|
|
|
|
setEditable(true);
|
|
|
|
setValidator(mValidator);
|
2013-02-11 14:01:00 +00:00
|
|
|
|
2021-05-19 00:20:59 +00:00
|
|
|
auto* edit = new ComboBoxLineEdit(this);
|
2013-10-23 02:52:35 +00:00
|
|
|
|
2013-02-11 14:01:00 +00:00
|
|
|
setLineEdit(edit);
|
2020-11-13 07:39:47 +00:00
|
|
|
setCompleter(nullptr);
|
2013-02-11 14:01:00 +00:00
|
|
|
|
2022-08-23 17:35:54 +00:00
|
|
|
connect(lineEdit(), &QLineEdit::editingFinished, this, &ProfilesComboBox::slotEditingFinished);
|
2013-02-11 14:01:00 +00:00
|
|
|
|
2022-08-23 17:35:54 +00:00
|
|
|
connect(lineEdit(), &QLineEdit::textChanged, this, &ProfilesComboBox::slotTextChanged);
|
2013-10-02 02:29:45 +00:00
|
|
|
|
2022-08-23 17:35:54 +00:00
|
|
|
connect(lineEdit(), &QLineEdit::textChanged, this, &ProfilesComboBox::signalProfileTextChanged);
|
2012-10-22 23:47:07 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 02:52:35 +00:00
|
|
|
void ProfilesComboBox::slotTextChanged(const QString& text)
|
2012-10-22 23:47:07 +00:00
|
|
|
{
|
2014-12-23 19:44:25 +00:00
|
|
|
QPalette palette;
|
|
|
|
palette.setColor(QPalette::Text, Qt::red);
|
2013-02-11 14:01:00 +00:00
|
|
|
|
2013-02-15 00:20:48 +00:00
|
|
|
int index = findText(text);
|
2013-02-11 14:01:00 +00:00
|
|
|
|
2013-02-15 00:20:48 +00:00
|
|
|
if (text.isEmpty() || (index != -1 && index != currentIndex()))
|
|
|
|
{
|
2014-12-23 19:44:25 +00:00
|
|
|
lineEdit()->setPalette(palette);
|
2013-02-15 00:20:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lineEdit()->setPalette(QApplication::palette());
|
2013-02-11 14:01:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 02:52:35 +00:00
|
|
|
void ProfilesComboBox::slotEditingFinished()
|
2013-02-11 14:01:00 +00:00
|
|
|
{
|
2012-10-22 23:47:07 +00:00
|
|
|
QString current = currentText();
|
|
|
|
QString previous = itemText(currentIndex());
|
|
|
|
|
2013-02-15 13:12:25 +00:00
|
|
|
if (currentIndex() == -1)
|
|
|
|
return;
|
|
|
|
|
2013-02-11 14:01:00 +00:00
|
|
|
if (current.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (current == previous)
|
|
|
|
return;
|
|
|
|
|
2012-10-22 23:47:07 +00:00
|
|
|
if (findText(current) != -1)
|
|
|
|
return;
|
|
|
|
|
2013-02-15 13:12:25 +00:00
|
|
|
setItemText(currentIndex(), current);
|
2022-09-12 13:56:38 +00:00
|
|
|
emit profileRenamed(previous, current);
|
2012-10-22 23:47:07 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 02:52:35 +00:00
|
|
|
void ProfilesComboBox::slotIndexChangedByUser(int index)
|
2012-10-22 23:47:07 +00:00
|
|
|
{
|
|
|
|
if (index == -1)
|
|
|
|
return;
|
|
|
|
|
2022-09-12 13:56:38 +00:00
|
|
|
emit signalProfileChanged(mOldProfile, currentText());
|
2013-10-07 02:13:47 +00:00
|
|
|
mOldProfile = currentText();
|
2012-10-22 23:47:07 +00:00
|
|
|
}
|
2013-08-16 23:00:23 +00:00
|
|
|
|
2013-10-23 02:52:35 +00:00
|
|
|
ProfilesComboBox::ComboBoxLineEdit::ComboBoxLineEdit(QWidget* parent)
|
|
|
|
: LineEdit(parent)
|
2013-08-16 23:00:23 +00:00
|
|
|
{
|
2013-10-23 02:52:35 +00:00
|
|
|
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
2013-08-18 22:11:23 +00:00
|
|
|
|
2013-10-23 02:52:35 +00:00
|
|
|
setObjectName(QString("ComboBoxLineEdit"));
|
|
|
|
setStyleSheet(QString("ComboBoxLineEdit { background-color: transparent; padding-right: %1px; } ")
|
|
|
|
.arg(mClearButton->sizeHint().width() + frameWidth + 1));
|
2013-08-18 22:11:23 +00:00
|
|
|
}
|