vnote/src/dialog/vnewdirdialog.cpp

59 lines
1.8 KiB
C++

#include <QtWidgets>
#include "vnewdirdialog.h"
VNewDirDialog::VNewDirDialog(const QString &title, const QString &info, const QString &name, const QString &defaultName,
QWidget *parent)
: QDialog(parent), title(title), info(info), name(name), defaultName(defaultName)
{
setupUI();
connect(nameEdit, &QLineEdit::textChanged, this, &VNewDirDialog::enableOkButton);
}
void VNewDirDialog::setupUI()
{
QLabel *infoLabel = NULL;
if (!info.isEmpty()) {
infoLabel = new QLabel(info);
infoLabel->setWordWrap(true);
}
nameLabel = new QLabel(name);
nameEdit = new QLineEdit(defaultName);
nameEdit->selectAll();
nameLabel->setBuddy(nameEdit);
// Ok is the default button.
m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
QHBoxLayout *topLayout = new QHBoxLayout();
topLayout->addWidget(nameLabel);
topLayout->addWidget(nameEdit);
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
nameEdit->setMinimumWidth(okBtn->sizeHint().width() * 3);
QVBoxLayout *mainLayout = new QVBoxLayout();
if (infoLabel) {
mainLayout->addWidget(infoLabel);
}
mainLayout->addLayout(topLayout);
mainLayout->addWidget(m_btnBox);
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
setLayout(mainLayout);
setWindowTitle(title);
}
void VNewDirDialog::enableOkButton(const QString &editText)
{
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
okBtn->setEnabled(!editText.isEmpty());
}
QString VNewDirDialog::getNameInput() const
{
return nameEdit->text();
}