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