~timo-jyrinki/ubuntu/saucy/qtcreator/add_workaround_back

« back to all changes in this revision

Viewing changes to src/plugins/texteditor/codestyleselectorwidget.cpp

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2011-11-18 16:18:49 UTC
  • mfrom: (1.1.16)
  • Revision ID: package-import@ubuntu.com-20111118161849-5t8jugl6egvs4iev
Tags: 2.4.0~rc-0ubuntu1
* New upstream release candidate.
* Drop 04_fix_ftbfs_arm_qreal.diff, merged upstream.
* Refresh 01_fix_installation_paths.diff.
* Compress binary packages with xz.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
**
5
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6
6
**
7
 
** Contact: Nokia Corporation (info@qt.nokia.com)
 
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8
8
**
9
9
**
10
10
** GNU Lesser General Public License Usage
26
26
** conditions contained in a signed written agreement between you and Nokia.
27
27
**
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.
30
30
**
31
31
**************************************************************************/
32
32
 
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;
 
71
private slots:
 
72
    void slotCopyClicked();
 
73
    void slotDisplayNameChanged();
71
74
private:
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;
74
81
};
75
82
 
76
83
CodeStyleDialog::CodeStyleDialog(ICodeStylePreferencesFactory *factory,
77
84
                ICodeStylePreferences *codeStyle, QWidget *parent)
78
 
    : QDialog(parent)
 
85
    : QDialog(parent),
 
86
      m_warningLabel(0),
 
87
      m_copyButton(0)
79
88
{
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);
 
97
 
 
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);
 
114
    }
 
115
 
 
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(
 
122
 
 
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);
 
128
    }
 
129
 
94
130
    if (editor)
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);
 
133
 
 
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()));
99
137
}
100
138
 
101
139
ICodeStylePreferences *CodeStyleDialog::codeStyle() const
103
141
    return m_codeStyle;
104
142
}
105
143
 
106
 
QString CodeStyleDialog::displayName() const
107
 
{
108
 
    return m_lineEdit->text();
 
144
void CodeStyleDialog::slotCopyClicked()
 
145
{
 
146
    if (m_warningLabel)
 
147
        m_warningLabel->hide();
 
148
    if (m_copyButton)
 
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();
 
155
}
 
156
 
 
157
void CodeStyleDialog::slotDisplayNameChanged()
 
158
{
 
159
    m_codeStyle->setDisplayName(m_lineEdit->text());
109
160
}
110
161
 
111
162
CodeStyleDialog::~CodeStyleDialog()
120
171
    QWidget(parent),
121
172
    m_factory(factory),
122
173
    m_codeStyle(0),
123
 
    m_layout(0),
124
 
    m_comboBox(0),
125
 
    m_comboBoxLabel(0),
 
174
    m_ui(new Ui::CodeStyleSelectorWidget),
126
175
    m_ignoreGuiSignals(false)
127
176
{
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);
 
177
    m_ui->setupUi(this);
 
178
    m_ui->importButton->setEnabled(false);
 
179
    m_ui->exportButton->setEnabled(false);
137
180
 
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)));
146
 
 
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);
152
 
 
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()));
163
193
}
164
194
 
 
195
CodeStyleSelectorWidget::~CodeStyleSelectorWidget()
 
196
{
 
197
    delete m_ui;
 
198
}
 
199
 
165
200
void CodeStyleSelectorWidget::setCodeStyle(TextEditor::ICodeStylePreferences *codeStyle)
166
201
{
167
202
    if (m_codeStyle == codeStyle)
179
214
        disconnect(m_codeStyle, SIGNAL(currentDelegateChanged(ICodeStylePreferences*)),
180
215
                this, SLOT(slotCurrentDelegateChanged(ICodeStylePreferences*)));
181
216
 
182
 
        m_exportButton->setEnabled(false);
183
 
        m_importButton->setEnabled(false);
184
 
        m_comboBox->clear();
 
217
        m_ui->exportButton->setEnabled(false);
 
218
        m_ui->importButton->setEnabled(false);
 
219
        m_ui->delegateComboBox->clear();
185
220
    }
186
221
    m_codeStyle = codeStyle;
187
222
    // fillup new
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);
200
235
        }
201
236
 
202
237
        for (int i = 0; i < delegates.count(); i++)
214
249
    if (m_ignoreGuiSignals)
215
250
        return;
216
251
 
217
 
    if (!m_comboBox || index < 0 || index >= m_comboBox->count())
 
252
    if (index < 0 || index >= m_ui->delegateComboBox->count())
218
253
        return;
219
254
    TextEditor::ICodeStylePreferences *delegate =
220
 
            m_comboBox->itemData(index).value<TextEditor::ICodeStylePreferences *>();
 
255
            m_ui->delegateComboBox->itemData(index).value<TextEditor::ICodeStylePreferences *>();
221
256
 
222
257
    const bool wasBlocked = blockSignals(true);
223
258
    m_codeStyle->setCurrentDelegate(delegate);
227
262
void CodeStyleSelectorWidget::slotCurrentDelegateChanged(TextEditor::ICodeStylePreferences *delegate)
228
263
{
229
264
    m_ignoreGuiSignals = true;
230
 
    if (m_comboBox) {
231
 
        m_comboBox->setCurrentIndex(m_comboBox->findData(QVariant::fromValue(delegate)));
232
 
        m_comboBox->setToolTip(m_comboBox->currentText());
233
 
    }
 
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;
235
268
 
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);
239
271
}
240
272
 
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);
 
309
            if (codeStyle)
 
310
                m_codeStyle->setCurrentDelegate(codeStyle);
 
311
            return;
 
312
        }
274
313
        codeStyle->setTabSettings(dialogCodeStyle->tabSettings());
275
314
        codeStyle->setValue(dialogCodeStyle->value());
276
 
        codeStyle->setDisplayName(dialog.displayName());
 
315
        codeStyle->setDisplayName(dialogCodeStyle->displayName());
277
316
    }
278
317
}
279
318
 
337
376
 
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()) {
350
389
void CodeStyleSelectorWidget::slotCodeStyleRemoved(ICodeStylePreferences *codeStylePreferences)
351
390
{
352
391
    m_ignoreGuiSignals = true;
353
 
    m_comboBox->removeItem(m_comboBox->findData(QVariant::fromValue(codeStylePreferences)));
 
392
    m_ui->delegateComboBox->removeItem(m_ui->delegateComboBox->findData(QVariant::fromValue(codeStylePreferences)));
354
393
    disconnect(codeStylePreferences, SIGNAL(displayNameChanged(QString)),
355
394
            this, SLOT(slotUpdateName()));
356
395
    if (codeStylePreferences->delegatingPool()) {
375
414
            updateName(codeStyle);
376
415
    }
377
416
 
378
 
    m_comboBox->setToolTip(m_comboBox->currentText());
 
417
    m_ui->delegateComboBox->setToolTip(m_ui->delegateComboBox->currentText());
379
418
}
380
419
 
381
420
void CodeStyleSelectorWidget::updateName(ICodeStylePreferences *codeStyle)
382
421
{
383
 
    const int idx = m_comboBox->findData(QVariant::fromValue(codeStyle));
 
422
    const int idx = m_ui->delegateComboBox->findData(QVariant::fromValue(codeStyle));
384
423
    if (idx < 0)
385
424
        return;
386
425
 
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);
390
429
}
391
430
 
392
431
QString CodeStyleSelectorWidget::displayName(ICodeStylePreferences *codeStyle) const