summaryrefslogtreecommitdiffstats
path: root/src/designer/src/components/propertyeditor/newdynamicpropertydialog.cpp
blob: 58058fef70322d490f39aedd9fd22707e8e34cdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: http://www.qt-project.org/
**
** This file is part of the Qt Designer of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "newdynamicpropertydialog.h"
#include "ui_newdynamicpropertydialog.h"
#include <abstractdialoggui_p.h>
#include <qdesigner_propertysheet_p.h>

#include <QtWidgets/QPushButton>

QT_BEGIN_NAMESPACE

namespace qdesigner_internal {

NewDynamicPropertyDialog::NewDynamicPropertyDialog(QDesignerDialogGuiInterface *dialogGui,
                                                       QWidget *parent)   :
    QDialog(parent),
    m_dialogGui(dialogGui),
    m_ui(new Ui::NewDynamicPropertyDialog)
{
    m_ui->setupUi(this);
    connect(m_ui->m_lineEdit, SIGNAL(textChanged(QString)), this, SLOT(nameChanged(QString)));

    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

    m_ui->m_comboBox->addItem(QStringLiteral("String"),      QVariant(QVariant::String));
    m_ui->m_comboBox->addItem(QStringLiteral("StringList"),  QVariant(QVariant::StringList));
    m_ui->m_comboBox->addItem(QStringLiteral("Char"),        QVariant(QVariant::Char));
    m_ui->m_comboBox->addItem(QStringLiteral("ByteArray"),   QVariant(QVariant::ByteArray));
    m_ui->m_comboBox->addItem(QStringLiteral("Url"),         QVariant(QVariant::Url));
    m_ui->m_comboBox->addItem(QStringLiteral("Bool"),        QVariant(QVariant::Bool));
    m_ui->m_comboBox->addItem(QStringLiteral("Int"),         QVariant(QVariant::Int));
    m_ui->m_comboBox->addItem(QStringLiteral("UInt"),        QVariant(QVariant::UInt));
    m_ui->m_comboBox->addItem(QStringLiteral("LongLong"),    QVariant(QVariant::LongLong));
    m_ui->m_comboBox->addItem(QStringLiteral("ULongLong"),   QVariant(QVariant::ULongLong));
    m_ui->m_comboBox->addItem(QStringLiteral("Double"),      QVariant(QVariant::Double));
    m_ui->m_comboBox->addItem(QStringLiteral("Size"),        QVariant(QVariant::Size));
    m_ui->m_comboBox->addItem(QStringLiteral("SizeF"),       QVariant(QVariant::SizeF));
    m_ui->m_comboBox->addItem(QStringLiteral("Point"),       QVariant(QVariant::Point));
    m_ui->m_comboBox->addItem(QStringLiteral("PointF"),      QVariant(QVariant::PointF));
    m_ui->m_comboBox->addItem(QStringLiteral("Rect"),        QVariant(QVariant::Rect));
    m_ui->m_comboBox->addItem(QStringLiteral("RectF"),       QVariant(QVariant::RectF));
    m_ui->m_comboBox->addItem(QStringLiteral("Date"),        QVariant(QVariant::Date));
    m_ui->m_comboBox->addItem(QStringLiteral("Time"),        QVariant(QVariant::Time));
    m_ui->m_comboBox->addItem(QStringLiteral("DateTime"),    QVariant(QVariant::DateTime));
    m_ui->m_comboBox->addItem(QStringLiteral("Font"),        QVariant(QVariant::Font));
    m_ui->m_comboBox->addItem(QStringLiteral("Palette"),     QVariant(QVariant::Palette));
    m_ui->m_comboBox->addItem(QStringLiteral("Color"),       QVariant(QVariant::Color));
    m_ui->m_comboBox->addItem(QStringLiteral("Pixmap"),      QVariant(QVariant::Pixmap));
    m_ui->m_comboBox->addItem(QStringLiteral("Icon"),        QVariant(QVariant::Icon));
    m_ui->m_comboBox->addItem(QStringLiteral("Cursor"),      QVariant(QVariant::Cursor));
    m_ui->m_comboBox->addItem(QStringLiteral("SizePolicy"),  QVariant(QVariant::SizePolicy));
    m_ui->m_comboBox->addItem(QStringLiteral("KeySequence"), QVariant(QVariant::KeySequence));

    m_ui->m_comboBox->setCurrentIndex(0); // String
    setOkButtonEnabled(false);
}

void NewDynamicPropertyDialog::setOkButtonEnabled(bool e)
{
    m_ui->m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(e);
}

NewDynamicPropertyDialog::~NewDynamicPropertyDialog()
{
    delete m_ui;
}

void NewDynamicPropertyDialog::setReservedNames(const QStringList &names)
{
    m_reservedNames = names;
}

void NewDynamicPropertyDialog::setPropertyType(QVariant::Type t)
{
    const int index = m_ui->m_comboBox->findData(QVariant(t));
    if (index != -1)
        m_ui->m_comboBox->setCurrentIndex(index);
}

QString NewDynamicPropertyDialog::propertyName() const
{
    return m_ui->m_lineEdit->text();
}

QVariant NewDynamicPropertyDialog::propertyValue() const
{
    const int index = m_ui->m_comboBox->currentIndex();
    if (index == -1)
        return QVariant();
    return m_ui->m_comboBox->itemData(index);
}

void NewDynamicPropertyDialog::information(const QString &message)
{
    m_dialogGui->message(this, QDesignerDialogGuiInterface::PropertyEditorMessage, QMessageBox::Information, tr("Set Property Name"), message);
}

void NewDynamicPropertyDialog::nameChanged(const QString &s)
{
    setOkButtonEnabled(!s.isEmpty());
}

bool NewDynamicPropertyDialog::validatePropertyName(const QString& name)
{
    if (m_reservedNames.contains(name)) {
        information(tr("The current object already has a property named '%1'.\nPlease select another, unique one.").arg(name));
        return false;
    }
    if (!QDesignerPropertySheet::internalDynamicPropertiesEnabled() && name.startsWith(QStringLiteral("_q_"))) {
        information(tr("The '_q_' prefix is reserved for the Qt library.\nPlease select another name."));
        return false;
    }
    return true;
}

void NewDynamicPropertyDialog::on_m_buttonBox_clicked(QAbstractButton *btn)
{
    const int role = m_ui->m_buttonBox->buttonRole(btn);
    switch (role) {
        case QDialogButtonBox::RejectRole:
            reject();
            break;
        case QDialogButtonBox::AcceptRole:
            if (validatePropertyName(propertyName()))
                accept();
            break;
    }
}
}

QT_END_NAMESPACE