The complete source code of IdealIRC http://www.idealirc.org/
 
 
 
 
idealirc/IConfig/ServerOptionsDelegate.cpp

105 lines
3.2 KiB

/*
* IdealIRC - Internet Relay Chat client
* Copyright (C) 2022 Tom-Andre Barstad.
* This software is licensed under the Software Attribution License.
* See LICENSE for more information.
*/
#include "ServerOptionsDelegate.h"
#include "ServerItem.h"
#include <QInputDialog>
#include <QPushButton>
#include <QDebug>
ServerOptionsDelegate::ServerOptionsDelegate(QObject* parent)
: QStyledItemDelegate(parent)
{}
QWidget* ServerOptionsDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
if (!index.isValid())
return nullptr;
if (index.column() == 2) {
auto* btn = new QPushButton(tr("SSL"), parent);
btn->setCheckable(true);
return btn;
}
else if (index.column() == 3) {
auto* item = static_cast<ServerItem*>(index.internalPointer());
auto* btn = new QPushButton(tr("Password"), parent);
connect(btn, &QPushButton::pressed,
[item, parent] {
auto password = QInputDialog::getText(
parent,
tr("Enter password"),
tr("Password for %1:").arg(item->name()),
QLineEdit::Password,
item->password()
);
item->setPassword(password);
});
return btn;
}
else if (index.column() == 4) {
auto* item = static_cast<ServerItem*>(index.internalPointer());
if (item->parent()) // Only items with a parent are network servers / child servers. No SASL for them.
return nullptr;
auto* btn = new QPushButton(tr("SASL"), parent);
connect(btn, &QPushButton::pressed,
[item, parent] {
auto password = QInputDialog::getText(
parent,
tr("SASL credentials"),
tr("SASL credentials for %1:").arg(item->name()),
QLineEdit::Password,
item->sasl()
);
item->setSasl(password);
});
return btn;
}
else {
return nullptr;
}
}
void ServerOptionsDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
const auto* item = static_cast<const ServerItem*>(index.internalPointer());
if (index.column() == 2) {
auto* btn = qobject_cast<QPushButton*>(editor);
if (btn)
btn->setChecked(item->ssl());
else
qCritical() << "SSL button is not a QPushButton?!";
}
}
void ServerOptionsDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
auto* item = static_cast<ServerItem*>(index.internalPointer());
if (index.column() == 2) {
auto* btn = qobject_cast<QPushButton*>(editor);
if (btn)
item->setSsl(btn->isChecked());
else
qCritical() << "SSL button is not a QPushButton?!";
}
}
void ServerOptionsDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& /*index*/) const
{
editor->setGeometry(option.rect);
}