26
26
** conditions contained in a signed written agreement between you and Nokia.
28
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at info@qt.nokia.com.
29
** Nokia at qt-info@nokia.com.
31
31
**************************************************************************/
33
33
#include "codestyleselectorwidget.h"
34
#include "ui_codestyleselectorwidget.h"
34
35
#include "icodestylepreferences.h"
35
36
#include "icodestylepreferencesfactory.h"
36
37
#include "codestylepool.h"
67
68
ICodeStylePreferences *codeStyle, QWidget *parent = 0);
68
69
~CodeStyleDialog();
69
70
ICodeStylePreferences *codeStyle() const;
70
QString displayName() const;
72
void slotCopyClicked();
73
void slotDisplayNameChanged();
72
75
ICodeStylePreferences *m_codeStyle;
73
76
QLineEdit *m_lineEdit;
77
QDialogButtonBox *m_buttons;
78
QLabel *m_warningLabel;
79
QPushButton *m_copyButton;
80
QString m_originalDisplayName;
76
83
CodeStyleDialog::CodeStyleDialog(ICodeStylePreferencesFactory *factory,
77
84
ICodeStylePreferences *codeStyle, QWidget *parent)
80
89
setWindowTitle(tr("Edit Code Style"));
81
90
QVBoxLayout *layout = new QVBoxLayout(this);
85
94
nameLayout->addWidget(label);
86
95
nameLayout->addWidget(m_lineEdit);
87
96
layout->addLayout(nameLayout);
98
if (codeStyle->isReadOnly()) {
99
QHBoxLayout *warningLayout = new QHBoxLayout();
100
m_warningLabel = new QLabel(
101
tr("You cannot save changes to a built-in code style. "
102
"Copy it first to create your own version."), this);
103
QFont font = m_warningLabel->font();
104
font.setItalic(true);
105
m_warningLabel->setFont(font);
106
m_warningLabel->setWordWrap(true);
107
m_copyButton = new QPushButton(tr("Copy Built-in Code Style"), this);
108
m_copyButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
109
connect(m_copyButton, SIGNAL(clicked()),
110
this, SLOT(slotCopyClicked()));
111
warningLayout->addWidget(m_warningLabel);
112
warningLayout->addWidget(m_copyButton);
113
layout->addLayout(warningLayout);
116
m_originalDisplayName = codeStyle->displayName();
88
117
m_codeStyle = factory->createCodeStyle();
89
118
m_codeStyle->setTabSettings(codeStyle->tabSettings());
90
119
m_codeStyle->setValue(codeStyle->value());
120
m_codeStyle->setDisplayName(m_originalDisplayName);
91
121
QWidget *editor = factory->createEditor(m_codeStyle, this);
92
QDialogButtonBox *buttons = new QDialogButtonBox(
123
m_buttons = new QDialogButtonBox(
93
124
QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
125
if (codeStyle->isReadOnly()) {
126
QPushButton *okButton = m_buttons->button(QDialogButtonBox::Ok);
127
okButton->setEnabled(false);
95
131
layout->addWidget(editor);
96
layout->addWidget(buttons);
97
connect(buttons, SIGNAL(accepted()), this, SLOT(accept()));
98
connect(buttons, SIGNAL(rejected()), this, SLOT(reject()));
132
layout->addWidget(m_buttons);
134
connect(m_lineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotDisplayNameChanged()));
135
connect(m_buttons, SIGNAL(accepted()), this, SLOT(accept()));
136
connect(m_buttons, SIGNAL(rejected()), this, SLOT(reject()));
101
139
ICodeStylePreferences *CodeStyleDialog::codeStyle() const
103
141
return m_codeStyle;
106
QString CodeStyleDialog::displayName() const
108
return m_lineEdit->text();
144
void CodeStyleDialog::slotCopyClicked()
147
m_warningLabel->hide();
149
m_copyButton->hide();
150
QPushButton *okButton = m_buttons->button(QDialogButtonBox::Ok);
151
okButton->setEnabled(true);
152
if (m_lineEdit->text() == m_originalDisplayName)
153
m_lineEdit->setText(tr("%1 (Copy)").arg(m_lineEdit->text()));
154
m_lineEdit->selectAll();
157
void CodeStyleDialog::slotDisplayNameChanged()
159
m_codeStyle->setDisplayName(m_lineEdit->text());
111
162
CodeStyleDialog::~CodeStyleDialog()
121
172
m_factory(factory),
174
m_ui(new Ui::CodeStyleSelectorWidget),
126
175
m_ignoreGuiSignals(false)
128
m_layout = new QHBoxLayout(this);
129
m_layout->setContentsMargins(QMargins());
130
m_copyButton = new QPushButton(tr("Copy..."), this);
131
m_editButton = new QPushButton(tr("Edit..."), this);
132
m_removeButton = new QPushButton(tr("Remove"), this);
133
m_importButton = new QPushButton(tr("Import..."), this);
134
m_exportButton = new QPushButton(tr("Export..."), this);
135
m_importButton->setEnabled(false);
136
m_exportButton->setEnabled(false);
178
m_ui->importButton->setEnabled(false);
179
m_ui->exportButton->setEnabled(false);
138
m_comboBoxLabel = new QLabel(tr("Current settings:"), this);
139
m_comboBoxLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
140
m_layout->addWidget(m_comboBoxLabel);
141
m_comboBox = new QComboBox(this);
142
m_comboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
143
m_layout->addWidget(m_comboBox);
144
connect(m_comboBox, SIGNAL(activated(int)),
181
connect(m_ui->delegateComboBox, SIGNAL(activated(int)),
145
182
this, SLOT(slotComboBoxActivated(int)));
147
m_layout->addWidget(m_copyButton);
148
m_layout->addWidget(m_editButton);
149
m_layout->addWidget(m_removeButton);
150
m_layout->addWidget(m_importButton);
151
m_layout->addWidget(m_exportButton);
153
connect(m_copyButton, SIGNAL(clicked()),
183
connect(m_ui->copyButton, SIGNAL(clicked()),
154
184
this, SLOT(slotCopyClicked()));
155
connect(m_editButton, SIGNAL(clicked()),
185
connect(m_ui->editButton, SIGNAL(clicked()),
156
186
this, SLOT(slotEditClicked()));
157
connect(m_removeButton, SIGNAL(clicked()),
187
connect(m_ui->removeButton, SIGNAL(clicked()),
158
188
this, SLOT(slotRemoveClicked()));
159
connect(m_importButton, SIGNAL(clicked()),
189
connect(m_ui->importButton, SIGNAL(clicked()),
160
190
this, SLOT(slotImportClicked()));
161
connect(m_exportButton, SIGNAL(clicked()),
191
connect(m_ui->exportButton, SIGNAL(clicked()),
162
192
this, SLOT(slotExportClicked()));
195
CodeStyleSelectorWidget::~CodeStyleSelectorWidget()
165
200
void CodeStyleSelectorWidget::setCodeStyle(TextEditor::ICodeStylePreferences *codeStyle)
167
202
if (m_codeStyle == codeStyle)
195
230
this, SLOT(slotCodeStyleAdded(ICodeStylePreferences*)));
196
231
connect(codeStylePool, SIGNAL(codeStyleRemoved(ICodeStylePreferences*)),
197
232
this, SLOT(slotCodeStyleRemoved(ICodeStylePreferences*)));
198
m_exportButton->setEnabled(true);
199
m_importButton->setEnabled(true);
233
m_ui->exportButton->setEnabled(true);
234
m_ui->importButton->setEnabled(true);
202
237
for (int i = 0; i < delegates.count(); i++)
227
262
void CodeStyleSelectorWidget::slotCurrentDelegateChanged(TextEditor::ICodeStylePreferences *delegate)
229
264
m_ignoreGuiSignals = true;
231
m_comboBox->setCurrentIndex(m_comboBox->findData(QVariant::fromValue(delegate)));
232
m_comboBox->setToolTip(m_comboBox->currentText());
265
m_ui->delegateComboBox->setCurrentIndex(m_ui->delegateComboBox->findData(QVariant::fromValue(delegate)));
266
m_ui->delegateComboBox->setToolTip(m_ui->delegateComboBox->currentText());
234
267
m_ignoreGuiSignals = false;
236
const bool enableEdit = delegate && !delegate->isReadOnly() && !delegate->currentDelegate();
237
m_editButton->setEnabled(enableEdit);
238
m_removeButton->setEnabled(enableEdit);
269
const bool removeEnabled = delegate && !delegate->isReadOnly() && !delegate->currentDelegate();
270
m_ui->removeButton->setEnabled(removeEnabled);
241
273
void CodeStyleSelectorWidget::slotCopyClicked()
271
303
Internal::CodeStyleDialog dialog(m_factory, codeStyle, this);
272
304
if (dialog.exec() == QDialog::Accepted) {
273
305
ICodeStylePreferences *dialogCodeStyle = dialog.codeStyle();
306
if (codeStyle->isReadOnly()) {
307
CodeStylePool *codeStylePool = m_codeStyle->delegatingPool();
308
codeStyle = codeStylePool->cloneCodeStyle(dialogCodeStyle);
310
m_codeStyle->setCurrentDelegate(codeStyle);
274
313
codeStyle->setTabSettings(dialogCodeStyle->tabSettings());
275
314
codeStyle->setValue(dialogCodeStyle->value());
276
codeStyle->setDisplayName(dialog.displayName());
315
codeStyle->setDisplayName(dialogCodeStyle->displayName());
338
377
const QVariant data = QVariant::fromValue(codeStylePreferences);
339
378
const QString name = displayName(codeStylePreferences);
340
m_comboBox->addItem(name, data);
341
m_comboBox->setItemData(m_comboBox->count() - 1, name, Qt::ToolTipRole);
379
m_ui->delegateComboBox->addItem(name, data);
380
m_ui->delegateComboBox->setItemData(m_ui->delegateComboBox->count() - 1, name, Qt::ToolTipRole);
342
381
connect(codeStylePreferences, SIGNAL(displayNameChanged(QString)),
343
382
this, SLOT(slotUpdateName()));
344
383
if (codeStylePreferences->delegatingPool()) {
375
414
updateName(codeStyle);
378
m_comboBox->setToolTip(m_comboBox->currentText());
417
m_ui->delegateComboBox->setToolTip(m_ui->delegateComboBox->currentText());
381
420
void CodeStyleSelectorWidget::updateName(ICodeStylePreferences *codeStyle)
383
const int idx = m_comboBox->findData(QVariant::fromValue(codeStyle));
422
const int idx = m_ui->delegateComboBox->findData(QVariant::fromValue(codeStyle));
387
426
const QString name = displayName(codeStyle);
388
m_comboBox->setItemText(idx, name);
389
m_comboBox->setItemData(idx, name, Qt::ToolTipRole);
427
m_ui->delegateComboBox->setItemText(idx, name);
428
m_ui->delegateComboBox->setItemData(idx, name, Qt::ToolTipRole);
392
431
QString CodeStyleSelectorWidget::displayName(ICodeStylePreferences *codeStyle) const