1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-23 20:53:54 +00:00
openmw/apps/launcher/utils/profilescombobox.cpp

98 lines
2.5 KiB
C++
Raw Normal View History

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